blob: b7f44a23def55209b2966e9bf492213decfca1d9 [file] [log] [blame]
Jiri Pirko007f7902014-11-28 14:34:17 +01001/*
2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
Scott Feldmanf8f21472015-03-09 13:59:09 -07004 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
Jiri Pirko007f7902014-11-28 14:34:17 +01005 *
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.
10 */
11
12#include <linux/kernel.h>
13#include <linux/types.h>
14#include <linux/init.h>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010015#include <linux/mutex.h>
16#include <linux/notifier.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010017#include <linux/netdevice.h>
Scott Feldman5e8d9042015-03-05 21:21:15 -080018#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010019#include <net/switchdev.h>
20
21/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -070022 * switchdev_parent_id_get - Get ID of a switch
Jiri Pirko007f7902014-11-28 14:34:17 +010023 * @dev: port device
24 * @psid: switch ID
25 *
26 * Get ID of a switch this port is part of.
27 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -070028int switchdev_parent_id_get(struct net_device *dev,
29 struct netdev_phys_item_id *psid)
Jiri Pirko007f7902014-11-28 14:34:17 +010030{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -070031 const struct switchdev_ops *ops = dev->switchdev_ops;
Jiri Pirko007f7902014-11-28 14:34:17 +010032
Jiri Pirko9d47c0a2015-05-10 09:47:47 -070033 if (!ops || !ops->switchdev_parent_id_get)
Jiri Pirko007f7902014-11-28 14:34:17 +010034 return -EOPNOTSUPP;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -070035 return ops->switchdev_parent_id_get(dev, psid);
Jiri Pirko007f7902014-11-28 14:34:17 +010036}
Jiri Pirkoebb9a032015-05-10 09:47:46 -070037EXPORT_SYMBOL_GPL(switchdev_parent_id_get);
Scott Feldman38dcf352014-11-28 14:34:20 +010038
39/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -070040 * switchdev_port_stp_update - Notify switch device port of STP
Scott Feldman38dcf352014-11-28 14:34:20 +010041 * state change
42 * @dev: port device
43 * @state: port STP state
44 *
45 * Notify switch device port of bridge port STP state change.
46 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -070047int switchdev_port_stp_update(struct net_device *dev, u8 state)
Scott Feldman38dcf352014-11-28 14:34:20 +010048{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -070049 const struct switchdev_ops *ops = dev->switchdev_ops;
Roopa Prabhu558d51f2015-03-21 10:27:28 -070050 struct net_device *lower_dev;
51 struct list_head *iter;
52 int err = -EOPNOTSUPP;
Scott Feldman38dcf352014-11-28 14:34:20 +010053
Jiri Pirko9d47c0a2015-05-10 09:47:47 -070054 if (ops && ops->switchdev_port_stp_update)
55 return ops->switchdev_port_stp_update(dev, state);
Roopa Prabhu558d51f2015-03-21 10:27:28 -070056
57 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -070058 err = switchdev_port_stp_update(lower_dev, state);
Roopa Prabhu558d51f2015-03-21 10:27:28 -070059 if (err && err != -EOPNOTSUPP)
60 return err;
61 }
62
63 return err;
Scott Feldman38dcf352014-11-28 14:34:20 +010064}
Jiri Pirkoebb9a032015-05-10 09:47:46 -070065EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
Jiri Pirko03bf0c22015-01-15 23:49:36 +010066
Jiri Pirkoebb9a032015-05-10 09:47:46 -070067static DEFINE_MUTEX(switchdev_mutex);
68static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +010069
70/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -070071 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +010072 * @nb: notifier_block
73 *
74 * Register switch device notifier. This should be used by code
75 * which needs to monitor events happening in particular device.
76 * Return values are same as for atomic_notifier_chain_register().
77 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -070078int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +010079{
80 int err;
81
Jiri Pirkoebb9a032015-05-10 09:47:46 -070082 mutex_lock(&switchdev_mutex);
83 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
84 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +010085 return err;
86}
Jiri Pirkoebb9a032015-05-10 09:47:46 -070087EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +010088
89/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -070090 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +010091 * @nb: notifier_block
92 *
93 * Unregister switch device notifier.
94 * Return values are same as for atomic_notifier_chain_unregister().
95 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -070096int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +010097{
98 int err;
99
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700100 mutex_lock(&switchdev_mutex);
101 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
102 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100103 return err;
104}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700105EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100106
107/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700108 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100109 * @val: value passed unmodified to notifier function
110 * @dev: port device
111 * @info: notifier information data
112 *
113 * Call all network notifier blocks. This should be called by driver
114 * when it needs to propagate hardware event.
115 * Return values are same as for atomic_notifier_call_chain().
116 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700117int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
118 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100119{
120 int err;
121
122 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700123 mutex_lock(&switchdev_mutex);
124 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
125 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100126 return err;
127}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700128EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800129
130/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700131 * switchdev_port_bridge_setlink - Notify switch device port of bridge
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800132 * port attributes
133 *
134 * @dev: port device
135 * @nlh: netlink msg with bridge port attributes
136 * @flags: bridge setlink flags
137 *
138 * Notify switch device port of bridge port attributes
139 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700140int switchdev_port_bridge_setlink(struct net_device *dev,
141 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800142{
143 const struct net_device_ops *ops = dev->netdev_ops;
144
145 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
146 return 0;
147
148 if (!ops->ndo_bridge_setlink)
149 return -EOPNOTSUPP;
150
151 return ops->ndo_bridge_setlink(dev, nlh, flags);
152}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700153EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800154
155/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700156 * switchdev_port_bridge_dellink - Notify switch device port of bridge
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800157 * port attribute delete
158 *
159 * @dev: port device
160 * @nlh: netlink msg with bridge port attributes
161 * @flags: bridge setlink flags
162 *
163 * Notify switch device port of bridge port attribute delete
164 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700165int switchdev_port_bridge_dellink(struct net_device *dev,
166 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800167{
168 const struct net_device_ops *ops = dev->netdev_ops;
169
170 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
171 return 0;
172
173 if (!ops->ndo_bridge_dellink)
174 return -EOPNOTSUPP;
175
176 return ops->ndo_bridge_dellink(dev, nlh, flags);
177}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700178EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800179
180/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700181 * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
182 * op for master devices
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800183 *
184 * @dev: port device
185 * @nlh: netlink msg with bridge port attributes
186 * @flags: bridge setlink flags
187 *
188 * Notify master device slaves of bridge port attributes
189 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700190int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
191 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800192{
193 struct net_device *lower_dev;
194 struct list_head *iter;
195 int ret = 0, err = 0;
196
197 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
198 return ret;
199
200 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700201 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800202 if (err && err != -EOPNOTSUPP)
203 ret = err;
204 }
205
206 return ret;
207}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700208EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800209
210/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700211 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
212 * op for master devices
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800213 *
214 * @dev: port device
215 * @nlh: netlink msg with bridge port attributes
216 * @flags: bridge dellink flags
217 *
218 * Notify master device slaves of bridge port attribute deletes
219 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700220int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
221 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800222{
223 struct net_device *lower_dev;
224 struct list_head *iter;
225 int ret = 0, err = 0;
226
227 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
228 return ret;
229
230 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700231 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800232 if (err && err != -EOPNOTSUPP)
233 ret = err;
234 }
235
236 return ret;
237}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700238EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800239
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700240static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800241{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700242 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800243 struct net_device *lower_dev;
244 struct net_device *port_dev;
245 struct list_head *iter;
246
247 /* Recusively search down until we find a sw port dev.
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700248 * (A sw port dev supports switchdev_parent_id_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800249 */
250
251 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700252 ops && ops->switchdev_parent_id_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800253 return dev;
254
255 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700256 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800257 if (port_dev)
258 return port_dev;
259 }
260
261 return NULL;
262}
263
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700264static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800265{
266 struct netdev_phys_item_id psid;
267 struct netdev_phys_item_id prev_psid;
268 struct net_device *dev = NULL;
269 int nhsel;
270
271 /* For this route, all nexthop devs must be on the same switch. */
272
273 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
274 const struct fib_nh *nh = &fi->fib_nh[nhsel];
275
276 if (!nh->nh_dev)
277 return NULL;
278
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700279 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800280 if (!dev)
281 return NULL;
282
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700283 if (switchdev_parent_id_get(dev, &psid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800284 return NULL;
285
286 if (nhsel > 0) {
287 if (prev_psid.id_len != psid.id_len)
288 return NULL;
289 if (memcmp(prev_psid.id, psid.id, psid.id_len))
290 return NULL;
291 }
292
293 prev_psid = psid;
294 }
295
296 return dev;
297}
298
Scott Feldman5e8d9042015-03-05 21:21:15 -0800299/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700300 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800301 *
302 * @dst: route's IPv4 destination address
303 * @dst_len: destination address length (prefix length)
304 * @fi: route FIB info structure
305 * @tos: route TOS
306 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -0700307 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800308 * @tb_id: route table ID
309 *
310 * Add IPv4 route entry to switch device.
311 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700312int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
313 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800314{
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800315 struct net_device *dev;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700316 const struct switchdev_ops *ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800317 int err = 0;
318
Scott Feldman8e05fd72015-03-05 21:21:19 -0800319 /* Don't offload route if using custom ip rules or if
320 * IPv4 FIB offloading has been disabled completely.
321 */
322
Scott Feldmane1315db2015-03-06 01:14:36 -0800323#ifdef CONFIG_IP_MULTIPLE_TABLES
324 if (fi->fib_net->ipv4.fib_has_custom_rules)
325 return 0;
326#endif
327
328 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -0800329 return 0;
330
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700331 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800332 if (!dev)
333 return 0;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700334 ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800335
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700336 if (ops->switchdev_fib_ipv4_add) {
337 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
338 fi, tos, type, nlflags,
339 tb_id);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800340 if (!err)
341 fi->fib_flags |= RTNH_F_EXTERNAL;
342 }
343
344 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800345}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700346EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800347
348/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700349 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800350 *
351 * @dst: route's IPv4 destination address
352 * @dst_len: destination address length (prefix length)
353 * @fi: route FIB info structure
354 * @tos: route TOS
355 * @type: route type
356 * @tb_id: route table ID
357 *
358 * Delete IPv4 route entry from switch device.
359 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700360int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
361 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800362{
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800363 struct net_device *dev;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700364 const struct switchdev_ops *ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800365 int err = 0;
366
367 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
368 return 0;
369
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700370 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800371 if (!dev)
372 return 0;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700373 ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800374
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700375 if (ops->switchdev_fib_ipv4_del) {
376 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
377 fi, tos, type, tb_id);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800378 if (!err)
379 fi->fib_flags &= ~RTNH_F_EXTERNAL;
380 }
381
382 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800383}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700384EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -0800385
386/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700387 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -0800388 *
389 * @fi: route FIB info structure
390 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700391void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -0800392{
393 /* There was a problem installing this route to the offload
394 * device. For now, until we come up with more refined
395 * policy handling, abruptly end IPv4 fib offloading for
396 * for entire net by flushing offload device(s) of all
397 * IPv4 routes, and mark IPv4 fib offloading broken from
398 * this point forward.
399 */
400
401 fib_flush_external(fi->fib_net);
402 fi->fib_net->ipv4.fib_offload_disabled = true;
403}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700404EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);