blob: eac68c4e57ec958f19e49cc34d88c719b7f77e94 [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>
Scott Feldman5e8d9042015-03-05 21:21:15 -080022#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010023#include <net/switchdev.h>
24
25/**
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020026 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
27 *
28 * @trans: transaction
29 * @data: pointer to data being queued
30 * @destructor: data destructor
31 * @tritem: transaction item being queued
32 *
33 * Enqeueue data item to transaction queue. tritem is typically placed in
34 * cointainter pointed at by data pointer. Destructor is called on
35 * transaction abort and after successful commit phase in case
36 * the caller did not dequeue the item before.
37 */
38void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
39 void *data, void (*destructor)(void const *),
40 struct switchdev_trans_item *tritem)
41{
42 tritem->data = data;
43 tritem->destructor = destructor;
44 list_add_tail(&tritem->list, &trans->item_list);
45}
46EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
47
48static struct switchdev_trans_item *
49__switchdev_trans_item_dequeue(struct switchdev_trans *trans)
50{
51 struct switchdev_trans_item *tritem;
52
53 if (list_empty(&trans->item_list))
54 return NULL;
55 tritem = list_first_entry(&trans->item_list,
56 struct switchdev_trans_item, list);
57 list_del(&tritem->list);
58 return tritem;
59}
60
61/**
62 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
63 *
64 * @trans: transaction
65 */
66void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
67{
68 struct switchdev_trans_item *tritem;
69
70 tritem = __switchdev_trans_item_dequeue(trans);
71 BUG_ON(!tritem);
72 return tritem->data;
73}
74EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
75
76static void switchdev_trans_init(struct switchdev_trans *trans)
77{
78 INIT_LIST_HEAD(&trans->item_list);
79}
80
81static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
82{
83 struct switchdev_trans_item *tritem;
84
85 while ((tritem = __switchdev_trans_item_dequeue(trans)))
86 tritem->destructor(tritem->data);
87}
88
89static void switchdev_trans_items_warn_destroy(struct net_device *dev,
90 struct switchdev_trans *trans)
91{
92 WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
93 dev->name);
94 switchdev_trans_items_destroy(trans);
95}
96
Jiri Pirko793f4012015-10-14 19:40:48 +020097static LIST_HEAD(deferred);
98static DEFINE_SPINLOCK(deferred_lock);
99
100typedef void switchdev_deferred_func_t(struct net_device *dev,
101 const void *data);
102
103struct switchdev_deferred_item {
104 struct list_head list;
105 struct net_device *dev;
106 switchdev_deferred_func_t *func;
107 unsigned long data[0];
108};
109
110static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
111{
112 struct switchdev_deferred_item *dfitem;
113
114 spin_lock_bh(&deferred_lock);
115 if (list_empty(&deferred)) {
116 dfitem = NULL;
117 goto unlock;
118 }
119 dfitem = list_first_entry(&deferred,
120 struct switchdev_deferred_item, list);
121 list_del(&dfitem->list);
122unlock:
123 spin_unlock_bh(&deferred_lock);
124 return dfitem;
125}
126
127/**
128 * switchdev_deferred_process - Process ops in deferred queue
129 *
130 * Called to flush the ops currently queued in deferred ops queue.
131 * rtnl_lock must be held.
132 */
133void switchdev_deferred_process(void)
134{
135 struct switchdev_deferred_item *dfitem;
136
137 ASSERT_RTNL();
138
139 while ((dfitem = switchdev_deferred_dequeue())) {
140 dfitem->func(dfitem->dev, dfitem->data);
141 dev_put(dfitem->dev);
142 kfree(dfitem);
143 }
144}
145EXPORT_SYMBOL_GPL(switchdev_deferred_process);
146
147static void switchdev_deferred_process_work(struct work_struct *work)
148{
149 rtnl_lock();
150 switchdev_deferred_process();
151 rtnl_unlock();
152}
153
154static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
155
156static int switchdev_deferred_enqueue(struct net_device *dev,
157 const void *data, size_t data_len,
158 switchdev_deferred_func_t *func)
159{
160 struct switchdev_deferred_item *dfitem;
161
162 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
163 if (!dfitem)
164 return -ENOMEM;
165 dfitem->dev = dev;
166 dfitem->func = func;
167 memcpy(dfitem->data, data, data_len);
168 dev_hold(dev);
169 spin_lock_bh(&deferred_lock);
170 list_add_tail(&dfitem->list, &deferred);
171 spin_unlock_bh(&deferred_lock);
172 schedule_work(&deferred_process_work);
173 return 0;
174}
175
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200176/**
Scott Feldman30943332015-05-10 09:47:48 -0700177 * switchdev_port_attr_get - Get port attribute
178 *
179 * @dev: port device
180 * @attr: attribute to get
181 */
182int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
183{
184 const struct switchdev_ops *ops = dev->switchdev_ops;
185 struct net_device *lower_dev;
186 struct list_head *iter;
187 struct switchdev_attr first = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200188 .id = SWITCHDEV_ATTR_ID_UNDEFINED
Scott Feldman30943332015-05-10 09:47:48 -0700189 };
190 int err = -EOPNOTSUPP;
191
192 if (ops && ops->switchdev_port_attr_get)
193 return ops->switchdev_port_attr_get(dev, attr);
194
195 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
196 return err;
197
198 /* Switch device port(s) may be stacked under
199 * bond/team/vlan dev, so recurse down to get attr on
200 * each port. Return -ENODATA if attr values don't
201 * compare across ports.
202 */
203
204 netdev_for_each_lower_dev(dev, lower_dev, iter) {
205 err = switchdev_port_attr_get(lower_dev, attr);
206 if (err)
207 break;
Jiri Pirko1f868392015-10-01 11:03:42 +0200208 if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED)
Scott Feldman30943332015-05-10 09:47:48 -0700209 first = *attr;
210 else if (memcmp(&first, attr, sizeof(*attr)))
211 return -ENODATA;
212 }
213
214 return err;
215}
216EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
217
218static int __switchdev_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200219 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200220 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700221{
222 const struct switchdev_ops *ops = dev->switchdev_ops;
223 struct net_device *lower_dev;
224 struct list_head *iter;
225 int err = -EOPNOTSUPP;
226
227 if (ops && ops->switchdev_port_attr_set)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200228 return ops->switchdev_port_attr_set(dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700229
230 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
Scott Feldman464314e2015-10-08 19:23:18 -0700231 goto done;
Scott Feldman30943332015-05-10 09:47:48 -0700232
233 /* Switch device port(s) may be stacked under
234 * bond/team/vlan dev, so recurse down to set attr on
235 * each port.
236 */
237
238 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200239 err = __switchdev_port_attr_set(lower_dev, attr, trans);
Scott Feldman464314e2015-10-08 19:23:18 -0700240 if (err == -EOPNOTSUPP &&
241 attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
242 continue;
Scott Feldman30943332015-05-10 09:47:48 -0700243 if (err)
244 break;
245 }
246
Scott Feldman464314e2015-10-08 19:23:18 -0700247done:
248 if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
249 err = 0;
250
Scott Feldman30943332015-05-10 09:47:48 -0700251 return err;
252}
253
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200254static int switchdev_port_attr_set_now(struct net_device *dev,
255 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700256{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200257 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700258 int err;
259
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200260 switchdev_trans_init(&trans);
261
Scott Feldman30943332015-05-10 09:47:48 -0700262 /* Phase I: prepare for attr set. Driver/device should fail
263 * here if there are going to be issues in the commit phase,
264 * such as lack of resources or support. The driver/device
265 * should reserve resources needed for the commit phase here,
266 * but should not commit the attr.
267 */
268
Jiri Pirkof623ab72015-09-24 10:02:49 +0200269 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200270 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700271 if (err) {
272 /* Prepare phase failed: abort the transaction. Any
273 * resources reserved in the prepare phase are
274 * released.
275 */
276
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200277 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200278 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700279
280 return err;
281 }
282
283 /* Phase II: commit attr set. This cannot fail as a fault
284 * of driver/device. If it does, it's a bug in the driver/device
285 * because the driver said everythings was OK in phase I.
286 */
287
Jiri Pirkof623ab72015-09-24 10:02:49 +0200288 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200289 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700290 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
291 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200292 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700293
294 return err;
295}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200296
297static void switchdev_port_attr_set_deferred(struct net_device *dev,
298 const void *data)
299{
300 const struct switchdev_attr *attr = data;
301 int err;
302
303 err = switchdev_port_attr_set_now(dev, attr);
304 if (err && err != -EOPNOTSUPP)
305 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
306 err, attr->id);
307}
308
309static int switchdev_port_attr_set_defer(struct net_device *dev,
310 const struct switchdev_attr *attr)
311{
312 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
313 switchdev_port_attr_set_deferred);
314}
315
316/**
317 * switchdev_port_attr_set - Set port attribute
318 *
319 * @dev: port device
320 * @attr: attribute to set
321 *
322 * Use a 2-phase prepare-commit transaction model to ensure
323 * system is not left in a partially updated state due to
324 * failure from driver/device.
325 *
326 * rtnl_lock must be held and must not be in atomic section,
327 * in case SWITCHDEV_F_DEFER flag is not set.
328 */
329int switchdev_port_attr_set(struct net_device *dev,
330 const struct switchdev_attr *attr)
331{
332 if (attr->flags & SWITCHDEV_F_DEFER)
333 return switchdev_port_attr_set_defer(dev, attr);
334 ASSERT_RTNL();
335 return switchdev_port_attr_set_now(dev, attr);
336}
Scott Feldman30943332015-05-10 09:47:48 -0700337EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
338
Scott Feldman22c1f672015-05-12 23:03:51 -0700339static int __switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200340 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200341 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700342{
343 const struct switchdev_ops *ops = dev->switchdev_ops;
344 struct net_device *lower_dev;
345 struct list_head *iter;
346 int err = -EOPNOTSUPP;
347
348 if (ops && ops->switchdev_port_obj_add)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200349 return ops->switchdev_port_obj_add(dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700350
351 /* Switch device port(s) may be stacked under
352 * bond/team/vlan dev, so recurse down to add object on
353 * each port.
354 */
355
356 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200357 err = __switchdev_port_obj_add(lower_dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700358 if (err)
359 break;
360 }
361
362 return err;
363}
364
Jiri Pirko4d429c52015-10-14 19:40:52 +0200365static int switchdev_port_obj_add_now(struct net_device *dev,
366 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700367{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200368 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700369 int err;
370
371 ASSERT_RTNL();
372
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200373 switchdev_trans_init(&trans);
374
Scott Feldman491d0f12015-05-10 09:47:52 -0700375 /* Phase I: prepare for obj add. Driver/device should fail
376 * here if there are going to be issues in the commit phase,
377 * such as lack of resources or support. The driver/device
378 * should reserve resources needed for the commit phase here,
379 * but should not commit the obj.
380 */
381
Jiri Pirkof623ab72015-09-24 10:02:49 +0200382 trans.ph_prepare = true;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200383 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700384 if (err) {
385 /* Prepare phase failed: abort the transaction. Any
386 * resources reserved in the prepare phase are
387 * released.
388 */
389
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200390 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200391 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700392
393 return err;
394 }
395
396 /* Phase II: commit obj add. This cannot fail as a fault
397 * of driver/device. If it does, it's a bug in the driver/device
398 * because the driver said everythings was OK in phase I.
399 */
400
Jiri Pirkof623ab72015-09-24 10:02:49 +0200401 trans.ph_prepare = false;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200402 err = __switchdev_port_obj_add(dev, obj, &trans);
403 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200404 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700405
406 return err;
407}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200408
409static void switchdev_port_obj_add_deferred(struct net_device *dev,
410 const void *data)
411{
412 const struct switchdev_obj *obj = data;
413 int err;
414
415 err = switchdev_port_obj_add_now(dev, obj);
416 if (err && err != -EOPNOTSUPP)
417 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
418 err, obj->id);
419}
420
421static int switchdev_port_obj_add_defer(struct net_device *dev,
422 const struct switchdev_obj *obj)
423{
424 return switchdev_deferred_enqueue(dev, obj, sizeof(*obj),
425 switchdev_port_obj_add_deferred);
426}
Scott Feldman491d0f12015-05-10 09:47:52 -0700427
428/**
Jiri Pirko4d429c52015-10-14 19:40:52 +0200429 * switchdev_port_obj_add - Add port object
Scott Feldman491d0f12015-05-10 09:47:52 -0700430 *
431 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400432 * @id: object ID
Jiri Pirko4d429c52015-10-14 19:40:52 +0200433 * @obj: object to add
434 *
435 * Use a 2-phase prepare-commit transaction model to ensure
436 * system is not left in a partially updated state due to
437 * failure from driver/device.
438 *
439 * rtnl_lock must be held and must not be in atomic section,
440 * in case SWITCHDEV_F_DEFER flag is not set.
Scott Feldman491d0f12015-05-10 09:47:52 -0700441 */
Jiri Pirko4d429c52015-10-14 19:40:52 +0200442int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200443 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700444{
Jiri Pirko4d429c52015-10-14 19:40:52 +0200445 if (obj->flags & SWITCHDEV_F_DEFER)
446 return switchdev_port_obj_add_defer(dev, obj);
447 ASSERT_RTNL();
448 return switchdev_port_obj_add_now(dev, obj);
449}
450EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
451
452static int switchdev_port_obj_del_now(struct net_device *dev,
453 const struct switchdev_obj *obj)
454{
Scott Feldman491d0f12015-05-10 09:47:52 -0700455 const struct switchdev_ops *ops = dev->switchdev_ops;
456 struct net_device *lower_dev;
457 struct list_head *iter;
458 int err = -EOPNOTSUPP;
459
460 if (ops && ops->switchdev_port_obj_del)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200461 return ops->switchdev_port_obj_del(dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700462
463 /* Switch device port(s) may be stacked under
464 * bond/team/vlan dev, so recurse down to delete object on
465 * each port.
466 */
467
468 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko4d429c52015-10-14 19:40:52 +0200469 err = switchdev_port_obj_del_now(lower_dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700470 if (err)
471 break;
472 }
473
474 return err;
475}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200476
477static void switchdev_port_obj_del_deferred(struct net_device *dev,
478 const void *data)
479{
480 const struct switchdev_obj *obj = data;
481 int err;
482
483 err = switchdev_port_obj_del_now(dev, obj);
484 if (err && err != -EOPNOTSUPP)
485 netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
486 err, obj->id);
487}
488
489static int switchdev_port_obj_del_defer(struct net_device *dev,
490 const struct switchdev_obj *obj)
491{
492 return switchdev_deferred_enqueue(dev, obj, sizeof(*obj),
493 switchdev_port_obj_del_deferred);
494}
495
496/**
497 * switchdev_port_obj_del - Delete port object
498 *
499 * @dev: port device
500 * @id: object ID
501 * @obj: object to delete
502 *
503 * rtnl_lock must be held and must not be in atomic section,
504 * in case SWITCHDEV_F_DEFER flag is not set.
505 */
506int switchdev_port_obj_del(struct net_device *dev,
507 const struct switchdev_obj *obj)
508{
509 if (obj->flags & SWITCHDEV_F_DEFER)
510 return switchdev_port_obj_del_defer(dev, obj);
511 ASSERT_RTNL();
512 return switchdev_port_obj_del_now(dev, obj);
513}
Scott Feldman491d0f12015-05-10 09:47:52 -0700514EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
515
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700516/**
517 * switchdev_port_obj_dump - Dump port objects
518 *
519 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400520 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700521 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400522 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700523 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200524int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200525 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700526{
527 const struct switchdev_ops *ops = dev->switchdev_ops;
528 struct net_device *lower_dev;
529 struct list_head *iter;
530 int err = -EOPNOTSUPP;
531
532 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200533 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700534
535 /* Switch device port(s) may be stacked under
536 * bond/team/vlan dev, so recurse down to dump objects on
537 * first port at bottom of stack.
538 */
539
540 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200541 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700542 break;
543 }
544
545 return err;
546}
547EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
548
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700549static DEFINE_MUTEX(switchdev_mutex);
550static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100551
552/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700553 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100554 * @nb: notifier_block
555 *
556 * Register switch device notifier. This should be used by code
557 * which needs to monitor events happening in particular device.
558 * Return values are same as for atomic_notifier_chain_register().
559 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700560int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100561{
562 int err;
563
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700564 mutex_lock(&switchdev_mutex);
565 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
566 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100567 return err;
568}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700569EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100570
571/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700572 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100573 * @nb: notifier_block
574 *
575 * Unregister switch device notifier.
576 * Return values are same as for atomic_notifier_chain_unregister().
577 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700578int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100579{
580 int err;
581
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700582 mutex_lock(&switchdev_mutex);
583 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
584 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100585 return err;
586}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700587EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100588
589/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700590 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100591 * @val: value passed unmodified to notifier function
592 * @dev: port device
593 * @info: notifier information data
594 *
595 * Call all network notifier blocks. This should be called by driver
596 * when it needs to propagate hardware event.
597 * Return values are same as for atomic_notifier_call_chain().
598 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700599int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
600 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100601{
602 int err;
603
604 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700605 mutex_lock(&switchdev_mutex);
606 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
607 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100608 return err;
609}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700610EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800611
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700612struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200613 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700614 struct sk_buff *skb;
615 u32 filter_mask;
616 u16 flags;
617 u16 begin;
618 u16 end;
619};
620
Vivien Didelote23b0022015-09-29 12:07:13 -0400621static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700622{
623 struct bridge_vlan_info vinfo;
624
625 vinfo.flags = dump->flags;
626
627 if (dump->begin == 0 && dump->end == 0) {
628 return 0;
629 } else if (dump->begin == dump->end) {
630 vinfo.vid = dump->begin;
631 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
632 sizeof(vinfo), &vinfo))
633 return -EMSGSIZE;
634 } else {
635 vinfo.vid = dump->begin;
636 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
637 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
638 sizeof(vinfo), &vinfo))
639 return -EMSGSIZE;
640 vinfo.vid = dump->end;
641 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
642 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
643 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
644 sizeof(vinfo), &vinfo))
645 return -EMSGSIZE;
646 }
647
648 return 0;
649}
650
Jiri Pirko648b4a92015-10-01 11:03:45 +0200651static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700652{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200653 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700654 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400655 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700656 int err = 0;
657
658 if (vlan->vid_begin > vlan->vid_end)
659 return -EINVAL;
660
661 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
662 dump->flags = vlan->flags;
663 for (dump->begin = dump->end = vlan->vid_begin;
664 dump->begin <= vlan->vid_end;
665 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400666 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700667 if (err)
668 return err;
669 }
670 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
671 if (dump->begin > vlan->vid_begin &&
672 dump->begin >= vlan->vid_end) {
673 if ((dump->begin - 1) == vlan->vid_end &&
674 dump->flags == vlan->flags) {
675 /* prepend */
676 dump->begin = vlan->vid_begin;
677 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400678 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700679 dump->flags = vlan->flags;
680 dump->begin = vlan->vid_begin;
681 dump->end = vlan->vid_end;
682 }
683 } else if (dump->end <= vlan->vid_begin &&
684 dump->end < vlan->vid_end) {
685 if ((dump->end + 1) == vlan->vid_begin &&
686 dump->flags == vlan->flags) {
687 /* append */
688 dump->end = vlan->vid_end;
689 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400690 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700691 dump->flags = vlan->flags;
692 dump->begin = vlan->vid_begin;
693 dump->end = vlan->vid_end;
694 }
695 } else {
696 err = -EINVAL;
697 }
698 }
699
700 return err;
701}
702
703static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
704 u32 filter_mask)
705{
706 struct switchdev_vlan_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200707 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700708 .skb = skb,
709 .filter_mask = filter_mask,
710 };
711 int err = 0;
712
713 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
714 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200715 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400716 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700717 if (err)
718 goto err_out;
719 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
720 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400721 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700722 }
723
724err_out:
725 return err == -EOPNOTSUPP ? 0 : err;
726}
727
Scott Feldman8793d0a2015-05-10 09:48:04 -0700728/**
729 * switchdev_port_bridge_getlink - Get bridge port attributes
730 *
731 * @dev: port device
732 *
733 * Called for SELF on rtnl_bridge_getlink to get bridge port
734 * attributes.
735 */
736int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
737 struct net_device *dev, u32 filter_mask,
738 int nlflags)
739{
740 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200741 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700742 };
743 u16 mode = BRIDGE_MODE_UNDEF;
744 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
745 int err;
746
747 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400748 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700749 return err;
750
751 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700752 attr.u.brport_flags, mask, nlflags,
753 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700754}
755EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
756
Scott Feldman47f83282015-05-10 09:47:56 -0700757static int switchdev_port_br_setflag(struct net_device *dev,
758 struct nlattr *nlattr,
759 unsigned long brport_flag)
760{
761 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200762 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700763 };
764 u8 flag = nla_get_u8(nlattr);
765 int err;
766
767 err = switchdev_port_attr_get(dev, &attr);
768 if (err)
769 return err;
770
771 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700772 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700773 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700774 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700775
776 return switchdev_port_attr_set(dev, &attr);
777}
778
779static const struct nla_policy
780switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
781 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
782 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
783 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
784 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
785 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
786 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
787 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
788 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
789 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
790 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
791};
792
793static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
794 struct nlattr *protinfo)
795{
796 struct nlattr *attr;
797 int rem;
798 int err;
799
800 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
801 switchdev_port_bridge_policy);
802 if (err)
803 return err;
804
805 nla_for_each_nested(attr, protinfo, rem) {
806 switch (nla_type(attr)) {
807 case IFLA_BRPORT_LEARNING:
808 err = switchdev_port_br_setflag(dev, attr,
809 BR_LEARNING);
810 break;
811 case IFLA_BRPORT_LEARNING_SYNC:
812 err = switchdev_port_br_setflag(dev, attr,
813 BR_LEARNING_SYNC);
814 break;
815 default:
816 err = -EOPNOTSUPP;
817 break;
818 }
819 if (err)
820 return err;
821 }
822
823 return 0;
824}
825
826static int switchdev_port_br_afspec(struct net_device *dev,
827 struct nlattr *afspec,
828 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200829 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700830{
831 struct nlattr *attr;
832 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200833 struct switchdev_obj_port_vlan vlan = {
834 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
835 };
Scott Feldman47f83282015-05-10 09:47:56 -0700836 int rem;
837 int err;
838
839 nla_for_each_nested(attr, afspec, rem) {
840 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
841 continue;
842 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
843 return -EINVAL;
844 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400845 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700846 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400847 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700848 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400849 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200850 /* don't allow range of pvids */
851 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
852 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700853 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400854 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700855 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400856 vlan.vid_end = vinfo->vid;
857 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700858 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200859 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700860 if (err)
861 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400862 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700863 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400864 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700865 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400866 vlan.vid_begin = vinfo->vid;
867 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200868 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700869 if (err)
870 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400871 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700872 }
873 }
874
875 return 0;
876}
877
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800878/**
Scott Feldman47f83282015-05-10 09:47:56 -0700879 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800880 *
881 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700882 * @nlh: netlink header
883 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800884 *
Scott Feldman47f83282015-05-10 09:47:56 -0700885 * Called for SELF on rtnl_bridge_setlink to set bridge port
886 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800887 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700888int switchdev_port_bridge_setlink(struct net_device *dev,
889 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800890{
Scott Feldman47f83282015-05-10 09:47:56 -0700891 struct nlattr *protinfo;
892 struct nlattr *afspec;
893 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800894
Scott Feldman47f83282015-05-10 09:47:56 -0700895 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
896 IFLA_PROTINFO);
897 if (protinfo) {
898 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
899 if (err)
900 return err;
901 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800902
Scott Feldman47f83282015-05-10 09:47:56 -0700903 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
904 IFLA_AF_SPEC);
905 if (afspec)
906 err = switchdev_port_br_afspec(dev, afspec,
907 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800908
Scott Feldman47f83282015-05-10 09:47:56 -0700909 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800910}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700911EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800912
913/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700914 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800915 *
916 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700917 * @nlh: netlink header
918 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800919 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700920 * Called for SELF on rtnl_bridge_dellink to set bridge port
921 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800922 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700923int switchdev_port_bridge_dellink(struct net_device *dev,
924 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800925{
Scott Feldman5c34e022015-05-10 09:48:00 -0700926 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800927
Scott Feldman5c34e022015-05-10 09:48:00 -0700928 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
929 IFLA_AF_SPEC);
930 if (afspec)
931 return switchdev_port_br_afspec(dev, afspec,
932 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800933
Scott Feldman5c34e022015-05-10 09:48:00 -0700934 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800935}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700936EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800937
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700938/**
939 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
940 *
941 * @ndmsg: netlink hdr
942 * @nlattr: netlink attributes
943 * @dev: port device
944 * @addr: MAC address to add
945 * @vid: VLAN to add
946 *
947 * Add FDB entry to switch device.
948 */
949int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
950 struct net_device *dev, const unsigned char *addr,
951 u16 vid, u16 nlm_flags)
952{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200953 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200954 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400955 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700956 };
957
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200958 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200959 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700960}
961EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
962
963/**
964 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
965 *
966 * @ndmsg: netlink hdr
967 * @nlattr: netlink attributes
968 * @dev: port device
969 * @addr: MAC address to delete
970 * @vid: VLAN to delete
971 *
972 * Delete FDB entry from switch device.
973 */
974int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
975 struct net_device *dev, const unsigned char *addr,
976 u16 vid)
977{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200978 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200979 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400980 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700981 };
982
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200983 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200984 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700985}
986EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
987
988struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200989 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b2015-09-29 12:07:14 -0400990 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700991 struct sk_buff *skb;
992 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700993 int idx;
994};
995
Jiri Pirko648b4a92015-10-01 11:03:45 +0200996static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700997{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200998 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700999 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001000 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001001 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1002 u32 seq = dump->cb->nlh->nlmsg_seq;
1003 struct nlmsghdr *nlh;
1004 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001005
1006 if (dump->idx < dump->cb->args[0])
1007 goto skip;
1008
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001009 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1010 sizeof(*ndm), NLM_F_MULTI);
1011 if (!nlh)
1012 return -EMSGSIZE;
1013
1014 ndm = nlmsg_data(nlh);
1015 ndm->ndm_family = AF_BRIDGE;
1016 ndm->ndm_pad1 = 0;
1017 ndm->ndm_pad2 = 0;
1018 ndm->ndm_flags = NTF_SELF;
1019 ndm->ndm_type = 0;
Vivien Didelote02a06b2015-09-29 12:07:14 -04001020 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001021 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001022
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001023 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001024 goto nla_put_failure;
1025
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001026 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001027 goto nla_put_failure;
1028
1029 nlmsg_end(dump->skb, nlh);
1030
1031skip:
1032 dump->idx++;
1033 return 0;
1034
1035nla_put_failure:
1036 nlmsg_cancel(dump->skb, nlh);
1037 return -EMSGSIZE;
1038}
1039
1040/**
1041 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
1042 *
1043 * @skb: netlink skb
1044 * @cb: netlink callback
1045 * @dev: port device
1046 * @filter_dev: filter device
1047 * @idx:
1048 *
1049 * Delete FDB entry from switch device.
1050 */
1051int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1052 struct net_device *dev,
1053 struct net_device *filter_dev, int idx)
1054{
1055 struct switchdev_fdb_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001056 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b2015-09-29 12:07:14 -04001057 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001058 .skb = skb,
1059 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001060 .idx = idx,
1061 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001062
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001063 switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001064 return dump.idx;
1065}
1066EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
1067
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001068static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001069{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001070 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001071 struct net_device *lower_dev;
1072 struct net_device *port_dev;
1073 struct list_head *iter;
1074
1075 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001076 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001077 */
1078
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001079 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001080 return dev;
1081
1082 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001083 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001084 if (port_dev)
1085 return port_dev;
1086 }
1087
1088 return NULL;
1089}
1090
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001091static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001092{
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001093 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001094 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001095 };
1096 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001097 struct net_device *dev = NULL;
1098 int nhsel;
1099
1100 /* For this route, all nexthop devs must be on the same switch. */
1101
1102 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1103 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1104
1105 if (!nh->nh_dev)
1106 return NULL;
1107
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001108 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001109 if (!dev)
1110 return NULL;
1111
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001112 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001113 return NULL;
1114
Scott Feldmand754f982015-07-18 18:24:49 -07001115 if (nhsel > 0 &&
1116 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001117 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001118
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001119 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001120 }
1121
1122 return dev;
1123}
1124
Scott Feldman5e8d9042015-03-05 21:21:15 -08001125/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001126 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001127 *
1128 * @dst: route's IPv4 destination address
1129 * @dst_len: destination address length (prefix length)
1130 * @fi: route FIB info structure
1131 * @tos: route TOS
1132 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001133 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001134 * @tb_id: route table ID
1135 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001136 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001137 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001138int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1139 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001140{
Vivien Didelotab069002015-09-29 12:07:17 -04001141 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001142 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001143 .dst = dst,
1144 .dst_len = dst_len,
Vivien Didelotab069002015-09-29 12:07:17 -04001145 .tos = tos,
1146 .type = type,
1147 .nlflags = nlflags,
1148 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001149 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001150 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001151 int err = 0;
1152
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001153 memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi));
1154
Scott Feldman8e05fd72015-03-05 21:21:19 -08001155 /* Don't offload route if using custom ip rules or if
1156 * IPv4 FIB offloading has been disabled completely.
1157 */
1158
Scott Feldmane1315db2015-03-06 01:14:36 -08001159#ifdef CONFIG_IP_MULTIPLE_TABLES
1160 if (fi->fib_net->ipv4.fib_has_custom_rules)
1161 return 0;
1162#endif
1163
1164 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001165 return 0;
1166
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001167 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001168 if (!dev)
1169 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001170
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001171 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001172 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001173 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001174
Scott Feldmanaf201f72015-06-10 17:04:49 -07001175 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001176}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001177EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001178
1179/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001180 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001181 *
1182 * @dst: route's IPv4 destination address
1183 * @dst_len: destination address length (prefix length)
1184 * @fi: route FIB info structure
1185 * @tos: route TOS
1186 * @type: route type
1187 * @tb_id: route table ID
1188 *
1189 * Delete IPv4 route entry from switch device.
1190 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001191int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1192 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001193{
Vivien Didelotab069002015-09-29 12:07:17 -04001194 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001195 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001196 .dst = dst,
1197 .dst_len = dst_len,
Vivien Didelotab069002015-09-29 12:07:17 -04001198 .tos = tos,
1199 .type = type,
1200 .nlflags = 0,
1201 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001202 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001203 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001204 int err = 0;
1205
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001206 memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi));
1207
Roopa Prabhueea39942015-05-13 21:17:41 -07001208 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001209 return 0;
1210
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001211 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001212 if (!dev)
1213 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001214
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001215 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001216 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001217 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001218
Scott Feldmanaf201f72015-06-10 17:04:49 -07001219 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001220}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001221EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001222
1223/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001224 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001225 *
1226 * @fi: route FIB info structure
1227 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001228void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001229{
1230 /* There was a problem installing this route to the offload
1231 * device. For now, until we come up with more refined
1232 * policy handling, abruptly end IPv4 fib offloading for
1233 * for entire net by flushing offload device(s) of all
1234 * IPv4 routes, and mark IPv4 fib offloading broken from
1235 * this point forward.
1236 */
1237
1238 fib_flush_external(fi->fib_net);
1239 fi->fib_net->ipv4.fib_offload_disabled = true;
1240}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001241EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001242
1243static bool switchdev_port_same_parent_id(struct net_device *a,
1244 struct net_device *b)
1245{
1246 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001247 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001248 .flags = SWITCHDEV_F_NO_RECURSE,
1249 };
1250 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001251 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001252 .flags = SWITCHDEV_F_NO_RECURSE,
1253 };
1254
1255 if (switchdev_port_attr_get(a, &a_attr) ||
1256 switchdev_port_attr_get(b, &b_attr))
1257 return false;
1258
1259 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1260}
1261
1262static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1263 struct net_device *group_dev)
1264{
1265 struct net_device *lower_dev;
1266 struct list_head *iter;
1267
1268 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1269 if (lower_dev == dev)
1270 continue;
1271 if (switchdev_port_same_parent_id(dev, lower_dev))
1272 return lower_dev->offload_fwd_mark;
1273 return switchdev_port_fwd_mark_get(dev, lower_dev);
1274 }
1275
1276 return dev->ifindex;
1277}
1278
1279static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1280 u32 old_mark, u32 *reset_mark)
1281{
1282 struct net_device *lower_dev;
1283 struct list_head *iter;
1284
1285 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1286 if (lower_dev->offload_fwd_mark == old_mark) {
1287 if (!*reset_mark)
1288 *reset_mark = lower_dev->ifindex;
1289 lower_dev->offload_fwd_mark = *reset_mark;
1290 }
1291 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1292 }
1293}
1294
1295/**
1296 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1297 *
1298 * @dev: port device
1299 * @group_dev: containing device
1300 * @joining: true if dev is joining group; false if leaving group
1301 *
1302 * An ungrouped port's offload mark is just its ifindex. A grouped
1303 * port's (member of a bridge, for example) offload mark is the ifindex
1304 * of one of the ports in the group with the same parent (switch) ID.
1305 * Ports on the same device in the same group will have the same mark.
1306 *
1307 * Example:
1308 *
1309 * br0 ifindex=9
1310 * sw1p1 ifindex=2 mark=2
1311 * sw1p2 ifindex=3 mark=2
1312 * sw2p1 ifindex=4 mark=5
1313 * sw2p2 ifindex=5 mark=5
1314 *
1315 * If sw2p2 leaves the bridge, we'll have:
1316 *
1317 * br0 ifindex=9
1318 * sw1p1 ifindex=2 mark=2
1319 * sw1p2 ifindex=3 mark=2
1320 * sw2p1 ifindex=4 mark=4
1321 * sw2p2 ifindex=5 mark=5
1322 */
1323void switchdev_port_fwd_mark_set(struct net_device *dev,
1324 struct net_device *group_dev,
1325 bool joining)
1326{
1327 u32 mark = dev->ifindex;
1328 u32 reset_mark = 0;
1329
1330 if (group_dev && joining) {
1331 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1332 } else if (group_dev && !joining) {
1333 if (dev->offload_fwd_mark == mark)
1334 /* Ohoh, this port was the mark reference port,
1335 * but it's leaving the group, so reset the
1336 * mark for the remaining ports in the group.
1337 */
1338 switchdev_port_fwd_mark_reset(group_dev, mark,
1339 &reset_mark);
1340 }
1341
1342 dev->offload_fwd_mark = mark;
1343}
1344EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);