xref: /linux/net/switchdev/switchdev.c (revision 0c69bd2ca6ee20064dde7853cd749284e053a874)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/switchdev/switchdev.c - Switch device API
4  * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
5  * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/init.h>
11 #include <linux/mutex.h>
12 #include <linux/notifier.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/if_bridge.h>
16 #include <linux/list.h>
17 #include <linux/workqueue.h>
18 #include <linux/if_vlan.h>
19 #include <linux/rtnetlink.h>
20 #include <net/switchdev.h>
21 
22 static LIST_HEAD(deferred);
23 static DEFINE_SPINLOCK(deferred_lock);
24 
25 typedef void switchdev_deferred_func_t(struct net_device *dev,
26 				       const void *data);
27 
28 struct switchdev_deferred_item {
29 	struct list_head list;
30 	struct net_device *dev;
31 	switchdev_deferred_func_t *func;
32 	unsigned long data[];
33 };
34 
35 static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
36 {
37 	struct switchdev_deferred_item *dfitem;
38 
39 	spin_lock_bh(&deferred_lock);
40 	if (list_empty(&deferred)) {
41 		dfitem = NULL;
42 		goto unlock;
43 	}
44 	dfitem = list_first_entry(&deferred,
45 				  struct switchdev_deferred_item, list);
46 	list_del(&dfitem->list);
47 unlock:
48 	spin_unlock_bh(&deferred_lock);
49 	return dfitem;
50 }
51 
52 /**
53  *	switchdev_deferred_process - Process ops in deferred queue
54  *
55  *	Called to flush the ops currently queued in deferred ops queue.
56  *	rtnl_lock must be held.
57  */
58 void switchdev_deferred_process(void)
59 {
60 	struct switchdev_deferred_item *dfitem;
61 
62 	ASSERT_RTNL();
63 
64 	while ((dfitem = switchdev_deferred_dequeue())) {
65 		dfitem->func(dfitem->dev, dfitem->data);
66 		dev_put(dfitem->dev);
67 		kfree(dfitem);
68 	}
69 }
70 EXPORT_SYMBOL_GPL(switchdev_deferred_process);
71 
72 static void switchdev_deferred_process_work(struct work_struct *work)
73 {
74 	rtnl_lock();
75 	switchdev_deferred_process();
76 	rtnl_unlock();
77 }
78 
79 static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
80 
81 static int switchdev_deferred_enqueue(struct net_device *dev,
82 				      const void *data, size_t data_len,
83 				      switchdev_deferred_func_t *func)
84 {
85 	struct switchdev_deferred_item *dfitem;
86 
87 	dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
88 	if (!dfitem)
89 		return -ENOMEM;
90 	dfitem->dev = dev;
91 	dfitem->func = func;
92 	memcpy(dfitem->data, data, data_len);
93 	dev_hold(dev);
94 	spin_lock_bh(&deferred_lock);
95 	list_add_tail(&dfitem->list, &deferred);
96 	spin_unlock_bh(&deferred_lock);
97 	schedule_work(&deferred_process_work);
98 	return 0;
99 }
100 
101 static int switchdev_port_attr_notify(enum switchdev_notifier_type nt,
102 				      struct net_device *dev,
103 				      const struct switchdev_attr *attr,
104 				      struct netlink_ext_ack *extack)
105 {
106 	int err;
107 	int rc;
108 
109 	struct switchdev_notifier_port_attr_info attr_info = {
110 		.attr = attr,
111 		.handled = false,
112 	};
113 
114 	rc = call_switchdev_blocking_notifiers(nt, dev,
115 					       &attr_info.info, extack);
116 	err = notifier_to_errno(rc);
117 	if (err) {
118 		WARN_ON(!attr_info.handled);
119 		return err;
120 	}
121 
122 	if (!attr_info.handled)
123 		return -EOPNOTSUPP;
124 
125 	return 0;
126 }
127 
128 static int switchdev_port_attr_set_now(struct net_device *dev,
129 				       const struct switchdev_attr *attr,
130 				       struct netlink_ext_ack *extack)
131 {
132 	return switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr,
133 					  extack);
134 }
135 
136 static void switchdev_port_attr_set_deferred(struct net_device *dev,
137 					     const void *data)
138 {
139 	const struct switchdev_attr *attr = data;
140 	int err;
141 
142 	err = switchdev_port_attr_set_now(dev, attr, NULL);
143 	if (err && err != -EOPNOTSUPP)
144 		netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
145 			   err, attr->id);
146 	if (attr->complete)
147 		attr->complete(dev, err, attr->complete_priv);
148 }
149 
150 static int switchdev_port_attr_set_defer(struct net_device *dev,
151 					 const struct switchdev_attr *attr)
152 {
153 	return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
154 					  switchdev_port_attr_set_deferred);
155 }
156 
157 /**
158  *	switchdev_port_attr_set - Set port attribute
159  *
160  *	@dev: port device
161  *	@attr: attribute to set
162  *	@extack: netlink extended ack, for error message propagation
163  *
164  *	rtnl_lock must be held and must not be in atomic section,
165  *	in case SWITCHDEV_F_DEFER flag is not set.
166  */
167 int switchdev_port_attr_set(struct net_device *dev,
168 			    const struct switchdev_attr *attr,
169 			    struct netlink_ext_ack *extack)
170 {
171 	if (attr->flags & SWITCHDEV_F_DEFER)
172 		return switchdev_port_attr_set_defer(dev, attr);
173 	ASSERT_RTNL();
174 	return switchdev_port_attr_set_now(dev, attr, extack);
175 }
176 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
177 
178 static size_t switchdev_obj_size(const struct switchdev_obj *obj)
179 {
180 	switch (obj->id) {
181 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
182 		return sizeof(struct switchdev_obj_port_vlan);
183 	case SWITCHDEV_OBJ_ID_PORT_MDB:
184 		return sizeof(struct switchdev_obj_port_mdb);
185 	case SWITCHDEV_OBJ_ID_HOST_MDB:
186 		return sizeof(struct switchdev_obj_port_mdb);
187 	default:
188 		BUG();
189 	}
190 	return 0;
191 }
192 
193 static int switchdev_port_obj_notify(enum switchdev_notifier_type nt,
194 				     struct net_device *dev,
195 				     const struct switchdev_obj *obj,
196 				     struct netlink_ext_ack *extack)
197 {
198 	int rc;
199 	int err;
200 
201 	struct switchdev_notifier_port_obj_info obj_info = {
202 		.obj = obj,
203 		.handled = false,
204 	};
205 
206 	rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack);
207 	err = notifier_to_errno(rc);
208 	if (err) {
209 		WARN_ON(!obj_info.handled);
210 		return err;
211 	}
212 	if (!obj_info.handled)
213 		return -EOPNOTSUPP;
214 	return 0;
215 }
216 
217 static void switchdev_port_obj_add_deferred(struct net_device *dev,
218 					    const void *data)
219 {
220 	const struct switchdev_obj *obj = data;
221 	int err;
222 
223 	ASSERT_RTNL();
224 	err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
225 					dev, obj, NULL);
226 	if (err && err != -EOPNOTSUPP)
227 		netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
228 			   err, obj->id);
229 	if (obj->complete)
230 		obj->complete(dev, err, obj->complete_priv);
231 }
232 
233 static int switchdev_port_obj_add_defer(struct net_device *dev,
234 					const struct switchdev_obj *obj)
235 {
236 	return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
237 					  switchdev_port_obj_add_deferred);
238 }
239 
240 /**
241  *	switchdev_port_obj_add - Add port object
242  *
243  *	@dev: port device
244  *	@obj: object to add
245  *	@extack: netlink extended ack
246  *
247  *	rtnl_lock must be held and must not be in atomic section,
248  *	in case SWITCHDEV_F_DEFER flag is not set.
249  */
250 int switchdev_port_obj_add(struct net_device *dev,
251 			   const struct switchdev_obj *obj,
252 			   struct netlink_ext_ack *extack)
253 {
254 	if (obj->flags & SWITCHDEV_F_DEFER)
255 		return switchdev_port_obj_add_defer(dev, obj);
256 	ASSERT_RTNL();
257 	return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
258 					 dev, obj, extack);
259 }
260 EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
261 
262 static int switchdev_port_obj_del_now(struct net_device *dev,
263 				      const struct switchdev_obj *obj)
264 {
265 	return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL,
266 					 dev, obj, NULL);
267 }
268 
269 static void switchdev_port_obj_del_deferred(struct net_device *dev,
270 					    const void *data)
271 {
272 	const struct switchdev_obj *obj = data;
273 	int err;
274 
275 	err = switchdev_port_obj_del_now(dev, obj);
276 	if (err && err != -EOPNOTSUPP)
277 		netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
278 			   err, obj->id);
279 	if (obj->complete)
280 		obj->complete(dev, err, obj->complete_priv);
281 }
282 
283 static int switchdev_port_obj_del_defer(struct net_device *dev,
284 					const struct switchdev_obj *obj)
285 {
286 	return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
287 					  switchdev_port_obj_del_deferred);
288 }
289 
290 /**
291  *	switchdev_port_obj_del - Delete port object
292  *
293  *	@dev: port device
294  *	@obj: object to delete
295  *
296  *	rtnl_lock must be held and must not be in atomic section,
297  *	in case SWITCHDEV_F_DEFER flag is not set.
298  */
299 int switchdev_port_obj_del(struct net_device *dev,
300 			   const struct switchdev_obj *obj)
301 {
302 	if (obj->flags & SWITCHDEV_F_DEFER)
303 		return switchdev_port_obj_del_defer(dev, obj);
304 	ASSERT_RTNL();
305 	return switchdev_port_obj_del_now(dev, obj);
306 }
307 EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
308 
309 static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
310 static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
311 
312 /**
313  *	register_switchdev_notifier - Register notifier
314  *	@nb: notifier_block
315  *
316  *	Register switch device notifier.
317  */
318 int register_switchdev_notifier(struct notifier_block *nb)
319 {
320 	return atomic_notifier_chain_register(&switchdev_notif_chain, nb);
321 }
322 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
323 
324 /**
325  *	unregister_switchdev_notifier - Unregister notifier
326  *	@nb: notifier_block
327  *
328  *	Unregister switch device notifier.
329  */
330 int unregister_switchdev_notifier(struct notifier_block *nb)
331 {
332 	return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb);
333 }
334 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
335 
336 /**
337  *	call_switchdev_notifiers - Call notifiers
338  *	@val: value passed unmodified to notifier function
339  *	@dev: port device
340  *	@info: notifier information data
341  *	@extack: netlink extended ack
342  *	Call all network notifier blocks.
343  */
344 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
345 			     struct switchdev_notifier_info *info,
346 			     struct netlink_ext_ack *extack)
347 {
348 	info->dev = dev;
349 	info->extack = extack;
350 	return atomic_notifier_call_chain(&switchdev_notif_chain, val, info);
351 }
352 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
353 
354 int register_switchdev_blocking_notifier(struct notifier_block *nb)
355 {
356 	struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
357 
358 	return blocking_notifier_chain_register(chain, nb);
359 }
360 EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
361 
362 int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
363 {
364 	struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
365 
366 	return blocking_notifier_chain_unregister(chain, nb);
367 }
368 EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
369 
370 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
371 				      struct switchdev_notifier_info *info,
372 				      struct netlink_ext_ack *extack)
373 {
374 	info->dev = dev;
375 	info->extack = extack;
376 	return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
377 					    val, info);
378 }
379 EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
380 
381 static int __switchdev_handle_port_obj_add(struct net_device *dev,
382 			struct switchdev_notifier_port_obj_info *port_obj_info,
383 			bool (*check_cb)(const struct net_device *dev),
384 			int (*add_cb)(struct net_device *dev, const void *ctx,
385 				      const struct switchdev_obj *obj,
386 				      struct netlink_ext_ack *extack))
387 {
388 	struct switchdev_notifier_info *info = &port_obj_info->info;
389 	struct netlink_ext_ack *extack;
390 	struct net_device *lower_dev;
391 	struct list_head *iter;
392 	int err = -EOPNOTSUPP;
393 
394 	extack = switchdev_notifier_info_to_extack(info);
395 
396 	if (check_cb(dev)) {
397 		err = add_cb(dev, info->ctx, port_obj_info->obj, extack);
398 		if (err != -EOPNOTSUPP)
399 			port_obj_info->handled = true;
400 		return err;
401 	}
402 
403 	/* Switch ports might be stacked under e.g. a LAG. Ignore the
404 	 * unsupported devices, another driver might be able to handle them. But
405 	 * propagate to the callers any hard errors.
406 	 *
407 	 * If the driver does its own bookkeeping of stacked ports, it's not
408 	 * necessary to go through this helper.
409 	 */
410 	netdev_for_each_lower_dev(dev, lower_dev, iter) {
411 		if (netif_is_bridge_master(lower_dev))
412 			continue;
413 
414 		err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info,
415 						      check_cb, add_cb);
416 		if (err && err != -EOPNOTSUPP)
417 			return err;
418 	}
419 
420 	return err;
421 }
422 
423 int switchdev_handle_port_obj_add(struct net_device *dev,
424 			struct switchdev_notifier_port_obj_info *port_obj_info,
425 			bool (*check_cb)(const struct net_device *dev),
426 			int (*add_cb)(struct net_device *dev, const void *ctx,
427 				      const struct switchdev_obj *obj,
428 				      struct netlink_ext_ack *extack))
429 {
430 	int err;
431 
432 	err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
433 					      add_cb);
434 	if (err == -EOPNOTSUPP)
435 		err = 0;
436 	return err;
437 }
438 EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
439 
440 static int __switchdev_handle_port_obj_del(struct net_device *dev,
441 			struct switchdev_notifier_port_obj_info *port_obj_info,
442 			bool (*check_cb)(const struct net_device *dev),
443 			int (*del_cb)(struct net_device *dev, const void *ctx,
444 				      const struct switchdev_obj *obj))
445 {
446 	struct switchdev_notifier_info *info = &port_obj_info->info;
447 	struct net_device *lower_dev;
448 	struct list_head *iter;
449 	int err = -EOPNOTSUPP;
450 
451 	if (check_cb(dev)) {
452 		err = del_cb(dev, info->ctx, port_obj_info->obj);
453 		if (err != -EOPNOTSUPP)
454 			port_obj_info->handled = true;
455 		return err;
456 	}
457 
458 	/* Switch ports might be stacked under e.g. a LAG. Ignore the
459 	 * unsupported devices, another driver might be able to handle them. But
460 	 * propagate to the callers any hard errors.
461 	 *
462 	 * If the driver does its own bookkeeping of stacked ports, it's not
463 	 * necessary to go through this helper.
464 	 */
465 	netdev_for_each_lower_dev(dev, lower_dev, iter) {
466 		if (netif_is_bridge_master(lower_dev))
467 			continue;
468 
469 		err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
470 						      check_cb, del_cb);
471 		if (err && err != -EOPNOTSUPP)
472 			return err;
473 	}
474 
475 	return err;
476 }
477 
478 int switchdev_handle_port_obj_del(struct net_device *dev,
479 			struct switchdev_notifier_port_obj_info *port_obj_info,
480 			bool (*check_cb)(const struct net_device *dev),
481 			int (*del_cb)(struct net_device *dev, const void *ctx,
482 				      const struct switchdev_obj *obj))
483 {
484 	int err;
485 
486 	err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
487 					      del_cb);
488 	if (err == -EOPNOTSUPP)
489 		err = 0;
490 	return err;
491 }
492 EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);
493 
494 static int __switchdev_handle_port_attr_set(struct net_device *dev,
495 			struct switchdev_notifier_port_attr_info *port_attr_info,
496 			bool (*check_cb)(const struct net_device *dev),
497 			int (*set_cb)(struct net_device *dev, const void *ctx,
498 				      const struct switchdev_attr *attr,
499 				      struct netlink_ext_ack *extack))
500 {
501 	struct switchdev_notifier_info *info = &port_attr_info->info;
502 	struct netlink_ext_ack *extack;
503 	struct net_device *lower_dev;
504 	struct list_head *iter;
505 	int err = -EOPNOTSUPP;
506 
507 	extack = switchdev_notifier_info_to_extack(info);
508 
509 	if (check_cb(dev)) {
510 		err = set_cb(dev, info->ctx, port_attr_info->attr, extack);
511 		if (err != -EOPNOTSUPP)
512 			port_attr_info->handled = true;
513 		return err;
514 	}
515 
516 	/* Switch ports might be stacked under e.g. a LAG. Ignore the
517 	 * unsupported devices, another driver might be able to handle them. But
518 	 * propagate to the callers any hard errors.
519 	 *
520 	 * If the driver does its own bookkeeping of stacked ports, it's not
521 	 * necessary to go through this helper.
522 	 */
523 	netdev_for_each_lower_dev(dev, lower_dev, iter) {
524 		if (netif_is_bridge_master(lower_dev))
525 			continue;
526 
527 		err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info,
528 						       check_cb, set_cb);
529 		if (err && err != -EOPNOTSUPP)
530 			return err;
531 	}
532 
533 	return err;
534 }
535 
536 int switchdev_handle_port_attr_set(struct net_device *dev,
537 			struct switchdev_notifier_port_attr_info *port_attr_info,
538 			bool (*check_cb)(const struct net_device *dev),
539 			int (*set_cb)(struct net_device *dev, const void *ctx,
540 				      const struct switchdev_attr *attr,
541 				      struct netlink_ext_ack *extack))
542 {
543 	int err;
544 
545 	err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb,
546 					       set_cb);
547 	if (err == -EOPNOTSUPP)
548 		err = 0;
549 	return err;
550 }
551 EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);
552