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.
6 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
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 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
34 static LIST_HEAD(devlink_list);
38 * An overall lock guarding every operation coming from userspace.
39 * It also guards devlink devices list and it is taken when
40 * driver registers/unregisters it.
42 static DEFINE_MUTEX(devlink_mutex);
46 * Shared lock to guard lists of ports in all devlink devices.
48 static DEFINE_MUTEX(devlink_port_mutex);
50 static struct net *devlink_net(const struct devlink *devlink)
52 return read_pnet(&devlink->_net);
55 static void devlink_net_set(struct devlink *devlink, struct net *net)
57 write_pnet(&devlink->_net, net);
60 static struct devlink *devlink_get_from_attrs(struct net *net,
61 struct nlattr **attrs)
63 struct devlink *devlink;
67 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
68 return ERR_PTR(-EINVAL);
70 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
71 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
73 list_for_each_entry(devlink, &devlink_list, list) {
74 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
75 strcmp(dev_name(devlink->dev), devname) == 0 &&
76 net_eq(devlink_net(devlink), net))
80 return ERR_PTR(-ENODEV);
83 static struct devlink *devlink_get_from_info(struct genl_info *info)
85 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
88 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
91 struct devlink_port *devlink_port;
93 list_for_each_entry(devlink_port, &devlink->port_list, list) {
94 if (devlink_port->index == port_index)
100 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
102 return devlink_port_get_by_index(devlink, port_index);
105 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
106 struct nlattr **attrs)
108 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
109 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
110 struct devlink_port *devlink_port;
112 devlink_port = devlink_port_get_by_index(devlink, port_index);
114 return ERR_PTR(-ENODEV);
117 return ERR_PTR(-EINVAL);
120 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
121 struct genl_info *info)
123 return devlink_port_get_from_attrs(devlink, info->attrs);
127 struct list_head list;
130 u16 ingress_pools_count;
131 u16 egress_pools_count;
132 u16 ingress_tc_count;
136 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
138 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
141 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
142 unsigned int sb_index)
144 struct devlink_sb *devlink_sb;
146 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
147 if (devlink_sb->index == sb_index)
153 static bool devlink_sb_index_exists(struct devlink *devlink,
154 unsigned int sb_index)
156 return devlink_sb_get_by_index(devlink, sb_index);
159 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
160 struct nlattr **attrs)
162 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
163 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
164 struct devlink_sb *devlink_sb;
166 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
168 return ERR_PTR(-ENODEV);
171 return ERR_PTR(-EINVAL);
174 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
175 struct genl_info *info)
177 return devlink_sb_get_from_attrs(devlink, info->attrs);
180 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
181 struct nlattr **attrs,
186 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
189 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
190 if (val >= devlink_sb_pool_count(devlink_sb))
196 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
197 struct genl_info *info,
200 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
205 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
206 enum devlink_sb_pool_type *p_pool_type)
210 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
213 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
214 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
215 val != DEVLINK_SB_POOL_TYPE_EGRESS)
222 devlink_sb_pool_type_get_from_info(struct genl_info *info,
223 enum devlink_sb_pool_type *p_pool_type)
225 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
229 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
230 enum devlink_sb_threshold_type *p_th_type)
234 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
237 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
238 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
239 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
246 devlink_sb_th_type_get_from_info(struct genl_info *info,
247 enum devlink_sb_threshold_type *p_th_type)
249 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
253 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
254 struct nlattr **attrs,
255 enum devlink_sb_pool_type pool_type,
260 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
263 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
264 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
265 val >= devlink_sb->ingress_tc_count)
267 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
268 val >= devlink_sb->egress_tc_count)
275 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
276 struct genl_info *info,
277 enum devlink_sb_pool_type pool_type,
280 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
281 pool_type, p_tc_index);
284 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
285 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
286 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
287 #define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3)
288 /* port is not needed but we need to ensure they don't
289 * change in the middle of command
292 static int devlink_nl_pre_doit(const struct genl_ops *ops,
293 struct sk_buff *skb, struct genl_info *info)
295 struct devlink *devlink;
297 mutex_lock(&devlink_mutex);
298 devlink = devlink_get_from_info(info);
299 if (IS_ERR(devlink)) {
300 mutex_unlock(&devlink_mutex);
301 return PTR_ERR(devlink);
303 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
304 info->user_ptr[0] = devlink;
305 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
306 struct devlink_port *devlink_port;
308 mutex_lock(&devlink_port_mutex);
309 devlink_port = devlink_port_get_from_info(devlink, info);
310 if (IS_ERR(devlink_port)) {
311 mutex_unlock(&devlink_port_mutex);
312 mutex_unlock(&devlink_mutex);
313 return PTR_ERR(devlink_port);
315 info->user_ptr[0] = devlink_port;
317 if (ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS) {
318 mutex_lock(&devlink_port_mutex);
320 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
321 struct devlink_sb *devlink_sb;
323 devlink_sb = devlink_sb_get_from_info(devlink, info);
324 if (IS_ERR(devlink_sb)) {
325 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT)
326 mutex_unlock(&devlink_port_mutex);
327 mutex_unlock(&devlink_mutex);
328 return PTR_ERR(devlink_sb);
330 info->user_ptr[1] = devlink_sb;
335 static void devlink_nl_post_doit(const struct genl_ops *ops,
336 struct sk_buff *skb, struct genl_info *info)
338 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT ||
339 ops->internal_flags & DEVLINK_NL_FLAG_LOCK_PORTS)
340 mutex_unlock(&devlink_port_mutex);
341 mutex_unlock(&devlink_mutex);
344 static struct genl_family devlink_nl_family;
346 enum devlink_multicast_groups {
347 DEVLINK_MCGRP_CONFIG,
350 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
351 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
354 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
356 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
358 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
363 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
364 enum devlink_command cmd, u32 portid,
369 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
373 if (devlink_nl_put_handle(msg, devlink))
374 goto nla_put_failure;
376 genlmsg_end(msg, hdr);
380 genlmsg_cancel(msg, hdr);
384 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
389 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
395 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
401 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
402 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
405 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
406 struct devlink_port *devlink_port,
407 enum devlink_command cmd, u32 portid,
412 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
416 if (devlink_nl_put_handle(msg, devlink))
417 goto nla_put_failure;
418 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
419 goto nla_put_failure;
420 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
421 goto nla_put_failure;
422 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
423 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
424 devlink_port->desired_type))
425 goto nla_put_failure;
426 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
427 struct net_device *netdev = devlink_port->type_dev;
430 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
432 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
434 goto nla_put_failure;
436 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
437 struct ib_device *ibdev = devlink_port->type_dev;
440 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
442 goto nla_put_failure;
444 if (devlink_port->split &&
445 nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
446 devlink_port->split_group))
447 goto nla_put_failure;
449 genlmsg_end(msg, hdr);
453 genlmsg_cancel(msg, hdr);
457 static void devlink_port_notify(struct devlink_port *devlink_port,
458 enum devlink_command cmd)
460 struct devlink *devlink = devlink_port->devlink;
464 if (!devlink_port->registered)
467 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
469 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
473 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
479 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
480 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
483 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
485 struct devlink *devlink = info->user_ptr[0];
489 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
493 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
494 info->snd_portid, info->snd_seq, 0);
500 return genlmsg_reply(msg, info);
503 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
504 struct netlink_callback *cb)
506 struct devlink *devlink;
507 int start = cb->args[0];
511 mutex_lock(&devlink_mutex);
512 list_for_each_entry(devlink, &devlink_list, list) {
513 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
519 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
520 NETLINK_CB(cb->skb).portid,
521 cb->nlh->nlmsg_seq, NLM_F_MULTI);
527 mutex_unlock(&devlink_mutex);
533 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
534 struct genl_info *info)
536 struct devlink_port *devlink_port = info->user_ptr[0];
537 struct devlink *devlink = devlink_port->devlink;
541 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
545 err = devlink_nl_port_fill(msg, devlink, devlink_port,
546 DEVLINK_CMD_PORT_NEW,
547 info->snd_portid, info->snd_seq, 0);
553 return genlmsg_reply(msg, info);
556 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
557 struct netlink_callback *cb)
559 struct devlink *devlink;
560 struct devlink_port *devlink_port;
561 int start = cb->args[0];
565 mutex_lock(&devlink_mutex);
566 mutex_lock(&devlink_port_mutex);
567 list_for_each_entry(devlink, &devlink_list, list) {
568 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
570 list_for_each_entry(devlink_port, &devlink->port_list, list) {
575 err = devlink_nl_port_fill(msg, devlink, devlink_port,
577 NETLINK_CB(cb->skb).portid,
586 mutex_unlock(&devlink_port_mutex);
587 mutex_unlock(&devlink_mutex);
593 static int devlink_port_type_set(struct devlink *devlink,
594 struct devlink_port *devlink_port,
595 enum devlink_port_type port_type)
600 if (devlink->ops && devlink->ops->port_type_set) {
601 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
603 if (port_type == devlink_port->type)
605 err = devlink->ops->port_type_set(devlink_port, port_type);
608 devlink_port->desired_type = port_type;
609 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
615 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
616 struct genl_info *info)
618 struct devlink_port *devlink_port = info->user_ptr[0];
619 struct devlink *devlink = devlink_port->devlink;
622 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
623 enum devlink_port_type port_type;
625 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
626 err = devlink_port_type_set(devlink, devlink_port, port_type);
633 static int devlink_port_split(struct devlink *devlink,
634 u32 port_index, u32 count)
637 if (devlink->ops && devlink->ops->port_split)
638 return devlink->ops->port_split(devlink, port_index, count);
642 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
643 struct genl_info *info)
645 struct devlink *devlink = info->user_ptr[0];
649 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
650 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
653 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
654 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
655 return devlink_port_split(devlink, port_index, count);
658 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
661 if (devlink->ops && devlink->ops->port_unsplit)
662 return devlink->ops->port_unsplit(devlink, port_index);
666 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
667 struct genl_info *info)
669 struct devlink *devlink = info->user_ptr[0];
672 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
675 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
676 return devlink_port_unsplit(devlink, port_index);
679 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
680 struct devlink_sb *devlink_sb,
681 enum devlink_command cmd, u32 portid,
686 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
690 if (devlink_nl_put_handle(msg, devlink))
691 goto nla_put_failure;
692 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
693 goto nla_put_failure;
694 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
695 goto nla_put_failure;
696 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
697 devlink_sb->ingress_pools_count))
698 goto nla_put_failure;
699 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
700 devlink_sb->egress_pools_count))
701 goto nla_put_failure;
702 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
703 devlink_sb->ingress_tc_count))
704 goto nla_put_failure;
705 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
706 devlink_sb->egress_tc_count))
707 goto nla_put_failure;
709 genlmsg_end(msg, hdr);
713 genlmsg_cancel(msg, hdr);
717 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
718 struct genl_info *info)
720 struct devlink *devlink = info->user_ptr[0];
721 struct devlink_sb *devlink_sb = info->user_ptr[1];
725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
729 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
731 info->snd_portid, info->snd_seq, 0);
737 return genlmsg_reply(msg, info);
740 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
741 struct netlink_callback *cb)
743 struct devlink *devlink;
744 struct devlink_sb *devlink_sb;
745 int start = cb->args[0];
749 mutex_lock(&devlink_mutex);
750 list_for_each_entry(devlink, &devlink_list, list) {
751 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
753 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
758 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
760 NETLINK_CB(cb->skb).portid,
769 mutex_unlock(&devlink_mutex);
775 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
776 struct devlink_sb *devlink_sb,
777 u16 pool_index, enum devlink_command cmd,
778 u32 portid, u32 seq, int flags)
780 struct devlink_sb_pool_info pool_info;
784 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
785 pool_index, &pool_info);
789 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
793 if (devlink_nl_put_handle(msg, devlink))
794 goto nla_put_failure;
795 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
796 goto nla_put_failure;
797 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
798 goto nla_put_failure;
799 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
800 goto nla_put_failure;
801 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
802 goto nla_put_failure;
803 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
804 pool_info.threshold_type))
805 goto nla_put_failure;
807 genlmsg_end(msg, hdr);
811 genlmsg_cancel(msg, hdr);
815 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
816 struct genl_info *info)
818 struct devlink *devlink = info->user_ptr[0];
819 struct devlink_sb *devlink_sb = info->user_ptr[1];
824 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
829 if (!devlink->ops || !devlink->ops->sb_pool_get)
832 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
836 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
837 DEVLINK_CMD_SB_POOL_NEW,
838 info->snd_portid, info->snd_seq, 0);
844 return genlmsg_reply(msg, info);
847 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
848 struct devlink *devlink,
849 struct devlink_sb *devlink_sb,
852 u16 pool_count = devlink_sb_pool_count(devlink_sb);
856 for (pool_index = 0; pool_index < pool_count; pool_index++) {
857 if (*p_idx < start) {
861 err = devlink_nl_sb_pool_fill(msg, devlink,
864 DEVLINK_CMD_SB_POOL_NEW,
865 portid, seq, NLM_F_MULTI);
873 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
874 struct netlink_callback *cb)
876 struct devlink *devlink;
877 struct devlink_sb *devlink_sb;
878 int start = cb->args[0];
882 mutex_lock(&devlink_mutex);
883 list_for_each_entry(devlink, &devlink_list, list) {
884 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
885 !devlink->ops || !devlink->ops->sb_pool_get)
887 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
888 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
890 NETLINK_CB(cb->skb).portid,
892 if (err && err != -EOPNOTSUPP)
897 mutex_unlock(&devlink_mutex);
903 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
904 u16 pool_index, u32 size,
905 enum devlink_sb_threshold_type threshold_type)
908 const struct devlink_ops *ops = devlink->ops;
910 if (ops && ops->sb_pool_set)
911 return ops->sb_pool_set(devlink, sb_index, pool_index,
912 size, threshold_type);
916 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
917 struct genl_info *info)
919 struct devlink *devlink = info->user_ptr[0];
920 struct devlink_sb *devlink_sb = info->user_ptr[1];
921 enum devlink_sb_threshold_type threshold_type;
926 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
931 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
935 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
938 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
939 return devlink_sb_pool_set(devlink, devlink_sb->index,
940 pool_index, size, threshold_type);
943 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
944 struct devlink *devlink,
945 struct devlink_port *devlink_port,
946 struct devlink_sb *devlink_sb,
948 enum devlink_command cmd,
949 u32 portid, u32 seq, int flags)
951 const struct devlink_ops *ops = devlink->ops;
956 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
957 pool_index, &threshold);
961 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
965 if (devlink_nl_put_handle(msg, devlink))
966 goto nla_put_failure;
967 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
968 goto nla_put_failure;
969 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
970 goto nla_put_failure;
971 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
972 goto nla_put_failure;
973 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
974 goto nla_put_failure;
976 if (ops->sb_occ_port_pool_get) {
980 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
981 pool_index, &cur, &max);
982 if (err && err != -EOPNOTSUPP)
985 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
986 goto nla_put_failure;
987 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
988 goto nla_put_failure;
992 genlmsg_end(msg, hdr);
996 genlmsg_cancel(msg, hdr);
1000 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1001 struct genl_info *info)
1003 struct devlink_port *devlink_port = info->user_ptr[0];
1004 struct devlink *devlink = devlink_port->devlink;
1005 struct devlink_sb *devlink_sb = info->user_ptr[1];
1006 struct sk_buff *msg;
1010 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1015 if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1022 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1023 devlink_sb, pool_index,
1024 DEVLINK_CMD_SB_PORT_POOL_NEW,
1025 info->snd_portid, info->snd_seq, 0);
1031 return genlmsg_reply(msg, info);
1034 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1035 struct devlink *devlink,
1036 struct devlink_sb *devlink_sb,
1037 u32 portid, u32 seq)
1039 struct devlink_port *devlink_port;
1040 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1044 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1045 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1046 if (*p_idx < start) {
1050 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1054 DEVLINK_CMD_SB_PORT_POOL_NEW,
1065 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1066 struct netlink_callback *cb)
1068 struct devlink *devlink;
1069 struct devlink_sb *devlink_sb;
1070 int start = cb->args[0];
1074 mutex_lock(&devlink_mutex);
1075 mutex_lock(&devlink_port_mutex);
1076 list_for_each_entry(devlink, &devlink_list, list) {
1077 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1078 !devlink->ops || !devlink->ops->sb_port_pool_get)
1080 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1081 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1082 devlink, devlink_sb,
1083 NETLINK_CB(cb->skb).portid,
1084 cb->nlh->nlmsg_seq);
1085 if (err && err != -EOPNOTSUPP)
1090 mutex_unlock(&devlink_port_mutex);
1091 mutex_unlock(&devlink_mutex);
1097 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1098 unsigned int sb_index, u16 pool_index,
1102 const struct devlink_ops *ops = devlink_port->devlink->ops;
1104 if (ops && ops->sb_port_pool_set)
1105 return ops->sb_port_pool_set(devlink_port, sb_index,
1106 pool_index, threshold);
1110 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1111 struct genl_info *info)
1113 struct devlink_port *devlink_port = info->user_ptr[0];
1114 struct devlink_sb *devlink_sb = info->user_ptr[1];
1119 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1124 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1127 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1128 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1129 pool_index, threshold);
1133 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1134 struct devlink_port *devlink_port,
1135 struct devlink_sb *devlink_sb, u16 tc_index,
1136 enum devlink_sb_pool_type pool_type,
1137 enum devlink_command cmd,
1138 u32 portid, u32 seq, int flags)
1140 const struct devlink_ops *ops = devlink->ops;
1146 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1147 tc_index, pool_type,
1148 &pool_index, &threshold);
1152 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1156 if (devlink_nl_put_handle(msg, devlink))
1157 goto nla_put_failure;
1158 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1159 goto nla_put_failure;
1160 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1161 goto nla_put_failure;
1162 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1163 goto nla_put_failure;
1164 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1165 goto nla_put_failure;
1166 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1167 goto nla_put_failure;
1168 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1169 goto nla_put_failure;
1171 if (ops->sb_occ_tc_port_bind_get) {
1175 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1177 tc_index, pool_type,
1179 if (err && err != -EOPNOTSUPP)
1182 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1183 goto nla_put_failure;
1184 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1185 goto nla_put_failure;
1189 genlmsg_end(msg, hdr);
1193 genlmsg_cancel(msg, hdr);
1197 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1198 struct genl_info *info)
1200 struct devlink_port *devlink_port = info->user_ptr[0];
1201 struct devlink *devlink = devlink_port->devlink;
1202 struct devlink_sb *devlink_sb = info->user_ptr[1];
1203 struct sk_buff *msg;
1204 enum devlink_sb_pool_type pool_type;
1208 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1212 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1213 pool_type, &tc_index);
1217 if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1220 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1224 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1225 devlink_sb, tc_index, pool_type,
1226 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1234 return genlmsg_reply(msg, info);
1237 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1238 int start, int *p_idx,
1239 struct devlink *devlink,
1240 struct devlink_sb *devlink_sb,
1241 u32 portid, u32 seq)
1243 struct devlink_port *devlink_port;
1247 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1249 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1250 if (*p_idx < start) {
1254 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1258 DEVLINK_SB_POOL_TYPE_INGRESS,
1259 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1267 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1268 if (*p_idx < start) {
1272 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1276 DEVLINK_SB_POOL_TYPE_EGRESS,
1277 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1289 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1290 struct netlink_callback *cb)
1292 struct devlink *devlink;
1293 struct devlink_sb *devlink_sb;
1294 int start = cb->args[0];
1298 mutex_lock(&devlink_mutex);
1299 mutex_lock(&devlink_port_mutex);
1300 list_for_each_entry(devlink, &devlink_list, list) {
1301 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1302 !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1304 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1305 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1308 NETLINK_CB(cb->skb).portid,
1309 cb->nlh->nlmsg_seq);
1310 if (err && err != -EOPNOTSUPP)
1315 mutex_unlock(&devlink_port_mutex);
1316 mutex_unlock(&devlink_mutex);
1322 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1323 unsigned int sb_index, u16 tc_index,
1324 enum devlink_sb_pool_type pool_type,
1325 u16 pool_index, u32 threshold)
1328 const struct devlink_ops *ops = devlink_port->devlink->ops;
1330 if (ops && ops->sb_tc_pool_bind_set)
1331 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1332 tc_index, pool_type,
1333 pool_index, threshold);
1337 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1338 struct genl_info *info)
1340 struct devlink_port *devlink_port = info->user_ptr[0];
1341 struct devlink_sb *devlink_sb = info->user_ptr[1];
1342 enum devlink_sb_pool_type pool_type;
1348 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1352 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1353 pool_type, &tc_index);
1357 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1362 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1365 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1366 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1367 tc_index, pool_type,
1368 pool_index, threshold);
1371 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1372 struct genl_info *info)
1374 struct devlink *devlink = info->user_ptr[0];
1375 struct devlink_sb *devlink_sb = info->user_ptr[1];
1376 const struct devlink_ops *ops = devlink->ops;
1378 if (ops && ops->sb_occ_snapshot)
1379 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1383 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1384 struct genl_info *info)
1386 struct devlink *devlink = info->user_ptr[0];
1387 struct devlink_sb *devlink_sb = info->user_ptr[1];
1388 const struct devlink_ops *ops = devlink->ops;
1390 if (ops && ops->sb_occ_max_clear)
1391 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1395 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1396 enum devlink_command cmd, u32 portid,
1399 const struct devlink_ops *ops = devlink->ops;
1400 u8 inline_mode, encap_mode;
1405 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1409 err = devlink_nl_put_handle(msg, devlink);
1411 goto nla_put_failure;
1413 if (ops->eswitch_mode_get) {
1414 err = ops->eswitch_mode_get(devlink, &mode);
1416 goto nla_put_failure;
1417 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1419 goto nla_put_failure;
1422 if (ops->eswitch_inline_mode_get) {
1423 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1425 goto nla_put_failure;
1426 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1429 goto nla_put_failure;
1432 if (ops->eswitch_encap_mode_get) {
1433 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1435 goto nla_put_failure;
1436 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1438 goto nla_put_failure;
1441 genlmsg_end(msg, hdr);
1445 genlmsg_cancel(msg, hdr);
1449 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1450 struct genl_info *info)
1452 struct devlink *devlink = info->user_ptr[0];
1453 const struct devlink_ops *ops = devlink->ops;
1454 struct sk_buff *msg;
1460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1464 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1465 info->snd_portid, info->snd_seq, 0);
1472 return genlmsg_reply(msg, info);
1475 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1476 struct genl_info *info)
1478 struct devlink *devlink = info->user_ptr[0];
1479 const struct devlink_ops *ops = devlink->ops;
1480 u8 inline_mode, encap_mode;
1487 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1488 if (!ops->eswitch_mode_set)
1490 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1491 err = ops->eswitch_mode_set(devlink, mode);
1496 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1497 if (!ops->eswitch_inline_mode_set)
1499 inline_mode = nla_get_u8(
1500 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1501 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1506 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1507 if (!ops->eswitch_encap_mode_set)
1509 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1510 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1518 int devlink_dpipe_match_put(struct sk_buff *skb,
1519 struct devlink_dpipe_match *match)
1521 struct devlink_dpipe_header *header = match->header;
1522 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1523 struct nlattr *match_attr;
1525 match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1529 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1530 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1531 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1532 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1533 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1534 goto nla_put_failure;
1536 nla_nest_end(skb, match_attr);
1540 nla_nest_cancel(skb, match_attr);
1543 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1545 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1546 struct sk_buff *skb)
1548 struct nlattr *matches_attr;
1550 matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1554 if (table->table_ops->matches_dump(table->priv, skb))
1555 goto nla_put_failure;
1557 nla_nest_end(skb, matches_attr);
1561 nla_nest_cancel(skb, matches_attr);
1565 int devlink_dpipe_action_put(struct sk_buff *skb,
1566 struct devlink_dpipe_action *action)
1568 struct devlink_dpipe_header *header = action->header;
1569 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1570 struct nlattr *action_attr;
1572 action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1576 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1577 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1578 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1579 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1580 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1581 goto nla_put_failure;
1583 nla_nest_end(skb, action_attr);
1587 nla_nest_cancel(skb, action_attr);
1590 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1592 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1593 struct sk_buff *skb)
1595 struct nlattr *actions_attr;
1597 actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1601 if (table->table_ops->actions_dump(table->priv, skb))
1602 goto nla_put_failure;
1604 nla_nest_end(skb, actions_attr);
1608 nla_nest_cancel(skb, actions_attr);
1612 static int devlink_dpipe_table_put(struct sk_buff *skb,
1613 struct devlink_dpipe_table *table)
1615 struct nlattr *table_attr;
1617 table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1621 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1622 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table->size,
1624 goto nla_put_failure;
1625 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1626 table->counters_enabled))
1627 goto nla_put_failure;
1629 if (devlink_dpipe_matches_put(table, skb))
1630 goto nla_put_failure;
1632 if (devlink_dpipe_actions_put(table, skb))
1633 goto nla_put_failure;
1635 nla_nest_end(skb, table_attr);
1639 nla_nest_cancel(skb, table_attr);
1643 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1644 struct genl_info *info)
1649 err = genlmsg_reply(*pskb, info);
1653 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1659 static int devlink_dpipe_tables_fill(struct genl_info *info,
1660 enum devlink_command cmd, int flags,
1661 struct list_head *dpipe_tables,
1662 const char *table_name)
1664 struct devlink *devlink = info->user_ptr[0];
1665 struct devlink_dpipe_table *table;
1666 struct nlattr *tables_attr;
1667 struct sk_buff *skb = NULL;
1668 struct nlmsghdr *nlh;
1674 table = list_first_entry(dpipe_tables,
1675 struct devlink_dpipe_table, list);
1677 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1681 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1682 &devlink_nl_family, NLM_F_MULTI, cmd);
1686 if (devlink_nl_put_handle(skb, devlink))
1687 goto nla_put_failure;
1688 tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1690 goto nla_put_failure;
1694 list_for_each_entry_from(table, dpipe_tables, list) {
1696 err = devlink_dpipe_table_put(skb, table);
1704 if (!strcmp(table->name, table_name)) {
1705 err = devlink_dpipe_table_put(skb, table);
1713 nla_nest_end(skb, tables_attr);
1714 genlmsg_end(skb, hdr);
1719 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1720 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1722 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1724 goto err_skb_send_alloc;
1728 return genlmsg_reply(skb, info);
1734 genlmsg_cancel(skb, hdr);
1739 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1740 struct genl_info *info)
1742 struct devlink *devlink = info->user_ptr[0];
1743 const char *table_name = NULL;
1745 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1746 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1748 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1749 &devlink->dpipe_table_list,
1753 static int devlink_dpipe_value_put(struct sk_buff *skb,
1754 struct devlink_dpipe_value *value)
1756 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1757 value->value_size, value->value))
1760 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1761 value->value_size, value->mask))
1763 if (value->mapping_valid)
1764 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1765 value->mapping_value))
1770 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1771 struct devlink_dpipe_value *value)
1775 if (devlink_dpipe_action_put(skb, value->action))
1777 if (devlink_dpipe_value_put(skb, value))
1782 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1783 struct devlink_dpipe_value *values,
1784 unsigned int values_count)
1786 struct nlattr *action_attr;
1790 for (i = 0; i < values_count; i++) {
1791 action_attr = nla_nest_start(skb,
1792 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1795 err = devlink_dpipe_action_value_put(skb, &values[i]);
1797 goto err_action_value_put;
1798 nla_nest_end(skb, action_attr);
1802 err_action_value_put:
1803 nla_nest_cancel(skb, action_attr);
1807 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1808 struct devlink_dpipe_value *value)
1812 if (devlink_dpipe_match_put(skb, value->match))
1814 if (devlink_dpipe_value_put(skb, value))
1819 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1820 struct devlink_dpipe_value *values,
1821 unsigned int values_count)
1823 struct nlattr *match_attr;
1827 for (i = 0; i < values_count; i++) {
1828 match_attr = nla_nest_start(skb,
1829 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1832 err = devlink_dpipe_match_value_put(skb, &values[i]);
1834 goto err_match_value_put;
1835 nla_nest_end(skb, match_attr);
1839 err_match_value_put:
1840 nla_nest_cancel(skb, match_attr);
1844 static int devlink_dpipe_entry_put(struct sk_buff *skb,
1845 struct devlink_dpipe_entry *entry)
1847 struct nlattr *entry_attr, *matches_attr, *actions_attr;
1850 entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
1854 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
1856 goto nla_put_failure;
1857 if (entry->counter_valid)
1858 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
1859 entry->counter, DEVLINK_ATTR_PAD))
1860 goto nla_put_failure;
1862 matches_attr = nla_nest_start(skb,
1863 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
1865 goto nla_put_failure;
1867 err = devlink_dpipe_match_values_put(skb, entry->match_values,
1868 entry->match_values_count);
1870 nla_nest_cancel(skb, matches_attr);
1871 goto err_match_values_put;
1873 nla_nest_end(skb, matches_attr);
1875 actions_attr = nla_nest_start(skb,
1876 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
1878 goto nla_put_failure;
1880 err = devlink_dpipe_action_values_put(skb, entry->action_values,
1881 entry->action_values_count);
1883 nla_nest_cancel(skb, actions_attr);
1884 goto err_action_values_put;
1886 nla_nest_end(skb, actions_attr);
1888 nla_nest_end(skb, entry_attr);
1893 err_match_values_put:
1894 err_action_values_put:
1895 nla_nest_cancel(skb, entry_attr);
1899 static struct devlink_dpipe_table *
1900 devlink_dpipe_table_find(struct list_head *dpipe_tables,
1901 const char *table_name)
1903 struct devlink_dpipe_table *table;
1905 list_for_each_entry_rcu(table, dpipe_tables, list) {
1906 if (!strcmp(table->name, table_name))
1912 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
1914 struct devlink *devlink;
1917 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
1922 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
1923 dump_ctx->info->snd_portid,
1924 dump_ctx->info->snd_seq,
1925 &devlink_nl_family, NLM_F_MULTI,
1928 goto nla_put_failure;
1930 devlink = dump_ctx->info->user_ptr[0];
1931 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
1932 goto nla_put_failure;
1933 dump_ctx->nest = nla_nest_start(dump_ctx->skb,
1934 DEVLINK_ATTR_DPIPE_ENTRIES);
1935 if (!dump_ctx->nest)
1936 goto nla_put_failure;
1940 genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr);
1941 nlmsg_free(dump_ctx->skb);
1944 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
1946 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
1947 struct devlink_dpipe_entry *entry)
1949 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
1951 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
1953 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
1955 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
1956 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
1959 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
1961 static int devlink_dpipe_entries_fill(struct genl_info *info,
1962 enum devlink_command cmd, int flags,
1963 struct devlink_dpipe_table *table)
1965 struct devlink_dpipe_dump_ctx dump_ctx;
1966 struct nlmsghdr *nlh;
1969 dump_ctx.skb = NULL;
1971 dump_ctx.info = info;
1973 err = table->table_ops->entries_dump(table->priv,
1974 table->counters_enabled,
1977 goto err_entries_dump;
1980 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
1981 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1983 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
1985 goto err_skb_send_alloc;
1988 return genlmsg_reply(dump_ctx.skb, info);
1992 genlmsg_cancel(dump_ctx.skb, dump_ctx.hdr);
1993 nlmsg_free(dump_ctx.skb);
1997 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
1998 struct genl_info *info)
2000 struct devlink *devlink = info->user_ptr[0];
2001 struct devlink_dpipe_table *table;
2002 const char *table_name;
2004 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2007 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2008 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2013 if (!table->table_ops->entries_dump)
2016 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2020 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2021 const struct devlink_dpipe_header *header)
2023 struct devlink_dpipe_field *field;
2024 struct nlattr *field_attr;
2027 for (i = 0; i < header->fields_count; i++) {
2028 field = &header->fields[i];
2029 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2032 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2033 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2034 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2035 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2036 goto nla_put_failure;
2037 nla_nest_end(skb, field_attr);
2042 nla_nest_cancel(skb, field_attr);
2046 static int devlink_dpipe_header_put(struct sk_buff *skb,
2047 struct devlink_dpipe_header *header)
2049 struct nlattr *fields_attr, *header_attr;
2052 header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
2056 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2057 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2058 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2059 goto nla_put_failure;
2061 fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2063 goto nla_put_failure;
2065 err = devlink_dpipe_fields_put(skb, header);
2067 nla_nest_cancel(skb, fields_attr);
2068 goto nla_put_failure;
2070 nla_nest_end(skb, fields_attr);
2071 nla_nest_end(skb, header_attr);
2076 nla_nest_cancel(skb, header_attr);
2080 static int devlink_dpipe_headers_fill(struct genl_info *info,
2081 enum devlink_command cmd, int flags,
2082 struct devlink_dpipe_headers *
2085 struct devlink *devlink = info->user_ptr[0];
2086 struct nlattr *headers_attr;
2087 struct sk_buff *skb = NULL;
2088 struct nlmsghdr *nlh;
2095 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2099 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2100 &devlink_nl_family, NLM_F_MULTI, cmd);
2104 if (devlink_nl_put_handle(skb, devlink))
2105 goto nla_put_failure;
2106 headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2108 goto nla_put_failure;
2111 for (; i < dpipe_headers->headers_count; i++) {
2112 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2120 nla_nest_end(skb, headers_attr);
2121 genlmsg_end(skb, hdr);
2122 if (i != dpipe_headers->headers_count)
2126 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2127 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2129 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2131 goto err_skb_send_alloc;
2134 return genlmsg_reply(skb, info);
2140 genlmsg_cancel(skb, hdr);
2145 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2146 struct genl_info *info)
2148 struct devlink *devlink = info->user_ptr[0];
2150 if (!devlink->dpipe_headers)
2152 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2153 0, devlink->dpipe_headers);
2156 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2157 const char *table_name,
2160 struct devlink_dpipe_table *table;
2162 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2167 if (table->counter_control_extern)
2170 if (!(table->counters_enabled ^ enable))
2173 table->counters_enabled = enable;
2174 if (table->table_ops->counters_set_update)
2175 table->table_ops->counters_set_update(table->priv, enable);
2179 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2180 struct genl_info *info)
2182 struct devlink *devlink = info->user_ptr[0];
2183 const char *table_name;
2184 bool counters_enable;
2186 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2187 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2190 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2191 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2193 return devlink_dpipe_table_counters_set(devlink, table_name,
2197 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
2198 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
2199 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
2200 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
2201 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
2202 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
2203 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
2204 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
2205 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
2206 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
2207 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
2208 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
2209 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
2210 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
2211 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
2212 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
2213 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
2214 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
2217 static const struct genl_ops devlink_nl_ops[] = {
2219 .cmd = DEVLINK_CMD_GET,
2220 .doit = devlink_nl_cmd_get_doit,
2221 .dumpit = devlink_nl_cmd_get_dumpit,
2222 .policy = devlink_nl_policy,
2223 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2224 /* can be retrieved by unprivileged users */
2227 .cmd = DEVLINK_CMD_PORT_GET,
2228 .doit = devlink_nl_cmd_port_get_doit,
2229 .dumpit = devlink_nl_cmd_port_get_dumpit,
2230 .policy = devlink_nl_policy,
2231 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
2232 /* can be retrieved by unprivileged users */
2235 .cmd = DEVLINK_CMD_PORT_SET,
2236 .doit = devlink_nl_cmd_port_set_doit,
2237 .policy = devlink_nl_policy,
2238 .flags = GENL_ADMIN_PERM,
2239 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
2242 .cmd = DEVLINK_CMD_PORT_SPLIT,
2243 .doit = devlink_nl_cmd_port_split_doit,
2244 .policy = devlink_nl_policy,
2245 .flags = GENL_ADMIN_PERM,
2246 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2249 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
2250 .doit = devlink_nl_cmd_port_unsplit_doit,
2251 .policy = devlink_nl_policy,
2252 .flags = GENL_ADMIN_PERM,
2253 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2256 .cmd = DEVLINK_CMD_SB_GET,
2257 .doit = devlink_nl_cmd_sb_get_doit,
2258 .dumpit = devlink_nl_cmd_sb_get_dumpit,
2259 .policy = devlink_nl_policy,
2260 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2261 DEVLINK_NL_FLAG_NEED_SB,
2262 /* can be retrieved by unprivileged users */
2265 .cmd = DEVLINK_CMD_SB_POOL_GET,
2266 .doit = devlink_nl_cmd_sb_pool_get_doit,
2267 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
2268 .policy = devlink_nl_policy,
2269 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2270 DEVLINK_NL_FLAG_NEED_SB,
2271 /* can be retrieved by unprivileged users */
2274 .cmd = DEVLINK_CMD_SB_POOL_SET,
2275 .doit = devlink_nl_cmd_sb_pool_set_doit,
2276 .policy = devlink_nl_policy,
2277 .flags = GENL_ADMIN_PERM,
2278 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2279 DEVLINK_NL_FLAG_NEED_SB,
2282 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
2283 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
2284 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
2285 .policy = devlink_nl_policy,
2286 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2287 DEVLINK_NL_FLAG_NEED_SB,
2288 /* can be retrieved by unprivileged users */
2291 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
2292 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
2293 .policy = devlink_nl_policy,
2294 .flags = GENL_ADMIN_PERM,
2295 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2296 DEVLINK_NL_FLAG_NEED_SB,
2299 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
2300 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
2301 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
2302 .policy = devlink_nl_policy,
2303 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2304 DEVLINK_NL_FLAG_NEED_SB,
2305 /* can be retrieved by unprivileged users */
2308 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
2309 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
2310 .policy = devlink_nl_policy,
2311 .flags = GENL_ADMIN_PERM,
2312 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
2313 DEVLINK_NL_FLAG_NEED_SB,
2316 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
2317 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
2318 .policy = devlink_nl_policy,
2319 .flags = GENL_ADMIN_PERM,
2320 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2321 DEVLINK_NL_FLAG_NEED_SB |
2322 DEVLINK_NL_FLAG_LOCK_PORTS,
2325 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
2326 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
2327 .policy = devlink_nl_policy,
2328 .flags = GENL_ADMIN_PERM,
2329 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
2330 DEVLINK_NL_FLAG_NEED_SB |
2331 DEVLINK_NL_FLAG_LOCK_PORTS,
2334 .cmd = DEVLINK_CMD_ESWITCH_GET,
2335 .doit = devlink_nl_cmd_eswitch_get_doit,
2336 .policy = devlink_nl_policy,
2337 .flags = GENL_ADMIN_PERM,
2338 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2341 .cmd = DEVLINK_CMD_ESWITCH_SET,
2342 .doit = devlink_nl_cmd_eswitch_set_doit,
2343 .policy = devlink_nl_policy,
2344 .flags = GENL_ADMIN_PERM,
2345 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2348 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
2349 .doit = devlink_nl_cmd_dpipe_table_get,
2350 .policy = devlink_nl_policy,
2351 .flags = GENL_ADMIN_PERM,
2352 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2355 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
2356 .doit = devlink_nl_cmd_dpipe_entries_get,
2357 .policy = devlink_nl_policy,
2358 .flags = GENL_ADMIN_PERM,
2359 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2362 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
2363 .doit = devlink_nl_cmd_dpipe_headers_get,
2364 .policy = devlink_nl_policy,
2365 .flags = GENL_ADMIN_PERM,
2366 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2369 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
2370 .doit = devlink_nl_cmd_dpipe_table_counters_set,
2371 .policy = devlink_nl_policy,
2372 .flags = GENL_ADMIN_PERM,
2373 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
2377 static struct genl_family devlink_nl_family __ro_after_init = {
2378 .name = DEVLINK_GENL_NAME,
2379 .version = DEVLINK_GENL_VERSION,
2380 .maxattr = DEVLINK_ATTR_MAX,
2382 .pre_doit = devlink_nl_pre_doit,
2383 .post_doit = devlink_nl_post_doit,
2384 .module = THIS_MODULE,
2385 .ops = devlink_nl_ops,
2386 .n_ops = ARRAY_SIZE(devlink_nl_ops),
2387 .mcgrps = devlink_nl_mcgrps,
2388 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
2392 * devlink_alloc - Allocate new devlink instance resources
2395 * @priv_size: size of user private data
2397 * Allocate new devlink instance resources, including devlink index
2400 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
2402 struct devlink *devlink;
2404 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
2408 devlink_net_set(devlink, &init_net);
2409 INIT_LIST_HEAD(&devlink->port_list);
2410 INIT_LIST_HEAD(&devlink->sb_list);
2411 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
2414 EXPORT_SYMBOL_GPL(devlink_alloc);
2417 * devlink_register - Register devlink instance
2421 int devlink_register(struct devlink *devlink, struct device *dev)
2423 mutex_lock(&devlink_mutex);
2425 list_add_tail(&devlink->list, &devlink_list);
2426 devlink_notify(devlink, DEVLINK_CMD_NEW);
2427 mutex_unlock(&devlink_mutex);
2430 EXPORT_SYMBOL_GPL(devlink_register);
2433 * devlink_unregister - Unregister devlink instance
2437 void devlink_unregister(struct devlink *devlink)
2439 mutex_lock(&devlink_mutex);
2440 devlink_notify(devlink, DEVLINK_CMD_DEL);
2441 list_del(&devlink->list);
2442 mutex_unlock(&devlink_mutex);
2444 EXPORT_SYMBOL_GPL(devlink_unregister);
2447 * devlink_free - Free devlink instance resources
2451 void devlink_free(struct devlink *devlink)
2455 EXPORT_SYMBOL_GPL(devlink_free);
2458 * devlink_port_register - Register devlink port
2461 * @devlink_port: devlink port
2464 * Register devlink port with provided port index. User can use
2465 * any indexing, even hw-related one. devlink_port structure
2466 * is convenient to be embedded inside user driver private structure.
2467 * Note that the caller should take care of zeroing the devlink_port
2470 int devlink_port_register(struct devlink *devlink,
2471 struct devlink_port *devlink_port,
2472 unsigned int port_index)
2474 mutex_lock(&devlink_port_mutex);
2475 if (devlink_port_index_exists(devlink, port_index)) {
2476 mutex_unlock(&devlink_port_mutex);
2479 devlink_port->devlink = devlink;
2480 devlink_port->index = port_index;
2481 devlink_port->registered = true;
2482 list_add_tail(&devlink_port->list, &devlink->port_list);
2483 mutex_unlock(&devlink_port_mutex);
2484 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2487 EXPORT_SYMBOL_GPL(devlink_port_register);
2490 * devlink_port_unregister - Unregister devlink port
2492 * @devlink_port: devlink port
2494 void devlink_port_unregister(struct devlink_port *devlink_port)
2496 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
2497 mutex_lock(&devlink_port_mutex);
2498 list_del(&devlink_port->list);
2499 mutex_unlock(&devlink_port_mutex);
2501 EXPORT_SYMBOL_GPL(devlink_port_unregister);
2503 static void __devlink_port_type_set(struct devlink_port *devlink_port,
2504 enum devlink_port_type type,
2507 devlink_port->type = type;
2508 devlink_port->type_dev = type_dev;
2509 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2513 * devlink_port_type_eth_set - Set port type to Ethernet
2515 * @devlink_port: devlink port
2516 * @netdev: related netdevice
2518 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
2519 struct net_device *netdev)
2521 return __devlink_port_type_set(devlink_port,
2522 DEVLINK_PORT_TYPE_ETH, netdev);
2524 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
2527 * devlink_port_type_ib_set - Set port type to InfiniBand
2529 * @devlink_port: devlink port
2530 * @ibdev: related IB device
2532 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
2533 struct ib_device *ibdev)
2535 return __devlink_port_type_set(devlink_port,
2536 DEVLINK_PORT_TYPE_IB, ibdev);
2538 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
2541 * devlink_port_type_clear - Clear port type
2543 * @devlink_port: devlink port
2545 void devlink_port_type_clear(struct devlink_port *devlink_port)
2547 return __devlink_port_type_set(devlink_port,
2548 DEVLINK_PORT_TYPE_NOTSET, NULL);
2550 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
2553 * devlink_port_split_set - Set port is split
2555 * @devlink_port: devlink port
2556 * @split_group: split group - identifies group split port is part of
2558 void devlink_port_split_set(struct devlink_port *devlink_port,
2561 devlink_port->split = true;
2562 devlink_port->split_group = split_group;
2563 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
2565 EXPORT_SYMBOL_GPL(devlink_port_split_set);
2567 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
2568 u32 size, u16 ingress_pools_count,
2569 u16 egress_pools_count, u16 ingress_tc_count,
2570 u16 egress_tc_count)
2572 struct devlink_sb *devlink_sb;
2575 mutex_lock(&devlink_mutex);
2576 if (devlink_sb_index_exists(devlink, sb_index)) {
2581 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
2586 devlink_sb->index = sb_index;
2587 devlink_sb->size = size;
2588 devlink_sb->ingress_pools_count = ingress_pools_count;
2589 devlink_sb->egress_pools_count = egress_pools_count;
2590 devlink_sb->ingress_tc_count = ingress_tc_count;
2591 devlink_sb->egress_tc_count = egress_tc_count;
2592 list_add_tail(&devlink_sb->list, &devlink->sb_list);
2594 mutex_unlock(&devlink_mutex);
2597 EXPORT_SYMBOL_GPL(devlink_sb_register);
2599 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
2601 struct devlink_sb *devlink_sb;
2603 mutex_lock(&devlink_mutex);
2604 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
2605 WARN_ON(!devlink_sb);
2606 list_del(&devlink_sb->list);
2607 mutex_unlock(&devlink_mutex);
2610 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
2613 * devlink_dpipe_headers_register - register dpipe headers
2616 * @dpipe_headers: dpipe header array
2618 * Register the headers supported by hardware.
2620 int devlink_dpipe_headers_register(struct devlink *devlink,
2621 struct devlink_dpipe_headers *dpipe_headers)
2623 mutex_lock(&devlink_mutex);
2624 devlink->dpipe_headers = dpipe_headers;
2625 mutex_unlock(&devlink_mutex);
2628 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
2631 * devlink_dpipe_headers_unregister - unregister dpipe headers
2635 * Unregister the headers supported by hardware.
2637 void devlink_dpipe_headers_unregister(struct devlink *devlink)
2639 mutex_lock(&devlink_mutex);
2640 devlink->dpipe_headers = NULL;
2641 mutex_unlock(&devlink_mutex);
2643 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
2646 * devlink_dpipe_table_counter_enabled - check if counter allocation
2649 * @table_name: tables name
2651 * Used by driver to check if counter allocation is required.
2652 * After counter allocation is turned on the table entries
2653 * are updated to include counter statistics.
2655 * After that point on the driver must respect the counter
2656 * state so that each entry added to the table is added
2659 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
2660 const char *table_name)
2662 struct devlink_dpipe_table *table;
2666 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2670 enabled = table->counters_enabled;
2674 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
2677 * devlink_dpipe_table_register - register dpipe table
2680 * @table_name: table name
2681 * @table_ops: table ops
2684 * @counter_control_extern: external control for counters
2686 int devlink_dpipe_table_register(struct devlink *devlink,
2687 const char *table_name,
2688 struct devlink_dpipe_table_ops *table_ops,
2689 void *priv, u64 size,
2690 bool counter_control_extern)
2692 struct devlink_dpipe_table *table;
2694 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
2697 table = kzalloc(sizeof(*table), GFP_KERNEL);
2701 table->name = table_name;
2702 table->table_ops = table_ops;
2705 table->counter_control_extern = counter_control_extern;
2707 mutex_lock(&devlink_mutex);
2708 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
2709 mutex_unlock(&devlink_mutex);
2712 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
2715 * devlink_dpipe_table_unregister - unregister dpipe table
2718 * @table_name: table name
2720 void devlink_dpipe_table_unregister(struct devlink *devlink,
2721 const char *table_name)
2723 struct devlink_dpipe_table *table;
2725 mutex_lock(&devlink_mutex);
2726 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2730 list_del_rcu(&table->list);
2731 mutex_unlock(&devlink_mutex);
2732 kfree_rcu(table, rcu);
2735 mutex_unlock(&devlink_mutex);
2737 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
2739 static int __init devlink_module_init(void)
2741 return genl_register_family(&devlink_nl_family);
2744 static void __exit devlink_module_exit(void)
2746 genl_unregister_family(&devlink_nl_family);
2749 module_init(devlink_module_init);
2750 module_exit(devlink_module_exit);
2752 MODULE_LICENSE("GPL v2");
2753 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2754 MODULE_DESCRIPTION("Network physical device Netlink interface");
2755 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);