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 * netdev_switch_parent_id_get - Get ID of a switch
26 * Get ID of a switch this port is part of.
28 int netdev_switch_parent_id_get(struct net_device *dev,
29 struct netdev_phys_item_id *psid)
31 const struct swdev_ops *ops = dev->swdev_ops;
33 if (!ops || !ops->swdev_parent_id_get)
35 return ops->swdev_parent_id_get(dev, psid);
37 EXPORT_SYMBOL_GPL(netdev_switch_parent_id_get);
40 * netdev_switch_port_stp_update - Notify switch device port of STP
43 * @state: port STP state
45 * Notify switch device port of bridge port STP state change.
47 int netdev_switch_port_stp_update(struct net_device *dev, u8 state)
49 const struct swdev_ops *ops = dev->swdev_ops;
51 if (!ops || !ops->swdev_port_stp_update)
53 WARN_ON(!ops->swdev_parent_id_get);
54 return ops->swdev_port_stp_update(dev, state);
56 EXPORT_SYMBOL_GPL(netdev_switch_port_stp_update);
58 static DEFINE_MUTEX(netdev_switch_mutex);
59 static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain);
62 * register_netdev_switch_notifier - Register notifier
65 * Register switch device notifier. This should be used by code
66 * which needs to monitor events happening in particular device.
67 * Return values are same as for atomic_notifier_chain_register().
69 int register_netdev_switch_notifier(struct notifier_block *nb)
73 mutex_lock(&netdev_switch_mutex);
74 err = raw_notifier_chain_register(&netdev_switch_notif_chain, nb);
75 mutex_unlock(&netdev_switch_mutex);
78 EXPORT_SYMBOL_GPL(register_netdev_switch_notifier);
81 * unregister_netdev_switch_notifier - Unregister notifier
84 * Unregister switch device notifier.
85 * Return values are same as for atomic_notifier_chain_unregister().
87 int unregister_netdev_switch_notifier(struct notifier_block *nb)
91 mutex_lock(&netdev_switch_mutex);
92 err = raw_notifier_chain_unregister(&netdev_switch_notif_chain, nb);
93 mutex_unlock(&netdev_switch_mutex);
96 EXPORT_SYMBOL_GPL(unregister_netdev_switch_notifier);
99 * call_netdev_switch_notifiers - Call notifiers
100 * @val: value passed unmodified to notifier function
102 * @info: notifier information data
104 * Call all network notifier blocks. This should be called by driver
105 * when it needs to propagate hardware event.
106 * Return values are same as for atomic_notifier_call_chain().
108 int call_netdev_switch_notifiers(unsigned long val, struct net_device *dev,
109 struct netdev_switch_notifier_info *info)
114 mutex_lock(&netdev_switch_mutex);
115 err = raw_notifier_call_chain(&netdev_switch_notif_chain, val, info);
116 mutex_unlock(&netdev_switch_mutex);
119 EXPORT_SYMBOL_GPL(call_netdev_switch_notifiers);
122 * netdev_switch_port_bridge_setlink - Notify switch device port of bridge
126 * @nlh: netlink msg with bridge port attributes
127 * @flags: bridge setlink flags
129 * Notify switch device port of bridge port attributes
131 int netdev_switch_port_bridge_setlink(struct net_device *dev,
132 struct nlmsghdr *nlh, u16 flags)
134 const struct net_device_ops *ops = dev->netdev_ops;
136 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
139 if (!ops->ndo_bridge_setlink)
142 return ops->ndo_bridge_setlink(dev, nlh, flags);
144 EXPORT_SYMBOL_GPL(netdev_switch_port_bridge_setlink);
147 * netdev_switch_port_bridge_dellink - Notify switch device port of bridge
148 * port attribute delete
151 * @nlh: netlink msg with bridge port attributes
152 * @flags: bridge setlink flags
154 * Notify switch device port of bridge port attribute delete
156 int netdev_switch_port_bridge_dellink(struct net_device *dev,
157 struct nlmsghdr *nlh, u16 flags)
159 const struct net_device_ops *ops = dev->netdev_ops;
161 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
164 if (!ops->ndo_bridge_dellink)
167 return ops->ndo_bridge_dellink(dev, nlh, flags);
169 EXPORT_SYMBOL_GPL(netdev_switch_port_bridge_dellink);
172 * ndo_dflt_netdev_switch_port_bridge_setlink - default ndo bridge setlink
173 * op for master devices
176 * @nlh: netlink msg with bridge port attributes
177 * @flags: bridge setlink flags
179 * Notify master device slaves of bridge port attributes
181 int ndo_dflt_netdev_switch_port_bridge_setlink(struct net_device *dev,
182 struct nlmsghdr *nlh, u16 flags)
184 struct net_device *lower_dev;
185 struct list_head *iter;
186 int ret = 0, err = 0;
188 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
191 netdev_for_each_lower_dev(dev, lower_dev, iter) {
192 err = netdev_switch_port_bridge_setlink(lower_dev, nlh, flags);
193 if (err && err != -EOPNOTSUPP)
199 EXPORT_SYMBOL_GPL(ndo_dflt_netdev_switch_port_bridge_setlink);
202 * ndo_dflt_netdev_switch_port_bridge_dellink - default ndo bridge dellink
203 * op for master devices
206 * @nlh: netlink msg with bridge port attributes
207 * @flags: bridge dellink flags
209 * Notify master device slaves of bridge port attribute deletes
211 int ndo_dflt_netdev_switch_port_bridge_dellink(struct net_device *dev,
212 struct nlmsghdr *nlh, u16 flags)
214 struct net_device *lower_dev;
215 struct list_head *iter;
216 int ret = 0, err = 0;
218 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
221 netdev_for_each_lower_dev(dev, lower_dev, iter) {
222 err = netdev_switch_port_bridge_dellink(lower_dev, nlh, flags);
223 if (err && err != -EOPNOTSUPP)
229 EXPORT_SYMBOL_GPL(ndo_dflt_netdev_switch_port_bridge_dellink);
231 static struct net_device *netdev_switch_get_lowest_dev(struct net_device *dev)
233 const struct swdev_ops *ops = dev->swdev_ops;
234 struct net_device *lower_dev;
235 struct net_device *port_dev;
236 struct list_head *iter;
238 /* Recusively search down until we find a sw port dev.
239 * (A sw port dev supports swdev_parent_id_get).
242 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
243 ops && ops->swdev_parent_id_get)
246 netdev_for_each_lower_dev(dev, lower_dev, iter) {
247 port_dev = netdev_switch_get_lowest_dev(lower_dev);
255 static struct net_device *netdev_switch_get_dev_by_nhs(struct fib_info *fi)
257 struct netdev_phys_item_id psid;
258 struct netdev_phys_item_id prev_psid;
259 struct net_device *dev = NULL;
262 /* For this route, all nexthop devs must be on the same switch. */
264 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
265 const struct fib_nh *nh = &fi->fib_nh[nhsel];
270 dev = netdev_switch_get_lowest_dev(nh->nh_dev);
274 if (netdev_switch_parent_id_get(dev, &psid))
278 if (prev_psid.id_len != psid.id_len)
280 if (memcmp(prev_psid.id, psid.id, psid.id_len))
291 * netdev_switch_fib_ipv4_add - Add IPv4 route entry to switch
293 * @dst: route's IPv4 destination address
294 * @dst_len: destination address length (prefix length)
295 * @fi: route FIB info structure
298 * @nlflags: netlink flags passed in (NLM_F_*)
299 * @tb_id: route table ID
301 * Add IPv4 route entry to switch device.
303 int netdev_switch_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
304 u8 tos, u8 type, u32 nlflags, u32 tb_id)
306 struct net_device *dev;
307 const struct swdev_ops *ops;
310 /* Don't offload route if using custom ip rules or if
311 * IPv4 FIB offloading has been disabled completely.
314 #ifdef CONFIG_IP_MULTIPLE_TABLES
315 if (fi->fib_net->ipv4.fib_has_custom_rules)
319 if (fi->fib_net->ipv4.fib_offload_disabled)
322 dev = netdev_switch_get_dev_by_nhs(fi);
325 ops = dev->swdev_ops;
327 if (ops->swdev_fib_ipv4_add) {
328 err = ops->swdev_fib_ipv4_add(dev, htonl(dst), dst_len,
329 fi, tos, type, nlflags,
332 fi->fib_flags |= RTNH_F_EXTERNAL;
337 EXPORT_SYMBOL_GPL(netdev_switch_fib_ipv4_add);
340 * netdev_switch_fib_ipv4_del - Delete IPv4 route entry from switch
342 * @dst: route's IPv4 destination address
343 * @dst_len: destination address length (prefix length)
344 * @fi: route FIB info structure
347 * @tb_id: route table ID
349 * Delete IPv4 route entry from switch device.
351 int netdev_switch_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
352 u8 tos, u8 type, u32 tb_id)
354 struct net_device *dev;
355 const struct swdev_ops *ops;
358 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
361 dev = netdev_switch_get_dev_by_nhs(fi);
364 ops = dev->swdev_ops;
366 if (ops->swdev_fib_ipv4_del) {
367 err = ops->swdev_fib_ipv4_del(dev, htonl(dst), dst_len,
368 fi, tos, type, tb_id);
370 fi->fib_flags &= ~RTNH_F_EXTERNAL;
375 EXPORT_SYMBOL_GPL(netdev_switch_fib_ipv4_del);
378 * netdev_switch_fib_ipv4_abort - Abort an IPv4 FIB operation
380 * @fi: route FIB info structure
382 void netdev_switch_fib_ipv4_abort(struct fib_info *fi)
384 /* There was a problem installing this route to the offload
385 * device. For now, until we come up with more refined
386 * policy handling, abruptly end IPv4 fib offloading for
387 * for entire net by flushing offload device(s) of all
388 * IPv4 routes, and mark IPv4 fib offloading broken from
389 * this point forward.
392 fib_flush_external(fi->fib_net);
393 fi->fib_net->ipv4.fib_offload_disabled = true;
395 EXPORT_SYMBOL_GPL(netdev_switch_fib_ipv4_abort);