blob: 117fd0797abd5bbb21bea5024e32327961285df1 [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/**
Scott Feldman30943332015-05-10 09:47:48 -070022 * switchdev_port_attr_get - Get port attribute
23 *
24 * @dev: port device
25 * @attr: attribute to get
26 */
27int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
28{
29 const struct switchdev_ops *ops = dev->switchdev_ops;
30 struct net_device *lower_dev;
31 struct list_head *iter;
32 struct switchdev_attr first = {
33 .id = SWITCHDEV_ATTR_UNDEFINED
34 };
35 int err = -EOPNOTSUPP;
36
37 if (ops && ops->switchdev_port_attr_get)
38 return ops->switchdev_port_attr_get(dev, attr);
39
40 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
41 return err;
42
43 /* Switch device port(s) may be stacked under
44 * bond/team/vlan dev, so recurse down to get attr on
45 * each port. Return -ENODATA if attr values don't
46 * compare across ports.
47 */
48
49 netdev_for_each_lower_dev(dev, lower_dev, iter) {
50 err = switchdev_port_attr_get(lower_dev, attr);
51 if (err)
52 break;
53 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
54 first = *attr;
55 else if (memcmp(&first, attr, sizeof(*attr)))
56 return -ENODATA;
57 }
58
59 return err;
60}
61EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
62
63static int __switchdev_port_attr_set(struct net_device *dev,
64 struct switchdev_attr *attr)
65{
66 const struct switchdev_ops *ops = dev->switchdev_ops;
67 struct net_device *lower_dev;
68 struct list_head *iter;
69 int err = -EOPNOTSUPP;
70
71 if (ops && ops->switchdev_port_attr_set)
72 return ops->switchdev_port_attr_set(dev, attr);
73
74 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
75 return err;
76
77 /* Switch device port(s) may be stacked under
78 * bond/team/vlan dev, so recurse down to set attr on
79 * each port.
80 */
81
82 netdev_for_each_lower_dev(dev, lower_dev, iter) {
83 err = __switchdev_port_attr_set(lower_dev, attr);
84 if (err)
85 break;
86 }
87
88 return err;
89}
90
91struct switchdev_attr_set_work {
92 struct work_struct work;
93 struct net_device *dev;
94 struct switchdev_attr attr;
95};
96
97static void switchdev_port_attr_set_work(struct work_struct *work)
98{
99 struct switchdev_attr_set_work *asw =
100 container_of(work, struct switchdev_attr_set_work, work);
101 int err;
102
103 rtnl_lock();
104 err = switchdev_port_attr_set(asw->dev, &asw->attr);
105 BUG_ON(err);
106 rtnl_unlock();
107
108 dev_put(asw->dev);
109 kfree(work);
110}
111
112static int switchdev_port_attr_set_defer(struct net_device *dev,
113 struct switchdev_attr *attr)
114{
115 struct switchdev_attr_set_work *asw;
116
117 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
118 if (!asw)
119 return -ENOMEM;
120
121 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
122
123 dev_hold(dev);
124 asw->dev = dev;
125 memcpy(&asw->attr, attr, sizeof(asw->attr));
126
127 schedule_work(&asw->work);
128
129 return 0;
130}
131
132/**
133 * switchdev_port_attr_set - Set port attribute
134 *
135 * @dev: port device
136 * @attr: attribute to set
137 *
138 * Use a 2-phase prepare-commit transaction model to ensure
139 * system is not left in a partially updated state due to
140 * failure from driver/device.
141 */
142int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
143{
144 int err;
145
146 if (!rtnl_is_locked()) {
147 /* Running prepare-commit transaction across stacked
148 * devices requires nothing moves, so if rtnl_lock is
149 * not held, schedule a worker thread to hold rtnl_lock
150 * while setting attr.
151 */
152
153 return switchdev_port_attr_set_defer(dev, attr);
154 }
155
156 /* Phase I: prepare for attr set. Driver/device should fail
157 * here if there are going to be issues in the commit phase,
158 * such as lack of resources or support. The driver/device
159 * should reserve resources needed for the commit phase here,
160 * but should not commit the attr.
161 */
162
163 attr->trans = SWITCHDEV_TRANS_PREPARE;
164 err = __switchdev_port_attr_set(dev, attr);
165 if (err) {
166 /* Prepare phase failed: abort the transaction. Any
167 * resources reserved in the prepare phase are
168 * released.
169 */
170
171 attr->trans = SWITCHDEV_TRANS_ABORT;
172 __switchdev_port_attr_set(dev, attr);
173
174 return err;
175 }
176
177 /* Phase II: commit attr set. This cannot fail as a fault
178 * of driver/device. If it does, it's a bug in the driver/device
179 * because the driver said everythings was OK in phase I.
180 */
181
182 attr->trans = SWITCHDEV_TRANS_COMMIT;
183 err = __switchdev_port_attr_set(dev, attr);
184 BUG_ON(err);
185
186 return err;
187}
188EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
189
190/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700191 * switchdev_port_stp_update - Notify switch device port of STP
Scott Feldman38dcf352014-11-28 14:34:20 +0100192 * state change
193 * @dev: port device
194 * @state: port STP state
195 *
196 * Notify switch device port of bridge port STP state change.
197 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700198int switchdev_port_stp_update(struct net_device *dev, u8 state)
Scott Feldman38dcf352014-11-28 14:34:20 +0100199{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700200 const struct switchdev_ops *ops = dev->switchdev_ops;
Roopa Prabhu558d51f2015-03-21 10:27:28 -0700201 struct net_device *lower_dev;
202 struct list_head *iter;
203 int err = -EOPNOTSUPP;
Scott Feldman38dcf352014-11-28 14:34:20 +0100204
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700205 if (ops && ops->switchdev_port_stp_update)
206 return ops->switchdev_port_stp_update(dev, state);
Roopa Prabhu558d51f2015-03-21 10:27:28 -0700207
208 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700209 err = switchdev_port_stp_update(lower_dev, state);
Roopa Prabhu558d51f2015-03-21 10:27:28 -0700210 if (err && err != -EOPNOTSUPP)
211 return err;
212 }
213
214 return err;
Scott Feldman38dcf352014-11-28 14:34:20 +0100215}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700216EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100217
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700218static DEFINE_MUTEX(switchdev_mutex);
219static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100220
221/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700222 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100223 * @nb: notifier_block
224 *
225 * Register switch device notifier. This should be used by code
226 * which needs to monitor events happening in particular device.
227 * Return values are same as for atomic_notifier_chain_register().
228 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700229int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100230{
231 int err;
232
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700233 mutex_lock(&switchdev_mutex);
234 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
235 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100236 return err;
237}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700238EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100239
240/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700241 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100242 * @nb: notifier_block
243 *
244 * Unregister switch device notifier.
245 * Return values are same as for atomic_notifier_chain_unregister().
246 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700247int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100248{
249 int err;
250
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700251 mutex_lock(&switchdev_mutex);
252 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
253 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100254 return err;
255}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700256EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100257
258/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700259 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100260 * @val: value passed unmodified to notifier function
261 * @dev: port device
262 * @info: notifier information data
263 *
264 * Call all network notifier blocks. This should be called by driver
265 * when it needs to propagate hardware event.
266 * Return values are same as for atomic_notifier_call_chain().
267 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700268int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
269 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100270{
271 int err;
272
273 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700274 mutex_lock(&switchdev_mutex);
275 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
276 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100277 return err;
278}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700279EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800280
281/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700282 * switchdev_port_bridge_setlink - Notify switch device port of bridge
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800283 * port attributes
284 *
285 * @dev: port device
286 * @nlh: netlink msg with bridge port attributes
287 * @flags: bridge setlink flags
288 *
289 * Notify switch device port of bridge port attributes
290 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700291int switchdev_port_bridge_setlink(struct net_device *dev,
292 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800293{
294 const struct net_device_ops *ops = dev->netdev_ops;
295
296 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
297 return 0;
298
299 if (!ops->ndo_bridge_setlink)
300 return -EOPNOTSUPP;
301
302 return ops->ndo_bridge_setlink(dev, nlh, flags);
303}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700304EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800305
306/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700307 * switchdev_port_bridge_dellink - Notify switch device port of bridge
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800308 * port attribute delete
309 *
310 * @dev: port device
311 * @nlh: netlink msg with bridge port attributes
312 * @flags: bridge setlink flags
313 *
314 * Notify switch device port of bridge port attribute delete
315 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700316int switchdev_port_bridge_dellink(struct net_device *dev,
317 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800318{
319 const struct net_device_ops *ops = dev->netdev_ops;
320
321 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
322 return 0;
323
324 if (!ops->ndo_bridge_dellink)
325 return -EOPNOTSUPP;
326
327 return ops->ndo_bridge_dellink(dev, nlh, flags);
328}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700329EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800330
331/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700332 * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
333 * op for master devices
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800334 *
335 * @dev: port device
336 * @nlh: netlink msg with bridge port attributes
337 * @flags: bridge setlink flags
338 *
339 * Notify master device slaves of bridge port attributes
340 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700341int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
342 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800343{
344 struct net_device *lower_dev;
345 struct list_head *iter;
346 int ret = 0, err = 0;
347
348 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
349 return ret;
350
351 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700352 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800353 if (err && err != -EOPNOTSUPP)
354 ret = err;
355 }
356
357 return ret;
358}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700359EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800360
361/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700362 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
363 * op for master devices
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800364 *
365 * @dev: port device
366 * @nlh: netlink msg with bridge port attributes
367 * @flags: bridge dellink flags
368 *
369 * Notify master device slaves of bridge port attribute deletes
370 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700371int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
372 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800373{
374 struct net_device *lower_dev;
375 struct list_head *iter;
376 int ret = 0, err = 0;
377
378 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
379 return ret;
380
381 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700382 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800383 if (err && err != -EOPNOTSUPP)
384 ret = err;
385 }
386
387 return ret;
388}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700389EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800390
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700391static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800392{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700393 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800394 struct net_device *lower_dev;
395 struct net_device *port_dev;
396 struct list_head *iter;
397
398 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700399 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800400 */
401
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700402 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800403 return dev;
404
405 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700406 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800407 if (port_dev)
408 return port_dev;
409 }
410
411 return NULL;
412}
413
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700414static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800415{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700416 struct switchdev_attr attr = {
417 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
418 };
419 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800420 struct net_device *dev = NULL;
421 int nhsel;
422
423 /* For this route, all nexthop devs must be on the same switch. */
424
425 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
426 const struct fib_nh *nh = &fi->fib_nh[nhsel];
427
428 if (!nh->nh_dev)
429 return NULL;
430
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700431 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800432 if (!dev)
433 return NULL;
434
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700435 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800436 return NULL;
437
438 if (nhsel > 0) {
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700439 if (prev_attr.ppid.id_len != attr.ppid.id_len)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800440 return NULL;
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700441 if (memcmp(prev_attr.ppid.id, attr.ppid.id,
442 attr.ppid.id_len))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800443 return NULL;
444 }
445
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700446 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800447 }
448
449 return dev;
450}
451
Scott Feldman5e8d9042015-03-05 21:21:15 -0800452/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700453 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800454 *
455 * @dst: route's IPv4 destination address
456 * @dst_len: destination address length (prefix length)
457 * @fi: route FIB info structure
458 * @tos: route TOS
459 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -0700460 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800461 * @tb_id: route table ID
462 *
463 * Add IPv4 route entry to switch device.
464 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700465int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
466 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800467{
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800468 struct net_device *dev;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700469 const struct switchdev_ops *ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800470 int err = 0;
471
Scott Feldman8e05fd72015-03-05 21:21:19 -0800472 /* Don't offload route if using custom ip rules or if
473 * IPv4 FIB offloading has been disabled completely.
474 */
475
Scott Feldmane1315db2015-03-06 01:14:36 -0800476#ifdef CONFIG_IP_MULTIPLE_TABLES
477 if (fi->fib_net->ipv4.fib_has_custom_rules)
478 return 0;
479#endif
480
481 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -0800482 return 0;
483
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700484 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800485 if (!dev)
486 return 0;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700487 ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800488
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700489 if (ops->switchdev_fib_ipv4_add) {
490 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
491 fi, tos, type, nlflags,
492 tb_id);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800493 if (!err)
494 fi->fib_flags |= RTNH_F_EXTERNAL;
495 }
496
497 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800498}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700499EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800500
501/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700502 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800503 *
504 * @dst: route's IPv4 destination address
505 * @dst_len: destination address length (prefix length)
506 * @fi: route FIB info structure
507 * @tos: route TOS
508 * @type: route type
509 * @tb_id: route table ID
510 *
511 * Delete IPv4 route entry from switch device.
512 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700513int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
514 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800515{
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800516 struct net_device *dev;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700517 const struct switchdev_ops *ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800518 int err = 0;
519
520 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
521 return 0;
522
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700523 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800524 if (!dev)
525 return 0;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700526 ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800527
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700528 if (ops->switchdev_fib_ipv4_del) {
529 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
530 fi, tos, type, tb_id);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800531 if (!err)
532 fi->fib_flags &= ~RTNH_F_EXTERNAL;
533 }
534
535 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800536}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700537EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -0800538
539/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700540 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -0800541 *
542 * @fi: route FIB info structure
543 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700544void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -0800545{
546 /* There was a problem installing this route to the offload
547 * device. For now, until we come up with more refined
548 * policy handling, abruptly end IPv4 fib offloading for
549 * for entire net by flushing offload device(s) of all
550 * IPv4 routes, and mark IPv4 fib offloading broken from
551 * this point forward.
552 */
553
554 fib_flush_external(fi->fib_net);
555 fi->fib_net->ipv4.fib_offload_disabled = true;
556}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700557EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);