blob: b8aaf820ef65de3e4473edbeaf68958a10bb028e [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>
Scott Feldman5e8d9042015-03-05 21:21:15 -080020#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010021#include <net/switchdev.h>
22
23/**
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020024 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
25 *
26 * @trans: transaction
27 * @data: pointer to data being queued
28 * @destructor: data destructor
29 * @tritem: transaction item being queued
30 *
31 * Enqeueue data item to transaction queue. tritem is typically placed in
32 * cointainter pointed at by data pointer. Destructor is called on
33 * transaction abort and after successful commit phase in case
34 * the caller did not dequeue the item before.
35 */
36void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
37 void *data, void (*destructor)(void const *),
38 struct switchdev_trans_item *tritem)
39{
40 tritem->data = data;
41 tritem->destructor = destructor;
42 list_add_tail(&tritem->list, &trans->item_list);
43}
44EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
45
46static struct switchdev_trans_item *
47__switchdev_trans_item_dequeue(struct switchdev_trans *trans)
48{
49 struct switchdev_trans_item *tritem;
50
51 if (list_empty(&trans->item_list))
52 return NULL;
53 tritem = list_first_entry(&trans->item_list,
54 struct switchdev_trans_item, list);
55 list_del(&tritem->list);
56 return tritem;
57}
58
59/**
60 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
61 *
62 * @trans: transaction
63 */
64void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
65{
66 struct switchdev_trans_item *tritem;
67
68 tritem = __switchdev_trans_item_dequeue(trans);
69 BUG_ON(!tritem);
70 return tritem->data;
71}
72EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
73
74static void switchdev_trans_init(struct switchdev_trans *trans)
75{
76 INIT_LIST_HEAD(&trans->item_list);
77}
78
79static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
80{
81 struct switchdev_trans_item *tritem;
82
83 while ((tritem = __switchdev_trans_item_dequeue(trans)))
84 tritem->destructor(tritem->data);
85}
86
87static void switchdev_trans_items_warn_destroy(struct net_device *dev,
88 struct switchdev_trans *trans)
89{
90 WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
91 dev->name);
92 switchdev_trans_items_destroy(trans);
93}
94
95/**
Scott Feldman30943332015-05-10 09:47:48 -070096 * switchdev_port_attr_get - Get port attribute
97 *
98 * @dev: port device
99 * @attr: attribute to get
100 */
101int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
102{
103 const struct switchdev_ops *ops = dev->switchdev_ops;
104 struct net_device *lower_dev;
105 struct list_head *iter;
106 struct switchdev_attr first = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200107 .id = SWITCHDEV_ATTR_ID_UNDEFINED
Scott Feldman30943332015-05-10 09:47:48 -0700108 };
109 int err = -EOPNOTSUPP;
110
111 if (ops && ops->switchdev_port_attr_get)
112 return ops->switchdev_port_attr_get(dev, attr);
113
114 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
115 return err;
116
117 /* Switch device port(s) may be stacked under
118 * bond/team/vlan dev, so recurse down to get attr on
119 * each port. Return -ENODATA if attr values don't
120 * compare across ports.
121 */
122
123 netdev_for_each_lower_dev(dev, lower_dev, iter) {
124 err = switchdev_port_attr_get(lower_dev, attr);
125 if (err)
126 break;
Jiri Pirko1f868392015-10-01 11:03:42 +0200127 if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED)
Scott Feldman30943332015-05-10 09:47:48 -0700128 first = *attr;
129 else if (memcmp(&first, attr, sizeof(*attr)))
130 return -ENODATA;
131 }
132
133 return err;
134}
135EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
136
137static int __switchdev_port_attr_set(struct net_device *dev,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200138 struct switchdev_attr *attr,
139 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700140{
141 const struct switchdev_ops *ops = dev->switchdev_ops;
142 struct net_device *lower_dev;
143 struct list_head *iter;
144 int err = -EOPNOTSUPP;
145
146 if (ops && ops->switchdev_port_attr_set)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200147 return ops->switchdev_port_attr_set(dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700148
149 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
Scott Feldman464314e2015-10-08 19:23:18 -0700150 goto done;
Scott Feldman30943332015-05-10 09:47:48 -0700151
152 /* Switch device port(s) may be stacked under
153 * bond/team/vlan dev, so recurse down to set attr on
154 * each port.
155 */
156
157 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200158 err = __switchdev_port_attr_set(lower_dev, attr, trans);
Scott Feldman464314e2015-10-08 19:23:18 -0700159 if (err == -EOPNOTSUPP &&
160 attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
161 continue;
Scott Feldman30943332015-05-10 09:47:48 -0700162 if (err)
163 break;
164 }
165
Scott Feldman464314e2015-10-08 19:23:18 -0700166done:
167 if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP)
168 err = 0;
169
Scott Feldman30943332015-05-10 09:47:48 -0700170 return err;
171}
172
173struct switchdev_attr_set_work {
174 struct work_struct work;
175 struct net_device *dev;
176 struct switchdev_attr attr;
177};
178
179static void switchdev_port_attr_set_work(struct work_struct *work)
180{
181 struct switchdev_attr_set_work *asw =
182 container_of(work, struct switchdev_attr_set_work, work);
183 int err;
184
185 rtnl_lock();
186 err = switchdev_port_attr_set(asw->dev, &asw->attr);
Scott Feldman57225e72015-06-11 08:19:01 -0700187 if (err && err != -EOPNOTSUPP)
188 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
189 err, asw->attr.id);
Scott Feldman30943332015-05-10 09:47:48 -0700190 rtnl_unlock();
191
192 dev_put(asw->dev);
193 kfree(work);
194}
195
196static int switchdev_port_attr_set_defer(struct net_device *dev,
197 struct switchdev_attr *attr)
198{
199 struct switchdev_attr_set_work *asw;
200
201 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
202 if (!asw)
203 return -ENOMEM;
204
205 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
206
207 dev_hold(dev);
208 asw->dev = dev;
209 memcpy(&asw->attr, attr, sizeof(asw->attr));
210
211 schedule_work(&asw->work);
212
213 return 0;
214}
215
216/**
217 * switchdev_port_attr_set - Set port attribute
218 *
219 * @dev: port device
220 * @attr: attribute to set
221 *
222 * Use a 2-phase prepare-commit transaction model to ensure
223 * system is not left in a partially updated state due to
224 * failure from driver/device.
225 */
226int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
227{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200228 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700229 int err;
230
231 if (!rtnl_is_locked()) {
232 /* Running prepare-commit transaction across stacked
233 * devices requires nothing moves, so if rtnl_lock is
234 * not held, schedule a worker thread to hold rtnl_lock
235 * while setting attr.
236 */
237
238 return switchdev_port_attr_set_defer(dev, attr);
239 }
240
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200241 switchdev_trans_init(&trans);
242
Scott Feldman30943332015-05-10 09:47:48 -0700243 /* Phase I: prepare for attr set. Driver/device should fail
244 * here if there are going to be issues in the commit phase,
245 * such as lack of resources or support. The driver/device
246 * should reserve resources needed for the commit phase here,
247 * but should not commit the attr.
248 */
249
Jiri Pirkof623ab72015-09-24 10:02:49 +0200250 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200251 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700252 if (err) {
253 /* Prepare phase failed: abort the transaction. Any
254 * resources reserved in the prepare phase are
255 * released.
256 */
257
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200258 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200259 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700260
261 return err;
262 }
263
264 /* Phase II: commit attr set. This cannot fail as a fault
265 * of driver/device. If it does, it's a bug in the driver/device
266 * because the driver said everythings was OK in phase I.
267 */
268
Jiri Pirkof623ab72015-09-24 10:02:49 +0200269 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200270 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700271 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
272 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200273 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700274
275 return err;
276}
277EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
278
Scott Feldman22c1f672015-05-12 23:03:51 -0700279static int __switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200280 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200281 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700282{
283 const struct switchdev_ops *ops = dev->switchdev_ops;
284 struct net_device *lower_dev;
285 struct list_head *iter;
286 int err = -EOPNOTSUPP;
287
288 if (ops && ops->switchdev_port_obj_add)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200289 return ops->switchdev_port_obj_add(dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700290
291 /* Switch device port(s) may be stacked under
292 * bond/team/vlan dev, so recurse down to add object on
293 * each port.
294 */
295
296 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200297 err = __switchdev_port_obj_add(lower_dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700298 if (err)
299 break;
300 }
301
302 return err;
303}
304
305/**
306 * switchdev_port_obj_add - Add port object
307 *
308 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400309 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700310 * @obj: object to add
311 *
312 * Use a 2-phase prepare-commit transaction model to ensure
313 * system is not left in a partially updated state due to
314 * failure from driver/device.
315 *
316 * rtnl_lock must be held.
317 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200318int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200319 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700320{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200321 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700322 int err;
323
324 ASSERT_RTNL();
325
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200326 switchdev_trans_init(&trans);
327
Scott Feldman491d0f12015-05-10 09:47:52 -0700328 /* Phase I: prepare for obj add. Driver/device should fail
329 * here if there are going to be issues in the commit phase,
330 * such as lack of resources or support. The driver/device
331 * should reserve resources needed for the commit phase here,
332 * but should not commit the obj.
333 */
334
Jiri Pirkof623ab72015-09-24 10:02:49 +0200335 trans.ph_prepare = true;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200336 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700337 if (err) {
338 /* Prepare phase failed: abort the transaction. Any
339 * resources reserved in the prepare phase are
340 * released.
341 */
342
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200343 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200344 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700345
346 return err;
347 }
348
349 /* Phase II: commit obj add. This cannot fail as a fault
350 * of driver/device. If it does, it's a bug in the driver/device
351 * because the driver said everythings was OK in phase I.
352 */
353
Jiri Pirkof623ab72015-09-24 10:02:49 +0200354 trans.ph_prepare = false;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200355 err = __switchdev_port_obj_add(dev, obj, &trans);
356 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200357 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700358
359 return err;
360}
361EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
362
363/**
364 * switchdev_port_obj_del - Delete port object
365 *
366 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400367 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700368 * @obj: object to delete
369 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200370int switchdev_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200371 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700372{
373 const struct switchdev_ops *ops = dev->switchdev_ops;
374 struct net_device *lower_dev;
375 struct list_head *iter;
376 int err = -EOPNOTSUPP;
377
378 if (ops && ops->switchdev_port_obj_del)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200379 return ops->switchdev_port_obj_del(dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700380
381 /* Switch device port(s) may be stacked under
382 * bond/team/vlan dev, so recurse down to delete object on
383 * each port.
384 */
385
386 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200387 err = switchdev_port_obj_del(lower_dev, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700388 if (err)
389 break;
390 }
391
392 return err;
393}
394EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
395
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700396/**
397 * switchdev_port_obj_dump - Dump port objects
398 *
399 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400400 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700401 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400402 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700403 */
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200404int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200405 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700406{
407 const struct switchdev_ops *ops = dev->switchdev_ops;
408 struct net_device *lower_dev;
409 struct list_head *iter;
410 int err = -EOPNOTSUPP;
411
412 if (ops && ops->switchdev_port_obj_dump)
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200413 return ops->switchdev_port_obj_dump(dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700414
415 /* Switch device port(s) may be stacked under
416 * bond/team/vlan dev, so recurse down to dump objects on
417 * first port at bottom of stack.
418 */
419
420 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200421 err = switchdev_port_obj_dump(lower_dev, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700422 break;
423 }
424
425 return err;
426}
427EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
428
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700429static DEFINE_MUTEX(switchdev_mutex);
430static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100431
432/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700433 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100434 * @nb: notifier_block
435 *
436 * Register switch device notifier. This should be used by code
437 * which needs to monitor events happening in particular device.
438 * Return values are same as for atomic_notifier_chain_register().
439 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700440int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100441{
442 int err;
443
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700444 mutex_lock(&switchdev_mutex);
445 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
446 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100447 return err;
448}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700449EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100450
451/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700452 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100453 * @nb: notifier_block
454 *
455 * Unregister switch device notifier.
456 * Return values are same as for atomic_notifier_chain_unregister().
457 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700458int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100459{
460 int err;
461
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700462 mutex_lock(&switchdev_mutex);
463 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
464 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100465 return err;
466}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700467EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100468
469/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700470 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100471 * @val: value passed unmodified to notifier function
472 * @dev: port device
473 * @info: notifier information data
474 *
475 * Call all network notifier blocks. This should be called by driver
476 * when it needs to propagate hardware event.
477 * Return values are same as for atomic_notifier_call_chain().
478 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700479int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
480 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100481{
482 int err;
483
484 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700485 mutex_lock(&switchdev_mutex);
486 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
487 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100488 return err;
489}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700490EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800491
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700492struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200493 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700494 struct sk_buff *skb;
495 u32 filter_mask;
496 u16 flags;
497 u16 begin;
498 u16 end;
499};
500
Vivien Didelote23b0022015-09-29 12:07:13 -0400501static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700502{
503 struct bridge_vlan_info vinfo;
504
505 vinfo.flags = dump->flags;
506
507 if (dump->begin == 0 && dump->end == 0) {
508 return 0;
509 } else if (dump->begin == dump->end) {
510 vinfo.vid = dump->begin;
511 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
512 sizeof(vinfo), &vinfo))
513 return -EMSGSIZE;
514 } else {
515 vinfo.vid = dump->begin;
516 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
517 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
518 sizeof(vinfo), &vinfo))
519 return -EMSGSIZE;
520 vinfo.vid = dump->end;
521 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
522 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
523 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
524 sizeof(vinfo), &vinfo))
525 return -EMSGSIZE;
526 }
527
528 return 0;
529}
530
Jiri Pirko648b4a92015-10-01 11:03:45 +0200531static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700532{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200533 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700534 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400535 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700536 int err = 0;
537
538 if (vlan->vid_begin > vlan->vid_end)
539 return -EINVAL;
540
541 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
542 dump->flags = vlan->flags;
543 for (dump->begin = dump->end = vlan->vid_begin;
544 dump->begin <= vlan->vid_end;
545 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400546 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700547 if (err)
548 return err;
549 }
550 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
551 if (dump->begin > vlan->vid_begin &&
552 dump->begin >= vlan->vid_end) {
553 if ((dump->begin - 1) == vlan->vid_end &&
554 dump->flags == vlan->flags) {
555 /* prepend */
556 dump->begin = vlan->vid_begin;
557 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400558 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700559 dump->flags = vlan->flags;
560 dump->begin = vlan->vid_begin;
561 dump->end = vlan->vid_end;
562 }
563 } else if (dump->end <= vlan->vid_begin &&
564 dump->end < vlan->vid_end) {
565 if ((dump->end + 1) == vlan->vid_begin &&
566 dump->flags == vlan->flags) {
567 /* append */
568 dump->end = vlan->vid_end;
569 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400570 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700571 dump->flags = vlan->flags;
572 dump->begin = vlan->vid_begin;
573 dump->end = vlan->vid_end;
574 }
575 } else {
576 err = -EINVAL;
577 }
578 }
579
580 return err;
581}
582
583static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
584 u32 filter_mask)
585{
586 struct switchdev_vlan_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200587 .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700588 .skb = skb,
589 .filter_mask = filter_mask,
590 };
591 int err = 0;
592
593 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
594 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200595 err = switchdev_port_obj_dump(dev, &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400596 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700597 if (err)
598 goto err_out;
599 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
600 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400601 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700602 }
603
604err_out:
605 return err == -EOPNOTSUPP ? 0 : err;
606}
607
Scott Feldman8793d0a2015-05-10 09:48:04 -0700608/**
609 * switchdev_port_bridge_getlink - Get bridge port attributes
610 *
611 * @dev: port device
612 *
613 * Called for SELF on rtnl_bridge_getlink to get bridge port
614 * attributes.
615 */
616int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
617 struct net_device *dev, u32 filter_mask,
618 int nlflags)
619{
620 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200621 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700622 };
623 u16 mode = BRIDGE_MODE_UNDEF;
624 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
625 int err;
626
627 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400628 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700629 return err;
630
631 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700632 attr.u.brport_flags, mask, nlflags,
633 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700634}
635EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
636
Scott Feldman47f83282015-05-10 09:47:56 -0700637static int switchdev_port_br_setflag(struct net_device *dev,
638 struct nlattr *nlattr,
639 unsigned long brport_flag)
640{
641 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200642 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700643 };
644 u8 flag = nla_get_u8(nlattr);
645 int err;
646
647 err = switchdev_port_attr_get(dev, &attr);
648 if (err)
649 return err;
650
651 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700652 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700653 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700654 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700655
656 return switchdev_port_attr_set(dev, &attr);
657}
658
659static const struct nla_policy
660switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
661 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
662 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
663 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
664 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
665 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
666 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
667 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
668 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
669 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
670 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
671};
672
673static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
674 struct nlattr *protinfo)
675{
676 struct nlattr *attr;
677 int rem;
678 int err;
679
680 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
681 switchdev_port_bridge_policy);
682 if (err)
683 return err;
684
685 nla_for_each_nested(attr, protinfo, rem) {
686 switch (nla_type(attr)) {
687 case IFLA_BRPORT_LEARNING:
688 err = switchdev_port_br_setflag(dev, attr,
689 BR_LEARNING);
690 break;
691 case IFLA_BRPORT_LEARNING_SYNC:
692 err = switchdev_port_br_setflag(dev, attr,
693 BR_LEARNING_SYNC);
694 break;
695 default:
696 err = -EOPNOTSUPP;
697 break;
698 }
699 if (err)
700 return err;
701 }
702
703 return 0;
704}
705
706static int switchdev_port_br_afspec(struct net_device *dev,
707 struct nlattr *afspec,
708 int (*f)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200709 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700710{
711 struct nlattr *attr;
712 struct bridge_vlan_info *vinfo;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200713 struct switchdev_obj_port_vlan vlan = {
714 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
715 };
Scott Feldman47f83282015-05-10 09:47:56 -0700716 int rem;
717 int err;
718
719 nla_for_each_nested(attr, afspec, rem) {
720 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
721 continue;
722 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
723 return -EINVAL;
724 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400725 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700726 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400727 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700728 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400729 vlan.vid_begin = vinfo->vid;
Nikolay Aleksandrovcc02aa82015-10-12 14:01:39 +0200730 /* don't allow range of pvids */
731 if (vlan.flags & BRIDGE_VLAN_INFO_PVID)
732 return -EINVAL;
Scott Feldman47f83282015-05-10 09:47:56 -0700733 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400734 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700735 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400736 vlan.vid_end = vinfo->vid;
737 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700738 return -EINVAL;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200739 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700740 if (err)
741 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400742 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700743 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400744 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700745 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400746 vlan.vid_begin = vinfo->vid;
747 vlan.vid_end = vinfo->vid;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200748 err = f(dev, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700749 if (err)
750 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400751 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700752 }
753 }
754
755 return 0;
756}
757
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800758/**
Scott Feldman47f83282015-05-10 09:47:56 -0700759 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800760 *
761 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700762 * @nlh: netlink header
763 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800764 *
Scott Feldman47f83282015-05-10 09:47:56 -0700765 * Called for SELF on rtnl_bridge_setlink to set bridge port
766 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800767 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700768int switchdev_port_bridge_setlink(struct net_device *dev,
769 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800770{
Scott Feldman47f83282015-05-10 09:47:56 -0700771 struct nlattr *protinfo;
772 struct nlattr *afspec;
773 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800774
Scott Feldman47f83282015-05-10 09:47:56 -0700775 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
776 IFLA_PROTINFO);
777 if (protinfo) {
778 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
779 if (err)
780 return err;
781 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800782
Scott Feldman47f83282015-05-10 09:47:56 -0700783 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
784 IFLA_AF_SPEC);
785 if (afspec)
786 err = switchdev_port_br_afspec(dev, afspec,
787 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800788
Scott Feldman47f83282015-05-10 09:47:56 -0700789 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800790}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700791EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800792
793/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700794 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800795 *
796 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700797 * @nlh: netlink header
798 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800799 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700800 * Called for SELF on rtnl_bridge_dellink to set bridge port
801 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800802 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700803int switchdev_port_bridge_dellink(struct net_device *dev,
804 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800805{
Scott Feldman5c34e022015-05-10 09:48:00 -0700806 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800807
Scott Feldman5c34e022015-05-10 09:48:00 -0700808 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
809 IFLA_AF_SPEC);
810 if (afspec)
811 return switchdev_port_br_afspec(dev, afspec,
812 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800813
Scott Feldman5c34e022015-05-10 09:48:00 -0700814 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800815}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700816EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800817
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700818/**
819 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
820 *
821 * @ndmsg: netlink hdr
822 * @nlattr: netlink attributes
823 * @dev: port device
824 * @addr: MAC address to add
825 * @vid: VLAN to add
826 *
827 * Add FDB entry to switch device.
828 */
829int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
830 struct net_device *dev, const unsigned char *addr,
831 u16 vid, u16 nlm_flags)
832{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200833 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200834 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400835 .addr = addr,
836 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700837 };
838
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200839 return switchdev_port_obj_add(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700840}
841EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
842
843/**
844 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
845 *
846 * @ndmsg: netlink hdr
847 * @nlattr: netlink attributes
848 * @dev: port device
849 * @addr: MAC address to delete
850 * @vid: VLAN to delete
851 *
852 * Delete FDB entry from switch device.
853 */
854int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
855 struct net_device *dev, const unsigned char *addr,
856 u16 vid)
857{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200858 struct switchdev_obj_port_fdb fdb = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200859 .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelotab069002015-09-29 12:07:17 -0400860 .addr = addr,
861 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700862 };
863
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200864 return switchdev_port_obj_del(dev, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700865}
866EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
867
868struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200869 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400870 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700871 struct sk_buff *skb;
872 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700873 int idx;
874};
875
Jiri Pirko648b4a92015-10-01 11:03:45 +0200876static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700877{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200878 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700879 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400880 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700881 u32 portid = NETLINK_CB(dump->cb->skb).portid;
882 u32 seq = dump->cb->nlh->nlmsg_seq;
883 struct nlmsghdr *nlh;
884 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700885
886 if (dump->idx < dump->cb->args[0])
887 goto skip;
888
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700889 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
890 sizeof(*ndm), NLM_F_MULTI);
891 if (!nlh)
892 return -EMSGSIZE;
893
894 ndm = nlmsg_data(nlh);
895 ndm->ndm_family = AF_BRIDGE;
896 ndm->ndm_pad1 = 0;
897 ndm->ndm_pad2 = 0;
898 ndm->ndm_flags = NTF_SELF;
899 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400900 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400901 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700902
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400903 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700904 goto nla_put_failure;
905
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400906 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700907 goto nla_put_failure;
908
909 nlmsg_end(dump->skb, nlh);
910
911skip:
912 dump->idx++;
913 return 0;
914
915nla_put_failure:
916 nlmsg_cancel(dump->skb, nlh);
917 return -EMSGSIZE;
918}
919
920/**
921 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
922 *
923 * @skb: netlink skb
924 * @cb: netlink callback
925 * @dev: port device
926 * @filter_dev: filter device
927 * @idx:
928 *
929 * Delete FDB entry from switch device.
930 */
931int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
932 struct net_device *dev,
933 struct net_device *filter_dev, int idx)
934{
935 struct switchdev_fdb_dump dump = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200936 .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB,
Vivien Didelote02a06b22015-09-29 12:07:14 -0400937 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700938 .skb = skb,
939 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700940 .idx = idx,
941 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700942
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200943 switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700944 return dump.idx;
945}
946EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
947
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700948static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800949{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700950 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800951 struct net_device *lower_dev;
952 struct net_device *port_dev;
953 struct list_head *iter;
954
955 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700956 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800957 */
958
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700959 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800960 return dev;
961
962 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700963 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800964 if (port_dev)
965 return port_dev;
966 }
967
968 return NULL;
969}
970
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700971static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800972{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700973 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200974 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700975 };
976 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800977 struct net_device *dev = NULL;
978 int nhsel;
979
980 /* For this route, all nexthop devs must be on the same switch. */
981
982 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
983 const struct fib_nh *nh = &fi->fib_nh[nhsel];
984
985 if (!nh->nh_dev)
986 return NULL;
987
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700988 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800989 if (!dev)
990 return NULL;
991
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700992 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800993 return NULL;
994
Scott Feldmand754f982015-07-18 18:24:49 -0700995 if (nhsel > 0 &&
996 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800997 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800998
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700999 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001000 }
1001
1002 return dev;
1003}
1004
Scott Feldman5e8d9042015-03-05 21:21:15 -08001005/**
Scott Feldman7616dcb2015-06-03 20:43:43 -07001006 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -08001007 *
1008 * @dst: route's IPv4 destination address
1009 * @dst_len: destination address length (prefix length)
1010 * @fi: route FIB info structure
1011 * @tos: route TOS
1012 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001013 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001014 * @tb_id: route table ID
1015 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001016 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001017 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001018int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1019 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001020{
Vivien Didelotab069002015-09-29 12:07:17 -04001021 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001022 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001023 .dst = dst,
1024 .dst_len = dst_len,
1025 .fi = fi,
1026 .tos = tos,
1027 .type = type,
1028 .nlflags = nlflags,
1029 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001030 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001031 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001032 int err = 0;
1033
Scott Feldman8e05fd72015-03-05 21:21:19 -08001034 /* Don't offload route if using custom ip rules or if
1035 * IPv4 FIB offloading has been disabled completely.
1036 */
1037
Scott Feldmane1315db2015-03-06 01:14:36 -08001038#ifdef CONFIG_IP_MULTIPLE_TABLES
1039 if (fi->fib_net->ipv4.fib_has_custom_rules)
1040 return 0;
1041#endif
1042
1043 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001044 return 0;
1045
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001046 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001047 if (!dev)
1048 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001049
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001050 err = switchdev_port_obj_add(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001051 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001052 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001053
Scott Feldmanaf201f72015-06-10 17:04:49 -07001054 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001055}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001056EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001057
1058/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001059 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001060 *
1061 * @dst: route's IPv4 destination address
1062 * @dst_len: destination address length (prefix length)
1063 * @fi: route FIB info structure
1064 * @tos: route TOS
1065 * @type: route type
1066 * @tb_id: route table ID
1067 *
1068 * Delete IPv4 route entry from switch device.
1069 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001070int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1071 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001072{
Vivien Didelotab069002015-09-29 12:07:17 -04001073 struct switchdev_obj_ipv4_fib ipv4_fib = {
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001074 .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB,
Vivien Didelotab069002015-09-29 12:07:17 -04001075 .dst = dst,
1076 .dst_len = dst_len,
1077 .fi = fi,
1078 .tos = tos,
1079 .type = type,
1080 .nlflags = 0,
1081 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001082 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001083 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001084 int err = 0;
1085
Roopa Prabhueea39942015-05-13 21:17:41 -07001086 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001087 return 0;
1088
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001089 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001090 if (!dev)
1091 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001092
Jiri Pirko9e8f4a52015-10-01 11:03:46 +02001093 err = switchdev_port_obj_del(dev, &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001094 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001095 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001096
Scott Feldmanaf201f72015-06-10 17:04:49 -07001097 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001098}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001099EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001100
1101/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001102 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001103 *
1104 * @fi: route FIB info structure
1105 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001106void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001107{
1108 /* There was a problem installing this route to the offload
1109 * device. For now, until we come up with more refined
1110 * policy handling, abruptly end IPv4 fib offloading for
1111 * for entire net by flushing offload device(s) of all
1112 * IPv4 routes, and mark IPv4 fib offloading broken from
1113 * this point forward.
1114 */
1115
1116 fib_flush_external(fi->fib_net);
1117 fi->fib_net->ipv4.fib_offload_disabled = true;
1118}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001119EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001120
1121static bool switchdev_port_same_parent_id(struct net_device *a,
1122 struct net_device *b)
1123{
1124 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001125 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001126 .flags = SWITCHDEV_F_NO_RECURSE,
1127 };
1128 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001129 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001130 .flags = SWITCHDEV_F_NO_RECURSE,
1131 };
1132
1133 if (switchdev_port_attr_get(a, &a_attr) ||
1134 switchdev_port_attr_get(b, &b_attr))
1135 return false;
1136
1137 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1138}
1139
1140static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1141 struct net_device *group_dev)
1142{
1143 struct net_device *lower_dev;
1144 struct list_head *iter;
1145
1146 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1147 if (lower_dev == dev)
1148 continue;
1149 if (switchdev_port_same_parent_id(dev, lower_dev))
1150 return lower_dev->offload_fwd_mark;
1151 return switchdev_port_fwd_mark_get(dev, lower_dev);
1152 }
1153
1154 return dev->ifindex;
1155}
1156
1157static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1158 u32 old_mark, u32 *reset_mark)
1159{
1160 struct net_device *lower_dev;
1161 struct list_head *iter;
1162
1163 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1164 if (lower_dev->offload_fwd_mark == old_mark) {
1165 if (!*reset_mark)
1166 *reset_mark = lower_dev->ifindex;
1167 lower_dev->offload_fwd_mark = *reset_mark;
1168 }
1169 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1170 }
1171}
1172
1173/**
1174 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1175 *
1176 * @dev: port device
1177 * @group_dev: containing device
1178 * @joining: true if dev is joining group; false if leaving group
1179 *
1180 * An ungrouped port's offload mark is just its ifindex. A grouped
1181 * port's (member of a bridge, for example) offload mark is the ifindex
1182 * of one of the ports in the group with the same parent (switch) ID.
1183 * Ports on the same device in the same group will have the same mark.
1184 *
1185 * Example:
1186 *
1187 * br0 ifindex=9
1188 * sw1p1 ifindex=2 mark=2
1189 * sw1p2 ifindex=3 mark=2
1190 * sw2p1 ifindex=4 mark=5
1191 * sw2p2 ifindex=5 mark=5
1192 *
1193 * If sw2p2 leaves the bridge, we'll have:
1194 *
1195 * br0 ifindex=9
1196 * sw1p1 ifindex=2 mark=2
1197 * sw1p2 ifindex=3 mark=2
1198 * sw2p1 ifindex=4 mark=4
1199 * sw2p2 ifindex=5 mark=5
1200 */
1201void switchdev_port_fwd_mark_set(struct net_device *dev,
1202 struct net_device *group_dev,
1203 bool joining)
1204{
1205 u32 mark = dev->ifindex;
1206 u32 reset_mark = 0;
1207
1208 if (group_dev && joining) {
1209 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1210 } else if (group_dev && !joining) {
1211 if (dev->offload_fwd_mark == mark)
1212 /* Ohoh, this port was the mark reference port,
1213 * but it's leaving the group, so reset the
1214 * mark for the remaining ports in the group.
1215 */
1216 switchdev_port_fwd_mark_reset(group_dev, mark,
1217 &reset_mark);
1218 }
1219
1220 dev->offload_fwd_mark = mark;
1221}
1222EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);