blob: d4c8cf8282409a64cc0823cd994a1d2fe6d9c537 [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);
106 BUG_ON(err);
107 rtnl_unlock();
108
109 dev_put(asw->dev);
110 kfree(work);
111}
112
113static int switchdev_port_attr_set_defer(struct net_device *dev,
114 struct switchdev_attr *attr)
115{
116 struct switchdev_attr_set_work *asw;
117
118 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
119 if (!asw)
120 return -ENOMEM;
121
122 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
123
124 dev_hold(dev);
125 asw->dev = dev;
126 memcpy(&asw->attr, attr, sizeof(asw->attr));
127
128 schedule_work(&asw->work);
129
130 return 0;
131}
132
133/**
134 * switchdev_port_attr_set - Set port attribute
135 *
136 * @dev: port device
137 * @attr: attribute to set
138 *
139 * Use a 2-phase prepare-commit transaction model to ensure
140 * system is not left in a partially updated state due to
141 * failure from driver/device.
142 */
143int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
144{
145 int err;
146
147 if (!rtnl_is_locked()) {
148 /* Running prepare-commit transaction across stacked
149 * devices requires nothing moves, so if rtnl_lock is
150 * not held, schedule a worker thread to hold rtnl_lock
151 * while setting attr.
152 */
153
154 return switchdev_port_attr_set_defer(dev, attr);
155 }
156
157 /* Phase I: prepare for attr set. Driver/device should fail
158 * here if there are going to be issues in the commit phase,
159 * such as lack of resources or support. The driver/device
160 * should reserve resources needed for the commit phase here,
161 * but should not commit the attr.
162 */
163
164 attr->trans = SWITCHDEV_TRANS_PREPARE;
165 err = __switchdev_port_attr_set(dev, attr);
166 if (err) {
167 /* Prepare phase failed: abort the transaction. Any
168 * resources reserved in the prepare phase are
169 * released.
170 */
171
172 attr->trans = SWITCHDEV_TRANS_ABORT;
173 __switchdev_port_attr_set(dev, attr);
174
175 return err;
176 }
177
178 /* Phase II: commit attr set. This cannot fail as a fault
179 * of driver/device. If it does, it's a bug in the driver/device
180 * because the driver said everythings was OK in phase I.
181 */
182
183 attr->trans = SWITCHDEV_TRANS_COMMIT;
184 err = __switchdev_port_attr_set(dev, attr);
185 BUG_ON(err);
186
187 return err;
188}
189EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
190
Scott Feldman22c1f672015-05-12 23:03:51 -0700191static int __switchdev_port_obj_add(struct net_device *dev,
192 struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700193{
194 const struct switchdev_ops *ops = dev->switchdev_ops;
195 struct net_device *lower_dev;
196 struct list_head *iter;
197 int err = -EOPNOTSUPP;
198
199 if (ops && ops->switchdev_port_obj_add)
200 return ops->switchdev_port_obj_add(dev, obj);
201
202 /* Switch device port(s) may be stacked under
203 * bond/team/vlan dev, so recurse down to add object on
204 * each port.
205 */
206
207 netdev_for_each_lower_dev(dev, lower_dev, iter) {
208 err = __switchdev_port_obj_add(lower_dev, obj);
209 if (err)
210 break;
211 }
212
213 return err;
214}
215
216/**
217 * switchdev_port_obj_add - Add port object
218 *
219 * @dev: port device
220 * @obj: object to add
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 *
226 * rtnl_lock must be held.
227 */
228int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
229{
230 int err;
231
232 ASSERT_RTNL();
233
234 /* Phase I: prepare for obj add. Driver/device should fail
235 * here if there are going to be issues in the commit phase,
236 * such as lack of resources or support. The driver/device
237 * should reserve resources needed for the commit phase here,
238 * but should not commit the obj.
239 */
240
241 obj->trans = SWITCHDEV_TRANS_PREPARE;
242 err = __switchdev_port_obj_add(dev, obj);
243 if (err) {
244 /* Prepare phase failed: abort the transaction. Any
245 * resources reserved in the prepare phase are
246 * released.
247 */
248
249 obj->trans = SWITCHDEV_TRANS_ABORT;
250 __switchdev_port_obj_add(dev, obj);
251
252 return err;
253 }
254
255 /* Phase II: commit obj add. This cannot fail as a fault
256 * of driver/device. If it does, it's a bug in the driver/device
257 * because the driver said everythings was OK in phase I.
258 */
259
260 obj->trans = SWITCHDEV_TRANS_COMMIT;
261 err = __switchdev_port_obj_add(dev, obj);
262 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
263
264 return err;
265}
266EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
267
268/**
269 * switchdev_port_obj_del - Delete port object
270 *
271 * @dev: port device
272 * @obj: object to delete
273 */
274int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
275{
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_del)
282 return ops->switchdev_port_obj_del(dev, obj);
283
284 /* Switch device port(s) may be stacked under
285 * bond/team/vlan dev, so recurse down to delete object on
286 * each port.
287 */
288
289 netdev_for_each_lower_dev(dev, lower_dev, iter) {
290 err = switchdev_port_obj_del(lower_dev, obj);
291 if (err)
292 break;
293 }
294
295 return err;
296}
297EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
298
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700299/**
300 * switchdev_port_obj_dump - Dump port objects
301 *
302 * @dev: port device
303 * @obj: object to dump
304 */
305int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
306{
307 const struct switchdev_ops *ops = dev->switchdev_ops;
308 struct net_device *lower_dev;
309 struct list_head *iter;
310 int err = -EOPNOTSUPP;
311
312 if (ops && ops->switchdev_port_obj_dump)
313 return ops->switchdev_port_obj_dump(dev, obj);
314
315 /* Switch device port(s) may be stacked under
316 * bond/team/vlan dev, so recurse down to dump objects on
317 * first port at bottom of stack.
318 */
319
320 netdev_for_each_lower_dev(dev, lower_dev, iter) {
321 err = switchdev_port_obj_dump(lower_dev, obj);
322 break;
323 }
324
325 return err;
326}
327EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
328
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700329static DEFINE_MUTEX(switchdev_mutex);
330static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100331
332/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700333 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100334 * @nb: notifier_block
335 *
336 * Register switch device notifier. This should be used by code
337 * which needs to monitor events happening in particular device.
338 * Return values are same as for atomic_notifier_chain_register().
339 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700340int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100341{
342 int err;
343
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700344 mutex_lock(&switchdev_mutex);
345 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
346 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100347 return err;
348}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700349EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100350
351/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700352 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100353 * @nb: notifier_block
354 *
355 * Unregister switch device notifier.
356 * Return values are same as for atomic_notifier_chain_unregister().
357 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700358int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100359{
360 int err;
361
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700362 mutex_lock(&switchdev_mutex);
363 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
364 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100365 return err;
366}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700367EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100368
369/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700370 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100371 * @val: value passed unmodified to notifier function
372 * @dev: port device
373 * @info: notifier information data
374 *
375 * Call all network notifier blocks. This should be called by driver
376 * when it needs to propagate hardware event.
377 * Return values are same as for atomic_notifier_call_chain().
378 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700379int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
380 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100381{
382 int err;
383
384 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700385 mutex_lock(&switchdev_mutex);
386 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
387 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100388 return err;
389}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700390EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800391
Scott Feldman8793d0a2015-05-10 09:48:04 -0700392/**
393 * switchdev_port_bridge_getlink - Get bridge port attributes
394 *
395 * @dev: port device
396 *
397 * Called for SELF on rtnl_bridge_getlink to get bridge port
398 * attributes.
399 */
400int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
401 struct net_device *dev, u32 filter_mask,
402 int nlflags)
403{
404 struct switchdev_attr attr = {
405 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
406 };
407 u16 mode = BRIDGE_MODE_UNDEF;
408 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
409 int err;
410
411 err = switchdev_port_attr_get(dev, &attr);
412 if (err)
413 return err;
414
415 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman42275bd2015-05-13 11:16:50 -0700416 attr.u.brport_flags, mask, nlflags);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700417}
418EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
419
Scott Feldman47f83282015-05-10 09:47:56 -0700420static int switchdev_port_br_setflag(struct net_device *dev,
421 struct nlattr *nlattr,
422 unsigned long brport_flag)
423{
424 struct switchdev_attr attr = {
425 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
426 };
427 u8 flag = nla_get_u8(nlattr);
428 int err;
429
430 err = switchdev_port_attr_get(dev, &attr);
431 if (err)
432 return err;
433
434 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700435 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700436 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700437 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700438
439 return switchdev_port_attr_set(dev, &attr);
440}
441
442static const struct nla_policy
443switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
444 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
445 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
446 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
447 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
448 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
449 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
450 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
451 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
452 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
453 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
454};
455
456static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
457 struct nlattr *protinfo)
458{
459 struct nlattr *attr;
460 int rem;
461 int err;
462
463 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
464 switchdev_port_bridge_policy);
465 if (err)
466 return err;
467
468 nla_for_each_nested(attr, protinfo, rem) {
469 switch (nla_type(attr)) {
470 case IFLA_BRPORT_LEARNING:
471 err = switchdev_port_br_setflag(dev, attr,
472 BR_LEARNING);
473 break;
474 case IFLA_BRPORT_LEARNING_SYNC:
475 err = switchdev_port_br_setflag(dev, attr,
476 BR_LEARNING_SYNC);
477 break;
478 default:
479 err = -EOPNOTSUPP;
480 break;
481 }
482 if (err)
483 return err;
484 }
485
486 return 0;
487}
488
489static int switchdev_port_br_afspec(struct net_device *dev,
490 struct nlattr *afspec,
491 int (*f)(struct net_device *dev,
492 struct switchdev_obj *obj))
493{
494 struct nlattr *attr;
495 struct bridge_vlan_info *vinfo;
496 struct switchdev_obj obj = {
497 .id = SWITCHDEV_OBJ_PORT_VLAN,
498 };
Scott Feldman42275bd2015-05-13 11:16:50 -0700499 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
Scott Feldman47f83282015-05-10 09:47:56 -0700500 int rem;
501 int err;
502
503 nla_for_each_nested(attr, afspec, rem) {
504 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
505 continue;
506 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
507 return -EINVAL;
508 vinfo = nla_data(attr);
Scott Feldman42275bd2015-05-13 11:16:50 -0700509 vlan->flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700510 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Scott Feldman42275bd2015-05-13 11:16:50 -0700511 if (vlan->vid_start)
Scott Feldman47f83282015-05-10 09:47:56 -0700512 return -EINVAL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700513 vlan->vid_start = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700514 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Scott Feldman42275bd2015-05-13 11:16:50 -0700515 if (!vlan->vid_start)
Scott Feldman47f83282015-05-10 09:47:56 -0700516 return -EINVAL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700517 vlan->vid_end = vinfo->vid;
518 if (vlan->vid_end <= vlan->vid_start)
Scott Feldman47f83282015-05-10 09:47:56 -0700519 return -EINVAL;
520 err = f(dev, &obj);
521 if (err)
522 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700523 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700524 } else {
Scott Feldman42275bd2015-05-13 11:16:50 -0700525 if (vlan->vid_start)
Scott Feldman47f83282015-05-10 09:47:56 -0700526 return -EINVAL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700527 vlan->vid_start = vinfo->vid;
528 vlan->vid_end = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700529 err = f(dev, &obj);
530 if (err)
531 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700532 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700533 }
534 }
535
536 return 0;
537}
538
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800539/**
Scott Feldman47f83282015-05-10 09:47:56 -0700540 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800541 *
542 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700543 * @nlh: netlink header
544 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800545 *
Scott Feldman47f83282015-05-10 09:47:56 -0700546 * Called for SELF on rtnl_bridge_setlink to set bridge port
547 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800548 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700549int switchdev_port_bridge_setlink(struct net_device *dev,
550 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800551{
Scott Feldman47f83282015-05-10 09:47:56 -0700552 struct nlattr *protinfo;
553 struct nlattr *afspec;
554 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800555
Scott Feldman47f83282015-05-10 09:47:56 -0700556 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
557 IFLA_PROTINFO);
558 if (protinfo) {
559 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
560 if (err)
561 return err;
562 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800563
Scott Feldman47f83282015-05-10 09:47:56 -0700564 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
565 IFLA_AF_SPEC);
566 if (afspec)
567 err = switchdev_port_br_afspec(dev, afspec,
568 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800569
Scott Feldman47f83282015-05-10 09:47:56 -0700570 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800571}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700572EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800573
574/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700575 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800576 *
577 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700578 * @nlh: netlink header
579 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800580 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700581 * Called for SELF on rtnl_bridge_dellink to set bridge port
582 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800583 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700584int switchdev_port_bridge_dellink(struct net_device *dev,
585 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800586{
Scott Feldman5c34e022015-05-10 09:48:00 -0700587 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800588
Scott Feldman5c34e022015-05-10 09:48:00 -0700589 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
590 IFLA_AF_SPEC);
591 if (afspec)
592 return switchdev_port_br_afspec(dev, afspec,
593 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800594
Scott Feldman5c34e022015-05-10 09:48:00 -0700595 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800596}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700597EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800598
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700599/**
600 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
601 *
602 * @ndmsg: netlink hdr
603 * @nlattr: netlink attributes
604 * @dev: port device
605 * @addr: MAC address to add
606 * @vid: VLAN to add
607 *
608 * Add FDB entry to switch device.
609 */
610int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
611 struct net_device *dev, const unsigned char *addr,
612 u16 vid, u16 nlm_flags)
613{
614 struct switchdev_obj obj = {
615 .id = SWITCHDEV_OBJ_PORT_FDB,
616 .u.fdb = {
617 .addr = addr,
618 .vid = vid,
619 },
620 };
621
622 return switchdev_port_obj_add(dev, &obj);
623}
624EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
625
626/**
627 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
628 *
629 * @ndmsg: netlink hdr
630 * @nlattr: netlink attributes
631 * @dev: port device
632 * @addr: MAC address to delete
633 * @vid: VLAN to delete
634 *
635 * Delete FDB entry from switch device.
636 */
637int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
638 struct net_device *dev, const unsigned char *addr,
639 u16 vid)
640{
641 struct switchdev_obj obj = {
642 .id = SWITCHDEV_OBJ_PORT_FDB,
643 .u.fdb = {
644 .addr = addr,
645 .vid = vid,
646 },
647 };
648
649 return switchdev_port_obj_del(dev, &obj);
650}
651EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
652
653struct switchdev_fdb_dump {
654 struct switchdev_obj obj;
655 struct sk_buff *skb;
656 struct netlink_callback *cb;
657 struct net_device *filter_dev;
658 int idx;
659};
660
661static int switchdev_port_fdb_dump_cb(struct net_device *dev,
662 struct switchdev_obj *obj)
663{
664 struct switchdev_fdb_dump *dump =
665 container_of(obj, struct switchdev_fdb_dump, obj);
666 u32 portid = NETLINK_CB(dump->cb->skb).portid;
667 u32 seq = dump->cb->nlh->nlmsg_seq;
668 struct nlmsghdr *nlh;
669 struct ndmsg *ndm;
670 struct net_device *master = netdev_master_upper_dev_get(dev);
671
672 if (dump->idx < dump->cb->args[0])
673 goto skip;
674
675 if (master && dump->filter_dev != master)
676 goto skip;
677
678 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
679 sizeof(*ndm), NLM_F_MULTI);
680 if (!nlh)
681 return -EMSGSIZE;
682
683 ndm = nlmsg_data(nlh);
684 ndm->ndm_family = AF_BRIDGE;
685 ndm->ndm_pad1 = 0;
686 ndm->ndm_pad2 = 0;
687 ndm->ndm_flags = NTF_SELF;
688 ndm->ndm_type = 0;
689 ndm->ndm_ifindex = dev->ifindex;
690 ndm->ndm_state = NUD_REACHABLE;
691
692 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
693 goto nla_put_failure;
694
695 if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
696 goto nla_put_failure;
697
698 nlmsg_end(dump->skb, nlh);
699
700skip:
701 dump->idx++;
702 return 0;
703
704nla_put_failure:
705 nlmsg_cancel(dump->skb, nlh);
706 return -EMSGSIZE;
707}
708
709/**
710 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
711 *
712 * @skb: netlink skb
713 * @cb: netlink callback
714 * @dev: port device
715 * @filter_dev: filter device
716 * @idx:
717 *
718 * Delete FDB entry from switch device.
719 */
720int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
721 struct net_device *dev,
722 struct net_device *filter_dev, int idx)
723{
724 struct switchdev_fdb_dump dump = {
725 .obj = {
726 .id = SWITCHDEV_OBJ_PORT_FDB,
727 .cb = switchdev_port_fdb_dump_cb,
728 },
729 .skb = skb,
730 .cb = cb,
731 .filter_dev = filter_dev,
732 .idx = idx,
733 };
734 int err;
735
736 err = switchdev_port_obj_dump(dev, &dump.obj);
737 if (err)
738 return err;
739
740 return dump.idx;
741}
742EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
743
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700744static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800745{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700746 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800747 struct net_device *lower_dev;
748 struct net_device *port_dev;
749 struct list_head *iter;
750
751 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700752 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800753 */
754
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700755 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800756 return dev;
757
758 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700759 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800760 if (port_dev)
761 return port_dev;
762 }
763
764 return NULL;
765}
766
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700767static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800768{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700769 struct switchdev_attr attr = {
770 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
771 };
772 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800773 struct net_device *dev = NULL;
774 int nhsel;
775
776 /* For this route, all nexthop devs must be on the same switch. */
777
778 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
779 const struct fib_nh *nh = &fi->fib_nh[nhsel];
780
781 if (!nh->nh_dev)
782 return NULL;
783
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700784 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800785 if (!dev)
786 return NULL;
787
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700788 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800789 return NULL;
790
791 if (nhsel > 0) {
Scott Feldman42275bd2015-05-13 11:16:50 -0700792 if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800793 return NULL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700794 if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
795 attr.u.ppid.id_len))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800796 return NULL;
797 }
798
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700799 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800800 }
801
802 return dev;
803}
804
Scott Feldman5e8d9042015-03-05 21:21:15 -0800805/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700806 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800807 *
808 * @dst: route's IPv4 destination address
809 * @dst_len: destination address length (prefix length)
810 * @fi: route FIB info structure
811 * @tos: route TOS
812 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -0700813 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800814 * @tb_id: route table ID
815 *
816 * Add IPv4 route entry to switch device.
817 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700818int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
819 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800820{
Scott Feldman58c2cb12015-05-10 09:48:06 -0700821 struct switchdev_obj fib_obj = {
822 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -0700823 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -0700824 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -0700825 .dst_len = dst_len,
826 .fi = fi,
827 .tos = tos,
828 .type = type,
829 .nlflags = nlflags,
830 .tb_id = tb_id,
831 },
832 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800833 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800834 int err = 0;
835
Scott Feldman8e05fd72015-03-05 21:21:19 -0800836 /* Don't offload route if using custom ip rules or if
837 * IPv4 FIB offloading has been disabled completely.
838 */
839
Scott Feldmane1315db2015-03-06 01:14:36 -0800840#ifdef CONFIG_IP_MULTIPLE_TABLES
841 if (fi->fib_net->ipv4.fib_has_custom_rules)
842 return 0;
843#endif
844
845 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -0800846 return 0;
847
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700848 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800849 if (!dev)
850 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800851
Scott Feldman58c2cb12015-05-10 09:48:06 -0700852 err = switchdev_port_obj_add(dev, &fib_obj);
853 if (!err)
854 fi->fib_flags |= RTNH_F_EXTERNAL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800855
856 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800857}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700858EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800859
860/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700861 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800862 *
863 * @dst: route's IPv4 destination address
864 * @dst_len: destination address length (prefix length)
865 * @fi: route FIB info structure
866 * @tos: route TOS
867 * @type: route type
868 * @tb_id: route table ID
869 *
870 * Delete IPv4 route entry from switch device.
871 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700872int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
873 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800874{
Scott Feldman58c2cb12015-05-10 09:48:06 -0700875 struct switchdev_obj fib_obj = {
876 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -0700877 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -0700878 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -0700879 .dst_len = dst_len,
880 .fi = fi,
881 .tos = tos,
882 .type = type,
883 .nlflags = 0,
884 .tb_id = tb_id,
885 },
886 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800887 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800888 int err = 0;
889
890 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
891 return 0;
892
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700893 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800894 if (!dev)
895 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800896
Scott Feldman58c2cb12015-05-10 09:48:06 -0700897 err = switchdev_port_obj_del(dev, &fib_obj);
898 if (!err)
899 fi->fib_flags &= ~RTNH_F_EXTERNAL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800900
901 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800902}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700903EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -0800904
905/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700906 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -0800907 *
908 * @fi: route FIB info structure
909 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700910void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -0800911{
912 /* There was a problem installing this route to the offload
913 * device. For now, until we come up with more refined
914 * policy handling, abruptly end IPv4 fib offloading for
915 * for entire net by flushing offload device(s) of all
916 * IPv4 routes, and mark IPv4 fib offloading broken from
917 * this point forward.
918 */
919
920 fib_flush_external(fi->fib_net);
921 fi->fib_net->ipv4.fib_offload_disabled = true;
922}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700923EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);