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/etherdevice.h>
19 #include <linux/if_bridge.h>
20 #include <net/ip_fib.h>
21 #include <net/switchdev.h>
24 * switchdev_port_attr_get - Get port attribute
27 * @attr: attribute to get
29 int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
31 const struct switchdev_ops *ops = dev->switchdev_ops;
32 struct net_device *lower_dev;
33 struct list_head *iter;
34 struct switchdev_attr first = {
35 .id = SWITCHDEV_ATTR_UNDEFINED
37 int err = -EOPNOTSUPP;
39 if (ops && ops->switchdev_port_attr_get)
40 return ops->switchdev_port_attr_get(dev, attr);
42 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
45 /* Switch device port(s) may be stacked under
46 * bond/team/vlan dev, so recurse down to get attr on
47 * each port. Return -ENODATA if attr values don't
48 * compare across ports.
51 netdev_for_each_lower_dev(dev, lower_dev, iter) {
52 err = switchdev_port_attr_get(lower_dev, attr);
55 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
57 else if (memcmp(&first, attr, sizeof(*attr)))
63 EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
65 static int __switchdev_port_attr_set(struct net_device *dev,
66 struct switchdev_attr *attr)
68 const struct switchdev_ops *ops = dev->switchdev_ops;
69 struct net_device *lower_dev;
70 struct list_head *iter;
71 int err = -EOPNOTSUPP;
73 if (ops && ops->switchdev_port_attr_set)
74 return ops->switchdev_port_attr_set(dev, attr);
76 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
79 /* Switch device port(s) may be stacked under
80 * bond/team/vlan dev, so recurse down to set attr on
84 netdev_for_each_lower_dev(dev, lower_dev, iter) {
85 err = __switchdev_port_attr_set(lower_dev, attr);
93 struct switchdev_attr_set_work {
94 struct work_struct work;
95 struct net_device *dev;
96 struct switchdev_attr attr;
99 static void switchdev_port_attr_set_work(struct work_struct *work)
101 struct switchdev_attr_set_work *asw =
102 container_of(work, struct switchdev_attr_set_work, work);
106 err = switchdev_port_attr_set(asw->dev, &asw->attr);
107 if (err && err != -EOPNOTSUPP)
108 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
116 static int switchdev_port_attr_set_defer(struct net_device *dev,
117 struct switchdev_attr *attr)
119 struct switchdev_attr_set_work *asw;
121 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
125 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
129 memcpy(&asw->attr, attr, sizeof(asw->attr));
131 schedule_work(&asw->work);
137 * switchdev_port_attr_set - Set port attribute
140 * @attr: attribute to set
142 * Use a 2-phase prepare-commit transaction model to ensure
143 * system is not left in a partially updated state due to
144 * failure from driver/device.
146 int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
150 if (!rtnl_is_locked()) {
151 /* Running prepare-commit transaction across stacked
152 * devices requires nothing moves, so if rtnl_lock is
153 * not held, schedule a worker thread to hold rtnl_lock
154 * while setting attr.
157 return switchdev_port_attr_set_defer(dev, attr);
160 /* Phase I: prepare for attr set. Driver/device should fail
161 * here if there are going to be issues in the commit phase,
162 * such as lack of resources or support. The driver/device
163 * should reserve resources needed for the commit phase here,
164 * but should not commit the attr.
167 attr->trans = SWITCHDEV_TRANS_PREPARE;
168 err = __switchdev_port_attr_set(dev, attr);
170 /* Prepare phase failed: abort the transaction. Any
171 * resources reserved in the prepare phase are
175 if (err != -EOPNOTSUPP) {
176 attr->trans = SWITCHDEV_TRANS_ABORT;
177 __switchdev_port_attr_set(dev, attr);
183 /* Phase II: commit attr set. This cannot fail as a fault
184 * of driver/device. If it does, it's a bug in the driver/device
185 * because the driver said everythings was OK in phase I.
188 attr->trans = SWITCHDEV_TRANS_COMMIT;
189 err = __switchdev_port_attr_set(dev, attr);
190 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
191 dev->name, attr->id);
195 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
197 static int __switchdev_port_obj_add(struct net_device *dev,
198 struct switchdev_obj *obj)
200 const struct switchdev_ops *ops = dev->switchdev_ops;
201 struct net_device *lower_dev;
202 struct list_head *iter;
203 int err = -EOPNOTSUPP;
205 if (ops && ops->switchdev_port_obj_add)
206 return ops->switchdev_port_obj_add(dev, obj);
208 /* Switch device port(s) may be stacked under
209 * bond/team/vlan dev, so recurse down to add object on
213 netdev_for_each_lower_dev(dev, lower_dev, iter) {
214 err = __switchdev_port_obj_add(lower_dev, obj);
223 * switchdev_port_obj_add - Add port object
226 * @obj: object to add
228 * Use a 2-phase prepare-commit transaction model to ensure
229 * system is not left in a partially updated state due to
230 * failure from driver/device.
232 * rtnl_lock must be held.
234 int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
240 /* Phase I: prepare for obj add. Driver/device should fail
241 * here if there are going to be issues in the commit phase,
242 * such as lack of resources or support. The driver/device
243 * should reserve resources needed for the commit phase here,
244 * but should not commit the obj.
247 obj->trans = SWITCHDEV_TRANS_PREPARE;
248 err = __switchdev_port_obj_add(dev, obj);
250 /* Prepare phase failed: abort the transaction. Any
251 * resources reserved in the prepare phase are
255 if (err != -EOPNOTSUPP) {
256 obj->trans = SWITCHDEV_TRANS_ABORT;
257 __switchdev_port_obj_add(dev, obj);
263 /* Phase II: commit obj add. This cannot fail as a fault
264 * of driver/device. If it does, it's a bug in the driver/device
265 * because the driver said everythings was OK in phase I.
268 obj->trans = SWITCHDEV_TRANS_COMMIT;
269 err = __switchdev_port_obj_add(dev, obj);
270 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
274 EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
277 * switchdev_port_obj_del - Delete port object
280 * @obj: object to delete
282 int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
284 const struct switchdev_ops *ops = dev->switchdev_ops;
285 struct net_device *lower_dev;
286 struct list_head *iter;
287 int err = -EOPNOTSUPP;
289 if (ops && ops->switchdev_port_obj_del)
290 return ops->switchdev_port_obj_del(dev, obj);
292 /* Switch device port(s) may be stacked under
293 * bond/team/vlan dev, so recurse down to delete object on
297 netdev_for_each_lower_dev(dev, lower_dev, iter) {
298 err = switchdev_port_obj_del(lower_dev, obj);
305 EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
308 * switchdev_port_obj_dump - Dump port objects
311 * @obj: object to dump
313 int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
315 const struct switchdev_ops *ops = dev->switchdev_ops;
316 struct net_device *lower_dev;
317 struct list_head *iter;
318 int err = -EOPNOTSUPP;
320 if (ops && ops->switchdev_port_obj_dump)
321 return ops->switchdev_port_obj_dump(dev, obj);
323 /* Switch device port(s) may be stacked under
324 * bond/team/vlan dev, so recurse down to dump objects on
325 * first port at bottom of stack.
328 netdev_for_each_lower_dev(dev, lower_dev, iter) {
329 err = switchdev_port_obj_dump(lower_dev, obj);
335 EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
337 static DEFINE_MUTEX(switchdev_mutex);
338 static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
341 * register_switchdev_notifier - Register notifier
342 * @nb: notifier_block
344 * Register switch device notifier. This should be used by code
345 * which needs to monitor events happening in particular device.
346 * Return values are same as for atomic_notifier_chain_register().
348 int register_switchdev_notifier(struct notifier_block *nb)
352 mutex_lock(&switchdev_mutex);
353 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
354 mutex_unlock(&switchdev_mutex);
357 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
360 * unregister_switchdev_notifier - Unregister notifier
361 * @nb: notifier_block
363 * Unregister switch device notifier.
364 * Return values are same as for atomic_notifier_chain_unregister().
366 int unregister_switchdev_notifier(struct notifier_block *nb)
370 mutex_lock(&switchdev_mutex);
371 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
372 mutex_unlock(&switchdev_mutex);
375 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
378 * call_switchdev_notifiers - Call notifiers
379 * @val: value passed unmodified to notifier function
381 * @info: notifier information data
383 * Call all network notifier blocks. This should be called by driver
384 * when it needs to propagate hardware event.
385 * Return values are same as for atomic_notifier_call_chain().
387 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
388 struct switchdev_notifier_info *info)
393 mutex_lock(&switchdev_mutex);
394 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
395 mutex_unlock(&switchdev_mutex);
398 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
400 struct switchdev_vlan_dump {
401 struct switchdev_obj obj;
409 static int switchdev_port_vlan_dump_put(struct net_device *dev,
410 struct switchdev_vlan_dump *dump)
412 struct bridge_vlan_info vinfo;
414 vinfo.flags = dump->flags;
416 if (dump->begin == 0 && dump->end == 0) {
418 } else if (dump->begin == dump->end) {
419 vinfo.vid = dump->begin;
420 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
421 sizeof(vinfo), &vinfo))
424 vinfo.vid = dump->begin;
425 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
426 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
427 sizeof(vinfo), &vinfo))
429 vinfo.vid = dump->end;
430 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
431 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
432 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
433 sizeof(vinfo), &vinfo))
440 static int switchdev_port_vlan_dump_cb(struct net_device *dev,
441 struct switchdev_obj *obj)
443 struct switchdev_vlan_dump *dump =
444 container_of(obj, struct switchdev_vlan_dump, obj);
445 struct switchdev_obj_vlan *vlan = &dump->obj.u.vlan;
448 if (vlan->vid_begin > vlan->vid_end)
451 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
452 dump->flags = vlan->flags;
453 for (dump->begin = dump->end = vlan->vid_begin;
454 dump->begin <= vlan->vid_end;
455 dump->begin++, dump->end++) {
456 err = switchdev_port_vlan_dump_put(dev, dump);
460 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
461 if (dump->begin > vlan->vid_begin &&
462 dump->begin >= vlan->vid_end) {
463 if ((dump->begin - 1) == vlan->vid_end &&
464 dump->flags == vlan->flags) {
466 dump->begin = vlan->vid_begin;
468 err = switchdev_port_vlan_dump_put(dev, dump);
469 dump->flags = vlan->flags;
470 dump->begin = vlan->vid_begin;
471 dump->end = vlan->vid_end;
473 } else if (dump->end <= vlan->vid_begin &&
474 dump->end < vlan->vid_end) {
475 if ((dump->end + 1) == vlan->vid_begin &&
476 dump->flags == vlan->flags) {
478 dump->end = vlan->vid_end;
480 err = switchdev_port_vlan_dump_put(dev, dump);
481 dump->flags = vlan->flags;
482 dump->begin = vlan->vid_begin;
483 dump->end = vlan->vid_end;
493 static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
496 struct switchdev_vlan_dump dump = {
498 .id = SWITCHDEV_OBJ_PORT_VLAN,
499 .cb = switchdev_port_vlan_dump_cb,
502 .filter_mask = filter_mask,
506 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
507 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
508 err = switchdev_port_obj_dump(dev, &dump.obj);
511 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
513 err = switchdev_port_vlan_dump_put(dev, &dump);
517 return err == -EOPNOTSUPP ? 0 : err;
521 * switchdev_port_bridge_getlink - Get bridge port attributes
525 * Called for SELF on rtnl_bridge_getlink to get bridge port
528 int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
529 struct net_device *dev, u32 filter_mask,
532 struct switchdev_attr attr = {
533 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
535 u16 mode = BRIDGE_MODE_UNDEF;
536 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
539 err = switchdev_port_attr_get(dev, &attr);
540 if (err && err != -EOPNOTSUPP)
543 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
544 attr.u.brport_flags, mask, nlflags,
545 filter_mask, switchdev_port_vlan_fill);
547 EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
549 static int switchdev_port_br_setflag(struct net_device *dev,
550 struct nlattr *nlattr,
551 unsigned long brport_flag)
553 struct switchdev_attr attr = {
554 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
556 u8 flag = nla_get_u8(nlattr);
559 err = switchdev_port_attr_get(dev, &attr);
564 attr.u.brport_flags |= brport_flag;
566 attr.u.brport_flags &= ~brport_flag;
568 return switchdev_port_attr_set(dev, &attr);
571 static const struct nla_policy
572 switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
573 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
574 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
575 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
576 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
577 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
578 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
579 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
580 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
581 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
582 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
585 static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
586 struct nlattr *protinfo)
592 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
593 switchdev_port_bridge_policy);
597 nla_for_each_nested(attr, protinfo, rem) {
598 switch (nla_type(attr)) {
599 case IFLA_BRPORT_LEARNING:
600 err = switchdev_port_br_setflag(dev, attr,
603 case IFLA_BRPORT_LEARNING_SYNC:
604 err = switchdev_port_br_setflag(dev, attr,
618 static int switchdev_port_br_afspec(struct net_device *dev,
619 struct nlattr *afspec,
620 int (*f)(struct net_device *dev,
621 struct switchdev_obj *obj))
624 struct bridge_vlan_info *vinfo;
625 struct switchdev_obj obj = {
626 .id = SWITCHDEV_OBJ_PORT_VLAN,
628 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
632 nla_for_each_nested(attr, afspec, rem) {
633 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
635 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
637 vinfo = nla_data(attr);
638 vlan->flags = vinfo->flags;
639 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
642 vlan->vid_begin = vinfo->vid;
643 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
644 if (!vlan->vid_begin)
646 vlan->vid_end = vinfo->vid;
647 if (vlan->vid_end <= vlan->vid_begin)
652 memset(vlan, 0, sizeof(*vlan));
656 vlan->vid_begin = vinfo->vid;
657 vlan->vid_end = vinfo->vid;
661 memset(vlan, 0, sizeof(*vlan));
669 * switchdev_port_bridge_setlink - Set bridge port attributes
672 * @nlh: netlink header
673 * @flags: netlink flags
675 * Called for SELF on rtnl_bridge_setlink to set bridge port
678 int switchdev_port_bridge_setlink(struct net_device *dev,
679 struct nlmsghdr *nlh, u16 flags)
681 struct nlattr *protinfo;
682 struct nlattr *afspec;
685 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
688 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
693 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
696 err = switchdev_port_br_afspec(dev, afspec,
697 switchdev_port_obj_add);
701 EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
704 * switchdev_port_bridge_dellink - Set bridge port attributes
707 * @nlh: netlink header
708 * @flags: netlink flags
710 * Called for SELF on rtnl_bridge_dellink to set bridge port
713 int switchdev_port_bridge_dellink(struct net_device *dev,
714 struct nlmsghdr *nlh, u16 flags)
716 struct nlattr *afspec;
718 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
721 return switchdev_port_br_afspec(dev, afspec,
722 switchdev_port_obj_del);
726 EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
729 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
731 * @ndmsg: netlink hdr
732 * @nlattr: netlink attributes
734 * @addr: MAC address to add
737 * Add FDB entry to switch device.
739 int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
740 struct net_device *dev, const unsigned char *addr,
741 u16 vid, u16 nlm_flags)
743 struct switchdev_obj obj = {
744 .id = SWITCHDEV_OBJ_PORT_FDB,
750 ether_addr_copy(obj.u.fdb.addr, addr);
751 return switchdev_port_obj_add(dev, &obj);
753 EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
756 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
758 * @ndmsg: netlink hdr
759 * @nlattr: netlink attributes
761 * @addr: MAC address to delete
762 * @vid: VLAN to delete
764 * Delete FDB entry from switch device.
766 int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
767 struct net_device *dev, const unsigned char *addr,
770 struct switchdev_obj obj = {
771 .id = SWITCHDEV_OBJ_PORT_FDB,
777 ether_addr_copy(obj.u.fdb.addr, addr);
778 return switchdev_port_obj_del(dev, &obj);
780 EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
782 struct switchdev_fdb_dump {
783 struct switchdev_obj obj;
785 struct netlink_callback *cb;
789 static int switchdev_port_fdb_dump_cb(struct net_device *dev,
790 struct switchdev_obj *obj)
792 struct switchdev_fdb_dump *dump =
793 container_of(obj, struct switchdev_fdb_dump, obj);
794 u32 portid = NETLINK_CB(dump->cb->skb).portid;
795 u32 seq = dump->cb->nlh->nlmsg_seq;
796 struct nlmsghdr *nlh;
799 if (dump->idx < dump->cb->args[0])
802 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
803 sizeof(*ndm), NLM_F_MULTI);
807 ndm = nlmsg_data(nlh);
808 ndm->ndm_family = AF_BRIDGE;
811 ndm->ndm_flags = NTF_SELF;
813 ndm->ndm_ifindex = dev->ifindex;
814 ndm->ndm_state = NUD_REACHABLE;
816 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
817 goto nla_put_failure;
819 if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
820 goto nla_put_failure;
822 nlmsg_end(dump->skb, nlh);
829 nlmsg_cancel(dump->skb, nlh);
834 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
837 * @cb: netlink callback
839 * @filter_dev: filter device
842 * Delete FDB entry from switch device.
844 int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
845 struct net_device *dev,
846 struct net_device *filter_dev, int idx)
848 struct switchdev_fdb_dump dump = {
850 .id = SWITCHDEV_OBJ_PORT_FDB,
851 .cb = switchdev_port_fdb_dump_cb,
859 err = switchdev_port_obj_dump(dev, &dump.obj);
865 EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
867 static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
869 const struct switchdev_ops *ops = dev->switchdev_ops;
870 struct net_device *lower_dev;
871 struct net_device *port_dev;
872 struct list_head *iter;
874 /* Recusively search down until we find a sw port dev.
875 * (A sw port dev supports switchdev_port_attr_get).
878 if (ops && ops->switchdev_port_attr_get)
881 netdev_for_each_lower_dev(dev, lower_dev, iter) {
882 port_dev = switchdev_get_lowest_dev(lower_dev);
890 static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
892 struct switchdev_attr attr = {
893 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
895 struct switchdev_attr prev_attr;
896 struct net_device *dev = NULL;
899 /* For this route, all nexthop devs must be on the same switch. */
901 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
902 const struct fib_nh *nh = &fi->fib_nh[nhsel];
907 dev = switchdev_get_lowest_dev(nh->nh_dev);
911 if (switchdev_port_attr_get(dev, &attr))
915 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
925 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
927 * @dst: route's IPv4 destination address
928 * @dst_len: destination address length (prefix length)
929 * @fi: route FIB info structure
932 * @nlflags: netlink flags passed in (NLM_F_*)
933 * @tb_id: route table ID
935 * Add/modify switch IPv4 route entry.
937 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
938 u8 tos, u8 type, u32 nlflags, u32 tb_id)
940 struct switchdev_obj fib_obj = {
941 .id = SWITCHDEV_OBJ_IPV4_FIB,
952 struct net_device *dev;
955 /* Don't offload route if using custom ip rules or if
956 * IPv4 FIB offloading has been disabled completely.
959 #ifdef CONFIG_IP_MULTIPLE_TABLES
960 if (fi->fib_net->ipv4.fib_has_custom_rules)
964 if (fi->fib_net->ipv4.fib_offload_disabled)
967 dev = switchdev_get_dev_by_nhs(fi);
971 err = switchdev_port_obj_add(dev, &fib_obj);
973 fi->fib_flags |= RTNH_F_OFFLOAD;
975 return err == -EOPNOTSUPP ? 0 : err;
977 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
980 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
982 * @dst: route's IPv4 destination address
983 * @dst_len: destination address length (prefix length)
984 * @fi: route FIB info structure
987 * @tb_id: route table ID
989 * Delete IPv4 route entry from switch device.
991 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
992 u8 tos, u8 type, u32 tb_id)
994 struct switchdev_obj fib_obj = {
995 .id = SWITCHDEV_OBJ_IPV4_FIB,
1006 struct net_device *dev;
1009 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
1012 dev = switchdev_get_dev_by_nhs(fi);
1016 err = switchdev_port_obj_del(dev, &fib_obj);
1018 fi->fib_flags &= ~RTNH_F_OFFLOAD;
1020 return err == -EOPNOTSUPP ? 0 : err;
1022 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
1025 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
1027 * @fi: route FIB info structure
1029 void switchdev_fib_ipv4_abort(struct fib_info *fi)
1031 /* There was a problem installing this route to the offload
1032 * device. For now, until we come up with more refined
1033 * policy handling, abruptly end IPv4 fib offloading for
1034 * for entire net by flushing offload device(s) of all
1035 * IPv4 routes, and mark IPv4 fib offloading broken from
1036 * this point forward.
1039 fib_flush_external(fi->fib_net);
1040 fi->fib_net->ipv4.fib_offload_disabled = true;
1042 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
1044 static bool switchdev_port_same_parent_id(struct net_device *a,
1045 struct net_device *b)
1047 struct switchdev_attr a_attr = {
1048 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1049 .flags = SWITCHDEV_F_NO_RECURSE,
1051 struct switchdev_attr b_attr = {
1052 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1053 .flags = SWITCHDEV_F_NO_RECURSE,
1056 if (switchdev_port_attr_get(a, &a_attr) ||
1057 switchdev_port_attr_get(b, &b_attr))
1060 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1063 static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1064 struct net_device *group_dev)
1066 struct net_device *lower_dev;
1067 struct list_head *iter;
1069 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1070 if (lower_dev == dev)
1072 if (switchdev_port_same_parent_id(dev, lower_dev))
1073 return lower_dev->offload_fwd_mark;
1074 return switchdev_port_fwd_mark_get(dev, lower_dev);
1077 return dev->ifindex;
1080 static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1081 u32 old_mark, u32 *reset_mark)
1083 struct net_device *lower_dev;
1084 struct list_head *iter;
1086 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1087 if (lower_dev->offload_fwd_mark == old_mark) {
1089 *reset_mark = lower_dev->ifindex;
1090 lower_dev->offload_fwd_mark = *reset_mark;
1092 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1097 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1100 * @group_dev: containing device
1101 * @joining: true if dev is joining group; false if leaving group
1103 * An ungrouped port's offload mark is just its ifindex. A grouped
1104 * port's (member of a bridge, for example) offload mark is the ifindex
1105 * of one of the ports in the group with the same parent (switch) ID.
1106 * Ports on the same device in the same group will have the same mark.
1111 * sw1p1 ifindex=2 mark=2
1112 * sw1p2 ifindex=3 mark=2
1113 * sw2p1 ifindex=4 mark=5
1114 * sw2p2 ifindex=5 mark=5
1116 * If sw2p2 leaves the bridge, we'll have:
1119 * sw1p1 ifindex=2 mark=2
1120 * sw1p2 ifindex=3 mark=2
1121 * sw2p1 ifindex=4 mark=4
1122 * sw2p2 ifindex=5 mark=5
1124 void switchdev_port_fwd_mark_set(struct net_device *dev,
1125 struct net_device *group_dev,
1128 u32 mark = dev->ifindex;
1131 if (group_dev && joining) {
1132 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1133 } else if (group_dev && !joining) {
1134 if (dev->offload_fwd_mark == mark)
1135 /* Ohoh, this port was the mark reference port,
1136 * but it's leaving the group, so reset the
1137 * mark for the remaining ports in the group.
1139 switchdev_port_fwd_mark_reset(group_dev, mark,
1143 dev->offload_fwd_mark = mark;
1145 EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);