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>
30 static LIST_HEAD(devlink_list
);
34 * An overall lock guarding every operation coming from userspace.
35 * It also guards devlink devices list and it is taken when
36 * driver registers/unregisters it.
38 static DEFINE_MUTEX(devlink_mutex
);
42 * Shared lock to guard lists of ports in all devlink devices.
44 static DEFINE_MUTEX(devlink_port_mutex
);
46 static struct net
*devlink_net(const struct devlink
*devlink
)
48 return read_pnet(&devlink
->_net
);
51 static void devlink_net_set(struct devlink
*devlink
, struct net
*net
)
53 write_pnet(&devlink
->_net
, net
);
56 static struct devlink
*devlink_get_from_attrs(struct net
*net
,
57 struct nlattr
**attrs
)
59 struct devlink
*devlink
;
63 if (!attrs
[DEVLINK_ATTR_BUS_NAME
] || !attrs
[DEVLINK_ATTR_DEV_NAME
])
64 return ERR_PTR(-EINVAL
);
66 busname
= nla_data(attrs
[DEVLINK_ATTR_BUS_NAME
]);
67 devname
= nla_data(attrs
[DEVLINK_ATTR_DEV_NAME
]);
69 list_for_each_entry(devlink
, &devlink_list
, list
) {
70 if (strcmp(devlink
->dev
->bus
->name
, busname
) == 0 &&
71 strcmp(dev_name(devlink
->dev
), devname
) == 0 &&
72 net_eq(devlink_net(devlink
), net
))
76 return ERR_PTR(-ENODEV
);
79 static struct devlink
*devlink_get_from_info(struct genl_info
*info
)
81 return devlink_get_from_attrs(genl_info_net(info
), info
->attrs
);
84 static struct devlink_port
*devlink_port_get_by_index(struct devlink
*devlink
,
87 struct devlink_port
*devlink_port
;
89 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
90 if (devlink_port
->index
== port_index
)
96 static bool devlink_port_index_exists(struct devlink
*devlink
, int port_index
)
98 return devlink_port_get_by_index(devlink
, port_index
);
101 static struct devlink_port
*devlink_port_get_from_attrs(struct devlink
*devlink
,
102 struct nlattr
**attrs
)
104 if (attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
105 u32 port_index
= nla_get_u32(attrs
[DEVLINK_ATTR_PORT_INDEX
]);
106 struct devlink_port
*devlink_port
;
108 devlink_port
= devlink_port_get_by_index(devlink
, port_index
);
110 return ERR_PTR(-ENODEV
);
113 return ERR_PTR(-EINVAL
);
116 static struct devlink_port
*devlink_port_get_from_info(struct devlink
*devlink
,
117 struct genl_info
*info
)
119 return devlink_port_get_from_attrs(devlink
, info
->attrs
);
122 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
124 static int devlink_nl_pre_doit(const struct genl_ops
*ops
,
125 struct sk_buff
*skb
, struct genl_info
*info
)
127 struct devlink
*devlink
;
129 mutex_lock(&devlink_mutex
);
130 devlink
= devlink_get_from_info(info
);
131 if (IS_ERR(devlink
)) {
132 mutex_unlock(&devlink_mutex
);
133 return PTR_ERR(devlink
);
135 info
->user_ptr
[0] = devlink
;
136 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
) {
137 struct devlink_port
*devlink_port
;
139 mutex_lock(&devlink_port_mutex
);
140 devlink_port
= devlink_port_get_from_info(devlink
, info
);
141 if (IS_ERR(devlink_port
)) {
142 mutex_unlock(&devlink_port_mutex
);
143 mutex_unlock(&devlink_mutex
);
144 return PTR_ERR(devlink_port
);
146 info
->user_ptr
[1] = devlink_port
;
151 static void devlink_nl_post_doit(const struct genl_ops
*ops
,
152 struct sk_buff
*skb
, struct genl_info
*info
)
154 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
)
155 mutex_unlock(&devlink_port_mutex
);
156 mutex_unlock(&devlink_mutex
);
159 static struct genl_family devlink_nl_family
= {
160 .id
= GENL_ID_GENERATE
,
161 .name
= DEVLINK_GENL_NAME
,
162 .version
= DEVLINK_GENL_VERSION
,
163 .maxattr
= DEVLINK_ATTR_MAX
,
165 .pre_doit
= devlink_nl_pre_doit
,
166 .post_doit
= devlink_nl_post_doit
,
169 enum devlink_multicast_groups
{
170 DEVLINK_MCGRP_CONFIG
,
173 static const struct genl_multicast_group devlink_nl_mcgrps
[] = {
174 [DEVLINK_MCGRP_CONFIG
] = { .name
= DEVLINK_GENL_MCGRP_CONFIG_NAME
},
177 static int devlink_nl_put_handle(struct sk_buff
*msg
, struct devlink
*devlink
)
179 if (nla_put_string(msg
, DEVLINK_ATTR_BUS_NAME
, devlink
->dev
->bus
->name
))
181 if (nla_put_string(msg
, DEVLINK_ATTR_DEV_NAME
, dev_name(devlink
->dev
)))
186 static int devlink_nl_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
187 enum devlink_command cmd
, u32 portid
,
192 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
196 if (devlink_nl_put_handle(msg
, devlink
))
197 goto nla_put_failure
;
199 genlmsg_end(msg
, hdr
);
203 genlmsg_cancel(msg
, hdr
);
207 static void devlink_notify(struct devlink
*devlink
, enum devlink_command cmd
)
212 WARN_ON(cmd
!= DEVLINK_CMD_NEW
&& cmd
!= DEVLINK_CMD_DEL
);
214 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
218 err
= devlink_nl_fill(msg
, devlink
, cmd
, 0, 0, 0);
224 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
225 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
228 static int devlink_nl_port_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
229 struct devlink_port
*devlink_port
,
230 enum devlink_command cmd
, u32 portid
,
235 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
239 if (devlink_nl_put_handle(msg
, devlink
))
240 goto nla_put_failure
;
241 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
242 goto nla_put_failure
;
243 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_TYPE
, devlink_port
->type
))
244 goto nla_put_failure
;
245 if (devlink_port
->desired_type
!= DEVLINK_PORT_TYPE_NOTSET
&&
246 nla_put_u16(msg
, DEVLINK_ATTR_PORT_DESIRED_TYPE
,
247 devlink_port
->desired_type
))
248 goto nla_put_failure
;
249 if (devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
) {
250 struct net_device
*netdev
= devlink_port
->type_dev
;
253 (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NETDEV_IFINDEX
,
255 nla_put_string(msg
, DEVLINK_ATTR_PORT_NETDEV_NAME
,
257 goto nla_put_failure
;
259 if (devlink_port
->type
== DEVLINK_PORT_TYPE_IB
) {
260 struct ib_device
*ibdev
= devlink_port
->type_dev
;
263 nla_put_string(msg
, DEVLINK_ATTR_PORT_IBDEV_NAME
,
265 goto nla_put_failure
;
267 if (devlink_port
->split
&&
268 nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_GROUP
,
269 devlink_port
->split_group
))
270 goto nla_put_failure
;
272 genlmsg_end(msg
, hdr
);
276 genlmsg_cancel(msg
, hdr
);
280 static void devlink_port_notify(struct devlink_port
*devlink_port
,
281 enum devlink_command cmd
)
283 struct devlink
*devlink
= devlink_port
->devlink
;
287 if (!devlink_port
->registered
)
290 WARN_ON(cmd
!= DEVLINK_CMD_PORT_NEW
&& cmd
!= DEVLINK_CMD_PORT_DEL
);
292 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
296 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
, cmd
, 0, 0, 0);
302 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
303 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
306 static int devlink_nl_cmd_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
308 struct devlink
*devlink
= info
->user_ptr
[0];
312 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
316 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
317 info
->snd_portid
, info
->snd_seq
, 0);
323 return genlmsg_reply(msg
, info
);
326 static int devlink_nl_cmd_get_dumpit(struct sk_buff
*msg
,
327 struct netlink_callback
*cb
)
329 struct devlink
*devlink
;
330 int start
= cb
->args
[0];
334 mutex_lock(&devlink_mutex
);
335 list_for_each_entry(devlink
, &devlink_list
, list
) {
336 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
342 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
343 NETLINK_CB(cb
->skb
).portid
,
344 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
350 mutex_unlock(&devlink_mutex
);
356 static int devlink_nl_cmd_port_get_doit(struct sk_buff
*skb
,
357 struct genl_info
*info
)
359 struct devlink
*devlink
= info
->user_ptr
[0];
360 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
364 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
368 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
369 DEVLINK_CMD_PORT_NEW
,
370 info
->snd_portid
, info
->snd_seq
, 0);
376 return genlmsg_reply(msg
, info
);
379 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff
*msg
,
380 struct netlink_callback
*cb
)
382 struct devlink
*devlink
;
383 struct devlink_port
*devlink_port
;
384 int start
= cb
->args
[0];
388 mutex_lock(&devlink_mutex
);
389 mutex_lock(&devlink_port_mutex
);
390 list_for_each_entry(devlink
, &devlink_list
, list
) {
391 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
393 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
398 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
400 NETLINK_CB(cb
->skb
).portid
,
409 mutex_unlock(&devlink_port_mutex
);
410 mutex_unlock(&devlink_mutex
);
416 static int devlink_port_type_set(struct devlink
*devlink
,
417 struct devlink_port
*devlink_port
,
418 enum devlink_port_type port_type
)
423 if (devlink
->ops
&& devlink
->ops
->port_type_set
) {
424 if (port_type
== DEVLINK_PORT_TYPE_NOTSET
)
426 err
= devlink
->ops
->port_type_set(devlink_port
, port_type
);
429 devlink_port
->desired_type
= port_type
;
430 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
436 static int devlink_nl_cmd_port_set_doit(struct sk_buff
*skb
,
437 struct genl_info
*info
)
439 struct devlink
*devlink
= info
->user_ptr
[0];
440 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
443 if (info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]) {
444 enum devlink_port_type port_type
;
446 port_type
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]);
447 err
= devlink_port_type_set(devlink
, devlink_port
, port_type
);
454 static int devlink_port_split(struct devlink
*devlink
,
455 u32 port_index
, u32 count
)
458 if (devlink
->ops
&& devlink
->ops
->port_split
)
459 return devlink
->ops
->port_split(devlink
, port_index
, count
);
463 static int devlink_nl_cmd_port_split_doit(struct sk_buff
*skb
,
464 struct genl_info
*info
)
466 struct devlink
*devlink
= info
->user_ptr
[0];
470 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
] ||
471 !info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
])
474 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
475 count
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
]);
476 return devlink_port_split(devlink
, port_index
, count
);
479 static int devlink_port_unsplit(struct devlink
*devlink
, u32 port_index
)
482 if (devlink
->ops
&& devlink
->ops
->port_unsplit
)
483 return devlink
->ops
->port_unsplit(devlink
, port_index
);
487 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff
*skb
,
488 struct genl_info
*info
)
490 struct devlink
*devlink
= info
->user_ptr
[0];
493 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
])
496 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
497 return devlink_port_unsplit(devlink
, port_index
);
500 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
501 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
502 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
503 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
504 [DEVLINK_ATTR_PORT_TYPE
] = { .type
= NLA_U16
},
505 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
508 static const struct genl_ops devlink_nl_ops
[] = {
510 .cmd
= DEVLINK_CMD_GET
,
511 .doit
= devlink_nl_cmd_get_doit
,
512 .dumpit
= devlink_nl_cmd_get_dumpit
,
513 .policy
= devlink_nl_policy
,
514 /* can be retrieved by unprivileged users */
517 .cmd
= DEVLINK_CMD_PORT_GET
,
518 .doit
= devlink_nl_cmd_port_get_doit
,
519 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
520 .policy
= devlink_nl_policy
,
521 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
522 /* can be retrieved by unprivileged users */
525 .cmd
= DEVLINK_CMD_PORT_SET
,
526 .doit
= devlink_nl_cmd_port_set_doit
,
527 .policy
= devlink_nl_policy
,
528 .flags
= GENL_ADMIN_PERM
,
529 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
532 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
533 .doit
= devlink_nl_cmd_port_split_doit
,
534 .policy
= devlink_nl_policy
,
535 .flags
= GENL_ADMIN_PERM
,
538 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
539 .doit
= devlink_nl_cmd_port_unsplit_doit
,
540 .policy
= devlink_nl_policy
,
541 .flags
= GENL_ADMIN_PERM
,
546 * devlink_alloc - Allocate new devlink instance resources
549 * @priv_size: size of user private data
551 * Allocate new devlink instance resources, including devlink index
554 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
556 struct devlink
*devlink
;
558 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
562 devlink_net_set(devlink
, &init_net
);
563 INIT_LIST_HEAD(&devlink
->port_list
);
566 EXPORT_SYMBOL_GPL(devlink_alloc
);
569 * devlink_register - Register devlink instance
573 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
575 mutex_lock(&devlink_mutex
);
577 list_add_tail(&devlink
->list
, &devlink_list
);
578 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
579 mutex_unlock(&devlink_mutex
);
582 EXPORT_SYMBOL_GPL(devlink_register
);
585 * devlink_unregister - Unregister devlink instance
589 void devlink_unregister(struct devlink
*devlink
)
591 mutex_lock(&devlink_mutex
);
592 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
593 list_del(&devlink
->list
);
594 mutex_unlock(&devlink_mutex
);
596 EXPORT_SYMBOL_GPL(devlink_unregister
);
599 * devlink_free - Free devlink instance resources
603 void devlink_free(struct devlink
*devlink
)
607 EXPORT_SYMBOL_GPL(devlink_free
);
610 * devlink_port_register - Register devlink port
613 * @devlink_port: devlink port
616 * Register devlink port with provided port index. User can use
617 * any indexing, even hw-related one. devlink_port structure
618 * is convenient to be embedded inside user driver private structure.
619 * Note that the caller should take care of zeroing the devlink_port
622 int devlink_port_register(struct devlink
*devlink
,
623 struct devlink_port
*devlink_port
,
624 unsigned int port_index
)
626 mutex_lock(&devlink_port_mutex
);
627 if (devlink_port_index_exists(devlink
, port_index
)) {
628 mutex_unlock(&devlink_port_mutex
);
631 devlink_port
->devlink
= devlink
;
632 devlink_port
->index
= port_index
;
633 devlink_port
->type
= DEVLINK_PORT_TYPE_NOTSET
;
634 devlink_port
->registered
= true;
635 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
636 mutex_unlock(&devlink_port_mutex
);
637 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
640 EXPORT_SYMBOL_GPL(devlink_port_register
);
643 * devlink_port_unregister - Unregister devlink port
645 * @devlink_port: devlink port
647 void devlink_port_unregister(struct devlink_port
*devlink_port
)
649 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
650 mutex_lock(&devlink_port_mutex
);
651 list_del(&devlink_port
->list
);
652 mutex_unlock(&devlink_port_mutex
);
654 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
656 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
657 enum devlink_port_type type
,
660 devlink_port
->type
= type
;
661 devlink_port
->type_dev
= type_dev
;
662 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
666 * devlink_port_type_eth_set - Set port type to Ethernet
668 * @devlink_port: devlink port
669 * @netdev: related netdevice
671 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
672 struct net_device
*netdev
)
674 return __devlink_port_type_set(devlink_port
,
675 DEVLINK_PORT_TYPE_ETH
, netdev
);
677 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
680 * devlink_port_type_ib_set - Set port type to InfiniBand
682 * @devlink_port: devlink port
683 * @ibdev: related IB device
685 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
686 struct ib_device
*ibdev
)
688 return __devlink_port_type_set(devlink_port
,
689 DEVLINK_PORT_TYPE_IB
, ibdev
);
691 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
694 * devlink_port_type_clear - Clear port type
696 * @devlink_port: devlink port
698 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
700 return __devlink_port_type_set(devlink_port
,
701 DEVLINK_PORT_TYPE_NOTSET
, NULL
);
703 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
706 * devlink_port_split_set - Set port is split
708 * @devlink_port: devlink port
709 * @split_group: split group - identifies group split port is part of
711 void devlink_port_split_set(struct devlink_port
*devlink_port
,
714 devlink_port
->split
= true;
715 devlink_port
->split_group
= split_group
;
716 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
718 EXPORT_SYMBOL_GPL(devlink_port_split_set
);
720 static int __init
devlink_module_init(void)
722 return genl_register_family_with_ops_groups(&devlink_nl_family
,
727 static void __exit
devlink_module_exit(void)
729 genl_unregister_family(&devlink_nl_family
);
732 module_init(devlink_module_init
);
733 module_exit(devlink_module_exit
);
735 MODULE_LICENSE("GPL v2");
736 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
737 MODULE_DESCRIPTION("Network physical device Netlink interface");
738 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME
);