1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/switchdev/switchdev.c - Switch device API
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>
22 static LIST_HEAD(deferred);
23 static DEFINE_SPINLOCK(deferred_lock);
25 typedef void switchdev_deferred_func_t(struct net_device *dev,
28 struct switchdev_deferred_item {
29 struct list_head list;
30 struct net_device *dev;
31 switchdev_deferred_func_t *func;
35 static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
37 struct switchdev_deferred_item *dfitem;
39 spin_lock_bh(&deferred_lock);
40 if (list_empty(&deferred)) {
44 dfitem = list_first_entry(&deferred,
45 struct switchdev_deferred_item, list);
46 list_del(&dfitem->list);
48 spin_unlock_bh(&deferred_lock);
53 * switchdev_deferred_process - Process ops in deferred queue
55 * Called to flush the ops currently queued in deferred ops queue.
56 * rtnl_lock must be held.
58 void switchdev_deferred_process(void)
60 struct switchdev_deferred_item *dfitem;
64 while ((dfitem = switchdev_deferred_dequeue())) {
65 dfitem->func(dfitem->dev, dfitem->data);
70 EXPORT_SYMBOL_GPL(switchdev_deferred_process);
72 static void switchdev_deferred_process_work(struct work_struct *work)
75 switchdev_deferred_process();
79 static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
81 static int switchdev_deferred_enqueue(struct net_device *dev,
82 const void *data, size_t data_len,
83 switchdev_deferred_func_t *func)
85 struct switchdev_deferred_item *dfitem;
87 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
92 memcpy(dfitem->data, data, data_len);
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);
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)
109 struct switchdev_notifier_port_attr_info attr_info = {
114 rc = call_switchdev_blocking_notifiers(nt, dev,
115 &attr_info.info, extack);
116 err = notifier_to_errno(rc);
118 WARN_ON(!attr_info.handled);
122 if (!attr_info.handled)
128 static int switchdev_port_attr_set_now(struct net_device *dev,
129 const struct switchdev_attr *attr,
130 struct netlink_ext_ack *extack)
132 return switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr,
136 static void switchdev_port_attr_set_deferred(struct net_device *dev,
139 const struct switchdev_attr *attr = data;
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",
147 attr->complete(dev, err, attr->complete_priv);
150 static int switchdev_port_attr_set_defer(struct net_device *dev,
151 const struct switchdev_attr *attr)
153 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
154 switchdev_port_attr_set_deferred);
158 * switchdev_port_attr_set - Set port attribute
161 * @attr: attribute to set
162 * @extack: netlink extended ack, for error message propagation
164 * rtnl_lock must be held and must not be in atomic section,
165 * in case SWITCHDEV_F_DEFER flag is not set.
167 int switchdev_port_attr_set(struct net_device *dev,
168 const struct switchdev_attr *attr,
169 struct netlink_ext_ack *extack)
171 if (attr->flags & SWITCHDEV_F_DEFER)
172 return switchdev_port_attr_set_defer(dev, attr);
174 return switchdev_port_attr_set_now(dev, attr, extack);
176 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
178 static size_t switchdev_obj_size(const struct switchdev_obj *obj)
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);
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)
201 struct switchdev_notifier_port_obj_info obj_info = {
206 rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack);
207 err = notifier_to_errno(rc);
209 WARN_ON(!obj_info.handled);
212 if (!obj_info.handled)
217 static void switchdev_port_obj_add_deferred(struct net_device *dev,
220 const struct switchdev_obj *obj = data;
224 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
226 if (err && err != -EOPNOTSUPP)
227 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
230 obj->complete(dev, err, obj->complete_priv);
233 static int switchdev_port_obj_add_defer(struct net_device *dev,
234 const struct switchdev_obj *obj)
236 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
237 switchdev_port_obj_add_deferred);
241 * switchdev_port_obj_add - Add port object
244 * @obj: object to add
245 * @extack: netlink extended ack
247 * rtnl_lock must be held and must not be in atomic section,
248 * in case SWITCHDEV_F_DEFER flag is not set.
250 int switchdev_port_obj_add(struct net_device *dev,
251 const struct switchdev_obj *obj,
252 struct netlink_ext_ack *extack)
254 if (obj->flags & SWITCHDEV_F_DEFER)
255 return switchdev_port_obj_add_defer(dev, obj);
257 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
260 EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
262 static int switchdev_port_obj_del_now(struct net_device *dev,
263 const struct switchdev_obj *obj)
265 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL,
269 static void switchdev_port_obj_del_deferred(struct net_device *dev,
272 const struct switchdev_obj *obj = data;
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",
280 obj->complete(dev, err, obj->complete_priv);
283 static int switchdev_port_obj_del_defer(struct net_device *dev,
284 const struct switchdev_obj *obj)
286 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
287 switchdev_port_obj_del_deferred);
291 * switchdev_port_obj_del - Delete port object
294 * @obj: object to delete
296 * rtnl_lock must be held and must not be in atomic section,
297 * in case SWITCHDEV_F_DEFER flag is not set.
299 int switchdev_port_obj_del(struct net_device *dev,
300 const struct switchdev_obj *obj)
302 if (obj->flags & SWITCHDEV_F_DEFER)
303 return switchdev_port_obj_del_defer(dev, obj);
305 return switchdev_port_obj_del_now(dev, obj);
307 EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
309 static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
310 static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
313 * register_switchdev_notifier - Register notifier
314 * @nb: notifier_block
316 * Register switch device notifier.
318 int register_switchdev_notifier(struct notifier_block *nb)
320 return atomic_notifier_chain_register(&switchdev_notif_chain, nb);
322 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
325 * unregister_switchdev_notifier - Unregister notifier
326 * @nb: notifier_block
328 * Unregister switch device notifier.
330 int unregister_switchdev_notifier(struct notifier_block *nb)
332 return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb);
334 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
337 * call_switchdev_notifiers - Call notifiers
338 * @val: value passed unmodified to notifier function
340 * @info: notifier information data
341 * @extack: netlink extended ack
342 * Call all network notifier blocks.
344 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
345 struct switchdev_notifier_info *info,
346 struct netlink_ext_ack *extack)
349 info->extack = extack;
350 return atomic_notifier_call_chain(&switchdev_notif_chain, val, info);
352 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
354 int register_switchdev_blocking_notifier(struct notifier_block *nb)
356 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
358 return blocking_notifier_chain_register(chain, nb);
360 EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
362 int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
364 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
366 return blocking_notifier_chain_unregister(chain, nb);
368 EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
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)
375 info->extack = extack;
376 return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
379 EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
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,
385 const struct switchdev_obj *obj,
386 struct netlink_ext_ack *extack))
388 struct netlink_ext_ack *extack;
389 struct net_device *lower_dev;
390 struct list_head *iter;
391 int err = -EOPNOTSUPP;
393 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
396 err = add_cb(dev, port_obj_info->obj, extack);
397 if (err != -EOPNOTSUPP)
398 port_obj_info->handled = true;
402 /* Switch ports might be stacked under e.g. a LAG. Ignore the
403 * unsupported devices, another driver might be able to handle them. But
404 * propagate to the callers any hard errors.
406 * If the driver does its own bookkeeping of stacked ports, it's not
407 * necessary to go through this helper.
409 netdev_for_each_lower_dev(dev, lower_dev, iter) {
410 if (netif_is_bridge_master(lower_dev))
413 err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info,
415 if (err && err != -EOPNOTSUPP)
422 int switchdev_handle_port_obj_add(struct net_device *dev,
423 struct switchdev_notifier_port_obj_info *port_obj_info,
424 bool (*check_cb)(const struct net_device *dev),
425 int (*add_cb)(struct net_device *dev,
426 const struct switchdev_obj *obj,
427 struct netlink_ext_ack *extack))
431 err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
433 if (err == -EOPNOTSUPP)
437 EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
439 static int __switchdev_handle_port_obj_del(struct net_device *dev,
440 struct switchdev_notifier_port_obj_info *port_obj_info,
441 bool (*check_cb)(const struct net_device *dev),
442 int (*del_cb)(struct net_device *dev,
443 const struct switchdev_obj *obj))
445 struct net_device *lower_dev;
446 struct list_head *iter;
447 int err = -EOPNOTSUPP;
450 err = del_cb(dev, port_obj_info->obj);
451 if (err != -EOPNOTSUPP)
452 port_obj_info->handled = true;
456 /* Switch ports might be stacked under e.g. a LAG. Ignore the
457 * unsupported devices, another driver might be able to handle them. But
458 * propagate to the callers any hard errors.
460 * If the driver does its own bookkeeping of stacked ports, it's not
461 * necessary to go through this helper.
463 netdev_for_each_lower_dev(dev, lower_dev, iter) {
464 if (netif_is_bridge_master(lower_dev))
467 err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
469 if (err && err != -EOPNOTSUPP)
476 int switchdev_handle_port_obj_del(struct net_device *dev,
477 struct switchdev_notifier_port_obj_info *port_obj_info,
478 bool (*check_cb)(const struct net_device *dev),
479 int (*del_cb)(struct net_device *dev,
480 const struct switchdev_obj *obj))
484 err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
486 if (err == -EOPNOTSUPP)
490 EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);
492 static int __switchdev_handle_port_attr_set(struct net_device *dev,
493 struct switchdev_notifier_port_attr_info *port_attr_info,
494 bool (*check_cb)(const struct net_device *dev),
495 int (*set_cb)(struct net_device *dev,
496 const struct switchdev_attr *attr,
497 struct netlink_ext_ack *extack))
499 struct netlink_ext_ack *extack;
500 struct net_device *lower_dev;
501 struct list_head *iter;
502 int err = -EOPNOTSUPP;
504 extack = switchdev_notifier_info_to_extack(&port_attr_info->info);
507 err = set_cb(dev, port_attr_info->attr, extack);
508 if (err != -EOPNOTSUPP)
509 port_attr_info->handled = true;
513 /* Switch ports might be stacked under e.g. a LAG. Ignore the
514 * unsupported devices, another driver might be able to handle them. But
515 * propagate to the callers any hard errors.
517 * If the driver does its own bookkeeping of stacked ports, it's not
518 * necessary to go through this helper.
520 netdev_for_each_lower_dev(dev, lower_dev, iter) {
521 if (netif_is_bridge_master(lower_dev))
524 err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info,
526 if (err && err != -EOPNOTSUPP)
533 int switchdev_handle_port_attr_set(struct net_device *dev,
534 struct switchdev_notifier_port_attr_info *port_attr_info,
535 bool (*check_cb)(const struct net_device *dev),
536 int (*set_cb)(struct net_device *dev,
537 const struct switchdev_attr *attr,
538 struct netlink_ext_ack *extack))
542 err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb,
544 if (err == -EOPNOTSUPP)
548 EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);