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);
106 if (err && err != -EOPNOTSUPP)
107 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
115 static int switchdev_port_attr_set_defer(struct net_device *dev,
116 struct switchdev_attr *attr)
118 struct switchdev_attr_set_work *asw;
120 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
124 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
128 memcpy(&asw->attr, attr, sizeof(asw->attr));
130 schedule_work(&asw->work);
136 * switchdev_port_attr_set - Set port attribute
139 * @attr: attribute to set
141 * Use a 2-phase prepare-commit transaction model to ensure
142 * system is not left in a partially updated state due to
143 * failure from driver/device.
145 int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
149 if (!rtnl_is_locked()) {
150 /* Running prepare-commit transaction across stacked
151 * devices requires nothing moves, so if rtnl_lock is
152 * not held, schedule a worker thread to hold rtnl_lock
153 * while setting attr.
156 return switchdev_port_attr_set_defer(dev, attr);
159 /* Phase I: prepare for attr set. Driver/device should fail
160 * here if there are going to be issues in the commit phase,
161 * such as lack of resources or support. The driver/device
162 * should reserve resources needed for the commit phase here,
163 * but should not commit the attr.
166 attr->trans = SWITCHDEV_TRANS_PREPARE;
167 err = __switchdev_port_attr_set(dev, attr);
169 /* Prepare phase failed: abort the transaction. Any
170 * resources reserved in the prepare phase are
174 attr->trans = SWITCHDEV_TRANS_ABORT;
175 __switchdev_port_attr_set(dev, attr);
180 /* Phase II: commit attr set. This cannot fail as a fault
181 * of driver/device. If it does, it's a bug in the driver/device
182 * because the driver said everythings was OK in phase I.
185 attr->trans = SWITCHDEV_TRANS_COMMIT;
186 err = __switchdev_port_attr_set(dev, attr);
187 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
188 dev->name, attr->id);
192 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
194 static int __switchdev_port_obj_add(struct net_device *dev,
195 struct switchdev_obj *obj)
197 const struct switchdev_ops *ops = dev->switchdev_ops;
198 struct net_device *lower_dev;
199 struct list_head *iter;
200 int err = -EOPNOTSUPP;
202 if (ops && ops->switchdev_port_obj_add)
203 return ops->switchdev_port_obj_add(dev, obj);
205 /* Switch device port(s) may be stacked under
206 * bond/team/vlan dev, so recurse down to add object on
210 netdev_for_each_lower_dev(dev, lower_dev, iter) {
211 err = __switchdev_port_obj_add(lower_dev, obj);
220 * switchdev_port_obj_add - Add port object
223 * @obj: object to add
225 * Use a 2-phase prepare-commit transaction model to ensure
226 * system is not left in a partially updated state due to
227 * failure from driver/device.
229 * rtnl_lock must be held.
231 int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
237 /* Phase I: prepare for obj add. Driver/device should fail
238 * here if there are going to be issues in the commit phase,
239 * such as lack of resources or support. The driver/device
240 * should reserve resources needed for the commit phase here,
241 * but should not commit the obj.
244 obj->trans = SWITCHDEV_TRANS_PREPARE;
245 err = __switchdev_port_obj_add(dev, obj);
247 /* Prepare phase failed: abort the transaction. Any
248 * resources reserved in the prepare phase are
252 obj->trans = SWITCHDEV_TRANS_ABORT;
253 __switchdev_port_obj_add(dev, obj);
258 /* Phase II: commit obj add. This cannot fail as a fault
259 * of driver/device. If it does, it's a bug in the driver/device
260 * because the driver said everythings was OK in phase I.
263 obj->trans = SWITCHDEV_TRANS_COMMIT;
264 err = __switchdev_port_obj_add(dev, obj);
265 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
269 EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
272 * switchdev_port_obj_del - Delete port object
275 * @obj: object to delete
277 int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
279 const struct switchdev_ops *ops = dev->switchdev_ops;
280 struct net_device *lower_dev;
281 struct list_head *iter;
282 int err = -EOPNOTSUPP;
284 if (ops && ops->switchdev_port_obj_del)
285 return ops->switchdev_port_obj_del(dev, obj);
287 /* Switch device port(s) may be stacked under
288 * bond/team/vlan dev, so recurse down to delete object on
292 netdev_for_each_lower_dev(dev, lower_dev, iter) {
293 err = switchdev_port_obj_del(lower_dev, obj);
300 EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
303 * switchdev_port_obj_dump - Dump port objects
306 * @obj: object to dump
308 int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj)
310 const struct switchdev_ops *ops = dev->switchdev_ops;
311 struct net_device *lower_dev;
312 struct list_head *iter;
313 int err = -EOPNOTSUPP;
315 if (ops && ops->switchdev_port_obj_dump)
316 return ops->switchdev_port_obj_dump(dev, obj);
318 /* Switch device port(s) may be stacked under
319 * bond/team/vlan dev, so recurse down to dump objects on
320 * first port at bottom of stack.
323 netdev_for_each_lower_dev(dev, lower_dev, iter) {
324 err = switchdev_port_obj_dump(lower_dev, obj);
330 EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
332 static DEFINE_MUTEX(switchdev_mutex);
333 static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
336 * register_switchdev_notifier - Register notifier
337 * @nb: notifier_block
339 * Register switch device notifier. This should be used by code
340 * which needs to monitor events happening in particular device.
341 * Return values are same as for atomic_notifier_chain_register().
343 int register_switchdev_notifier(struct notifier_block *nb)
347 mutex_lock(&switchdev_mutex);
348 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
349 mutex_unlock(&switchdev_mutex);
352 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
355 * unregister_switchdev_notifier - Unregister notifier
356 * @nb: notifier_block
358 * Unregister switch device notifier.
359 * Return values are same as for atomic_notifier_chain_unregister().
361 int unregister_switchdev_notifier(struct notifier_block *nb)
365 mutex_lock(&switchdev_mutex);
366 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
367 mutex_unlock(&switchdev_mutex);
370 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
373 * call_switchdev_notifiers - Call notifiers
374 * @val: value passed unmodified to notifier function
376 * @info: notifier information data
378 * Call all network notifier blocks. This should be called by driver
379 * when it needs to propagate hardware event.
380 * Return values are same as for atomic_notifier_call_chain().
382 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
383 struct switchdev_notifier_info *info)
388 mutex_lock(&switchdev_mutex);
389 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
390 mutex_unlock(&switchdev_mutex);
393 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
395 struct switchdev_vlan_dump {
396 struct switchdev_obj obj;
404 static int switchdev_port_vlan_dump_put(struct net_device *dev,
405 struct switchdev_vlan_dump *dump)
407 struct bridge_vlan_info vinfo;
409 vinfo.flags = dump->flags;
411 if (dump->begin == 0 && dump->end == 0) {
413 } else if (dump->begin == dump->end) {
414 vinfo.vid = dump->begin;
415 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
416 sizeof(vinfo), &vinfo))
419 vinfo.vid = dump->begin;
420 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
421 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
422 sizeof(vinfo), &vinfo))
424 vinfo.vid = dump->end;
425 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
426 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
427 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
428 sizeof(vinfo), &vinfo))
435 static int switchdev_port_vlan_dump_cb(struct net_device *dev,
436 struct switchdev_obj *obj)
438 struct switchdev_vlan_dump *dump =
439 container_of(obj, struct switchdev_vlan_dump, obj);
440 struct switchdev_obj_vlan *vlan = &dump->obj.u.vlan;
443 if (vlan->vid_begin > vlan->vid_end)
446 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
447 dump->flags = vlan->flags;
448 for (dump->begin = dump->end = vlan->vid_begin;
449 dump->begin <= vlan->vid_end;
450 dump->begin++, dump->end++) {
451 err = switchdev_port_vlan_dump_put(dev, dump);
455 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
456 if (dump->begin > vlan->vid_begin &&
457 dump->begin >= vlan->vid_end) {
458 if ((dump->begin - 1) == vlan->vid_end &&
459 dump->flags == vlan->flags) {
461 dump->begin = vlan->vid_begin;
463 err = switchdev_port_vlan_dump_put(dev, dump);
464 dump->flags = vlan->flags;
465 dump->begin = vlan->vid_begin;
466 dump->end = vlan->vid_end;
468 } else if (dump->end <= vlan->vid_begin &&
469 dump->end < vlan->vid_end) {
470 if ((dump->end + 1) == vlan->vid_begin &&
471 dump->flags == vlan->flags) {
473 dump->end = vlan->vid_end;
475 err = switchdev_port_vlan_dump_put(dev, dump);
476 dump->flags = vlan->flags;
477 dump->begin = vlan->vid_begin;
478 dump->end = vlan->vid_end;
488 static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
491 struct switchdev_vlan_dump dump = {
493 .id = SWITCHDEV_OBJ_PORT_VLAN,
494 .cb = switchdev_port_vlan_dump_cb,
497 .filter_mask = filter_mask,
501 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
502 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
503 err = switchdev_port_obj_dump(dev, &dump.obj);
506 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
508 err = switchdev_port_vlan_dump_put(dev, &dump);
512 return err == -EOPNOTSUPP ? 0 : err;
516 * switchdev_port_bridge_getlink - Get bridge port attributes
520 * Called for SELF on rtnl_bridge_getlink to get bridge port
523 int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
524 struct net_device *dev, u32 filter_mask,
527 struct switchdev_attr attr = {
528 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
530 u16 mode = BRIDGE_MODE_UNDEF;
531 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
534 err = switchdev_port_attr_get(dev, &attr);
535 if (err && err != -EOPNOTSUPP)
538 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
539 attr.u.brport_flags, mask, nlflags,
540 filter_mask, switchdev_port_vlan_fill);
542 EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
544 static int switchdev_port_br_setflag(struct net_device *dev,
545 struct nlattr *nlattr,
546 unsigned long brport_flag)
548 struct switchdev_attr attr = {
549 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
551 u8 flag = nla_get_u8(nlattr);
554 err = switchdev_port_attr_get(dev, &attr);
559 attr.u.brport_flags |= brport_flag;
561 attr.u.brport_flags &= ~brport_flag;
563 return switchdev_port_attr_set(dev, &attr);
566 static const struct nla_policy
567 switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
568 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
569 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
570 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
571 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
572 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
573 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
574 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
575 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
576 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
577 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
580 static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
581 struct nlattr *protinfo)
587 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
588 switchdev_port_bridge_policy);
592 nla_for_each_nested(attr, protinfo, rem) {
593 switch (nla_type(attr)) {
594 case IFLA_BRPORT_LEARNING:
595 err = switchdev_port_br_setflag(dev, attr,
598 case IFLA_BRPORT_LEARNING_SYNC:
599 err = switchdev_port_br_setflag(dev, attr,
613 static int switchdev_port_br_afspec(struct net_device *dev,
614 struct nlattr *afspec,
615 int (*f)(struct net_device *dev,
616 struct switchdev_obj *obj))
619 struct bridge_vlan_info *vinfo;
620 struct switchdev_obj obj = {
621 .id = SWITCHDEV_OBJ_PORT_VLAN,
623 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
627 nla_for_each_nested(attr, afspec, rem) {
628 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
630 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
632 vinfo = nla_data(attr);
633 vlan->flags = vinfo->flags;
634 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
637 vlan->vid_begin = vinfo->vid;
638 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
639 if (!vlan->vid_begin)
641 vlan->vid_end = vinfo->vid;
642 if (vlan->vid_end <= vlan->vid_begin)
647 memset(vlan, 0, sizeof(*vlan));
651 vlan->vid_begin = vinfo->vid;
652 vlan->vid_end = vinfo->vid;
656 memset(vlan, 0, sizeof(*vlan));
664 * switchdev_port_bridge_setlink - Set bridge port attributes
667 * @nlh: netlink header
668 * @flags: netlink flags
670 * Called for SELF on rtnl_bridge_setlink to set bridge port
673 int switchdev_port_bridge_setlink(struct net_device *dev,
674 struct nlmsghdr *nlh, u16 flags)
676 struct nlattr *protinfo;
677 struct nlattr *afspec;
680 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
683 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
688 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
691 err = switchdev_port_br_afspec(dev, afspec,
692 switchdev_port_obj_add);
696 EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
699 * switchdev_port_bridge_dellink - Set bridge port attributes
702 * @nlh: netlink header
703 * @flags: netlink flags
705 * Called for SELF on rtnl_bridge_dellink to set bridge port
708 int switchdev_port_bridge_dellink(struct net_device *dev,
709 struct nlmsghdr *nlh, u16 flags)
711 struct nlattr *afspec;
713 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
716 return switchdev_port_br_afspec(dev, afspec,
717 switchdev_port_obj_del);
721 EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
724 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
726 * @ndmsg: netlink hdr
727 * @nlattr: netlink attributes
729 * @addr: MAC address to add
732 * Add FDB entry to switch device.
734 int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
735 struct net_device *dev, const unsigned char *addr,
736 u16 vid, u16 nlm_flags)
738 struct switchdev_obj obj = {
739 .id = SWITCHDEV_OBJ_PORT_FDB,
746 return switchdev_port_obj_add(dev, &obj);
748 EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
751 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
753 * @ndmsg: netlink hdr
754 * @nlattr: netlink attributes
756 * @addr: MAC address to delete
757 * @vid: VLAN to delete
759 * Delete FDB entry from switch device.
761 int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
762 struct net_device *dev, const unsigned char *addr,
765 struct switchdev_obj obj = {
766 .id = SWITCHDEV_OBJ_PORT_FDB,
773 return switchdev_port_obj_del(dev, &obj);
775 EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
777 struct switchdev_fdb_dump {
778 struct switchdev_obj obj;
780 struct netlink_callback *cb;
784 static int switchdev_port_fdb_dump_cb(struct net_device *dev,
785 struct switchdev_obj *obj)
787 struct switchdev_fdb_dump *dump =
788 container_of(obj, struct switchdev_fdb_dump, obj);
789 u32 portid = NETLINK_CB(dump->cb->skb).portid;
790 u32 seq = dump->cb->nlh->nlmsg_seq;
791 struct nlmsghdr *nlh;
794 if (dump->idx < dump->cb->args[0])
797 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
798 sizeof(*ndm), NLM_F_MULTI);
802 ndm = nlmsg_data(nlh);
803 ndm->ndm_family = AF_BRIDGE;
806 ndm->ndm_flags = NTF_SELF;
808 ndm->ndm_ifindex = dev->ifindex;
809 ndm->ndm_state = NUD_REACHABLE;
811 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr))
812 goto nla_put_failure;
814 if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid))
815 goto nla_put_failure;
817 nlmsg_end(dump->skb, nlh);
824 nlmsg_cancel(dump->skb, nlh);
829 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
832 * @cb: netlink callback
834 * @filter_dev: filter device
837 * Delete FDB entry from switch device.
839 int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
840 struct net_device *dev,
841 struct net_device *filter_dev, int idx)
843 struct switchdev_fdb_dump dump = {
845 .id = SWITCHDEV_OBJ_PORT_FDB,
846 .cb = switchdev_port_fdb_dump_cb,
854 err = switchdev_port_obj_dump(dev, &dump.obj);
860 EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
862 static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
864 const struct switchdev_ops *ops = dev->switchdev_ops;
865 struct net_device *lower_dev;
866 struct net_device *port_dev;
867 struct list_head *iter;
869 /* Recusively search down until we find a sw port dev.
870 * (A sw port dev supports switchdev_port_attr_get).
873 if (ops && ops->switchdev_port_attr_get)
876 netdev_for_each_lower_dev(dev, lower_dev, iter) {
877 port_dev = switchdev_get_lowest_dev(lower_dev);
885 static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
887 struct switchdev_attr attr = {
888 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
890 struct switchdev_attr prev_attr;
891 struct net_device *dev = NULL;
894 /* For this route, all nexthop devs must be on the same switch. */
896 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
897 const struct fib_nh *nh = &fi->fib_nh[nhsel];
902 dev = switchdev_get_lowest_dev(nh->nh_dev);
906 if (switchdev_port_attr_get(dev, &attr))
910 if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
912 if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
924 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
926 * @dst: route's IPv4 destination address
927 * @dst_len: destination address length (prefix length)
928 * @fi: route FIB info structure
931 * @nlflags: netlink flags passed in (NLM_F_*)
932 * @tb_id: route table ID
934 * Add/modify switch IPv4 route entry.
936 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
937 u8 tos, u8 type, u32 nlflags, u32 tb_id)
939 struct switchdev_obj fib_obj = {
940 .id = SWITCHDEV_OBJ_IPV4_FIB,
951 struct net_device *dev;
954 /* Don't offload route if using custom ip rules or if
955 * IPv4 FIB offloading has been disabled completely.
958 #ifdef CONFIG_IP_MULTIPLE_TABLES
959 if (fi->fib_net->ipv4.fib_has_custom_rules)
963 if (fi->fib_net->ipv4.fib_offload_disabled)
966 dev = switchdev_get_dev_by_nhs(fi);
970 err = switchdev_port_obj_add(dev, &fib_obj);
972 fi->fib_flags |= RTNH_F_OFFLOAD;
974 return err == -EOPNOTSUPP ? 0 : err;
976 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
979 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
981 * @dst: route's IPv4 destination address
982 * @dst_len: destination address length (prefix length)
983 * @fi: route FIB info structure
986 * @tb_id: route table ID
988 * Delete IPv4 route entry from switch device.
990 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
991 u8 tos, u8 type, u32 tb_id)
993 struct switchdev_obj fib_obj = {
994 .id = SWITCHDEV_OBJ_IPV4_FIB,
1005 struct net_device *dev;
1008 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
1011 dev = switchdev_get_dev_by_nhs(fi);
1015 err = switchdev_port_obj_del(dev, &fib_obj);
1017 fi->fib_flags &= ~RTNH_F_OFFLOAD;
1019 return err == -EOPNOTSUPP ? 0 : err;
1021 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
1024 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
1026 * @fi: route FIB info structure
1028 void switchdev_fib_ipv4_abort(struct fib_info *fi)
1030 /* There was a problem installing this route to the offload
1031 * device. For now, until we come up with more refined
1032 * policy handling, abruptly end IPv4 fib offloading for
1033 * for entire net by flushing offload device(s) of all
1034 * IPv4 routes, and mark IPv4 fib offloading broken from
1035 * this point forward.
1038 fib_flush_external(fi->fib_net);
1039 fi->fib_net->ipv4.fib_offload_disabled = true;
1041 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);