blob: 73e3895175cf7e1951a5a168ba4eff5ef69349d2 [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
Jiri Pirko771acac2015-10-14 19:40:55 +0200523 *
524 * rtnl_lock must be held.
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700525 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200526int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200527 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700528{
529 const struct switchdev_ops *ops = dev->switchdev_ops;
530 struct net_device *lower_dev;
531 struct list_head *iter;
532 int err = -EOPNOTSUPP;
533
Jiri Pirko771acac2015-10-14 19:40:55 +0200534 ASSERT_RTNL();
535
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700536 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200537 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700538
539 /* Switch device port(s) may be stacked under
540 * bond/team/vlan dev, so recurse down to dump objects on
541 * first port at bottom of stack.
542 */
543
544 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200545 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700546 break;
547 }
548
549 return err;
550}
551EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
552
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700553static DEFINE_MUTEX(switchdev_mutex);
554static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100555
556/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700557 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100558 * @nb: notifier_block
559 *
560 * Register switch device notifier. This should be used by code
561 * which needs to monitor events happening in particular device.
562 * Return values are same as for atomic_notifier_chain_register().
563 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700564int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100565{
566 int err;
567
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700568 mutex_lock(&switchdev_mutex);
569 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
570 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100571 return err;
572}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700573EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100574
575/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700576 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100577 * @nb: notifier_block
578 *
579 * Unregister switch device notifier.
580 * Return values are same as for atomic_notifier_chain_unregister().
581 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700582int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100583{
584 int err;
585
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700586 mutex_lock(&switchdev_mutex);
587 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
588 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100589 return err;
590}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700591EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100592
593/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700594 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100595 * @val: value passed unmodified to notifier function
596 * @dev: port device
597 * @info: notifier information data
598 *
599 * Call all network notifier blocks. This should be called by driver
600 * when it needs to propagate hardware event.
601 * Return values are same as for atomic_notifier_call_chain().
602 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700603int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
604 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100605{
606 int err;
607
608 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700609 mutex_lock(&switchdev_mutex);
610 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
611 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100612 return err;
613}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700614EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800615
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700616struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200617 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700618 struct sk_buff *skb;
619 u32 filter_mask;
620 u16 flags;
621 u16 begin;
622 u16 end;
623};
624
Vivien Didelote23b0022015-09-29 12:07:13 -0400625static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700626{
627 struct bridge_vlan_info vinfo;
628
629 vinfo.flags = dump->flags;
630
631 if (dump->begin == 0 && dump->end == 0) {
632 return 0;
633 } else if (dump->begin == dump->end) {
634 vinfo.vid = dump->begin;
635 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
636 sizeof(vinfo), &vinfo))
637 return -EMSGSIZE;
638 } else {
639 vinfo.vid = dump->begin;
640 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
641 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
642 sizeof(vinfo), &vinfo))
643 return -EMSGSIZE;
644 vinfo.vid = dump->end;
645 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
646 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
647 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
648 sizeof(vinfo), &vinfo))
649 return -EMSGSIZE;
650 }
651
652 return 0;
653}
654
Jiri Pirko648b4a92015-10-01 11:03:45 +0200655static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700656{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200657 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700658 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400659 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700660 int err = 0;
661
662 if (vlan->vid_begin > vlan->vid_end)
663 return -EINVAL;
664
665 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
666 dump->flags = vlan->flags;
667 for (dump->begin = dump->end = vlan->vid_begin;
668 dump->begin <= vlan->vid_end;
669 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400670 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700671 if (err)
672 return err;
673 }
674 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
675 if (dump->begin > vlan->vid_begin &&
676 dump->begin >= vlan->vid_end) {
677 if ((dump->begin - 1) == vlan->vid_end &&
678 dump->flags == vlan->flags) {
679 /* prepend */
680 dump->begin = vlan->vid_begin;
681 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400682 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700683 dump->flags = vlan->flags;
684 dump->begin = vlan->vid_begin;
685 dump->end = vlan->vid_end;
686 }
687 } else if (dump->end <= vlan->vid_begin &&
688 dump->end < vlan->vid_end) {
689 if ((dump->end + 1) == vlan->vid_begin &&
690 dump->flags == vlan->flags) {
691 /* append */
692 dump->end = vlan->vid_end;
693 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400694 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700695 dump->flags = vlan->flags;
696 dump->begin = vlan->vid_begin;
697 dump->end = vlan->vid_end;
698 }
699 } else {
700 err = -EINVAL;
701 }
702 }
703
704 return err;
705}
706
707static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
708 u32 filter_mask)
709{
710 struct switchdev_vlan_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200711 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700712 .skb = skb,
713 .filter_mask = filter_mask,
714 };
715 int err = 0;
716
717 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
718 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200719 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400720 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700721 if (err)
722 goto err_out;
723 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
724 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400725 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700726 }
727
728err_out:
729 return err == -EOPNOTSUPP ? 0 : err;
730}
731
Scott Feldman8793d0a2015-05-10 09:48:04 -0700732/**
733 * switchdev_port_bridge_getlink - Get bridge port attributes
734 *
735 * @dev: port device
736 *
737 * Called for SELF on rtnl_bridge_getlink to get bridge port
738 * attributes.
739 */
740int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
741 struct net_device *dev, u32 filter_mask,
742 int nlflags)
743{
744 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200745 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700746 };
747 u16 mode = BRIDGE_MODE_UNDEF;
748 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
749 int err;
750
751 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400752 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700753 return err;
754
755 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700756 attr.u.brport_flags, mask, nlflags,
757 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700758}
759EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
760
Scott Feldman47f83282015-05-10 09:47:56 -0700761static int switchdev_port_br_setflag(struct net_device *dev,
762 struct nlattr *nlattr,
763 unsigned long brport_flag)
764{
765 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200766 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700767 };
768 u8 flag = nla_get_u8(nlattr);
769 int err;
770
771 err = switchdev_port_attr_get(dev, &attr);
772 if (err)
773 return err;
774
775 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700776 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700777 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700778 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700779
780 return switchdev_port_attr_set(dev, &attr);
781}
782
783static const struct nla_policy
784switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
785 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
786 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
787 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
788 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
789 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
790 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
791 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
792 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
793 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
794 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
795};
796
797static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
798 struct nlattr *protinfo)
799{
800 struct nlattr *attr;
801 int rem;
802 int err;
803
804 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
805 switchdev_port_bridge_policy);
806 if (err)
807 return err;
808
809 nla_for_each_nested(attr, protinfo, rem) {
810 switch (nla_type(attr)) {
811 case IFLA_BRPORT_LEARNING:
812 err = switchdev_port_br_setflag(dev, attr,
813 BR_LEARNING);
814 break;
815 case IFLA_BRPORT_LEARNING_SYNC:
816 err = switchdev_port_br_setflag(dev, attr,
817 BR_LEARNING_SYNC);
818 break;
819 default:
820 err = -EOPNOTSUPP;
821 break;
822 }
823 if (err)
824 return err;
825 }
826
827 return 0;
828}
829
830static int switchdev_port_br_afspec(struct net_device *dev,
831 struct nlattr *afspec,
832 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200833 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700834{
835 struct nlattr *attr;
836 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200837 struct switchdev_obj_port_vlan vlan = {
838 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
839 };
Scott Feldman47f83282015-05-10 09:47:56 -0700840 int rem;
841 int err;
842
843 nla_for_each_nested(attr, afspec, rem) {
844 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
845 continue;
846 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
847 return -EINVAL;
848 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400849 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700850 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400851 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700852 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400853 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200854 /* don't allow range of pvids */
855 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
856 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700857 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400858 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700859 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400860 vlan.vid_end = vinfo->vid;
861 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700862 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200863 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700864 if (err)
865 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400866 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700867 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400868 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700869 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400870 vlan.vid_begin = vinfo->vid;
871 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200872 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700873 if (err)
874 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400875 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700876 }
877 }
878
879 return 0;
880}
881
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800882/**
Scott Feldman47f83282015-05-10 09:47:56 -0700883 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800884 *
885 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700886 * @nlh: netlink header
887 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800888 *
Scott Feldman47f83282015-05-10 09:47:56 -0700889 * Called for SELF on rtnl_bridge_setlink to set bridge port
890 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800891 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700892int switchdev_port_bridge_setlink(struct net_device *dev,
893 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800894{
Scott Feldman47f83282015-05-10 09:47:56 -0700895 struct nlattr *protinfo;
896 struct nlattr *afspec;
897 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800898
Scott Feldman47f83282015-05-10 09:47:56 -0700899 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
900 IFLA_PROTINFO);
901 if (protinfo) {
902 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
903 if (err)
904 return err;
905 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800906
Scott Feldman47f83282015-05-10 09:47:56 -0700907 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
908 IFLA_AF_SPEC);
909 if (afspec)
910 err = switchdev_port_br_afspec(dev, afspec,
911 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800912
Scott Feldman47f83282015-05-10 09:47:56 -0700913 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800914}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700915EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800916
917/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700918 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800919 *
920 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700921 * @nlh: netlink header
922 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800923 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700924 * Called for SELF on rtnl_bridge_dellink to set bridge port
925 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800926 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700927int switchdev_port_bridge_dellink(struct net_device *dev,
928 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800929{
Scott Feldman5c34e022015-05-10 09:48:00 -0700930 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800931
Scott Feldman5c34e022015-05-10 09:48:00 -0700932 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
933 IFLA_AF_SPEC);
934 if (afspec)
935 return switchdev_port_br_afspec(dev, afspec,
936 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800937
Scott Feldman5c34e022015-05-10 09:48:00 -0700938 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800939}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700940EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800941
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700942/**
943 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
944 *
945 * @ndmsg: netlink hdr
946 * @nlattr: netlink attributes
947 * @dev: port device
948 * @addr: MAC address to add
949 * @vid: VLAN to add
950 *
951 * Add FDB entry to switch device.
952 */
953int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
954 struct net_device *dev, const unsigned char *addr,
955 u16 vid, u16 nlm_flags)
956{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200957 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200958 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400959 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700960 };
961
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200962 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200963 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700964}
965EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
966
967/**
968 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
969 *
970 * @ndmsg: netlink hdr
971 * @nlattr: netlink attributes
972 * @dev: port device
973 * @addr: MAC address to delete
974 * @vid: VLAN to delete
975 *
976 * Delete FDB entry from switch device.
977 */
978int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
979 struct net_device *dev, const unsigned char *addr,
980 u16 vid)
981{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200982 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200983 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400984 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700985 };
986
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200987 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200988 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700989}
990EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
991
992struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200993 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400994 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700995 struct sk_buff *skb;
996 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700997 int idx;
998};
999
Jiri Pirko648b4a92015-10-01 11:03:45 +02001000static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001001{
Jiri Pirko648b4a92015-10-01 11:03:45 +02001002 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001003 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001004 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001005 u32 portid = NETLINK_CB(dump->cb->skb).portid;
1006 u32 seq = dump->cb->nlh->nlmsg_seq;
1007 struct nlmsghdr *nlh;
1008 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001009
1010 if (dump->idx < dump->cb->args[0])
1011 goto skip;
1012
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001013 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1014 sizeof(*ndm), NLM_F_MULTI);
1015 if (!nlh)
1016 return -EMSGSIZE;
1017
1018 ndm = nlmsg_data(nlh);
1019 ndm->ndm_family = AF_BRIDGE;
1020 ndm->ndm_pad1 = 0;
1021 ndm->ndm_pad2 = 0;
1022 ndm->ndm_flags = NTF_SELF;
1023 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -04001024 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001025 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001026
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001027 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001028 goto nla_put_failure;
1029
Vivien Didelot25f07ad2015-09-29 12:07:16 -04001030 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001031 goto nla_put_failure;
1032
1033 nlmsg_end(dump->skb, nlh);
1034
1035skip:
1036 dump->idx++;
1037 return 0;
1038
1039nla_put_failure:
1040 nlmsg_cancel(dump->skb, nlh);
1041 return -EMSGSIZE;
1042}
1043
1044/**
1045 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
1046 *
1047 * @skb: netlink skb
1048 * @cb: netlink callback
1049 * @dev: port device
1050 * @filter_dev: filter device
1051 * @idx:
1052 *
1053 * Delete FDB entry from switch device.
1054 */
1055int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1056 struct net_device *dev,
1057 struct net_device *filter_dev, int idx)
1058{
1059 struct switchdev_fdb_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001060 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b22015-09-29 12:07:14 -04001061 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001062 .skb = skb,
1063 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001064 .idx = idx,
1065 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001066
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001067 switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001068 return dump.idx;
1069}
1070EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
1071
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001072static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001073{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001074 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001075 struct net_device *lower_dev;
1076 struct net_device *port_dev;
1077 struct list_head *iter;
1078
1079 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001080 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001081 */
1082
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001083 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001084 return dev;
1085
1086 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001087 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001088 if (port_dev)
1089 return port_dev;
1090 }
1091
1092 return NULL;
1093}
1094
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001095static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001096{
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001097 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001098 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001099 };
1100 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001101 struct net_device *dev = NULL;
1102 int nhsel;
1103
Jiri Pirko771acac2015-10-14 19:40:55 +02001104 ASSERT_RTNL();
1105
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001106 /* For this route, all nexthop devs must be on the same switch. */
1107
1108 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1109 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1110
1111 if (!nh->nh_dev)
1112 return NULL;
1113
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001114 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001115 if (!dev)
1116 return NULL;
1117
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001118 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001119 return NULL;
1120
Scott Feldmand754f982015-07-18 18:24:49 -07001121 if (nhsel > 0 &&
1122 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001123 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001124
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001125 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001126 }
1127
1128 return dev;
1129}
1130
Scott Feldman5e8d9042015-03-05 21:21:15 -08001131/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001132 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001133 *
1134 * @dst: route's IPv4 destination address
1135 * @dst_len: destination address length (prefix length)
1136 * @fi: route FIB info structure
1137 * @tos: route TOS
1138 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001139 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001140 * @tb_id: route table ID
1141 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001142 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001143 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001144int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1145 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001146{
Vivien Didelotab069002015-09-29 12:07:17 -04001147 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001148 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001149 .dst = dst,
1150 .dst_len = dst_len,
Vivien Didelotab069002015-09-29 12:07:17 -04001151 .tos = tos,
1152 .type = type,
1153 .nlflags = nlflags,
1154 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001155 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001156 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001157 int err = 0;
1158
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001159 memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi));
1160
Scott Feldman8e05fd72015-03-05 21:21:19 -08001161 /* Don't offload route if using custom ip rules or if
1162 * IPv4 FIB offloading has been disabled completely.
1163 */
1164
Scott Feldmane1315db2015-03-06 01:14:36 -08001165#ifdef CONFIG_IP_MULTIPLE_TABLES
1166 if (fi->fib_net->ipv4.fib_has_custom_rules)
1167 return 0;
1168#endif
1169
1170 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001171 return 0;
1172
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001173 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001174 if (!dev)
1175 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001176
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001177 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001178 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001179 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001180
Scott Feldmanaf201f72015-06-10 17:04:49 -07001181 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001182}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001183EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001184
1185/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001186 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001187 *
1188 * @dst: route's IPv4 destination address
1189 * @dst_len: destination address length (prefix length)
1190 * @fi: route FIB info structure
1191 * @tos: route TOS
1192 * @type: route type
1193 * @tb_id: route table ID
1194 *
1195 * Delete IPv4 route entry from switch device.
1196 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001197int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1198 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001199{
Vivien Didelotab069002015-09-29 12:07:17 -04001200 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001201 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001202 .dst = dst,
1203 .dst_len = dst_len,
Vivien Didelotab069002015-09-29 12:07:17 -04001204 .tos = tos,
1205 .type = type,
1206 .nlflags = 0,
1207 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001208 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001209 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001210 int err = 0;
1211
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001212 memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi));
1213
Roopa Prabhueea39942015-05-13 21:17:41 -07001214 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001215 return 0;
1216
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001217 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001218 if (!dev)
1219 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001220
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001221 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001222 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001223 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001224
Scott Feldmanaf201f72015-06-10 17:04:49 -07001225 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001226}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001227EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001228
1229/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001230 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001231 *
1232 * @fi: route FIB info structure
1233 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001234void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001235{
1236 /* There was a problem installing this route to the offload
1237 * device. For now, until we come up with more refined
1238 * policy handling, abruptly end IPv4 fib offloading for
1239 * for entire net by flushing offload device(s) of all
1240 * IPv4 routes, and mark IPv4 fib offloading broken from
1241 * this point forward.
1242 */
1243
1244 fib_flush_external(fi->fib_net);
1245 fi->fib_net->ipv4.fib_offload_disabled = true;
1246}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001247EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001248
1249static bool switchdev_port_same_parent_id(struct net_device *a,
1250 struct net_device *b)
1251{
1252 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001253 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001254 .flags = SWITCHDEV_F_NO_RECURSE,
1255 };
1256 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001257 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001258 .flags = SWITCHDEV_F_NO_RECURSE,
1259 };
1260
1261 if (switchdev_port_attr_get(a, &a_attr) ||
1262 switchdev_port_attr_get(b, &b_attr))
1263 return false;
1264
1265 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1266}
1267
1268static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1269 struct net_device *group_dev)
1270{
1271 struct net_device *lower_dev;
1272 struct list_head *iter;
1273
1274 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1275 if (lower_dev == dev)
1276 continue;
1277 if (switchdev_port_same_parent_id(dev, lower_dev))
1278 return lower_dev->offload_fwd_mark;
1279 return switchdev_port_fwd_mark_get(dev, lower_dev);
1280 }
1281
1282 return dev->ifindex;
1283}
1284
1285static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1286 u32 old_mark, u32 *reset_mark)
1287{
1288 struct net_device *lower_dev;
1289 struct list_head *iter;
1290
1291 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1292 if (lower_dev->offload_fwd_mark == old_mark) {
1293 if (!*reset_mark)
1294 *reset_mark = lower_dev->ifindex;
1295 lower_dev->offload_fwd_mark = *reset_mark;
1296 }
1297 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1298 }
1299}
1300
1301/**
1302 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1303 *
1304 * @dev: port device
1305 * @group_dev: containing device
1306 * @joining: true if dev is joining group; false if leaving group
1307 *
1308 * An ungrouped port's offload mark is just its ifindex. A grouped
1309 * port's (member of a bridge, for example) offload mark is the ifindex
1310 * of one of the ports in the group with the same parent (switch) ID.
1311 * Ports on the same device in the same group will have the same mark.
1312 *
1313 * Example:
1314 *
1315 * br0 ifindex=9
1316 * sw1p1 ifindex=2 mark=2
1317 * sw1p2 ifindex=3 mark=2
1318 * sw2p1 ifindex=4 mark=5
1319 * sw2p2 ifindex=5 mark=5
1320 *
1321 * If sw2p2 leaves the bridge, we'll have:
1322 *
1323 * br0 ifindex=9
1324 * sw1p1 ifindex=2 mark=2
1325 * sw1p2 ifindex=3 mark=2
1326 * sw2p1 ifindex=4 mark=4
1327 * sw2p2 ifindex=5 mark=5
1328 */
1329void switchdev_port_fwd_mark_set(struct net_device *dev,
1330 struct net_device *group_dev,
1331 bool joining)
1332{
1333 u32 mark = dev->ifindex;
1334 u32 reset_mark = 0;
1335
Jiri Pirko771acac2015-10-14 19:40:55 +02001336 if (group_dev) {
1337 ASSERT_RTNL();
1338 if (joining)
1339 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1340 else if (dev->offload_fwd_mark == mark)
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001341 /* Ohoh, this port was the mark reference port,
1342 * but it's leaving the group, so reset the
1343 * mark for the remaining ports in the group.
1344 */
1345 switchdev_port_fwd_mark_reset(group_dev, mark,
1346 &reset_mark);
1347 }
1348
1349 dev->offload_fwd_mark = mark;
1350}
1351EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);