blob: 90ba4a1f0a6db9c38f073fb0dfcc2a2ff1c7c135 [file] [log] [blame]
Jiri Pirko007f7902014-11-28 14:34:17 +01001/*
2 * net/switchdev/switchdev.c - Switch device API
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02003 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
Scott Feldmanf8f21472015-03-09 13:59:09 -07004 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
Jiri Pirko007f7902014-11-28 14:34:17 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/types.h>
14#include <linux/init.h>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010015#include <linux/mutex.h>
16#include <linux/notifier.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010017#include <linux/netdevice.h>
Jiri Pirko850d0cb2015-10-14 19:40:51 +020018#include <linux/etherdevice.h>
Scott Feldman47f83282015-05-10 09:47:56 -070019#include <linux/if_bridge.h>
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020020#include <linux/list.h>
Jiri Pirko793f4012015-10-14 19:40:48 +020021#include <linux/workqueue.h>
Nikolay Aleksandrov87aaf2c2015-10-12 14:31:01 +020022#include <linux/if_vlan.h>
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +010023#include <linux/rtnetlink.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010024#include <net/switchdev.h>
25
Jiri Pirko793f4012015-10-14 19:40:48 +020026static LIST_HEAD(deferred);
27static DEFINE_SPINLOCK(deferred_lock);
28
29typedef void switchdev_deferred_func_t(struct net_device *dev,
30 const void *data);
31
32struct switchdev_deferred_item {
33 struct list_head list;
34 struct net_device *dev;
35 switchdev_deferred_func_t *func;
36 unsigned long data[0];
37};
38
39static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
40{
41 struct switchdev_deferred_item *dfitem;
42
43 spin_lock_bh(&deferred_lock);
44 if (list_empty(&deferred)) {
45 dfitem = NULL;
46 goto unlock;
47 }
48 dfitem = list_first_entry(&deferred,
49 struct switchdev_deferred_item, list);
50 list_del(&dfitem->list);
51unlock:
52 spin_unlock_bh(&deferred_lock);
53 return dfitem;
54}
55
56/**
57 * switchdev_deferred_process - Process ops in deferred queue
58 *
59 * Called to flush the ops currently queued in deferred ops queue.
60 * rtnl_lock must be held.
61 */
62void switchdev_deferred_process(void)
63{
64 struct switchdev_deferred_item *dfitem;
65
66 ASSERT_RTNL();
67
68 while ((dfitem = switchdev_deferred_dequeue())) {
69 dfitem->func(dfitem->dev, dfitem->data);
70 dev_put(dfitem->dev);
71 kfree(dfitem);
72 }
73}
74EXPORT_SYMBOL_GPL(switchdev_deferred_process);
75
76static void switchdev_deferred_process_work(struct work_struct *work)
77{
78 rtnl_lock();
79 switchdev_deferred_process();
80 rtnl_unlock();
81}
82
83static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
84
85static int switchdev_deferred_enqueue(struct net_device *dev,
86 const void *data, size_t data_len,
87 switchdev_deferred_func_t *func)
88{
89 struct switchdev_deferred_item *dfitem;
90
91 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
92 if (!dfitem)
93 return -ENOMEM;
94 dfitem->dev = dev;
95 dfitem->func = func;
96 memcpy(dfitem->data, data, data_len);
97 dev_hold(dev);
98 spin_lock_bh(&deferred_lock);
99 list_add_tail(&dfitem->list, &deferred);
100 spin_unlock_bh(&deferred_lock);
101 schedule_work(&deferred_process_work);
102 return 0;
103}
104
Florian Fainellid45224d2019-02-27 11:44:31 -0800105static int switchdev_port_attr_notify(enum switchdev_notifier_type nt,
106 struct net_device *dev,
107 const struct switchdev_attr *attr,
108 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700109{
Florian Fainellid45224d2019-02-27 11:44:31 -0800110 int err;
111 int rc;
Scott Feldman30943332015-05-10 09:47:48 -0700112
Florian Fainellid45224d2019-02-27 11:44:31 -0800113 struct switchdev_notifier_port_attr_info attr_info = {
114 .attr = attr,
115 .trans = trans,
116 .handled = false,
117 };
118
119 rc = call_switchdev_blocking_notifiers(nt, dev,
120 &attr_info.info, NULL);
121 err = notifier_to_errno(rc);
122 if (err) {
123 WARN_ON(!attr_info.handled);
124 return err;
Jiri Pirko0c63d802015-11-03 17:40:53 +0100125 }
Scott Feldman30943332015-05-10 09:47:48 -0700126
Florian Fainellid45224d2019-02-27 11:44:31 -0800127 if (!attr_info.handled)
128 return -EOPNOTSUPP;
Scott Feldman30943332015-05-10 09:47:48 -0700129
Florian Fainellid45224d2019-02-27 11:44:31 -0800130 return 0;
Scott Feldman30943332015-05-10 09:47:48 -0700131}
132
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200133static int switchdev_port_attr_set_now(struct net_device *dev,
134 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700135{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200136 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700137 int err;
138
Scott Feldman30943332015-05-10 09:47:48 -0700139 /* Phase I: prepare for attr set. Driver/device should fail
140 * here if there are going to be issues in the commit phase,
141 * such as lack of resources or support. The driver/device
142 * should reserve resources needed for the commit phase here,
143 * but should not commit the attr.
144 */
145
Jiri Pirkof623ab72015-09-24 10:02:49 +0200146 trans.ph_prepare = true;
Florian Fainellid45224d2019-02-27 11:44:31 -0800147 err = switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr,
148 &trans);
Florian Fainelli91cf8ec2019-02-27 16:29:16 -0800149 if (err)
Scott Feldman30943332015-05-10 09:47:48 -0700150 return err;
Scott Feldman30943332015-05-10 09:47:48 -0700151
152 /* Phase II: commit attr set. This cannot fail as a fault
153 * of driver/device. If it does, it's a bug in the driver/device
154 * because the driver said everythings was OK in phase I.
155 */
156
Jiri Pirkof623ab72015-09-24 10:02:49 +0200157 trans.ph_prepare = false;
Florian Fainellid45224d2019-02-27 11:44:31 -0800158 err = switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr,
159 &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700160 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
161 dev->name, attr->id);
Scott Feldman30943332015-05-10 09:47:48 -0700162
163 return err;
164}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200165
166static void switchdev_port_attr_set_deferred(struct net_device *dev,
167 const void *data)
168{
169 const struct switchdev_attr *attr = data;
170 int err;
171
172 err = switchdev_port_attr_set_now(dev, attr);
173 if (err && err != -EOPNOTSUPP)
174 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
175 err, attr->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200176 if (attr->complete)
177 attr->complete(dev, err, attr->complete_priv);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200178}
179
180static int switchdev_port_attr_set_defer(struct net_device *dev,
181 const struct switchdev_attr *attr)
182{
183 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
184 switchdev_port_attr_set_deferred);
185}
186
187/**
188 * switchdev_port_attr_set - Set port attribute
189 *
190 * @dev: port device
191 * @attr: attribute to set
192 *
193 * Use a 2-phase prepare-commit transaction model to ensure
194 * system is not left in a partially updated state due to
195 * failure from driver/device.
196 *
197 * rtnl_lock must be held and must not be in atomic section,
198 * in case SWITCHDEV_F_DEFER flag is not set.
199 */
200int switchdev_port_attr_set(struct net_device *dev,
201 const struct switchdev_attr *attr)
202{
203 if (attr->flags & SWITCHDEV_F_DEFER)
204 return switchdev_port_attr_set_defer(dev, attr);
205 ASSERT_RTNL();
206 return switchdev_port_attr_set_now(dev, attr);
207}
Scott Feldman30943332015-05-10 09:47:48 -0700208EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
209
Scott Feldmane258d912015-10-28 23:17:31 -0700210static size_t switchdev_obj_size(const struct switchdev_obj *obj)
211{
212 switch (obj->id) {
213 case SWITCHDEV_OBJ_ID_PORT_VLAN:
214 return sizeof(struct switchdev_obj_port_vlan);
Elad Raz4d41e1252016-01-10 21:06:22 +0100215 case SWITCHDEV_OBJ_ID_PORT_MDB:
216 return sizeof(struct switchdev_obj_port_mdb);
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100217 case SWITCHDEV_OBJ_ID_HOST_MDB:
218 return sizeof(struct switchdev_obj_port_mdb);
Scott Feldmane258d912015-10-28 23:17:31 -0700219 default:
220 BUG();
221 }
222 return 0;
223}
224
Petr Machatad17d9f52018-11-22 23:32:57 +0000225static int switchdev_port_obj_notify(enum switchdev_notifier_type nt,
226 struct net_device *dev,
227 const struct switchdev_obj *obj,
Petr Machata69b73202018-12-12 17:02:52 +0000228 struct switchdev_trans *trans,
229 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700230{
Petr Machatad17d9f52018-11-22 23:32:57 +0000231 int rc;
232 int err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700233
Petr Machatad17d9f52018-11-22 23:32:57 +0000234 struct switchdev_notifier_port_obj_info obj_info = {
235 .obj = obj,
236 .trans = trans,
237 .handled = false,
238 };
Scott Feldman491d0f12015-05-10 09:47:52 -0700239
Petr Machata479c86d2018-12-12 17:02:54 +0000240 rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack);
Petr Machatad17d9f52018-11-22 23:32:57 +0000241 err = notifier_to_errno(rc);
242 if (err) {
243 WARN_ON(!obj_info.handled);
244 return err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700245 }
Petr Machatad17d9f52018-11-22 23:32:57 +0000246 if (!obj_info.handled)
247 return -EOPNOTSUPP;
248 return 0;
Scott Feldman491d0f12015-05-10 09:47:52 -0700249}
250
Jiri Pirko4d429c52015-10-14 19:40:52 +0200251static int switchdev_port_obj_add_now(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 Pirko7ea6eb32015-09-24 10:02:41 +0200255 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700256 int err;
257
258 ASSERT_RTNL();
259
260 /* Phase I: prepare for obj add. Driver/device should fail
261 * here if there are going to be issues in the commit phase,
262 * such as lack of resources or support. The driver/device
263 * should reserve resources needed for the commit phase here,
264 * but should not commit the obj.
265 */
266
Jiri Pirkof623ab72015-09-24 10:02:49 +0200267 trans.ph_prepare = true;
Petr Machatad17d9f52018-11-22 23:32:57 +0000268 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
Petr Machata69b73202018-12-12 17:02:52 +0000269 dev, obj, &trans, extack);
Florian Fainelli91cf8ec2019-02-27 16:29:16 -0800270 if (err)
Scott Feldman491d0f12015-05-10 09:47:52 -0700271 return err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700272
273 /* Phase II: commit obj add. This cannot fail as a fault
274 * of driver/device. If it does, it's a bug in the driver/device
275 * because the driver said everythings was OK in phase I.
276 */
277
Jiri Pirkof623ab72015-09-24 10:02:49 +0200278 trans.ph_prepare = false;
Petr Machatad17d9f52018-11-22 23:32:57 +0000279 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
Petr Machata69b73202018-12-12 17:02:52 +0000280 dev, obj, &trans, extack);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200281 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Scott Feldman491d0f12015-05-10 09:47:52 -0700282
283 return err;
284}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200285
286static void switchdev_port_obj_add_deferred(struct net_device *dev,
287 const void *data)
288{
289 const struct switchdev_obj *obj = data;
290 int err;
291
Petr Machata69b73202018-12-12 17:02:52 +0000292 err = switchdev_port_obj_add_now(dev, obj, NULL);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200293 if (err && err != -EOPNOTSUPP)
294 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
295 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200296 if (obj->complete)
297 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200298}
299
300static int switchdev_port_obj_add_defer(struct net_device *dev,
301 const struct switchdev_obj *obj)
302{
Scott Feldmane258d912015-10-28 23:17:31 -0700303 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200304 switchdev_port_obj_add_deferred);
305}
Scott Feldman491d0f12015-05-10 09:47:52 -0700306
307/**
Jiri Pirko4d429c52015-10-14 19:40:52 +0200308 * switchdev_port_obj_add - Add port object
Scott Feldman491d0f12015-05-10 09:47:52 -0700309 *
310 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400311 * @id: object ID
Jiri Pirko4d429c52015-10-14 19:40:52 +0200312 * @obj: object to add
313 *
314 * Use a 2-phase prepare-commit transaction model to ensure
315 * system is not left in a partially updated state due to
316 * failure from driver/device.
317 *
318 * rtnl_lock must be held and must not be in atomic section,
319 * in case SWITCHDEV_F_DEFER flag is not set.
Scott Feldman491d0f12015-05-10 09:47:52 -0700320 */
Jiri Pirko4d429c52015-10-14 19:40:52 +0200321int switchdev_port_obj_add(struct net_device *dev,
Petr Machata69b73202018-12-12 17:02:52 +0000322 const struct switchdev_obj *obj,
323 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700324{
Jiri Pirko4d429c52015-10-14 19:40:52 +0200325 if (obj->flags & SWITCHDEV_F_DEFER)
326 return switchdev_port_obj_add_defer(dev, obj);
327 ASSERT_RTNL();
Petr Machata69b73202018-12-12 17:02:52 +0000328 return switchdev_port_obj_add_now(dev, obj, extack);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200329}
330EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
331
332static int switchdev_port_obj_del_now(struct net_device *dev,
333 const struct switchdev_obj *obj)
334{
Petr Machatad17d9f52018-11-22 23:32:57 +0000335 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL,
Petr Machata69b73202018-12-12 17:02:52 +0000336 dev, obj, NULL, NULL);
Scott Feldman491d0f12015-05-10 09:47:52 -0700337}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200338
339static void switchdev_port_obj_del_deferred(struct net_device *dev,
340 const void *data)
341{
342 const struct switchdev_obj *obj = data;
343 int err;
344
345 err = switchdev_port_obj_del_now(dev, obj);
346 if (err && err != -EOPNOTSUPP)
347 netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
348 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200349 if (obj->complete)
350 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200351}
352
353static int switchdev_port_obj_del_defer(struct net_device *dev,
354 const struct switchdev_obj *obj)
355{
Scott Feldmane258d912015-10-28 23:17:31 -0700356 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200357 switchdev_port_obj_del_deferred);
358}
359
360/**
361 * switchdev_port_obj_del - Delete port object
362 *
363 * @dev: port device
364 * @id: object ID
365 * @obj: object to delete
366 *
367 * rtnl_lock must be held and must not be in atomic section,
368 * in case SWITCHDEV_F_DEFER flag is not set.
369 */
370int switchdev_port_obj_del(struct net_device *dev,
371 const struct switchdev_obj *obj)
372{
373 if (obj->flags & SWITCHDEV_F_DEFER)
374 return switchdev_port_obj_del_defer(dev, obj);
375 ASSERT_RTNL();
376 return switchdev_port_obj_del_now(dev, obj);
377}
Scott Feldman491d0f12015-05-10 09:47:52 -0700378EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
379
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200380static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
Petr Machataa93e3b12018-11-22 23:28:25 +0000381static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100382
383/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700384 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100385 * @nb: notifier_block
386 *
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200387 * Register switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100388 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700389int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100390{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200391 return atomic_notifier_chain_register(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100392}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700393EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100394
395/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700396 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100397 * @nb: notifier_block
398 *
399 * Unregister switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100400 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700401int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100402{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200403 return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100404}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700405EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100406
407/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700408 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100409 * @val: value passed unmodified to notifier function
410 * @dev: port device
411 * @info: notifier information data
412 *
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200413 * Call all network notifier blocks.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100414 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700415int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
Petr Machata66859872019-01-16 23:06:56 +0000416 struct switchdev_notifier_info *info,
417 struct netlink_ext_ack *extack)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100418{
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100419 info->dev = dev;
Petr Machata66859872019-01-16 23:06:56 +0000420 info->extack = extack;
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200421 return atomic_notifier_call_chain(&switchdev_notif_chain, val, info);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100422}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700423EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800424
Petr Machataa93e3b12018-11-22 23:28:25 +0000425int register_switchdev_blocking_notifier(struct notifier_block *nb)
426{
427 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
428
429 return blocking_notifier_chain_register(chain, nb);
430}
431EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
432
433int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
434{
435 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
436
437 return blocking_notifier_chain_unregister(chain, nb);
438}
439EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
440
441int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
Petr Machata479c86d2018-12-12 17:02:54 +0000442 struct switchdev_notifier_info *info,
443 struct netlink_ext_ack *extack)
Petr Machataa93e3b12018-11-22 23:28:25 +0000444{
445 info->dev = dev;
Petr Machata479c86d2018-12-12 17:02:54 +0000446 info->extack = extack;
Petr Machataa93e3b12018-11-22 23:28:25 +0000447 return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
448 val, info);
449}
450EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
451
Petr Machataf30f0602018-11-22 23:29:44 +0000452static int __switchdev_handle_port_obj_add(struct net_device *dev,
453 struct switchdev_notifier_port_obj_info *port_obj_info,
454 bool (*check_cb)(const struct net_device *dev),
455 int (*add_cb)(struct net_device *dev,
456 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000457 struct switchdev_trans *trans,
458 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000459{
Petr Machata69213512018-12-12 17:02:56 +0000460 struct netlink_ext_ack *extack;
Petr Machataf30f0602018-11-22 23:29:44 +0000461 struct net_device *lower_dev;
462 struct list_head *iter;
463 int err = -EOPNOTSUPP;
464
Petr Machata69213512018-12-12 17:02:56 +0000465 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
466
Petr Machataf30f0602018-11-22 23:29:44 +0000467 if (check_cb(dev)) {
468 /* This flag is only checked if the return value is success. */
469 port_obj_info->handled = true;
Petr Machata69213512018-12-12 17:02:56 +0000470 return add_cb(dev, port_obj_info->obj, port_obj_info->trans,
471 extack);
Petr Machataf30f0602018-11-22 23:29:44 +0000472 }
473
474 /* Switch ports might be stacked under e.g. a LAG. Ignore the
475 * unsupported devices, another driver might be able to handle them. But
476 * propagate to the callers any hard errors.
477 *
478 * If the driver does its own bookkeeping of stacked ports, it's not
479 * necessary to go through this helper.
480 */
481 netdev_for_each_lower_dev(dev, lower_dev, iter) {
482 err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info,
483 check_cb, add_cb);
484 if (err && err != -EOPNOTSUPP)
485 return err;
486 }
487
488 return err;
489}
490
491int switchdev_handle_port_obj_add(struct net_device *dev,
492 struct switchdev_notifier_port_obj_info *port_obj_info,
493 bool (*check_cb)(const struct net_device *dev),
494 int (*add_cb)(struct net_device *dev,
495 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000496 struct switchdev_trans *trans,
497 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000498{
499 int err;
500
501 err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
502 add_cb);
503 if (err == -EOPNOTSUPP)
504 err = 0;
505 return err;
506}
507EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
508
509static int __switchdev_handle_port_obj_del(struct net_device *dev,
510 struct switchdev_notifier_port_obj_info *port_obj_info,
511 bool (*check_cb)(const struct net_device *dev),
512 int (*del_cb)(struct net_device *dev,
513 const struct switchdev_obj *obj))
514{
515 struct net_device *lower_dev;
516 struct list_head *iter;
517 int err = -EOPNOTSUPP;
518
519 if (check_cb(dev)) {
520 /* This flag is only checked if the return value is success. */
521 port_obj_info->handled = true;
522 return del_cb(dev, port_obj_info->obj);
523 }
524
525 /* Switch ports might be stacked under e.g. a LAG. Ignore the
526 * unsupported devices, another driver might be able to handle them. But
527 * propagate to the callers any hard errors.
528 *
529 * If the driver does its own bookkeeping of stacked ports, it's not
530 * necessary to go through this helper.
531 */
532 netdev_for_each_lower_dev(dev, lower_dev, iter) {
533 err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
534 check_cb, del_cb);
535 if (err && err != -EOPNOTSUPP)
536 return err;
537 }
538
539 return err;
540}
541
542int switchdev_handle_port_obj_del(struct net_device *dev,
543 struct switchdev_notifier_port_obj_info *port_obj_info,
544 bool (*check_cb)(const struct net_device *dev),
545 int (*del_cb)(struct net_device *dev,
546 const struct switchdev_obj *obj))
547{
548 int err;
549
550 err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
551 del_cb);
552 if (err == -EOPNOTSUPP)
553 err = 0;
554 return err;
555}
556EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800557
558static int __switchdev_handle_port_attr_set(struct net_device *dev,
559 struct switchdev_notifier_port_attr_info *port_attr_info,
560 bool (*check_cb)(const struct net_device *dev),
561 int (*set_cb)(struct net_device *dev,
562 const struct switchdev_attr *attr,
563 struct switchdev_trans *trans))
564{
565 struct net_device *lower_dev;
566 struct list_head *iter;
567 int err = -EOPNOTSUPP;
568
569 if (check_cb(dev)) {
570 port_attr_info->handled = true;
571 return set_cb(dev, port_attr_info->attr,
572 port_attr_info->trans);
573 }
574
575 /* Switch ports might be stacked under e.g. a LAG. Ignore the
576 * unsupported devices, another driver might be able to handle them. But
577 * propagate to the callers any hard errors.
578 *
579 * If the driver does its own bookkeeping of stacked ports, it's not
580 * necessary to go through this helper.
581 */
582 netdev_for_each_lower_dev(dev, lower_dev, iter) {
583 err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info,
584 check_cb, set_cb);
585 if (err && err != -EOPNOTSUPP)
586 return err;
587 }
588
589 return err;
590}
591
592int switchdev_handle_port_attr_set(struct net_device *dev,
593 struct switchdev_notifier_port_attr_info *port_attr_info,
594 bool (*check_cb)(const struct net_device *dev),
595 int (*set_cb)(struct net_device *dev,
596 const struct switchdev_attr *attr,
597 struct switchdev_trans *trans))
598{
599 int err;
600
601 err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb,
602 set_cb);
603 if (err == -EOPNOTSUPP)
604 err = 0;
605 return err;
606}
607EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);