xref: /openbmc/linux/net/devlink/core.c (revision d47a97bd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5  */
6 
7 #include <net/genetlink.h>
8 
9 #include "devl_internal.h"
10 
11 DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
12 
13 void *devlink_priv(struct devlink *devlink)
14 {
15 	return &devlink->priv;
16 }
17 EXPORT_SYMBOL_GPL(devlink_priv);
18 
19 struct devlink *priv_to_devlink(void *priv)
20 {
21 	return container_of(priv, struct devlink, priv);
22 }
23 EXPORT_SYMBOL_GPL(priv_to_devlink);
24 
25 struct device *devlink_to_dev(const struct devlink *devlink)
26 {
27 	return devlink->dev;
28 }
29 EXPORT_SYMBOL_GPL(devlink_to_dev);
30 
31 struct net *devlink_net(const struct devlink *devlink)
32 {
33 	return read_pnet(&devlink->_net);
34 }
35 EXPORT_SYMBOL_GPL(devlink_net);
36 
37 void devl_assert_locked(struct devlink *devlink)
38 {
39 	lockdep_assert_held(&devlink->lock);
40 }
41 EXPORT_SYMBOL_GPL(devl_assert_locked);
42 
43 #ifdef CONFIG_LOCKDEP
44 /* For use in conjunction with LOCKDEP only e.g. rcu_dereference_protected() */
45 bool devl_lock_is_held(struct devlink *devlink)
46 {
47 	return lockdep_is_held(&devlink->lock);
48 }
49 EXPORT_SYMBOL_GPL(devl_lock_is_held);
50 #endif
51 
52 void devl_lock(struct devlink *devlink)
53 {
54 	mutex_lock(&devlink->lock);
55 }
56 EXPORT_SYMBOL_GPL(devl_lock);
57 
58 int devl_trylock(struct devlink *devlink)
59 {
60 	return mutex_trylock(&devlink->lock);
61 }
62 EXPORT_SYMBOL_GPL(devl_trylock);
63 
64 void devl_unlock(struct devlink *devlink)
65 {
66 	mutex_unlock(&devlink->lock);
67 }
68 EXPORT_SYMBOL_GPL(devl_unlock);
69 
70 /**
71  * devlink_try_get() - try to obtain a reference on a devlink instance
72  * @devlink: instance to reference
73  *
74  * Obtain a reference on a devlink instance. A reference on a devlink instance
75  * only implies that it's safe to take the instance lock. It does not imply
76  * that the instance is registered, use devl_is_registered() after taking
77  * the instance lock to check registration status.
78  */
79 struct devlink *__must_check devlink_try_get(struct devlink *devlink)
80 {
81 	if (refcount_inc_not_zero(&devlink->refcount))
82 		return devlink;
83 	return NULL;
84 }
85 
86 static void devlink_release(struct work_struct *work)
87 {
88 	struct devlink *devlink;
89 
90 	devlink = container_of(to_rcu_work(work), struct devlink, rwork);
91 
92 	mutex_destroy(&devlink->lock);
93 	lockdep_unregister_key(&devlink->lock_key);
94 	kfree(devlink);
95 }
96 
97 void devlink_put(struct devlink *devlink)
98 {
99 	if (refcount_dec_and_test(&devlink->refcount))
100 		queue_rcu_work(system_wq, &devlink->rwork);
101 }
102 
103 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp)
104 {
105 	struct devlink *devlink = NULL;
106 
107 	rcu_read_lock();
108 retry:
109 	devlink = xa_find(&devlinks, indexp, ULONG_MAX, DEVLINK_REGISTERED);
110 	if (!devlink)
111 		goto unlock;
112 
113 	if (!devlink_try_get(devlink))
114 		goto next;
115 	if (!net_eq(devlink_net(devlink), net)) {
116 		devlink_put(devlink);
117 		goto next;
118 	}
119 unlock:
120 	rcu_read_unlock();
121 	return devlink;
122 
123 next:
124 	(*indexp)++;
125 	goto retry;
126 }
127 
128 /**
129  * devl_register - Register devlink instance
130  * @devlink: devlink
131  */
132 int devl_register(struct devlink *devlink)
133 {
134 	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
135 	devl_assert_locked(devlink);
136 
137 	xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
138 	devlink_notify_register(devlink);
139 
140 	return 0;
141 }
142 EXPORT_SYMBOL_GPL(devl_register);
143 
144 void devlink_register(struct devlink *devlink)
145 {
146 	devl_lock(devlink);
147 	devl_register(devlink);
148 	devl_unlock(devlink);
149 }
150 EXPORT_SYMBOL_GPL(devlink_register);
151 
152 /**
153  * devl_unregister - Unregister devlink instance
154  * @devlink: devlink
155  */
156 void devl_unregister(struct devlink *devlink)
157 {
158 	ASSERT_DEVLINK_REGISTERED(devlink);
159 	devl_assert_locked(devlink);
160 
161 	devlink_notify_unregister(devlink);
162 	xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
163 }
164 EXPORT_SYMBOL_GPL(devl_unregister);
165 
166 void devlink_unregister(struct devlink *devlink)
167 {
168 	devl_lock(devlink);
169 	devl_unregister(devlink);
170 	devl_unlock(devlink);
171 }
172 EXPORT_SYMBOL_GPL(devlink_unregister);
173 
174 /**
175  *	devlink_alloc_ns - Allocate new devlink instance resources
176  *	in specific namespace
177  *
178  *	@ops: ops
179  *	@priv_size: size of user private data
180  *	@net: net namespace
181  *	@dev: parent device
182  *
183  *	Allocate new devlink instance resources, including devlink index
184  *	and name.
185  */
186 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
187 				 size_t priv_size, struct net *net,
188 				 struct device *dev)
189 {
190 	struct devlink *devlink;
191 	static u32 last_id;
192 	int ret;
193 
194 	WARN_ON(!ops || !dev);
195 	if (!devlink_reload_actions_valid(ops))
196 		return NULL;
197 
198 	devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
199 	if (!devlink)
200 		return NULL;
201 
202 	ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
203 			      &last_id, GFP_KERNEL);
204 	if (ret < 0)
205 		goto err_xa_alloc;
206 
207 	devlink->netdevice_nb.notifier_call = devlink_port_netdevice_event;
208 	ret = register_netdevice_notifier(&devlink->netdevice_nb);
209 	if (ret)
210 		goto err_register_netdevice_notifier;
211 
212 	devlink->dev = dev;
213 	devlink->ops = ops;
214 	xa_init_flags(&devlink->ports, XA_FLAGS_ALLOC);
215 	xa_init_flags(&devlink->params, XA_FLAGS_ALLOC);
216 	xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
217 	write_pnet(&devlink->_net, net);
218 	INIT_LIST_HEAD(&devlink->rate_list);
219 	INIT_LIST_HEAD(&devlink->linecard_list);
220 	INIT_LIST_HEAD(&devlink->sb_list);
221 	INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
222 	INIT_LIST_HEAD(&devlink->resource_list);
223 	INIT_LIST_HEAD(&devlink->region_list);
224 	INIT_LIST_HEAD(&devlink->reporter_list);
225 	INIT_LIST_HEAD(&devlink->trap_list);
226 	INIT_LIST_HEAD(&devlink->trap_group_list);
227 	INIT_LIST_HEAD(&devlink->trap_policer_list);
228 	INIT_RCU_WORK(&devlink->rwork, devlink_release);
229 	lockdep_register_key(&devlink->lock_key);
230 	mutex_init(&devlink->lock);
231 	lockdep_set_class(&devlink->lock, &devlink->lock_key);
232 	refcount_set(&devlink->refcount, 1);
233 
234 	return devlink;
235 
236 err_register_netdevice_notifier:
237 	xa_erase(&devlinks, devlink->index);
238 err_xa_alloc:
239 	kfree(devlink);
240 	return NULL;
241 }
242 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
243 
244 /**
245  *	devlink_free - Free devlink instance resources
246  *
247  *	@devlink: devlink
248  */
249 void devlink_free(struct devlink *devlink)
250 {
251 	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
252 
253 	WARN_ON(!list_empty(&devlink->trap_policer_list));
254 	WARN_ON(!list_empty(&devlink->trap_group_list));
255 	WARN_ON(!list_empty(&devlink->trap_list));
256 	WARN_ON(!list_empty(&devlink->reporter_list));
257 	WARN_ON(!list_empty(&devlink->region_list));
258 	WARN_ON(!list_empty(&devlink->resource_list));
259 	WARN_ON(!list_empty(&devlink->dpipe_table_list));
260 	WARN_ON(!list_empty(&devlink->sb_list));
261 	WARN_ON(!list_empty(&devlink->rate_list));
262 	WARN_ON(!list_empty(&devlink->linecard_list));
263 	WARN_ON(!xa_empty(&devlink->ports));
264 
265 	xa_destroy(&devlink->snapshot_ids);
266 	xa_destroy(&devlink->params);
267 	xa_destroy(&devlink->ports);
268 
269 	WARN_ON_ONCE(unregister_netdevice_notifier(&devlink->netdevice_nb));
270 
271 	xa_erase(&devlinks, devlink->index);
272 
273 	devlink_put(devlink);
274 }
275 EXPORT_SYMBOL_GPL(devlink_free);
276 
277 static void __net_exit devlink_pernet_pre_exit(struct net *net)
278 {
279 	struct devlink *devlink;
280 	u32 actions_performed;
281 	unsigned long index;
282 	int err;
283 
284 	/* In case network namespace is getting destroyed, reload
285 	 * all devlink instances from this namespace into init_net.
286 	 */
287 	devlinks_xa_for_each_registered_get(net, index, devlink) {
288 		devl_lock(devlink);
289 		err = 0;
290 		if (devl_is_registered(devlink))
291 			err = devlink_reload(devlink, &init_net,
292 					     DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
293 					     DEVLINK_RELOAD_LIMIT_UNSPEC,
294 					     &actions_performed, NULL);
295 		devl_unlock(devlink);
296 		devlink_put(devlink);
297 		if (err && err != -EOPNOTSUPP)
298 			pr_warn("Failed to reload devlink instance into init_net\n");
299 	}
300 }
301 
302 static struct pernet_operations devlink_pernet_ops __net_initdata = {
303 	.pre_exit = devlink_pernet_pre_exit,
304 };
305 
306 static int __init devlink_init(void)
307 {
308 	int err;
309 
310 	err = genl_register_family(&devlink_nl_family);
311 	if (err)
312 		goto out;
313 	err = register_pernet_subsys(&devlink_pernet_ops);
314 
315 out:
316 	WARN_ON(err);
317 	return err;
318 }
319 
320 subsys_initcall(devlink_init);
321