blob: 93f4971e68dbad66427bf56ec323449661613813 [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 = {
107 .id = SWITCHDEV_ATTR_UNDEFINED
108 };
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;
127 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
128 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)
150 return err;
151
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 Feldman30943332015-05-10 09:47:48 -0700159 if (err)
160 break;
161 }
162
163 return err;
164}
165
166struct switchdev_attr_set_work {
167 struct work_struct work;
168 struct net_device *dev;
169 struct switchdev_attr attr;
170};
171
172static void switchdev_port_attr_set_work(struct work_struct *work)
173{
174 struct switchdev_attr_set_work *asw =
175 container_of(work, struct switchdev_attr_set_work, work);
176 int err;
177
178 rtnl_lock();
179 err = switchdev_port_attr_set(asw->dev, &asw->attr);
Scott Feldman57225e772015-06-11 08:19:01 -0700180 if (err && err != -EOPNOTSUPP)
181 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
182 err, asw->attr.id);
Scott Feldman30943332015-05-10 09:47:48 -0700183 rtnl_unlock();
184
185 dev_put(asw->dev);
186 kfree(work);
187}
188
189static int switchdev_port_attr_set_defer(struct net_device *dev,
190 struct switchdev_attr *attr)
191{
192 struct switchdev_attr_set_work *asw;
193
194 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
195 if (!asw)
196 return -ENOMEM;
197
198 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
199
200 dev_hold(dev);
201 asw->dev = dev;
202 memcpy(&asw->attr, attr, sizeof(asw->attr));
203
204 schedule_work(&asw->work);
205
206 return 0;
207}
208
209/**
210 * switchdev_port_attr_set - Set port attribute
211 *
212 * @dev: port device
213 * @attr: attribute to set
214 *
215 * Use a 2-phase prepare-commit transaction model to ensure
216 * system is not left in a partially updated state due to
217 * failure from driver/device.
218 */
219int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
220{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200221 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700222 int err;
223
224 if (!rtnl_is_locked()) {
225 /* Running prepare-commit transaction across stacked
226 * devices requires nothing moves, so if rtnl_lock is
227 * not held, schedule a worker thread to hold rtnl_lock
228 * while setting attr.
229 */
230
231 return switchdev_port_attr_set_defer(dev, attr);
232 }
233
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200234 switchdev_trans_init(&trans);
235
Scott Feldman30943332015-05-10 09:47:48 -0700236 /* Phase I: prepare for attr set. Driver/device should fail
237 * here if there are going to be issues in the commit phase,
238 * such as lack of resources or support. The driver/device
239 * should reserve resources needed for the commit phase here,
240 * but should not commit the attr.
241 */
242
Jiri Pirkof623ab72015-09-24 10:02:49 +0200243 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200244 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700245 if (err) {
246 /* Prepare phase failed: abort the transaction. Any
247 * resources reserved in the prepare phase are
248 * released.
249 */
250
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200251 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200252 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700253
254 return err;
255 }
256
257 /* Phase II: commit attr set. This cannot fail as a fault
258 * of driver/device. If it does, it's a bug in the driver/device
259 * because the driver said everythings was OK in phase I.
260 */
261
Jiri Pirkof623ab72015-09-24 10:02:49 +0200262 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200263 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700264 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
265 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200266 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700267
268 return err;
269}
270EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
271
Scott Feldman22c1f672015-05-12 23:03:51 -0700272static int __switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200273 struct switchdev_obj *obj,
274 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700275{
276 const struct switchdev_ops *ops = dev->switchdev_ops;
277 struct net_device *lower_dev;
278 struct list_head *iter;
279 int err = -EOPNOTSUPP;
280
281 if (ops && ops->switchdev_port_obj_add)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200282 return ops->switchdev_port_obj_add(dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700283
284 /* Switch device port(s) may be stacked under
285 * bond/team/vlan dev, so recurse down to add object on
286 * each port.
287 */
288
289 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200290 err = __switchdev_port_obj_add(lower_dev, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700291 if (err)
292 break;
293 }
294
295 return err;
296}
297
298/**
299 * switchdev_port_obj_add - Add port object
300 *
301 * @dev: port device
302 * @obj: object to add
303 *
304 * Use a 2-phase prepare-commit transaction model to ensure
305 * system is not left in a partially updated state due to
306 * failure from driver/device.
307 *
308 * rtnl_lock must be held.
309 */
310int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
311{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200312 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700313 int err;
314
315 ASSERT_RTNL();
316
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200317 switchdev_trans_init(&trans);
318
Scott Feldman491d0f12015-05-10 09:47:52 -0700319 /* Phase I: prepare for obj add. Driver/device should fail
320 * here if there are going to be issues in the commit phase,
321 * such as lack of resources or support. The driver/device
322 * should reserve resources needed for the commit phase here,
323 * but should not commit the obj.
324 */
325
Jiri Pirkof623ab72015-09-24 10:02:49 +0200326 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200327 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700328 if (err) {
329 /* Prepare phase failed: abort the transaction. Any
330 * resources reserved in the prepare phase are
331 * released.
332 */
333
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200334 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200335 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700336
337 return err;
338 }
339
340 /* Phase II: commit obj add. This cannot fail as a fault
341 * of driver/device. If it does, it's a bug in the driver/device
342 * because the driver said everythings was OK in phase I.
343 */
344
Jiri Pirkof623ab72015-09-24 10:02:49 +0200345 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200346 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700347 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200348 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700349
350 return err;
351}
352EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
353
354/**
355 * switchdev_port_obj_del - Delete port object
356 *
357 * @dev: port device
358 * @obj: object to delete
359 */
360int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
361{
362 const struct switchdev_ops *ops = dev->switchdev_ops;
363 struct net_device *lower_dev;
364 struct list_head *iter;
365 int err = -EOPNOTSUPP;
366
367 if (ops && ops->switchdev_port_obj_del)
368 return ops->switchdev_port_obj_del(dev, obj);
369
370 /* Switch device port(s) may be stacked under
371 * bond/team/vlan dev, so recurse down to delete object on
372 * each port.
373 */
374
375 netdev_for_each_lower_dev(dev, lower_dev, iter) {
376 err = switchdev_port_obj_del(lower_dev, obj);
377 if (err)
378 break;
379 }
380
381 return err;
382}
383EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
384
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700385/**
386 * switchdev_port_obj_dump - Dump port objects
387 *
388 * @dev: port device
389 * @obj: object to dump
390 */
391int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
392{
393 const struct switchdev_ops *ops = dev->switchdev_ops;
394 struct net_device *lower_dev;
395 struct list_head *iter;
396 int err = -EOPNOTSUPP;
397
398 if (ops && ops->switchdev_port_obj_dump)
399 return ops->switchdev_port_obj_dump(dev, obj);
400
401 /* Switch device port(s) may be stacked under
402 * bond/team/vlan dev, so recurse down to dump objects on
403 * first port at bottom of stack.
404 */
405
406 netdev_for_each_lower_dev(dev, lower_dev, iter) {
407 err = switchdev_port_obj_dump(lower_dev, obj);
408 break;
409 }
410
411 return err;
412}
413EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
414
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700415static DEFINE_MUTEX(switchdev_mutex);
416static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100417
418/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700419 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100420 * @nb: notifier_block
421 *
422 * Register switch device notifier. This should be used by code
423 * which needs to monitor events happening in particular device.
424 * Return values are same as for atomic_notifier_chain_register().
425 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700426int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100427{
428 int err;
429
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700430 mutex_lock(&switchdev_mutex);
431 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
432 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100433 return err;
434}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700435EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100436
437/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700438 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100439 * @nb: notifier_block
440 *
441 * Unregister switch device notifier.
442 * Return values are same as for atomic_notifier_chain_unregister().
443 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700444int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100445{
446 int err;
447
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700448 mutex_lock(&switchdev_mutex);
449 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
450 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100451 return err;
452}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700453EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100454
455/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700456 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100457 * @val: value passed unmodified to notifier function
458 * @dev: port device
459 * @info: notifier information data
460 *
461 * Call all network notifier blocks. This should be called by driver
462 * when it needs to propagate hardware event.
463 * Return values are same as for atomic_notifier_call_chain().
464 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700465int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
466 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100467{
468 int err;
469
470 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700471 mutex_lock(&switchdev_mutex);
472 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
473 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100474 return err;
475}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700476EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800477
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700478struct switchdev_vlan_dump {
479 struct switchdev_obj obj;
480 struct sk_buff *skb;
481 u32 filter_mask;
482 u16 flags;
483 u16 begin;
484 u16 end;
485};
486
Vivien Didelote23b0022015-09-29 12:07:13 -0400487static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700488{
489 struct bridge_vlan_info vinfo;
490
491 vinfo.flags = dump->flags;
492
493 if (dump->begin == 0 && dump->end == 0) {
494 return 0;
495 } else if (dump->begin == dump->end) {
496 vinfo.vid = dump->begin;
497 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
498 sizeof(vinfo), &vinfo))
499 return -EMSGSIZE;
500 } else {
501 vinfo.vid = dump->begin;
502 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
503 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
504 sizeof(vinfo), &vinfo))
505 return -EMSGSIZE;
506 vinfo.vid = dump->end;
507 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
508 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
509 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
510 sizeof(vinfo), &vinfo))
511 return -EMSGSIZE;
512 }
513
514 return 0;
515}
516
Vivien Didelot03d5fb12015-09-29 12:07:15 -0400517static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700518{
519 struct switchdev_vlan_dump *dump =
520 container_of(obj, struct switchdev_vlan_dump, obj);
521 struct switchdev_obj_vlan *vlan = &dump->obj.u.vlan;
522 int err = 0;
523
524 if (vlan->vid_begin > vlan->vid_end)
525 return -EINVAL;
526
527 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
528 dump->flags = vlan->flags;
529 for (dump->begin = dump->end = vlan->vid_begin;
530 dump->begin <= vlan->vid_end;
531 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400532 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700533 if (err)
534 return err;
535 }
536 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
537 if (dump->begin > vlan->vid_begin &&
538 dump->begin >= vlan->vid_end) {
539 if ((dump->begin - 1) == vlan->vid_end &&
540 dump->flags == vlan->flags) {
541 /* prepend */
542 dump->begin = vlan->vid_begin;
543 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400544 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700545 dump->flags = vlan->flags;
546 dump->begin = vlan->vid_begin;
547 dump->end = vlan->vid_end;
548 }
549 } else if (dump->end <= vlan->vid_begin &&
550 dump->end < vlan->vid_end) {
551 if ((dump->end + 1) == vlan->vid_begin &&
552 dump->flags == vlan->flags) {
553 /* append */
554 dump->end = vlan->vid_end;
555 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400556 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700557 dump->flags = vlan->flags;
558 dump->begin = vlan->vid_begin;
559 dump->end = vlan->vid_end;
560 }
561 } else {
562 err = -EINVAL;
563 }
564 }
565
566 return err;
567}
568
569static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
570 u32 filter_mask)
571{
572 struct switchdev_vlan_dump dump = {
573 .obj = {
574 .id = SWITCHDEV_OBJ_PORT_VLAN,
575 .cb = switchdev_port_vlan_dump_cb,
576 },
577 .skb = skb,
578 .filter_mask = filter_mask,
579 };
580 int err = 0;
581
582 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
583 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
584 err = switchdev_port_obj_dump(dev, &dump.obj);
585 if (err)
586 goto err_out;
587 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
588 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400589 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700590 }
591
592err_out:
593 return err == -EOPNOTSUPP ? 0 : err;
594}
595
Scott Feldman8793d0a2015-05-10 09:48:04 -0700596/**
597 * switchdev_port_bridge_getlink - Get bridge port attributes
598 *
599 * @dev: port device
600 *
601 * Called for SELF on rtnl_bridge_getlink to get bridge port
602 * attributes.
603 */
604int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
605 struct net_device *dev, u32 filter_mask,
606 int nlflags)
607{
608 struct switchdev_attr attr = {
609 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
610 };
611 u16 mode = BRIDGE_MODE_UNDEF;
612 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
613 int err;
614
615 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400616 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700617 return err;
618
619 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700620 attr.u.brport_flags, mask, nlflags,
621 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700622}
623EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
624
Scott Feldman47f83282015-05-10 09:47:56 -0700625static int switchdev_port_br_setflag(struct net_device *dev,
626 struct nlattr *nlattr,
627 unsigned long brport_flag)
628{
629 struct switchdev_attr attr = {
630 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
631 };
632 u8 flag = nla_get_u8(nlattr);
633 int err;
634
635 err = switchdev_port_attr_get(dev, &attr);
636 if (err)
637 return err;
638
639 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700640 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700641 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700642 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700643
644 return switchdev_port_attr_set(dev, &attr);
645}
646
647static const struct nla_policy
648switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
649 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
650 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
651 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
652 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
653 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
654 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
655 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
656 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
657 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
658 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
659};
660
661static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
662 struct nlattr *protinfo)
663{
664 struct nlattr *attr;
665 int rem;
666 int err;
667
668 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
669 switchdev_port_bridge_policy);
670 if (err)
671 return err;
672
673 nla_for_each_nested(attr, protinfo, rem) {
674 switch (nla_type(attr)) {
675 case IFLA_BRPORT_LEARNING:
676 err = switchdev_port_br_setflag(dev, attr,
677 BR_LEARNING);
678 break;
679 case IFLA_BRPORT_LEARNING_SYNC:
680 err = switchdev_port_br_setflag(dev, attr,
681 BR_LEARNING_SYNC);
682 break;
683 default:
684 err = -EOPNOTSUPP;
685 break;
686 }
687 if (err)
688 return err;
689 }
690
691 return 0;
692}
693
694static int switchdev_port_br_afspec(struct net_device *dev,
695 struct nlattr *afspec,
696 int (*f)(struct net_device *dev,
697 struct switchdev_obj *obj))
698{
699 struct nlattr *attr;
700 struct bridge_vlan_info *vinfo;
701 struct switchdev_obj obj = {
702 .id = SWITCHDEV_OBJ_PORT_VLAN,
703 };
Scott Feldman42275bd2015-05-13 11:16:50 -0700704 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
Scott Feldman47f83282015-05-10 09:47:56 -0700705 int rem;
706 int err;
707
708 nla_for_each_nested(attr, afspec, rem) {
709 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
710 continue;
711 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
712 return -EINVAL;
713 vinfo = nla_data(attr);
Scott Feldman42275bd2015-05-13 11:16:50 -0700714 vlan->flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700715 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700716 if (vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700717 return -EINVAL;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700718 vlan->vid_begin = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700719 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700720 if (!vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700721 return -EINVAL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700722 vlan->vid_end = vinfo->vid;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700723 if (vlan->vid_end <= vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700724 return -EINVAL;
725 err = f(dev, &obj);
726 if (err)
727 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700728 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700729 } else {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700730 if (vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700731 return -EINVAL;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700732 vlan->vid_begin = vinfo->vid;
Scott Feldman42275bd2015-05-13 11:16:50 -0700733 vlan->vid_end = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700734 err = f(dev, &obj);
735 if (err)
736 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700737 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700738 }
739 }
740
741 return 0;
742}
743
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800744/**
Scott Feldman47f83282015-05-10 09:47:56 -0700745 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800746 *
747 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700748 * @nlh: netlink header
749 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800750 *
Scott Feldman47f83282015-05-10 09:47:56 -0700751 * Called for SELF on rtnl_bridge_setlink to set bridge port
752 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800753 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700754int switchdev_port_bridge_setlink(struct net_device *dev,
755 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800756{
Scott Feldman47f83282015-05-10 09:47:56 -0700757 struct nlattr *protinfo;
758 struct nlattr *afspec;
759 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800760
Scott Feldman47f83282015-05-10 09:47:56 -0700761 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
762 IFLA_PROTINFO);
763 if (protinfo) {
764 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
765 if (err)
766 return err;
767 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800768
Scott Feldman47f83282015-05-10 09:47:56 -0700769 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
770 IFLA_AF_SPEC);
771 if (afspec)
772 err = switchdev_port_br_afspec(dev, afspec,
773 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800774
Scott Feldman47f83282015-05-10 09:47:56 -0700775 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800776}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700777EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800778
779/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700780 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800781 *
782 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700783 * @nlh: netlink header
784 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800785 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700786 * Called for SELF on rtnl_bridge_dellink to set bridge port
787 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800788 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700789int switchdev_port_bridge_dellink(struct net_device *dev,
790 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800791{
Scott Feldman5c34e022015-05-10 09:48:00 -0700792 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800793
Scott Feldman5c34e022015-05-10 09:48:00 -0700794 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
795 IFLA_AF_SPEC);
796 if (afspec)
797 return switchdev_port_br_afspec(dev, afspec,
798 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800799
Scott Feldman5c34e022015-05-10 09:48:00 -0700800 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800801}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700802EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800803
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700804/**
805 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
806 *
807 * @ndmsg: netlink hdr
808 * @nlattr: netlink attributes
809 * @dev: port device
810 * @addr: MAC address to add
811 * @vid: VLAN to add
812 *
813 * Add FDB entry to switch device.
814 */
815int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
816 struct net_device *dev, const unsigned char *addr,
817 u16 vid, u16 nlm_flags)
818{
819 struct switchdev_obj obj = {
820 .id = SWITCHDEV_OBJ_PORT_FDB,
821 .u.fdb = {
David S. Millercdf09692015-08-11 12:00:37 -0700822 .addr = addr,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700823 .vid = vid,
824 },
825 };
826
827 return switchdev_port_obj_add(dev, &obj);
828}
829EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
830
831/**
832 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
833 *
834 * @ndmsg: netlink hdr
835 * @nlattr: netlink attributes
836 * @dev: port device
837 * @addr: MAC address to delete
838 * @vid: VLAN to delete
839 *
840 * Delete FDB entry from switch device.
841 */
842int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
843 struct net_device *dev, const unsigned char *addr,
844 u16 vid)
845{
846 struct switchdev_obj obj = {
847 .id = SWITCHDEV_OBJ_PORT_FDB,
848 .u.fdb = {
David S. Millercdf09692015-08-11 12:00:37 -0700849 .addr = addr,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700850 .vid = vid,
851 },
852 };
853
854 return switchdev_port_obj_del(dev, &obj);
855}
856EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
857
858struct switchdev_fdb_dump {
859 struct switchdev_obj obj;
Vivien Didelote02a06b2015-09-29 12:07:14 -0400860 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700861 struct sk_buff *skb;
862 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700863 int idx;
864};
865
Vivien Didelot03d5fb12015-09-29 12:07:15 -0400866static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700867{
868 struct switchdev_fdb_dump *dump =
869 container_of(obj, struct switchdev_fdb_dump, obj);
870 u32 portid = NETLINK_CB(dump->cb->skb).portid;
871 u32 seq = dump->cb->nlh->nlmsg_seq;
872 struct nlmsghdr *nlh;
873 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700874
875 if (dump->idx < dump->cb->args[0])
876 goto skip;
877
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700878 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
879 sizeof(*ndm), NLM_F_MULTI);
880 if (!nlh)
881 return -EMSGSIZE;
882
883 ndm = nlmsg_data(nlh);
884 ndm->ndm_family = AF_BRIDGE;
885 ndm->ndm_pad1 = 0;
886 ndm->ndm_pad2 = 0;
887 ndm->ndm_flags = NTF_SELF;
888 ndm->ndm_type = 0;
Vivien Didelote02a06b2015-09-29 12:07:14 -0400889 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelotce80e7b2015-08-10 09:09:52 -0400890 ndm->ndm_state = obj->u.fdb.ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700891
892 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
893 goto nla_put_failure;
894
895 if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
896 goto nla_put_failure;
897
898 nlmsg_end(dump->skb, nlh);
899
900skip:
901 dump->idx++;
902 return 0;
903
904nla_put_failure:
905 nlmsg_cancel(dump->skb, nlh);
906 return -EMSGSIZE;
907}
908
909/**
910 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
911 *
912 * @skb: netlink skb
913 * @cb: netlink callback
914 * @dev: port device
915 * @filter_dev: filter device
916 * @idx:
917 *
918 * Delete FDB entry from switch device.
919 */
920int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
921 struct net_device *dev,
922 struct net_device *filter_dev, int idx)
923{
924 struct switchdev_fdb_dump dump = {
925 .obj = {
926 .id = SWITCHDEV_OBJ_PORT_FDB,
927 .cb = switchdev_port_fdb_dump_cb,
928 },
Vivien Didelote02a06b2015-09-29 12:07:14 -0400929 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700930 .skb = skb,
931 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700932 .idx = idx,
933 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700934
Jiri Pirko0890cf62015-09-03 14:04:17 +0200935 switchdev_port_obj_dump(dev, &dump.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700936 return dump.idx;
937}
938EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
939
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700940static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800941{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700942 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800943 struct net_device *lower_dev;
944 struct net_device *port_dev;
945 struct list_head *iter;
946
947 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700948 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800949 */
950
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700951 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800952 return dev;
953
954 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700955 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800956 if (port_dev)
957 return port_dev;
958 }
959
960 return NULL;
961}
962
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700963static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800964{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700965 struct switchdev_attr attr = {
966 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
967 };
968 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800969 struct net_device *dev = NULL;
970 int nhsel;
971
972 /* For this route, all nexthop devs must be on the same switch. */
973
974 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
975 const struct fib_nh *nh = &fi->fib_nh[nhsel];
976
977 if (!nh->nh_dev)
978 return NULL;
979
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700980 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800981 if (!dev)
982 return NULL;
983
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700984 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800985 return NULL;
986
Scott Feldmand754f982015-07-18 18:24:49 -0700987 if (nhsel > 0 &&
988 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800989 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800990
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700991 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800992 }
993
994 return dev;
995}
996
Scott Feldman5e8d9042015-03-05 21:21:15 -0800997/**
Scott Feldman7616dcb2015-06-03 20:43:43 -0700998 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -0800999 *
1000 * @dst: route's IPv4 destination address
1001 * @dst_len: destination address length (prefix length)
1002 * @fi: route FIB info structure
1003 * @tos: route TOS
1004 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001005 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001006 * @tb_id: route table ID
1007 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001008 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001009 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001010int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1011 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001012{
Scott Feldman58c2cb12015-05-10 09:48:06 -07001013 struct switchdev_obj fib_obj = {
1014 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -07001015 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -07001016 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001017 .dst_len = dst_len,
1018 .fi = fi,
1019 .tos = tos,
1020 .type = type,
1021 .nlflags = nlflags,
1022 .tb_id = tb_id,
1023 },
1024 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001025 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001026 int err = 0;
1027
Scott Feldman8e05fd72015-03-05 21:21:19 -08001028 /* Don't offload route if using custom ip rules or if
1029 * IPv4 FIB offloading has been disabled completely.
1030 */
1031
Scott Feldmane1315db2015-03-06 01:14:36 -08001032#ifdef CONFIG_IP_MULTIPLE_TABLES
1033 if (fi->fib_net->ipv4.fib_has_custom_rules)
1034 return 0;
1035#endif
1036
1037 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001038 return 0;
1039
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001040 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001041 if (!dev)
1042 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001043
Scott Feldman58c2cb12015-05-10 09:48:06 -07001044 err = switchdev_port_obj_add(dev, &fib_obj);
1045 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001046 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001047
Scott Feldmanaf201f72015-06-10 17:04:49 -07001048 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001049}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001050EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001051
1052/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001053 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001054 *
1055 * @dst: route's IPv4 destination address
1056 * @dst_len: destination address length (prefix length)
1057 * @fi: route FIB info structure
1058 * @tos: route TOS
1059 * @type: route type
1060 * @tb_id: route table ID
1061 *
1062 * Delete IPv4 route entry from switch device.
1063 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001064int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1065 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001066{
Scott Feldman58c2cb12015-05-10 09:48:06 -07001067 struct switchdev_obj fib_obj = {
1068 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -07001069 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -07001070 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001071 .dst_len = dst_len,
1072 .fi = fi,
1073 .tos = tos,
1074 .type = type,
1075 .nlflags = 0,
1076 .tb_id = tb_id,
1077 },
1078 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001079 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001080 int err = 0;
1081
Roopa Prabhueea39942015-05-13 21:17:41 -07001082 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001083 return 0;
1084
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001085 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001086 if (!dev)
1087 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001088
Scott Feldman58c2cb12015-05-10 09:48:06 -07001089 err = switchdev_port_obj_del(dev, &fib_obj);
1090 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001091 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001092
Scott Feldmanaf201f72015-06-10 17:04:49 -07001093 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001094}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001095EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001096
1097/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001098 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001099 *
1100 * @fi: route FIB info structure
1101 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001102void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001103{
1104 /* There was a problem installing this route to the offload
1105 * device. For now, until we come up with more refined
1106 * policy handling, abruptly end IPv4 fib offloading for
1107 * for entire net by flushing offload device(s) of all
1108 * IPv4 routes, and mark IPv4 fib offloading broken from
1109 * this point forward.
1110 */
1111
1112 fib_flush_external(fi->fib_net);
1113 fi->fib_net->ipv4.fib_offload_disabled = true;
1114}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001115EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001116
1117static bool switchdev_port_same_parent_id(struct net_device *a,
1118 struct net_device *b)
1119{
1120 struct switchdev_attr a_attr = {
1121 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1122 .flags = SWITCHDEV_F_NO_RECURSE,
1123 };
1124 struct switchdev_attr b_attr = {
1125 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1126 .flags = SWITCHDEV_F_NO_RECURSE,
1127 };
1128
1129 if (switchdev_port_attr_get(a, &a_attr) ||
1130 switchdev_port_attr_get(b, &b_attr))
1131 return false;
1132
1133 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1134}
1135
1136static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1137 struct net_device *group_dev)
1138{
1139 struct net_device *lower_dev;
1140 struct list_head *iter;
1141
1142 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1143 if (lower_dev == dev)
1144 continue;
1145 if (switchdev_port_same_parent_id(dev, lower_dev))
1146 return lower_dev->offload_fwd_mark;
1147 return switchdev_port_fwd_mark_get(dev, lower_dev);
1148 }
1149
1150 return dev->ifindex;
1151}
1152
1153static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1154 u32 old_mark, u32 *reset_mark)
1155{
1156 struct net_device *lower_dev;
1157 struct list_head *iter;
1158
1159 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1160 if (lower_dev->offload_fwd_mark == old_mark) {
1161 if (!*reset_mark)
1162 *reset_mark = lower_dev->ifindex;
1163 lower_dev->offload_fwd_mark = *reset_mark;
1164 }
1165 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1166 }
1167}
1168
1169/**
1170 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1171 *
1172 * @dev: port device
1173 * @group_dev: containing device
1174 * @joining: true if dev is joining group; false if leaving group
1175 *
1176 * An ungrouped port's offload mark is just its ifindex. A grouped
1177 * port's (member of a bridge, for example) offload mark is the ifindex
1178 * of one of the ports in the group with the same parent (switch) ID.
1179 * Ports on the same device in the same group will have the same mark.
1180 *
1181 * Example:
1182 *
1183 * br0 ifindex=9
1184 * sw1p1 ifindex=2 mark=2
1185 * sw1p2 ifindex=3 mark=2
1186 * sw2p1 ifindex=4 mark=5
1187 * sw2p2 ifindex=5 mark=5
1188 *
1189 * If sw2p2 leaves the bridge, we'll have:
1190 *
1191 * br0 ifindex=9
1192 * sw1p1 ifindex=2 mark=2
1193 * sw1p2 ifindex=3 mark=2
1194 * sw2p1 ifindex=4 mark=4
1195 * sw2p2 ifindex=5 mark=5
1196 */
1197void switchdev_port_fwd_mark_set(struct net_device *dev,
1198 struct net_device *group_dev,
1199 bool joining)
1200{
1201 u32 mark = dev->ifindex;
1202 u32 reset_mark = 0;
1203
1204 if (group_dev && joining) {
1205 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1206 } else if (group_dev && !joining) {
1207 if (dev->offload_fwd_mark == mark)
1208 /* Ohoh, this port was the mark reference port,
1209 * but it's leaving the group, so reset the
1210 * mark for the remaining ports in the group.
1211 */
1212 switchdev_port_fwd_mark_reset(group_dev, mark,
1213 &reset_mark);
1214 }
1215
1216 dev->offload_fwd_mark = mark;
1217}
1218EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);