blob: 5963d7ac10268a1e90956878d3327365b27e3f0b [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
365/**
366 * switchdev_port_obj_add - Add port object
367 *
368 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400369 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700370 * @obj: object to add
371 *
372 * Use a 2-phase prepare-commit transaction model to ensure
373 * system is not left in a partially updated state due to
374 * failure from driver/device.
375 *
376 * rtnl_lock must be held.
377 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200378int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200379 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700380{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200381 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700382 int err;
383
384 ASSERT_RTNL();
385
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200386 switchdev_trans_init(&trans);
387
Scott Feldman491d0f12015-05-10 09:47:52 -0700388 /* Phase I: prepare for obj add. Driver/device should fail
389 * here if there are going to be issues in the commit phase,
390 * such as lack of resources or support. The driver/device
391 * should reserve resources needed for the commit phase here,
392 * but should not commit the obj.
393 */
394
Jiri Pirkof623ab72015-09-24 10:02:49 +0200395 trans.ph_prepare = true;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200396 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700397 if (err) {
398 /* Prepare phase failed: abort the transaction. Any
399 * resources reserved in the prepare phase are
400 * released.
401 */
402
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200403 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200404 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700405
406 return err;
407 }
408
409 /* Phase II: commit obj add. This cannot fail as a fault
410 * of driver/device. If it does, it's a bug in the driver/device
411 * because the driver said everythings was OK in phase I.
412 */
413
Jiri Pirkof623ab72015-09-24 10:02:49 +0200414 trans.ph_prepare = false;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200415 err = __switchdev_port_obj_add(dev, obj, &trans);
416 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200417 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700418
419 return err;
420}
421EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
422
423/**
424 * switchdev_port_obj_del - Delete port object
425 *
426 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400427 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700428 * @obj: object to delete
429 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200430int switchdev_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200431 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700432{
433 const struct switchdev_ops *ops = dev->switchdev_ops;
434 struct net_device *lower_dev;
435 struct list_head *iter;
436 int err = -EOPNOTSUPP;
437
438 if (ops && ops->switchdev_port_obj_del)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200439 return ops->switchdev_port_obj_del(dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700440
441 /* Switch device port(s) may be stacked under
442 * bond/team/vlan dev, so recurse down to delete object on
443 * each port.
444 */
445
446 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200447 err = switchdev_port_obj_del(lower_dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700448 if (err)
449 break;
450 }
451
452 return err;
453}
454EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
455
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700456/**
457 * switchdev_port_obj_dump - Dump port objects
458 *
459 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400460 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700461 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400462 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700463 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200464int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200465 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700466{
467 const struct switchdev_ops *ops = dev->switchdev_ops;
468 struct net_device *lower_dev;
469 struct list_head *iter;
470 int err = -EOPNOTSUPP;
471
472 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200473 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700474
475 /* Switch device port(s) may be stacked under
476 * bond/team/vlan dev, so recurse down to dump objects on
477 * first port at bottom of stack.
478 */
479
480 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200481 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700482 break;
483 }
484
485 return err;
486}
487EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
488
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700489static DEFINE_MUTEX(switchdev_mutex);
490static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100491
492/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700493 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100494 * @nb: notifier_block
495 *
496 * Register switch device notifier. This should be used by code
497 * which needs to monitor events happening in particular device.
498 * Return values are same as for atomic_notifier_chain_register().
499 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700500int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100501{
502 int err;
503
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700504 mutex_lock(&switchdev_mutex);
505 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
506 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100507 return err;
508}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700509EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100510
511/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700512 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100513 * @nb: notifier_block
514 *
515 * Unregister switch device notifier.
516 * Return values are same as for atomic_notifier_chain_unregister().
517 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700518int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100519{
520 int err;
521
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700522 mutex_lock(&switchdev_mutex);
523 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
524 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100525 return err;
526}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700527EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100528
529/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700530 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100531 * @val: value passed unmodified to notifier function
532 * @dev: port device
533 * @info: notifier information data
534 *
535 * Call all network notifier blocks. This should be called by driver
536 * when it needs to propagate hardware event.
537 * Return values are same as for atomic_notifier_call_chain().
538 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700539int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
540 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100541{
542 int err;
543
544 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700545 mutex_lock(&switchdev_mutex);
546 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
547 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100548 return err;
549}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700550EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800551
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700552struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200553 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700554 struct sk_buff *skb;
555 u32 filter_mask;
556 u16 flags;
557 u16 begin;
558 u16 end;
559};
560
Vivien Didelote23b0022015-09-29 12:07:13 -0400561static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700562{
563 struct bridge_vlan_info vinfo;
564
565 vinfo.flags = dump->flags;
566
567 if (dump->begin == 0 && dump->end == 0) {
568 return 0;
569 } else if (dump->begin == dump->end) {
570 vinfo.vid = dump->begin;
571 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
572 sizeof(vinfo), &vinfo))
573 return -EMSGSIZE;
574 } else {
575 vinfo.vid = dump->begin;
576 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
577 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
578 sizeof(vinfo), &vinfo))
579 return -EMSGSIZE;
580 vinfo.vid = dump->end;
581 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
582 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
583 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
584 sizeof(vinfo), &vinfo))
585 return -EMSGSIZE;
586 }
587
588 return 0;
589}
590
Jiri Pirko648b4a92015-10-01 11:03:45 +0200591static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700592{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200593 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700594 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400595 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700596 int err = 0;
597
598 if (vlan->vid_begin > vlan->vid_end)
599 return -EINVAL;
600
601 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
602 dump->flags = vlan->flags;
603 for (dump->begin = dump->end = vlan->vid_begin;
604 dump->begin <= vlan->vid_end;
605 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400606 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700607 if (err)
608 return err;
609 }
610 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
611 if (dump->begin > vlan->vid_begin &&
612 dump->begin >= vlan->vid_end) {
613 if ((dump->begin - 1) == vlan->vid_end &&
614 dump->flags == vlan->flags) {
615 /* prepend */
616 dump->begin = vlan->vid_begin;
617 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400618 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700619 dump->flags = vlan->flags;
620 dump->begin = vlan->vid_begin;
621 dump->end = vlan->vid_end;
622 }
623 } else if (dump->end <= vlan->vid_begin &&
624 dump->end < vlan->vid_end) {
625 if ((dump->end + 1) == vlan->vid_begin &&
626 dump->flags == vlan->flags) {
627 /* append */
628 dump->end = vlan->vid_end;
629 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400630 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700631 dump->flags = vlan->flags;
632 dump->begin = vlan->vid_begin;
633 dump->end = vlan->vid_end;
634 }
635 } else {
636 err = -EINVAL;
637 }
638 }
639
640 return err;
641}
642
643static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
644 u32 filter_mask)
645{
646 struct switchdev_vlan_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200647 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700648 .skb = skb,
649 .filter_mask = filter_mask,
650 };
651 int err = 0;
652
653 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
654 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200655 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400656 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700657 if (err)
658 goto err_out;
659 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
660 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400661 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700662 }
663
664err_out:
665 return err == -EOPNOTSUPP ? 0 : err;
666}
667
Scott Feldman8793d0a2015-05-10 09:48:04 -0700668/**
669 * switchdev_port_bridge_getlink - Get bridge port attributes
670 *
671 * @dev: port device
672 *
673 * Called for SELF on rtnl_bridge_getlink to get bridge port
674 * attributes.
675 */
676int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
677 struct net_device *dev, u32 filter_mask,
678 int nlflags)
679{
680 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200681 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700682 };
683 u16 mode = BRIDGE_MODE_UNDEF;
684 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
685 int err;
686
687 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400688 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700689 return err;
690
691 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700692 attr.u.brport_flags, mask, nlflags,
693 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700694}
695EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
696
Scott Feldman47f83282015-05-10 09:47:56 -0700697static int switchdev_port_br_setflag(struct net_device *dev,
698 struct nlattr *nlattr,
699 unsigned long brport_flag)
700{
701 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200702 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700703 };
704 u8 flag = nla_get_u8(nlattr);
705 int err;
706
707 err = switchdev_port_attr_get(dev, &attr);
708 if (err)
709 return err;
710
711 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700712 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700713 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700714 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700715
716 return switchdev_port_attr_set(dev, &attr);
717}
718
719static const struct nla_policy
720switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
721 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
722 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
723 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
724 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
725 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
726 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
727 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
728 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
729 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
730 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
731};
732
733static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
734 struct nlattr *protinfo)
735{
736 struct nlattr *attr;
737 int rem;
738 int err;
739
740 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
741 switchdev_port_bridge_policy);
742 if (err)
743 return err;
744
745 nla_for_each_nested(attr, protinfo, rem) {
746 switch (nla_type(attr)) {
747 case IFLA_BRPORT_LEARNING:
748 err = switchdev_port_br_setflag(dev, attr,
749 BR_LEARNING);
750 break;
751 case IFLA_BRPORT_LEARNING_SYNC:
752 err = switchdev_port_br_setflag(dev, attr,
753 BR_LEARNING_SYNC);
754 break;
755 default:
756 err = -EOPNOTSUPP;
757 break;
758 }
759 if (err)
760 return err;
761 }
762
763 return 0;
764}
765
766static int switchdev_port_br_afspec(struct net_device *dev,
767 struct nlattr *afspec,
768 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200769 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700770{
771 struct nlattr *attr;
772 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200773 struct switchdev_obj_port_vlan vlan = {
774 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
775 };
Scott Feldman47f83282015-05-10 09:47:56 -0700776 int rem;
777 int err;
778
779 nla_for_each_nested(attr, afspec, rem) {
780 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
781 continue;
782 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
783 return -EINVAL;
784 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400785 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700786 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400787 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700788 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400789 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200790 /* don't allow range of pvids */
791 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
792 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700793 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400794 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700795 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400796 vlan.vid_end = vinfo->vid;
797 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700798 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200799 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700800 if (err)
801 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400802 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700803 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400804 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700805 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400806 vlan.vid_begin = vinfo->vid;
807 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200808 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700809 if (err)
810 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400811 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700812 }
813 }
814
815 return 0;
816}
817
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800818/**
Scott Feldman47f83282015-05-10 09:47:56 -0700819 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800820 *
821 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700822 * @nlh: netlink header
823 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800824 *
Scott Feldman47f83282015-05-10 09:47:56 -0700825 * Called for SELF on rtnl_bridge_setlink to set bridge port
826 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800827 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700828int switchdev_port_bridge_setlink(struct net_device *dev,
829 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800830{
Scott Feldman47f83282015-05-10 09:47:56 -0700831 struct nlattr *protinfo;
832 struct nlattr *afspec;
833 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800834
Scott Feldman47f83282015-05-10 09:47:56 -0700835 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
836 IFLA_PROTINFO);
837 if (protinfo) {
838 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
839 if (err)
840 return err;
841 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800842
Scott Feldman47f83282015-05-10 09:47:56 -0700843 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
844 IFLA_AF_SPEC);
845 if (afspec)
846 err = switchdev_port_br_afspec(dev, afspec,
847 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800848
Scott Feldman47f83282015-05-10 09:47:56 -0700849 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800850}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700851EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800852
853/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700854 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800855 *
856 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700857 * @nlh: netlink header
858 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800859 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700860 * Called for SELF on rtnl_bridge_dellink to set bridge port
861 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800862 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700863int switchdev_port_bridge_dellink(struct net_device *dev,
864 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800865{
Scott Feldman5c34e022015-05-10 09:48:00 -0700866 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800867
Scott Feldman5c34e022015-05-10 09:48:00 -0700868 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
869 IFLA_AF_SPEC);
870 if (afspec)
871 return switchdev_port_br_afspec(dev, afspec,
872 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800873
Scott Feldman5c34e022015-05-10 09:48:00 -0700874 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800875}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700876EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800877
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700878/**
879 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
880 *
881 * @ndmsg: netlink hdr
882 * @nlattr: netlink attributes
883 * @dev: port device
884 * @addr: MAC address to add
885 * @vid: VLAN to add
886 *
887 * Add FDB entry to switch device.
888 */
889int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
890 struct net_device *dev, const unsigned char *addr,
891 u16 vid, u16 nlm_flags)
892{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200893 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200894 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400895 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700896 };
897
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200898 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200899 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700900}
901EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
902
903/**
904 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
905 *
906 * @ndmsg: netlink hdr
907 * @nlattr: netlink attributes
908 * @dev: port device
909 * @addr: MAC address to delete
910 * @vid: VLAN to delete
911 *
912 * Delete FDB entry from switch device.
913 */
914int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
915 struct net_device *dev, const unsigned char *addr,
916 u16 vid)
917{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200918 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200919 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400920 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700921 };
922
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200923 ether_addr_copy(fdb.addr, addr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200924 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700925}
926EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
927
928struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200929 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400930 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700931 struct sk_buff *skb;
932 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700933 int idx;
934};
935
Jiri Pirko648b4a92015-10-01 11:03:45 +0200936static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700937{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200938 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700939 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400940 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700941 u32 portid = NETLINK_CB(dump->cb->skb).portid;
942 u32 seq = dump->cb->nlh->nlmsg_seq;
943 struct nlmsghdr *nlh;
944 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700945
946 if (dump->idx < dump->cb->args[0])
947 goto skip;
948
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700949 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
950 sizeof(*ndm), NLM_F_MULTI);
951 if (!nlh)
952 return -EMSGSIZE;
953
954 ndm = nlmsg_data(nlh);
955 ndm->ndm_family = AF_BRIDGE;
956 ndm->ndm_pad1 = 0;
957 ndm->ndm_pad2 = 0;
958 ndm->ndm_flags = NTF_SELF;
959 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400960 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400961 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700962
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400963 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700964 goto nla_put_failure;
965
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400966 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700967 goto nla_put_failure;
968
969 nlmsg_end(dump->skb, nlh);
970
971skip:
972 dump->idx++;
973 return 0;
974
975nla_put_failure:
976 nlmsg_cancel(dump->skb, nlh);
977 return -EMSGSIZE;
978}
979
980/**
981 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
982 *
983 * @skb: netlink skb
984 * @cb: netlink callback
985 * @dev: port device
986 * @filter_dev: filter device
987 * @idx:
988 *
989 * Delete FDB entry from switch device.
990 */
991int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
992 struct net_device *dev,
993 struct net_device *filter_dev, int idx)
994{
995 struct switchdev_fdb_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200996 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b22015-09-29 12:07:14 -0400997 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700998 .skb = skb,
999 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001000 .idx = idx,
1001 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001002
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001003 switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001004 return dump.idx;
1005}
1006EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
1007
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001008static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001009{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001010 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001011 struct net_device *lower_dev;
1012 struct net_device *port_dev;
1013 struct list_head *iter;
1014
1015 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001016 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001017 */
1018
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001019 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001020 return dev;
1021
1022 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001023 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001024 if (port_dev)
1025 return port_dev;
1026 }
1027
1028 return NULL;
1029}
1030
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001031static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001032{
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001033 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001034 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001035 };
1036 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001037 struct net_device *dev = NULL;
1038 int nhsel;
1039
1040 /* For this route, all nexthop devs must be on the same switch. */
1041
1042 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1043 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1044
1045 if (!nh->nh_dev)
1046 return NULL;
1047
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001048 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001049 if (!dev)
1050 return NULL;
1051
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001052 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001053 return NULL;
1054
Scott Feldmand754f982015-07-18 18:24:49 -07001055 if (nhsel > 0 &&
1056 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001057 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001058
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001059 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001060 }
1061
1062 return dev;
1063}
1064
Scott Feldman5e8d9042015-03-05 21:21:15 -08001065/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001066 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001067 *
1068 * @dst: route's IPv4 destination address
1069 * @dst_len: destination address length (prefix length)
1070 * @fi: route FIB info structure
1071 * @tos: route TOS
1072 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001073 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001074 * @tb_id: route table ID
1075 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001076 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001077 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001078int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1079 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001080{
Vivien Didelotab069002015-09-29 12:07:17 -04001081 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001082 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001083 .dst = dst,
1084 .dst_len = dst_len,
Vivien Didelotab069002015-09-29 12:07:17 -04001085 .tos = tos,
1086 .type = type,
1087 .nlflags = nlflags,
1088 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001089 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001090 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001091 int err = 0;
1092
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001093 memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi));
1094
Scott Feldman8e05fd72015-03-05 21:21:19 -08001095 /* Don't offload route if using custom ip rules or if
1096 * IPv4 FIB offloading has been disabled completely.
1097 */
1098
Scott Feldmane1315db2015-03-06 01:14:36 -08001099#ifdef CONFIG_IP_MULTIPLE_TABLES
1100 if (fi->fib_net->ipv4.fib_has_custom_rules)
1101 return 0;
1102#endif
1103
1104 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001105 return 0;
1106
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001107 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001108 if (!dev)
1109 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001110
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001111 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001112 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001113 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001114
Scott Feldmanaf201f72015-06-10 17:04:49 -07001115 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001116}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001117EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001118
1119/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001120 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001121 *
1122 * @dst: route's IPv4 destination address
1123 * @dst_len: destination address length (prefix length)
1124 * @fi: route FIB info structure
1125 * @tos: route TOS
1126 * @type: route type
1127 * @tb_id: route table ID
1128 *
1129 * Delete IPv4 route entry from switch device.
1130 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001131int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1132 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001133{
Vivien Didelotab069002015-09-29 12:07:17 -04001134 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001135 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001136 .dst = dst,
1137 .dst_len = dst_len,
Vivien Didelotab069002015-09-29 12:07:17 -04001138 .tos = tos,
1139 .type = type,
1140 .nlflags = 0,
1141 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001142 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001143 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001144 int err = 0;
1145
Jiri Pirko850d0cb2015-10-14 19:40:51 +02001146 memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi));
1147
Roopa Prabhueea39942015-05-13 21:17:41 -07001148 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001149 return 0;
1150
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001151 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001152 if (!dev)
1153 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001154
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001155 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001156 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001157 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001158
Scott Feldmanaf201f72015-06-10 17:04:49 -07001159 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001160}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001161EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001162
1163/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001164 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001165 *
1166 * @fi: route FIB info structure
1167 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001168void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001169{
1170 /* There was a problem installing this route to the offload
1171 * device. For now, until we come up with more refined
1172 * policy handling, abruptly end IPv4 fib offloading for
1173 * for entire net by flushing offload device(s) of all
1174 * IPv4 routes, and mark IPv4 fib offloading broken from
1175 * this point forward.
1176 */
1177
1178 fib_flush_external(fi->fib_net);
1179 fi->fib_net->ipv4.fib_offload_disabled = true;
1180}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001181EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001182
1183static bool switchdev_port_same_parent_id(struct net_device *a,
1184 struct net_device *b)
1185{
1186 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001187 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001188 .flags = SWITCHDEV_F_NO_RECURSE,
1189 };
1190 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001191 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001192 .flags = SWITCHDEV_F_NO_RECURSE,
1193 };
1194
1195 if (switchdev_port_attr_get(a, &a_attr) ||
1196 switchdev_port_attr_get(b, &b_attr))
1197 return false;
1198
1199 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1200}
1201
1202static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1203 struct net_device *group_dev)
1204{
1205 struct net_device *lower_dev;
1206 struct list_head *iter;
1207
1208 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1209 if (lower_dev == dev)
1210 continue;
1211 if (switchdev_port_same_parent_id(dev, lower_dev))
1212 return lower_dev->offload_fwd_mark;
1213 return switchdev_port_fwd_mark_get(dev, lower_dev);
1214 }
1215
1216 return dev->ifindex;
1217}
1218
1219static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1220 u32 old_mark, u32 *reset_mark)
1221{
1222 struct net_device *lower_dev;
1223 struct list_head *iter;
1224
1225 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1226 if (lower_dev->offload_fwd_mark == old_mark) {
1227 if (!*reset_mark)
1228 *reset_mark = lower_dev->ifindex;
1229 lower_dev->offload_fwd_mark = *reset_mark;
1230 }
1231 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1232 }
1233}
1234
1235/**
1236 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1237 *
1238 * @dev: port device
1239 * @group_dev: containing device
1240 * @joining: true if dev is joining group; false if leaving group
1241 *
1242 * An ungrouped port's offload mark is just its ifindex. A grouped
1243 * port's (member of a bridge, for example) offload mark is the ifindex
1244 * of one of the ports in the group with the same parent (switch) ID.
1245 * Ports on the same device in the same group will have the same mark.
1246 *
1247 * Example:
1248 *
1249 * br0 ifindex=9
1250 * sw1p1 ifindex=2 mark=2
1251 * sw1p2 ifindex=3 mark=2
1252 * sw2p1 ifindex=4 mark=5
1253 * sw2p2 ifindex=5 mark=5
1254 *
1255 * If sw2p2 leaves the bridge, we'll have:
1256 *
1257 * br0 ifindex=9
1258 * sw1p1 ifindex=2 mark=2
1259 * sw1p2 ifindex=3 mark=2
1260 * sw2p1 ifindex=4 mark=4
1261 * sw2p2 ifindex=5 mark=5
1262 */
1263void switchdev_port_fwd_mark_set(struct net_device *dev,
1264 struct net_device *group_dev,
1265 bool joining)
1266{
1267 u32 mark = dev->ifindex;
1268 u32 reset_mark = 0;
1269
1270 if (group_dev && joining) {
1271 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1272 } else if (group_dev && !joining) {
1273 if (dev->offload_fwd_mark == mark)
1274 /* Ohoh, this port was the mark reference port,
1275 * but it's leaving the group, so reset the
1276 * mark for the remaining ports in the group.
1277 */
1278 switchdev_port_fwd_mark_reset(group_dev, mark,
1279 &reset_mark);
1280 }
1281
1282 dev->offload_fwd_mark = mark;
1283}
1284EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);