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 <linux/if_bridge.h>
19 #include <net/ip_fib.h>
20 #include <net/switchdev.h>
23 * switchdev_port_attr_get - Get port attribute
26 * @attr: attribute to get
28 int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
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
36 int err = -EOPNOTSUPP;
38 if (ops && ops->switchdev_port_attr_get)
39 return ops->switchdev_port_attr_get(dev, attr);
41 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
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.
50 netdev_for_each_lower_dev(dev, lower_dev, iter) {
51 err = switchdev_port_attr_get(lower_dev, attr);
54 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
56 else if (memcmp(&first, attr, sizeof(*attr)))
62 EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
64 static int __switchdev_port_attr_set(struct net_device *dev,
65 struct switchdev_attr *attr)
67 const struct switchdev_ops *ops = dev->switchdev_ops;
68 struct net_device *lower_dev;
69 struct list_head *iter;
70 int err = -EOPNOTSUPP;
72 if (ops && ops->switchdev_port_attr_set)
73 return ops->switchdev_port_attr_set(dev, attr);
75 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
78 /* Switch device port(s) may be stacked under
79 * bond/team/vlan dev, so recurse down to set attr on
83 netdev_for_each_lower_dev(dev, lower_dev, iter) {
84 err = __switchdev_port_attr_set(lower_dev, attr);
92 struct switchdev_attr_set_work {
93 struct work_struct work;
94 struct net_device *dev;
95 struct switchdev_attr attr;
98 static void switchdev_port_attr_set_work(struct work_struct *work)
100 struct switchdev_attr_set_work *asw =
101 container_of(work, struct switchdev_attr_set_work, work);
105 err = switchdev_port_attr_set(asw->dev, &asw->attr);
113 static int switchdev_port_attr_set_defer(struct net_device *dev,
114 struct switchdev_attr *attr)
116 struct switchdev_attr_set_work *asw;
118 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
122 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
126 memcpy(&asw->attr, attr, sizeof(asw->attr));
128 schedule_work(&asw->work);
134 * switchdev_port_attr_set - Set port attribute
137 * @attr: attribute to set
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.
143 int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
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.
154 return switchdev_port_attr_set_defer(dev, attr);
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.
164 attr->trans = SWITCHDEV_TRANS_PREPARE;
165 err = __switchdev_port_attr_set(dev, attr);
167 /* Prepare phase failed: abort the transaction. Any
168 * resources reserved in the prepare phase are
172 attr->trans = SWITCHDEV_TRANS_ABORT;
173 __switchdev_port_attr_set(dev, attr);
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.
183 attr->trans = SWITCHDEV_TRANS_COMMIT;
184 err = __switchdev_port_attr_set(dev, attr);
189 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
191 static int __switchdev_port_obj_add(struct net_device *dev,
192 struct switchdev_obj *obj)
194 const struct switchdev_ops *ops = dev->switchdev_ops;
195 struct net_device *lower_dev;
196 struct list_head *iter;
197 int err = -EOPNOTSUPP;
199 if (ops && ops->switchdev_port_obj_add)
200 return ops->switchdev_port_obj_add(dev, obj);
202 /* Switch device port(s) may be stacked under
203 * bond/team/vlan dev, so recurse down to add object on
207 netdev_for_each_lower_dev(dev, lower_dev, iter) {
208 err = __switchdev_port_obj_add(lower_dev, obj);
217 * switchdev_port_obj_add - Add port object
220 * @obj: object to add
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.
226 * rtnl_lock must be held.
228 int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
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.
241 obj->trans = SWITCHDEV_TRANS_PREPARE;
242 err = __switchdev_port_obj_add(dev, obj);
244 /* Prepare phase failed: abort the transaction. Any
245 * resources reserved in the prepare phase are
249 obj->trans = SWITCHDEV_TRANS_ABORT;
250 __switchdev_port_obj_add(dev, obj);
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.
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);
266 EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
269 * switchdev_port_obj_del - Delete port object
272 * @obj: object to delete
274 int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
276 const struct switchdev_ops *ops = dev->switchdev_ops;
277 struct net_device *lower_dev;
278 struct list_head *iter;
279 int err = -EOPNOTSUPP;
281 if (ops && ops->switchdev_port_obj_del)
282 return ops->switchdev_port_obj_del(dev, obj);
284 /* Switch device port(s) may be stacked under
285 * bond/team/vlan dev, so recurse down to delete object on
289 netdev_for_each_lower_dev(dev, lower_dev, iter) {
290 err = switchdev_port_obj_del(lower_dev, obj);
297 EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
300 * switchdev_port_obj_dump - Dump port objects
303 * @obj: object to dump
305 int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
307 const struct switchdev_ops *ops = dev->switchdev_ops;
308 struct net_device *lower_dev;
309 struct list_head *iter;
310 int err = -EOPNOTSUPP;
312 if (ops && ops->switchdev_port_obj_dump)
313 return ops->switchdev_port_obj_dump(dev, obj);
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.
320 netdev_for_each_lower_dev(dev, lower_dev, iter) {
321 err = switchdev_port_obj_dump(lower_dev, obj);
327 EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
329 static DEFINE_MUTEX(switchdev_mutex);
330 static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
333 * register_switchdev_notifier - Register notifier
334 * @nb: notifier_block
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().
340 int register_switchdev_notifier(struct notifier_block *nb)
344 mutex_lock(&switchdev_mutex);
345 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
346 mutex_unlock(&switchdev_mutex);
349 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
352 * unregister_switchdev_notifier - Unregister notifier
353 * @nb: notifier_block
355 * Unregister switch device notifier.
356 * Return values are same as for atomic_notifier_chain_unregister().
358 int unregister_switchdev_notifier(struct notifier_block *nb)
362 mutex_lock(&switchdev_mutex);
363 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
364 mutex_unlock(&switchdev_mutex);
367 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
370 * call_switchdev_notifiers - Call notifiers
371 * @val: value passed unmodified to notifier function
373 * @info: notifier information data
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().
379 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
380 struct switchdev_notifier_info *info)
385 mutex_lock(&switchdev_mutex);
386 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
387 mutex_unlock(&switchdev_mutex);
390 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
393 * switchdev_port_bridge_getlink - Get bridge port attributes
397 * Called for SELF on rtnl_bridge_getlink to get bridge port
400 int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
401 struct net_device *dev, u32 filter_mask,
404 struct switchdev_attr attr = {
405 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
407 u16 mode = BRIDGE_MODE_UNDEF;
408 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
411 err = switchdev_port_attr_get(dev, &attr);
415 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
416 attr.u.brport_flags, mask, nlflags);
418 EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
420 static int switchdev_port_br_setflag(struct net_device *dev,
421 struct nlattr *nlattr,
422 unsigned long brport_flag)
424 struct switchdev_attr attr = {
425 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
427 u8 flag = nla_get_u8(nlattr);
430 err = switchdev_port_attr_get(dev, &attr);
435 attr.u.brport_flags |= brport_flag;
437 attr.u.brport_flags &= ~brport_flag;
439 return switchdev_port_attr_set(dev, &attr);
442 static const struct nla_policy
443 switchdev_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 },
456 static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
457 struct nlattr *protinfo)
463 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
464 switchdev_port_bridge_policy);
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,
474 case IFLA_BRPORT_LEARNING_SYNC:
475 err = switchdev_port_br_setflag(dev, attr,
489 static 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))
495 struct bridge_vlan_info *vinfo;
496 struct switchdev_obj obj = {
497 .id = SWITCHDEV_OBJ_PORT_VLAN,
499 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
503 nla_for_each_nested(attr, afspec, rem) {
504 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
506 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
508 vinfo = nla_data(attr);
509 vlan->flags = vinfo->flags;
510 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
513 vlan->vid_start = vinfo->vid;
514 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
515 if (!vlan->vid_start)
517 vlan->vid_end = vinfo->vid;
518 if (vlan->vid_end <= vlan->vid_start)
523 memset(vlan, 0, sizeof(*vlan));
527 vlan->vid_start = vinfo->vid;
528 vlan->vid_end = vinfo->vid;
532 memset(vlan, 0, sizeof(*vlan));
540 * switchdev_port_bridge_setlink - Set bridge port attributes
543 * @nlh: netlink header
544 * @flags: netlink flags
546 * Called for SELF on rtnl_bridge_setlink to set bridge port
549 int switchdev_port_bridge_setlink(struct net_device *dev,
550 struct nlmsghdr *nlh, u16 flags)
552 struct nlattr *protinfo;
553 struct nlattr *afspec;
556 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
559 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
564 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
567 err = switchdev_port_br_afspec(dev, afspec,
568 switchdev_port_obj_add);
572 EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
575 * switchdev_port_bridge_dellink - Set bridge port attributes
578 * @nlh: netlink header
579 * @flags: netlink flags
581 * Called for SELF on rtnl_bridge_dellink to set bridge port
584 int switchdev_port_bridge_dellink(struct net_device *dev,
585 struct nlmsghdr *nlh, u16 flags)
587 struct nlattr *afspec;
589 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
592 return switchdev_port_br_afspec(dev, afspec,
593 switchdev_port_obj_del);
597 EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
600 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
602 * @ndmsg: netlink hdr
603 * @nlattr: netlink attributes
605 * @addr: MAC address to add
608 * Add FDB entry to switch device.
610 int 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)
614 struct switchdev_obj obj = {
615 .id = SWITCHDEV_OBJ_PORT_FDB,
622 return switchdev_port_obj_add(dev, &obj);
624 EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
627 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
629 * @ndmsg: netlink hdr
630 * @nlattr: netlink attributes
632 * @addr: MAC address to delete
633 * @vid: VLAN to delete
635 * Delete FDB entry from switch device.
637 int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
638 struct net_device *dev, const unsigned char *addr,
641 struct switchdev_obj obj = {
642 .id = SWITCHDEV_OBJ_PORT_FDB,
649 return switchdev_port_obj_del(dev, &obj);
651 EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
653 struct switchdev_fdb_dump {
654 struct switchdev_obj obj;
656 struct netlink_callback *cb;
657 struct net_device *filter_dev;
661 static int switchdev_port_fdb_dump_cb(struct net_device *dev,
662 struct switchdev_obj *obj)
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;
670 struct net_device *master = netdev_master_upper_dev_get(dev);
672 if (dump->idx < dump->cb->args[0])
675 if (master && dump->filter_dev != master)
678 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
679 sizeof(*ndm), NLM_F_MULTI);
683 ndm = nlmsg_data(nlh);
684 ndm->ndm_family = AF_BRIDGE;
687 ndm->ndm_flags = NTF_SELF;
689 ndm->ndm_ifindex = dev->ifindex;
690 ndm->ndm_state = NUD_REACHABLE;
692 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
693 goto nla_put_failure;
695 if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
696 goto nla_put_failure;
698 nlmsg_end(dump->skb, nlh);
705 nlmsg_cancel(dump->skb, nlh);
710 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
713 * @cb: netlink callback
715 * @filter_dev: filter device
718 * Delete FDB entry from switch device.
720 int 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)
724 struct switchdev_fdb_dump dump = {
726 .id = SWITCHDEV_OBJ_PORT_FDB,
727 .cb = switchdev_port_fdb_dump_cb,
731 .filter_dev = filter_dev,
736 err = switchdev_port_obj_dump(dev, &dump.obj);
742 EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
744 static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
746 const struct switchdev_ops *ops = dev->switchdev_ops;
747 struct net_device *lower_dev;
748 struct net_device *port_dev;
749 struct list_head *iter;
751 /* Recusively search down until we find a sw port dev.
752 * (A sw port dev supports switchdev_port_attr_get).
755 if (ops && ops->switchdev_port_attr_get)
758 netdev_for_each_lower_dev(dev, lower_dev, iter) {
759 port_dev = switchdev_get_lowest_dev(lower_dev);
767 static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
769 struct switchdev_attr attr = {
770 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
772 struct switchdev_attr prev_attr;
773 struct net_device *dev = NULL;
776 /* For this route, all nexthop devs must be on the same switch. */
778 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
779 const struct fib_nh *nh = &fi->fib_nh[nhsel];
784 dev = switchdev_get_lowest_dev(nh->nh_dev);
788 if (switchdev_port_attr_get(dev, &attr))
792 if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
794 if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
806 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
808 * @dst: route's IPv4 destination address
809 * @dst_len: destination address length (prefix length)
810 * @fi: route FIB info structure
813 * @nlflags: netlink flags passed in (NLM_F_*)
814 * @tb_id: route table ID
816 * Add IPv4 route entry to switch device.
818 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
819 u8 tos, u8 type, u32 nlflags, u32 tb_id)
821 struct switchdev_obj fib_obj = {
822 .id = SWITCHDEV_OBJ_IPV4_FIB,
833 struct net_device *dev;
836 /* Don't offload route if using custom ip rules or if
837 * IPv4 FIB offloading has been disabled completely.
840 #ifdef CONFIG_IP_MULTIPLE_TABLES
841 if (fi->fib_net->ipv4.fib_has_custom_rules)
845 if (fi->fib_net->ipv4.fib_offload_disabled)
848 dev = switchdev_get_dev_by_nhs(fi);
852 err = switchdev_port_obj_add(dev, &fib_obj);
854 fi->fib_flags |= RTNH_F_OFFLOAD;
858 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
861 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
863 * @dst: route's IPv4 destination address
864 * @dst_len: destination address length (prefix length)
865 * @fi: route FIB info structure
868 * @tb_id: route table ID
870 * Delete IPv4 route entry from switch device.
872 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
873 u8 tos, u8 type, u32 tb_id)
875 struct switchdev_obj fib_obj = {
876 .id = SWITCHDEV_OBJ_IPV4_FIB,
887 struct net_device *dev;
890 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
893 dev = switchdev_get_dev_by_nhs(fi);
897 err = switchdev_port_obj_del(dev, &fib_obj);
899 fi->fib_flags &= ~RTNH_F_OFFLOAD;
903 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
906 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
908 * @fi: route FIB info structure
910 void switchdev_fib_ipv4_abort(struct fib_info *fi)
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.
920 fib_flush_external(fi->fib_net);
921 fi->fib_net->ipv4.fib_offload_disabled = true;
923 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);