switchdev: s/swdev_/switchdev_/
[deliverable/linux.git] / net / switchdev / switchdev.c
CommitLineData
007f790c
JP
1/*
2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
f8f21471 4 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
007f790c
JP
5 *
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>
03bf0c28
JP
15#include <linux/mutex.h>
16#include <linux/notifier.h>
007f790c 17#include <linux/netdevice.h>
5e8d9049 18#include <net/ip_fib.h>
007f790c
JP
19#include <net/switchdev.h>
20
21/**
ebb9a03a 22 * switchdev_parent_id_get - Get ID of a switch
007f790c
JP
23 * @dev: port device
24 * @psid: switch ID
25 *
26 * Get ID of a switch this port is part of.
27 */
ebb9a03a
JP
28int switchdev_parent_id_get(struct net_device *dev,
29 struct netdev_phys_item_id *psid)
007f790c 30{
9d47c0a2 31 const struct switchdev_ops *ops = dev->switchdev_ops;
007f790c 32
9d47c0a2 33 if (!ops || !ops->switchdev_parent_id_get)
007f790c 34 return -EOPNOTSUPP;
9d47c0a2 35 return ops->switchdev_parent_id_get(dev, psid);
007f790c 36}
ebb9a03a 37EXPORT_SYMBOL_GPL(switchdev_parent_id_get);
38dcf357
SF
38
39/**
ebb9a03a 40 * switchdev_port_stp_update - Notify switch device port of STP
38dcf357
SF
41 * state change
42 * @dev: port device
43 * @state: port STP state
44 *
45 * Notify switch device port of bridge port STP state change.
46 */
ebb9a03a 47int switchdev_port_stp_update(struct net_device *dev, u8 state)
38dcf357 48{
9d47c0a2 49 const struct switchdev_ops *ops = dev->switchdev_ops;
558d51fa
RP
50 struct net_device *lower_dev;
51 struct list_head *iter;
52 int err = -EOPNOTSUPP;
38dcf357 53
9d47c0a2
JP
54 if (ops && ops->switchdev_port_stp_update)
55 return ops->switchdev_port_stp_update(dev, state);
558d51fa
RP
56
57 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 58 err = switchdev_port_stp_update(lower_dev, state);
558d51fa
RP
59 if (err && err != -EOPNOTSUPP)
60 return err;
61 }
62
63 return err;
38dcf357 64}
ebb9a03a 65EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
03bf0c28 66
ebb9a03a
JP
67static DEFINE_MUTEX(switchdev_mutex);
68static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
03bf0c28
JP
69
70/**
ebb9a03a 71 * register_switchdev_notifier - Register notifier
03bf0c28
JP
72 * @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 */
ebb9a03a 78int register_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
79{
80 int err;
81
ebb9a03a
JP
82 mutex_lock(&switchdev_mutex);
83 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
84 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
85 return err;
86}
ebb9a03a 87EXPORT_SYMBOL_GPL(register_switchdev_notifier);
03bf0c28
JP
88
89/**
ebb9a03a 90 * unregister_switchdev_notifier - Unregister notifier
03bf0c28
JP
91 * @nb: notifier_block
92 *
93 * Unregister switch device notifier.
94 * Return values are same as for atomic_notifier_chain_unregister().
95 */
ebb9a03a 96int unregister_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
97{
98 int err;
99
ebb9a03a
JP
100 mutex_lock(&switchdev_mutex);
101 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
102 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
103 return err;
104}
ebb9a03a 105EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
03bf0c28
JP
106
107/**
ebb9a03a 108 * call_switchdev_notifiers - Call notifiers
03bf0c28
JP
109 * @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 */
ebb9a03a
JP
117int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
118 struct switchdev_notifier_info *info)
03bf0c28
JP
119{
120 int err;
121
122 info->dev = dev;
ebb9a03a
JP
123 mutex_lock(&switchdev_mutex);
124 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
125 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
126 return err;
127}
ebb9a03a 128EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
8a44dbb2
RP
129
130/**
ebb9a03a 131 * switchdev_port_bridge_setlink - Notify switch device port of bridge
8a44dbb2
RP
132 * 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 */
ebb9a03a
JP
140int switchdev_port_bridge_setlink(struct net_device *dev,
141 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
142{
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}
ebb9a03a 153EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
8a44dbb2
RP
154
155/**
ebb9a03a 156 * switchdev_port_bridge_dellink - Notify switch device port of bridge
8a44dbb2
RP
157 * 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 */
ebb9a03a
JP
165int switchdev_port_bridge_dellink(struct net_device *dev,
166 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
167{
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}
ebb9a03a 178EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
8a44dbb2
RP
179
180/**
ebb9a03a
JP
181 * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
182 * op for master devices
8a44dbb2
RP
183 *
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 */
ebb9a03a
JP
190int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
191 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
192{
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) {
ebb9a03a 201 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
8a44dbb2
RP
202 if (err && err != -EOPNOTSUPP)
203 ret = err;
204 }
205
206 return ret;
207}
ebb9a03a 208EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
8a44dbb2
RP
209
210/**
ebb9a03a
JP
211 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
212 * op for master devices
8a44dbb2
RP
213 *
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 */
ebb9a03a
JP
220int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
221 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
222{
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) {
ebb9a03a 231 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
8a44dbb2
RP
232 if (err && err != -EOPNOTSUPP)
233 ret = err;
234 }
235
236 return ret;
237}
ebb9a03a 238EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
5e8d9049 239
ebb9a03a 240static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
b5d6fbde 241{
9d47c0a2 242 const struct switchdev_ops *ops = dev->switchdev_ops;
b5d6fbde
SF
243 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.
9d47c0a2 248 * (A sw port dev supports switchdev_parent_id_get).
b5d6fbde
SF
249 */
250
251 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
9d47c0a2 252 ops && ops->switchdev_parent_id_get)
b5d6fbde
SF
253 return dev;
254
255 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 256 port_dev = switchdev_get_lowest_dev(lower_dev);
b5d6fbde
SF
257 if (port_dev)
258 return port_dev;
259 }
260
261 return NULL;
262}
263
ebb9a03a 264static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
b5d6fbde
SF
265{
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
ebb9a03a 279 dev = switchdev_get_lowest_dev(nh->nh_dev);
b5d6fbde
SF
280 if (!dev)
281 return NULL;
282
ebb9a03a 283 if (switchdev_parent_id_get(dev, &psid))
b5d6fbde
SF
284 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
5e8d9049 299/**
ebb9a03a 300 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
5e8d9049
SF
301 *
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
f8f21471 307 * @nlflags: netlink flags passed in (NLM_F_*)
5e8d9049
SF
308 * @tb_id: route table ID
309 *
310 * Add IPv4 route entry to switch device.
311 */
ebb9a03a
JP
312int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
313 u8 tos, u8 type, u32 nlflags, u32 tb_id)
5e8d9049 314{
b5d6fbde 315 struct net_device *dev;
9d47c0a2 316 const struct switchdev_ops *ops;
b5d6fbde
SF
317 int err = 0;
318
8e05fd71
SF
319 /* Don't offload route if using custom ip rules or if
320 * IPv4 FIB offloading has been disabled completely.
321 */
322
e1315db1
SF
323#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)
104616e7
SF
329 return 0;
330
ebb9a03a 331 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
332 if (!dev)
333 return 0;
9d47c0a2 334 ops = dev->switchdev_ops;
b5d6fbde 335
9d47c0a2
JP
336 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);
b5d6fbde
SF
340 if (!err)
341 fi->fib_flags |= RTNH_F_EXTERNAL;
342 }
343
344 return err;
5e8d9049 345}
ebb9a03a 346EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
5e8d9049
SF
347
348/**
ebb9a03a 349 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
5e8d9049
SF
350 *
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 */
ebb9a03a
JP
360int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
361 u8 tos, u8 type, u32 tb_id)
5e8d9049 362{
b5d6fbde 363 struct net_device *dev;
9d47c0a2 364 const struct switchdev_ops *ops;
b5d6fbde
SF
365 int err = 0;
366
367 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
368 return 0;
369
ebb9a03a 370 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
371 if (!dev)
372 return 0;
9d47c0a2 373 ops = dev->switchdev_ops;
b5d6fbde 374
9d47c0a2
JP
375 if (ops->switchdev_fib_ipv4_del) {
376 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
377 fi, tos, type, tb_id);
b5d6fbde
SF
378 if (!err)
379 fi->fib_flags &= ~RTNH_F_EXTERNAL;
380 }
381
382 return err;
5e8d9049 383}
ebb9a03a 384EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
8e05fd71
SF
385
386/**
ebb9a03a 387 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
8e05fd71
SF
388 *
389 * @fi: route FIB info structure
390 */
ebb9a03a 391void switchdev_fib_ipv4_abort(struct fib_info *fi)
8e05fd71
SF
392{
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}
ebb9a03a 404EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
This page took 0.066233 seconds and 5 git commands to generate.