blob: 23b4e5b347dca94958abc0e1d6e7e78f354b97da [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>
Scott Feldman47f83282015-05-10 09:47:56 -070018#include <linux/if_bridge.h>
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020019#include <linux/list.h>
Jiri Pirko793f4012015-10-14 19:40:48 +020020#include <linux/workqueue.h>
Scott Feldman5e8d9042015-03-05 21:21:15 -080021#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010022#include <net/switchdev.h>
23
24/**
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020025 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
26 *
27 * @trans: transaction
28 * @data: pointer to data being queued
29 * @destructor: data destructor
30 * @tritem: transaction item being queued
31 *
32 * Enqeueue data item to transaction queue. tritem is typically placed in
33 * cointainter pointed at by data pointer. Destructor is called on
34 * transaction abort and after successful commit phase in case
35 * the caller did not dequeue the item before.
36 */
37void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
38 void *data, void (*destructor)(void const *),
39 struct switchdev_trans_item *tritem)
40{
41 tritem->data = data;
42 tritem->destructor = destructor;
43 list_add_tail(&tritem->list, &trans->item_list);
44}
45EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
46
47static struct switchdev_trans_item *
48__switchdev_trans_item_dequeue(struct switchdev_trans *trans)
49{
50 struct switchdev_trans_item *tritem;
51
52 if (list_empty(&trans->item_list))
53 return NULL;
54 tritem = list_first_entry(&trans->item_list,
55 struct switchdev_trans_item, list);
56 list_del(&tritem->list);
57 return tritem;
58}
59
60/**
61 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
62 *
63 * @trans: transaction
64 */
65void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
66{
67 struct switchdev_trans_item *tritem;
68
69 tritem = __switchdev_trans_item_dequeue(trans);
70 BUG_ON(!tritem);
71 return tritem->data;
72}
73EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
74
75static void switchdev_trans_init(struct switchdev_trans *trans)
76{
77 INIT_LIST_HEAD(&trans->item_list);
78}
79
80static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
81{
82 struct switchdev_trans_item *tritem;
83
84 while ((tritem = __switchdev_trans_item_dequeue(trans)))
85 tritem->destructor(tritem->data);
86}
87
88static void switchdev_trans_items_warn_destroy(struct net_device *dev,
89 struct switchdev_trans *trans)
90{
91 WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
92 dev->name);
93 switchdev_trans_items_destroy(trans);
94}
95
Jiri Pirko793f4012015-10-14 19:40:48 +020096static LIST_HEAD(deferred);
97static DEFINE_SPINLOCK(deferred_lock);
98
99typedef void switchdev_deferred_func_t(struct net_device *dev,
100 const void *data);
101
102struct switchdev_deferred_item {
103 struct list_head list;
104 struct net_device *dev;
105 switchdev_deferred_func_t *func;
106 unsigned long data[0];
107};
108
109static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
110{
111 struct switchdev_deferred_item *dfitem;
112
113 spin_lock_bh(&deferred_lock);
114 if (list_empty(&deferred)) {
115 dfitem = NULL;
116 goto unlock;
117 }
118 dfitem = list_first_entry(&deferred,
119 struct switchdev_deferred_item, list);
120 list_del(&dfitem->list);
121unlock:
122 spin_unlock_bh(&deferred_lock);
123 return dfitem;
124}
125
126/**
127 * switchdev_deferred_process - Process ops in deferred queue
128 *
129 * Called to flush the ops currently queued in deferred ops queue.
130 * rtnl_lock must be held.
131 */
132void switchdev_deferred_process(void)
133{
134 struct switchdev_deferred_item *dfitem;
135
136 ASSERT_RTNL();
137
138 while ((dfitem = switchdev_deferred_dequeue())) {
139 dfitem->func(dfitem->dev, dfitem->data);
140 dev_put(dfitem->dev);
141 kfree(dfitem);
142 }
143}
144EXPORT_SYMBOL_GPL(switchdev_deferred_process);
145
146static void switchdev_deferred_process_work(struct work_struct *work)
147{
148 rtnl_lock();
149 switchdev_deferred_process();
150 rtnl_unlock();
151}
152
153static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
154
155static int switchdev_deferred_enqueue(struct net_device *dev,
156 const void *data, size_t data_len,
157 switchdev_deferred_func_t *func)
158{
159 struct switchdev_deferred_item *dfitem;
160
161 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
162 if (!dfitem)
163 return -ENOMEM;
164 dfitem->dev = dev;
165 dfitem->func = func;
166 memcpy(dfitem->data, data, data_len);
167 dev_hold(dev);
168 spin_lock_bh(&deferred_lock);
169 list_add_tail(&dfitem->list, &deferred);
170 spin_unlock_bh(&deferred_lock);
171 schedule_work(&deferred_process_work);
172 return 0;
173}
174
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200175/**
Scott Feldman30943332015-05-10 09:47:48 -0700176 * switchdev_port_attr_get - Get port attribute
177 *
178 * @dev: port device
179 * @attr: attribute to get
180 */
181int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
182{
183 const struct switchdev_ops *ops = dev->switchdev_ops;
184 struct net_device *lower_dev;
185 struct list_head *iter;
186 struct switchdev_attr first = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200187 .id = SWITCHDEV_ATTR_ID_UNDEFINED
Scott Feldman30943332015-05-10 09:47:48 -0700188 };
189 int err = -EOPNOTSUPP;
190
191 if (ops && ops->switchdev_port_attr_get)
192 return ops->switchdev_port_attr_get(dev, attr);
193
194 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
195 return err;
196
197 /* Switch device port(s) may be stacked under
198 * bond/team/vlan dev, so recurse down to get attr on
199 * each port. Return -ENODATA if attr values don't
200 * compare across ports.
201 */
202
203 netdev_for_each_lower_dev(dev, lower_dev, iter) {
204 err = switchdev_port_attr_get(lower_dev, attr);
205 if (err)
206 break;
Jiri Pirko1f868392015-10-01 11:03:42 +0200207 if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED)
Scott Feldman30943332015-05-10 09:47:48 -0700208 first = *attr;
209 else if (memcmp(&first, attr, sizeof(*attr)))
210 return -ENODATA;
211 }
212
213 return err;
214}
215EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
216
217static int __switchdev_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200218 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200219 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700220{
221 const struct switchdev_ops *ops = dev->switchdev_ops;
222 struct net_device *lower_dev;
223 struct list_head *iter;
224 int err = -EOPNOTSUPP;
225
226 if (ops && ops->switchdev_port_attr_set)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200227 return ops->switchdev_port_attr_set(dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700228
229 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
Scott Feldman464314e2015-10-08 19:23:18 -0700230 goto done;
Scott Feldman30943332015-05-10 09:47:48 -0700231
232 /* Switch device port(s) may be stacked under
233 * bond/team/vlan dev, so recurse down to set attr on
234 * each port.
235 */
236
237 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200238 err = __switchdev_port_attr_set(lower_dev, attr, trans);
Scott Feldman464314e2015-10-08 19:23:18 -0700239 if (err == -EOPNOTSUPP &&
240 attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
241 continue;
Scott Feldman30943332015-05-10 09:47:48 -0700242 if (err)
243 break;
244 }
245
Scott Feldman464314e2015-10-08 19:23:18 -0700246done:
247 if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
248 err = 0;
249
Scott Feldman30943332015-05-10 09:47:48 -0700250 return err;
251}
252
253struct switchdev_attr_set_work {
254 struct work_struct work;
255 struct net_device *dev;
256 struct switchdev_attr attr;
257};
258
259static void switchdev_port_attr_set_work(struct work_struct *work)
260{
261 struct switchdev_attr_set_work *asw =
262 container_of(work, struct switchdev_attr_set_work, work);
263 int err;
264
265 rtnl_lock();
266 err = switchdev_port_attr_set(asw->dev, &asw->attr);
Scott Feldman57225e72015-06-11 08:19:01 -0700267 if (err && err != -EOPNOTSUPP)
268 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
269 err, asw->attr.id);
Scott Feldman30943332015-05-10 09:47:48 -0700270 rtnl_unlock();
271
272 dev_put(asw->dev);
273 kfree(work);
274}
275
276static int switchdev_port_attr_set_defer(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200277 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700278{
279 struct switchdev_attr_set_work *asw;
280
281 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
282 if (!asw)
283 return -ENOMEM;
284
285 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
286
287 dev_hold(dev);
288 asw->dev = dev;
289 memcpy(&asw->attr, attr, sizeof(asw->attr));
290
291 schedule_work(&asw->work);
292
293 return 0;
294}
295
296/**
297 * switchdev_port_attr_set - Set port attribute
298 *
299 * @dev: port device
300 * @attr: attribute to set
301 *
302 * Use a 2-phase prepare-commit transaction model to ensure
303 * system is not left in a partially updated state due to
304 * failure from driver/device.
305 */
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200306int switchdev_port_attr_set(struct net_device *dev,
307 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700308{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200309 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700310 int err;
311
312 if (!rtnl_is_locked()) {
313 /* Running prepare-commit transaction across stacked
314 * devices requires nothing moves, so if rtnl_lock is
315 * not held, schedule a worker thread to hold rtnl_lock
316 * while setting attr.
317 */
318
319 return switchdev_port_attr_set_defer(dev, attr);
320 }
321
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200322 switchdev_trans_init(&trans);
323
Scott Feldman30943332015-05-10 09:47:48 -0700324 /* Phase I: prepare for attr set. Driver/device should fail
325 * here if there are going to be issues in the commit phase,
326 * such as lack of resources or support. The driver/device
327 * should reserve resources needed for the commit phase here,
328 * but should not commit the attr.
329 */
330
Jiri Pirkof623ab72015-09-24 10:02:49 +0200331 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200332 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700333 if (err) {
334 /* Prepare phase failed: abort the transaction. Any
335 * resources reserved in the prepare phase are
336 * released.
337 */
338
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200339 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200340 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700341
342 return err;
343 }
344
345 /* Phase II: commit attr set. This cannot fail as a fault
346 * of driver/device. If it does, it's a bug in the driver/device
347 * because the driver said everythings was OK in phase I.
348 */
349
Jiri Pirkof623ab72015-09-24 10:02:49 +0200350 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200351 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700352 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
353 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200354 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700355
356 return err;
357}
358EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
359
Scott Feldman22c1f672015-05-12 23:03:51 -0700360static int __switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200361 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200362 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700363{
364 const struct switchdev_ops *ops = dev->switchdev_ops;
365 struct net_device *lower_dev;
366 struct list_head *iter;
367 int err = -EOPNOTSUPP;
368
369 if (ops && ops->switchdev_port_obj_add)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200370 return ops->switchdev_port_obj_add(dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700371
372 /* Switch device port(s) may be stacked under
373 * bond/team/vlan dev, so recurse down to add object on
374 * each port.
375 */
376
377 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200378 err = __switchdev_port_obj_add(lower_dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700379 if (err)
380 break;
381 }
382
383 return err;
384}
385
386/**
387 * switchdev_port_obj_add - Add port object
388 *
389 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400390 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700391 * @obj: object to add
392 *
393 * Use a 2-phase prepare-commit transaction model to ensure
394 * system is not left in a partially updated state due to
395 * failure from driver/device.
396 *
397 * rtnl_lock must be held.
398 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200399int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200400 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700401{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200402 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700403 int err;
404
405 ASSERT_RTNL();
406
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200407 switchdev_trans_init(&trans);
408
Scott Feldman491d0f12015-05-10 09:47:52 -0700409 /* Phase I: prepare for obj add. Driver/device should fail
410 * here if there are going to be issues in the commit phase,
411 * such as lack of resources or support. The driver/device
412 * should reserve resources needed for the commit phase here,
413 * but should not commit the obj.
414 */
415
Jiri Pirkof623ab72015-09-24 10:02:49 +0200416 trans.ph_prepare = true;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200417 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700418 if (err) {
419 /* Prepare phase failed: abort the transaction. Any
420 * resources reserved in the prepare phase are
421 * released.
422 */
423
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200424 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200425 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700426
427 return err;
428 }
429
430 /* Phase II: commit obj add. This cannot fail as a fault
431 * of driver/device. If it does, it's a bug in the driver/device
432 * because the driver said everythings was OK in phase I.
433 */
434
Jiri Pirkof623ab72015-09-24 10:02:49 +0200435 trans.ph_prepare = false;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200436 err = __switchdev_port_obj_add(dev, obj, &trans);
437 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200438 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700439
440 return err;
441}
442EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
443
444/**
445 * switchdev_port_obj_del - Delete port object
446 *
447 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400448 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700449 * @obj: object to delete
450 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200451int switchdev_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200452 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700453{
454 const struct switchdev_ops *ops = dev->switchdev_ops;
455 struct net_device *lower_dev;
456 struct list_head *iter;
457 int err = -EOPNOTSUPP;
458
459 if (ops && ops->switchdev_port_obj_del)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200460 return ops->switchdev_port_obj_del(dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700461
462 /* Switch device port(s) may be stacked under
463 * bond/team/vlan dev, so recurse down to delete object on
464 * each port.
465 */
466
467 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200468 err = switchdev_port_obj_del(lower_dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700469 if (err)
470 break;
471 }
472
473 return err;
474}
475EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
476
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700477/**
478 * switchdev_port_obj_dump - Dump port objects
479 *
480 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400481 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700482 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400483 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700484 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200485int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200486 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700487{
488 const struct switchdev_ops *ops = dev->switchdev_ops;
489 struct net_device *lower_dev;
490 struct list_head *iter;
491 int err = -EOPNOTSUPP;
492
493 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200494 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700495
496 /* Switch device port(s) may be stacked under
497 * bond/team/vlan dev, so recurse down to dump objects on
498 * first port at bottom of stack.
499 */
500
501 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200502 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700503 break;
504 }
505
506 return err;
507}
508EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
509
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700510static DEFINE_MUTEX(switchdev_mutex);
511static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100512
513/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700514 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100515 * @nb: notifier_block
516 *
517 * Register switch device notifier. This should be used by code
518 * which needs to monitor events happening in particular device.
519 * Return values are same as for atomic_notifier_chain_register().
520 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700521int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100522{
523 int err;
524
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700525 mutex_lock(&switchdev_mutex);
526 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
527 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100528 return err;
529}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700530EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100531
532/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700533 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100534 * @nb: notifier_block
535 *
536 * Unregister switch device notifier.
537 * Return values are same as for atomic_notifier_chain_unregister().
538 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700539int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100540{
541 int err;
542
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700543 mutex_lock(&switchdev_mutex);
544 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
545 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100546 return err;
547}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700548EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100549
550/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700551 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100552 * @val: value passed unmodified to notifier function
553 * @dev: port device
554 * @info: notifier information data
555 *
556 * Call all network notifier blocks. This should be called by driver
557 * when it needs to propagate hardware event.
558 * Return values are same as for atomic_notifier_call_chain().
559 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700560int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
561 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100562{
563 int err;
564
565 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700566 mutex_lock(&switchdev_mutex);
567 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
568 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100569 return err;
570}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700571EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800572
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700573struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200574 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700575 struct sk_buff *skb;
576 u32 filter_mask;
577 u16 flags;
578 u16 begin;
579 u16 end;
580};
581
Vivien Didelote23b0022015-09-29 12:07:13 -0400582static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700583{
584 struct bridge_vlan_info vinfo;
585
586 vinfo.flags = dump->flags;
587
588 if (dump->begin == 0 && dump->end == 0) {
589 return 0;
590 } else if (dump->begin == dump->end) {
591 vinfo.vid = dump->begin;
592 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
593 sizeof(vinfo), &vinfo))
594 return -EMSGSIZE;
595 } else {
596 vinfo.vid = dump->begin;
597 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
598 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
599 sizeof(vinfo), &vinfo))
600 return -EMSGSIZE;
601 vinfo.vid = dump->end;
602 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
603 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
604 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
605 sizeof(vinfo), &vinfo))
606 return -EMSGSIZE;
607 }
608
609 return 0;
610}
611
Jiri Pirko648b4a92015-10-01 11:03:45 +0200612static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700613{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200614 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700615 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400616 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700617 int err = 0;
618
619 if (vlan->vid_begin > vlan->vid_end)
620 return -EINVAL;
621
622 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
623 dump->flags = vlan->flags;
624 for (dump->begin = dump->end = vlan->vid_begin;
625 dump->begin <= vlan->vid_end;
626 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400627 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700628 if (err)
629 return err;
630 }
631 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
632 if (dump->begin > vlan->vid_begin &&
633 dump->begin >= vlan->vid_end) {
634 if ((dump->begin - 1) == vlan->vid_end &&
635 dump->flags == vlan->flags) {
636 /* prepend */
637 dump->begin = vlan->vid_begin;
638 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400639 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700640 dump->flags = vlan->flags;
641 dump->begin = vlan->vid_begin;
642 dump->end = vlan->vid_end;
643 }
644 } else if (dump->end <= vlan->vid_begin &&
645 dump->end < vlan->vid_end) {
646 if ((dump->end + 1) == vlan->vid_begin &&
647 dump->flags == vlan->flags) {
648 /* append */
649 dump->end = vlan->vid_end;
650 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400651 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700652 dump->flags = vlan->flags;
653 dump->begin = vlan->vid_begin;
654 dump->end = vlan->vid_end;
655 }
656 } else {
657 err = -EINVAL;
658 }
659 }
660
661 return err;
662}
663
664static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
665 u32 filter_mask)
666{
667 struct switchdev_vlan_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200668 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700669 .skb = skb,
670 .filter_mask = filter_mask,
671 };
672 int err = 0;
673
674 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
675 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200676 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400677 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700678 if (err)
679 goto err_out;
680 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
681 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400682 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700683 }
684
685err_out:
686 return err == -EOPNOTSUPP ? 0 : err;
687}
688
Scott Feldman8793d0a2015-05-10 09:48:04 -0700689/**
690 * switchdev_port_bridge_getlink - Get bridge port attributes
691 *
692 * @dev: port device
693 *
694 * Called for SELF on rtnl_bridge_getlink to get bridge port
695 * attributes.
696 */
697int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
698 struct net_device *dev, u32 filter_mask,
699 int nlflags)
700{
701 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200702 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700703 };
704 u16 mode = BRIDGE_MODE_UNDEF;
705 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
706 int err;
707
708 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400709 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700710 return err;
711
712 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700713 attr.u.brport_flags, mask, nlflags,
714 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700715}
716EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
717
Scott Feldman47f83282015-05-10 09:47:56 -0700718static int switchdev_port_br_setflag(struct net_device *dev,
719 struct nlattr *nlattr,
720 unsigned long brport_flag)
721{
722 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200723 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700724 };
725 u8 flag = nla_get_u8(nlattr);
726 int err;
727
728 err = switchdev_port_attr_get(dev, &attr);
729 if (err)
730 return err;
731
732 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700733 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700734 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700735 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700736
737 return switchdev_port_attr_set(dev, &attr);
738}
739
740static const struct nla_policy
741switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
742 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
743 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
744 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
745 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
746 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
747 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
748 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
749 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
750 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
751 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
752};
753
754static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
755 struct nlattr *protinfo)
756{
757 struct nlattr *attr;
758 int rem;
759 int err;
760
761 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
762 switchdev_port_bridge_policy);
763 if (err)
764 return err;
765
766 nla_for_each_nested(attr, protinfo, rem) {
767 switch (nla_type(attr)) {
768 case IFLA_BRPORT_LEARNING:
769 err = switchdev_port_br_setflag(dev, attr,
770 BR_LEARNING);
771 break;
772 case IFLA_BRPORT_LEARNING_SYNC:
773 err = switchdev_port_br_setflag(dev, attr,
774 BR_LEARNING_SYNC);
775 break;
776 default:
777 err = -EOPNOTSUPP;
778 break;
779 }
780 if (err)
781 return err;
782 }
783
784 return 0;
785}
786
787static int switchdev_port_br_afspec(struct net_device *dev,
788 struct nlattr *afspec,
789 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200790 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700791{
792 struct nlattr *attr;
793 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200794 struct switchdev_obj_port_vlan vlan = {
795 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
796 };
Scott Feldman47f83282015-05-10 09:47:56 -0700797 int rem;
798 int err;
799
800 nla_for_each_nested(attr, afspec, rem) {
801 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
802 continue;
803 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
804 return -EINVAL;
805 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400806 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700807 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400808 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700809 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400810 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200811 /* don't allow range of pvids */
812 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
813 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700814 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400815 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700816 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400817 vlan.vid_end = vinfo->vid;
818 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700819 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200820 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700821 if (err)
822 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400823 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700824 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400825 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700826 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400827 vlan.vid_begin = vinfo->vid;
828 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200829 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700830 if (err)
831 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400832 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700833 }
834 }
835
836 return 0;
837}
838
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800839/**
Scott Feldman47f83282015-05-10 09:47:56 -0700840 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800841 *
842 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700843 * @nlh: netlink header
844 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800845 *
Scott Feldman47f83282015-05-10 09:47:56 -0700846 * Called for SELF on rtnl_bridge_setlink to set bridge port
847 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800848 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700849int switchdev_port_bridge_setlink(struct net_device *dev,
850 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800851{
Scott Feldman47f83282015-05-10 09:47:56 -0700852 struct nlattr *protinfo;
853 struct nlattr *afspec;
854 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800855
Scott Feldman47f83282015-05-10 09:47:56 -0700856 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
857 IFLA_PROTINFO);
858 if (protinfo) {
859 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
860 if (err)
861 return err;
862 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800863
Scott Feldman47f83282015-05-10 09:47:56 -0700864 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
865 IFLA_AF_SPEC);
866 if (afspec)
867 err = switchdev_port_br_afspec(dev, afspec,
868 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800869
Scott Feldman47f83282015-05-10 09:47:56 -0700870 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800871}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700872EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800873
874/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700875 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800876 *
877 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700878 * @nlh: netlink header
879 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800880 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700881 * Called for SELF on rtnl_bridge_dellink to set bridge port
882 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800883 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700884int switchdev_port_bridge_dellink(struct net_device *dev,
885 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800886{
Scott Feldman5c34e022015-05-10 09:48:00 -0700887 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800888
Scott Feldman5c34e022015-05-10 09:48:00 -0700889 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
890 IFLA_AF_SPEC);
891 if (afspec)
892 return switchdev_port_br_afspec(dev, afspec,
893 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800894
Scott Feldman5c34e022015-05-10 09:48:00 -0700895 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800896}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700897EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800898
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700899/**
900 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
901 *
902 * @ndmsg: netlink hdr
903 * @nlattr: netlink attributes
904 * @dev: port device
905 * @addr: MAC address to add
906 * @vid: VLAN to add
907 *
908 * Add FDB entry to switch device.
909 */
910int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
911 struct net_device *dev, const unsigned char *addr,
912 u16 vid, u16 nlm_flags)
913{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200914 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200915 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400916 .addr = addr,
917 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700918 };
919
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200920 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700921}
922EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
923
924/**
925 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
926 *
927 * @ndmsg: netlink hdr
928 * @nlattr: netlink attributes
929 * @dev: port device
930 * @addr: MAC address to delete
931 * @vid: VLAN to delete
932 *
933 * Delete FDB entry from switch device.
934 */
935int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
936 struct net_device *dev, const unsigned char *addr,
937 u16 vid)
938{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200939 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200940 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400941 .addr = addr,
942 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700943 };
944
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200945 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700946}
947EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
948
949struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200950 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b2015-09-29 12:07:14 -0400951 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700952 struct sk_buff *skb;
953 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700954 int idx;
955};
956
Jiri Pirko648b4a92015-10-01 11:03:45 +0200957static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700958{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200959 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700960 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400961 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700962 u32 portid = NETLINK_CB(dump->cb->skb).portid;
963 u32 seq = dump->cb->nlh->nlmsg_seq;
964 struct nlmsghdr *nlh;
965 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700966
967 if (dump->idx < dump->cb->args[0])
968 goto skip;
969
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700970 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
971 sizeof(*ndm), NLM_F_MULTI);
972 if (!nlh)
973 return -EMSGSIZE;
974
975 ndm = nlmsg_data(nlh);
976 ndm->ndm_family = AF_BRIDGE;
977 ndm->ndm_pad1 = 0;
978 ndm->ndm_pad2 = 0;
979 ndm->ndm_flags = NTF_SELF;
980 ndm->ndm_type = 0;
Vivien Didelote02a06b2015-09-29 12:07:14 -0400981 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400982 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700983
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400984 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700985 goto nla_put_failure;
986
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400987 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700988 goto nla_put_failure;
989
990 nlmsg_end(dump->skb, nlh);
991
992skip:
993 dump->idx++;
994 return 0;
995
996nla_put_failure:
997 nlmsg_cancel(dump->skb, nlh);
998 return -EMSGSIZE;
999}
1000
1001/**
1002 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
1003 *
1004 * @skb: netlink skb
1005 * @cb: netlink callback
1006 * @dev: port device
1007 * @filter_dev: filter device
1008 * @idx:
1009 *
1010 * Delete FDB entry from switch device.
1011 */
1012int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1013 struct net_device *dev,
1014 struct net_device *filter_dev, int idx)
1015{
1016 struct switchdev_fdb_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001017 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b2015-09-29 12:07:14 -04001018 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001019 .skb = skb,
1020 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001021 .idx = idx,
1022 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001023
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001024 switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001025 return dump.idx;
1026}
1027EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
1028
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001029static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001030{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001031 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001032 struct net_device *lower_dev;
1033 struct net_device *port_dev;
1034 struct list_head *iter;
1035
1036 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001037 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001038 */
1039
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001040 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001041 return dev;
1042
1043 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001044 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001045 if (port_dev)
1046 return port_dev;
1047 }
1048
1049 return NULL;
1050}
1051
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001052static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001053{
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001054 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001055 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001056 };
1057 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001058 struct net_device *dev = NULL;
1059 int nhsel;
1060
1061 /* For this route, all nexthop devs must be on the same switch. */
1062
1063 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1064 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1065
1066 if (!nh->nh_dev)
1067 return NULL;
1068
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001069 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001070 if (!dev)
1071 return NULL;
1072
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001073 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001074 return NULL;
1075
Scott Feldmand754f982015-07-18 18:24:49 -07001076 if (nhsel > 0 &&
1077 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001078 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001079
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001080 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001081 }
1082
1083 return dev;
1084}
1085
Scott Feldman5e8d9042015-03-05 21:21:15 -08001086/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001087 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001088 *
1089 * @dst: route's IPv4 destination address
1090 * @dst_len: destination address length (prefix length)
1091 * @fi: route FIB info structure
1092 * @tos: route TOS
1093 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001094 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001095 * @tb_id: route table ID
1096 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001097 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001098 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001099int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1100 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001101{
Vivien Didelotab069002015-09-29 12:07:17 -04001102 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001103 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001104 .dst = dst,
1105 .dst_len = dst_len,
1106 .fi = fi,
1107 .tos = tos,
1108 .type = type,
1109 .nlflags = nlflags,
1110 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001111 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001112 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001113 int err = 0;
1114
Scott Feldman8e05fd72015-03-05 21:21:19 -08001115 /* Don't offload route if using custom ip rules or if
1116 * IPv4 FIB offloading has been disabled completely.
1117 */
1118
Scott Feldmane1315db2015-03-06 01:14:36 -08001119#ifdef CONFIG_IP_MULTIPLE_TABLES
1120 if (fi->fib_net->ipv4.fib_has_custom_rules)
1121 return 0;
1122#endif
1123
1124 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001125 return 0;
1126
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001127 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001128 if (!dev)
1129 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001130
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001131 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001132 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001133 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001134
Scott Feldmanaf201f72015-06-10 17:04:49 -07001135 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001136}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001137EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001138
1139/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001140 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001141 *
1142 * @dst: route's IPv4 destination address
1143 * @dst_len: destination address length (prefix length)
1144 * @fi: route FIB info structure
1145 * @tos: route TOS
1146 * @type: route type
1147 * @tb_id: route table ID
1148 *
1149 * Delete IPv4 route entry from switch device.
1150 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001151int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1152 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001153{
Vivien Didelotab069002015-09-29 12:07:17 -04001154 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001155 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001156 .dst = dst,
1157 .dst_len = dst_len,
1158 .fi = fi,
1159 .tos = tos,
1160 .type = type,
1161 .nlflags = 0,
1162 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001163 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001164 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001165 int err = 0;
1166
Roopa Prabhueea39942015-05-13 21:17:41 -07001167 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001168 return 0;
1169
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001170 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001171 if (!dev)
1172 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001173
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001174 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001175 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001176 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001177
Scott Feldmanaf201f72015-06-10 17:04:49 -07001178 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001179}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001180EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001181
1182/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001183 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001184 *
1185 * @fi: route FIB info structure
1186 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001187void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001188{
1189 /* There was a problem installing this route to the offload
1190 * device. For now, until we come up with more refined
1191 * policy handling, abruptly end IPv4 fib offloading for
1192 * for entire net by flushing offload device(s) of all
1193 * IPv4 routes, and mark IPv4 fib offloading broken from
1194 * this point forward.
1195 */
1196
1197 fib_flush_external(fi->fib_net);
1198 fi->fib_net->ipv4.fib_offload_disabled = true;
1199}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001200EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001201
1202static bool switchdev_port_same_parent_id(struct net_device *a,
1203 struct net_device *b)
1204{
1205 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001206 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001207 .flags = SWITCHDEV_F_NO_RECURSE,
1208 };
1209 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001210 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001211 .flags = SWITCHDEV_F_NO_RECURSE,
1212 };
1213
1214 if (switchdev_port_attr_get(a, &a_attr) ||
1215 switchdev_port_attr_get(b, &b_attr))
1216 return false;
1217
1218 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1219}
1220
1221static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1222 struct net_device *group_dev)
1223{
1224 struct net_device *lower_dev;
1225 struct list_head *iter;
1226
1227 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1228 if (lower_dev == dev)
1229 continue;
1230 if (switchdev_port_same_parent_id(dev, lower_dev))
1231 return lower_dev->offload_fwd_mark;
1232 return switchdev_port_fwd_mark_get(dev, lower_dev);
1233 }
1234
1235 return dev->ifindex;
1236}
1237
1238static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1239 u32 old_mark, u32 *reset_mark)
1240{
1241 struct net_device *lower_dev;
1242 struct list_head *iter;
1243
1244 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1245 if (lower_dev->offload_fwd_mark == old_mark) {
1246 if (!*reset_mark)
1247 *reset_mark = lower_dev->ifindex;
1248 lower_dev->offload_fwd_mark = *reset_mark;
1249 }
1250 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1251 }
1252}
1253
1254/**
1255 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1256 *
1257 * @dev: port device
1258 * @group_dev: containing device
1259 * @joining: true if dev is joining group; false if leaving group
1260 *
1261 * An ungrouped port's offload mark is just its ifindex. A grouped
1262 * port's (member of a bridge, for example) offload mark is the ifindex
1263 * of one of the ports in the group with the same parent (switch) ID.
1264 * Ports on the same device in the same group will have the same mark.
1265 *
1266 * Example:
1267 *
1268 * br0 ifindex=9
1269 * sw1p1 ifindex=2 mark=2
1270 * sw1p2 ifindex=3 mark=2
1271 * sw2p1 ifindex=4 mark=5
1272 * sw2p2 ifindex=5 mark=5
1273 *
1274 * If sw2p2 leaves the bridge, we'll have:
1275 *
1276 * br0 ifindex=9
1277 * sw1p1 ifindex=2 mark=2
1278 * sw1p2 ifindex=3 mark=2
1279 * sw2p1 ifindex=4 mark=4
1280 * sw2p2 ifindex=5 mark=5
1281 */
1282void switchdev_port_fwd_mark_set(struct net_device *dev,
1283 struct net_device *group_dev,
1284 bool joining)
1285{
1286 u32 mark = dev->ifindex;
1287 u32 reset_mark = 0;
1288
1289 if (group_dev && joining) {
1290 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1291 } else if (group_dev && !joining) {
1292 if (dev->offload_fwd_mark == mark)
1293 /* Ohoh, this port was the mark reference port,
1294 * but it's leaving the group, so reset the
1295 * mark for the remaining ports in the group.
1296 */
1297 switchdev_port_fwd_mark_reset(group_dev, mark,
1298 &reset_mark);
1299 }
1300
1301 dev->offload_fwd_mark = mark;
1302}
1303EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);