2 * net/core/devlink.c - Network physical/parent device Netlink interface
4 * Heavily inspired by net/wireless/
5 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/gfp.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/netdevice.h>
22 #include <rdma/ib_verbs.h>
23 #include <net/netlink.h>
24 #include <net/genetlink.h>
25 #include <net/rtnetlink.h>
26 #include <net/net_namespace.h>
28 #include <net/devlink.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/devlink.h>
32 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
34 .name = "destination mac",
35 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
40 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
42 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
43 .fields = devlink_dpipe_fields_ethernet,
44 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
47 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
49 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
51 .name = "destination ip",
52 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
57 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
59 .id = DEVLINK_DPIPE_HEADER_IPV4,
60 .fields = devlink_dpipe_fields_ipv4,
61 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
64 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
66 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
68 .name = "destination ip",
69 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
74 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
76 .id = DEVLINK_DPIPE_HEADER_IPV6,
77 .fields = devlink_dpipe_fields_ipv6,
78 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
81 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
83 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
84 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
86 static LIST_HEAD(devlink_list);
90 * An overall lock guarding every operation coming from userspace.
91 * It also guards devlink devices list and it is taken when
92 * driver registers/unregisters it.
94 static DEFINE_MUTEX(devlink_mutex);
96 static struct net *devlink_net(const struct devlink *devlink)
98 return read_pnet(&devlink->_net);
101 static void devlink_net_set(struct devlink *devlink, struct net *net)
103 write_pnet(&devlink->_net, net);
106 static struct devlink *devlink_get_from_attrs(struct net *net,
107 struct nlattr **attrs)
109 struct devlink *devlink;
113 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
114 return ERR_PTR(-EINVAL);
116 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
117 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
119 lockdep_assert_held(&devlink_mutex);
121 list_for_each_entry(devlink, &devlink_list, list) {
122 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
123 strcmp(dev_name(devlink->dev), devname) == 0 &&
124 net_eq(devlink_net(devlink), net))
128 return ERR_PTR(-ENODEV);
131 static struct devlink *devlink_get_from_info(struct genl_info *info)
133 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
136 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
139 struct devlink_port *devlink_port;
141 list_for_each_entry(devlink_port, &devlink->port_list, list) {
142 if (devlink_port->index == port_index)
148 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
150 return devlink_port_get_by_index(devlink, port_index);
153 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
154 struct nlattr **attrs)
156 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
157 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
158 struct devlink_port *devlink_port;
160 devlink_port = devlink_port_get_by_index(devlink, port_index);
162 return ERR_PTR(-ENODEV);
165 return ERR_PTR(-EINVAL);
168 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
169 struct genl_info *info)
171 return devlink_port_get_from_attrs(devlink, info->attrs);
175 struct list_head list;
178 u16 ingress_pools_count;
179 u16 egress_pools_count;
180 u16 ingress_tc_count;
184 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
186 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
189 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
190 unsigned int sb_index)
192 struct devlink_sb *devlink_sb;
194 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
195 if (devlink_sb->index == sb_index)
201 static bool devlink_sb_index_exists(struct devlink *devlink,
202 unsigned int sb_index)
204 return devlink_sb_get_by_index(devlink, sb_index);
207 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
208 struct nlattr **attrs)
210 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
211 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
212 struct devlink_sb *devlink_sb;
214 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
216 return ERR_PTR(-ENODEV);
219 return ERR_PTR(-EINVAL);
222 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
223 struct genl_info *info)
225 return devlink_sb_get_from_attrs(devlink, info->attrs);
228 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
229 struct nlattr **attrs,
234 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
237 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
238 if (val >= devlink_sb_pool_count(devlink_sb))
244 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
245 struct genl_info *info,
248 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
253 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
254 enum devlink_sb_pool_type *p_pool_type)
258 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
261 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
262 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
263 val != DEVLINK_SB_POOL_TYPE_EGRESS)
270 devlink_sb_pool_type_get_from_info(struct genl_info *info,
271 enum devlink_sb_pool_type *p_pool_type)
273 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
277 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
278 enum devlink_sb_threshold_type *p_th_type)
282 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
285 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
286 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
287 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
294 devlink_sb_th_type_get_from_info(struct genl_info *info,
295 enum devlink_sb_threshold_type *p_th_type)
297 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
301 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
302 struct nlattr **attrs,
303 enum devlink_sb_pool_type pool_type,
308 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
311 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
312 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
313 val >= devlink_sb->ingress_tc_count)
315 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
316 val >= devlink_sb->egress_tc_count)
323 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
324 struct genl_info *info,
325 enum devlink_sb_pool_type pool_type,
328 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
329 pool_type, p_tc_index);
332 struct devlink_region {
333 struct devlink *devlink;
334 struct list_head list;
336 struct list_head snapshot_list;
342 struct devlink_snapshot {
343 struct list_head list;
344 struct devlink_region *region;
345 devlink_snapshot_data_dest_t *data_destructor;
351 static struct devlink_region *
352 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
354 struct devlink_region *region;
356 list_for_each_entry(region, &devlink->region_list, list)
357 if (!strcmp(region->name, region_name))
363 static struct devlink_snapshot *
364 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
366 struct devlink_snapshot *snapshot;
368 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
369 if (snapshot->id == id)
375 static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
377 snapshot->region->cur_snapshots--;
378 list_del(&snapshot->list);
379 (*snapshot->data_destructor)(snapshot->data);
383 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
384 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
385 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
387 /* The per devlink instance lock is taken by default in the pre-doit
388 * operation, yet several commands do not require this. The global
389 * devlink lock is taken and protects from disruption by user-calls.
391 #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
393 static int devlink_nl_pre_doit(const struct genl_ops *ops,
394 struct sk_buff *skb, struct genl_info *info)
396 struct devlink *devlink;
399 mutex_lock(&devlink_mutex);
400 devlink = devlink_get_from_info(info);
401 if (IS_ERR(devlink)) {
402 mutex_unlock(&devlink_mutex);
403 return PTR_ERR(devlink);
405 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
406 mutex_lock(&devlink->lock);
407 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
408 info->user_ptr[0] = devlink;
409 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
410 struct devlink_port *devlink_port;
412 devlink_port = devlink_port_get_from_info(devlink, info);
413 if (IS_ERR(devlink_port)) {
414 err = PTR_ERR(devlink_port);
417 info->user_ptr[0] = devlink_port;
419 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
420 struct devlink_sb *devlink_sb;
422 devlink_sb = devlink_sb_get_from_info(devlink, info);
423 if (IS_ERR(devlink_sb)) {
424 err = PTR_ERR(devlink_sb);
427 info->user_ptr[1] = devlink_sb;
432 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
433 mutex_unlock(&devlink->lock);
434 mutex_unlock(&devlink_mutex);
438 static void devlink_nl_post_doit(const struct genl_ops *ops,
439 struct sk_buff *skb, struct genl_info *info)
441 struct devlink *devlink;
443 devlink = devlink_get_from_info(info);
444 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
445 mutex_unlock(&devlink->lock);
446 mutex_unlock(&devlink_mutex);
449 static struct genl_family devlink_nl_family;
451 enum devlink_multicast_groups {
452 DEVLINK_MCGRP_CONFIG,
455 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
456 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
459 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
461 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
463 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
468 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
469 enum devlink_command cmd, u32 portid,
474 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
478 if (devlink_nl_put_handle(msg, devlink))
479 goto nla_put_failure;
481 genlmsg_end(msg, hdr);
485 genlmsg_cancel(msg, hdr);
489 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
494 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
496 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
500 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
506 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
507 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
510 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
511 struct devlink_port *devlink_port)
513 struct devlink_port_attrs *attrs = &devlink_port->attrs;
517 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
519 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
523 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
525 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
526 attrs->split_subport_number))
531 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
532 struct devlink_port *devlink_port,
533 enum devlink_command cmd, u32 portid,
538 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
542 if (devlink_nl_put_handle(msg, devlink))
543 goto nla_put_failure;
544 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
545 goto nla_put_failure;
546 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
547 goto nla_put_failure;
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;
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;
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;
570 if (devlink_nl_port_attrs_put(msg, devlink_port))
571 goto nla_put_failure;
573 genlmsg_end(msg, hdr);
577 genlmsg_cancel(msg, hdr);
581 static void devlink_port_notify(struct devlink_port *devlink_port,
582 enum devlink_command cmd)
584 struct devlink *devlink = devlink_port->devlink;
588 if (!devlink_port->registered)
591 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
593 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
597 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
603 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
604 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
607 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
609 struct devlink *devlink = info->user_ptr[0];
613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
617 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
618 info->snd_portid, info->snd_seq, 0);
624 return genlmsg_reply(msg, info);
627 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
628 struct netlink_callback *cb)
630 struct devlink *devlink;
631 int start = cb->args[0];
635 mutex_lock(&devlink_mutex);
636 list_for_each_entry(devlink, &devlink_list, list) {
637 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
643 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
644 NETLINK_CB(cb->skb).portid,
645 cb->nlh->nlmsg_seq, NLM_F_MULTI);
651 mutex_unlock(&devlink_mutex);
657 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
658 struct genl_info *info)
660 struct devlink_port *devlink_port = info->user_ptr[0];
661 struct devlink *devlink = devlink_port->devlink;
665 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
669 err = devlink_nl_port_fill(msg, devlink, devlink_port,
670 DEVLINK_CMD_PORT_NEW,
671 info->snd_portid, info->snd_seq, 0);
677 return genlmsg_reply(msg, info);
680 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
681 struct netlink_callback *cb)
683 struct devlink *devlink;
684 struct devlink_port *devlink_port;
685 int start = cb->args[0];
689 mutex_lock(&devlink_mutex);
690 list_for_each_entry(devlink, &devlink_list, list) {
691 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
693 mutex_lock(&devlink->lock);
694 list_for_each_entry(devlink_port, &devlink->port_list, list) {
699 err = devlink_nl_port_fill(msg, devlink, devlink_port,
701 NETLINK_CB(cb->skb).portid,
705 mutex_unlock(&devlink->lock);
710 mutex_unlock(&devlink->lock);
713 mutex_unlock(&devlink_mutex);
719 static int devlink_port_type_set(struct devlink *devlink,
720 struct devlink_port *devlink_port,
721 enum devlink_port_type port_type)
726 if (devlink->ops->port_type_set) {
727 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
729 if (port_type == devlink_port->type)
731 err = devlink->ops->port_type_set(devlink_port, port_type);
734 devlink_port->desired_type = port_type;
735 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
741 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
742 struct genl_info *info)
744 struct devlink_port *devlink_port = info->user_ptr[0];
745 struct devlink *devlink = devlink_port->devlink;
748 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
749 enum devlink_port_type port_type;
751 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
752 err = devlink_port_type_set(devlink, devlink_port, port_type);
759 static int devlink_port_split(struct devlink *devlink, u32 port_index,
760 u32 count, struct netlink_ext_ack *extack)
763 if (devlink->ops->port_split)
764 return devlink->ops->port_split(devlink, port_index, count,
769 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
770 struct genl_info *info)
772 struct devlink *devlink = info->user_ptr[0];
776 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
777 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
780 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
781 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
782 return devlink_port_split(devlink, port_index, count, info->extack);
785 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
786 struct netlink_ext_ack *extack)
789 if (devlink->ops->port_unsplit)
790 return devlink->ops->port_unsplit(devlink, port_index, extack);
794 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
795 struct genl_info *info)
797 struct devlink *devlink = info->user_ptr[0];
800 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
803 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
804 return devlink_port_unsplit(devlink, port_index, info->extack);
807 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
808 struct devlink_sb *devlink_sb,
809 enum devlink_command cmd, u32 portid,
814 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
818 if (devlink_nl_put_handle(msg, devlink))
819 goto nla_put_failure;
820 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
821 goto nla_put_failure;
822 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
823 goto nla_put_failure;
824 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
825 devlink_sb->ingress_pools_count))
826 goto nla_put_failure;
827 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
828 devlink_sb->egress_pools_count))
829 goto nla_put_failure;
830 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
831 devlink_sb->ingress_tc_count))
832 goto nla_put_failure;
833 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
834 devlink_sb->egress_tc_count))
835 goto nla_put_failure;
837 genlmsg_end(msg, hdr);
841 genlmsg_cancel(msg, hdr);
845 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
846 struct genl_info *info)
848 struct devlink *devlink = info->user_ptr[0];
849 struct devlink_sb *devlink_sb = info->user_ptr[1];
853 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
857 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
859 info->snd_portid, info->snd_seq, 0);
865 return genlmsg_reply(msg, info);
868 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
869 struct netlink_callback *cb)
871 struct devlink *devlink;
872 struct devlink_sb *devlink_sb;
873 int start = cb->args[0];
877 mutex_lock(&devlink_mutex);
878 list_for_each_entry(devlink, &devlink_list, list) {
879 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
881 mutex_lock(&devlink->lock);
882 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
887 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
889 NETLINK_CB(cb->skb).portid,
893 mutex_unlock(&devlink->lock);
898 mutex_unlock(&devlink->lock);
901 mutex_unlock(&devlink_mutex);
907 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
908 struct devlink_sb *devlink_sb,
909 u16 pool_index, enum devlink_command cmd,
910 u32 portid, u32 seq, int flags)
912 struct devlink_sb_pool_info pool_info;
916 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
917 pool_index, &pool_info);
921 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
925 if (devlink_nl_put_handle(msg, devlink))
926 goto nla_put_failure;
927 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
928 goto nla_put_failure;
929 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
930 goto nla_put_failure;
931 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
932 goto nla_put_failure;
933 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
934 goto nla_put_failure;
935 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
936 pool_info.threshold_type))
937 goto nla_put_failure;
938 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
939 pool_info.cell_size))
940 goto nla_put_failure;
942 genlmsg_end(msg, hdr);
946 genlmsg_cancel(msg, hdr);
950 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
951 struct genl_info *info)
953 struct devlink *devlink = info->user_ptr[0];
954 struct devlink_sb *devlink_sb = info->user_ptr[1];
959 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
964 if (!devlink->ops->sb_pool_get)
967 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
971 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
972 DEVLINK_CMD_SB_POOL_NEW,
973 info->snd_portid, info->snd_seq, 0);
979 return genlmsg_reply(msg, info);
982 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
983 struct devlink *devlink,
984 struct devlink_sb *devlink_sb,
987 u16 pool_count = devlink_sb_pool_count(devlink_sb);
991 for (pool_index = 0; pool_index < pool_count; pool_index++) {
992 if (*p_idx < start) {
996 err = devlink_nl_sb_pool_fill(msg, devlink,
999 DEVLINK_CMD_SB_POOL_NEW,
1000 portid, seq, NLM_F_MULTI);
1008 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1009 struct netlink_callback *cb)
1011 struct devlink *devlink;
1012 struct devlink_sb *devlink_sb;
1013 int start = cb->args[0];
1017 mutex_lock(&devlink_mutex);
1018 list_for_each_entry(devlink, &devlink_list, list) {
1019 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1020 !devlink->ops->sb_pool_get)
1022 mutex_lock(&devlink->lock);
1023 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1024 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1026 NETLINK_CB(cb->skb).portid,
1027 cb->nlh->nlmsg_seq);
1028 if (err && err != -EOPNOTSUPP) {
1029 mutex_unlock(&devlink->lock);
1033 mutex_unlock(&devlink->lock);
1036 mutex_unlock(&devlink_mutex);
1042 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1043 u16 pool_index, u32 size,
1044 enum devlink_sb_threshold_type threshold_type)
1047 const struct devlink_ops *ops = devlink->ops;
1049 if (ops->sb_pool_set)
1050 return ops->sb_pool_set(devlink, sb_index, pool_index,
1051 size, threshold_type);
1055 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1056 struct genl_info *info)
1058 struct devlink *devlink = info->user_ptr[0];
1059 struct devlink_sb *devlink_sb = info->user_ptr[1];
1060 enum devlink_sb_threshold_type threshold_type;
1065 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1070 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1074 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1077 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1078 return devlink_sb_pool_set(devlink, devlink_sb->index,
1079 pool_index, size, threshold_type);
1082 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1083 struct devlink *devlink,
1084 struct devlink_port *devlink_port,
1085 struct devlink_sb *devlink_sb,
1087 enum devlink_command cmd,
1088 u32 portid, u32 seq, int flags)
1090 const struct devlink_ops *ops = devlink->ops;
1095 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1096 pool_index, &threshold);
1100 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1104 if (devlink_nl_put_handle(msg, devlink))
1105 goto nla_put_failure;
1106 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1107 goto nla_put_failure;
1108 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1109 goto nla_put_failure;
1110 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1111 goto nla_put_failure;
1112 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1113 goto nla_put_failure;
1115 if (ops->sb_occ_port_pool_get) {
1119 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1120 pool_index, &cur, &max);
1121 if (err && err != -EOPNOTSUPP)
1124 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1125 goto nla_put_failure;
1126 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1127 goto nla_put_failure;
1131 genlmsg_end(msg, hdr);
1135 genlmsg_cancel(msg, hdr);
1139 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1140 struct genl_info *info)
1142 struct devlink_port *devlink_port = info->user_ptr[0];
1143 struct devlink *devlink = devlink_port->devlink;
1144 struct devlink_sb *devlink_sb = info->user_ptr[1];
1145 struct sk_buff *msg;
1149 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1154 if (!devlink->ops->sb_port_pool_get)
1157 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1161 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1162 devlink_sb, pool_index,
1163 DEVLINK_CMD_SB_PORT_POOL_NEW,
1164 info->snd_portid, info->snd_seq, 0);
1170 return genlmsg_reply(msg, info);
1173 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1174 struct devlink *devlink,
1175 struct devlink_sb *devlink_sb,
1176 u32 portid, u32 seq)
1178 struct devlink_port *devlink_port;
1179 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1183 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1184 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1185 if (*p_idx < start) {
1189 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1193 DEVLINK_CMD_SB_PORT_POOL_NEW,
1204 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1205 struct netlink_callback *cb)
1207 struct devlink *devlink;
1208 struct devlink_sb *devlink_sb;
1209 int start = cb->args[0];
1213 mutex_lock(&devlink_mutex);
1214 list_for_each_entry(devlink, &devlink_list, list) {
1215 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1216 !devlink->ops->sb_port_pool_get)
1218 mutex_lock(&devlink->lock);
1219 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1220 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1221 devlink, devlink_sb,
1222 NETLINK_CB(cb->skb).portid,
1223 cb->nlh->nlmsg_seq);
1224 if (err && err != -EOPNOTSUPP) {
1225 mutex_unlock(&devlink->lock);
1229 mutex_unlock(&devlink->lock);
1232 mutex_unlock(&devlink_mutex);
1238 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1239 unsigned int sb_index, u16 pool_index,
1243 const struct devlink_ops *ops = devlink_port->devlink->ops;
1245 if (ops->sb_port_pool_set)
1246 return ops->sb_port_pool_set(devlink_port, sb_index,
1247 pool_index, threshold);
1251 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1252 struct genl_info *info)
1254 struct devlink_port *devlink_port = info->user_ptr[0];
1255 struct devlink_sb *devlink_sb = info->user_ptr[1];
1260 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1265 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1268 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1269 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1270 pool_index, threshold);
1274 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1275 struct devlink_port *devlink_port,
1276 struct devlink_sb *devlink_sb, u16 tc_index,
1277 enum devlink_sb_pool_type pool_type,
1278 enum devlink_command cmd,
1279 u32 portid, u32 seq, int flags)
1281 const struct devlink_ops *ops = devlink->ops;
1287 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1288 tc_index, pool_type,
1289 &pool_index, &threshold);
1293 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1297 if (devlink_nl_put_handle(msg, devlink))
1298 goto nla_put_failure;
1299 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1300 goto nla_put_failure;
1301 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1302 goto nla_put_failure;
1303 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1304 goto nla_put_failure;
1305 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1306 goto nla_put_failure;
1307 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1308 goto nla_put_failure;
1309 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1310 goto nla_put_failure;
1312 if (ops->sb_occ_tc_port_bind_get) {
1316 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1318 tc_index, pool_type,
1320 if (err && err != -EOPNOTSUPP)
1323 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1324 goto nla_put_failure;
1325 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1326 goto nla_put_failure;
1330 genlmsg_end(msg, hdr);
1334 genlmsg_cancel(msg, hdr);
1338 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1339 struct genl_info *info)
1341 struct devlink_port *devlink_port = info->user_ptr[0];
1342 struct devlink *devlink = devlink_port->devlink;
1343 struct devlink_sb *devlink_sb = info->user_ptr[1];
1344 struct sk_buff *msg;
1345 enum devlink_sb_pool_type pool_type;
1349 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1353 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1354 pool_type, &tc_index);
1358 if (!devlink->ops->sb_tc_pool_bind_get)
1361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1365 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1366 devlink_sb, tc_index, pool_type,
1367 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1375 return genlmsg_reply(msg, info);
1378 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1379 int start, int *p_idx,
1380 struct devlink *devlink,
1381 struct devlink_sb *devlink_sb,
1382 u32 portid, u32 seq)
1384 struct devlink_port *devlink_port;
1388 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1390 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1391 if (*p_idx < start) {
1395 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1399 DEVLINK_SB_POOL_TYPE_INGRESS,
1400 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1408 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1409 if (*p_idx < start) {
1413 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1417 DEVLINK_SB_POOL_TYPE_EGRESS,
1418 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1430 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1431 struct netlink_callback *cb)
1433 struct devlink *devlink;
1434 struct devlink_sb *devlink_sb;
1435 int start = cb->args[0];
1439 mutex_lock(&devlink_mutex);
1440 list_for_each_entry(devlink, &devlink_list, list) {
1441 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1442 !devlink->ops->sb_tc_pool_bind_get)
1445 mutex_lock(&devlink->lock);
1446 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1447 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1450 NETLINK_CB(cb->skb).portid,
1451 cb->nlh->nlmsg_seq);
1452 if (err && err != -EOPNOTSUPP) {
1453 mutex_unlock(&devlink->lock);
1457 mutex_unlock(&devlink->lock);
1460 mutex_unlock(&devlink_mutex);
1466 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1467 unsigned int sb_index, u16 tc_index,
1468 enum devlink_sb_pool_type pool_type,
1469 u16 pool_index, u32 threshold)
1472 const struct devlink_ops *ops = devlink_port->devlink->ops;
1474 if (ops->sb_tc_pool_bind_set)
1475 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1476 tc_index, pool_type,
1477 pool_index, threshold);
1481 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1482 struct genl_info *info)
1484 struct devlink_port *devlink_port = info->user_ptr[0];
1485 struct devlink_sb *devlink_sb = info->user_ptr[1];
1486 enum devlink_sb_pool_type pool_type;
1492 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1496 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1497 pool_type, &tc_index);
1501 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1506 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1509 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1510 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1511 tc_index, pool_type,
1512 pool_index, threshold);
1515 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1516 struct genl_info *info)
1518 struct devlink *devlink = info->user_ptr[0];
1519 struct devlink_sb *devlink_sb = info->user_ptr[1];
1520 const struct devlink_ops *ops = devlink->ops;
1522 if (ops->sb_occ_snapshot)
1523 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1527 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1528 struct genl_info *info)
1530 struct devlink *devlink = info->user_ptr[0];
1531 struct devlink_sb *devlink_sb = info->user_ptr[1];
1532 const struct devlink_ops *ops = devlink->ops;
1534 if (ops->sb_occ_max_clear)
1535 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1539 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1540 enum devlink_command cmd, u32 portid,
1543 const struct devlink_ops *ops = devlink->ops;
1544 u8 inline_mode, encap_mode;
1549 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1553 err = devlink_nl_put_handle(msg, devlink);
1555 goto nla_put_failure;
1557 if (ops->eswitch_mode_get) {
1558 err = ops->eswitch_mode_get(devlink, &mode);
1560 goto nla_put_failure;
1561 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1563 goto nla_put_failure;
1566 if (ops->eswitch_inline_mode_get) {
1567 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1569 goto nla_put_failure;
1570 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1573 goto nla_put_failure;
1576 if (ops->eswitch_encap_mode_get) {
1577 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1579 goto nla_put_failure;
1580 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1582 goto nla_put_failure;
1585 genlmsg_end(msg, hdr);
1589 genlmsg_cancel(msg, hdr);
1593 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1594 struct genl_info *info)
1596 struct devlink *devlink = info->user_ptr[0];
1597 struct sk_buff *msg;
1600 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1604 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1605 info->snd_portid, info->snd_seq, 0);
1612 return genlmsg_reply(msg, info);
1615 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1616 struct genl_info *info)
1618 struct devlink *devlink = info->user_ptr[0];
1619 const struct devlink_ops *ops = devlink->ops;
1620 u8 inline_mode, encap_mode;
1624 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1625 if (!ops->eswitch_mode_set)
1627 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1628 err = ops->eswitch_mode_set(devlink, mode, info->extack);
1633 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1634 if (!ops->eswitch_inline_mode_set)
1636 inline_mode = nla_get_u8(
1637 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1638 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1644 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1645 if (!ops->eswitch_encap_mode_set)
1647 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1648 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1657 int devlink_dpipe_match_put(struct sk_buff *skb,
1658 struct devlink_dpipe_match *match)
1660 struct devlink_dpipe_header *header = match->header;
1661 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1662 struct nlattr *match_attr;
1664 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1668 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1669 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1670 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1671 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1672 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1673 goto nla_put_failure;
1675 nla_nest_end(skb, match_attr);
1679 nla_nest_cancel(skb, match_attr);
1682 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1684 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1685 struct sk_buff *skb)
1687 struct nlattr *matches_attr;
1689 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1693 if (table->table_ops->matches_dump(table->priv, skb))
1694 goto nla_put_failure;
1696 nla_nest_end(skb, matches_attr);
1700 nla_nest_cancel(skb, matches_attr);
1704 int devlink_dpipe_action_put(struct sk_buff *skb,
1705 struct devlink_dpipe_action *action)
1707 struct devlink_dpipe_header *header = action->header;
1708 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1709 struct nlattr *action_attr;
1711 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1715 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1716 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1717 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1718 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1719 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1720 goto nla_put_failure;
1722 nla_nest_end(skb, action_attr);
1726 nla_nest_cancel(skb, action_attr);
1729 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1731 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1732 struct sk_buff *skb)
1734 struct nlattr *actions_attr;
1736 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1740 if (table->table_ops->actions_dump(table->priv, skb))
1741 goto nla_put_failure;
1743 nla_nest_end(skb, actions_attr);
1747 nla_nest_cancel(skb, actions_attr);
1751 static int devlink_dpipe_table_put(struct sk_buff *skb,
1752 struct devlink_dpipe_table *table)
1754 struct nlattr *table_attr;
1757 table_size = table->table_ops->size_get(table->priv);
1758 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1762 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1763 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1765 goto nla_put_failure;
1766 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1767 table->counters_enabled))
1768 goto nla_put_failure;
1770 if (table->resource_valid) {
1771 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1772 table->resource_id, DEVLINK_ATTR_PAD) ||
1773 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1774 table->resource_units, DEVLINK_ATTR_PAD))
1775 goto nla_put_failure;
1777 if (devlink_dpipe_matches_put(table, skb))
1778 goto nla_put_failure;
1780 if (devlink_dpipe_actions_put(table, skb))
1781 goto nla_put_failure;
1783 nla_nest_end(skb, table_attr);
1787 nla_nest_cancel(skb, table_attr);
1791 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1792 struct genl_info *info)
1797 err = genlmsg_reply(*pskb, info);
1801 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1807 static int devlink_dpipe_tables_fill(struct genl_info *info,
1808 enum devlink_command cmd, int flags,
1809 struct list_head *dpipe_tables,
1810 const char *table_name)
1812 struct devlink *devlink = info->user_ptr[0];
1813 struct devlink_dpipe_table *table;
1814 struct nlattr *tables_attr;
1815 struct sk_buff *skb = NULL;
1816 struct nlmsghdr *nlh;
1822 table = list_first_entry(dpipe_tables,
1823 struct devlink_dpipe_table, list);
1825 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1829 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1830 &devlink_nl_family, NLM_F_MULTI, cmd);
1836 if (devlink_nl_put_handle(skb, devlink))
1837 goto nla_put_failure;
1838 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1840 goto nla_put_failure;
1844 list_for_each_entry_from(table, dpipe_tables, list) {
1846 err = devlink_dpipe_table_put(skb, table);
1854 if (!strcmp(table->name, table_name)) {
1855 err = devlink_dpipe_table_put(skb, table);
1863 nla_nest_end(skb, tables_attr);
1864 genlmsg_end(skb, hdr);
1869 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1870 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1872 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1878 return genlmsg_reply(skb, info);
1887 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1888 struct genl_info *info)
1890 struct devlink *devlink = info->user_ptr[0];
1891 const char *table_name = NULL;
1893 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1894 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1896 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1897 &devlink->dpipe_table_list,
1901 static int devlink_dpipe_value_put(struct sk_buff *skb,
1902 struct devlink_dpipe_value *value)
1904 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1905 value->value_size, value->value))
1908 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1909 value->value_size, value->mask))
1911 if (value->mapping_valid)
1912 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1913 value->mapping_value))
1918 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1919 struct devlink_dpipe_value *value)
1923 if (devlink_dpipe_action_put(skb, value->action))
1925 if (devlink_dpipe_value_put(skb, value))
1930 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1931 struct devlink_dpipe_value *values,
1932 unsigned int values_count)
1934 struct nlattr *action_attr;
1938 for (i = 0; i < values_count; i++) {
1939 action_attr = nla_nest_start(skb,
1940 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1943 err = devlink_dpipe_action_value_put(skb, &values[i]);
1945 goto err_action_value_put;
1946 nla_nest_end(skb, action_attr);
1950 err_action_value_put:
1951 nla_nest_cancel(skb, action_attr);
1955 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1956 struct devlink_dpipe_value *value)
1960 if (devlink_dpipe_match_put(skb, value->match))
1962 if (devlink_dpipe_value_put(skb, value))
1967 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1968 struct devlink_dpipe_value *values,
1969 unsigned int values_count)
1971 struct nlattr *match_attr;
1975 for (i = 0; i < values_count; i++) {
1976 match_attr = nla_nest_start(skb,
1977 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1980 err = devlink_dpipe_match_value_put(skb, &values[i]);
1982 goto err_match_value_put;
1983 nla_nest_end(skb, match_attr);
1987 err_match_value_put:
1988 nla_nest_cancel(skb, match_attr);
1992 static int devlink_dpipe_entry_put(struct sk_buff *skb,
1993 struct devlink_dpipe_entry *entry)
1995 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1998 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2002 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2004 goto nla_put_failure;
2005 if (entry->counter_valid)
2006 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2007 entry->counter, DEVLINK_ATTR_PAD))
2008 goto nla_put_failure;
2010 matches_attr = nla_nest_start(skb,
2011 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2013 goto nla_put_failure;
2015 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2016 entry->match_values_count);
2018 nla_nest_cancel(skb, matches_attr);
2019 goto err_match_values_put;
2021 nla_nest_end(skb, matches_attr);
2023 actions_attr = nla_nest_start(skb,
2024 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2026 goto nla_put_failure;
2028 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2029 entry->action_values_count);
2031 nla_nest_cancel(skb, actions_attr);
2032 goto err_action_values_put;
2034 nla_nest_end(skb, actions_attr);
2036 nla_nest_end(skb, entry_attr);
2041 err_match_values_put:
2042 err_action_values_put:
2043 nla_nest_cancel(skb, entry_attr);
2047 static struct devlink_dpipe_table *
2048 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2049 const char *table_name)
2051 struct devlink_dpipe_table *table;
2053 list_for_each_entry_rcu(table, dpipe_tables, list) {
2054 if (!strcmp(table->name, table_name))
2060 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2062 struct devlink *devlink;
2065 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2070 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2071 dump_ctx->info->snd_portid,
2072 dump_ctx->info->snd_seq,
2073 &devlink_nl_family, NLM_F_MULTI,
2076 goto nla_put_failure;
2078 devlink = dump_ctx->info->user_ptr[0];
2079 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2080 goto nla_put_failure;
2081 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2082 DEVLINK_ATTR_DPIPE_ENTRIES);
2083 if (!dump_ctx->nest)
2084 goto nla_put_failure;
2088 nlmsg_free(dump_ctx->skb);
2091 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2093 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2094 struct devlink_dpipe_entry *entry)
2096 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2098 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2100 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2102 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2103 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2106 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2108 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2111 unsigned int value_count, value_index;
2112 struct devlink_dpipe_value *value;
2114 value = entry->action_values;
2115 value_count = entry->action_values_count;
2116 for (value_index = 0; value_index < value_count; value_index++) {
2117 kfree(value[value_index].value);
2118 kfree(value[value_index].mask);
2121 value = entry->match_values;
2122 value_count = entry->match_values_count;
2123 for (value_index = 0; value_index < value_count; value_index++) {
2124 kfree(value[value_index].value);
2125 kfree(value[value_index].mask);
2128 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2130 static int devlink_dpipe_entries_fill(struct genl_info *info,
2131 enum devlink_command cmd, int flags,
2132 struct devlink_dpipe_table *table)
2134 struct devlink_dpipe_dump_ctx dump_ctx;
2135 struct nlmsghdr *nlh;
2138 dump_ctx.skb = NULL;
2140 dump_ctx.info = info;
2142 err = table->table_ops->entries_dump(table->priv,
2143 table->counters_enabled,
2149 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2150 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2152 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2157 return genlmsg_reply(dump_ctx.skb, info);
2160 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2161 struct genl_info *info)
2163 struct devlink *devlink = info->user_ptr[0];
2164 struct devlink_dpipe_table *table;
2165 const char *table_name;
2167 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2170 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2171 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2176 if (!table->table_ops->entries_dump)
2179 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2183 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2184 const struct devlink_dpipe_header *header)
2186 struct devlink_dpipe_field *field;
2187 struct nlattr *field_attr;
2190 for (i = 0; i < header->fields_count; i++) {
2191 field = &header->fields[i];
2192 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2195 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2196 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2197 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2198 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2199 goto nla_put_failure;
2200 nla_nest_end(skb, field_attr);
2205 nla_nest_cancel(skb, field_attr);
2209 static int devlink_dpipe_header_put(struct sk_buff *skb,
2210 struct devlink_dpipe_header *header)
2212 struct nlattr *fields_attr, *header_attr;
2215 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
2219 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2220 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2221 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2222 goto nla_put_failure;
2224 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2226 goto nla_put_failure;
2228 err = devlink_dpipe_fields_put(skb, header);
2230 nla_nest_cancel(skb, fields_attr);
2231 goto nla_put_failure;
2233 nla_nest_end(skb, fields_attr);
2234 nla_nest_end(skb, header_attr);
2239 nla_nest_cancel(skb, header_attr);
2243 static int devlink_dpipe_headers_fill(struct genl_info *info,
2244 enum devlink_command cmd, int flags,
2245 struct devlink_dpipe_headers *
2248 struct devlink *devlink = info->user_ptr[0];
2249 struct nlattr *headers_attr;
2250 struct sk_buff *skb = NULL;
2251 struct nlmsghdr *nlh;
2258 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2262 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2263 &devlink_nl_family, NLM_F_MULTI, cmd);
2269 if (devlink_nl_put_handle(skb, devlink))
2270 goto nla_put_failure;
2271 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2273 goto nla_put_failure;
2276 for (; i < dpipe_headers->headers_count; i++) {
2277 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2285 nla_nest_end(skb, headers_attr);
2286 genlmsg_end(skb, hdr);
2287 if (i != dpipe_headers->headers_count)
2291 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2292 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2294 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2299 return genlmsg_reply(skb, info);
2308 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2309 struct genl_info *info)
2311 struct devlink *devlink = info->user_ptr[0];
2313 if (!devlink->dpipe_headers)
2315 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2316 0, devlink->dpipe_headers);
2319 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2320 const char *table_name,
2323 struct devlink_dpipe_table *table;
2325 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2330 if (table->counter_control_extern)
2333 if (!(table->counters_enabled ^ enable))
2336 table->counters_enabled = enable;
2337 if (table->table_ops->counters_set_update)
2338 table->table_ops->counters_set_update(table->priv, enable);
2342 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2343 struct genl_info *info)
2345 struct devlink *devlink = info->user_ptr[0];
2346 const char *table_name;
2347 bool counters_enable;
2349 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2350 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2353 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2354 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2356 return devlink_dpipe_table_counters_set(devlink, table_name,
2360 static struct devlink_resource *
2361 devlink_resource_find(struct devlink *devlink,
2362 struct devlink_resource *resource, u64 resource_id)
2364 struct list_head *resource_list;
2367 resource_list = &resource->resource_list;
2369 resource_list = &devlink->resource_list;
2371 list_for_each_entry(resource, resource_list, list) {
2372 struct devlink_resource *child_resource;
2374 if (resource->id == resource_id)
2377 child_resource = devlink_resource_find(devlink, resource,
2380 return child_resource;
2386 devlink_resource_validate_children(struct devlink_resource *resource)
2388 struct devlink_resource *child_resource;
2389 bool size_valid = true;
2392 if (list_empty(&resource->resource_list))
2395 list_for_each_entry(child_resource, &resource->resource_list, list)
2396 parts_size += child_resource->size_new;
2398 if (parts_size > resource->size_new)
2401 resource->size_valid = size_valid;
2405 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2406 struct netlink_ext_ack *extack)
2411 if (size > resource->size_params.size_max) {
2412 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2416 if (size < resource->size_params.size_min) {
2417 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2421 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2423 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2430 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2431 struct genl_info *info)
2433 struct devlink *devlink = info->user_ptr[0];
2434 struct devlink_resource *resource;
2439 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2440 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2442 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2444 resource = devlink_resource_find(devlink, NULL, resource_id);
2448 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2449 err = devlink_resource_validate_size(resource, size, info->extack);
2453 resource->size_new = size;
2454 devlink_resource_validate_children(resource);
2455 if (resource->parent)
2456 devlink_resource_validate_children(resource->parent);
2461 devlink_resource_size_params_put(struct devlink_resource *resource,
2462 struct sk_buff *skb)
2464 struct devlink_resource_size_params *size_params;
2466 size_params = &resource->size_params;
2467 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2468 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2469 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2470 size_params->size_max, DEVLINK_ATTR_PAD) ||
2471 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2472 size_params->size_min, DEVLINK_ATTR_PAD) ||
2473 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2478 static int devlink_resource_occ_put(struct devlink_resource *resource,
2479 struct sk_buff *skb)
2481 if (!resource->occ_get)
2483 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2484 resource->occ_get(resource->occ_get_priv),
2488 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2489 struct devlink_resource *resource)
2491 struct devlink_resource *child_resource;
2492 struct nlattr *child_resource_attr;
2493 struct nlattr *resource_attr;
2495 resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2499 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2500 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2501 DEVLINK_ATTR_PAD) ||
2502 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2504 goto nla_put_failure;
2505 if (resource->size != resource->size_new)
2506 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2507 resource->size_new, DEVLINK_ATTR_PAD);
2508 if (devlink_resource_occ_put(resource, skb))
2509 goto nla_put_failure;
2510 if (devlink_resource_size_params_put(resource, skb))
2511 goto nla_put_failure;
2512 if (list_empty(&resource->resource_list))
2515 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2516 resource->size_valid))
2517 goto nla_put_failure;
2519 child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2520 if (!child_resource_attr)
2521 goto nla_put_failure;
2523 list_for_each_entry(child_resource, &resource->resource_list, list) {
2524 if (devlink_resource_put(devlink, skb, child_resource))
2525 goto resource_put_failure;
2528 nla_nest_end(skb, child_resource_attr);
2530 nla_nest_end(skb, resource_attr);
2533 resource_put_failure:
2534 nla_nest_cancel(skb, child_resource_attr);
2536 nla_nest_cancel(skb, resource_attr);
2540 static int devlink_resource_fill(struct genl_info *info,
2541 enum devlink_command cmd, int flags)
2543 struct devlink *devlink = info->user_ptr[0];
2544 struct devlink_resource *resource;
2545 struct nlattr *resources_attr;
2546 struct sk_buff *skb = NULL;
2547 struct nlmsghdr *nlh;
2553 resource = list_first_entry(&devlink->resource_list,
2554 struct devlink_resource, list);
2556 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2560 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2561 &devlink_nl_family, NLM_F_MULTI, cmd);
2567 if (devlink_nl_put_handle(skb, devlink))
2568 goto nla_put_failure;
2570 resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2571 if (!resources_attr)
2572 goto nla_put_failure;
2576 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2577 err = devlink_resource_put(devlink, skb, resource);
2580 goto err_resource_put;
2586 nla_nest_end(skb, resources_attr);
2587 genlmsg_end(skb, hdr);
2591 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2592 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2594 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2599 return genlmsg_reply(skb, info);
2608 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2609 struct genl_info *info)
2611 struct devlink *devlink = info->user_ptr[0];
2613 if (list_empty(&devlink->resource_list))
2616 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2620 devlink_resources_validate(struct devlink *devlink,
2621 struct devlink_resource *resource,
2622 struct genl_info *info)
2624 struct list_head *resource_list;
2628 resource_list = &resource->resource_list;
2630 resource_list = &devlink->resource_list;
2632 list_for_each_entry(resource, resource_list, list) {
2633 if (!resource->size_valid)
2635 err = devlink_resources_validate(devlink, resource, info);
2642 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2644 struct devlink *devlink = info->user_ptr[0];
2647 if (!devlink->ops->reload)
2650 err = devlink_resources_validate(devlink, NULL, info);
2652 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2655 return devlink->ops->reload(devlink, info->extack);
2658 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2659 struct genl_info *info)
2661 struct devlink *devlink = info->user_ptr[0];
2662 const char *file_name, *component;
2663 struct nlattr *nla_component;
2665 if (!devlink->ops->flash_update)
2668 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2670 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2672 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2673 component = nla_component ? nla_data(nla_component) : NULL;
2675 return devlink->ops->flash_update(devlink, file_name, component,
2679 static const struct devlink_param devlink_param_generic[] = {
2681 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2682 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2683 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2686 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2687 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2688 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2691 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2692 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2693 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2696 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2697 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2698 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2701 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2702 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2703 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2706 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2707 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2708 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2711 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2712 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2713 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2716 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2717 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2718 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2722 static int devlink_param_generic_verify(const struct devlink_param *param)
2724 /* verify it match generic parameter by id and name */
2725 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2727 if (strcmp(param->name, devlink_param_generic[param->id].name))
2730 WARN_ON(param->type != devlink_param_generic[param->id].type);
2735 static int devlink_param_driver_verify(const struct devlink_param *param)
2739 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2741 /* verify no such name in generic params */
2742 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2743 if (!strcmp(param->name, devlink_param_generic[i].name))
2749 static struct devlink_param_item *
2750 devlink_param_find_by_name(struct list_head *param_list,
2751 const char *param_name)
2753 struct devlink_param_item *param_item;
2755 list_for_each_entry(param_item, param_list, list)
2756 if (!strcmp(param_item->param->name, param_name))
2761 static struct devlink_param_item *
2762 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2764 struct devlink_param_item *param_item;
2766 list_for_each_entry(param_item, param_list, list)
2767 if (param_item->param->id == param_id)
2773 devlink_param_cmode_is_supported(const struct devlink_param *param,
2774 enum devlink_param_cmode cmode)
2776 return test_bit(cmode, ¶m->supported_cmodes);
2779 static int devlink_param_get(struct devlink *devlink,
2780 const struct devlink_param *param,
2781 struct devlink_param_gset_ctx *ctx)
2785 return param->get(devlink, param->id, ctx);
2788 static int devlink_param_set(struct devlink *devlink,
2789 const struct devlink_param *param,
2790 struct devlink_param_gset_ctx *ctx)
2794 return param->set(devlink, param->id, ctx);
2798 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2800 switch (param_type) {
2801 case DEVLINK_PARAM_TYPE_U8:
2803 case DEVLINK_PARAM_TYPE_U16:
2805 case DEVLINK_PARAM_TYPE_U32:
2807 case DEVLINK_PARAM_TYPE_STRING:
2809 case DEVLINK_PARAM_TYPE_BOOL:
2817 devlink_nl_param_value_fill_one(struct sk_buff *msg,
2818 enum devlink_param_type type,
2819 enum devlink_param_cmode cmode,
2820 union devlink_param_value val)
2822 struct nlattr *param_value_attr;
2824 param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2825 if (!param_value_attr)
2826 goto nla_put_failure;
2828 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2829 goto value_nest_cancel;
2832 case DEVLINK_PARAM_TYPE_U8:
2833 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2834 goto value_nest_cancel;
2836 case DEVLINK_PARAM_TYPE_U16:
2837 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2838 goto value_nest_cancel;
2840 case DEVLINK_PARAM_TYPE_U32:
2841 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2842 goto value_nest_cancel;
2844 case DEVLINK_PARAM_TYPE_STRING:
2845 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2847 goto value_nest_cancel;
2849 case DEVLINK_PARAM_TYPE_BOOL:
2851 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2852 goto value_nest_cancel;
2856 nla_nest_end(msg, param_value_attr);
2860 nla_nest_cancel(msg, param_value_attr);
2865 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2866 unsigned int port_index,
2867 struct devlink_param_item *param_item,
2868 enum devlink_command cmd,
2869 u32 portid, u32 seq, int flags)
2871 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2872 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
2873 const struct devlink_param *param = param_item->param;
2874 struct devlink_param_gset_ctx ctx;
2875 struct nlattr *param_values_list;
2876 struct nlattr *param_attr;
2882 /* Get value from driver part to driverinit configuration mode */
2883 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2884 if (!devlink_param_cmode_is_supported(param, i))
2886 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2887 if (!param_item->driverinit_value_valid)
2889 param_value[i] = param_item->driverinit_value;
2891 if (!param_item->published)
2894 err = devlink_param_get(devlink, param, &ctx);
2897 param_value[i] = ctx.val;
2899 param_value_set[i] = true;
2902 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2906 if (devlink_nl_put_handle(msg, devlink))
2907 goto genlmsg_cancel;
2909 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
2910 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
2911 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
2912 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
2913 goto genlmsg_cancel;
2915 param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2917 goto genlmsg_cancel;
2918 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2919 goto param_nest_cancel;
2920 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2921 goto param_nest_cancel;
2923 nla_type = devlink_param_type_to_nla_type(param->type);
2925 goto param_nest_cancel;
2926 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2927 goto param_nest_cancel;
2929 param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2930 if (!param_values_list)
2931 goto param_nest_cancel;
2933 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2934 if (!param_value_set[i])
2936 err = devlink_nl_param_value_fill_one(msg, param->type,
2939 goto values_list_nest_cancel;
2942 nla_nest_end(msg, param_values_list);
2943 nla_nest_end(msg, param_attr);
2944 genlmsg_end(msg, hdr);
2947 values_list_nest_cancel:
2948 nla_nest_end(msg, param_values_list);
2950 nla_nest_cancel(msg, param_attr);
2952 genlmsg_cancel(msg, hdr);
2956 static void devlink_param_notify(struct devlink *devlink,
2957 unsigned int port_index,
2958 struct devlink_param_item *param_item,
2959 enum devlink_command cmd)
2961 struct sk_buff *msg;
2964 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
2965 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
2966 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
2968 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2971 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
2978 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2979 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2982 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2983 struct netlink_callback *cb)
2985 struct devlink_param_item *param_item;
2986 struct devlink *devlink;
2987 int start = cb->args[0];
2991 mutex_lock(&devlink_mutex);
2992 list_for_each_entry(devlink, &devlink_list, list) {
2993 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2995 mutex_lock(&devlink->lock);
2996 list_for_each_entry(param_item, &devlink->param_list, list) {
3001 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3002 DEVLINK_CMD_PARAM_GET,
3003 NETLINK_CB(cb->skb).portid,
3007 mutex_unlock(&devlink->lock);
3012 mutex_unlock(&devlink->lock);
3015 mutex_unlock(&devlink_mutex);
3022 devlink_param_type_get_from_info(struct genl_info *info,
3023 enum devlink_param_type *param_type)
3025 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3028 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3030 *param_type = DEVLINK_PARAM_TYPE_U8;
3033 *param_type = DEVLINK_PARAM_TYPE_U16;
3036 *param_type = DEVLINK_PARAM_TYPE_U32;
3039 *param_type = DEVLINK_PARAM_TYPE_STRING;
3042 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3052 devlink_param_value_get_from_info(const struct devlink_param *param,
3053 struct genl_info *info,
3054 union devlink_param_value *value)
3058 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3059 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3062 switch (param->type) {
3063 case DEVLINK_PARAM_TYPE_U8:
3064 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3066 case DEVLINK_PARAM_TYPE_U16:
3067 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3069 case DEVLINK_PARAM_TYPE_U32:
3070 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3072 case DEVLINK_PARAM_TYPE_STRING:
3073 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3074 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3075 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
3076 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3079 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3081 case DEVLINK_PARAM_TYPE_BOOL:
3082 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3089 static struct devlink_param_item *
3090 devlink_param_get_from_info(struct list_head *param_list,
3091 struct genl_info *info)
3095 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3098 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3099 return devlink_param_find_by_name(param_list, param_name);
3102 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3103 struct genl_info *info)
3105 struct devlink *devlink = info->user_ptr[0];
3106 struct devlink_param_item *param_item;
3107 struct sk_buff *msg;
3110 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3118 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3119 DEVLINK_CMD_PARAM_GET,
3120 info->snd_portid, info->snd_seq, 0);
3126 return genlmsg_reply(msg, info);
3129 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3130 unsigned int port_index,
3131 struct list_head *param_list,
3132 struct genl_info *info,
3133 enum devlink_command cmd)
3135 enum devlink_param_type param_type;
3136 struct devlink_param_gset_ctx ctx;
3137 enum devlink_param_cmode cmode;
3138 struct devlink_param_item *param_item;
3139 const struct devlink_param *param;
3140 union devlink_param_value value;
3143 param_item = devlink_param_get_from_info(param_list, info);
3146 param = param_item->param;
3147 err = devlink_param_type_get_from_info(info, ¶m_type);
3150 if (param_type != param->type)
3152 err = devlink_param_value_get_from_info(param, info, &value);
3155 if (param->validate) {
3156 err = param->validate(devlink, param->id, value, info->extack);
3161 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3163 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3164 if (!devlink_param_cmode_is_supported(param, cmode))
3167 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3168 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3169 strcpy(param_item->driverinit_value.vstr, value.vstr);
3171 param_item->driverinit_value = value;
3172 param_item->driverinit_value_valid = true;
3178 err = devlink_param_set(devlink, param, &ctx);
3183 devlink_param_notify(devlink, port_index, param_item, cmd);
3187 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3188 struct genl_info *info)
3190 struct devlink *devlink = info->user_ptr[0];
3192 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
3193 info, DEVLINK_CMD_PARAM_NEW);
3196 static int devlink_param_register_one(struct devlink *devlink,
3197 unsigned int port_index,
3198 struct list_head *param_list,
3199 const struct devlink_param *param,
3200 enum devlink_command cmd)
3202 struct devlink_param_item *param_item;
3204 if (devlink_param_find_by_name(param_list, param->name))
3207 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3208 WARN_ON(param->get || param->set);
3210 WARN_ON(!param->get || !param->set);
3212 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3215 param_item->param = param;
3217 list_add_tail(¶m_item->list, param_list);
3218 devlink_param_notify(devlink, port_index, param_item, cmd);
3222 static void devlink_param_unregister_one(struct devlink *devlink,
3223 unsigned int port_index,
3224 struct list_head *param_list,
3225 const struct devlink_param *param,
3226 enum devlink_command cmd)
3228 struct devlink_param_item *param_item;
3230 param_item = devlink_param_find_by_name(param_list, param->name);
3231 WARN_ON(!param_item);
3232 devlink_param_notify(devlink, port_index, param_item, cmd);
3233 list_del(¶m_item->list);
3237 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3238 struct netlink_callback *cb)
3240 struct devlink_param_item *param_item;
3241 struct devlink_port *devlink_port;
3242 struct devlink *devlink;
3243 int start = cb->args[0];
3247 mutex_lock(&devlink_mutex);
3248 list_for_each_entry(devlink, &devlink_list, list) {
3249 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3251 mutex_lock(&devlink->lock);
3252 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3253 list_for_each_entry(param_item,
3254 &devlink_port->param_list, list) {
3259 err = devlink_nl_param_fill(msg,
3260 devlink_port->devlink,
3261 devlink_port->index, param_item,
3262 DEVLINK_CMD_PORT_PARAM_GET,
3263 NETLINK_CB(cb->skb).portid,
3267 mutex_unlock(&devlink->lock);
3273 mutex_unlock(&devlink->lock);
3276 mutex_unlock(&devlink_mutex);
3282 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3283 struct genl_info *info)
3285 struct devlink_port *devlink_port = info->user_ptr[0];
3286 struct devlink_param_item *param_item;
3287 struct sk_buff *msg;
3290 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3295 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3299 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3300 devlink_port->index, param_item,
3301 DEVLINK_CMD_PORT_PARAM_GET,
3302 info->snd_portid, info->snd_seq, 0);
3308 return genlmsg_reply(msg, info);
3311 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3312 struct genl_info *info)
3314 struct devlink_port *devlink_port = info->user_ptr[0];
3316 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
3317 devlink_port->index,
3318 &devlink_port->param_list, info,
3319 DEVLINK_CMD_PORT_PARAM_NEW);
3322 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3323 struct devlink *devlink,
3324 struct devlink_snapshot *snapshot)
3326 struct nlattr *snap_attr;
3329 snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3333 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3335 goto nla_put_failure;
3337 nla_nest_end(msg, snap_attr);
3341 nla_nest_cancel(msg, snap_attr);
3345 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3346 struct devlink *devlink,
3347 struct devlink_region *region)
3349 struct devlink_snapshot *snapshot;
3350 struct nlattr *snapshots_attr;
3353 snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
3354 if (!snapshots_attr)
3357 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
3358 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3360 goto nla_put_failure;
3363 nla_nest_end(msg, snapshots_attr);
3367 nla_nest_cancel(msg, snapshots_attr);
3371 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3372 enum devlink_command cmd, u32 portid,
3374 struct devlink_region *region)
3379 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3383 err = devlink_nl_put_handle(msg, devlink);
3385 goto nla_put_failure;
3387 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3389 goto nla_put_failure;
3391 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3395 goto nla_put_failure;
3397 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3399 goto nla_put_failure;
3401 genlmsg_end(msg, hdr);
3405 genlmsg_cancel(msg, hdr);
3409 static void devlink_nl_region_notify(struct devlink_region *region,
3410 struct devlink_snapshot *snapshot,
3411 enum devlink_command cmd)
3413 struct devlink *devlink = region->devlink;
3414 struct sk_buff *msg;
3418 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3420 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3424 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3428 err = devlink_nl_put_handle(msg, devlink);
3430 goto out_cancel_msg;
3432 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3435 goto out_cancel_msg;
3438 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3441 goto out_cancel_msg;
3443 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3444 region->size, DEVLINK_ATTR_PAD);
3446 goto out_cancel_msg;
3448 genlmsg_end(msg, hdr);
3450 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3451 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3456 genlmsg_cancel(msg, hdr);
3461 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3462 struct genl_info *info)
3464 struct devlink *devlink = info->user_ptr[0];
3465 struct devlink_region *region;
3466 const char *region_name;
3467 struct sk_buff *msg;
3470 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3473 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3474 region = devlink_region_get_by_name(devlink, region_name);
3478 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3482 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3483 info->snd_portid, info->snd_seq, 0,
3490 return genlmsg_reply(msg, info);
3493 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3494 struct netlink_callback *cb)
3496 struct devlink_region *region;
3497 struct devlink *devlink;
3498 int start = cb->args[0];
3502 mutex_lock(&devlink_mutex);
3503 list_for_each_entry(devlink, &devlink_list, list) {
3504 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3507 mutex_lock(&devlink->lock);
3508 list_for_each_entry(region, &devlink->region_list, list) {
3513 err = devlink_nl_region_fill(msg, devlink,
3514 DEVLINK_CMD_REGION_GET,
3515 NETLINK_CB(cb->skb).portid,
3517 NLM_F_MULTI, region);
3519 mutex_unlock(&devlink->lock);
3524 mutex_unlock(&devlink->lock);
3527 mutex_unlock(&devlink_mutex);
3532 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3533 struct genl_info *info)
3535 struct devlink *devlink = info->user_ptr[0];
3536 struct devlink_snapshot *snapshot;
3537 struct devlink_region *region;
3538 const char *region_name;
3541 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3542 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3545 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3546 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3548 region = devlink_region_get_by_name(devlink, region_name);
3552 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3556 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3557 devlink_region_snapshot_del(snapshot);
3561 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3562 struct devlink *devlink,
3563 u8 *chunk, u32 chunk_size,
3566 struct nlattr *chunk_attr;
3569 chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
3573 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3575 goto nla_put_failure;
3577 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3580 goto nla_put_failure;
3582 nla_nest_end(msg, chunk_attr);
3586 nla_nest_cancel(msg, chunk_attr);
3590 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
3592 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3593 struct devlink *devlink,
3594 struct devlink_region *region,
3595 struct nlattr **attrs,
3601 struct devlink_snapshot *snapshot;
3602 u64 curr_offset = start_offset;
3606 *new_offset = start_offset;
3608 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3609 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3613 if (end_offset > snapshot->data_len || dump)
3614 end_offset = snapshot->data_len;
3616 while (curr_offset < end_offset) {
3620 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3621 data_size = end_offset - curr_offset;
3623 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3625 data = &snapshot->data[curr_offset];
3626 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3632 curr_offset += data_size;
3634 *new_offset = curr_offset;
3639 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3640 struct netlink_callback *cb)
3642 u64 ret_offset, start_offset, end_offset = 0;
3643 const struct genl_ops *ops = cb->data;
3644 struct devlink_region *region;
3645 struct nlattr *chunks_attr;
3646 const char *region_name;
3647 struct devlink *devlink;
3648 struct nlattr **attrs;
3653 start_offset = *((u64 *)&cb->args[0]);
3655 attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
3659 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
3660 attrs, DEVLINK_ATTR_MAX, ops->policy, cb->extack);
3664 mutex_lock(&devlink_mutex);
3665 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3666 if (IS_ERR(devlink)) {
3667 err = PTR_ERR(devlink);
3671 mutex_lock(&devlink->lock);
3673 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3674 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3679 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3680 region = devlink_region_get_by_name(devlink, region_name);
3686 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3687 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3688 DEVLINK_CMD_REGION_READ);
3694 err = devlink_nl_put_handle(skb, devlink);
3696 goto nla_put_failure;
3698 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3700 goto nla_put_failure;
3702 chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
3705 goto nla_put_failure;
3708 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3709 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3712 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3714 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3715 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3719 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3725 if (err && err != -EMSGSIZE)
3726 goto nla_put_failure;
3728 /* Check if there was any progress done to prevent infinite loop */
3729 if (ret_offset == start_offset) {
3731 goto nla_put_failure;
3734 *((u64 *)&cb->args[0]) = ret_offset;
3736 nla_nest_end(skb, chunks_attr);
3737 genlmsg_end(skb, hdr);
3738 mutex_unlock(&devlink->lock);
3739 mutex_unlock(&devlink_mutex);
3745 genlmsg_cancel(skb, hdr);
3747 mutex_unlock(&devlink->lock);
3749 mutex_unlock(&devlink_mutex);
3755 struct devlink_info_req {
3756 struct sk_buff *msg;
3759 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
3761 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
3763 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
3765 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
3767 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
3769 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
3771 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
3772 const char *version_name,
3773 const char *version_value)
3775 struct nlattr *nest;
3778 nest = nla_nest_start(req->msg, attr);
3782 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
3785 goto nla_put_failure;
3787 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
3790 goto nla_put_failure;
3792 nla_nest_end(req->msg, nest);
3797 nla_nest_cancel(req->msg, nest);
3801 int devlink_info_version_fixed_put(struct devlink_info_req *req,
3802 const char *version_name,
3803 const char *version_value)
3805 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
3806 version_name, version_value);
3808 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
3810 int devlink_info_version_stored_put(struct devlink_info_req *req,
3811 const char *version_name,
3812 const char *version_value)
3814 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
3815 version_name, version_value);
3817 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
3819 int devlink_info_version_running_put(struct devlink_info_req *req,
3820 const char *version_name,
3821 const char *version_value)
3823 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
3824 version_name, version_value);
3826 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
3829 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
3830 enum devlink_command cmd, u32 portid,
3831 u32 seq, int flags, struct netlink_ext_ack *extack)
3833 struct devlink_info_req req;
3837 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3842 if (devlink_nl_put_handle(msg, devlink))
3843 goto err_cancel_msg;
3846 err = devlink->ops->info_get(devlink, &req, extack);
3848 goto err_cancel_msg;
3850 genlmsg_end(msg, hdr);
3854 genlmsg_cancel(msg, hdr);
3858 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
3859 struct genl_info *info)
3861 struct devlink *devlink = info->user_ptr[0];
3862 struct sk_buff *msg;
3865 if (!devlink->ops->info_get)
3868 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3872 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3873 info->snd_portid, info->snd_seq, 0,
3880 return genlmsg_reply(msg, info);
3883 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
3884 struct netlink_callback *cb)
3886 struct devlink *devlink;
3887 int start = cb->args[0];
3891 mutex_lock(&devlink_mutex);
3892 list_for_each_entry(devlink, &devlink_list, list) {
3893 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3900 mutex_lock(&devlink->lock);
3901 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3902 NETLINK_CB(cb->skb).portid,
3903 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3905 mutex_unlock(&devlink->lock);
3910 mutex_unlock(&devlink_mutex);
3916 struct devlink_fmsg_item {
3917 struct list_head list;
3924 struct devlink_fmsg {
3925 struct list_head item_list;
3928 static struct devlink_fmsg *devlink_fmsg_alloc(void)
3930 struct devlink_fmsg *fmsg;
3932 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
3936 INIT_LIST_HEAD(&fmsg->item_list);
3941 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
3943 struct devlink_fmsg_item *item, *tmp;
3945 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
3946 list_del(&item->list);
3952 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
3955 struct devlink_fmsg_item *item;
3957 item = kzalloc(sizeof(*item), GFP_KERNEL);
3961 item->attrtype = attrtype;
3962 list_add_tail(&item->list, &fmsg->item_list);
3967 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
3969 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
3971 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
3973 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
3975 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
3978 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
3980 return devlink_fmsg_nest_end(fmsg);
3982 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
3984 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
3986 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
3988 struct devlink_fmsg_item *item;
3990 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
3993 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
3997 item->nla_type = NLA_NUL_STRING;
3998 item->len = strlen(name) + 1;
3999 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4000 memcpy(&item->value, name, item->len);
4001 list_add_tail(&item->list, &fmsg->item_list);
4006 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4010 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4014 err = devlink_fmsg_put_name(fmsg, name);
4020 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4022 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4024 return devlink_fmsg_nest_end(fmsg);
4026 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4028 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4033 err = devlink_fmsg_pair_nest_start(fmsg, name);
4037 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4043 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4045 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4049 err = devlink_fmsg_nest_end(fmsg);
4053 err = devlink_fmsg_nest_end(fmsg);
4059 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4061 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4062 const void *value, u16 value_len,
4065 struct devlink_fmsg_item *item;
4067 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4070 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4074 item->nla_type = value_nla_type;
4075 item->len = value_len;
4076 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4077 memcpy(&item->value, value, item->len);
4078 list_add_tail(&item->list, &fmsg->item_list);
4083 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4085 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4087 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4089 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4091 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4093 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4095 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4097 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4099 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4101 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4103 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4105 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4107 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4109 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4112 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4114 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4117 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4119 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
4121 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4126 err = devlink_fmsg_pair_nest_start(fmsg, name);
4130 err = devlink_fmsg_bool_put(fmsg, value);
4134 err = devlink_fmsg_pair_nest_end(fmsg);
4140 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4142 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4147 err = devlink_fmsg_pair_nest_start(fmsg, name);
4151 err = devlink_fmsg_u8_put(fmsg, value);
4155 err = devlink_fmsg_pair_nest_end(fmsg);
4161 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4163 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4168 err = devlink_fmsg_pair_nest_start(fmsg, name);
4172 err = devlink_fmsg_u32_put(fmsg, value);
4176 err = devlink_fmsg_pair_nest_end(fmsg);
4182 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4184 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4189 err = devlink_fmsg_pair_nest_start(fmsg, name);
4193 err = devlink_fmsg_u64_put(fmsg, value);
4197 err = devlink_fmsg_pair_nest_end(fmsg);
4203 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4205 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4210 err = devlink_fmsg_pair_nest_start(fmsg, name);
4214 err = devlink_fmsg_string_put(fmsg, value);
4218 err = devlink_fmsg_pair_nest_end(fmsg);
4224 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4226 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
4227 const void *value, u16 value_len)
4231 err = devlink_fmsg_pair_nest_start(fmsg, name);
4235 err = devlink_fmsg_binary_put(fmsg, value, value_len);
4239 err = devlink_fmsg_pair_nest_end(fmsg);
4245 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4248 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4250 switch (msg->nla_type) {
4255 case NLA_NUL_STRING:
4257 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4265 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4267 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4270 switch (msg->nla_type) {
4272 /* Always provide flag data, regardless of its value */
4273 tmp = *(bool *) msg->value;
4275 return nla_put_u8(skb, attrtype, tmp);
4277 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4279 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4281 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4283 case NLA_NUL_STRING:
4284 return nla_put_string(skb, attrtype, (char *) &msg->value);
4286 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4293 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4296 struct devlink_fmsg_item *item;
4297 struct nlattr *fmsg_nlattr;
4301 fmsg_nlattr = nla_nest_start(skb, DEVLINK_ATTR_FMSG);
4305 list_for_each_entry(item, &fmsg->item_list, list) {
4311 switch (item->attrtype) {
4312 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4313 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4314 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4315 case DEVLINK_ATTR_FMSG_NEST_END:
4316 err = nla_put_flag(skb, item->attrtype);
4318 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4319 err = devlink_fmsg_item_fill_type(item, skb);
4322 err = devlink_fmsg_item_fill_data(item, skb);
4324 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4325 err = nla_put_string(skb, item->attrtype,
4326 (char *) &item->value);
4338 nla_nest_end(skb, fmsg_nlattr);
4342 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4343 struct genl_info *info,
4344 enum devlink_command cmd, int flags)
4346 struct nlmsghdr *nlh;
4347 struct sk_buff *skb;
4354 int tmp_index = index;
4356 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4360 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4361 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4364 goto nla_put_failure;
4367 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4370 else if (err != -EMSGSIZE || tmp_index == index)
4371 goto nla_put_failure;
4373 genlmsg_end(skb, hdr);
4374 err = genlmsg_reply(skb, info);
4379 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4382 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4383 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4386 goto nla_put_failure;
4389 return genlmsg_reply(skb, info);
4396 struct devlink_health_reporter {
4397 struct list_head list;
4399 const struct devlink_health_reporter_ops *ops;
4400 struct devlink *devlink;
4401 struct devlink_fmsg *dump_fmsg;
4402 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
4403 u64 graceful_period;
4409 u64 last_recovery_ts;
4413 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4415 return reporter->priv;
4417 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4419 static struct devlink_health_reporter *
4420 devlink_health_reporter_find_by_name(struct devlink *devlink,
4421 const char *reporter_name)
4423 struct devlink_health_reporter *reporter;
4425 list_for_each_entry(reporter, &devlink->reporter_list, list)
4426 if (!strcmp(reporter->ops->name, reporter_name))
4432 * devlink_health_reporter_create - create devlink health reporter
4436 * @graceful_period: to avoid recovery loops, in msecs
4437 * @auto_recover: auto recover when error occurs
4440 struct devlink_health_reporter *
4441 devlink_health_reporter_create(struct devlink *devlink,
4442 const struct devlink_health_reporter_ops *ops,
4443 u64 graceful_period, bool auto_recover,
4446 struct devlink_health_reporter *reporter;
4448 mutex_lock(&devlink->lock);
4449 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4450 reporter = ERR_PTR(-EEXIST);
4454 if (WARN_ON(auto_recover && !ops->recover) ||
4455 WARN_ON(graceful_period && !ops->recover)) {
4456 reporter = ERR_PTR(-EINVAL);
4460 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4462 reporter = ERR_PTR(-ENOMEM);
4466 reporter->priv = priv;
4467 reporter->ops = ops;
4468 reporter->devlink = devlink;
4469 reporter->graceful_period = graceful_period;
4470 reporter->auto_recover = auto_recover;
4471 mutex_init(&reporter->dump_lock);
4472 list_add_tail(&reporter->list, &devlink->reporter_list);
4474 mutex_unlock(&devlink->lock);
4477 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4480 * devlink_health_reporter_destroy - destroy devlink health reporter
4482 * @reporter: devlink health reporter to destroy
4485 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4487 mutex_lock(&reporter->devlink->lock);
4488 list_del(&reporter->list);
4489 mutex_unlock(&reporter->devlink->lock);
4490 if (reporter->dump_fmsg)
4491 devlink_fmsg_free(reporter->dump_fmsg);
4494 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4497 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4498 enum devlink_health_reporter_state state)
4500 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4501 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4504 if (reporter->health_state == state)
4507 reporter->health_state = state;
4508 trace_devlink_health_reporter_state_update(reporter->devlink,
4509 reporter->ops->name, state);
4511 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4514 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
4519 if (!reporter->ops->recover)
4522 err = reporter->ops->recover(reporter, priv_ctx);
4526 reporter->recovery_count++;
4527 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4528 reporter->last_recovery_ts = jiffies;
4534 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4536 if (!reporter->dump_fmsg)
4538 devlink_fmsg_free(reporter->dump_fmsg);
4539 reporter->dump_fmsg = NULL;
4542 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
4547 if (!reporter->ops->dump)
4550 if (reporter->dump_fmsg)
4553 reporter->dump_fmsg = devlink_fmsg_alloc();
4554 if (!reporter->dump_fmsg) {
4559 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4563 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
4568 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4572 reporter->dump_ts = jiffies;
4577 devlink_health_dump_clear(reporter);
4581 int devlink_health_report(struct devlink_health_reporter *reporter,
4582 const char *msg, void *priv_ctx)
4584 enum devlink_health_reporter_state prev_health_state;
4585 struct devlink *devlink = reporter->devlink;
4587 /* write a log message of the current error */
4589 trace_devlink_health_report(devlink, reporter->ops->name, msg);
4590 reporter->error_count++;
4591 prev_health_state = reporter->health_state;
4592 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4594 /* abort if the previous error wasn't recovered */
4595 if (reporter->auto_recover &&
4596 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
4597 jiffies - reporter->last_recovery_ts <
4598 msecs_to_jiffies(reporter->graceful_period))) {
4599 trace_devlink_health_recover_aborted(devlink,
4600 reporter->ops->name,
4601 reporter->health_state,
4603 reporter->last_recovery_ts);
4607 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4609 mutex_lock(&reporter->dump_lock);
4610 /* store current dump of current error, for later analysis */
4611 devlink_health_do_dump(reporter, priv_ctx);
4612 mutex_unlock(&reporter->dump_lock);
4614 if (reporter->auto_recover)
4615 return devlink_health_reporter_recover(reporter, priv_ctx);
4619 EXPORT_SYMBOL_GPL(devlink_health_report);
4621 static struct devlink_health_reporter *
4622 devlink_health_reporter_get_from_info(struct devlink *devlink,
4623 struct genl_info *info)
4625 char *reporter_name;
4627 if (!info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
4631 nla_data(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
4632 return devlink_health_reporter_find_by_name(devlink, reporter_name);
4636 devlink_nl_health_reporter_fill(struct sk_buff *msg,
4637 struct devlink *devlink,
4638 struct devlink_health_reporter *reporter,
4639 enum devlink_command cmd, u32 portid,
4642 struct nlattr *reporter_attr;
4645 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4649 if (devlink_nl_put_handle(msg, devlink))
4650 goto genlmsg_cancel;
4652 reporter_attr = nla_nest_start(msg, DEVLINK_ATTR_HEALTH_REPORTER);
4654 goto genlmsg_cancel;
4655 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
4656 reporter->ops->name))
4657 goto reporter_nest_cancel;
4658 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
4659 reporter->health_state))
4660 goto reporter_nest_cancel;
4661 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
4662 reporter->error_count, DEVLINK_ATTR_PAD))
4663 goto reporter_nest_cancel;
4664 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
4665 reporter->recovery_count, DEVLINK_ATTR_PAD))
4666 goto reporter_nest_cancel;
4667 if (reporter->ops->recover &&
4668 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
4669 reporter->graceful_period,
4671 goto reporter_nest_cancel;
4672 if (reporter->ops->recover &&
4673 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
4674 reporter->auto_recover))
4675 goto reporter_nest_cancel;
4676 if (reporter->dump_fmsg &&
4677 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
4678 jiffies_to_msecs(reporter->dump_ts),
4680 goto reporter_nest_cancel;
4682 nla_nest_end(msg, reporter_attr);
4683 genlmsg_end(msg, hdr);
4686 reporter_nest_cancel:
4687 nla_nest_end(msg, reporter_attr);
4689 genlmsg_cancel(msg, hdr);
4693 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
4694 struct genl_info *info)
4696 struct devlink *devlink = info->user_ptr[0];
4697 struct devlink_health_reporter *reporter;
4698 struct sk_buff *msg;
4701 reporter = devlink_health_reporter_get_from_info(devlink, info);
4705 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4709 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
4710 DEVLINK_CMD_HEALTH_REPORTER_GET,
4711 info->snd_portid, info->snd_seq,
4718 return genlmsg_reply(msg, info);
4722 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
4723 struct netlink_callback *cb)
4725 struct devlink_health_reporter *reporter;
4726 struct devlink *devlink;
4727 int start = cb->args[0];
4731 mutex_lock(&devlink_mutex);
4732 list_for_each_entry(devlink, &devlink_list, list) {
4733 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4735 mutex_lock(&devlink->lock);
4736 list_for_each_entry(reporter, &devlink->reporter_list,
4742 err = devlink_nl_health_reporter_fill(msg, devlink,
4744 DEVLINK_CMD_HEALTH_REPORTER_GET,
4745 NETLINK_CB(cb->skb).portid,
4749 mutex_unlock(&devlink->lock);
4754 mutex_unlock(&devlink->lock);
4757 mutex_unlock(&devlink_mutex);
4764 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
4765 struct genl_info *info)
4767 struct devlink *devlink = info->user_ptr[0];
4768 struct devlink_health_reporter *reporter;
4770 reporter = devlink_health_reporter_get_from_info(devlink, info);
4774 if (!reporter->ops->recover &&
4775 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
4776 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]))
4779 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
4780 reporter->graceful_period =
4781 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
4783 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
4784 reporter->auto_recover =
4785 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
4790 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
4791 struct genl_info *info)
4793 struct devlink *devlink = info->user_ptr[0];
4794 struct devlink_health_reporter *reporter;
4796 reporter = devlink_health_reporter_get_from_info(devlink, info);
4800 return devlink_health_reporter_recover(reporter, NULL);
4803 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
4804 struct genl_info *info)
4806 struct devlink *devlink = info->user_ptr[0];
4807 struct devlink_health_reporter *reporter;
4808 struct devlink_fmsg *fmsg;
4811 reporter = devlink_health_reporter_get_from_info(devlink, info);
4815 if (!reporter->ops->diagnose)
4818 fmsg = devlink_fmsg_alloc();
4822 err = devlink_fmsg_obj_nest_start(fmsg);
4826 err = reporter->ops->diagnose(reporter, fmsg);
4830 err = devlink_fmsg_obj_nest_end(fmsg);
4834 err = devlink_fmsg_snd(fmsg, info,
4835 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
4838 devlink_fmsg_free(fmsg);
4842 static int devlink_nl_cmd_health_reporter_dump_get_doit(struct sk_buff *skb,
4843 struct genl_info *info)
4845 struct devlink *devlink = info->user_ptr[0];
4846 struct devlink_health_reporter *reporter;
4849 reporter = devlink_health_reporter_get_from_info(devlink, info);
4853 if (!reporter->ops->dump)
4856 mutex_lock(&reporter->dump_lock);
4857 err = devlink_health_do_dump(reporter, NULL);
4861 err = devlink_fmsg_snd(reporter->dump_fmsg, info,
4862 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET, 0);
4865 mutex_unlock(&reporter->dump_lock);
4870 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
4871 struct genl_info *info)
4873 struct devlink *devlink = info->user_ptr[0];
4874 struct devlink_health_reporter *reporter;
4876 reporter = devlink_health_reporter_get_from_info(devlink, info);
4880 if (!reporter->ops->dump)
4883 mutex_lock(&reporter->dump_lock);
4884 devlink_health_dump_clear(reporter);
4885 mutex_unlock(&reporter->dump_lock);
4889 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
4890 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
4891 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
4892 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
4893 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
4894 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
4895 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
4896 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
4897 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
4898 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
4899 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
4900 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
4901 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
4902 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
4903 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
4904 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
4905 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
4906 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
4907 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
4908 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
4909 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
4910 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
4911 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
4912 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
4913 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
4914 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
4915 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
4916 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
4917 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
4918 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
4921 static const struct genl_ops devlink_nl_ops[] = {
4923 .cmd = DEVLINK_CMD_GET,
4924 .doit = devlink_nl_cmd_get_doit,
4925 .dumpit = devlink_nl_cmd_get_dumpit,
4926 .policy = devlink_nl_policy,
4927 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4928 /* can be retrieved by unprivileged users */
4931 .cmd = DEVLINK_CMD_PORT_GET,
4932 .doit = devlink_nl_cmd_port_get_doit,
4933 .dumpit = devlink_nl_cmd_port_get_dumpit,
4934 .policy = devlink_nl_policy,
4935 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
4936 /* can be retrieved by unprivileged users */
4939 .cmd = DEVLINK_CMD_PORT_SET,
4940 .doit = devlink_nl_cmd_port_set_doit,
4941 .policy = devlink_nl_policy,
4942 .flags = GENL_ADMIN_PERM,
4943 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
4946 .cmd = DEVLINK_CMD_PORT_SPLIT,
4947 .doit = devlink_nl_cmd_port_split_doit,
4948 .policy = devlink_nl_policy,
4949 .flags = GENL_ADMIN_PERM,
4950 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4951 DEVLINK_NL_FLAG_NO_LOCK,
4954 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
4955 .doit = devlink_nl_cmd_port_unsplit_doit,
4956 .policy = devlink_nl_policy,
4957 .flags = GENL_ADMIN_PERM,
4958 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4959 DEVLINK_NL_FLAG_NO_LOCK,
4962 .cmd = DEVLINK_CMD_SB_GET,
4963 .doit = devlink_nl_cmd_sb_get_doit,
4964 .dumpit = devlink_nl_cmd_sb_get_dumpit,
4965 .policy = devlink_nl_policy,
4966 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4967 DEVLINK_NL_FLAG_NEED_SB,
4968 /* can be retrieved by unprivileged users */
4971 .cmd = DEVLINK_CMD_SB_POOL_GET,
4972 .doit = devlink_nl_cmd_sb_pool_get_doit,
4973 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
4974 .policy = devlink_nl_policy,
4975 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4976 DEVLINK_NL_FLAG_NEED_SB,
4977 /* can be retrieved by unprivileged users */
4980 .cmd = DEVLINK_CMD_SB_POOL_SET,
4981 .doit = devlink_nl_cmd_sb_pool_set_doit,
4982 .policy = devlink_nl_policy,
4983 .flags = GENL_ADMIN_PERM,
4984 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4985 DEVLINK_NL_FLAG_NEED_SB,
4988 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
4989 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
4990 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
4991 .policy = devlink_nl_policy,
4992 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
4993 DEVLINK_NL_FLAG_NEED_SB,
4994 /* can be retrieved by unprivileged users */
4997 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
4998 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
4999 .policy = devlink_nl_policy,
5000 .flags = GENL_ADMIN_PERM,
5001 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5002 DEVLINK_NL_FLAG_NEED_SB,
5005 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
5006 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
5007 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
5008 .policy = devlink_nl_policy,
5009 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5010 DEVLINK_NL_FLAG_NEED_SB,
5011 /* can be retrieved by unprivileged users */
5014 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
5015 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
5016 .policy = devlink_nl_policy,
5017 .flags = GENL_ADMIN_PERM,
5018 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5019 DEVLINK_NL_FLAG_NEED_SB,
5022 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
5023 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
5024 .policy = devlink_nl_policy,
5025 .flags = GENL_ADMIN_PERM,
5026 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5027 DEVLINK_NL_FLAG_NEED_SB,
5030 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
5031 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
5032 .policy = devlink_nl_policy,
5033 .flags = GENL_ADMIN_PERM,
5034 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5035 DEVLINK_NL_FLAG_NEED_SB,
5038 .cmd = DEVLINK_CMD_ESWITCH_GET,
5039 .doit = devlink_nl_cmd_eswitch_get_doit,
5040 .policy = devlink_nl_policy,
5041 .flags = GENL_ADMIN_PERM,
5042 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5045 .cmd = DEVLINK_CMD_ESWITCH_SET,
5046 .doit = devlink_nl_cmd_eswitch_set_doit,
5047 .policy = devlink_nl_policy,
5048 .flags = GENL_ADMIN_PERM,
5049 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5050 DEVLINK_NL_FLAG_NO_LOCK,
5053 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
5054 .doit = devlink_nl_cmd_dpipe_table_get,
5055 .policy = devlink_nl_policy,
5056 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5057 /* can be retrieved by unprivileged users */
5060 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
5061 .doit = devlink_nl_cmd_dpipe_entries_get,
5062 .policy = devlink_nl_policy,
5063 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5064 /* can be retrieved by unprivileged users */
5067 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
5068 .doit = devlink_nl_cmd_dpipe_headers_get,
5069 .policy = devlink_nl_policy,
5070 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5071 /* can be retrieved by unprivileged users */
5074 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
5075 .doit = devlink_nl_cmd_dpipe_table_counters_set,
5076 .policy = devlink_nl_policy,
5077 .flags = GENL_ADMIN_PERM,
5078 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5081 .cmd = DEVLINK_CMD_RESOURCE_SET,
5082 .doit = devlink_nl_cmd_resource_set,
5083 .policy = devlink_nl_policy,
5084 .flags = GENL_ADMIN_PERM,
5085 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5088 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
5089 .doit = devlink_nl_cmd_resource_dump,
5090 .policy = devlink_nl_policy,
5091 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5092 /* can be retrieved by unprivileged users */
5095 .cmd = DEVLINK_CMD_RELOAD,
5096 .doit = devlink_nl_cmd_reload,
5097 .policy = devlink_nl_policy,
5098 .flags = GENL_ADMIN_PERM,
5099 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5100 DEVLINK_NL_FLAG_NO_LOCK,
5103 .cmd = DEVLINK_CMD_PARAM_GET,
5104 .doit = devlink_nl_cmd_param_get_doit,
5105 .dumpit = devlink_nl_cmd_param_get_dumpit,
5106 .policy = devlink_nl_policy,
5107 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5108 /* can be retrieved by unprivileged users */
5111 .cmd = DEVLINK_CMD_PARAM_SET,
5112 .doit = devlink_nl_cmd_param_set_doit,
5113 .policy = devlink_nl_policy,
5114 .flags = GENL_ADMIN_PERM,
5115 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5118 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
5119 .doit = devlink_nl_cmd_port_param_get_doit,
5120 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
5121 .policy = devlink_nl_policy,
5122 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5123 /* can be retrieved by unprivileged users */
5126 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
5127 .doit = devlink_nl_cmd_port_param_set_doit,
5128 .policy = devlink_nl_policy,
5129 .flags = GENL_ADMIN_PERM,
5130 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5133 .cmd = DEVLINK_CMD_REGION_GET,
5134 .doit = devlink_nl_cmd_region_get_doit,
5135 .dumpit = devlink_nl_cmd_region_get_dumpit,
5136 .policy = devlink_nl_policy,
5137 .flags = GENL_ADMIN_PERM,
5138 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5141 .cmd = DEVLINK_CMD_REGION_DEL,
5142 .doit = devlink_nl_cmd_region_del,
5143 .policy = devlink_nl_policy,
5144 .flags = GENL_ADMIN_PERM,
5145 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5148 .cmd = DEVLINK_CMD_REGION_READ,
5149 .dumpit = devlink_nl_cmd_region_read_dumpit,
5150 .policy = devlink_nl_policy,
5151 .flags = GENL_ADMIN_PERM,
5152 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5155 .cmd = DEVLINK_CMD_INFO_GET,
5156 .doit = devlink_nl_cmd_info_get_doit,
5157 .dumpit = devlink_nl_cmd_info_get_dumpit,
5158 .policy = devlink_nl_policy,
5159 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5160 /* can be retrieved by unprivileged users */
5163 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
5164 .doit = devlink_nl_cmd_health_reporter_get_doit,
5165 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
5166 .policy = devlink_nl_policy,
5167 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5168 /* can be retrieved by unprivileged users */
5171 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
5172 .doit = devlink_nl_cmd_health_reporter_set_doit,
5173 .policy = devlink_nl_policy,
5174 .flags = GENL_ADMIN_PERM,
5175 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5178 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
5179 .doit = devlink_nl_cmd_health_reporter_recover_doit,
5180 .policy = devlink_nl_policy,
5181 .flags = GENL_ADMIN_PERM,
5182 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5185 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
5186 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
5187 .policy = devlink_nl_policy,
5188 .flags = GENL_ADMIN_PERM,
5189 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5192 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
5193 .doit = devlink_nl_cmd_health_reporter_dump_get_doit,
5194 .policy = devlink_nl_policy,
5195 .flags = GENL_ADMIN_PERM,
5196 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5197 DEVLINK_NL_FLAG_NO_LOCK,
5200 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
5201 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
5202 .policy = devlink_nl_policy,
5203 .flags = GENL_ADMIN_PERM,
5204 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5205 DEVLINK_NL_FLAG_NO_LOCK,
5208 .cmd = DEVLINK_CMD_FLASH_UPDATE,
5209 .doit = devlink_nl_cmd_flash_update,
5210 .policy = devlink_nl_policy,
5211 .flags = GENL_ADMIN_PERM,
5212 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5216 static struct genl_family devlink_nl_family __ro_after_init = {
5217 .name = DEVLINK_GENL_NAME,
5218 .version = DEVLINK_GENL_VERSION,
5219 .maxattr = DEVLINK_ATTR_MAX,
5221 .pre_doit = devlink_nl_pre_doit,
5222 .post_doit = devlink_nl_post_doit,
5223 .module = THIS_MODULE,
5224 .ops = devlink_nl_ops,
5225 .n_ops = ARRAY_SIZE(devlink_nl_ops),
5226 .mcgrps = devlink_nl_mcgrps,
5227 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
5231 * devlink_alloc - Allocate new devlink instance resources
5234 * @priv_size: size of user private data
5236 * Allocate new devlink instance resources, including devlink index
5239 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
5241 struct devlink *devlink;
5246 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
5250 devlink_net_set(devlink, &init_net);
5251 INIT_LIST_HEAD(&devlink->port_list);
5252 INIT_LIST_HEAD(&devlink->sb_list);
5253 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
5254 INIT_LIST_HEAD(&devlink->resource_list);
5255 INIT_LIST_HEAD(&devlink->param_list);
5256 INIT_LIST_HEAD(&devlink->region_list);
5257 INIT_LIST_HEAD(&devlink->reporter_list);
5258 mutex_init(&devlink->lock);
5261 EXPORT_SYMBOL_GPL(devlink_alloc);
5264 * devlink_register - Register devlink instance
5267 * @dev: parent device
5269 int devlink_register(struct devlink *devlink, struct device *dev)
5271 mutex_lock(&devlink_mutex);
5273 list_add_tail(&devlink->list, &devlink_list);
5274 devlink_notify(devlink, DEVLINK_CMD_NEW);
5275 mutex_unlock(&devlink_mutex);
5278 EXPORT_SYMBOL_GPL(devlink_register);
5281 * devlink_unregister - Unregister devlink instance
5285 void devlink_unregister(struct devlink *devlink)
5287 mutex_lock(&devlink_mutex);
5288 devlink_notify(devlink, DEVLINK_CMD_DEL);
5289 list_del(&devlink->list);
5290 mutex_unlock(&devlink_mutex);
5292 EXPORT_SYMBOL_GPL(devlink_unregister);
5295 * devlink_free - Free devlink instance resources
5299 void devlink_free(struct devlink *devlink)
5301 WARN_ON(!list_empty(&devlink->reporter_list));
5302 WARN_ON(!list_empty(&devlink->region_list));
5303 WARN_ON(!list_empty(&devlink->param_list));
5304 WARN_ON(!list_empty(&devlink->resource_list));
5305 WARN_ON(!list_empty(&devlink->dpipe_table_list));
5306 WARN_ON(!list_empty(&devlink->sb_list));
5307 WARN_ON(!list_empty(&devlink->port_list));
5311 EXPORT_SYMBOL_GPL(devlink_free);
5314 * devlink_port_register - Register devlink port
5317 * @devlink_port: devlink port
5318 * @port_index: driver-specific numerical identifier of the port
5320 * Register devlink port with provided port index. User can use
5321 * any indexing, even hw-related one. devlink_port structure
5322 * is convenient to be embedded inside user driver private structure.
5323 * Note that the caller should take care of zeroing the devlink_port
5326 int devlink_port_register(struct devlink *devlink,
5327 struct devlink_port *devlink_port,
5328 unsigned int port_index)
5330 mutex_lock(&devlink->lock);
5331 if (devlink_port_index_exists(devlink, port_index)) {
5332 mutex_unlock(&devlink->lock);
5335 devlink_port->devlink = devlink;
5336 devlink_port->index = port_index;
5337 devlink_port->registered = true;
5338 list_add_tail(&devlink_port->list, &devlink->port_list);
5339 INIT_LIST_HEAD(&devlink_port->param_list);
5340 mutex_unlock(&devlink->lock);
5341 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5344 EXPORT_SYMBOL_GPL(devlink_port_register);
5347 * devlink_port_unregister - Unregister devlink port
5349 * @devlink_port: devlink port
5351 void devlink_port_unregister(struct devlink_port *devlink_port)
5353 struct devlink *devlink = devlink_port->devlink;
5355 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
5356 mutex_lock(&devlink->lock);
5357 list_del(&devlink_port->list);
5358 mutex_unlock(&devlink->lock);
5360 EXPORT_SYMBOL_GPL(devlink_port_unregister);
5362 static void __devlink_port_type_set(struct devlink_port *devlink_port,
5363 enum devlink_port_type type,
5366 devlink_port->type = type;
5367 devlink_port->type_dev = type_dev;
5368 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5372 * devlink_port_type_eth_set - Set port type to Ethernet
5374 * @devlink_port: devlink port
5375 * @netdev: related netdevice
5377 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
5378 struct net_device *netdev)
5380 return __devlink_port_type_set(devlink_port,
5381 DEVLINK_PORT_TYPE_ETH, netdev);
5383 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
5386 * devlink_port_type_ib_set - Set port type to InfiniBand
5388 * @devlink_port: devlink port
5389 * @ibdev: related IB device
5391 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
5392 struct ib_device *ibdev)
5394 return __devlink_port_type_set(devlink_port,
5395 DEVLINK_PORT_TYPE_IB, ibdev);
5397 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
5400 * devlink_port_type_clear - Clear port type
5402 * @devlink_port: devlink port
5404 void devlink_port_type_clear(struct devlink_port *devlink_port)
5406 return __devlink_port_type_set(devlink_port,
5407 DEVLINK_PORT_TYPE_NOTSET, NULL);
5409 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
5412 * devlink_port_attrs_set - Set port attributes
5414 * @devlink_port: devlink port
5415 * @flavour: flavour of the port
5416 * @port_number: number of the port that is facing user, for example
5417 * the front panel port number
5418 * @split: indicates if this is split port
5419 * @split_subport_number: if the port is split, this is the number
5422 void devlink_port_attrs_set(struct devlink_port *devlink_port,
5423 enum devlink_port_flavour flavour,
5424 u32 port_number, bool split,
5425 u32 split_subport_number)
5427 struct devlink_port_attrs *attrs = &devlink_port->attrs;
5430 attrs->flavour = flavour;
5431 attrs->port_number = port_number;
5432 attrs->split = split;
5433 attrs->split_subport_number = split_subport_number;
5434 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5436 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
5438 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
5439 char *name, size_t len)
5441 struct devlink_port_attrs *attrs = &devlink_port->attrs;
5447 switch (attrs->flavour) {
5448 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
5450 n = snprintf(name, len, "p%u", attrs->port_number);
5452 n = snprintf(name, len, "p%us%u", attrs->port_number,
5453 attrs->split_subport_number);
5455 case DEVLINK_PORT_FLAVOUR_CPU:
5456 case DEVLINK_PORT_FLAVOUR_DSA:
5457 /* As CPU and DSA ports do not have a netdevice associated
5458 * case should not ever happen.
5469 EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
5471 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
5472 u32 size, u16 ingress_pools_count,
5473 u16 egress_pools_count, u16 ingress_tc_count,
5474 u16 egress_tc_count)
5476 struct devlink_sb *devlink_sb;
5479 mutex_lock(&devlink->lock);
5480 if (devlink_sb_index_exists(devlink, sb_index)) {
5485 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
5490 devlink_sb->index = sb_index;
5491 devlink_sb->size = size;
5492 devlink_sb->ingress_pools_count = ingress_pools_count;
5493 devlink_sb->egress_pools_count = egress_pools_count;
5494 devlink_sb->ingress_tc_count = ingress_tc_count;
5495 devlink_sb->egress_tc_count = egress_tc_count;
5496 list_add_tail(&devlink_sb->list, &devlink->sb_list);
5498 mutex_unlock(&devlink->lock);
5501 EXPORT_SYMBOL_GPL(devlink_sb_register);
5503 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
5505 struct devlink_sb *devlink_sb;
5507 mutex_lock(&devlink->lock);
5508 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
5509 WARN_ON(!devlink_sb);
5510 list_del(&devlink_sb->list);
5511 mutex_unlock(&devlink->lock);
5514 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
5517 * devlink_dpipe_headers_register - register dpipe headers
5520 * @dpipe_headers: dpipe header array
5522 * Register the headers supported by hardware.
5524 int devlink_dpipe_headers_register(struct devlink *devlink,
5525 struct devlink_dpipe_headers *dpipe_headers)
5527 mutex_lock(&devlink->lock);
5528 devlink->dpipe_headers = dpipe_headers;
5529 mutex_unlock(&devlink->lock);
5532 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
5535 * devlink_dpipe_headers_unregister - unregister dpipe headers
5539 * Unregister the headers supported by hardware.
5541 void devlink_dpipe_headers_unregister(struct devlink *devlink)
5543 mutex_lock(&devlink->lock);
5544 devlink->dpipe_headers = NULL;
5545 mutex_unlock(&devlink->lock);
5547 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
5550 * devlink_dpipe_table_counter_enabled - check if counter allocation
5553 * @table_name: tables name
5555 * Used by driver to check if counter allocation is required.
5556 * After counter allocation is turned on the table entries
5557 * are updated to include counter statistics.
5559 * After that point on the driver must respect the counter
5560 * state so that each entry added to the table is added
5563 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
5564 const char *table_name)
5566 struct devlink_dpipe_table *table;
5570 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5574 enabled = table->counters_enabled;
5578 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
5581 * devlink_dpipe_table_register - register dpipe table
5584 * @table_name: table name
5585 * @table_ops: table ops
5587 * @counter_control_extern: external control for counters
5589 int devlink_dpipe_table_register(struct devlink *devlink,
5590 const char *table_name,
5591 struct devlink_dpipe_table_ops *table_ops,
5592 void *priv, bool counter_control_extern)
5594 struct devlink_dpipe_table *table;
5596 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
5599 if (WARN_ON(!table_ops->size_get))
5602 table = kzalloc(sizeof(*table), GFP_KERNEL);
5606 table->name = table_name;
5607 table->table_ops = table_ops;
5609 table->counter_control_extern = counter_control_extern;
5611 mutex_lock(&devlink->lock);
5612 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
5613 mutex_unlock(&devlink->lock);
5616 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
5619 * devlink_dpipe_table_unregister - unregister dpipe table
5622 * @table_name: table name
5624 void devlink_dpipe_table_unregister(struct devlink *devlink,
5625 const char *table_name)
5627 struct devlink_dpipe_table *table;
5629 mutex_lock(&devlink->lock);
5630 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5634 list_del_rcu(&table->list);
5635 mutex_unlock(&devlink->lock);
5636 kfree_rcu(table, rcu);
5639 mutex_unlock(&devlink->lock);
5641 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
5644 * devlink_resource_register - devlink resource register
5647 * @resource_name: resource's name
5648 * @resource_size: resource's size
5649 * @resource_id: resource's id
5650 * @parent_resource_id: resource's parent id
5651 * @size_params: size parameters
5653 int devlink_resource_register(struct devlink *devlink,
5654 const char *resource_name,
5657 u64 parent_resource_id,
5658 const struct devlink_resource_size_params *size_params)
5660 struct devlink_resource *resource;
5661 struct list_head *resource_list;
5665 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
5667 mutex_lock(&devlink->lock);
5668 resource = devlink_resource_find(devlink, NULL, resource_id);
5674 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
5680 if (top_hierarchy) {
5681 resource_list = &devlink->resource_list;
5683 struct devlink_resource *parent_resource;
5685 parent_resource = devlink_resource_find(devlink, NULL,
5686 parent_resource_id);
5687 if (parent_resource) {
5688 resource_list = &parent_resource->resource_list;
5689 resource->parent = parent_resource;
5697 resource->name = resource_name;
5698 resource->size = resource_size;
5699 resource->size_new = resource_size;
5700 resource->id = resource_id;
5701 resource->size_valid = true;
5702 memcpy(&resource->size_params, size_params,
5703 sizeof(resource->size_params));
5704 INIT_LIST_HEAD(&resource->resource_list);
5705 list_add_tail(&resource->list, resource_list);
5707 mutex_unlock(&devlink->lock);
5710 EXPORT_SYMBOL_GPL(devlink_resource_register);
5713 * devlink_resources_unregister - free all resources
5716 * @resource: resource
5718 void devlink_resources_unregister(struct devlink *devlink,
5719 struct devlink_resource *resource)
5721 struct devlink_resource *tmp, *child_resource;
5722 struct list_head *resource_list;
5725 resource_list = &resource->resource_list;
5727 resource_list = &devlink->resource_list;
5730 mutex_lock(&devlink->lock);
5732 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
5733 devlink_resources_unregister(devlink, child_resource);
5734 list_del(&child_resource->list);
5735 kfree(child_resource);
5739 mutex_unlock(&devlink->lock);
5741 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
5744 * devlink_resource_size_get - get and update size
5747 * @resource_id: the requested resource id
5748 * @p_resource_size: ptr to update
5750 int devlink_resource_size_get(struct devlink *devlink,
5752 u64 *p_resource_size)
5754 struct devlink_resource *resource;
5757 mutex_lock(&devlink->lock);
5758 resource = devlink_resource_find(devlink, NULL, resource_id);
5763 *p_resource_size = resource->size_new;
5764 resource->size = resource->size_new;
5766 mutex_unlock(&devlink->lock);
5769 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
5772 * devlink_dpipe_table_resource_set - set the resource id
5775 * @table_name: table name
5776 * @resource_id: resource id
5777 * @resource_units: number of resource's units consumed per table's entry
5779 int devlink_dpipe_table_resource_set(struct devlink *devlink,
5780 const char *table_name, u64 resource_id,
5783 struct devlink_dpipe_table *table;
5786 mutex_lock(&devlink->lock);
5787 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5793 table->resource_id = resource_id;
5794 table->resource_units = resource_units;
5795 table->resource_valid = true;
5797 mutex_unlock(&devlink->lock);
5800 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
5803 * devlink_resource_occ_get_register - register occupancy getter
5806 * @resource_id: resource id
5807 * @occ_get: occupancy getter callback
5808 * @occ_get_priv: occupancy getter callback priv
5810 void devlink_resource_occ_get_register(struct devlink *devlink,
5812 devlink_resource_occ_get_t *occ_get,
5815 struct devlink_resource *resource;
5817 mutex_lock(&devlink->lock);
5818 resource = devlink_resource_find(devlink, NULL, resource_id);
5819 if (WARN_ON(!resource))
5821 WARN_ON(resource->occ_get);
5823 resource->occ_get = occ_get;
5824 resource->occ_get_priv = occ_get_priv;
5826 mutex_unlock(&devlink->lock);
5828 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
5831 * devlink_resource_occ_get_unregister - unregister occupancy getter
5834 * @resource_id: resource id
5836 void devlink_resource_occ_get_unregister(struct devlink *devlink,
5839 struct devlink_resource *resource;
5841 mutex_lock(&devlink->lock);
5842 resource = devlink_resource_find(devlink, NULL, resource_id);
5843 if (WARN_ON(!resource))
5845 WARN_ON(!resource->occ_get);
5847 resource->occ_get = NULL;
5848 resource->occ_get_priv = NULL;
5850 mutex_unlock(&devlink->lock);
5852 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
5854 static int devlink_param_verify(const struct devlink_param *param)
5856 if (!param || !param->name || !param->supported_cmodes)
5859 return devlink_param_generic_verify(param);
5861 return devlink_param_driver_verify(param);
5864 static int __devlink_params_register(struct devlink *devlink,
5865 unsigned int port_index,
5866 struct list_head *param_list,
5867 const struct devlink_param *params,
5868 size_t params_count,
5869 enum devlink_command reg_cmd,
5870 enum devlink_command unreg_cmd)
5872 const struct devlink_param *param = params;
5876 mutex_lock(&devlink->lock);
5877 for (i = 0; i < params_count; i++, param++) {
5878 err = devlink_param_verify(param);
5882 err = devlink_param_register_one(devlink, port_index,
5883 param_list, param, reg_cmd);
5888 mutex_unlock(&devlink->lock);
5894 for (param--; i > 0; i--, param--)
5895 devlink_param_unregister_one(devlink, port_index, param_list,
5898 mutex_unlock(&devlink->lock);
5902 static void __devlink_params_unregister(struct devlink *devlink,
5903 unsigned int port_index,
5904 struct list_head *param_list,
5905 const struct devlink_param *params,
5906 size_t params_count,
5907 enum devlink_command cmd)
5909 const struct devlink_param *param = params;
5912 mutex_lock(&devlink->lock);
5913 for (i = 0; i < params_count; i++, param++)
5914 devlink_param_unregister_one(devlink, 0, param_list, param,
5916 mutex_unlock(&devlink->lock);
5920 * devlink_params_register - register configuration parameters
5923 * @params: configuration parameters array
5924 * @params_count: number of parameters provided
5926 * Register the configuration parameters supported by the driver.
5928 int devlink_params_register(struct devlink *devlink,
5929 const struct devlink_param *params,
5930 size_t params_count)
5932 return __devlink_params_register(devlink, 0, &devlink->param_list,
5933 params, params_count,
5934 DEVLINK_CMD_PARAM_NEW,
5935 DEVLINK_CMD_PARAM_DEL);
5937 EXPORT_SYMBOL_GPL(devlink_params_register);
5940 * devlink_params_unregister - unregister configuration parameters
5942 * @params: configuration parameters to unregister
5943 * @params_count: number of parameters provided
5945 void devlink_params_unregister(struct devlink *devlink,
5946 const struct devlink_param *params,
5947 size_t params_count)
5949 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
5950 params, params_count,
5951 DEVLINK_CMD_PARAM_DEL);
5953 EXPORT_SYMBOL_GPL(devlink_params_unregister);
5956 * devlink_params_publish - publish configuration parameters
5960 * Publish previously registered configuration parameters.
5962 void devlink_params_publish(struct devlink *devlink)
5964 struct devlink_param_item *param_item;
5966 list_for_each_entry(param_item, &devlink->param_list, list) {
5967 if (param_item->published)
5969 param_item->published = true;
5970 devlink_param_notify(devlink, 0, param_item,
5971 DEVLINK_CMD_PARAM_NEW);
5974 EXPORT_SYMBOL_GPL(devlink_params_publish);
5977 * devlink_params_unpublish - unpublish configuration parameters
5981 * Unpublish previously registered configuration parameters.
5983 void devlink_params_unpublish(struct devlink *devlink)
5985 struct devlink_param_item *param_item;
5987 list_for_each_entry(param_item, &devlink->param_list, list) {
5988 if (!param_item->published)
5990 param_item->published = false;
5991 devlink_param_notify(devlink, 0, param_item,
5992 DEVLINK_CMD_PARAM_DEL);
5995 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
5998 * devlink_port_params_register - register port configuration parameters
6000 * @devlink_port: devlink port
6001 * @params: configuration parameters array
6002 * @params_count: number of parameters provided
6004 * Register the configuration parameters supported by the port.
6006 int devlink_port_params_register(struct devlink_port *devlink_port,
6007 const struct devlink_param *params,
6008 size_t params_count)
6010 return __devlink_params_register(devlink_port->devlink,
6011 devlink_port->index,
6012 &devlink_port->param_list, params,
6014 DEVLINK_CMD_PORT_PARAM_NEW,
6015 DEVLINK_CMD_PORT_PARAM_DEL);
6017 EXPORT_SYMBOL_GPL(devlink_port_params_register);
6020 * devlink_port_params_unregister - unregister port configuration
6023 * @devlink_port: devlink port
6024 * @params: configuration parameters array
6025 * @params_count: number of parameters provided
6027 void devlink_port_params_unregister(struct devlink_port *devlink_port,
6028 const struct devlink_param *params,
6029 size_t params_count)
6031 return __devlink_params_unregister(devlink_port->devlink,
6032 devlink_port->index,
6033 &devlink_port->param_list,
6034 params, params_count,
6035 DEVLINK_CMD_PORT_PARAM_DEL);
6037 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
6040 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
6041 union devlink_param_value *init_val)
6043 struct devlink_param_item *param_item;
6045 param_item = devlink_param_find_by_id(param_list, param_id);
6049 if (!param_item->driverinit_value_valid ||
6050 !devlink_param_cmode_is_supported(param_item->param,
6051 DEVLINK_PARAM_CMODE_DRIVERINIT))
6054 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6055 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
6057 *init_val = param_item->driverinit_value;
6063 __devlink_param_driverinit_value_set(struct devlink *devlink,
6064 unsigned int port_index,
6065 struct list_head *param_list, u32 param_id,
6066 union devlink_param_value init_val,
6067 enum devlink_command cmd)
6069 struct devlink_param_item *param_item;
6071 param_item = devlink_param_find_by_id(param_list, param_id);
6075 if (!devlink_param_cmode_is_supported(param_item->param,
6076 DEVLINK_PARAM_CMODE_DRIVERINIT))
6079 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6080 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
6082 param_item->driverinit_value = init_val;
6083 param_item->driverinit_value_valid = true;
6085 devlink_param_notify(devlink, port_index, param_item, cmd);
6090 * devlink_param_driverinit_value_get - get configuration parameter
6091 * value for driver initializing
6094 * @param_id: parameter ID
6095 * @init_val: value of parameter in driverinit configuration mode
6097 * This function should be used by the driver to get driverinit
6098 * configuration for initialization after reload command.
6100 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
6101 union devlink_param_value *init_val)
6103 if (!devlink->ops->reload)
6106 return __devlink_param_driverinit_value_get(&devlink->param_list,
6107 param_id, init_val);
6109 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
6112 * devlink_param_driverinit_value_set - set value of configuration
6113 * parameter for driverinit
6114 * configuration mode
6117 * @param_id: parameter ID
6118 * @init_val: value of parameter to set for driverinit configuration mode
6120 * This function should be used by the driver to set driverinit
6121 * configuration mode default value.
6123 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
6124 union devlink_param_value init_val)
6126 return __devlink_param_driverinit_value_set(devlink, 0,
6127 &devlink->param_list,
6129 DEVLINK_CMD_PARAM_NEW);
6131 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
6134 * devlink_port_param_driverinit_value_get - get configuration parameter
6135 * value for driver initializing
6137 * @devlink_port: devlink_port
6138 * @param_id: parameter ID
6139 * @init_val: value of parameter in driverinit configuration mode
6141 * This function should be used by the driver to get driverinit
6142 * configuration for initialization after reload command.
6144 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
6146 union devlink_param_value *init_val)
6148 struct devlink *devlink = devlink_port->devlink;
6150 if (!devlink->ops->reload)
6153 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
6154 param_id, init_val);
6156 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
6159 * devlink_port_param_driverinit_value_set - set value of configuration
6160 * parameter for driverinit
6161 * configuration mode
6163 * @devlink_port: devlink_port
6164 * @param_id: parameter ID
6165 * @init_val: value of parameter to set for driverinit configuration mode
6167 * This function should be used by the driver to set driverinit
6168 * configuration mode default value.
6170 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
6172 union devlink_param_value init_val)
6174 return __devlink_param_driverinit_value_set(devlink_port->devlink,
6175 devlink_port->index,
6176 &devlink_port->param_list,
6178 DEVLINK_CMD_PORT_PARAM_NEW);
6180 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
6183 * devlink_param_value_changed - notify devlink on a parameter's value
6184 * change. Should be called by the driver
6185 * right after the change.
6188 * @param_id: parameter ID
6190 * This function should be used by the driver to notify devlink on value
6191 * change, excluding driverinit configuration mode.
6192 * For driverinit configuration mode driver should use the function
6194 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
6196 struct devlink_param_item *param_item;
6198 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
6199 WARN_ON(!param_item);
6201 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
6203 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
6206 * devlink_port_param_value_changed - notify devlink on a parameter's value
6207 * change. Should be called by the driver
6208 * right after the change.
6210 * @devlink_port: devlink_port
6211 * @param_id: parameter ID
6213 * This function should be used by the driver to notify devlink on value
6214 * change, excluding driverinit configuration mode.
6215 * For driverinit configuration mode driver should use the function
6216 * devlink_port_param_driverinit_value_set() instead.
6218 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
6221 struct devlink_param_item *param_item;
6223 param_item = devlink_param_find_by_id(&devlink_port->param_list,
6225 WARN_ON(!param_item);
6227 devlink_param_notify(devlink_port->devlink, devlink_port->index,
6228 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
6230 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
6233 * devlink_param_value_str_fill - Safely fill-up the string preventing
6234 * from overflow of the preallocated buffer
6236 * @dst_val: destination devlink_param_value
6237 * @src: source buffer
6239 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
6244 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
6245 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
6247 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
6250 * devlink_region_create - create a new address region
6253 * @region_name: region name
6254 * @region_max_snapshots: Maximum supported number of snapshots for region
6255 * @region_size: size of region
6257 struct devlink_region *devlink_region_create(struct devlink *devlink,
6258 const char *region_name,
6259 u32 region_max_snapshots,
6262 struct devlink_region *region;
6265 mutex_lock(&devlink->lock);
6267 if (devlink_region_get_by_name(devlink, region_name)) {
6272 region = kzalloc(sizeof(*region), GFP_KERNEL);
6278 region->devlink = devlink;
6279 region->max_snapshots = region_max_snapshots;
6280 region->name = region_name;
6281 region->size = region_size;
6282 INIT_LIST_HEAD(®ion->snapshot_list);
6283 list_add_tail(®ion->list, &devlink->region_list);
6284 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
6286 mutex_unlock(&devlink->lock);
6290 mutex_unlock(&devlink->lock);
6291 return ERR_PTR(err);
6293 EXPORT_SYMBOL_GPL(devlink_region_create);
6296 * devlink_region_destroy - destroy address region
6298 * @region: devlink region to destroy
6300 void devlink_region_destroy(struct devlink_region *region)
6302 struct devlink *devlink = region->devlink;
6303 struct devlink_snapshot *snapshot, *ts;
6305 mutex_lock(&devlink->lock);
6307 /* Free all snapshots of region */
6308 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
6309 devlink_region_snapshot_del(snapshot);
6311 list_del(®ion->list);
6313 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
6314 mutex_unlock(&devlink->lock);
6317 EXPORT_SYMBOL_GPL(devlink_region_destroy);
6320 * devlink_region_shapshot_id_get - get snapshot ID
6322 * This callback should be called when adding a new snapshot,
6323 * Driver should use the same id for multiple snapshots taken
6324 * on multiple regions at the same time/by the same trigger.
6328 u32 devlink_region_shapshot_id_get(struct devlink *devlink)
6332 mutex_lock(&devlink->lock);
6333 id = ++devlink->snapshot_id;
6334 mutex_unlock(&devlink->lock);
6338 EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
6341 * devlink_region_snapshot_create - create a new snapshot
6342 * This will add a new snapshot of a region. The snapshot
6343 * will be stored on the region struct and can be accessed
6344 * from devlink. This is useful for future analyses of snapshots.
6345 * Multiple snapshots can be created on a region.
6346 * The @snapshot_id should be obtained using the getter function.
6348 * @region: devlink region of the snapshot
6349 * @data_len: size of snapshot data
6350 * @data: snapshot data
6351 * @snapshot_id: snapshot id to be created
6352 * @data_destructor: pointer to destructor function to free data
6354 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
6355 u8 *data, u32 snapshot_id,
6356 devlink_snapshot_data_dest_t *data_destructor)
6358 struct devlink *devlink = region->devlink;
6359 struct devlink_snapshot *snapshot;
6362 mutex_lock(&devlink->lock);
6364 /* check if region can hold one more snapshot */
6365 if (region->cur_snapshots == region->max_snapshots) {
6370 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6375 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
6381 snapshot->id = snapshot_id;
6382 snapshot->region = region;
6383 snapshot->data = data;
6384 snapshot->data_len = data_len;
6385 snapshot->data_destructor = data_destructor;
6387 list_add_tail(&snapshot->list, ®ion->snapshot_list);
6389 region->cur_snapshots++;
6391 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
6392 mutex_unlock(&devlink->lock);
6396 mutex_unlock(&devlink->lock);
6399 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
6401 static void __devlink_compat_running_version(struct devlink *devlink,
6402 char *buf, size_t len)
6404 const struct nlattr *nlattr;
6405 struct devlink_info_req req;
6406 struct sk_buff *msg;
6409 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6414 err = devlink->ops->info_get(devlink, &req, NULL);
6418 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
6419 const struct nlattr *kv;
6422 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
6425 nla_for_each_nested(kv, nlattr, rem_kv) {
6426 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
6429 strlcat(buf, nla_data(kv), len);
6430 strlcat(buf, " ", len);
6437 void devlink_compat_running_version(struct net_device *dev,
6438 char *buf, size_t len)
6440 struct devlink *devlink;
6445 mutex_lock(&devlink_mutex);
6446 devlink = netdev_to_devlink(dev);
6447 if (!devlink || !devlink->ops->info_get)
6450 mutex_lock(&devlink->lock);
6451 __devlink_compat_running_version(devlink, buf, len);
6452 mutex_unlock(&devlink->lock);
6454 mutex_unlock(&devlink_mutex);
6460 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
6462 struct devlink *devlink;
6463 int ret = -EOPNOTSUPP;
6468 mutex_lock(&devlink_mutex);
6469 devlink = netdev_to_devlink(dev);
6470 if (!devlink || !devlink->ops->flash_update)
6473 mutex_lock(&devlink->lock);
6474 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
6475 mutex_unlock(&devlink->lock);
6477 mutex_unlock(&devlink_mutex);
6485 static int __init devlink_init(void)
6487 return genl_register_family(&devlink_nl_family);
6490 subsys_initcall(devlink_init);