]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/switchdev/switchdev.c
switchdev: introduce get/set attrs ops
[mirror_ubuntu-zesty-kernel.git] / net / switchdev / switchdev.c
CommitLineData
007f790c
JP
1/*
2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
f8f21471 4 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
007f790c
JP
5 *
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>
03bf0c28
JP
15#include <linux/mutex.h>
16#include <linux/notifier.h>
007f790c 17#include <linux/netdevice.h>
5e8d9049 18#include <net/ip_fib.h>
007f790c
JP
19#include <net/switchdev.h>
20
21/**
ebb9a03a 22 * switchdev_parent_id_get - Get ID of a switch
007f790c
JP
23 * @dev: port device
24 * @psid: switch ID
25 *
26 * Get ID of a switch this port is part of.
27 */
ebb9a03a
JP
28int switchdev_parent_id_get(struct net_device *dev,
29 struct netdev_phys_item_id *psid)
007f790c 30{
9d47c0a2 31 const struct switchdev_ops *ops = dev->switchdev_ops;
007f790c 32
9d47c0a2 33 if (!ops || !ops->switchdev_parent_id_get)
007f790c 34 return -EOPNOTSUPP;
9d47c0a2 35 return ops->switchdev_parent_id_get(dev, psid);
007f790c 36}
ebb9a03a 37EXPORT_SYMBOL_GPL(switchdev_parent_id_get);
38dcf357 38
3094333d
SF
39/**
40 * switchdev_port_attr_get - Get port attribute
41 *
42 * @dev: port device
43 * @attr: attribute to get
44 */
45int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
46{
47 const struct switchdev_ops *ops = dev->switchdev_ops;
48 struct net_device *lower_dev;
49 struct list_head *iter;
50 struct switchdev_attr first = {
51 .id = SWITCHDEV_ATTR_UNDEFINED
52 };
53 int err = -EOPNOTSUPP;
54
55 if (ops && ops->switchdev_port_attr_get)
56 return ops->switchdev_port_attr_get(dev, attr);
57
58 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
59 return err;
60
61 /* Switch device port(s) may be stacked under
62 * bond/team/vlan dev, so recurse down to get attr on
63 * each port. Return -ENODATA if attr values don't
64 * compare across ports.
65 */
66
67 netdev_for_each_lower_dev(dev, lower_dev, iter) {
68 err = switchdev_port_attr_get(lower_dev, attr);
69 if (err)
70 break;
71 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
72 first = *attr;
73 else if (memcmp(&first, attr, sizeof(*attr)))
74 return -ENODATA;
75 }
76
77 return err;
78}
79EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
80
81static int __switchdev_port_attr_set(struct net_device *dev,
82 struct switchdev_attr *attr)
83{
84 const struct switchdev_ops *ops = dev->switchdev_ops;
85 struct net_device *lower_dev;
86 struct list_head *iter;
87 int err = -EOPNOTSUPP;
88
89 if (ops && ops->switchdev_port_attr_set)
90 return ops->switchdev_port_attr_set(dev, attr);
91
92 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
93 return err;
94
95 /* Switch device port(s) may be stacked under
96 * bond/team/vlan dev, so recurse down to set attr on
97 * each port.
98 */
99
100 netdev_for_each_lower_dev(dev, lower_dev, iter) {
101 err = __switchdev_port_attr_set(lower_dev, attr);
102 if (err)
103 break;
104 }
105
106 return err;
107}
108
109struct switchdev_attr_set_work {
110 struct work_struct work;
111 struct net_device *dev;
112 struct switchdev_attr attr;
113};
114
115static void switchdev_port_attr_set_work(struct work_struct *work)
116{
117 struct switchdev_attr_set_work *asw =
118 container_of(work, struct switchdev_attr_set_work, work);
119 int err;
120
121 rtnl_lock();
122 err = switchdev_port_attr_set(asw->dev, &asw->attr);
123 BUG_ON(err);
124 rtnl_unlock();
125
126 dev_put(asw->dev);
127 kfree(work);
128}
129
130static int switchdev_port_attr_set_defer(struct net_device *dev,
131 struct switchdev_attr *attr)
132{
133 struct switchdev_attr_set_work *asw;
134
135 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
136 if (!asw)
137 return -ENOMEM;
138
139 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
140
141 dev_hold(dev);
142 asw->dev = dev;
143 memcpy(&asw->attr, attr, sizeof(asw->attr));
144
145 schedule_work(&asw->work);
146
147 return 0;
148}
149
150/**
151 * switchdev_port_attr_set - Set port attribute
152 *
153 * @dev: port device
154 * @attr: attribute to set
155 *
156 * Use a 2-phase prepare-commit transaction model to ensure
157 * system is not left in a partially updated state due to
158 * failure from driver/device.
159 */
160int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
161{
162 int err;
163
164 if (!rtnl_is_locked()) {
165 /* Running prepare-commit transaction across stacked
166 * devices requires nothing moves, so if rtnl_lock is
167 * not held, schedule a worker thread to hold rtnl_lock
168 * while setting attr.
169 */
170
171 return switchdev_port_attr_set_defer(dev, attr);
172 }
173
174 /* Phase I: prepare for attr set. Driver/device should fail
175 * here if there are going to be issues in the commit phase,
176 * such as lack of resources or support. The driver/device
177 * should reserve resources needed for the commit phase here,
178 * but should not commit the attr.
179 */
180
181 attr->trans = SWITCHDEV_TRANS_PREPARE;
182 err = __switchdev_port_attr_set(dev, attr);
183 if (err) {
184 /* Prepare phase failed: abort the transaction. Any
185 * resources reserved in the prepare phase are
186 * released.
187 */
188
189 attr->trans = SWITCHDEV_TRANS_ABORT;
190 __switchdev_port_attr_set(dev, attr);
191
192 return err;
193 }
194
195 /* Phase II: commit attr set. This cannot fail as a fault
196 * of driver/device. If it does, it's a bug in the driver/device
197 * because the driver said everythings was OK in phase I.
198 */
199
200 attr->trans = SWITCHDEV_TRANS_COMMIT;
201 err = __switchdev_port_attr_set(dev, attr);
202 BUG_ON(err);
203
204 return err;
205}
206EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
207
38dcf357 208/**
ebb9a03a 209 * switchdev_port_stp_update - Notify switch device port of STP
38dcf357
SF
210 * state change
211 * @dev: port device
212 * @state: port STP state
213 *
214 * Notify switch device port of bridge port STP state change.
215 */
ebb9a03a 216int switchdev_port_stp_update(struct net_device *dev, u8 state)
38dcf357 217{
9d47c0a2 218 const struct switchdev_ops *ops = dev->switchdev_ops;
558d51fa
RP
219 struct net_device *lower_dev;
220 struct list_head *iter;
221 int err = -EOPNOTSUPP;
38dcf357 222
9d47c0a2
JP
223 if (ops && ops->switchdev_port_stp_update)
224 return ops->switchdev_port_stp_update(dev, state);
558d51fa
RP
225
226 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 227 err = switchdev_port_stp_update(lower_dev, state);
558d51fa
RP
228 if (err && err != -EOPNOTSUPP)
229 return err;
230 }
231
232 return err;
38dcf357 233}
ebb9a03a 234EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
03bf0c28 235
ebb9a03a
JP
236static DEFINE_MUTEX(switchdev_mutex);
237static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
03bf0c28
JP
238
239/**
ebb9a03a 240 * register_switchdev_notifier - Register notifier
03bf0c28
JP
241 * @nb: notifier_block
242 *
243 * Register switch device notifier. This should be used by code
244 * which needs to monitor events happening in particular device.
245 * Return values are same as for atomic_notifier_chain_register().
246 */
ebb9a03a 247int register_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
248{
249 int err;
250
ebb9a03a
JP
251 mutex_lock(&switchdev_mutex);
252 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
253 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
254 return err;
255}
ebb9a03a 256EXPORT_SYMBOL_GPL(register_switchdev_notifier);
03bf0c28
JP
257
258/**
ebb9a03a 259 * unregister_switchdev_notifier - Unregister notifier
03bf0c28
JP
260 * @nb: notifier_block
261 *
262 * Unregister switch device notifier.
263 * Return values are same as for atomic_notifier_chain_unregister().
264 */
ebb9a03a 265int unregister_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
266{
267 int err;
268
ebb9a03a
JP
269 mutex_lock(&switchdev_mutex);
270 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
271 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
272 return err;
273}
ebb9a03a 274EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
03bf0c28
JP
275
276/**
ebb9a03a 277 * call_switchdev_notifiers - Call notifiers
03bf0c28
JP
278 * @val: value passed unmodified to notifier function
279 * @dev: port device
280 * @info: notifier information data
281 *
282 * Call all network notifier blocks. This should be called by driver
283 * when it needs to propagate hardware event.
284 * Return values are same as for atomic_notifier_call_chain().
285 */
ebb9a03a
JP
286int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
287 struct switchdev_notifier_info *info)
03bf0c28
JP
288{
289 int err;
290
291 info->dev = dev;
ebb9a03a
JP
292 mutex_lock(&switchdev_mutex);
293 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
294 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
295 return err;
296}
ebb9a03a 297EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
8a44dbb2
RP
298
299/**
ebb9a03a 300 * switchdev_port_bridge_setlink - Notify switch device port of bridge
8a44dbb2
RP
301 * port attributes
302 *
303 * @dev: port device
304 * @nlh: netlink msg with bridge port attributes
305 * @flags: bridge setlink flags
306 *
307 * Notify switch device port of bridge port attributes
308 */
ebb9a03a
JP
309int switchdev_port_bridge_setlink(struct net_device *dev,
310 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
311{
312 const struct net_device_ops *ops = dev->netdev_ops;
313
314 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
315 return 0;
316
317 if (!ops->ndo_bridge_setlink)
318 return -EOPNOTSUPP;
319
320 return ops->ndo_bridge_setlink(dev, nlh, flags);
321}
ebb9a03a 322EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
8a44dbb2
RP
323
324/**
ebb9a03a 325 * switchdev_port_bridge_dellink - Notify switch device port of bridge
8a44dbb2
RP
326 * port attribute delete
327 *
328 * @dev: port device
329 * @nlh: netlink msg with bridge port attributes
330 * @flags: bridge setlink flags
331 *
332 * Notify switch device port of bridge port attribute delete
333 */
ebb9a03a
JP
334int switchdev_port_bridge_dellink(struct net_device *dev,
335 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
336{
337 const struct net_device_ops *ops = dev->netdev_ops;
338
339 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
340 return 0;
341
342 if (!ops->ndo_bridge_dellink)
343 return -EOPNOTSUPP;
344
345 return ops->ndo_bridge_dellink(dev, nlh, flags);
346}
ebb9a03a 347EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
8a44dbb2
RP
348
349/**
ebb9a03a
JP
350 * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
351 * op for master devices
8a44dbb2
RP
352 *
353 * @dev: port device
354 * @nlh: netlink msg with bridge port attributes
355 * @flags: bridge setlink flags
356 *
357 * Notify master device slaves of bridge port attributes
358 */
ebb9a03a
JP
359int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
360 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
361{
362 struct net_device *lower_dev;
363 struct list_head *iter;
364 int ret = 0, err = 0;
365
366 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
367 return ret;
368
369 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 370 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
8a44dbb2
RP
371 if (err && err != -EOPNOTSUPP)
372 ret = err;
373 }
374
375 return ret;
376}
ebb9a03a 377EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
8a44dbb2
RP
378
379/**
ebb9a03a
JP
380 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
381 * op for master devices
8a44dbb2
RP
382 *
383 * @dev: port device
384 * @nlh: netlink msg with bridge port attributes
385 * @flags: bridge dellink flags
386 *
387 * Notify master device slaves of bridge port attribute deletes
388 */
ebb9a03a
JP
389int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
390 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
391{
392 struct net_device *lower_dev;
393 struct list_head *iter;
394 int ret = 0, err = 0;
395
396 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
397 return ret;
398
399 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 400 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
8a44dbb2
RP
401 if (err && err != -EOPNOTSUPP)
402 ret = err;
403 }
404
405 return ret;
406}
ebb9a03a 407EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
5e8d9049 408
ebb9a03a 409static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
b5d6fbde 410{
9d47c0a2 411 const struct switchdev_ops *ops = dev->switchdev_ops;
b5d6fbde
SF
412 struct net_device *lower_dev;
413 struct net_device *port_dev;
414 struct list_head *iter;
415
416 /* Recusively search down until we find a sw port dev.
9d47c0a2 417 * (A sw port dev supports switchdev_parent_id_get).
b5d6fbde
SF
418 */
419
420 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
9d47c0a2 421 ops && ops->switchdev_parent_id_get)
b5d6fbde
SF
422 return dev;
423
424 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 425 port_dev = switchdev_get_lowest_dev(lower_dev);
b5d6fbde
SF
426 if (port_dev)
427 return port_dev;
428 }
429
430 return NULL;
431}
432
ebb9a03a 433static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
b5d6fbde
SF
434{
435 struct netdev_phys_item_id psid;
436 struct netdev_phys_item_id prev_psid;
437 struct net_device *dev = NULL;
438 int nhsel;
439
440 /* For this route, all nexthop devs must be on the same switch. */
441
442 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
443 const struct fib_nh *nh = &fi->fib_nh[nhsel];
444
445 if (!nh->nh_dev)
446 return NULL;
447
ebb9a03a 448 dev = switchdev_get_lowest_dev(nh->nh_dev);
b5d6fbde
SF
449 if (!dev)
450 return NULL;
451
ebb9a03a 452 if (switchdev_parent_id_get(dev, &psid))
b5d6fbde
SF
453 return NULL;
454
455 if (nhsel > 0) {
456 if (prev_psid.id_len != psid.id_len)
457 return NULL;
458 if (memcmp(prev_psid.id, psid.id, psid.id_len))
459 return NULL;
460 }
461
462 prev_psid = psid;
463 }
464
465 return dev;
466}
467
5e8d9049 468/**
ebb9a03a 469 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
5e8d9049
SF
470 *
471 * @dst: route's IPv4 destination address
472 * @dst_len: destination address length (prefix length)
473 * @fi: route FIB info structure
474 * @tos: route TOS
475 * @type: route type
f8f21471 476 * @nlflags: netlink flags passed in (NLM_F_*)
5e8d9049
SF
477 * @tb_id: route table ID
478 *
479 * Add IPv4 route entry to switch device.
480 */
ebb9a03a
JP
481int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
482 u8 tos, u8 type, u32 nlflags, u32 tb_id)
5e8d9049 483{
b5d6fbde 484 struct net_device *dev;
9d47c0a2 485 const struct switchdev_ops *ops;
b5d6fbde
SF
486 int err = 0;
487
8e05fd71
SF
488 /* Don't offload route if using custom ip rules or if
489 * IPv4 FIB offloading has been disabled completely.
490 */
491
e1315db1
SF
492#ifdef CONFIG_IP_MULTIPLE_TABLES
493 if (fi->fib_net->ipv4.fib_has_custom_rules)
494 return 0;
495#endif
496
497 if (fi->fib_net->ipv4.fib_offload_disabled)
104616e7
SF
498 return 0;
499
ebb9a03a 500 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
501 if (!dev)
502 return 0;
9d47c0a2 503 ops = dev->switchdev_ops;
b5d6fbde 504
9d47c0a2
JP
505 if (ops->switchdev_fib_ipv4_add) {
506 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
507 fi, tos, type, nlflags,
508 tb_id);
b5d6fbde
SF
509 if (!err)
510 fi->fib_flags |= RTNH_F_EXTERNAL;
511 }
512
513 return err;
5e8d9049 514}
ebb9a03a 515EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
5e8d9049
SF
516
517/**
ebb9a03a 518 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
5e8d9049
SF
519 *
520 * @dst: route's IPv4 destination address
521 * @dst_len: destination address length (prefix length)
522 * @fi: route FIB info structure
523 * @tos: route TOS
524 * @type: route type
525 * @tb_id: route table ID
526 *
527 * Delete IPv4 route entry from switch device.
528 */
ebb9a03a
JP
529int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
530 u8 tos, u8 type, u32 tb_id)
5e8d9049 531{
b5d6fbde 532 struct net_device *dev;
9d47c0a2 533 const struct switchdev_ops *ops;
b5d6fbde
SF
534 int err = 0;
535
536 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
537 return 0;
538
ebb9a03a 539 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
540 if (!dev)
541 return 0;
9d47c0a2 542 ops = dev->switchdev_ops;
b5d6fbde 543
9d47c0a2
JP
544 if (ops->switchdev_fib_ipv4_del) {
545 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
546 fi, tos, type, tb_id);
b5d6fbde
SF
547 if (!err)
548 fi->fib_flags &= ~RTNH_F_EXTERNAL;
549 }
550
551 return err;
5e8d9049 552}
ebb9a03a 553EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
8e05fd71
SF
554
555/**
ebb9a03a 556 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
8e05fd71
SF
557 *
558 * @fi: route FIB info structure
559 */
ebb9a03a 560void switchdev_fib_ipv4_abort(struct fib_info *fi)
8e05fd71
SF
561{
562 /* There was a problem installing this route to the offload
563 * device. For now, until we come up with more refined
564 * policy handling, abruptly end IPv4 fib offloading for
565 * for entire net by flushing offload device(s) of all
566 * IPv4 routes, and mark IPv4 fib offloading broken from
567 * this point forward.
568 */
569
570 fib_flush_external(fi->fib_net);
571 fi->fib_net->ipv4.fib_offload_disabled = true;
572}
ebb9a03a 573EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);