blob: 3e4b35a0c8cbc7ea1eddc0128729b8cb54b8bf81 [file] [log] [blame]
Jiri Pirko007f7902014-11-28 14:34:17 +01001/*
2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 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>
Scott Feldman5e8d9042015-03-05 21:21:15 -080019#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010020#include <net/switchdev.h>
21
22/**
Scott Feldman30943332015-05-10 09:47:48 -070023 * switchdev_port_attr_get - Get port attribute
24 *
25 * @dev: port device
26 * @attr: attribute to get
27 */
28int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
29{
30 const struct switchdev_ops *ops = dev->switchdev_ops;
31 struct net_device *lower_dev;
32 struct list_head *iter;
33 struct switchdev_attr first = {
34 .id = SWITCHDEV_ATTR_UNDEFINED
35 };
36 int err = -EOPNOTSUPP;
37
38 if (ops && ops->switchdev_port_attr_get)
39 return ops->switchdev_port_attr_get(dev, attr);
40
41 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
42 return err;
43
44 /* Switch device port(s) may be stacked under
45 * bond/team/vlan dev, so recurse down to get attr on
46 * each port. Return -ENODATA if attr values don't
47 * compare across ports.
48 */
49
50 netdev_for_each_lower_dev(dev, lower_dev, iter) {
51 err = switchdev_port_attr_get(lower_dev, attr);
52 if (err)
53 break;
54 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
55 first = *attr;
56 else if (memcmp(&first, attr, sizeof(*attr)))
57 return -ENODATA;
58 }
59
60 return err;
61}
62EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
63
64static int __switchdev_port_attr_set(struct net_device *dev,
65 struct switchdev_attr *attr)
66{
67 const struct switchdev_ops *ops = dev->switchdev_ops;
68 struct net_device *lower_dev;
69 struct list_head *iter;
70 int err = -EOPNOTSUPP;
71
72 if (ops && ops->switchdev_port_attr_set)
73 return ops->switchdev_port_attr_set(dev, attr);
74
75 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
76 return err;
77
78 /* Switch device port(s) may be stacked under
79 * bond/team/vlan dev, so recurse down to set attr on
80 * each port.
81 */
82
83 netdev_for_each_lower_dev(dev, lower_dev, iter) {
84 err = __switchdev_port_attr_set(lower_dev, attr);
85 if (err)
86 break;
87 }
88
89 return err;
90}
91
92struct switchdev_attr_set_work {
93 struct work_struct work;
94 struct net_device *dev;
95 struct switchdev_attr attr;
96};
97
98static void switchdev_port_attr_set_work(struct work_struct *work)
99{
100 struct switchdev_attr_set_work *asw =
101 container_of(work, struct switchdev_attr_set_work, work);
102 int err;
103
104 rtnl_lock();
105 err = switchdev_port_attr_set(asw->dev, &asw->attr);
Scott Feldman57225e72015-06-11 08:19:01 -0700106 if (err && err != -EOPNOTSUPP)
107 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
108 err, asw->attr.id);
Scott Feldman30943332015-05-10 09:47:48 -0700109 rtnl_unlock();
110
111 dev_put(asw->dev);
112 kfree(work);
113}
114
115static int switchdev_port_attr_set_defer(struct net_device *dev,
116 struct switchdev_attr *attr)
117{
118 struct switchdev_attr_set_work *asw;
119
120 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
121 if (!asw)
122 return -ENOMEM;
123
124 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
125
126 dev_hold(dev);
127 asw->dev = dev;
128 memcpy(&asw->attr, attr, sizeof(asw->attr));
129
130 schedule_work(&asw->work);
131
132 return 0;
133}
134
135/**
136 * switchdev_port_attr_set - Set port attribute
137 *
138 * @dev: port device
139 * @attr: attribute to set
140 *
141 * Use a 2-phase prepare-commit transaction model to ensure
142 * system is not left in a partially updated state due to
143 * failure from driver/device.
144 */
145int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
146{
147 int err;
148
149 if (!rtnl_is_locked()) {
150 /* Running prepare-commit transaction across stacked
151 * devices requires nothing moves, so if rtnl_lock is
152 * not held, schedule a worker thread to hold rtnl_lock
153 * while setting attr.
154 */
155
156 return switchdev_port_attr_set_defer(dev, attr);
157 }
158
159 /* Phase I: prepare for attr set. Driver/device should fail
160 * here if there are going to be issues in the commit phase,
161 * such as lack of resources or support. The driver/device
162 * should reserve resources needed for the commit phase here,
163 * but should not commit the attr.
164 */
165
166 attr->trans = SWITCHDEV_TRANS_PREPARE;
167 err = __switchdev_port_attr_set(dev, attr);
168 if (err) {
169 /* Prepare phase failed: abort the transaction. Any
170 * resources reserved in the prepare phase are
171 * released.
172 */
173
174 attr->trans = SWITCHDEV_TRANS_ABORT;
175 __switchdev_port_attr_set(dev, attr);
176
177 return err;
178 }
179
180 /* Phase II: commit attr set. This cannot fail as a fault
181 * of driver/device. If it does, it's a bug in the driver/device
182 * because the driver said everythings was OK in phase I.
183 */
184
185 attr->trans = SWITCHDEV_TRANS_COMMIT;
186 err = __switchdev_port_attr_set(dev, attr);
187 BUG_ON(err);
188
189 return err;
190}
191EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
192
Scott Feldman22c1f672015-05-12 23:03:51 -0700193static int __switchdev_port_obj_add(struct net_device *dev,
194 struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700195{
196 const struct switchdev_ops *ops = dev->switchdev_ops;
197 struct net_device *lower_dev;
198 struct list_head *iter;
199 int err = -EOPNOTSUPP;
200
201 if (ops && ops->switchdev_port_obj_add)
202 return ops->switchdev_port_obj_add(dev, obj);
203
204 /* Switch device port(s) may be stacked under
205 * bond/team/vlan dev, so recurse down to add object on
206 * each port.
207 */
208
209 netdev_for_each_lower_dev(dev, lower_dev, iter) {
210 err = __switchdev_port_obj_add(lower_dev, obj);
211 if (err)
212 break;
213 }
214
215 return err;
216}
217
218/**
219 * switchdev_port_obj_add - Add port object
220 *
221 * @dev: port device
222 * @obj: object to add
223 *
224 * Use a 2-phase prepare-commit transaction model to ensure
225 * system is not left in a partially updated state due to
226 * failure from driver/device.
227 *
228 * rtnl_lock must be held.
229 */
230int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
231{
232 int err;
233
234 ASSERT_RTNL();
235
236 /* Phase I: prepare for obj add. 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 obj.
241 */
242
243 obj->trans = SWITCHDEV_TRANS_PREPARE;
244 err = __switchdev_port_obj_add(dev, obj);
245 if (err) {
246 /* Prepare phase failed: abort the transaction. Any
247 * resources reserved in the prepare phase are
248 * released.
249 */
250
251 obj->trans = SWITCHDEV_TRANS_ABORT;
252 __switchdev_port_obj_add(dev, obj);
253
254 return err;
255 }
256
257 /* Phase II: commit obj add. 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
262 obj->trans = SWITCHDEV_TRANS_COMMIT;
263 err = __switchdev_port_obj_add(dev, obj);
264 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
265
266 return err;
267}
268EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
269
270/**
271 * switchdev_port_obj_del - Delete port object
272 *
273 * @dev: port device
274 * @obj: object to delete
275 */
276int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
277{
278 const struct switchdev_ops *ops = dev->switchdev_ops;
279 struct net_device *lower_dev;
280 struct list_head *iter;
281 int err = -EOPNOTSUPP;
282
283 if (ops && ops->switchdev_port_obj_del)
284 return ops->switchdev_port_obj_del(dev, obj);
285
286 /* Switch device port(s) may be stacked under
287 * bond/team/vlan dev, so recurse down to delete object on
288 * each port.
289 */
290
291 netdev_for_each_lower_dev(dev, lower_dev, iter) {
292 err = switchdev_port_obj_del(lower_dev, obj);
293 if (err)
294 break;
295 }
296
297 return err;
298}
299EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
300
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700301/**
302 * switchdev_port_obj_dump - Dump port objects
303 *
304 * @dev: port device
305 * @obj: object to dump
306 */
307int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
308{
309 const struct switchdev_ops *ops = dev->switchdev_ops;
310 struct net_device *lower_dev;
311 struct list_head *iter;
312 int err = -EOPNOTSUPP;
313
314 if (ops && ops->switchdev_port_obj_dump)
315 return ops->switchdev_port_obj_dump(dev, obj);
316
317 /* Switch device port(s) may be stacked under
318 * bond/team/vlan dev, so recurse down to dump objects on
319 * first port at bottom of stack.
320 */
321
322 netdev_for_each_lower_dev(dev, lower_dev, iter) {
323 err = switchdev_port_obj_dump(lower_dev, obj);
324 break;
325 }
326
327 return err;
328}
329EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
330
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700331static DEFINE_MUTEX(switchdev_mutex);
332static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100333
334/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700335 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100336 * @nb: notifier_block
337 *
338 * Register switch device notifier. This should be used by code
339 * which needs to monitor events happening in particular device.
340 * Return values are same as for atomic_notifier_chain_register().
341 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700342int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100343{
344 int err;
345
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700346 mutex_lock(&switchdev_mutex);
347 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
348 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100349 return err;
350}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700351EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100352
353/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700354 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100355 * @nb: notifier_block
356 *
357 * Unregister switch device notifier.
358 * Return values are same as for atomic_notifier_chain_unregister().
359 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700360int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100361{
362 int err;
363
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700364 mutex_lock(&switchdev_mutex);
365 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
366 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100367 return err;
368}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700369EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100370
371/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700372 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100373 * @val: value passed unmodified to notifier function
374 * @dev: port device
375 * @info: notifier information data
376 *
377 * Call all network notifier blocks. This should be called by driver
378 * when it needs to propagate hardware event.
379 * Return values are same as for atomic_notifier_call_chain().
380 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700381int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
382 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100383{
384 int err;
385
386 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700387 mutex_lock(&switchdev_mutex);
388 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
389 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100390 return err;
391}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700392EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800393
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700394struct switchdev_vlan_dump {
395 struct switchdev_obj obj;
396 struct sk_buff *skb;
397 u32 filter_mask;
398 u16 flags;
399 u16 begin;
400 u16 end;
401};
402
403static int switchdev_port_vlan_dump_put(struct net_device *dev,
404 struct switchdev_vlan_dump *dump)
405{
406 struct bridge_vlan_info vinfo;
407
408 vinfo.flags = dump->flags;
409
410 if (dump->begin == 0 && dump->end == 0) {
411 return 0;
412 } else if (dump->begin == dump->end) {
413 vinfo.vid = dump->begin;
414 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
415 sizeof(vinfo), &vinfo))
416 return -EMSGSIZE;
417 } else {
418 vinfo.vid = dump->begin;
419 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
420 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
421 sizeof(vinfo), &vinfo))
422 return -EMSGSIZE;
423 vinfo.vid = dump->end;
424 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
425 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
426 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
427 sizeof(vinfo), &vinfo))
428 return -EMSGSIZE;
429 }
430
431 return 0;
432}
433
434static int switchdev_port_vlan_dump_cb(struct net_device *dev,
435 struct switchdev_obj *obj)
436{
437 struct switchdev_vlan_dump *dump =
438 container_of(obj, struct switchdev_vlan_dump, obj);
439 struct switchdev_obj_vlan *vlan = &dump->obj.u.vlan;
440 int err = 0;
441
442 if (vlan->vid_begin > vlan->vid_end)
443 return -EINVAL;
444
445 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
446 dump->flags = vlan->flags;
447 for (dump->begin = dump->end = vlan->vid_begin;
448 dump->begin <= vlan->vid_end;
449 dump->begin++, dump->end++) {
450 err = switchdev_port_vlan_dump_put(dev, dump);
451 if (err)
452 return err;
453 }
454 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
455 if (dump->begin > vlan->vid_begin &&
456 dump->begin >= vlan->vid_end) {
457 if ((dump->begin - 1) == vlan->vid_end &&
458 dump->flags == vlan->flags) {
459 /* prepend */
460 dump->begin = vlan->vid_begin;
461 } else {
462 err = switchdev_port_vlan_dump_put(dev, dump);
463 dump->flags = vlan->flags;
464 dump->begin = vlan->vid_begin;
465 dump->end = vlan->vid_end;
466 }
467 } else if (dump->end <= vlan->vid_begin &&
468 dump->end < vlan->vid_end) {
469 if ((dump->end + 1) == vlan->vid_begin &&
470 dump->flags == vlan->flags) {
471 /* append */
472 dump->end = vlan->vid_end;
473 } else {
474 err = switchdev_port_vlan_dump_put(dev, dump);
475 dump->flags = vlan->flags;
476 dump->begin = vlan->vid_begin;
477 dump->end = vlan->vid_end;
478 }
479 } else {
480 err = -EINVAL;
481 }
482 }
483
484 return err;
485}
486
487static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
488 u32 filter_mask)
489{
490 struct switchdev_vlan_dump dump = {
491 .obj = {
492 .id = SWITCHDEV_OBJ_PORT_VLAN,
493 .cb = switchdev_port_vlan_dump_cb,
494 },
495 .skb = skb,
496 .filter_mask = filter_mask,
497 };
498 int err = 0;
499
500 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
501 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
502 err = switchdev_port_obj_dump(dev, &dump.obj);
503 if (err)
504 goto err_out;
505 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
506 /* last one */
507 err = switchdev_port_vlan_dump_put(dev, &dump);
508 }
509
510err_out:
511 return err == -EOPNOTSUPP ? 0 : err;
512}
513
Scott Feldman8793d0a2015-05-10 09:48:04 -0700514/**
515 * switchdev_port_bridge_getlink - Get bridge port attributes
516 *
517 * @dev: port device
518 *
519 * Called for SELF on rtnl_bridge_getlink to get bridge port
520 * attributes.
521 */
522int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
523 struct net_device *dev, u32 filter_mask,
524 int nlflags)
525{
526 struct switchdev_attr attr = {
527 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
528 };
529 u16 mode = BRIDGE_MODE_UNDEF;
530 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
531 int err;
532
533 err = switchdev_port_attr_get(dev, &attr);
534 if (err)
535 return err;
536
537 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700538 attr.u.brport_flags, mask, nlflags,
539 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700540}
541EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
542
Scott Feldman47f83282015-05-10 09:47:56 -0700543static int switchdev_port_br_setflag(struct net_device *dev,
544 struct nlattr *nlattr,
545 unsigned long brport_flag)
546{
547 struct switchdev_attr attr = {
548 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
549 };
550 u8 flag = nla_get_u8(nlattr);
551 int err;
552
553 err = switchdev_port_attr_get(dev, &attr);
554 if (err)
555 return err;
556
557 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700558 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700559 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700560 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700561
562 return switchdev_port_attr_set(dev, &attr);
563}
564
565static const struct nla_policy
566switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
567 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
568 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
569 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
570 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
571 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
572 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
573 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
574 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
575 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
576 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
577};
578
579static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
580 struct nlattr *protinfo)
581{
582 struct nlattr *attr;
583 int rem;
584 int err;
585
586 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
587 switchdev_port_bridge_policy);
588 if (err)
589 return err;
590
591 nla_for_each_nested(attr, protinfo, rem) {
592 switch (nla_type(attr)) {
593 case IFLA_BRPORT_LEARNING:
594 err = switchdev_port_br_setflag(dev, attr,
595 BR_LEARNING);
596 break;
597 case IFLA_BRPORT_LEARNING_SYNC:
598 err = switchdev_port_br_setflag(dev, attr,
599 BR_LEARNING_SYNC);
600 break;
601 default:
602 err = -EOPNOTSUPP;
603 break;
604 }
605 if (err)
606 return err;
607 }
608
609 return 0;
610}
611
612static int switchdev_port_br_afspec(struct net_device *dev,
613 struct nlattr *afspec,
614 int (*f)(struct net_device *dev,
615 struct switchdev_obj *obj))
616{
617 struct nlattr *attr;
618 struct bridge_vlan_info *vinfo;
619 struct switchdev_obj obj = {
620 .id = SWITCHDEV_OBJ_PORT_VLAN,
621 };
Scott Feldman42275bd2015-05-13 11:16:50 -0700622 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
Scott Feldman47f83282015-05-10 09:47:56 -0700623 int rem;
624 int err;
625
626 nla_for_each_nested(attr, afspec, rem) {
627 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
628 continue;
629 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
630 return -EINVAL;
631 vinfo = nla_data(attr);
Scott Feldman42275bd2015-05-13 11:16:50 -0700632 vlan->flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700633 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700634 if (vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700635 return -EINVAL;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700636 vlan->vid_begin = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700637 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700638 if (!vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700639 return -EINVAL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700640 vlan->vid_end = vinfo->vid;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700641 if (vlan->vid_end <= vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700642 return -EINVAL;
643 err = f(dev, &obj);
644 if (err)
645 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700646 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700647 } else {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700648 if (vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700649 return -EINVAL;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700650 vlan->vid_begin = vinfo->vid;
Scott Feldman42275bd2015-05-13 11:16:50 -0700651 vlan->vid_end = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700652 err = f(dev, &obj);
653 if (err)
654 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700655 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700656 }
657 }
658
659 return 0;
660}
661
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800662/**
Scott Feldman47f83282015-05-10 09:47:56 -0700663 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800664 *
665 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700666 * @nlh: netlink header
667 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800668 *
Scott Feldman47f83282015-05-10 09:47:56 -0700669 * Called for SELF on rtnl_bridge_setlink to set bridge port
670 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800671 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700672int switchdev_port_bridge_setlink(struct net_device *dev,
673 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800674{
Scott Feldman47f83282015-05-10 09:47:56 -0700675 struct nlattr *protinfo;
676 struct nlattr *afspec;
677 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800678
Scott Feldman47f83282015-05-10 09:47:56 -0700679 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
680 IFLA_PROTINFO);
681 if (protinfo) {
682 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
683 if (err)
684 return err;
685 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800686
Scott Feldman47f83282015-05-10 09:47:56 -0700687 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
688 IFLA_AF_SPEC);
689 if (afspec)
690 err = switchdev_port_br_afspec(dev, afspec,
691 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800692
Scott Feldman47f83282015-05-10 09:47:56 -0700693 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800694}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700695EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800696
697/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700698 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800699 *
700 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700701 * @nlh: netlink header
702 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800703 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700704 * Called for SELF on rtnl_bridge_dellink to set bridge port
705 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800706 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700707int switchdev_port_bridge_dellink(struct net_device *dev,
708 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800709{
Scott Feldman5c34e022015-05-10 09:48:00 -0700710 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800711
Scott Feldman5c34e022015-05-10 09:48:00 -0700712 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
713 IFLA_AF_SPEC);
714 if (afspec)
715 return switchdev_port_br_afspec(dev, afspec,
716 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800717
Scott Feldman5c34e022015-05-10 09:48:00 -0700718 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800719}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700720EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800721
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700722/**
723 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
724 *
725 * @ndmsg: netlink hdr
726 * @nlattr: netlink attributes
727 * @dev: port device
728 * @addr: MAC address to add
729 * @vid: VLAN to add
730 *
731 * Add FDB entry to switch device.
732 */
733int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
734 struct net_device *dev, const unsigned char *addr,
735 u16 vid, u16 nlm_flags)
736{
737 struct switchdev_obj obj = {
738 .id = SWITCHDEV_OBJ_PORT_FDB,
739 .u.fdb = {
740 .addr = addr,
741 .vid = vid,
742 },
743 };
744
745 return switchdev_port_obj_add(dev, &obj);
746}
747EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
748
749/**
750 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
751 *
752 * @ndmsg: netlink hdr
753 * @nlattr: netlink attributes
754 * @dev: port device
755 * @addr: MAC address to delete
756 * @vid: VLAN to delete
757 *
758 * Delete FDB entry from switch device.
759 */
760int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
761 struct net_device *dev, const unsigned char *addr,
762 u16 vid)
763{
764 struct switchdev_obj obj = {
765 .id = SWITCHDEV_OBJ_PORT_FDB,
766 .u.fdb = {
767 .addr = addr,
768 .vid = vid,
769 },
770 };
771
772 return switchdev_port_obj_del(dev, &obj);
773}
774EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
775
776struct switchdev_fdb_dump {
777 struct switchdev_obj obj;
778 struct sk_buff *skb;
779 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700780 int idx;
781};
782
783static int switchdev_port_fdb_dump_cb(struct net_device *dev,
784 struct switchdev_obj *obj)
785{
786 struct switchdev_fdb_dump *dump =
787 container_of(obj, struct switchdev_fdb_dump, obj);
788 u32 portid = NETLINK_CB(dump->cb->skb).portid;
789 u32 seq = dump->cb->nlh->nlmsg_seq;
790 struct nlmsghdr *nlh;
791 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700792
793 if (dump->idx < dump->cb->args[0])
794 goto skip;
795
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700796 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
797 sizeof(*ndm), NLM_F_MULTI);
798 if (!nlh)
799 return -EMSGSIZE;
800
801 ndm = nlmsg_data(nlh);
802 ndm->ndm_family = AF_BRIDGE;
803 ndm->ndm_pad1 = 0;
804 ndm->ndm_pad2 = 0;
805 ndm->ndm_flags = NTF_SELF;
806 ndm->ndm_type = 0;
807 ndm->ndm_ifindex = dev->ifindex;
808 ndm->ndm_state = NUD_REACHABLE;
809
810 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
811 goto nla_put_failure;
812
813 if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
814 goto nla_put_failure;
815
816 nlmsg_end(dump->skb, nlh);
817
818skip:
819 dump->idx++;
820 return 0;
821
822nla_put_failure:
823 nlmsg_cancel(dump->skb, nlh);
824 return -EMSGSIZE;
825}
826
827/**
828 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
829 *
830 * @skb: netlink skb
831 * @cb: netlink callback
832 * @dev: port device
833 * @filter_dev: filter device
834 * @idx:
835 *
836 * Delete FDB entry from switch device.
837 */
838int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
839 struct net_device *dev,
840 struct net_device *filter_dev, int idx)
841{
842 struct switchdev_fdb_dump dump = {
843 .obj = {
844 .id = SWITCHDEV_OBJ_PORT_FDB,
845 .cb = switchdev_port_fdb_dump_cb,
846 },
847 .skb = skb,
848 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700849 .idx = idx,
850 };
851 int err;
852
853 err = switchdev_port_obj_dump(dev, &dump.obj);
854 if (err)
855 return err;
856
857 return dump.idx;
858}
859EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
860
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700861static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800862{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700863 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800864 struct net_device *lower_dev;
865 struct net_device *port_dev;
866 struct list_head *iter;
867
868 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700869 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800870 */
871
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700872 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800873 return dev;
874
875 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700876 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800877 if (port_dev)
878 return port_dev;
879 }
880
881 return NULL;
882}
883
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700884static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800885{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700886 struct switchdev_attr attr = {
887 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
888 };
889 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800890 struct net_device *dev = NULL;
891 int nhsel;
892
893 /* For this route, all nexthop devs must be on the same switch. */
894
895 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
896 const struct fib_nh *nh = &fi->fib_nh[nhsel];
897
898 if (!nh->nh_dev)
899 return NULL;
900
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700901 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800902 if (!dev)
903 return NULL;
904
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700905 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800906 return NULL;
907
908 if (nhsel > 0) {
Scott Feldman42275bd2015-05-13 11:16:50 -0700909 if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800910 return NULL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700911 if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
912 attr.u.ppid.id_len))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800913 return NULL;
914 }
915
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700916 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800917 }
918
919 return dev;
920}
921
Scott Feldman5e8d9042015-03-05 21:21:15 -0800922/**
Scott Feldman7616dcb2015-06-03 20:43:43 -0700923 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -0800924 *
925 * @dst: route's IPv4 destination address
926 * @dst_len: destination address length (prefix length)
927 * @fi: route FIB info structure
928 * @tos: route TOS
929 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -0700930 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800931 * @tb_id: route table ID
932 *
Scott Feldman7616dcb2015-06-03 20:43:43 -0700933 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -0800934 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700935int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
936 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800937{
Scott Feldman58c2cb12015-05-10 09:48:06 -0700938 struct switchdev_obj fib_obj = {
939 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -0700940 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -0700941 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -0700942 .dst_len = dst_len,
943 .fi = fi,
944 .tos = tos,
945 .type = type,
946 .nlflags = nlflags,
947 .tb_id = tb_id,
948 },
949 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800950 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800951 int err = 0;
952
Scott Feldman8e05fd72015-03-05 21:21:19 -0800953 /* Don't offload route if using custom ip rules or if
954 * IPv4 FIB offloading has been disabled completely.
955 */
956
Scott Feldmane1315db2015-03-06 01:14:36 -0800957#ifdef CONFIG_IP_MULTIPLE_TABLES
958 if (fi->fib_net->ipv4.fib_has_custom_rules)
959 return 0;
960#endif
961
962 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -0800963 return 0;
964
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700965 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800966 if (!dev)
967 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800968
Scott Feldman58c2cb12015-05-10 09:48:06 -0700969 err = switchdev_port_obj_add(dev, &fib_obj);
970 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -0400971 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800972
Scott Feldmanaf201f72015-06-10 17:04:49 -0700973 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800974}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700975EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800976
977/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700978 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800979 *
980 * @dst: route's IPv4 destination address
981 * @dst_len: destination address length (prefix length)
982 * @fi: route FIB info structure
983 * @tos: route TOS
984 * @type: route type
985 * @tb_id: route table ID
986 *
987 * Delete IPv4 route entry from switch device.
988 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700989int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
990 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800991{
Scott Feldman58c2cb12015-05-10 09:48:06 -0700992 struct switchdev_obj fib_obj = {
993 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -0700994 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -0700995 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -0700996 .dst_len = dst_len,
997 .fi = fi,
998 .tos = tos,
999 .type = type,
1000 .nlflags = 0,
1001 .tb_id = tb_id,
1002 },
1003 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001004 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001005 int err = 0;
1006
Roopa Prabhueea39942015-05-13 21:17:41 -07001007 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001008 return 0;
1009
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001010 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001011 if (!dev)
1012 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001013
Scott Feldman58c2cb12015-05-10 09:48:06 -07001014 err = switchdev_port_obj_del(dev, &fib_obj);
1015 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001016 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001017
Scott Feldmanaf201f72015-06-10 17:04:49 -07001018 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001019}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001020EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001021
1022/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001023 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001024 *
1025 * @fi: route FIB info structure
1026 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001027void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001028{
1029 /* There was a problem installing this route to the offload
1030 * device. For now, until we come up with more refined
1031 * policy handling, abruptly end IPv4 fib offloading for
1032 * for entire net by flushing offload device(s) of all
1033 * IPv4 routes, and mark IPv4 fib offloading broken from
1034 * this point forward.
1035 */
1036
1037 fib_flush_external(fi->fib_net);
1038 fi->fib_net->ipv4.fib_offload_disabled = true;
1039}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001040EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);