blob: a5fc9dd24aa9101eb80478f31a78d5fc6a278bd0 [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>
Scott Feldman5e8d9042015-03-05 21:21:15 -080024#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010025#include <net/switchdev.h>
26
27/**
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020028 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
29 *
30 * @trans: transaction
31 * @data: pointer to data being queued
32 * @destructor: data destructor
33 * @tritem: transaction item being queued
34 *
35 * Enqeueue data item to transaction queue. tritem is typically placed in
36 * cointainter pointed at by data pointer. Destructor is called on
37 * transaction abort and after successful commit phase in case
38 * the caller did not dequeue the item before.
39 */
40void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
41 void *data, void (*destructor)(void const *),
42 struct switchdev_trans_item *tritem)
43{
44 tritem->data = data;
45 tritem->destructor = destructor;
46 list_add_tail(&tritem->list, &trans->item_list);
47}
48EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
49
50static struct switchdev_trans_item *
51__switchdev_trans_item_dequeue(struct switchdev_trans *trans)
52{
53 struct switchdev_trans_item *tritem;
54
55 if (list_empty(&trans->item_list))
56 return NULL;
57 tritem = list_first_entry(&trans->item_list,
58 struct switchdev_trans_item, list);
59 list_del(&tritem->list);
60 return tritem;
61}
62
63/**
64 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
65 *
66 * @trans: transaction
67 */
68void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
69{
70 struct switchdev_trans_item *tritem;
71
72 tritem = __switchdev_trans_item_dequeue(trans);
73 BUG_ON(!tritem);
74 return tritem->data;
75}
76EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
77
78static void switchdev_trans_init(struct switchdev_trans *trans)
79{
80 INIT_LIST_HEAD(&trans->item_list);
81}
82
83static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
84{
85 struct switchdev_trans_item *tritem;
86
87 while ((tritem = __switchdev_trans_item_dequeue(trans)))
88 tritem->destructor(tritem->data);
89}
90
91static void switchdev_trans_items_warn_destroy(struct net_device *dev,
92 struct switchdev_trans *trans)
93{
94 WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
95 dev->name);
96 switchdev_trans_items_destroy(trans);
97}
98
Jiri Pirko793f4012015-10-14 19:40:48 +020099static LIST_HEAD(deferred);
100static DEFINE_SPINLOCK(deferred_lock);
101
102typedef void switchdev_deferred_func_t(struct net_device *dev,
103 const void *data);
104
105struct switchdev_deferred_item {
106 struct list_head list;
107 struct net_device *dev;
108 switchdev_deferred_func_t *func;
109 unsigned long data[0];
110};
111
112static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
113{
114 struct switchdev_deferred_item *dfitem;
115
116 spin_lock_bh(&deferred_lock);
117 if (list_empty(&deferred)) {
118 dfitem = NULL;
119 goto unlock;
120 }
121 dfitem = list_first_entry(&deferred,
122 struct switchdev_deferred_item, list);
123 list_del(&dfitem->list);
124unlock:
125 spin_unlock_bh(&deferred_lock);
126 return dfitem;
127}
128
129/**
130 * switchdev_deferred_process - Process ops in deferred queue
131 *
132 * Called to flush the ops currently queued in deferred ops queue.
133 * rtnl_lock must be held.
134 */
135void switchdev_deferred_process(void)
136{
137 struct switchdev_deferred_item *dfitem;
138
139 ASSERT_RTNL();
140
141 while ((dfitem = switchdev_deferred_dequeue())) {
142 dfitem->func(dfitem->dev, dfitem->data);
143 dev_put(dfitem->dev);
144 kfree(dfitem);
145 }
146}
147EXPORT_SYMBOL_GPL(switchdev_deferred_process);
148
149static void switchdev_deferred_process_work(struct work_struct *work)
150{
151 rtnl_lock();
152 switchdev_deferred_process();
153 rtnl_unlock();
154}
155
156static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
157
158static int switchdev_deferred_enqueue(struct net_device *dev,
159 const void *data, size_t data_len,
160 switchdev_deferred_func_t *func)
161{
162 struct switchdev_deferred_item *dfitem;
163
164 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
165 if (!dfitem)
166 return -ENOMEM;
167 dfitem->dev = dev;
168 dfitem->func = func;
169 memcpy(dfitem->data, data, data_len);
170 dev_hold(dev);
171 spin_lock_bh(&deferred_lock);
172 list_add_tail(&dfitem->list, &deferred);
173 spin_unlock_bh(&deferred_lock);
174 schedule_work(&deferred_process_work);
175 return 0;
176}
177
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200178/**
Scott Feldman30943332015-05-10 09:47:48 -0700179 * switchdev_port_attr_get - Get port attribute
180 *
181 * @dev: port device
182 * @attr: attribute to get
183 */
184int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
185{
186 const struct switchdev_ops *ops = dev->switchdev_ops;
187 struct net_device *lower_dev;
188 struct list_head *iter;
189 struct switchdev_attr first = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200190 .id = SWITCHDEV_ATTR_ID_UNDEFINED
Scott Feldman30943332015-05-10 09:47:48 -0700191 };
192 int err = -EOPNOTSUPP;
193
194 if (ops && ops->switchdev_port_attr_get)
195 return ops->switchdev_port_attr_get(dev, attr);
196
197 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
198 return err;
199
200 /* Switch device port(s) may be stacked under
201 * bond/team/vlan dev, so recurse down to get attr on
202 * each port. Return -ENODATA if attr values don't
203 * compare across ports.
204 */
205
206 netdev_for_each_lower_dev(dev, lower_dev, iter) {
207 err = switchdev_port_attr_get(lower_dev, attr);
208 if (err)
209 break;
Jiri Pirko1f868392015-10-01 11:03:42 +0200210 if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED)
Scott Feldman30943332015-05-10 09:47:48 -0700211 first = *attr;
212 else if (memcmp(&first, attr, sizeof(*attr)))
213 return -ENODATA;
214 }
215
216 return err;
217}
218EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
219
220static int __switchdev_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200221 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200222 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700223{
224 const struct switchdev_ops *ops = dev->switchdev_ops;
225 struct net_device *lower_dev;
226 struct list_head *iter;
227 int err = -EOPNOTSUPP;
228
Jiri Pirko0c63d802015-11-03 17:40:53 +0100229 if (ops && ops->switchdev_port_attr_set) {
230 err = ops->switchdev_port_attr_set(dev, attr, trans);
231 goto done;
232 }
Scott Feldman30943332015-05-10 09:47:48 -0700233
234 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
Scott Feldman464314e2015-10-08 19:23:18 -0700235 goto done;
Scott Feldman30943332015-05-10 09:47:48 -0700236
237 /* Switch device port(s) may be stacked under
238 * bond/team/vlan dev, so recurse down to set attr on
239 * each port.
240 */
241
242 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200243 err = __switchdev_port_attr_set(lower_dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700244 if (err)
245 break;
246 }
247
Scott Feldman464314e2015-10-08 19:23:18 -0700248done:
249 if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
250 err = 0;
251
Scott Feldman30943332015-05-10 09:47:48 -0700252 return err;
253}
254
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200255static int switchdev_port_attr_set_now(struct net_device *dev,
256 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700257{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200258 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700259 int err;
260
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200261 switchdev_trans_init(&trans);
262
Scott Feldman30943332015-05-10 09:47:48 -0700263 /* Phase I: prepare for attr set. Driver/device should fail
264 * here if there are going to be issues in the commit phase,
265 * such as lack of resources or support. The driver/device
266 * should reserve resources needed for the commit phase here,
267 * but should not commit the attr.
268 */
269
Jiri Pirkof623ab72015-09-24 10:02:49 +0200270 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200271 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700272 if (err) {
273 /* Prepare phase failed: abort the transaction. Any
274 * resources reserved in the prepare phase are
275 * released.
276 */
277
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200278 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200279 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700280
281 return err;
282 }
283
284 /* Phase II: commit attr set. This cannot fail as a fault
285 * of driver/device. If it does, it's a bug in the driver/device
286 * because the driver said everythings was OK in phase I.
287 */
288
Jiri Pirkof623ab72015-09-24 10:02:49 +0200289 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200290 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700291 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
292 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200293 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700294
295 return err;
296}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200297
298static void switchdev_port_attr_set_deferred(struct net_device *dev,
299 const void *data)
300{
301 const struct switchdev_attr *attr = data;
302 int err;
303
304 err = switchdev_port_attr_set_now(dev, attr);
305 if (err && err != -EOPNOTSUPP)
306 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
307 err, attr->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200308 if (attr->complete)
309 attr->complete(dev, err, attr->complete_priv);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200310}
311
312static int switchdev_port_attr_set_defer(struct net_device *dev,
313 const struct switchdev_attr *attr)
314{
315 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
316 switchdev_port_attr_set_deferred);
317}
318
319/**
320 * switchdev_port_attr_set - Set port attribute
321 *
322 * @dev: port device
323 * @attr: attribute to set
324 *
325 * Use a 2-phase prepare-commit transaction model to ensure
326 * system is not left in a partially updated state due to
327 * failure from driver/device.
328 *
329 * rtnl_lock must be held and must not be in atomic section,
330 * in case SWITCHDEV_F_DEFER flag is not set.
331 */
332int switchdev_port_attr_set(struct net_device *dev,
333 const struct switchdev_attr *attr)
334{
335 if (attr->flags & SWITCHDEV_F_DEFER)
336 return switchdev_port_attr_set_defer(dev, attr);
337 ASSERT_RTNL();
338 return switchdev_port_attr_set_now(dev, attr);
339}
Scott Feldman30943332015-05-10 09:47:48 -0700340EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
341
Scott Feldmane258d912015-10-28 23:17:31 -0700342static size_t switchdev_obj_size(const struct switchdev_obj *obj)
343{
344 switch (obj->id) {
345 case SWITCHDEV_OBJ_ID_PORT_VLAN:
346 return sizeof(struct switchdev_obj_port_vlan);
347 case SWITCHDEV_OBJ_ID_IPV4_FIB:
348 return sizeof(struct switchdev_obj_ipv4_fib);
349 case SWITCHDEV_OBJ_ID_PORT_FDB:
350 return sizeof(struct switchdev_obj_port_fdb);
Elad Raz4d41e1252016-01-10 21:06:22 +0100351 case SWITCHDEV_OBJ_ID_PORT_MDB:
352 return sizeof(struct switchdev_obj_port_mdb);
Scott Feldmane258d912015-10-28 23:17:31 -0700353 default:
354 BUG();
355 }
356 return 0;
357}
358
Scott Feldman22c1f672015-05-12 23:03:51 -0700359static int __switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200360 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200361 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700362{
363 const struct switchdev_ops *ops = dev->switchdev_ops;
364 struct net_device *lower_dev;
365 struct list_head *iter;
366 int err = -EOPNOTSUPP;
367
368 if (ops && ops->switchdev_port_obj_add)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200369 return ops->switchdev_port_obj_add(dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700370
371 /* Switch device port(s) may be stacked under
372 * bond/team/vlan dev, so recurse down to add object on
373 * each port.
374 */
375
376 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200377 err = __switchdev_port_obj_add(lower_dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700378 if (err)
379 break;
380 }
381
382 return err;
383}
384
Jiri Pirko4d429c52015-10-14 19:40:52 +0200385static int switchdev_port_obj_add_now(struct net_device *dev,
386 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700387{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200388 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700389 int err;
390
391 ASSERT_RTNL();
392
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200393 switchdev_trans_init(&trans);
394
Scott Feldman491d0f12015-05-10 09:47:52 -0700395 /* Phase I: prepare for obj add. Driver/device should fail
396 * here if there are going to be issues in the commit phase,
397 * such as lack of resources or support. The driver/device
398 * should reserve resources needed for the commit phase here,
399 * but should not commit the obj.
400 */
401
Jiri Pirkof623ab72015-09-24 10:02:49 +0200402 trans.ph_prepare = true;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200403 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700404 if (err) {
405 /* Prepare phase failed: abort the transaction. Any
406 * resources reserved in the prepare phase are
407 * released.
408 */
409
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200410 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200411 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700412
413 return err;
414 }
415
416 /* Phase II: commit obj add. This cannot fail as a fault
417 * of driver/device. If it does, it's a bug in the driver/device
418 * because the driver said everythings was OK in phase I.
419 */
420
Jiri Pirkof623ab72015-09-24 10:02:49 +0200421 trans.ph_prepare = false;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200422 err = __switchdev_port_obj_add(dev, obj, &trans);
423 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200424 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700425
426 return err;
427}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200428
429static void switchdev_port_obj_add_deferred(struct net_device *dev,
430 const void *data)
431{
432 const struct switchdev_obj *obj = data;
433 int err;
434
435 err = switchdev_port_obj_add_now(dev, obj);
436 if (err && err != -EOPNOTSUPP)
437 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
438 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200439 if (obj->complete)
440 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200441}
442
443static int switchdev_port_obj_add_defer(struct net_device *dev,
444 const struct switchdev_obj *obj)
445{
Scott Feldmane258d912015-10-28 23:17:31 -0700446 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200447 switchdev_port_obj_add_deferred);
448}
Scott Feldman491d0f12015-05-10 09:47:52 -0700449
450/**
Jiri Pirko4d429c52015-10-14 19:40:52 +0200451 * switchdev_port_obj_add - Add port object
Scott Feldman491d0f12015-05-10 09:47:52 -0700452 *
453 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400454 * @id: object ID
Jiri Pirko4d429c52015-10-14 19:40:52 +0200455 * @obj: object to add
456 *
457 * Use a 2-phase prepare-commit transaction model to ensure
458 * system is not left in a partially updated state due to
459 * failure from driver/device.
460 *
461 * rtnl_lock must be held and must not be in atomic section,
462 * in case SWITCHDEV_F_DEFER flag is not set.
Scott Feldman491d0f12015-05-10 09:47:52 -0700463 */
Jiri Pirko4d429c52015-10-14 19:40:52 +0200464int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200465 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700466{
Jiri Pirko4d429c52015-10-14 19:40:52 +0200467 if (obj->flags & SWITCHDEV_F_DEFER)
468 return switchdev_port_obj_add_defer(dev, obj);
469 ASSERT_RTNL();
470 return switchdev_port_obj_add_now(dev, obj);
471}
472EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
473
474static int switchdev_port_obj_del_now(struct net_device *dev,
475 const struct switchdev_obj *obj)
476{
Scott Feldman491d0f12015-05-10 09:47:52 -0700477 const struct switchdev_ops *ops = dev->switchdev_ops;
478 struct net_device *lower_dev;
479 struct list_head *iter;
480 int err = -EOPNOTSUPP;
481
482 if (ops && ops->switchdev_port_obj_del)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200483 return ops->switchdev_port_obj_del(dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700484
485 /* Switch device port(s) may be stacked under
486 * bond/team/vlan dev, so recurse down to delete object on
487 * each port.
488 */
489
490 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko4d429c52015-10-14 19:40:52 +0200491 err = switchdev_port_obj_del_now(lower_dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700492 if (err)
493 break;
494 }
495
496 return err;
497}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200498
499static void switchdev_port_obj_del_deferred(struct net_device *dev,
500 const void *data)
501{
502 const struct switchdev_obj *obj = data;
503 int err;
504
505 err = switchdev_port_obj_del_now(dev, obj);
506 if (err && err != -EOPNOTSUPP)
507 netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
508 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200509 if (obj->complete)
510 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200511}
512
513static int switchdev_port_obj_del_defer(struct net_device *dev,
514 const struct switchdev_obj *obj)
515{
Scott Feldmane258d912015-10-28 23:17:31 -0700516 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200517 switchdev_port_obj_del_deferred);
518}
519
520/**
521 * switchdev_port_obj_del - Delete port object
522 *
523 * @dev: port device
524 * @id: object ID
525 * @obj: object to delete
526 *
527 * rtnl_lock must be held and must not be in atomic section,
528 * in case SWITCHDEV_F_DEFER flag is not set.
529 */
530int switchdev_port_obj_del(struct net_device *dev,
531 const struct switchdev_obj *obj)
532{
533 if (obj->flags & SWITCHDEV_F_DEFER)
534 return switchdev_port_obj_del_defer(dev, obj);
535 ASSERT_RTNL();
536 return switchdev_port_obj_del_now(dev, obj);
537}
Scott Feldman491d0f12015-05-10 09:47:52 -0700538EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
539
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700540/**
541 * switchdev_port_obj_dump - Dump port objects
542 *
543 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400544 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700545 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400546 * @cb: function to call with a filled object
Jiri Pirko771acac2015-10-14 19:40:55 +0200547 *
548 * rtnl_lock must be held.
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700549 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200550int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200551 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700552{
553 const struct switchdev_ops *ops = dev->switchdev_ops;
554 struct net_device *lower_dev;
555 struct list_head *iter;
556 int err = -EOPNOTSUPP;
557
Jiri Pirko771acac2015-10-14 19:40:55 +0200558 ASSERT_RTNL();
559
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700560 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200561 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700562
563 /* Switch device port(s) may be stacked under
564 * bond/team/vlan dev, so recurse down to dump objects on
565 * first port at bottom of stack.
566 */
567
568 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200569 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700570 break;
571 }
572
573 return err;
574}
575EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
576
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700577static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100578
579/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700580 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100581 * @nb: notifier_block
582 *
583 * Register switch device notifier. This should be used by code
584 * which needs to monitor events happening in particular device.
585 * Return values are same as for atomic_notifier_chain_register().
586 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700587int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100588{
589 int err;
590
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +0100591 rtnl_lock();
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700592 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +0100593 rtnl_unlock();
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100594 return err;
595}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700596EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100597
598/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700599 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100600 * @nb: notifier_block
601 *
602 * Unregister switch device notifier.
603 * Return values are same as for atomic_notifier_chain_unregister().
604 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700605int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100606{
607 int err;
608
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +0100609 rtnl_lock();
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700610 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +0100611 rtnl_unlock();
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100612 return err;
613}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700614EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100615
616/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700617 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100618 * @val: value passed unmodified to notifier function
619 * @dev: port device
620 * @info: notifier information data
621 *
622 * Call all network notifier blocks. This should be called by driver
623 * when it needs to propagate hardware event.
624 * Return values are same as for atomic_notifier_call_chain().
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +0100625 * rtnl_lock must be held.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100626 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700627int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
628 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100629{
630 int err;
631
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +0100632 ASSERT_RTNL();
633
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100634 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700635 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100636 return err;
637}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700638EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800639
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700640struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200641 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700642 struct sk_buff *skb;
643 u32 filter_mask;
644 u16 flags;
645 u16 begin;
646 u16 end;
647};
648
Vivien Didelote23b0022015-09-29 12:07:13 -0400649static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700650{
651 struct bridge_vlan_info vinfo;
652
653 vinfo.flags = dump->flags;
654
655 if (dump->begin == 0 && dump->end == 0) {
656 return 0;
657 } else if (dump->begin == dump->end) {
658 vinfo.vid = dump->begin;
659 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
660 sizeof(vinfo), &vinfo))
661 return -EMSGSIZE;
662 } else {
663 vinfo.vid = dump->begin;
664 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
665 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
666 sizeof(vinfo), &vinfo))
667 return -EMSGSIZE;
668 vinfo.vid = dump->end;
669 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
670 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
671 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
672 sizeof(vinfo), &vinfo))
673 return -EMSGSIZE;
674 }
675
676 return 0;
677}
678
Jiri Pirko648b4a92015-10-01 11:03:45 +0200679static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700680{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200681 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700682 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400683 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700684 int err = 0;
685
686 if (vlan->vid_begin > vlan->vid_end)
687 return -EINVAL;
688
689 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
690 dump->flags = vlan->flags;
691 for (dump->begin = dump->end = vlan->vid_begin;
692 dump->begin <= vlan->vid_end;
693 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400694 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700695 if (err)
696 return err;
697 }
698 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
699 if (dump->begin > vlan->vid_begin &&
700 dump->begin >= vlan->vid_end) {
701 if ((dump->begin - 1) == vlan->vid_end &&
702 dump->flags == vlan->flags) {
703 /* prepend */
704 dump->begin = vlan->vid_begin;
705 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400706 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700707 dump->flags = vlan->flags;
708 dump->begin = vlan->vid_begin;
709 dump->end = vlan->vid_end;
710 }
711 } else if (dump->end <= vlan->vid_begin &&
712 dump->end < vlan->vid_end) {
713 if ((dump->end + 1) == vlan->vid_begin &&
714 dump->flags == vlan->flags) {
715 /* append */
716 dump->end = vlan->vid_end;
717 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400718 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700719 dump->flags = vlan->flags;
720 dump->begin = vlan->vid_begin;
721 dump->end = vlan->vid_end;
722 }
723 } else {
724 err = -EINVAL;
725 }
726 }
727
728 return err;
729}
730
731static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
732 u32 filter_mask)
733{
734 struct switchdev_vlan_dump dump = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100735 .vlan.obj.orig_dev = dev,
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200736 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700737 .skb = skb,
738 .filter_mask = filter_mask,
739 };
740 int err = 0;
741
742 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
743 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200744 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400745 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700746 if (err)
747 goto err_out;
748 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
749 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400750 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700751 }
752
753err_out:
754 return err == -EOPNOTSUPP ? 0 : err;
755}
756
Scott Feldman8793d0a2015-05-10 09:48:04 -0700757/**
758 * switchdev_port_bridge_getlink - Get bridge port attributes
759 *
760 * @dev: port device
761 *
762 * Called for SELF on rtnl_bridge_getlink to get bridge port
763 * attributes.
764 */
765int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
766 struct net_device *dev, u32 filter_mask,
767 int nlflags)
768{
769 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100770 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +0200771 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700772 };
773 u16 mode = BRIDGE_MODE_UNDEF;
Ido Schimmel741af002015-10-28 10:16:54 +0100774 u32 mask = BR_LEARNING | BR_LEARNING_SYNC | BR_FLOOD;
Scott Feldman8793d0a2015-05-10 09:48:04 -0700775 int err;
776
777 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400778 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700779 return err;
780
781 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700782 attr.u.brport_flags, mask, nlflags,
783 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700784}
785EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
786
Scott Feldman47f83282015-05-10 09:47:56 -0700787static int switchdev_port_br_setflag(struct net_device *dev,
788 struct nlattr *nlattr,
789 unsigned long brport_flag)
790{
791 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100792 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +0200793 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700794 };
795 u8 flag = nla_get_u8(nlattr);
796 int err;
797
798 err = switchdev_port_attr_get(dev, &attr);
799 if (err)
800 return err;
801
802 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700803 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700804 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700805 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700806
807 return switchdev_port_attr_set(dev, &attr);
808}
809
810static const struct nla_policy
811switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
812 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
813 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
814 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
815 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
816 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
817 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
818 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
819 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
820 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
821 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
822};
823
824static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
825 struct nlattr *protinfo)
826{
827 struct nlattr *attr;
828 int rem;
829 int err;
830
831 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
832 switchdev_port_bridge_policy);
833 if (err)
834 return err;
835
836 nla_for_each_nested(attr, protinfo, rem) {
837 switch (nla_type(attr)) {
838 case IFLA_BRPORT_LEARNING:
839 err = switchdev_port_br_setflag(dev, attr,
840 BR_LEARNING);
841 break;
842 case IFLA_BRPORT_LEARNING_SYNC:
843 err = switchdev_port_br_setflag(dev, attr,
844 BR_LEARNING_SYNC);
845 break;
Ido Schimmel741af002015-10-28 10:16:54 +0100846 case IFLA_BRPORT_UNICAST_FLOOD:
847 err = switchdev_port_br_setflag(dev, attr, BR_FLOOD);
848 break;
Scott Feldman47f83282015-05-10 09:47:56 -0700849 default:
850 err = -EOPNOTSUPP;
851 break;
852 }
853 if (err)
854 return err;
855 }
856
857 return 0;
858}
859
860static int switchdev_port_br_afspec(struct net_device *dev,
861 struct nlattr *afspec,
862 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200863 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700864{
865 struct nlattr *attr;
866 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200867 struct switchdev_obj_port_vlan vlan = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100868 .obj.orig_dev = dev,
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200869 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
870 };
Scott Feldman47f83282015-05-10 09:47:56 -0700871 int rem;
872 int err;
873
874 nla_for_each_nested(attr, afspec, rem) {
875 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
876 continue;
877 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
878 return -EINVAL;
879 vinfo = nla_data(attr);
Nikolay Aleksandrov87aaf2c2015-10-12 14:31:01 +0200880 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
881 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400882 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700883 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400884 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700885 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400886 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200887 /* don't allow range of pvids */
888 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
889 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700890 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400891 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700892 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400893 vlan.vid_end = vinfo->vid;
894 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700895 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200896 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700897 if (err)
898 return err;
Scott Feldman3a7bde52015-10-28 23:17:30 -0700899 vlan.vid_begin = 0;
Scott Feldman47f83282015-05-10 09:47:56 -0700900 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400901 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700902 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400903 vlan.vid_begin = vinfo->vid;
904 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200905 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700906 if (err)
907 return err;
Scott Feldman3a7bde52015-10-28 23:17:30 -0700908 vlan.vid_begin = 0;
Scott Feldman47f83282015-05-10 09:47:56 -0700909 }
910 }
911
912 return 0;
913}
914
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800915/**
Scott Feldman47f83282015-05-10 09:47:56 -0700916 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800917 *
918 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700919 * @nlh: netlink header
920 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800921 *
Scott Feldman47f83282015-05-10 09:47:56 -0700922 * Called for SELF on rtnl_bridge_setlink to set bridge port
923 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800924 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700925int switchdev_port_bridge_setlink(struct net_device *dev,
926 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800927{
Scott Feldman47f83282015-05-10 09:47:56 -0700928 struct nlattr *protinfo;
929 struct nlattr *afspec;
930 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800931
Scott Feldman47f83282015-05-10 09:47:56 -0700932 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
933 IFLA_PROTINFO);
934 if (protinfo) {
935 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
936 if (err)
937 return err;
938 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800939
Scott Feldman47f83282015-05-10 09:47:56 -0700940 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
941 IFLA_AF_SPEC);
942 if (afspec)
943 err = switchdev_port_br_afspec(dev, afspec,
944 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800945
Scott Feldman47f83282015-05-10 09:47:56 -0700946 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800947}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700948EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800949
950/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700951 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800952 *
953 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700954 * @nlh: netlink header
955 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800956 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700957 * Called for SELF on rtnl_bridge_dellink to set bridge port
958 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800959 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700960int switchdev_port_bridge_dellink(struct net_device *dev,
961 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800962{
Scott Feldman5c34e022015-05-10 09:48:00 -0700963 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800964
Scott Feldman5c34e022015-05-10 09:48:00 -0700965 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
966 IFLA_AF_SPEC);
967 if (afspec)
968 return switchdev_port_br_afspec(dev, afspec,
969 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800970
Scott Feldman5c34e022015-05-10 09:48:00 -0700971 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800972}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700973EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800974
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700975/**
976 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
977 *
978 * @ndmsg: netlink hdr
979 * @nlattr: netlink attributes
980 * @dev: port device
981 * @addr: MAC address to add
982 * @vid: VLAN to add
983 *
984 * Add FDB entry to switch device.
985 */
986int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
987 struct net_device *dev, const unsigned char *addr,
988 u16 vid, u16 nlm_flags)
989{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200990 struct switchdev_obj_port_fdb fdb = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +0100991 .obj.orig_dev = dev,
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200992 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400993 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700994 };
995
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200996 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200997 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700998}
999EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
1000
1001/**
1002 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
1003 *
1004 * @ndmsg: netlink hdr
1005 * @nlattr: netlink attributes
1006 * @dev: port device
1007 * @addr: MAC address to delete
1008 * @vid: VLAN to delete
1009 *
1010 * Delete FDB entry from switch device.
1011 */
1012int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1013 struct net_device *dev, const unsigned char *addr,
1014 u16 vid)
1015{
Jiri Pirko52ba57c2015-10-01 11:03:44 +02001016 struct switchdev_obj_port_fdb fdb = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001017 .obj.orig_dev = dev,
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001018 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -04001019 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001020 };
1021
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001022 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001023 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001024}
1025EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
1026
1027struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +02001028 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b2015-09-29 12:07:14 -04001029 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001030 struct sk_buff *skb;
1031 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001032 int idx;
1033};
1034
Jiri Pirko648b4a92015-10-01 11:03:45 +02001035static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001036{
Jiri Pirko648b4a92015-10-01 11:03:45 +02001037 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001038 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001039 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001040 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1041 u32 seq = dump->cb->nlh->nlmsg_seq;
1042 struct nlmsghdr *nlh;
1043 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001044
1045 if (dump->idx < dump->cb->args[0])
1046 goto skip;
1047
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001048 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1049 sizeof(*ndm), NLM_F_MULTI);
1050 if (!nlh)
1051 return -EMSGSIZE;
1052
1053 ndm = nlmsg_data(nlh);
1054 ndm->ndm_family = AF_BRIDGE;
1055 ndm->ndm_pad1 = 0;
1056 ndm->ndm_pad2 = 0;
1057 ndm->ndm_flags = NTF_SELF;
1058 ndm->ndm_type = 0;
Vivien Didelote02a06b2015-09-29 12:07:14 -04001059 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001060 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001061
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001062 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001063 goto nla_put_failure;
1064
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001065 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001066 goto nla_put_failure;
1067
1068 nlmsg_end(dump->skb, nlh);
1069
1070skip:
1071 dump->idx++;
1072 return 0;
1073
1074nla_put_failure:
1075 nlmsg_cancel(dump->skb, nlh);
1076 return -EMSGSIZE;
1077}
1078
1079/**
1080 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
1081 *
1082 * @skb: netlink skb
1083 * @cb: netlink callback
1084 * @dev: port device
1085 * @filter_dev: filter device
1086 * @idx:
1087 *
Nicolas Dichtel3e347662016-03-24 16:50:00 +01001088 * Dump FDB entries from switch device.
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001089 */
1090int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1091 struct net_device *dev,
1092 struct net_device *filter_dev, int idx)
1093{
1094 struct switchdev_fdb_dump dump = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001095 .fdb.obj.orig_dev = dev,
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001096 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b2015-09-29 12:07:14 -04001097 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001098 .skb = skb,
1099 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001100 .idx = idx,
1101 };
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09001102 int err;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001103
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09001104 err = switchdev_port_obj_dump(dev, &dump.fdb.obj,
1105 switchdev_port_fdb_dump_cb);
1106 cb->args[1] = err;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001107 return dump.idx;
1108}
1109EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
1110
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001111static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001112{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001113 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001114 struct net_device *lower_dev;
1115 struct net_device *port_dev;
1116 struct list_head *iter;
1117
1118 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001119 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001120 */
1121
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001122 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001123 return dev;
1124
1125 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001126 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001127 if (port_dev)
1128 return port_dev;
1129 }
1130
1131 return NULL;
1132}
1133
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001134static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001135{
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001136 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001137 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001138 };
1139 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001140 struct net_device *dev = NULL;
1141 int nhsel;
1142
Jiri Pirko771acac2015-10-14 19:40:55 +02001143 ASSERT_RTNL();
1144
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001145 /* For this route, all nexthop devs must be on the same switch. */
1146
1147 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1148 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1149
1150 if (!nh->nh_dev)
1151 return NULL;
1152
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001153 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001154 if (!dev)
1155 return NULL;
1156
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001157 attr.orig_dev = dev;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001158 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001159 return NULL;
1160
Scott Feldmand754f982015-07-18 18:24:49 -07001161 if (nhsel > 0 &&
1162 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001163 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001164
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001165 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001166 }
1167
1168 return dev;
1169}
1170
Scott Feldman5e8d9042015-03-05 21:21:15 -08001171/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001172 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001173 *
1174 * @dst: route's IPv4 destination address
1175 * @dst_len: destination address length (prefix length)
1176 * @fi: route FIB info structure
1177 * @tos: route TOS
1178 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001179 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001180 * @tb_id: route table ID
1181 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001182 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001183 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001184int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1185 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001186{
Vivien Didelotab069002015-09-29 12:07:17 -04001187 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001188 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001189 .dst = dst,
1190 .dst_len = dst_len,
Jiri Pirkoda4ed552016-05-17 18:58:08 +02001191 .fi = fi,
Vivien Didelotab069002015-09-29 12:07:17 -04001192 .tos = tos,
1193 .type = type,
1194 .nlflags = nlflags,
1195 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001196 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001197 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001198 int err = 0;
1199
Scott Feldman8e05fd72015-03-05 21:21:19 -08001200 /* Don't offload route if using custom ip rules or if
1201 * IPv4 FIB offloading has been disabled completely.
1202 */
1203
Scott Feldmane1315db2015-03-06 01:14:36 -08001204#ifdef CONFIG_IP_MULTIPLE_TABLES
1205 if (fi->fib_net->ipv4.fib_has_custom_rules)
1206 return 0;
1207#endif
1208
1209 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001210 return 0;
1211
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001212 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001213 if (!dev)
1214 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001215
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001216 ipv4_fib.obj.orig_dev = dev;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001217 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001218 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001219 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001220
Scott Feldmanaf201f72015-06-10 17:04:49 -07001221 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001222}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001223EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001224
1225/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001226 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001227 *
1228 * @dst: route's IPv4 destination address
1229 * @dst_len: destination address length (prefix length)
1230 * @fi: route FIB info structure
1231 * @tos: route TOS
1232 * @type: route type
1233 * @tb_id: route table ID
1234 *
1235 * Delete IPv4 route entry from switch device.
1236 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001237int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1238 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001239{
Vivien Didelotab069002015-09-29 12:07:17 -04001240 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001241 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001242 .dst = dst,
1243 .dst_len = dst_len,
Jiri Pirkoda4ed552016-05-17 18:58:08 +02001244 .fi = fi,
Vivien Didelotab069002015-09-29 12:07:17 -04001245 .tos = tos,
1246 .type = type,
1247 .nlflags = 0,
1248 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001249 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001250 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001251 int err = 0;
1252
Roopa Prabhueea39942015-05-13 21:17:41 -07001253 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001254 return 0;
1255
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001256 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001257 if (!dev)
1258 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001259
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001260 ipv4_fib.obj.orig_dev = dev;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001261 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001262 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001263 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001264
Scott Feldmanaf201f72015-06-10 17:04:49 -07001265 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001266}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001267EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001268
1269/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001270 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001271 *
1272 * @fi: route FIB info structure
1273 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001274void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001275{
1276 /* There was a problem installing this route to the offload
1277 * device. For now, until we come up with more refined
1278 * policy handling, abruptly end IPv4 fib offloading for
1279 * for entire net by flushing offload device(s) of all
1280 * IPv4 routes, and mark IPv4 fib offloading broken from
1281 * this point forward.
1282 */
1283
1284 fib_flush_external(fi->fib_net);
1285 fi->fib_net->ipv4.fib_offload_disabled = true;
1286}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001287EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001288
Or Gerlitz84388842016-07-14 10:32:43 +03001289bool switchdev_port_same_parent_id(struct net_device *a,
1290 struct net_device *b)
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001291{
1292 struct switchdev_attr a_attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001293 .orig_dev = a,
Jiri Pirko1f868392015-10-01 11:03:42 +02001294 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001295 .flags = SWITCHDEV_F_NO_RECURSE,
1296 };
1297 struct switchdev_attr b_attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001298 .orig_dev = b,
Jiri Pirko1f868392015-10-01 11:03:42 +02001299 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001300 .flags = SWITCHDEV_F_NO_RECURSE,
1301 };
1302
1303 if (switchdev_port_attr_get(a, &a_attr) ||
1304 switchdev_port_attr_get(b, &b_attr))
1305 return false;
1306
1307 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1308}
1309
1310static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1311 struct net_device *group_dev)
1312{
1313 struct net_device *lower_dev;
1314 struct list_head *iter;
1315
1316 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1317 if (lower_dev == dev)
1318 continue;
1319 if (switchdev_port_same_parent_id(dev, lower_dev))
1320 return lower_dev->offload_fwd_mark;
1321 return switchdev_port_fwd_mark_get(dev, lower_dev);
1322 }
1323
1324 return dev->ifindex;
1325}
Or Gerlitz84388842016-07-14 10:32:43 +03001326EXPORT_SYMBOL_GPL(switchdev_port_same_parent_id);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001327
1328static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1329 u32 old_mark, u32 *reset_mark)
1330{
1331 struct net_device *lower_dev;
1332 struct list_head *iter;
1333
1334 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1335 if (lower_dev->offload_fwd_mark == old_mark) {
1336 if (!*reset_mark)
1337 *reset_mark = lower_dev->ifindex;
1338 lower_dev->offload_fwd_mark = *reset_mark;
1339 }
1340 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1341 }
1342}
1343
1344/**
1345 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1346 *
1347 * @dev: port device
1348 * @group_dev: containing device
1349 * @joining: true if dev is joining group; false if leaving group
1350 *
1351 * An ungrouped port's offload mark is just its ifindex. A grouped
1352 * port's (member of a bridge, for example) offload mark is the ifindex
1353 * of one of the ports in the group with the same parent (switch) ID.
1354 * Ports on the same device in the same group will have the same mark.
1355 *
1356 * Example:
1357 *
1358 * br0 ifindex=9
1359 * sw1p1 ifindex=2 mark=2
1360 * sw1p2 ifindex=3 mark=2
1361 * sw2p1 ifindex=4 mark=5
1362 * sw2p2 ifindex=5 mark=5
1363 *
1364 * If sw2p2 leaves the bridge, we'll have:
1365 *
1366 * br0 ifindex=9
1367 * sw1p1 ifindex=2 mark=2
1368 * sw1p2 ifindex=3 mark=2
1369 * sw2p1 ifindex=4 mark=4
1370 * sw2p2 ifindex=5 mark=5
1371 */
1372void switchdev_port_fwd_mark_set(struct net_device *dev,
1373 struct net_device *group_dev,
1374 bool joining)
1375{
1376 u32 mark = dev->ifindex;
1377 u32 reset_mark = 0;
1378
Jiri Pirko771acac2015-10-14 19:40:55 +02001379 if (group_dev) {
1380 ASSERT_RTNL();
1381 if (joining)
1382 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1383 else if (dev->offload_fwd_mark == mark)
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001384 /* Ohoh, this port was the mark reference port,
1385 * but it's leaving the group, so reset the
1386 * mark for the remaining ports in the group.
1387 */
1388 switchdev_port_fwd_mark_reset(group_dev, mark,
1389 &reset_mark);
1390 }
1391
1392 dev->offload_fwd_mark = mark;
1393}
1394EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);