xref: /linux/net/devlink/netlink.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1623cd13bSJakub Kicinski // SPDX-License-Identifier: GPL-2.0-or-later
2623cd13bSJakub Kicinski /*
3623cd13bSJakub Kicinski  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4623cd13bSJakub Kicinski  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5623cd13bSJakub Kicinski  */
6623cd13bSJakub Kicinski 
7623cd13bSJakub Kicinski #include <net/genetlink.h>
807f3af66SJakub Kicinski #include <net/sock.h>
9623cd13bSJakub Kicinski 
10623cd13bSJakub Kicinski #include "devl_internal.h"
11623cd13bSJakub Kicinski 
12526dd6d7SIdo Schimmel #define DEVLINK_NL_FLAG_NEED_PORT		BIT(0)
13526dd6d7SIdo Schimmel #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT	BIT(1)
14d32c3825SIdo Schimmel #define DEVLINK_NL_FLAG_NEED_DEV_LOCK		BIT(2)
15526dd6d7SIdo Schimmel 
16623cd13bSJakub Kicinski static const struct genl_multicast_group devlink_nl_mcgrps[] = {
17623cd13bSJakub Kicinski 	[DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
18623cd13bSJakub Kicinski };
19623cd13bSJakub Kicinski 
2013b127d2SJiri Pirko struct devlink_nl_sock_priv {
2113b127d2SJiri Pirko 	struct devlink_obj_desc __rcu *flt;
2213b127d2SJiri Pirko 	spinlock_t flt_lock; /* Protects flt. */
2313b127d2SJiri Pirko };
2413b127d2SJiri Pirko 
devlink_nl_sock_priv_init(void * priv)2513b127d2SJiri Pirko static void devlink_nl_sock_priv_init(void *priv)
2613b127d2SJiri Pirko {
2713b127d2SJiri Pirko 	struct devlink_nl_sock_priv *sk_priv = priv;
2813b127d2SJiri Pirko 
2913b127d2SJiri Pirko 	spin_lock_init(&sk_priv->flt_lock);
3013b127d2SJiri Pirko }
3113b127d2SJiri Pirko 
devlink_nl_sock_priv_destroy(void * priv)3213b127d2SJiri Pirko static void devlink_nl_sock_priv_destroy(void *priv)
3313b127d2SJiri Pirko {
3413b127d2SJiri Pirko 	struct devlink_nl_sock_priv *sk_priv = priv;
3513b127d2SJiri Pirko 	struct devlink_obj_desc *flt;
3613b127d2SJiri Pirko 
3713b127d2SJiri Pirko 	flt = rcu_dereference_protected(sk_priv->flt, true);
3813b127d2SJiri Pirko 	kfree_rcu(flt, rcu);
3913b127d2SJiri Pirko }
4013b127d2SJiri Pirko 
devlink_nl_notify_filter_set_doit(struct sk_buff * skb,struct genl_info * info)4113b127d2SJiri Pirko int devlink_nl_notify_filter_set_doit(struct sk_buff *skb,
4213b127d2SJiri Pirko 				      struct genl_info *info)
4313b127d2SJiri Pirko {
4413b127d2SJiri Pirko 	struct devlink_nl_sock_priv *sk_priv;
4513b127d2SJiri Pirko 	struct nlattr **attrs = info->attrs;
4613b127d2SJiri Pirko 	struct devlink_obj_desc *flt;
4713b127d2SJiri Pirko 	size_t data_offset = 0;
4813b127d2SJiri Pirko 	size_t data_size = 0;
4913b127d2SJiri Pirko 	char *pos;
5013b127d2SJiri Pirko 
5113b127d2SJiri Pirko 	if (attrs[DEVLINK_ATTR_BUS_NAME])
5213b127d2SJiri Pirko 		data_size = size_add(data_size,
5313b127d2SJiri Pirko 				     nla_len(attrs[DEVLINK_ATTR_BUS_NAME]) + 1);
5413b127d2SJiri Pirko 	if (attrs[DEVLINK_ATTR_DEV_NAME])
5513b127d2SJiri Pirko 		data_size = size_add(data_size,
5613b127d2SJiri Pirko 				     nla_len(attrs[DEVLINK_ATTR_DEV_NAME]) + 1);
5713b127d2SJiri Pirko 
5813b127d2SJiri Pirko 	flt = kzalloc(size_add(sizeof(*flt), data_size), GFP_KERNEL);
5913b127d2SJiri Pirko 	if (!flt)
6013b127d2SJiri Pirko 		return -ENOMEM;
6113b127d2SJiri Pirko 
6213b127d2SJiri Pirko 	pos = (char *) flt->data;
6313b127d2SJiri Pirko 	if (attrs[DEVLINK_ATTR_BUS_NAME]) {
6413b127d2SJiri Pirko 		data_offset += nla_strscpy(pos,
6513b127d2SJiri Pirko 					   attrs[DEVLINK_ATTR_BUS_NAME],
6613b127d2SJiri Pirko 					   data_size) + 1;
6713b127d2SJiri Pirko 		flt->bus_name = pos;
6813b127d2SJiri Pirko 		pos += data_offset;
6913b127d2SJiri Pirko 	}
7013b127d2SJiri Pirko 	if (attrs[DEVLINK_ATTR_DEV_NAME]) {
7113b127d2SJiri Pirko 		nla_strscpy(pos, attrs[DEVLINK_ATTR_DEV_NAME],
7213b127d2SJiri Pirko 			    data_size - data_offset);
7313b127d2SJiri Pirko 		flt->dev_name = pos;
7413b127d2SJiri Pirko 	}
7513b127d2SJiri Pirko 
76ded6f77cSJiri Pirko 	if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
77ded6f77cSJiri Pirko 		flt->port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
78ded6f77cSJiri Pirko 		flt->port_index_valid = true;
79ded6f77cSJiri Pirko 	}
80ded6f77cSJiri Pirko 
8113b127d2SJiri Pirko 	/* Don't attach empty filter. */
82ded6f77cSJiri Pirko 	if (!flt->bus_name && !flt->dev_name && !flt->port_index_valid) {
8313b127d2SJiri Pirko 		kfree(flt);
8413b127d2SJiri Pirko 		flt = NULL;
8513b127d2SJiri Pirko 	}
8613b127d2SJiri Pirko 
8713b127d2SJiri Pirko 	sk_priv = genl_sk_priv_get(&devlink_nl_family, NETLINK_CB(skb).sk);
8813b127d2SJiri Pirko 	if (IS_ERR(sk_priv)) {
8913b127d2SJiri Pirko 		kfree(flt);
9013b127d2SJiri Pirko 		return PTR_ERR(sk_priv);
9113b127d2SJiri Pirko 	}
9213b127d2SJiri Pirko 	spin_lock(&sk_priv->flt_lock);
9313b127d2SJiri Pirko 	flt = rcu_replace_pointer(sk_priv->flt, flt,
9413b127d2SJiri Pirko 				  lockdep_is_held(&sk_priv->flt_lock));
9513b127d2SJiri Pirko 	spin_unlock(&sk_priv->flt_lock);
9613b127d2SJiri Pirko 	kfree_rcu(flt, rcu);
9713b127d2SJiri Pirko 	return 0;
9813b127d2SJiri Pirko }
9913b127d2SJiri Pirko 
devlink_obj_desc_match(const struct devlink_obj_desc * desc,const struct devlink_obj_desc * flt)10013b127d2SJiri Pirko static bool devlink_obj_desc_match(const struct devlink_obj_desc *desc,
10113b127d2SJiri Pirko 				   const struct devlink_obj_desc *flt)
10213b127d2SJiri Pirko {
10313b127d2SJiri Pirko 	if (desc->bus_name && flt->bus_name &&
10413b127d2SJiri Pirko 	    strcmp(desc->bus_name, flt->bus_name))
10513b127d2SJiri Pirko 		return false;
10613b127d2SJiri Pirko 	if (desc->dev_name && flt->dev_name &&
10713b127d2SJiri Pirko 	    strcmp(desc->dev_name, flt->dev_name))
10813b127d2SJiri Pirko 		return false;
109ded6f77cSJiri Pirko 	if (desc->port_index_valid && flt->port_index_valid &&
110ded6f77cSJiri Pirko 	    desc->port_index != flt->port_index)
111ded6f77cSJiri Pirko 		return false;
11213b127d2SJiri Pirko 	return true;
11313b127d2SJiri Pirko }
11413b127d2SJiri Pirko 
devlink_nl_notify_filter(struct sock * dsk,struct sk_buff * skb,void * data)11513b127d2SJiri Pirko int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data)
11613b127d2SJiri Pirko {
11713b127d2SJiri Pirko 	struct devlink_obj_desc *desc = data;
11813b127d2SJiri Pirko 	struct devlink_nl_sock_priv *sk_priv;
11913b127d2SJiri Pirko 	struct devlink_obj_desc *flt;
12013b127d2SJiri Pirko 	int ret = 0;
12113b127d2SJiri Pirko 
12213b127d2SJiri Pirko 	rcu_read_lock();
12313b127d2SJiri Pirko 	sk_priv = __genl_sk_priv_get(&devlink_nl_family, dsk);
12413b127d2SJiri Pirko 	if (!IS_ERR_OR_NULL(sk_priv)) {
12513b127d2SJiri Pirko 		flt = rcu_dereference(sk_priv->flt);
12613b127d2SJiri Pirko 		if (flt)
12713b127d2SJiri Pirko 			ret = !devlink_obj_desc_match(desc, flt);
12813b127d2SJiri Pirko 	}
12913b127d2SJiri Pirko 	rcu_read_unlock();
13013b127d2SJiri Pirko 	return ret;
13113b127d2SJiri Pirko }
13213b127d2SJiri Pirko 
devlink_nl_put_nested_handle(struct sk_buff * msg,struct net * net,struct devlink * devlink,int attrtype)133af1f1400SJiri Pirko int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net,
1341c2197c4SJiri Pirko 				 struct devlink *devlink, int attrtype)
135af1f1400SJiri Pirko {
136af1f1400SJiri Pirko 	struct nlattr *nested_attr;
137c503bc7dSJiri Pirko 	struct net *devl_net;
138af1f1400SJiri Pirko 
1391c2197c4SJiri Pirko 	nested_attr = nla_nest_start(msg, attrtype);
140af1f1400SJiri Pirko 	if (!nested_attr)
141af1f1400SJiri Pirko 		return -EMSGSIZE;
142af1f1400SJiri Pirko 	if (devlink_nl_put_handle(msg, devlink))
143af1f1400SJiri Pirko 		goto nla_put_failure;
144af1f1400SJiri Pirko 
145c503bc7dSJiri Pirko 	rcu_read_lock();
146c503bc7dSJiri Pirko 	devl_net = read_pnet_rcu(&devlink->_net);
147c503bc7dSJiri Pirko 	if (!net_eq(net, devl_net)) {
148c503bc7dSJiri Pirko 		int id = peernet2id_alloc(net, devl_net, GFP_ATOMIC);
149c503bc7dSJiri Pirko 
150c503bc7dSJiri Pirko 		rcu_read_unlock();
151af1f1400SJiri Pirko 		if (nla_put_s32(msg, DEVLINK_ATTR_NETNS_ID, id))
152af1f1400SJiri Pirko 			return -EMSGSIZE;
153c503bc7dSJiri Pirko 	} else {
154c503bc7dSJiri Pirko 		rcu_read_unlock();
155af1f1400SJiri Pirko 	}
156af1f1400SJiri Pirko 
157af1f1400SJiri Pirko 	nla_nest_end(msg, nested_attr);
158af1f1400SJiri Pirko 	return 0;
159af1f1400SJiri Pirko 
160af1f1400SJiri Pirko nla_put_failure:
161af1f1400SJiri Pirko 	nla_nest_cancel(msg, nested_attr);
162af1f1400SJiri Pirko 	return -EMSGSIZE;
163af1f1400SJiri Pirko }
164af1f1400SJiri Pirko 
devlink_nl_msg_reply_and_new(struct sk_buff ** msg,struct genl_info * info)1652475ed15SJiri Pirko int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info)
1662475ed15SJiri Pirko {
1672475ed15SJiri Pirko 	int err;
1682475ed15SJiri Pirko 
1692475ed15SJiri Pirko 	if (*msg) {
1702475ed15SJiri Pirko 		err = genlmsg_reply(*msg, info);
1712475ed15SJiri Pirko 		if (err)
1722475ed15SJiri Pirko 			return err;
1732475ed15SJiri Pirko 	}
1742475ed15SJiri Pirko 	*msg = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1752475ed15SJiri Pirko 	if (!*msg)
1762475ed15SJiri Pirko 		return -ENOMEM;
1772475ed15SJiri Pirko 	return 0;
1782475ed15SJiri Pirko }
1792475ed15SJiri Pirko 
180870c7ad4SJakub Kicinski struct devlink *
devlink_get_from_attrs_lock(struct net * net,struct nlattr ** attrs,bool dev_lock)181d32c3825SIdo Schimmel devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs,
182d32c3825SIdo Schimmel 			    bool dev_lock)
183623cd13bSJakub Kicinski {
184623cd13bSJakub Kicinski 	struct devlink *devlink;
185623cd13bSJakub Kicinski 	unsigned long index;
186623cd13bSJakub Kicinski 	char *busname;
187623cd13bSJakub Kicinski 	char *devname;
188623cd13bSJakub Kicinski 
189623cd13bSJakub Kicinski 	if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
190623cd13bSJakub Kicinski 		return ERR_PTR(-EINVAL);
191623cd13bSJakub Kicinski 
192623cd13bSJakub Kicinski 	busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
193623cd13bSJakub Kicinski 	devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
194623cd13bSJakub Kicinski 
195623cd13bSJakub Kicinski 	devlinks_xa_for_each_registered_get(net, index, devlink) {
196*d7d75124SShay Drory 		if (strcmp(devlink->dev->bus->name, busname) == 0 &&
197*d7d75124SShay Drory 		    strcmp(dev_name(devlink->dev), devname) == 0) {
198d32c3825SIdo Schimmel 			devl_dev_lock(devlink, dev_lock);
199*d7d75124SShay Drory 			if (devl_is_registered(devlink))
200623cd13bSJakub Kicinski 				return devlink;
201d32c3825SIdo Schimmel 			devl_dev_unlock(devlink, dev_lock);
202*d7d75124SShay Drory 		}
203623cd13bSJakub Kicinski 		devlink_put(devlink);
204623cd13bSJakub Kicinski 	}
205623cd13bSJakub Kicinski 
206623cd13bSJakub Kicinski 	return ERR_PTR(-ENODEV);
207623cd13bSJakub Kicinski }
208623cd13bSJakub Kicinski 
__devlink_nl_pre_doit(struct sk_buff * skb,struct genl_info * info,u8 flags)209ee6d78acSJiri Pirko static int __devlink_nl_pre_doit(struct sk_buff *skb, struct genl_info *info,
210ee6d78acSJiri Pirko 				 u8 flags)
211623cd13bSJakub Kicinski {
212d32c3825SIdo Schimmel 	bool dev_lock = flags & DEVLINK_NL_FLAG_NEED_DEV_LOCK;
213623cd13bSJakub Kicinski 	struct devlink_port *devlink_port;
214623cd13bSJakub Kicinski 	struct devlink *devlink;
215623cd13bSJakub Kicinski 	int err;
216623cd13bSJakub Kicinski 
217d32c3825SIdo Schimmel 	devlink = devlink_get_from_attrs_lock(genl_info_net(info), info->attrs,
218d32c3825SIdo Schimmel 					      dev_lock);
219623cd13bSJakub Kicinski 	if (IS_ERR(devlink))
220623cd13bSJakub Kicinski 		return PTR_ERR(devlink);
221870c7ad4SJakub Kicinski 
222623cd13bSJakub Kicinski 	info->user_ptr[0] = devlink;
223ee6d78acSJiri Pirko 	if (flags & DEVLINK_NL_FLAG_NEED_PORT) {
224623cd13bSJakub Kicinski 		devlink_port = devlink_port_get_from_info(devlink, info);
225623cd13bSJakub Kicinski 		if (IS_ERR(devlink_port)) {
226623cd13bSJakub Kicinski 			err = PTR_ERR(devlink_port);
227623cd13bSJakub Kicinski 			goto unlock;
228623cd13bSJakub Kicinski 		}
229623cd13bSJakub Kicinski 		info->user_ptr[1] = devlink_port;
230ee6d78acSJiri Pirko 	} else if (flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
231623cd13bSJakub Kicinski 		devlink_port = devlink_port_get_from_info(devlink, info);
232623cd13bSJakub Kicinski 		if (!IS_ERR(devlink_port))
233623cd13bSJakub Kicinski 			info->user_ptr[1] = devlink_port;
234623cd13bSJakub Kicinski 	}
235623cd13bSJakub Kicinski 	return 0;
236623cd13bSJakub Kicinski 
237623cd13bSJakub Kicinski unlock:
238d32c3825SIdo Schimmel 	devl_dev_unlock(devlink, dev_lock);
239623cd13bSJakub Kicinski 	devlink_put(devlink);
240623cd13bSJakub Kicinski 	return err;
241623cd13bSJakub Kicinski }
242623cd13bSJakub Kicinski 
devlink_nl_pre_doit(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)243ee6d78acSJiri Pirko int devlink_nl_pre_doit(const struct genl_split_ops *ops,
244ee6d78acSJiri Pirko 			struct sk_buff *skb, struct genl_info *info)
245ee6d78acSJiri Pirko {
246cebe7306SJiri Pirko 	return __devlink_nl_pre_doit(skb, info, 0);
247ee6d78acSJiri Pirko }
248ee6d78acSJiri Pirko 
devlink_nl_pre_doit_port(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)249ee6d78acSJiri Pirko int devlink_nl_pre_doit_port(const struct genl_split_ops *ops,
250ee6d78acSJiri Pirko 			     struct sk_buff *skb, struct genl_info *info)
251ee6d78acSJiri Pirko {
252ee6d78acSJiri Pirko 	return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_PORT);
253ee6d78acSJiri Pirko }
254ee6d78acSJiri Pirko 
devlink_nl_pre_doit_dev_lock(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)255bf6b200bSIdo Schimmel int devlink_nl_pre_doit_dev_lock(const struct genl_split_ops *ops,
256bf6b200bSIdo Schimmel 				 struct sk_buff *skb, struct genl_info *info)
257bf6b200bSIdo Schimmel {
258bf6b200bSIdo Schimmel 	return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEV_LOCK);
259bf6b200bSIdo Schimmel }
260bf6b200bSIdo Schimmel 
devlink_nl_pre_doit_port_optional(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)261ee6d78acSJiri Pirko int devlink_nl_pre_doit_port_optional(const struct genl_split_ops *ops,
262ee6d78acSJiri Pirko 				      struct sk_buff *skb,
263ee6d78acSJiri Pirko 				      struct genl_info *info)
264ee6d78acSJiri Pirko {
265ee6d78acSJiri Pirko 	return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT);
266ee6d78acSJiri Pirko }
267ee6d78acSJiri Pirko 
__devlink_nl_post_doit(struct sk_buff * skb,struct genl_info * info,u8 flags)268c8d0a7d6SIdo Schimmel static void __devlink_nl_post_doit(struct sk_buff *skb, struct genl_info *info,
269c8d0a7d6SIdo Schimmel 				   u8 flags)
270623cd13bSJakub Kicinski {
271d32c3825SIdo Schimmel 	bool dev_lock = flags & DEVLINK_NL_FLAG_NEED_DEV_LOCK;
272623cd13bSJakub Kicinski 	struct devlink *devlink;
273623cd13bSJakub Kicinski 
274623cd13bSJakub Kicinski 	devlink = info->user_ptr[0];
275d32c3825SIdo Schimmel 	devl_dev_unlock(devlink, dev_lock);
276623cd13bSJakub Kicinski 	devlink_put(devlink);
277623cd13bSJakub Kicinski }
278623cd13bSJakub Kicinski 
devlink_nl_post_doit(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)279c8d0a7d6SIdo Schimmel void devlink_nl_post_doit(const struct genl_split_ops *ops,
280c8d0a7d6SIdo Schimmel 			  struct sk_buff *skb, struct genl_info *info)
281c8d0a7d6SIdo Schimmel {
282c8d0a7d6SIdo Schimmel 	__devlink_nl_post_doit(skb, info, 0);
283c8d0a7d6SIdo Schimmel }
284c8d0a7d6SIdo Schimmel 
285bf6b200bSIdo Schimmel void
devlink_nl_post_doit_dev_lock(const struct genl_split_ops * ops,struct sk_buff * skb,struct genl_info * info)286bf6b200bSIdo Schimmel devlink_nl_post_doit_dev_lock(const struct genl_split_ops *ops,
287bf6b200bSIdo Schimmel 			      struct sk_buff *skb, struct genl_info *info)
288bf6b200bSIdo Schimmel {
289bf6b200bSIdo Schimmel 	__devlink_nl_post_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEV_LOCK);
290bf6b200bSIdo Schimmel }
291bf6b200bSIdo Schimmel 
devlink_nl_inst_single_dumpit(struct sk_buff * msg,struct netlink_callback * cb,int flags,devlink_nl_dump_one_func_t * dump_one,struct nlattr ** attrs)2924a1b5aa8SJiri Pirko static int devlink_nl_inst_single_dumpit(struct sk_buff *msg,
2934a1b5aa8SJiri Pirko 					 struct netlink_callback *cb, int flags,
2944a1b5aa8SJiri Pirko 					 devlink_nl_dump_one_func_t *dump_one,
2954a1b5aa8SJiri Pirko 					 struct nlattr **attrs)
2964a1b5aa8SJiri Pirko {
2974a1b5aa8SJiri Pirko 	struct devlink *devlink;
2984a1b5aa8SJiri Pirko 	int err;
2994a1b5aa8SJiri Pirko 
300d32c3825SIdo Schimmel 	devlink = devlink_get_from_attrs_lock(sock_net(msg->sk), attrs, false);
3014a1b5aa8SJiri Pirko 	if (IS_ERR(devlink))
3024a1b5aa8SJiri Pirko 		return PTR_ERR(devlink);
3034a1b5aa8SJiri Pirko 	err = dump_one(msg, devlink, cb, flags | NLM_F_DUMP_FILTERED);
3044a1b5aa8SJiri Pirko 
3054a1b5aa8SJiri Pirko 	devl_unlock(devlink);
3064a1b5aa8SJiri Pirko 	devlink_put(devlink);
3074a1b5aa8SJiri Pirko 
3084a1b5aa8SJiri Pirko 	if (err != -EMSGSIZE)
3094a1b5aa8SJiri Pirko 		return err;
3104a1b5aa8SJiri Pirko 	return msg->len;
3114a1b5aa8SJiri Pirko }
3124a1b5aa8SJiri Pirko 
devlink_nl_inst_iter_dumpit(struct sk_buff * msg,struct netlink_callback * cb,int flags,devlink_nl_dump_one_func_t * dump_one)3134a1b5aa8SJiri Pirko static int devlink_nl_inst_iter_dumpit(struct sk_buff *msg,
3144a1b5aa8SJiri Pirko 				       struct netlink_callback *cb, int flags,
315491a2487SJiri Pirko 				       devlink_nl_dump_one_func_t *dump_one)
31607f3af66SJakub Kicinski {
31707f3af66SJakub Kicinski 	struct devlink_nl_dump_state *state = devlink_dump_state(cb);
31807f3af66SJakub Kicinski 	struct devlink *devlink;
31907f3af66SJakub Kicinski 	int err = 0;
32007f3af66SJakub Kicinski 
321543753d9SJiri Pirko 	while ((devlink = devlinks_xa_find_get(sock_net(msg->sk),
322543753d9SJiri Pirko 					       &state->instance))) {
32307f3af66SJakub Kicinski 		devl_lock(devlink);
324ed539ba6SJakub Kicinski 
325ed539ba6SJakub Kicinski 		if (devl_is_registered(devlink))
3264a1b5aa8SJiri Pirko 			err = dump_one(msg, devlink, cb, flags);
327ed539ba6SJakub Kicinski 		else
328ed539ba6SJakub Kicinski 			err = 0;
329ed539ba6SJakub Kicinski 
33007f3af66SJakub Kicinski 		devl_unlock(devlink);
33107f3af66SJakub Kicinski 		devlink_put(devlink);
33207f3af66SJakub Kicinski 
33307f3af66SJakub Kicinski 		if (err)
33407f3af66SJakub Kicinski 			break;
33507f3af66SJakub Kicinski 
336543753d9SJiri Pirko 		state->instance++;
337543753d9SJiri Pirko 
33807f3af66SJakub Kicinski 		/* restart sub-object walk for the next instance */
33907f3af66SJakub Kicinski 		state->idx = 0;
34007f3af66SJakub Kicinski 	}
34107f3af66SJakub Kicinski 
34207f3af66SJakub Kicinski 	if (err != -EMSGSIZE)
34307f3af66SJakub Kicinski 		return err;
34407f3af66SJakub Kicinski 	return msg->len;
34507f3af66SJakub Kicinski }
34607f3af66SJakub Kicinski 
devlink_nl_dumpit(struct sk_buff * msg,struct netlink_callback * cb,devlink_nl_dump_one_func_t * dump_one)3474a1b5aa8SJiri Pirko int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb,
3484a1b5aa8SJiri Pirko 		      devlink_nl_dump_one_func_t *dump_one)
3494a1b5aa8SJiri Pirko {
3507288dd2fSJakub Kicinski 	const struct genl_info *info = genl_info_dump(cb);
3514a1b5aa8SJiri Pirko 	struct nlattr **attrs = info->attrs;
3524a1b5aa8SJiri Pirko 	int flags = NLM_F_MULTI;
3534a1b5aa8SJiri Pirko 
3544a1b5aa8SJiri Pirko 	if (attrs &&
3554a1b5aa8SJiri Pirko 	    (attrs[DEVLINK_ATTR_BUS_NAME] || attrs[DEVLINK_ATTR_DEV_NAME]))
3564a1b5aa8SJiri Pirko 		return devlink_nl_inst_single_dumpit(msg, cb, flags, dump_one,
3574a1b5aa8SJiri Pirko 						     attrs);
3584a1b5aa8SJiri Pirko 	else
3594a1b5aa8SJiri Pirko 		return devlink_nl_inst_iter_dumpit(msg, cb, flags, dump_one);
3604a1b5aa8SJiri Pirko }
3614a1b5aa8SJiri Pirko 
362623cd13bSJakub Kicinski struct genl_family devlink_nl_family __ro_after_init = {
363623cd13bSJakub Kicinski 	.name		= DEVLINK_GENL_NAME,
364623cd13bSJakub Kicinski 	.version	= DEVLINK_GENL_VERSION,
365623cd13bSJakub Kicinski 	.netnsok	= true,
366623cd13bSJakub Kicinski 	.parallel_ops	= true,
367623cd13bSJakub Kicinski 	.module		= THIS_MODULE,
3686e067d0cSJiri Pirko 	.split_ops	= devlink_nl_ops,
3696e067d0cSJiri Pirko 	.n_split_ops	= ARRAY_SIZE(devlink_nl_ops),
370623cd13bSJakub Kicinski 	.resv_start_op	= DEVLINK_CMD_SELFTESTS_RUN + 1,
371623cd13bSJakub Kicinski 	.mcgrps		= devlink_nl_mcgrps,
372623cd13bSJakub Kicinski 	.n_mcgrps	= ARRAY_SIZE(devlink_nl_mcgrps),
37313b127d2SJiri Pirko 	.sock_priv_size		= sizeof(struct devlink_nl_sock_priv),
37413b127d2SJiri Pirko 	.sock_priv_init		= devlink_nl_sock_priv_init,
37513b127d2SJiri Pirko 	.sock_priv_destroy	= devlink_nl_sock_priv_destroy,
376623cd13bSJakub Kicinski };
377