blob: 3560c19aa7e2e5fe62fb3e789e9d6660835670b7 [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
26/**
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020027 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
28 *
29 * @trans: transaction
30 * @data: pointer to data being queued
31 * @destructor: data destructor
32 * @tritem: transaction item being queued
33 *
34 * Enqeueue data item to transaction queue. tritem is typically placed in
35 * cointainter pointed at by data pointer. Destructor is called on
36 * transaction abort and after successful commit phase in case
37 * the caller did not dequeue the item before.
38 */
39void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
40 void *data, void (*destructor)(void const *),
41 struct switchdev_trans_item *tritem)
42{
43 tritem->data = data;
44 tritem->destructor = destructor;
45 list_add_tail(&tritem->list, &trans->item_list);
46}
47EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
48
49static struct switchdev_trans_item *
50__switchdev_trans_item_dequeue(struct switchdev_trans *trans)
51{
52 struct switchdev_trans_item *tritem;
53
54 if (list_empty(&trans->item_list))
55 return NULL;
56 tritem = list_first_entry(&trans->item_list,
57 struct switchdev_trans_item, list);
58 list_del(&tritem->list);
59 return tritem;
60}
61
62/**
63 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
64 *
65 * @trans: transaction
66 */
67void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
68{
69 struct switchdev_trans_item *tritem;
70
71 tritem = __switchdev_trans_item_dequeue(trans);
72 BUG_ON(!tritem);
73 return tritem->data;
74}
75EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
76
77static void switchdev_trans_init(struct switchdev_trans *trans)
78{
79 INIT_LIST_HEAD(&trans->item_list);
80}
81
82static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
83{
84 struct switchdev_trans_item *tritem;
85
86 while ((tritem = __switchdev_trans_item_dequeue(trans)))
87 tritem->destructor(tritem->data);
88}
89
90static void switchdev_trans_items_warn_destroy(struct net_device *dev,
91 struct switchdev_trans *trans)
92{
93 WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
94 dev->name);
95 switchdev_trans_items_destroy(trans);
96}
97
Jiri Pirko793f4012015-10-14 19:40:48 +020098static LIST_HEAD(deferred);
99static DEFINE_SPINLOCK(deferred_lock);
100
101typedef void switchdev_deferred_func_t(struct net_device *dev,
102 const void *data);
103
104struct switchdev_deferred_item {
105 struct list_head list;
106 struct net_device *dev;
107 switchdev_deferred_func_t *func;
108 unsigned long data[0];
109};
110
111static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
112{
113 struct switchdev_deferred_item *dfitem;
114
115 spin_lock_bh(&deferred_lock);
116 if (list_empty(&deferred)) {
117 dfitem = NULL;
118 goto unlock;
119 }
120 dfitem = list_first_entry(&deferred,
121 struct switchdev_deferred_item, list);
122 list_del(&dfitem->list);
123unlock:
124 spin_unlock_bh(&deferred_lock);
125 return dfitem;
126}
127
128/**
129 * switchdev_deferred_process - Process ops in deferred queue
130 *
131 * Called to flush the ops currently queued in deferred ops queue.
132 * rtnl_lock must be held.
133 */
134void switchdev_deferred_process(void)
135{
136 struct switchdev_deferred_item *dfitem;
137
138 ASSERT_RTNL();
139
140 while ((dfitem = switchdev_deferred_dequeue())) {
141 dfitem->func(dfitem->dev, dfitem->data);
142 dev_put(dfitem->dev);
143 kfree(dfitem);
144 }
145}
146EXPORT_SYMBOL_GPL(switchdev_deferred_process);
147
148static void switchdev_deferred_process_work(struct work_struct *work)
149{
150 rtnl_lock();
151 switchdev_deferred_process();
152 rtnl_unlock();
153}
154
155static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
156
157static int switchdev_deferred_enqueue(struct net_device *dev,
158 const void *data, size_t data_len,
159 switchdev_deferred_func_t *func)
160{
161 struct switchdev_deferred_item *dfitem;
162
163 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
164 if (!dfitem)
165 return -ENOMEM;
166 dfitem->dev = dev;
167 dfitem->func = func;
168 memcpy(dfitem->data, data, data_len);
169 dev_hold(dev);
170 spin_lock_bh(&deferred_lock);
171 list_add_tail(&dfitem->list, &deferred);
172 spin_unlock_bh(&deferred_lock);
173 schedule_work(&deferred_process_work);
174 return 0;
175}
176
Scott Feldman30943332015-05-10 09:47:48 -0700177static int __switchdev_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200178 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200179 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700180{
181 const struct switchdev_ops *ops = dev->switchdev_ops;
182 struct net_device *lower_dev;
183 struct list_head *iter;
184 int err = -EOPNOTSUPP;
185
Jiri Pirko0c63d802015-11-03 17:40:53 +0100186 if (ops && ops->switchdev_port_attr_set) {
187 err = ops->switchdev_port_attr_set(dev, attr, trans);
188 goto done;
189 }
Scott Feldman30943332015-05-10 09:47:48 -0700190
191 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
Scott Feldman464314e2015-10-08 19:23:18 -0700192 goto done;
Scott Feldman30943332015-05-10 09:47:48 -0700193
194 /* Switch device port(s) may be stacked under
195 * bond/team/vlan dev, so recurse down to set attr on
196 * each port.
197 */
198
199 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200200 err = __switchdev_port_attr_set(lower_dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700201 if (err)
202 break;
203 }
204
Scott Feldman464314e2015-10-08 19:23:18 -0700205done:
206 if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
207 err = 0;
208
Scott Feldman30943332015-05-10 09:47:48 -0700209 return err;
210}
211
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200212static int switchdev_port_attr_set_now(struct net_device *dev,
213 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700214{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200215 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700216 int err;
217
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200218 switchdev_trans_init(&trans);
219
Scott Feldman30943332015-05-10 09:47:48 -0700220 /* Phase I: prepare for attr set. Driver/device should fail
221 * here if there are going to be issues in the commit phase,
222 * such as lack of resources or support. The driver/device
223 * should reserve resources needed for the commit phase here,
224 * but should not commit the attr.
225 */
226
Jiri Pirkof623ab72015-09-24 10:02:49 +0200227 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200228 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700229 if (err) {
230 /* Prepare phase failed: abort the transaction. Any
231 * resources reserved in the prepare phase are
232 * released.
233 */
234
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200235 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200236 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700237
238 return err;
239 }
240
241 /* Phase II: commit attr set. This cannot fail as a fault
242 * of driver/device. If it does, it's a bug in the driver/device
243 * because the driver said everythings was OK in phase I.
244 */
245
Jiri Pirkof623ab72015-09-24 10:02:49 +0200246 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200247 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700248 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
249 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200250 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700251
252 return err;
253}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200254
255static void switchdev_port_attr_set_deferred(struct net_device *dev,
256 const void *data)
257{
258 const struct switchdev_attr *attr = data;
259 int err;
260
261 err = switchdev_port_attr_set_now(dev, attr);
262 if (err && err != -EOPNOTSUPP)
263 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
264 err, attr->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200265 if (attr->complete)
266 attr->complete(dev, err, attr->complete_priv);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200267}
268
269static int switchdev_port_attr_set_defer(struct net_device *dev,
270 const struct switchdev_attr *attr)
271{
272 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
273 switchdev_port_attr_set_deferred);
274}
275
276/**
277 * switchdev_port_attr_set - Set port attribute
278 *
279 * @dev: port device
280 * @attr: attribute to set
281 *
282 * Use a 2-phase prepare-commit transaction model to ensure
283 * system is not left in a partially updated state due to
284 * failure from driver/device.
285 *
286 * rtnl_lock must be held and must not be in atomic section,
287 * in case SWITCHDEV_F_DEFER flag is not set.
288 */
289int switchdev_port_attr_set(struct net_device *dev,
290 const struct switchdev_attr *attr)
291{
292 if (attr->flags & SWITCHDEV_F_DEFER)
293 return switchdev_port_attr_set_defer(dev, attr);
294 ASSERT_RTNL();
295 return switchdev_port_attr_set_now(dev, attr);
296}
Scott Feldman30943332015-05-10 09:47:48 -0700297EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
298
Scott Feldmane258d912015-10-28 23:17:31 -0700299static size_t switchdev_obj_size(const struct switchdev_obj *obj)
300{
301 switch (obj->id) {
302 case SWITCHDEV_OBJ_ID_PORT_VLAN:
303 return sizeof(struct switchdev_obj_port_vlan);
Elad Raz4d41e1252016-01-10 21:06:22 +0100304 case SWITCHDEV_OBJ_ID_PORT_MDB:
305 return sizeof(struct switchdev_obj_port_mdb);
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100306 case SWITCHDEV_OBJ_ID_HOST_MDB:
307 return sizeof(struct switchdev_obj_port_mdb);
Scott Feldmane258d912015-10-28 23:17:31 -0700308 default:
309 BUG();
310 }
311 return 0;
312}
313
Petr Machatad17d9f52018-11-22 23:32:57 +0000314static int switchdev_port_obj_notify(enum switchdev_notifier_type nt,
315 struct net_device *dev,
316 const struct switchdev_obj *obj,
Petr Machata69b73202018-12-12 17:02:52 +0000317 struct switchdev_trans *trans,
318 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700319{
Petr Machatad17d9f52018-11-22 23:32:57 +0000320 int rc;
321 int err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700322
Petr Machatad17d9f52018-11-22 23:32:57 +0000323 struct switchdev_notifier_port_obj_info obj_info = {
324 .obj = obj,
325 .trans = trans,
326 .handled = false,
327 };
Scott Feldman491d0f12015-05-10 09:47:52 -0700328
Petr Machata479c86d2018-12-12 17:02:54 +0000329 rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack);
Petr Machatad17d9f52018-11-22 23:32:57 +0000330 err = notifier_to_errno(rc);
331 if (err) {
332 WARN_ON(!obj_info.handled);
333 return err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700334 }
Petr Machatad17d9f52018-11-22 23:32:57 +0000335 if (!obj_info.handled)
336 return -EOPNOTSUPP;
337 return 0;
Scott Feldman491d0f12015-05-10 09:47:52 -0700338}
339
Jiri Pirko4d429c52015-10-14 19:40:52 +0200340static int switchdev_port_obj_add_now(struct net_device *dev,
Petr Machata69b73202018-12-12 17:02:52 +0000341 const struct switchdev_obj *obj,
342 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700343{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200344 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700345 int err;
346
347 ASSERT_RTNL();
348
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200349 switchdev_trans_init(&trans);
350
Scott Feldman491d0f12015-05-10 09:47:52 -0700351 /* Phase I: prepare for obj add. Driver/device should fail
352 * here if there are going to be issues in the commit phase,
353 * such as lack of resources or support. The driver/device
354 * should reserve resources needed for the commit phase here,
355 * but should not commit the obj.
356 */
357
Jiri Pirkof623ab72015-09-24 10:02:49 +0200358 trans.ph_prepare = true;
Petr Machatad17d9f52018-11-22 23:32:57 +0000359 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
Petr Machata69b73202018-12-12 17:02:52 +0000360 dev, obj, &trans, extack);
Scott Feldman491d0f12015-05-10 09:47:52 -0700361 if (err) {
362 /* Prepare phase failed: abort the transaction. Any
363 * resources reserved in the prepare phase are
364 * released.
365 */
366
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200367 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200368 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700369
370 return err;
371 }
372
373 /* Phase II: commit obj add. This cannot fail as a fault
374 * of driver/device. If it does, it's a bug in the driver/device
375 * because the driver said everythings was OK in phase I.
376 */
377
Jiri Pirkof623ab72015-09-24 10:02:49 +0200378 trans.ph_prepare = false;
Petr Machatad17d9f52018-11-22 23:32:57 +0000379 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
Petr Machata69b73202018-12-12 17:02:52 +0000380 dev, obj, &trans, extack);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200381 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200382 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700383
384 return err;
385}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200386
387static void switchdev_port_obj_add_deferred(struct net_device *dev,
388 const void *data)
389{
390 const struct switchdev_obj *obj = data;
391 int err;
392
Petr Machata69b73202018-12-12 17:02:52 +0000393 err = switchdev_port_obj_add_now(dev, obj, NULL);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200394 if (err && err != -EOPNOTSUPP)
395 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
396 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200397 if (obj->complete)
398 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200399}
400
401static int switchdev_port_obj_add_defer(struct net_device *dev,
402 const struct switchdev_obj *obj)
403{
Scott Feldmane258d912015-10-28 23:17:31 -0700404 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200405 switchdev_port_obj_add_deferred);
406}
Scott Feldman491d0f12015-05-10 09:47:52 -0700407
408/**
Jiri Pirko4d429c52015-10-14 19:40:52 +0200409 * switchdev_port_obj_add - Add port object
Scott Feldman491d0f12015-05-10 09:47:52 -0700410 *
411 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400412 * @id: object ID
Jiri Pirko4d429c52015-10-14 19:40:52 +0200413 * @obj: object to add
414 *
415 * Use a 2-phase prepare-commit transaction model to ensure
416 * system is not left in a partially updated state due to
417 * failure from driver/device.
418 *
419 * rtnl_lock must be held and must not be in atomic section,
420 * in case SWITCHDEV_F_DEFER flag is not set.
Scott Feldman491d0f12015-05-10 09:47:52 -0700421 */
Jiri Pirko4d429c52015-10-14 19:40:52 +0200422int switchdev_port_obj_add(struct net_device *dev,
Petr Machata69b73202018-12-12 17:02:52 +0000423 const struct switchdev_obj *obj,
424 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700425{
Jiri Pirko4d429c52015-10-14 19:40:52 +0200426 if (obj->flags & SWITCHDEV_F_DEFER)
427 return switchdev_port_obj_add_defer(dev, obj);
428 ASSERT_RTNL();
Petr Machata69b73202018-12-12 17:02:52 +0000429 return switchdev_port_obj_add_now(dev, obj, extack);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200430}
431EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
432
433static int switchdev_port_obj_del_now(struct net_device *dev,
434 const struct switchdev_obj *obj)
435{
Petr Machatad17d9f52018-11-22 23:32:57 +0000436 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL,
Petr Machata69b73202018-12-12 17:02:52 +0000437 dev, obj, NULL, NULL);
Scott Feldman491d0f12015-05-10 09:47:52 -0700438}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200439
440static void switchdev_port_obj_del_deferred(struct net_device *dev,
441 const void *data)
442{
443 const struct switchdev_obj *obj = data;
444 int err;
445
446 err = switchdev_port_obj_del_now(dev, obj);
447 if (err && err != -EOPNOTSUPP)
448 netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
449 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200450 if (obj->complete)
451 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200452}
453
454static int switchdev_port_obj_del_defer(struct net_device *dev,
455 const struct switchdev_obj *obj)
456{
Scott Feldmane258d912015-10-28 23:17:31 -0700457 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200458 switchdev_port_obj_del_deferred);
459}
460
461/**
462 * switchdev_port_obj_del - Delete port object
463 *
464 * @dev: port device
465 * @id: object ID
466 * @obj: object to delete
467 *
468 * rtnl_lock must be held and must not be in atomic section,
469 * in case SWITCHDEV_F_DEFER flag is not set.
470 */
471int switchdev_port_obj_del(struct net_device *dev,
472 const struct switchdev_obj *obj)
473{
474 if (obj->flags & SWITCHDEV_F_DEFER)
475 return switchdev_port_obj_del_defer(dev, obj);
476 ASSERT_RTNL();
477 return switchdev_port_obj_del_now(dev, obj);
478}
Scott Feldman491d0f12015-05-10 09:47:52 -0700479EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
480
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200481static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
Petr Machataa93e3b12018-11-22 23:28:25 +0000482static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100483
484/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700485 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100486 * @nb: notifier_block
487 *
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200488 * Register switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100489 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700490int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100491{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200492 return atomic_notifier_chain_register(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100493}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700494EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100495
496/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700497 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100498 * @nb: notifier_block
499 *
500 * Unregister switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100501 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700502int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100503{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200504 return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100505}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700506EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100507
508/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700509 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100510 * @val: value passed unmodified to notifier function
511 * @dev: port device
512 * @info: notifier information data
513 *
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200514 * Call all network notifier blocks.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100515 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700516int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
Petr Machata66859872019-01-16 23:06:56 +0000517 struct switchdev_notifier_info *info,
518 struct netlink_ext_ack *extack)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100519{
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100520 info->dev = dev;
Petr Machata66859872019-01-16 23:06:56 +0000521 info->extack = extack;
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200522 return atomic_notifier_call_chain(&switchdev_notif_chain, val, info);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100523}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700524EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800525
Petr Machataa93e3b12018-11-22 23:28:25 +0000526int register_switchdev_blocking_notifier(struct notifier_block *nb)
527{
528 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
529
530 return blocking_notifier_chain_register(chain, nb);
531}
532EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
533
534int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
535{
536 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
537
538 return blocking_notifier_chain_unregister(chain, nb);
539}
540EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
541
542int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
Petr Machata479c86d2018-12-12 17:02:54 +0000543 struct switchdev_notifier_info *info,
544 struct netlink_ext_ack *extack)
Petr Machataa93e3b12018-11-22 23:28:25 +0000545{
546 info->dev = dev;
Petr Machata479c86d2018-12-12 17:02:54 +0000547 info->extack = extack;
Petr Machataa93e3b12018-11-22 23:28:25 +0000548 return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
549 val, info);
550}
551EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
552
Petr Machataf30f0602018-11-22 23:29:44 +0000553static int __switchdev_handle_port_obj_add(struct net_device *dev,
554 struct switchdev_notifier_port_obj_info *port_obj_info,
555 bool (*check_cb)(const struct net_device *dev),
556 int (*add_cb)(struct net_device *dev,
557 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000558 struct switchdev_trans *trans,
559 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000560{
Petr Machata69213512018-12-12 17:02:56 +0000561 struct netlink_ext_ack *extack;
Petr Machataf30f0602018-11-22 23:29:44 +0000562 struct net_device *lower_dev;
563 struct list_head *iter;
564 int err = -EOPNOTSUPP;
565
Petr Machata69213512018-12-12 17:02:56 +0000566 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
567
Petr Machataf30f0602018-11-22 23:29:44 +0000568 if (check_cb(dev)) {
569 /* This flag is only checked if the return value is success. */
570 port_obj_info->handled = true;
Petr Machata69213512018-12-12 17:02:56 +0000571 return add_cb(dev, port_obj_info->obj, port_obj_info->trans,
572 extack);
Petr Machataf30f0602018-11-22 23:29:44 +0000573 }
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_obj_add(lower_dev, port_obj_info,
584 check_cb, add_cb);
585 if (err && err != -EOPNOTSUPP)
586 return err;
587 }
588
589 return err;
590}
591
592int switchdev_handle_port_obj_add(struct net_device *dev,
593 struct switchdev_notifier_port_obj_info *port_obj_info,
594 bool (*check_cb)(const struct net_device *dev),
595 int (*add_cb)(struct net_device *dev,
596 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000597 struct switchdev_trans *trans,
598 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000599{
600 int err;
601
602 err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
603 add_cb);
604 if (err == -EOPNOTSUPP)
605 err = 0;
606 return err;
607}
608EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
609
610static int __switchdev_handle_port_obj_del(struct net_device *dev,
611 struct switchdev_notifier_port_obj_info *port_obj_info,
612 bool (*check_cb)(const struct net_device *dev),
613 int (*del_cb)(struct net_device *dev,
614 const struct switchdev_obj *obj))
615{
616 struct net_device *lower_dev;
617 struct list_head *iter;
618 int err = -EOPNOTSUPP;
619
620 if (check_cb(dev)) {
621 /* This flag is only checked if the return value is success. */
622 port_obj_info->handled = true;
623 return del_cb(dev, port_obj_info->obj);
624 }
625
626 /* Switch ports might be stacked under e.g. a LAG. Ignore the
627 * unsupported devices, another driver might be able to handle them. But
628 * propagate to the callers any hard errors.
629 *
630 * If the driver does its own bookkeeping of stacked ports, it's not
631 * necessary to go through this helper.
632 */
633 netdev_for_each_lower_dev(dev, lower_dev, iter) {
634 err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
635 check_cb, del_cb);
636 if (err && err != -EOPNOTSUPP)
637 return err;
638 }
639
640 return err;
641}
642
643int switchdev_handle_port_obj_del(struct net_device *dev,
644 struct switchdev_notifier_port_obj_info *port_obj_info,
645 bool (*check_cb)(const struct net_device *dev),
646 int (*del_cb)(struct net_device *dev,
647 const struct switchdev_obj *obj))
648{
649 int err;
650
651 err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
652 del_cb);
653 if (err == -EOPNOTSUPP)
654 err = 0;
655 return err;
656}
657EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800658
659static int __switchdev_handle_port_attr_set(struct net_device *dev,
660 struct switchdev_notifier_port_attr_info *port_attr_info,
661 bool (*check_cb)(const struct net_device *dev),
662 int (*set_cb)(struct net_device *dev,
663 const struct switchdev_attr *attr,
664 struct switchdev_trans *trans))
665{
666 struct net_device *lower_dev;
667 struct list_head *iter;
668 int err = -EOPNOTSUPP;
669
670 if (check_cb(dev)) {
671 port_attr_info->handled = true;
672 return set_cb(dev, port_attr_info->attr,
673 port_attr_info->trans);
674 }
675
676 /* Switch ports might be stacked under e.g. a LAG. Ignore the
677 * unsupported devices, another driver might be able to handle them. But
678 * propagate to the callers any hard errors.
679 *
680 * If the driver does its own bookkeeping of stacked ports, it's not
681 * necessary to go through this helper.
682 */
683 netdev_for_each_lower_dev(dev, lower_dev, iter) {
684 err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info,
685 check_cb, set_cb);
686 if (err && err != -EOPNOTSUPP)
687 return err;
688 }
689
690 return err;
691}
692
693int switchdev_handle_port_attr_set(struct net_device *dev,
694 struct switchdev_notifier_port_attr_info *port_attr_info,
695 bool (*check_cb)(const struct net_device *dev),
696 int (*set_cb)(struct net_device *dev,
697 const struct switchdev_attr *attr,
698 struct switchdev_trans *trans))
699{
700 int err;
701
702 err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb,
703 set_cb);
704 if (err == -EOPNOTSUPP)
705 err = 0;
706 return err;
707}
708EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);