2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
4 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
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.
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <net/ip_fib.h>
19 #include <net/switchdev.h>
22 * switchdev_parent_id_get - Get ID of a switch
26 * Get ID of a switch this port is part of.
28 int switchdev_parent_id_get(struct net_device *dev,
29 struct netdev_phys_item_id *psid)
31 const struct switchdev_ops *ops = dev->switchdev_ops;
33 if (!ops || !ops->switchdev_parent_id_get)
35 return ops->switchdev_parent_id_get(dev, psid);
37 EXPORT_SYMBOL_GPL(switchdev_parent_id_get);
40 * switchdev_port_attr_get - Get port attribute
43 * @attr: attribute to get
45 int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
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
53 int err = -EOPNOTSUPP;
55 if (ops && ops->switchdev_port_attr_get)
56 return ops->switchdev_port_attr_get(dev, attr);
58 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
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.
67 netdev_for_each_lower_dev(dev, lower_dev, iter) {
68 err = switchdev_port_attr_get(lower_dev, attr);
71 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
73 else if (memcmp(&first, attr, sizeof(*attr)))
79 EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
81 static int __switchdev_port_attr_set(struct net_device *dev,
82 struct switchdev_attr *attr)
84 const struct switchdev_ops *ops = dev->switchdev_ops;
85 struct net_device *lower_dev;
86 struct list_head *iter;
87 int err = -EOPNOTSUPP;
89 if (ops && ops->switchdev_port_attr_set)
90 return ops->switchdev_port_attr_set(dev, attr);
92 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
95 /* Switch device port(s) may be stacked under
96 * bond/team/vlan dev, so recurse down to set attr on
100 netdev_for_each_lower_dev(dev, lower_dev, iter) {
101 err = __switchdev_port_attr_set(lower_dev, attr);
109 struct switchdev_attr_set_work {
110 struct work_struct work;
111 struct net_device *dev;
112 struct switchdev_attr attr;
115 static void switchdev_port_attr_set_work(struct work_struct *work)
117 struct switchdev_attr_set_work *asw =
118 container_of(work, struct switchdev_attr_set_work, work);
122 err = switchdev_port_attr_set(asw->dev, &asw->attr);
130 static int switchdev_port_attr_set_defer(struct net_device *dev,
131 struct switchdev_attr *attr)
133 struct switchdev_attr_set_work *asw;
135 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
139 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
143 memcpy(&asw->attr, attr, sizeof(asw->attr));
145 schedule_work(&asw->work);
151 * switchdev_port_attr_set - Set port attribute
154 * @attr: attribute to set
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.
160 int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
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.
171 return switchdev_port_attr_set_defer(dev, attr);
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.
181 attr->trans = SWITCHDEV_TRANS_PREPARE;
182 err = __switchdev_port_attr_set(dev, attr);
184 /* Prepare phase failed: abort the transaction. Any
185 * resources reserved in the prepare phase are
189 attr->trans = SWITCHDEV_TRANS_ABORT;
190 __switchdev_port_attr_set(dev, attr);
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.
200 attr->trans = SWITCHDEV_TRANS_COMMIT;
201 err = __switchdev_port_attr_set(dev, attr);
206 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
209 * switchdev_port_stp_update - Notify switch device port of STP
212 * @state: port STP state
214 * Notify switch device port of bridge port STP state change.
216 int switchdev_port_stp_update(struct net_device *dev, u8 state)
218 const struct switchdev_ops *ops = dev->switchdev_ops;
219 struct net_device *lower_dev;
220 struct list_head *iter;
221 int err = -EOPNOTSUPP;
223 if (ops && ops->switchdev_port_stp_update)
224 return ops->switchdev_port_stp_update(dev, state);
226 netdev_for_each_lower_dev(dev, lower_dev, iter) {
227 err = switchdev_port_stp_update(lower_dev, state);
228 if (err && err != -EOPNOTSUPP)
234 EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
236 static DEFINE_MUTEX(switchdev_mutex);
237 static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
240 * register_switchdev_notifier - Register notifier
241 * @nb: notifier_block
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().
247 int register_switchdev_notifier(struct notifier_block *nb)
251 mutex_lock(&switchdev_mutex);
252 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
253 mutex_unlock(&switchdev_mutex);
256 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
259 * unregister_switchdev_notifier - Unregister notifier
260 * @nb: notifier_block
262 * Unregister switch device notifier.
263 * Return values are same as for atomic_notifier_chain_unregister().
265 int unregister_switchdev_notifier(struct notifier_block *nb)
269 mutex_lock(&switchdev_mutex);
270 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
271 mutex_unlock(&switchdev_mutex);
274 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
277 * call_switchdev_notifiers - Call notifiers
278 * @val: value passed unmodified to notifier function
280 * @info: notifier information data
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().
286 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
287 struct switchdev_notifier_info *info)
292 mutex_lock(&switchdev_mutex);
293 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
294 mutex_unlock(&switchdev_mutex);
297 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
300 * switchdev_port_bridge_setlink - Notify switch device port of bridge
304 * @nlh: netlink msg with bridge port attributes
305 * @flags: bridge setlink flags
307 * Notify switch device port of bridge port attributes
309 int switchdev_port_bridge_setlink(struct net_device *dev,
310 struct nlmsghdr *nlh, u16 flags)
312 const struct net_device_ops *ops = dev->netdev_ops;
314 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
317 if (!ops->ndo_bridge_setlink)
320 return ops->ndo_bridge_setlink(dev, nlh, flags);
322 EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
325 * switchdev_port_bridge_dellink - Notify switch device port of bridge
326 * port attribute delete
329 * @nlh: netlink msg with bridge port attributes
330 * @flags: bridge setlink flags
332 * Notify switch device port of bridge port attribute delete
334 int switchdev_port_bridge_dellink(struct net_device *dev,
335 struct nlmsghdr *nlh, u16 flags)
337 const struct net_device_ops *ops = dev->netdev_ops;
339 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
342 if (!ops->ndo_bridge_dellink)
345 return ops->ndo_bridge_dellink(dev, nlh, flags);
347 EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
350 * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
351 * op for master devices
354 * @nlh: netlink msg with bridge port attributes
355 * @flags: bridge setlink flags
357 * Notify master device slaves of bridge port attributes
359 int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
360 struct nlmsghdr *nlh, u16 flags)
362 struct net_device *lower_dev;
363 struct list_head *iter;
364 int ret = 0, err = 0;
366 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
369 netdev_for_each_lower_dev(dev, lower_dev, iter) {
370 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
371 if (err && err != -EOPNOTSUPP)
377 EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
380 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
381 * op for master devices
384 * @nlh: netlink msg with bridge port attributes
385 * @flags: bridge dellink flags
387 * Notify master device slaves of bridge port attribute deletes
389 int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
390 struct nlmsghdr *nlh, u16 flags)
392 struct net_device *lower_dev;
393 struct list_head *iter;
394 int ret = 0, err = 0;
396 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
399 netdev_for_each_lower_dev(dev, lower_dev, iter) {
400 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
401 if (err && err != -EOPNOTSUPP)
407 EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
409 static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
411 const struct switchdev_ops *ops = dev->switchdev_ops;
412 struct net_device *lower_dev;
413 struct net_device *port_dev;
414 struct list_head *iter;
416 /* Recusively search down until we find a sw port dev.
417 * (A sw port dev supports switchdev_parent_id_get).
420 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
421 ops && ops->switchdev_parent_id_get)
424 netdev_for_each_lower_dev(dev, lower_dev, iter) {
425 port_dev = switchdev_get_lowest_dev(lower_dev);
433 static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
435 struct netdev_phys_item_id psid;
436 struct netdev_phys_item_id prev_psid;
437 struct net_device *dev = NULL;
440 /* For this route, all nexthop devs must be on the same switch. */
442 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
443 const struct fib_nh *nh = &fi->fib_nh[nhsel];
448 dev = switchdev_get_lowest_dev(nh->nh_dev);
452 if (switchdev_parent_id_get(dev, &psid))
456 if (prev_psid.id_len != psid.id_len)
458 if (memcmp(prev_psid.id, psid.id, psid.id_len))
469 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
471 * @dst: route's IPv4 destination address
472 * @dst_len: destination address length (prefix length)
473 * @fi: route FIB info structure
476 * @nlflags: netlink flags passed in (NLM_F_*)
477 * @tb_id: route table ID
479 * Add IPv4 route entry to switch device.
481 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
482 u8 tos, u8 type, u32 nlflags, u32 tb_id)
484 struct net_device *dev;
485 const struct switchdev_ops *ops;
488 /* Don't offload route if using custom ip rules or if
489 * IPv4 FIB offloading has been disabled completely.
492 #ifdef CONFIG_IP_MULTIPLE_TABLES
493 if (fi->fib_net->ipv4.fib_has_custom_rules)
497 if (fi->fib_net->ipv4.fib_offload_disabled)
500 dev = switchdev_get_dev_by_nhs(fi);
503 ops = dev->switchdev_ops;
505 if (ops->switchdev_fib_ipv4_add) {
506 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
507 fi, tos, type, nlflags,
510 fi->fib_flags |= RTNH_F_EXTERNAL;
515 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
518 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
520 * @dst: route's IPv4 destination address
521 * @dst_len: destination address length (prefix length)
522 * @fi: route FIB info structure
525 * @tb_id: route table ID
527 * Delete IPv4 route entry from switch device.
529 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
530 u8 tos, u8 type, u32 tb_id)
532 struct net_device *dev;
533 const struct switchdev_ops *ops;
536 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
539 dev = switchdev_get_dev_by_nhs(fi);
542 ops = dev->switchdev_ops;
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);
548 fi->fib_flags &= ~RTNH_F_EXTERNAL;
553 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
556 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
558 * @fi: route FIB info structure
560 void switchdev_fib_ipv4_abort(struct fib_info *fi)
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.
570 fib_flush_external(fi->fib_net);
571 fi->fib_net->ipv4.fib_offload_disabled = true;
573 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);