blob: b62565278faccfb7f96228a19304a9ec6c161655 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirko007f7902014-11-28 14:34:17 +01002/*
3 * net/switchdev/switchdev.c - Switch device API
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02004 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
Scott Feldmanf8f21472015-03-09 13:59:09 -07005 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
Jiri Pirko007f7902014-11-28 14:34:17 +01006 */
7
8#include <linux/kernel.h>
9#include <linux/types.h>
10#include <linux/init.h>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010011#include <linux/mutex.h>
12#include <linux/notifier.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010013#include <linux/netdevice.h>
Jiri Pirko850d0cb2015-10-14 19:40:51 +020014#include <linux/etherdevice.h>
Scott Feldman47f83282015-05-10 09:47:56 -070015#include <linux/if_bridge.h>
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020016#include <linux/list.h>
Jiri Pirko793f4012015-10-14 19:40:48 +020017#include <linux/workqueue.h>
Nikolay Aleksandrov87aaf2c2015-10-12 14:31:01 +020018#include <linux/if_vlan.h>
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +010019#include <linux/rtnetlink.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010020#include <net/switchdev.h>
21
Jiri Pirko793f4012015-10-14 19:40:48 +020022static LIST_HEAD(deferred);
23static DEFINE_SPINLOCK(deferred_lock);
24
25typedef void switchdev_deferred_func_t(struct net_device *dev,
26 const void *data);
27
28struct switchdev_deferred_item {
29 struct list_head list;
30 struct net_device *dev;
Eric Dumazet4fc003f2021-12-06 17:30:31 -080031 netdevice_tracker dev_tracker;
Jiri Pirko793f4012015-10-14 19:40:48 +020032 switchdev_deferred_func_t *func;
Gustavo A. R. Silvafbfc8502020-02-17 14:02:36 -060033 unsigned long data[];
Jiri Pirko793f4012015-10-14 19:40:48 +020034};
35
36static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
37{
38 struct switchdev_deferred_item *dfitem;
39
40 spin_lock_bh(&deferred_lock);
41 if (list_empty(&deferred)) {
42 dfitem = NULL;
43 goto unlock;
44 }
45 dfitem = list_first_entry(&deferred,
46 struct switchdev_deferred_item, list);
47 list_del(&dfitem->list);
48unlock:
49 spin_unlock_bh(&deferred_lock);
50 return dfitem;
51}
52
53/**
54 * switchdev_deferred_process - Process ops in deferred queue
55 *
56 * Called to flush the ops currently queued in deferred ops queue.
57 * rtnl_lock must be held.
58 */
59void switchdev_deferred_process(void)
60{
61 struct switchdev_deferred_item *dfitem;
62
63 ASSERT_RTNL();
64
65 while ((dfitem = switchdev_deferred_dequeue())) {
66 dfitem->func(dfitem->dev, dfitem->data);
Eric Dumazet4fc003f2021-12-06 17:30:31 -080067 dev_put_track(dfitem->dev, &dfitem->dev_tracker);
Jiri Pirko793f4012015-10-14 19:40:48 +020068 kfree(dfitem);
69 }
70}
71EXPORT_SYMBOL_GPL(switchdev_deferred_process);
72
73static void switchdev_deferred_process_work(struct work_struct *work)
74{
75 rtnl_lock();
76 switchdev_deferred_process();
77 rtnl_unlock();
78}
79
80static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
81
82static int switchdev_deferred_enqueue(struct net_device *dev,
83 const void *data, size_t data_len,
84 switchdev_deferred_func_t *func)
85{
86 struct switchdev_deferred_item *dfitem;
87
88 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
89 if (!dfitem)
90 return -ENOMEM;
91 dfitem->dev = dev;
92 dfitem->func = func;
93 memcpy(dfitem->data, data, data_len);
Eric Dumazet4fc003f2021-12-06 17:30:31 -080094 dev_hold_track(dev, &dfitem->dev_tracker, GFP_ATOMIC);
Jiri Pirko793f4012015-10-14 19:40:48 +020095 spin_lock_bh(&deferred_lock);
96 list_add_tail(&dfitem->list, &deferred);
97 spin_unlock_bh(&deferred_lock);
98 schedule_work(&deferred_process_work);
99 return 0;
100}
101
Florian Fainellid45224d2019-02-27 11:44:31 -0800102static int switchdev_port_attr_notify(enum switchdev_notifier_type nt,
103 struct net_device *dev,
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200104 const struct switchdev_attr *attr,
105 struct netlink_ext_ack *extack)
Scott Feldman30943332015-05-10 09:47:48 -0700106{
Florian Fainellid45224d2019-02-27 11:44:31 -0800107 int err;
108 int rc;
Scott Feldman30943332015-05-10 09:47:48 -0700109
Florian Fainellid45224d2019-02-27 11:44:31 -0800110 struct switchdev_notifier_port_attr_info attr_info = {
111 .attr = attr,
Florian Fainellid45224d2019-02-27 11:44:31 -0800112 .handled = false,
113 };
114
115 rc = call_switchdev_blocking_notifiers(nt, dev,
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200116 &attr_info.info, extack);
Florian Fainellid45224d2019-02-27 11:44:31 -0800117 err = notifier_to_errno(rc);
118 if (err) {
119 WARN_ON(!attr_info.handled);
120 return err;
Jiri Pirko0c63d802015-11-03 17:40:53 +0100121 }
Scott Feldman30943332015-05-10 09:47:48 -0700122
Florian Fainellid45224d2019-02-27 11:44:31 -0800123 if (!attr_info.handled)
124 return -EOPNOTSUPP;
Scott Feldman30943332015-05-10 09:47:48 -0700125
Florian Fainellid45224d2019-02-27 11:44:31 -0800126 return 0;
Scott Feldman30943332015-05-10 09:47:48 -0700127}
128
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200129static int switchdev_port_attr_set_now(struct net_device *dev,
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200130 const struct switchdev_attr *attr,
131 struct netlink_ext_ack *extack)
Scott Feldman30943332015-05-10 09:47:48 -0700132{
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200133 return switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr,
134 extack);
Scott Feldman30943332015-05-10 09:47:48 -0700135}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200136
137static void switchdev_port_attr_set_deferred(struct net_device *dev,
138 const void *data)
139{
140 const struct switchdev_attr *attr = data;
141 int err;
142
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200143 err = switchdev_port_attr_set_now(dev, attr, NULL);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200144 if (err && err != -EOPNOTSUPP)
145 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
146 err, attr->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200147 if (attr->complete)
148 attr->complete(dev, err, attr->complete_priv);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200149}
150
151static int switchdev_port_attr_set_defer(struct net_device *dev,
152 const struct switchdev_attr *attr)
153{
154 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
155 switchdev_port_attr_set_deferred);
156}
157
158/**
159 * switchdev_port_attr_set - Set port attribute
160 *
161 * @dev: port device
162 * @attr: attribute to set
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200163 * @extack: netlink extended ack, for error message propagation
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200164 *
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200165 * rtnl_lock must be held and must not be in atomic section,
166 * in case SWITCHDEV_F_DEFER flag is not set.
167 */
168int switchdev_port_attr_set(struct net_device *dev,
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200169 const struct switchdev_attr *attr,
170 struct netlink_ext_ack *extack)
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200171{
172 if (attr->flags & SWITCHDEV_F_DEFER)
173 return switchdev_port_attr_set_defer(dev, attr);
174 ASSERT_RTNL();
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200175 return switchdev_port_attr_set_now(dev, attr, extack);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200176}
Scott Feldman30943332015-05-10 09:47:48 -0700177EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
178
Scott Feldmane258d912015-10-28 23:17:31 -0700179static size_t switchdev_obj_size(const struct switchdev_obj *obj)
180{
181 switch (obj->id) {
182 case SWITCHDEV_OBJ_ID_PORT_VLAN:
183 return sizeof(struct switchdev_obj_port_vlan);
Elad Raz4d41e1252016-01-10 21:06:22 +0100184 case SWITCHDEV_OBJ_ID_PORT_MDB:
185 return sizeof(struct switchdev_obj_port_mdb);
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100186 case SWITCHDEV_OBJ_ID_HOST_MDB:
187 return sizeof(struct switchdev_obj_port_mdb);
Scott Feldmane258d912015-10-28 23:17:31 -0700188 default:
189 BUG();
190 }
191 return 0;
192}
193
Petr Machatad17d9f52018-11-22 23:32:57 +0000194static int switchdev_port_obj_notify(enum switchdev_notifier_type nt,
195 struct net_device *dev,
196 const struct switchdev_obj *obj,
Petr Machata69b73202018-12-12 17:02:52 +0000197 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700198{
Petr Machatad17d9f52018-11-22 23:32:57 +0000199 int rc;
200 int err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700201
Petr Machatad17d9f52018-11-22 23:32:57 +0000202 struct switchdev_notifier_port_obj_info obj_info = {
203 .obj = obj,
Petr Machatad17d9f52018-11-22 23:32:57 +0000204 .handled = false,
205 };
Scott Feldman491d0f12015-05-10 09:47:52 -0700206
Petr Machata479c86d2018-12-12 17:02:54 +0000207 rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack);
Petr Machatad17d9f52018-11-22 23:32:57 +0000208 err = notifier_to_errno(rc);
209 if (err) {
210 WARN_ON(!obj_info.handled);
211 return err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700212 }
Petr Machatad17d9f52018-11-22 23:32:57 +0000213 if (!obj_info.handled)
214 return -EOPNOTSUPP;
215 return 0;
Scott Feldman491d0f12015-05-10 09:47:52 -0700216}
217
Jiri Pirko4d429c52015-10-14 19:40:52 +0200218static void switchdev_port_obj_add_deferred(struct net_device *dev,
219 const void *data)
220{
221 const struct switchdev_obj *obj = data;
222 int err;
223
Vladimir Olteancf6def52021-01-09 02:01:49 +0200224 ASSERT_RTNL();
225 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
226 dev, obj, NULL);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200227 if (err && err != -EOPNOTSUPP)
228 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
229 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200230 if (obj->complete)
231 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200232}
233
234static int switchdev_port_obj_add_defer(struct net_device *dev,
235 const struct switchdev_obj *obj)
236{
Scott Feldmane258d912015-10-28 23:17:31 -0700237 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200238 switchdev_port_obj_add_deferred);
239}
Scott Feldman491d0f12015-05-10 09:47:52 -0700240
241/**
Jiri Pirko4d429c52015-10-14 19:40:52 +0200242 * switchdev_port_obj_add - Add port object
Scott Feldman491d0f12015-05-10 09:47:52 -0700243 *
244 * @dev: port device
Jiri Pirko4d429c52015-10-14 19:40:52 +0200245 * @obj: object to add
Andrew Lunnc8af73f2020-07-13 01:15:13 +0200246 * @extack: netlink extended ack
Jiri Pirko4d429c52015-10-14 19:40:52 +0200247 *
Jiri Pirko4d429c52015-10-14 19:40:52 +0200248 * rtnl_lock must be held and must not be in atomic section,
249 * in case SWITCHDEV_F_DEFER flag is not set.
Scott Feldman491d0f12015-05-10 09:47:52 -0700250 */
Jiri Pirko4d429c52015-10-14 19:40:52 +0200251int switchdev_port_obj_add(struct net_device *dev,
Petr Machata69b73202018-12-12 17:02:52 +0000252 const struct switchdev_obj *obj,
253 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700254{
Jiri Pirko4d429c52015-10-14 19:40:52 +0200255 if (obj->flags & SWITCHDEV_F_DEFER)
256 return switchdev_port_obj_add_defer(dev, obj);
257 ASSERT_RTNL();
Vladimir Olteancf6def52021-01-09 02:01:49 +0200258 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
259 dev, obj, extack);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200260}
261EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
262
263static int switchdev_port_obj_del_now(struct net_device *dev,
264 const struct switchdev_obj *obj)
265{
Petr Machatad17d9f52018-11-22 23:32:57 +0000266 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200267 dev, obj, NULL);
Scott Feldman491d0f12015-05-10 09:47:52 -0700268}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200269
270static void switchdev_port_obj_del_deferred(struct net_device *dev,
271 const void *data)
272{
273 const struct switchdev_obj *obj = data;
274 int err;
275
276 err = switchdev_port_obj_del_now(dev, obj);
277 if (err && err != -EOPNOTSUPP)
278 netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
279 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200280 if (obj->complete)
281 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200282}
283
284static int switchdev_port_obj_del_defer(struct net_device *dev,
285 const struct switchdev_obj *obj)
286{
Scott Feldmane258d912015-10-28 23:17:31 -0700287 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200288 switchdev_port_obj_del_deferred);
289}
290
291/**
292 * switchdev_port_obj_del - Delete port object
293 *
294 * @dev: port device
Jiri Pirko4d429c52015-10-14 19:40:52 +0200295 * @obj: object to delete
296 *
297 * rtnl_lock must be held and must not be in atomic section,
298 * in case SWITCHDEV_F_DEFER flag is not set.
299 */
300int switchdev_port_obj_del(struct net_device *dev,
301 const struct switchdev_obj *obj)
302{
303 if (obj->flags & SWITCHDEV_F_DEFER)
304 return switchdev_port_obj_del_defer(dev, obj);
305 ASSERT_RTNL();
306 return switchdev_port_obj_del_now(dev, obj);
307}
Scott Feldman491d0f12015-05-10 09:47:52 -0700308EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
309
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200310static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
Petr Machataa93e3b12018-11-22 23:28:25 +0000311static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100312
313/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700314 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100315 * @nb: notifier_block
316 *
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200317 * Register switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100318 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700319int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100320{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200321 return atomic_notifier_chain_register(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100322}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700323EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100324
325/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700326 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100327 * @nb: notifier_block
328 *
329 * Unregister switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100330 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700331int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100332{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200333 return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100334}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700335EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100336
337/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700338 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100339 * @val: value passed unmodified to notifier function
340 * @dev: port device
341 * @info: notifier information data
Tian Taoea6754a2020-09-22 21:32:19 +0800342 * @extack: netlink extended ack
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200343 * Call all network notifier blocks.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100344 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700345int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
Petr Machata66859872019-01-16 23:06:56 +0000346 struct switchdev_notifier_info *info,
347 struct netlink_ext_ack *extack)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100348{
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100349 info->dev = dev;
Petr Machata66859872019-01-16 23:06:56 +0000350 info->extack = extack;
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200351 return atomic_notifier_call_chain(&switchdev_notif_chain, val, info);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100352}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700353EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800354
Petr Machataa93e3b12018-11-22 23:28:25 +0000355int register_switchdev_blocking_notifier(struct notifier_block *nb)
356{
357 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
358
359 return blocking_notifier_chain_register(chain, nb);
360}
361EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
362
363int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
364{
365 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
366
367 return blocking_notifier_chain_unregister(chain, nb);
368}
369EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
370
371int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
Petr Machata479c86d2018-12-12 17:02:54 +0000372 struct switchdev_notifier_info *info,
373 struct netlink_ext_ack *extack)
Petr Machataa93e3b12018-11-22 23:28:25 +0000374{
375 info->dev = dev;
Petr Machata479c86d2018-12-12 17:02:54 +0000376 info->extack = extack;
Petr Machataa93e3b12018-11-22 23:28:25 +0000377 return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
378 val, info);
379}
380EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
381
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300382struct switchdev_nested_priv {
383 bool (*check_cb)(const struct net_device *dev);
384 bool (*foreign_dev_check_cb)(const struct net_device *dev,
385 const struct net_device *foreign_dev);
386 const struct net_device *dev;
387 struct net_device *lower_dev;
388};
389
390static int switchdev_lower_dev_walk(struct net_device *lower_dev,
391 struct netdev_nested_priv *priv)
392{
393 struct switchdev_nested_priv *switchdev_priv = priv->data;
394 bool (*foreign_dev_check_cb)(const struct net_device *dev,
395 const struct net_device *foreign_dev);
396 bool (*check_cb)(const struct net_device *dev);
397 const struct net_device *dev;
398
399 check_cb = switchdev_priv->check_cb;
400 foreign_dev_check_cb = switchdev_priv->foreign_dev_check_cb;
401 dev = switchdev_priv->dev;
402
403 if (check_cb(lower_dev) && !foreign_dev_check_cb(lower_dev, dev)) {
404 switchdev_priv->lower_dev = lower_dev;
405 return 1;
406 }
407
408 return 0;
409}
410
411static struct net_device *
412switchdev_lower_dev_find(struct net_device *dev,
413 bool (*check_cb)(const struct net_device *dev),
414 bool (*foreign_dev_check_cb)(const struct net_device *dev,
415 const struct net_device *foreign_dev))
416{
417 struct switchdev_nested_priv switchdev_priv = {
418 .check_cb = check_cb,
419 .foreign_dev_check_cb = foreign_dev_check_cb,
420 .dev = dev,
421 .lower_dev = NULL,
422 };
423 struct netdev_nested_priv priv = {
424 .data = &switchdev_priv,
425 };
426
427 netdev_walk_all_lower_dev_rcu(dev, switchdev_lower_dev_walk, &priv);
428
429 return switchdev_priv.lower_dev;
430}
431
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300432static int __switchdev_handle_fdb_event_to_device(struct net_device *dev,
433 struct net_device *orig_dev, unsigned long event,
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300434 const struct switchdev_notifier_fdb_info *fdb_info,
435 bool (*check_cb)(const struct net_device *dev),
436 bool (*foreign_dev_check_cb)(const struct net_device *dev,
437 const struct net_device *foreign_dev),
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300438 int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
439 unsigned long event, const void *ctx,
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300440 const struct switchdev_notifier_fdb_info *fdb_info),
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300441 int (*lag_mod_cb)(struct net_device *dev, struct net_device *orig_dev,
442 unsigned long event, const void *ctx,
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300443 const struct switchdev_notifier_fdb_info *fdb_info))
444{
445 const struct switchdev_notifier_info *info = &fdb_info->info;
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300446 struct net_device *br, *lower_dev;
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300447 struct list_head *iter;
448 int err = -EOPNOTSUPP;
449
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300450 if (check_cb(dev))
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300451 return mod_cb(dev, orig_dev, event, info->ctx, fdb_info);
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300452
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300453 if (netif_is_lag_master(dev)) {
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300454 if (!switchdev_lower_dev_find(dev, check_cb, foreign_dev_check_cb))
455 goto maybe_bridged_with_us;
456
457 /* This is a LAG interface that we offload */
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300458 if (!lag_mod_cb)
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300459 return -EOPNOTSUPP;
460
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300461 return lag_mod_cb(dev, orig_dev, event, info->ctx, fdb_info);
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300462 }
463
464 /* Recurse through lower interfaces in case the FDB entry is pointing
465 * towards a bridge device.
466 */
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300467 if (netif_is_bridge_master(dev)) {
468 if (!switchdev_lower_dev_find(dev, check_cb, foreign_dev_check_cb))
469 return 0;
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300470
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300471 /* This is a bridge interface that we offload */
472 netdev_for_each_lower_dev(dev, lower_dev, iter) {
473 /* Do not propagate FDB entries across bridges */
474 if (netif_is_bridge_master(lower_dev))
475 continue;
476
477 /* Bridge ports might be either us, or LAG interfaces
478 * that we offload.
479 */
480 if (!check_cb(lower_dev) &&
481 !switchdev_lower_dev_find(lower_dev, check_cb,
482 foreign_dev_check_cb))
483 continue;
484
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300485 err = __switchdev_handle_fdb_event_to_device(lower_dev, orig_dev,
486 event, fdb_info, check_cb,
487 foreign_dev_check_cb,
488 mod_cb, lag_mod_cb);
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300489 if (err && err != -EOPNOTSUPP)
490 return err;
491 }
492
493 return 0;
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300494 }
495
Vladimir Oltean2b0a5682021-07-22 02:05:55 +0300496maybe_bridged_with_us:
497 /* Event is neither on a bridge nor a LAG. Check whether it is on an
498 * interface that is in a bridge with us.
499 */
500 br = netdev_master_upper_dev_get_rcu(dev);
501 if (!br || !netif_is_bridge_master(br))
502 return 0;
503
504 if (!switchdev_lower_dev_find(br, check_cb, foreign_dev_check_cb))
505 return 0;
506
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300507 return __switchdev_handle_fdb_event_to_device(br, orig_dev, event, fdb_info,
508 check_cb, foreign_dev_check_cb,
509 mod_cb, lag_mod_cb);
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300510}
511
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300512int switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300513 const struct switchdev_notifier_fdb_info *fdb_info,
514 bool (*check_cb)(const struct net_device *dev),
515 bool (*foreign_dev_check_cb)(const struct net_device *dev,
516 const struct net_device *foreign_dev),
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300517 int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
518 unsigned long event, const void *ctx,
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300519 const struct switchdev_notifier_fdb_info *fdb_info),
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300520 int (*lag_mod_cb)(struct net_device *dev, struct net_device *orig_dev,
521 unsigned long event, const void *ctx,
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300522 const struct switchdev_notifier_fdb_info *fdb_info))
523{
524 int err;
525
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300526 err = __switchdev_handle_fdb_event_to_device(dev, dev, event, fdb_info,
527 check_cb, foreign_dev_check_cb,
528 mod_cb, lag_mod_cb);
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300529 if (err == -EOPNOTSUPP)
530 err = 0;
531
532 return err;
533}
Vladimir Oltean716a30a2021-10-26 17:27:43 +0300534EXPORT_SYMBOL_GPL(switchdev_handle_fdb_event_to_device);
Vladimir Oltean8ca07172021-07-19 16:51:39 +0300535
Petr Machataf30f0602018-11-22 23:29:44 +0000536static int __switchdev_handle_port_obj_add(struct net_device *dev,
537 struct switchdev_notifier_port_obj_info *port_obj_info,
538 bool (*check_cb)(const struct net_device *dev),
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300539 int (*add_cb)(struct net_device *dev, const void *ctx,
Petr Machataf30f0602018-11-22 23:29:44 +0000540 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000541 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000542{
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300543 struct switchdev_notifier_info *info = &port_obj_info->info;
Petr Machata69213512018-12-12 17:02:56 +0000544 struct netlink_ext_ack *extack;
Petr Machataf30f0602018-11-22 23:29:44 +0000545 struct net_device *lower_dev;
546 struct list_head *iter;
547 int err = -EOPNOTSUPP;
548
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300549 extack = switchdev_notifier_info_to_extack(info);
Petr Machata69213512018-12-12 17:02:56 +0000550
Petr Machataf30f0602018-11-22 23:29:44 +0000551 if (check_cb(dev)) {
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300552 err = add_cb(dev, info->ctx, port_obj_info->obj, extack);
Rasmus Villemoes20776b42021-01-25 13:41:16 +0100553 if (err != -EOPNOTSUPP)
554 port_obj_info->handled = true;
555 return err;
Petr Machataf30f0602018-11-22 23:29:44 +0000556 }
557
558 /* Switch ports might be stacked under e.g. a LAG. Ignore the
559 * unsupported devices, another driver might be able to handle them. But
560 * propagate to the callers any hard errors.
561 *
562 * If the driver does its own bookkeeping of stacked ports, it's not
563 * necessary to go through this helper.
564 */
565 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Russell King07c6f982020-02-26 17:14:21 +0000566 if (netif_is_bridge_master(lower_dev))
567 continue;
568
Petr Machataf30f0602018-11-22 23:29:44 +0000569 err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info,
570 check_cb, add_cb);
571 if (err && err != -EOPNOTSUPP)
572 return err;
573 }
574
575 return err;
576}
577
578int switchdev_handle_port_obj_add(struct net_device *dev,
579 struct switchdev_notifier_port_obj_info *port_obj_info,
580 bool (*check_cb)(const struct net_device *dev),
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300581 int (*add_cb)(struct net_device *dev, const void *ctx,
Petr Machataf30f0602018-11-22 23:29:44 +0000582 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000583 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000584{
585 int err;
586
587 err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
588 add_cb);
589 if (err == -EOPNOTSUPP)
590 err = 0;
591 return err;
592}
593EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
594
595static int __switchdev_handle_port_obj_del(struct net_device *dev,
596 struct switchdev_notifier_port_obj_info *port_obj_info,
597 bool (*check_cb)(const struct net_device *dev),
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300598 int (*del_cb)(struct net_device *dev, const void *ctx,
Petr Machataf30f0602018-11-22 23:29:44 +0000599 const struct switchdev_obj *obj))
600{
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300601 struct switchdev_notifier_info *info = &port_obj_info->info;
Petr Machataf30f0602018-11-22 23:29:44 +0000602 struct net_device *lower_dev;
603 struct list_head *iter;
604 int err = -EOPNOTSUPP;
605
606 if (check_cb(dev)) {
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300607 err = del_cb(dev, info->ctx, port_obj_info->obj);
Rasmus Villemoes20776b42021-01-25 13:41:16 +0100608 if (err != -EOPNOTSUPP)
609 port_obj_info->handled = true;
610 return err;
Petr Machataf30f0602018-11-22 23:29:44 +0000611 }
612
613 /* Switch ports might be stacked under e.g. a LAG. Ignore the
614 * unsupported devices, another driver might be able to handle them. But
615 * propagate to the callers any hard errors.
616 *
617 * If the driver does its own bookkeeping of stacked ports, it's not
618 * necessary to go through this helper.
619 */
620 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Russell King07c6f982020-02-26 17:14:21 +0000621 if (netif_is_bridge_master(lower_dev))
622 continue;
623
Petr Machataf30f0602018-11-22 23:29:44 +0000624 err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
625 check_cb, del_cb);
626 if (err && err != -EOPNOTSUPP)
627 return err;
628 }
629
630 return err;
631}
632
633int switchdev_handle_port_obj_del(struct net_device *dev,
634 struct switchdev_notifier_port_obj_info *port_obj_info,
635 bool (*check_cb)(const struct net_device *dev),
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300636 int (*del_cb)(struct net_device *dev, const void *ctx,
Petr Machataf30f0602018-11-22 23:29:44 +0000637 const struct switchdev_obj *obj))
638{
639 int err;
640
641 err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
642 del_cb);
643 if (err == -EOPNOTSUPP)
644 err = 0;
645 return err;
646}
647EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800648
649static int __switchdev_handle_port_attr_set(struct net_device *dev,
650 struct switchdev_notifier_port_attr_info *port_attr_info,
651 bool (*check_cb)(const struct net_device *dev),
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300652 int (*set_cb)(struct net_device *dev, const void *ctx,
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200653 const struct switchdev_attr *attr,
654 struct netlink_ext_ack *extack))
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800655{
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300656 struct switchdev_notifier_info *info = &port_attr_info->info;
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200657 struct netlink_ext_ack *extack;
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800658 struct net_device *lower_dev;
659 struct list_head *iter;
660 int err = -EOPNOTSUPP;
661
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300662 extack = switchdev_notifier_info_to_extack(info);
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200663
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800664 if (check_cb(dev)) {
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300665 err = set_cb(dev, info->ctx, port_attr_info->attr, extack);
Rasmus Villemoes20776b42021-01-25 13:41:16 +0100666 if (err != -EOPNOTSUPP)
667 port_attr_info->handled = true;
668 return err;
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800669 }
670
671 /* Switch ports might be stacked under e.g. a LAG. Ignore the
672 * unsupported devices, another driver might be able to handle them. But
673 * propagate to the callers any hard errors.
674 *
675 * If the driver does its own bookkeeping of stacked ports, it's not
676 * necessary to go through this helper.
677 */
678 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Russell King07c6f982020-02-26 17:14:21 +0000679 if (netif_is_bridge_master(lower_dev))
680 continue;
681
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800682 err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info,
683 check_cb, set_cb);
684 if (err && err != -EOPNOTSUPP)
685 return err;
686 }
687
688 return err;
689}
690
691int switchdev_handle_port_attr_set(struct net_device *dev,
692 struct switchdev_notifier_port_attr_info *port_attr_info,
693 bool (*check_cb)(const struct net_device *dev),
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300694 int (*set_cb)(struct net_device *dev, const void *ctx,
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200695 const struct switchdev_attr *attr,
696 struct netlink_ext_ack *extack))
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800697{
698 int err;
699
700 err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb,
701 set_cb);
702 if (err == -EOPNOTSUPP)
703 err = 0;
704 return err;
705}
706EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);
Vladimir Oltean957e2232021-08-03 23:34:08 +0300707
708int switchdev_bridge_port_offload(struct net_device *brport_dev,
709 struct net_device *dev, const void *ctx,
710 struct notifier_block *atomic_nb,
711 struct notifier_block *blocking_nb,
712 bool tx_fwd_offload,
713 struct netlink_ext_ack *extack)
714{
715 struct switchdev_notifier_brport_info brport_info = {
716 .brport = {
717 .dev = dev,
718 .ctx = ctx,
719 .atomic_nb = atomic_nb,
720 .blocking_nb = blocking_nb,
721 .tx_fwd_offload = tx_fwd_offload,
722 },
723 };
724 int err;
725
726 ASSERT_RTNL();
727
728 err = call_switchdev_blocking_notifiers(SWITCHDEV_BRPORT_OFFLOADED,
729 brport_dev, &brport_info.info,
730 extack);
731 return notifier_to_errno(err);
732}
733EXPORT_SYMBOL_GPL(switchdev_bridge_port_offload);
734
735void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
736 const void *ctx,
737 struct notifier_block *atomic_nb,
738 struct notifier_block *blocking_nb)
739{
740 struct switchdev_notifier_brport_info brport_info = {
741 .brport = {
742 .ctx = ctx,
743 .atomic_nb = atomic_nb,
744 .blocking_nb = blocking_nb,
745 },
746 };
747
748 ASSERT_RTNL();
749
750 call_switchdev_blocking_notifiers(SWITCHDEV_BRPORT_UNOFFLOADED,
751 brport_dev, &brport_info.info,
752 NULL);
753}
754EXPORT_SYMBOL_GPL(switchdev_bridge_port_unoffload);