blob: 8f47187dc185f6475d47427636ecd780e0bdb15d [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/**
Scott Feldman30943332015-05-10 09:47:48 -070040 * switchdev_port_attr_get - Get port attribute
41 *
42 * @dev: port device
43 * @attr: attribute to get
44 */
45int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
46{
47 const struct switchdev_ops *ops = dev->switchdev_ops;
48 struct net_device *lower_dev;
49 struct list_head *iter;
50 struct switchdev_attr first = {
51 .id = SWITCHDEV_ATTR_UNDEFINED
52 };
53 int err = -EOPNOTSUPP;
54
55 if (ops && ops->switchdev_port_attr_get)
56 return ops->switchdev_port_attr_get(dev, attr);
57
58 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
59 return err;
60
61 /* Switch device port(s) may be stacked under
62 * bond/team/vlan dev, so recurse down to get attr on
63 * each port. Return -ENODATA if attr values don't
64 * compare across ports.
65 */
66
67 netdev_for_each_lower_dev(dev, lower_dev, iter) {
68 err = switchdev_port_attr_get(lower_dev, attr);
69 if (err)
70 break;
71 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
72 first = *attr;
73 else if (memcmp(&first, attr, sizeof(*attr)))
74 return -ENODATA;
75 }
76
77 return err;
78}
79EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
80
81static int __switchdev_port_attr_set(struct net_device *dev,
82 struct switchdev_attr *attr)
83{
84 const struct switchdev_ops *ops = dev->switchdev_ops;
85 struct net_device *lower_dev;
86 struct list_head *iter;
87 int err = -EOPNOTSUPP;
88
89 if (ops && ops->switchdev_port_attr_set)
90 return ops->switchdev_port_attr_set(dev, attr);
91
92 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
93 return err;
94
95 /* Switch device port(s) may be stacked under
96 * bond/team/vlan dev, so recurse down to set attr on
97 * each port.
98 */
99
100 netdev_for_each_lower_dev(dev, lower_dev, iter) {
101 err = __switchdev_port_attr_set(lower_dev, attr);
102 if (err)
103 break;
104 }
105
106 return err;
107}
108
109struct switchdev_attr_set_work {
110 struct work_struct work;
111 struct net_device *dev;
112 struct switchdev_attr attr;
113};
114
115static void switchdev_port_attr_set_work(struct work_struct *work)
116{
117 struct switchdev_attr_set_work *asw =
118 container_of(work, struct switchdev_attr_set_work, work);
119 int err;
120
121 rtnl_lock();
122 err = switchdev_port_attr_set(asw->dev, &asw->attr);
123 BUG_ON(err);
124 rtnl_unlock();
125
126 dev_put(asw->dev);
127 kfree(work);
128}
129
130static int switchdev_port_attr_set_defer(struct net_device *dev,
131 struct switchdev_attr *attr)
132{
133 struct switchdev_attr_set_work *asw;
134
135 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
136 if (!asw)
137 return -ENOMEM;
138
139 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
140
141 dev_hold(dev);
142 asw->dev = dev;
143 memcpy(&asw->attr, attr, sizeof(asw->attr));
144
145 schedule_work(&asw->work);
146
147 return 0;
148}
149
150/**
151 * switchdev_port_attr_set - Set port attribute
152 *
153 * @dev: port device
154 * @attr: attribute to set
155 *
156 * Use a 2-phase prepare-commit transaction model to ensure
157 * system is not left in a partially updated state due to
158 * failure from driver/device.
159 */
160int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
161{
162 int err;
163
164 if (!rtnl_is_locked()) {
165 /* Running prepare-commit transaction across stacked
166 * devices requires nothing moves, so if rtnl_lock is
167 * not held, schedule a worker thread to hold rtnl_lock
168 * while setting attr.
169 */
170
171 return switchdev_port_attr_set_defer(dev, attr);
172 }
173
174 /* Phase I: prepare for attr set. Driver/device should fail
175 * here if there are going to be issues in the commit phase,
176 * such as lack of resources or support. The driver/device
177 * should reserve resources needed for the commit phase here,
178 * but should not commit the attr.
179 */
180
181 attr->trans = SWITCHDEV_TRANS_PREPARE;
182 err = __switchdev_port_attr_set(dev, attr);
183 if (err) {
184 /* Prepare phase failed: abort the transaction. Any
185 * resources reserved in the prepare phase are
186 * released.
187 */
188
189 attr->trans = SWITCHDEV_TRANS_ABORT;
190 __switchdev_port_attr_set(dev, attr);
191
192 return err;
193 }
194
195 /* Phase II: commit attr set. This cannot fail as a fault
196 * of driver/device. If it does, it's a bug in the driver/device
197 * because the driver said everythings was OK in phase I.
198 */
199
200 attr->trans = SWITCHDEV_TRANS_COMMIT;
201 err = __switchdev_port_attr_set(dev, attr);
202 BUG_ON(err);
203
204 return err;
205}
206EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
207
208/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700209 * switchdev_port_stp_update - Notify switch device port of STP
Scott Feldman38dcf352014-11-28 14:34:20 +0100210 * state change
211 * @dev: port device
212 * @state: port STP state
213 *
214 * Notify switch device port of bridge port STP state change.
215 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700216int switchdev_port_stp_update(struct net_device *dev, u8 state)
Scott Feldman38dcf352014-11-28 14:34:20 +0100217{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700218 const struct switchdev_ops *ops = dev->switchdev_ops;
Roopa Prabhu558d51f2015-03-21 10:27:28 -0700219 struct net_device *lower_dev;
220 struct list_head *iter;
221 int err = -EOPNOTSUPP;
Scott Feldman38dcf352014-11-28 14:34:20 +0100222
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700223 if (ops && ops->switchdev_port_stp_update)
224 return ops->switchdev_port_stp_update(dev, state);
Roopa Prabhu558d51f2015-03-21 10:27:28 -0700225
226 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700227 err = switchdev_port_stp_update(lower_dev, state);
Roopa Prabhu558d51f2015-03-21 10:27:28 -0700228 if (err && err != -EOPNOTSUPP)
229 return err;
230 }
231
232 return err;
Scott Feldman38dcf352014-11-28 14:34:20 +0100233}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700234EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100235
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700236static DEFINE_MUTEX(switchdev_mutex);
237static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100238
239/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700240 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100241 * @nb: notifier_block
242 *
243 * Register switch device notifier. This should be used by code
244 * which needs to monitor events happening in particular device.
245 * Return values are same as for atomic_notifier_chain_register().
246 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700247int register_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_register(&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(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100257
258/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700259 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100260 * @nb: notifier_block
261 *
262 * Unregister switch device notifier.
263 * Return values are same as for atomic_notifier_chain_unregister().
264 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700265int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100266{
267 int err;
268
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700269 mutex_lock(&switchdev_mutex);
270 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
271 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100272 return err;
273}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700274EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100275
276/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700277 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100278 * @val: value passed unmodified to notifier function
279 * @dev: port device
280 * @info: notifier information data
281 *
282 * Call all network notifier blocks. This should be called by driver
283 * when it needs to propagate hardware event.
284 * Return values are same as for atomic_notifier_call_chain().
285 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700286int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
287 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100288{
289 int err;
290
291 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700292 mutex_lock(&switchdev_mutex);
293 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
294 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100295 return err;
296}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700297EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800298
299/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700300 * switchdev_port_bridge_setlink - Notify switch device port of bridge
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800301 * port attributes
302 *
303 * @dev: port device
304 * @nlh: netlink msg with bridge port attributes
305 * @flags: bridge setlink flags
306 *
307 * Notify switch device port of bridge port attributes
308 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700309int switchdev_port_bridge_setlink(struct net_device *dev,
310 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800311{
312 const struct net_device_ops *ops = dev->netdev_ops;
313
314 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
315 return 0;
316
317 if (!ops->ndo_bridge_setlink)
318 return -EOPNOTSUPP;
319
320 return ops->ndo_bridge_setlink(dev, nlh, flags);
321}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700322EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800323
324/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700325 * switchdev_port_bridge_dellink - Notify switch device port of bridge
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800326 * port attribute delete
327 *
328 * @dev: port device
329 * @nlh: netlink msg with bridge port attributes
330 * @flags: bridge setlink flags
331 *
332 * Notify switch device port of bridge port attribute delete
333 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700334int switchdev_port_bridge_dellink(struct net_device *dev,
335 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800336{
337 const struct net_device_ops *ops = dev->netdev_ops;
338
339 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
340 return 0;
341
342 if (!ops->ndo_bridge_dellink)
343 return -EOPNOTSUPP;
344
345 return ops->ndo_bridge_dellink(dev, nlh, flags);
346}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700347EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800348
349/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700350 * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
351 * op for master devices
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800352 *
353 * @dev: port device
354 * @nlh: netlink msg with bridge port attributes
355 * @flags: bridge setlink flags
356 *
357 * Notify master device slaves of bridge port attributes
358 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700359int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
360 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800361{
362 struct net_device *lower_dev;
363 struct list_head *iter;
364 int ret = 0, err = 0;
365
366 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
367 return ret;
368
369 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700370 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800371 if (err && err != -EOPNOTSUPP)
372 ret = err;
373 }
374
375 return ret;
376}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700377EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800378
379/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700380 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
381 * op for master devices
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800382 *
383 * @dev: port device
384 * @nlh: netlink msg with bridge port attributes
385 * @flags: bridge dellink flags
386 *
387 * Notify master device slaves of bridge port attribute deletes
388 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700389int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
390 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800391{
392 struct net_device *lower_dev;
393 struct list_head *iter;
394 int ret = 0, err = 0;
395
396 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
397 return ret;
398
399 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700400 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800401 if (err && err != -EOPNOTSUPP)
402 ret = err;
403 }
404
405 return ret;
406}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700407EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800408
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700409static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800410{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700411 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800412 struct net_device *lower_dev;
413 struct net_device *port_dev;
414 struct list_head *iter;
415
416 /* Recusively search down until we find a sw port dev.
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700417 * (A sw port dev supports switchdev_parent_id_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800418 */
419
420 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700421 ops && ops->switchdev_parent_id_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800422 return dev;
423
424 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700425 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800426 if (port_dev)
427 return port_dev;
428 }
429
430 return NULL;
431}
432
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700433static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800434{
435 struct netdev_phys_item_id psid;
436 struct netdev_phys_item_id prev_psid;
437 struct net_device *dev = NULL;
438 int nhsel;
439
440 /* For this route, all nexthop devs must be on the same switch. */
441
442 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
443 const struct fib_nh *nh = &fi->fib_nh[nhsel];
444
445 if (!nh->nh_dev)
446 return NULL;
447
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700448 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800449 if (!dev)
450 return NULL;
451
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700452 if (switchdev_parent_id_get(dev, &psid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800453 return NULL;
454
455 if (nhsel > 0) {
456 if (prev_psid.id_len != psid.id_len)
457 return NULL;
458 if (memcmp(prev_psid.id, psid.id, psid.id_len))
459 return NULL;
460 }
461
462 prev_psid = psid;
463 }
464
465 return dev;
466}
467
Scott Feldman5e8d9042015-03-05 21:21:15 -0800468/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700469 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800470 *
471 * @dst: route's IPv4 destination address
472 * @dst_len: destination address length (prefix length)
473 * @fi: route FIB info structure
474 * @tos: route TOS
475 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -0700476 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800477 * @tb_id: route table ID
478 *
479 * Add IPv4 route entry to switch device.
480 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700481int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
482 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800483{
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800484 struct net_device *dev;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700485 const struct switchdev_ops *ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800486 int err = 0;
487
Scott Feldman8e05fd72015-03-05 21:21:19 -0800488 /* Don't offload route if using custom ip rules or if
489 * IPv4 FIB offloading has been disabled completely.
490 */
491
Scott Feldmane1315db2015-03-06 01:14:36 -0800492#ifdef CONFIG_IP_MULTIPLE_TABLES
493 if (fi->fib_net->ipv4.fib_has_custom_rules)
494 return 0;
495#endif
496
497 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -0800498 return 0;
499
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700500 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800501 if (!dev)
502 return 0;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700503 ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800504
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700505 if (ops->switchdev_fib_ipv4_add) {
506 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
507 fi, tos, type, nlflags,
508 tb_id);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800509 if (!err)
510 fi->fib_flags |= RTNH_F_EXTERNAL;
511 }
512
513 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800514}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700515EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800516
517/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700518 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -0800519 *
520 * @dst: route's IPv4 destination address
521 * @dst_len: destination address length (prefix length)
522 * @fi: route FIB info structure
523 * @tos: route TOS
524 * @type: route type
525 * @tb_id: route table ID
526 *
527 * Delete IPv4 route entry from switch device.
528 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700529int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
530 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800531{
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800532 struct net_device *dev;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700533 const struct switchdev_ops *ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800534 int err = 0;
535
536 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
537 return 0;
538
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700539 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800540 if (!dev)
541 return 0;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700542 ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800543
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700544 if (ops->switchdev_fib_ipv4_del) {
545 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
546 fi, tos, type, tb_id);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800547 if (!err)
548 fi->fib_flags &= ~RTNH_F_EXTERNAL;
549 }
550
551 return err;
Scott Feldman5e8d9042015-03-05 21:21:15 -0800552}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700553EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -0800554
555/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700556 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -0800557 *
558 * @fi: route FIB info structure
559 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700560void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -0800561{
562 /* There was a problem installing this route to the offload
563 * device. For now, until we come up with more refined
564 * policy handling, abruptly end IPv4 fib offloading for
565 * for entire net by flushing offload device(s) of all
566 * IPv4 routes, and mark IPv4 fib offloading broken from
567 * this point forward.
568 */
569
570 fib_flush_external(fi->fib_net);
571 fi->fib_net->ipv4.fib_offload_disabled = true;
572}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700573EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);