blob: 007b8f40df069b0d9d0fda7fb9eb759fcbd86d97 [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
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200253static int switchdev_port_attr_set_now(struct net_device *dev,
254 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700255{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200256 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700257 int err;
258
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200259 switchdev_trans_init(&trans);
260
Scott Feldman30943332015-05-10 09:47:48 -0700261 /* Phase I: prepare for attr set. Driver/device should fail
262 * here if there are going to be issues in the commit phase,
263 * such as lack of resources or support. The driver/device
264 * should reserve resources needed for the commit phase here,
265 * but should not commit the attr.
266 */
267
Jiri Pirkof623ab72015-09-24 10:02:49 +0200268 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200269 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700270 if (err) {
271 /* Prepare phase failed: abort the transaction. Any
272 * resources reserved in the prepare phase are
273 * released.
274 */
275
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200276 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200277 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700278
279 return err;
280 }
281
282 /* Phase II: commit attr set. This cannot fail as a fault
283 * of driver/device. If it does, it's a bug in the driver/device
284 * because the driver said everythings was OK in phase I.
285 */
286
Jiri Pirkof623ab72015-09-24 10:02:49 +0200287 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200288 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700289 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
290 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200291 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700292
293 return err;
294}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200295
296static void switchdev_port_attr_set_deferred(struct net_device *dev,
297 const void *data)
298{
299 const struct switchdev_attr *attr = data;
300 int err;
301
302 err = switchdev_port_attr_set_now(dev, attr);
303 if (err && err != -EOPNOTSUPP)
304 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
305 err, attr->id);
306}
307
308static int switchdev_port_attr_set_defer(struct net_device *dev,
309 const struct switchdev_attr *attr)
310{
311 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
312 switchdev_port_attr_set_deferred);
313}
314
315/**
316 * switchdev_port_attr_set - Set port attribute
317 *
318 * @dev: port device
319 * @attr: attribute to set
320 *
321 * Use a 2-phase prepare-commit transaction model to ensure
322 * system is not left in a partially updated state due to
323 * failure from driver/device.
324 *
325 * rtnl_lock must be held and must not be in atomic section,
326 * in case SWITCHDEV_F_DEFER flag is not set.
327 */
328int switchdev_port_attr_set(struct net_device *dev,
329 const struct switchdev_attr *attr)
330{
331 if (attr->flags & SWITCHDEV_F_DEFER)
332 return switchdev_port_attr_set_defer(dev, attr);
333 ASSERT_RTNL();
334 return switchdev_port_attr_set_now(dev, attr);
335}
Scott Feldman30943332015-05-10 09:47:48 -0700336EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
337
Scott Feldman22c1f672015-05-12 23:03:51 -0700338static int __switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200339 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200340 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700341{
342 const struct switchdev_ops *ops = dev->switchdev_ops;
343 struct net_device *lower_dev;
344 struct list_head *iter;
345 int err = -EOPNOTSUPP;
346
347 if (ops && ops->switchdev_port_obj_add)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200348 return ops->switchdev_port_obj_add(dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700349
350 /* Switch device port(s) may be stacked under
351 * bond/team/vlan dev, so recurse down to add object on
352 * each port.
353 */
354
355 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200356 err = __switchdev_port_obj_add(lower_dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700357 if (err)
358 break;
359 }
360
361 return err;
362}
363
364/**
365 * switchdev_port_obj_add - Add port object
366 *
367 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400368 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700369 * @obj: object to add
370 *
371 * Use a 2-phase prepare-commit transaction model to ensure
372 * system is not left in a partially updated state due to
373 * failure from driver/device.
374 *
375 * rtnl_lock must be held.
376 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200377int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200378 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700379{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200380 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700381 int err;
382
383 ASSERT_RTNL();
384
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200385 switchdev_trans_init(&trans);
386
Scott Feldman491d0f12015-05-10 09:47:52 -0700387 /* Phase I: prepare for obj add. Driver/device should fail
388 * here if there are going to be issues in the commit phase,
389 * such as lack of resources or support. The driver/device
390 * should reserve resources needed for the commit phase here,
391 * but should not commit the obj.
392 */
393
Jiri Pirkof623ab72015-09-24 10:02:49 +0200394 trans.ph_prepare = true;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200395 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700396 if (err) {
397 /* Prepare phase failed: abort the transaction. Any
398 * resources reserved in the prepare phase are
399 * released.
400 */
401
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200402 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200403 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700404
405 return err;
406 }
407
408 /* Phase II: commit obj add. This cannot fail as a fault
409 * of driver/device. If it does, it's a bug in the driver/device
410 * because the driver said everythings was OK in phase I.
411 */
412
Jiri Pirkof623ab72015-09-24 10:02:49 +0200413 trans.ph_prepare = false;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200414 err = __switchdev_port_obj_add(dev, obj, &trans);
415 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200416 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700417
418 return err;
419}
420EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
421
422/**
423 * switchdev_port_obj_del - Delete port object
424 *
425 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400426 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700427 * @obj: object to delete
428 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200429int switchdev_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200430 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700431{
432 const struct switchdev_ops *ops = dev->switchdev_ops;
433 struct net_device *lower_dev;
434 struct list_head *iter;
435 int err = -EOPNOTSUPP;
436
437 if (ops && ops->switchdev_port_obj_del)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200438 return ops->switchdev_port_obj_del(dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700439
440 /* Switch device port(s) may be stacked under
441 * bond/team/vlan dev, so recurse down to delete object on
442 * each port.
443 */
444
445 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200446 err = switchdev_port_obj_del(lower_dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700447 if (err)
448 break;
449 }
450
451 return err;
452}
453EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
454
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700455/**
456 * switchdev_port_obj_dump - Dump port objects
457 *
458 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400459 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700460 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400461 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700462 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200463int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200464 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700465{
466 const struct switchdev_ops *ops = dev->switchdev_ops;
467 struct net_device *lower_dev;
468 struct list_head *iter;
469 int err = -EOPNOTSUPP;
470
471 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200472 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700473
474 /* Switch device port(s) may be stacked under
475 * bond/team/vlan dev, so recurse down to dump objects on
476 * first port at bottom of stack.
477 */
478
479 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200480 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700481 break;
482 }
483
484 return err;
485}
486EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
487
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700488static DEFINE_MUTEX(switchdev_mutex);
489static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100490
491/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700492 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100493 * @nb: notifier_block
494 *
495 * Register switch device notifier. This should be used by code
496 * which needs to monitor events happening in particular device.
497 * Return values are same as for atomic_notifier_chain_register().
498 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700499int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100500{
501 int err;
502
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700503 mutex_lock(&switchdev_mutex);
504 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
505 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100506 return err;
507}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700508EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100509
510/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700511 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100512 * @nb: notifier_block
513 *
514 * Unregister switch device notifier.
515 * Return values are same as for atomic_notifier_chain_unregister().
516 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700517int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100518{
519 int err;
520
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700521 mutex_lock(&switchdev_mutex);
522 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
523 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100524 return err;
525}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700526EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100527
528/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700529 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100530 * @val: value passed unmodified to notifier function
531 * @dev: port device
532 * @info: notifier information data
533 *
534 * Call all network notifier blocks. This should be called by driver
535 * when it needs to propagate hardware event.
536 * Return values are same as for atomic_notifier_call_chain().
537 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700538int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
539 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100540{
541 int err;
542
543 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700544 mutex_lock(&switchdev_mutex);
545 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
546 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100547 return err;
548}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700549EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800550
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700551struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200552 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700553 struct sk_buff *skb;
554 u32 filter_mask;
555 u16 flags;
556 u16 begin;
557 u16 end;
558};
559
Vivien Didelote23b0022015-09-29 12:07:13 -0400560static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700561{
562 struct bridge_vlan_info vinfo;
563
564 vinfo.flags = dump->flags;
565
566 if (dump->begin == 0 && dump->end == 0) {
567 return 0;
568 } else if (dump->begin == dump->end) {
569 vinfo.vid = dump->begin;
570 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
571 sizeof(vinfo), &vinfo))
572 return -EMSGSIZE;
573 } else {
574 vinfo.vid = dump->begin;
575 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
576 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
577 sizeof(vinfo), &vinfo))
578 return -EMSGSIZE;
579 vinfo.vid = dump->end;
580 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
581 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
582 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
583 sizeof(vinfo), &vinfo))
584 return -EMSGSIZE;
585 }
586
587 return 0;
588}
589
Jiri Pirko648b4a92015-10-01 11:03:45 +0200590static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700591{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200592 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700593 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400594 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700595 int err = 0;
596
597 if (vlan->vid_begin > vlan->vid_end)
598 return -EINVAL;
599
600 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
601 dump->flags = vlan->flags;
602 for (dump->begin = dump->end = vlan->vid_begin;
603 dump->begin <= vlan->vid_end;
604 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400605 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700606 if (err)
607 return err;
608 }
609 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
610 if (dump->begin > vlan->vid_begin &&
611 dump->begin >= vlan->vid_end) {
612 if ((dump->begin - 1) == vlan->vid_end &&
613 dump->flags == vlan->flags) {
614 /* prepend */
615 dump->begin = vlan->vid_begin;
616 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400617 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700618 dump->flags = vlan->flags;
619 dump->begin = vlan->vid_begin;
620 dump->end = vlan->vid_end;
621 }
622 } else if (dump->end <= vlan->vid_begin &&
623 dump->end < vlan->vid_end) {
624 if ((dump->end + 1) == vlan->vid_begin &&
625 dump->flags == vlan->flags) {
626 /* append */
627 dump->end = vlan->vid_end;
628 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400629 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700630 dump->flags = vlan->flags;
631 dump->begin = vlan->vid_begin;
632 dump->end = vlan->vid_end;
633 }
634 } else {
635 err = -EINVAL;
636 }
637 }
638
639 return err;
640}
641
642static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
643 u32 filter_mask)
644{
645 struct switchdev_vlan_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200646 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700647 .skb = skb,
648 .filter_mask = filter_mask,
649 };
650 int err = 0;
651
652 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
653 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200654 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400655 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700656 if (err)
657 goto err_out;
658 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
659 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400660 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700661 }
662
663err_out:
664 return err == -EOPNOTSUPP ? 0 : err;
665}
666
Scott Feldman8793d0a2015-05-10 09:48:04 -0700667/**
668 * switchdev_port_bridge_getlink - Get bridge port attributes
669 *
670 * @dev: port device
671 *
672 * Called for SELF on rtnl_bridge_getlink to get bridge port
673 * attributes.
674 */
675int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
676 struct net_device *dev, u32 filter_mask,
677 int nlflags)
678{
679 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200680 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700681 };
682 u16 mode = BRIDGE_MODE_UNDEF;
683 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
684 int err;
685
686 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400687 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700688 return err;
689
690 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700691 attr.u.brport_flags, mask, nlflags,
692 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700693}
694EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
695
Scott Feldman47f83282015-05-10 09:47:56 -0700696static int switchdev_port_br_setflag(struct net_device *dev,
697 struct nlattr *nlattr,
698 unsigned long brport_flag)
699{
700 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200701 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700702 };
703 u8 flag = nla_get_u8(nlattr);
704 int err;
705
706 err = switchdev_port_attr_get(dev, &attr);
707 if (err)
708 return err;
709
710 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700711 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700712 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700713 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700714
715 return switchdev_port_attr_set(dev, &attr);
716}
717
718static const struct nla_policy
719switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
720 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
721 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
722 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
723 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
724 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
725 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
726 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
727 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
728 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
729 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
730};
731
732static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
733 struct nlattr *protinfo)
734{
735 struct nlattr *attr;
736 int rem;
737 int err;
738
739 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
740 switchdev_port_bridge_policy);
741 if (err)
742 return err;
743
744 nla_for_each_nested(attr, protinfo, rem) {
745 switch (nla_type(attr)) {
746 case IFLA_BRPORT_LEARNING:
747 err = switchdev_port_br_setflag(dev, attr,
748 BR_LEARNING);
749 break;
750 case IFLA_BRPORT_LEARNING_SYNC:
751 err = switchdev_port_br_setflag(dev, attr,
752 BR_LEARNING_SYNC);
753 break;
754 default:
755 err = -EOPNOTSUPP;
756 break;
757 }
758 if (err)
759 return err;
760 }
761
762 return 0;
763}
764
765static int switchdev_port_br_afspec(struct net_device *dev,
766 struct nlattr *afspec,
767 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200768 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700769{
770 struct nlattr *attr;
771 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200772 struct switchdev_obj_port_vlan vlan = {
773 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
774 };
Scott Feldman47f83282015-05-10 09:47:56 -0700775 int rem;
776 int err;
777
778 nla_for_each_nested(attr, afspec, rem) {
779 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
780 continue;
781 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
782 return -EINVAL;
783 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400784 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700785 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400786 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700787 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400788 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200789 /* don't allow range of pvids */
790 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
791 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700792 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400793 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700794 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400795 vlan.vid_end = vinfo->vid;
796 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700797 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200798 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700799 if (err)
800 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400801 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700802 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400803 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700804 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400805 vlan.vid_begin = vinfo->vid;
806 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200807 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700808 if (err)
809 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400810 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700811 }
812 }
813
814 return 0;
815}
816
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800817/**
Scott Feldman47f83282015-05-10 09:47:56 -0700818 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800819 *
820 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700821 * @nlh: netlink header
822 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800823 *
Scott Feldman47f83282015-05-10 09:47:56 -0700824 * Called for SELF on rtnl_bridge_setlink to set bridge port
825 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800826 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700827int switchdev_port_bridge_setlink(struct net_device *dev,
828 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800829{
Scott Feldman47f83282015-05-10 09:47:56 -0700830 struct nlattr *protinfo;
831 struct nlattr *afspec;
832 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800833
Scott Feldman47f83282015-05-10 09:47:56 -0700834 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
835 IFLA_PROTINFO);
836 if (protinfo) {
837 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
838 if (err)
839 return err;
840 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800841
Scott Feldman47f83282015-05-10 09:47:56 -0700842 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
843 IFLA_AF_SPEC);
844 if (afspec)
845 err = switchdev_port_br_afspec(dev, afspec,
846 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800847
Scott Feldman47f83282015-05-10 09:47:56 -0700848 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800849}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700850EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800851
852/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700853 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800854 *
855 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700856 * @nlh: netlink header
857 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800858 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700859 * Called for SELF on rtnl_bridge_dellink to set bridge port
860 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800861 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700862int switchdev_port_bridge_dellink(struct net_device *dev,
863 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800864{
Scott Feldman5c34e022015-05-10 09:48:00 -0700865 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800866
Scott Feldman5c34e022015-05-10 09:48:00 -0700867 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
868 IFLA_AF_SPEC);
869 if (afspec)
870 return switchdev_port_br_afspec(dev, afspec,
871 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800872
Scott Feldman5c34e022015-05-10 09:48:00 -0700873 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800874}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700875EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800876
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700877/**
878 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
879 *
880 * @ndmsg: netlink hdr
881 * @nlattr: netlink attributes
882 * @dev: port device
883 * @addr: MAC address to add
884 * @vid: VLAN to add
885 *
886 * Add FDB entry to switch device.
887 */
888int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
889 struct net_device *dev, const unsigned char *addr,
890 u16 vid, u16 nlm_flags)
891{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200892 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200893 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400894 .addr = addr,
895 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700896 };
897
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200898 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700899}
900EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
901
902/**
903 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
904 *
905 * @ndmsg: netlink hdr
906 * @nlattr: netlink attributes
907 * @dev: port device
908 * @addr: MAC address to delete
909 * @vid: VLAN to delete
910 *
911 * Delete FDB entry from switch device.
912 */
913int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
914 struct net_device *dev, const unsigned char *addr,
915 u16 vid)
916{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200917 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200918 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400919 .addr = addr,
920 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700921 };
922
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200923 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700924}
925EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
926
927struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200928 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400929 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700930 struct sk_buff *skb;
931 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700932 int idx;
933};
934
Jiri Pirko648b4a92015-10-01 11:03:45 +0200935static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700936{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200937 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700938 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400939 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700940 u32 portid = NETLINK_CB(dump->cb->skb).portid;
941 u32 seq = dump->cb->nlh->nlmsg_seq;
942 struct nlmsghdr *nlh;
943 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700944
945 if (dump->idx < dump->cb->args[0])
946 goto skip;
947
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700948 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
949 sizeof(*ndm), NLM_F_MULTI);
950 if (!nlh)
951 return -EMSGSIZE;
952
953 ndm = nlmsg_data(nlh);
954 ndm->ndm_family = AF_BRIDGE;
955 ndm->ndm_pad1 = 0;
956 ndm->ndm_pad2 = 0;
957 ndm->ndm_flags = NTF_SELF;
958 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400959 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400960 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700961
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400962 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700963 goto nla_put_failure;
964
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400965 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700966 goto nla_put_failure;
967
968 nlmsg_end(dump->skb, nlh);
969
970skip:
971 dump->idx++;
972 return 0;
973
974nla_put_failure:
975 nlmsg_cancel(dump->skb, nlh);
976 return -EMSGSIZE;
977}
978
979/**
980 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
981 *
982 * @skb: netlink skb
983 * @cb: netlink callback
984 * @dev: port device
985 * @filter_dev: filter device
986 * @idx:
987 *
988 * Delete FDB entry from switch device.
989 */
990int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
991 struct net_device *dev,
992 struct net_device *filter_dev, int idx)
993{
994 struct switchdev_fdb_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200995 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b22015-09-29 12:07:14 -0400996 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700997 .skb = skb,
998 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700999 .idx = idx,
1000 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001001
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001002 switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07001003 return dump.idx;
1004}
1005EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
1006
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001007static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001008{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001009 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001010 struct net_device *lower_dev;
1011 struct net_device *port_dev;
1012 struct list_head *iter;
1013
1014 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001015 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001016 */
1017
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001018 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001019 return dev;
1020
1021 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001022 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001023 if (port_dev)
1024 return port_dev;
1025 }
1026
1027 return NULL;
1028}
1029
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001030static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001031{
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001032 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001033 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001034 };
1035 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001036 struct net_device *dev = NULL;
1037 int nhsel;
1038
1039 /* For this route, all nexthop devs must be on the same switch. */
1040
1041 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1042 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1043
1044 if (!nh->nh_dev)
1045 return NULL;
1046
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001047 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001048 if (!dev)
1049 return NULL;
1050
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001051 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001052 return NULL;
1053
Scott Feldmand754f982015-07-18 18:24:49 -07001054 if (nhsel > 0 &&
1055 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001056 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001057
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001058 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001059 }
1060
1061 return dev;
1062}
1063
Scott Feldman5e8d9042015-03-05 21:21:15 -08001064/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001065 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001066 *
1067 * @dst: route's IPv4 destination address
1068 * @dst_len: destination address length (prefix length)
1069 * @fi: route FIB info structure
1070 * @tos: route TOS
1071 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001072 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001073 * @tb_id: route table ID
1074 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001075 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001076 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001077int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1078 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001079{
Vivien Didelotab069002015-09-29 12:07:17 -04001080 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001081 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001082 .dst = dst,
1083 .dst_len = dst_len,
1084 .fi = fi,
1085 .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
Scott Feldman8e05fd72015-03-05 21:21:19 -08001093 /* Don't offload route if using custom ip rules or if
1094 * IPv4 FIB offloading has been disabled completely.
1095 */
1096
Scott Feldmane1315db2015-03-06 01:14:36 -08001097#ifdef CONFIG_IP_MULTIPLE_TABLES
1098 if (fi->fib_net->ipv4.fib_has_custom_rules)
1099 return 0;
1100#endif
1101
1102 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001103 return 0;
1104
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001105 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001106 if (!dev)
1107 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001108
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001109 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001110 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001111 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001112
Scott Feldmanaf201f72015-06-10 17:04:49 -07001113 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001114}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001115EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001116
1117/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001118 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001119 *
1120 * @dst: route's IPv4 destination address
1121 * @dst_len: destination address length (prefix length)
1122 * @fi: route FIB info structure
1123 * @tos: route TOS
1124 * @type: route type
1125 * @tb_id: route table ID
1126 *
1127 * Delete IPv4 route entry from switch device.
1128 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001129int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1130 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001131{
Vivien Didelotab069002015-09-29 12:07:17 -04001132 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001133 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001134 .dst = dst,
1135 .dst_len = dst_len,
1136 .fi = fi,
1137 .tos = tos,
1138 .type = type,
1139 .nlflags = 0,
1140 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001141 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001142 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001143 int err = 0;
1144
Roopa Prabhueea39942015-05-13 21:17:41 -07001145 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001146 return 0;
1147
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001148 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001149 if (!dev)
1150 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001151
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001152 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001153 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001154 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001155
Scott Feldmanaf201f72015-06-10 17:04:49 -07001156 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001157}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001158EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001159
1160/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001161 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001162 *
1163 * @fi: route FIB info structure
1164 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001165void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001166{
1167 /* There was a problem installing this route to the offload
1168 * device. For now, until we come up with more refined
1169 * policy handling, abruptly end IPv4 fib offloading for
1170 * for entire net by flushing offload device(s) of all
1171 * IPv4 routes, and mark IPv4 fib offloading broken from
1172 * this point forward.
1173 */
1174
1175 fib_flush_external(fi->fib_net);
1176 fi->fib_net->ipv4.fib_offload_disabled = true;
1177}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001178EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001179
1180static bool switchdev_port_same_parent_id(struct net_device *a,
1181 struct net_device *b)
1182{
1183 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001184 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001185 .flags = SWITCHDEV_F_NO_RECURSE,
1186 };
1187 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001188 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001189 .flags = SWITCHDEV_F_NO_RECURSE,
1190 };
1191
1192 if (switchdev_port_attr_get(a, &a_attr) ||
1193 switchdev_port_attr_get(b, &b_attr))
1194 return false;
1195
1196 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1197}
1198
1199static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1200 struct net_device *group_dev)
1201{
1202 struct net_device *lower_dev;
1203 struct list_head *iter;
1204
1205 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1206 if (lower_dev == dev)
1207 continue;
1208 if (switchdev_port_same_parent_id(dev, lower_dev))
1209 return lower_dev->offload_fwd_mark;
1210 return switchdev_port_fwd_mark_get(dev, lower_dev);
1211 }
1212
1213 return dev->ifindex;
1214}
1215
1216static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1217 u32 old_mark, u32 *reset_mark)
1218{
1219 struct net_device *lower_dev;
1220 struct list_head *iter;
1221
1222 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1223 if (lower_dev->offload_fwd_mark == old_mark) {
1224 if (!*reset_mark)
1225 *reset_mark = lower_dev->ifindex;
1226 lower_dev->offload_fwd_mark = *reset_mark;
1227 }
1228 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1229 }
1230}
1231
1232/**
1233 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1234 *
1235 * @dev: port device
1236 * @group_dev: containing device
1237 * @joining: true if dev is joining group; false if leaving group
1238 *
1239 * An ungrouped port's offload mark is just its ifindex. A grouped
1240 * port's (member of a bridge, for example) offload mark is the ifindex
1241 * of one of the ports in the group with the same parent (switch) ID.
1242 * Ports on the same device in the same group will have the same mark.
1243 *
1244 * Example:
1245 *
1246 * br0 ifindex=9
1247 * sw1p1 ifindex=2 mark=2
1248 * sw1p2 ifindex=3 mark=2
1249 * sw2p1 ifindex=4 mark=5
1250 * sw2p2 ifindex=5 mark=5
1251 *
1252 * If sw2p2 leaves the bridge, we'll have:
1253 *
1254 * br0 ifindex=9
1255 * sw1p1 ifindex=2 mark=2
1256 * sw1p2 ifindex=3 mark=2
1257 * sw2p1 ifindex=4 mark=4
1258 * sw2p2 ifindex=5 mark=5
1259 */
1260void switchdev_port_fwd_mark_set(struct net_device *dev,
1261 struct net_device *group_dev,
1262 bool joining)
1263{
1264 u32 mark = dev->ifindex;
1265 u32 reset_mark = 0;
1266
1267 if (group_dev && joining) {
1268 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1269 } else if (group_dev && !joining) {
1270 if (dev->offload_fwd_mark == mark)
1271 /* Ohoh, this port was the mark reference port,
1272 * but it's leaving the group, so reset the
1273 * mark for the remaining ports in the group.
1274 */
1275 switchdev_port_fwd_mark_reset(group_dev, mark,
1276 &reset_mark);
1277 }
1278
1279 dev->offload_fwd_mark = mark;
1280}
1281EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);