switchdev: add new switchdev_port_bridge_dellink
[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>
47f8328b 18#include <linux/if_bridge.h>
5e8d9049 19#include <net/ip_fib.h>
007f790c
JP
20#include <net/switchdev.h>
21
3094333d
SF
22/**
23 * switchdev_port_attr_get - Get port attribute
24 *
25 * @dev: port device
26 * @attr: attribute to get
27 */
28int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
29{
30 const struct switchdev_ops *ops = dev->switchdev_ops;
31 struct net_device *lower_dev;
32 struct list_head *iter;
33 struct switchdev_attr first = {
34 .id = SWITCHDEV_ATTR_UNDEFINED
35 };
36 int err = -EOPNOTSUPP;
37
38 if (ops && ops->switchdev_port_attr_get)
39 return ops->switchdev_port_attr_get(dev, attr);
40
41 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
42 return err;
43
44 /* Switch device port(s) may be stacked under
45 * bond/team/vlan dev, so recurse down to get attr on
46 * each port. Return -ENODATA if attr values don't
47 * compare across ports.
48 */
49
50 netdev_for_each_lower_dev(dev, lower_dev, iter) {
51 err = switchdev_port_attr_get(lower_dev, attr);
52 if (err)
53 break;
54 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
55 first = *attr;
56 else if (memcmp(&first, attr, sizeof(*attr)))
57 return -ENODATA;
58 }
59
60 return err;
61}
62EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
63
64static int __switchdev_port_attr_set(struct net_device *dev,
65 struct switchdev_attr *attr)
66{
67 const struct switchdev_ops *ops = dev->switchdev_ops;
68 struct net_device *lower_dev;
69 struct list_head *iter;
70 int err = -EOPNOTSUPP;
71
72 if (ops && ops->switchdev_port_attr_set)
73 return ops->switchdev_port_attr_set(dev, attr);
74
75 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
76 return err;
77
78 /* Switch device port(s) may be stacked under
79 * bond/team/vlan dev, so recurse down to set attr on
80 * each port.
81 */
82
83 netdev_for_each_lower_dev(dev, lower_dev, iter) {
84 err = __switchdev_port_attr_set(lower_dev, attr);
85 if (err)
86 break;
87 }
88
89 return err;
90}
91
92struct switchdev_attr_set_work {
93 struct work_struct work;
94 struct net_device *dev;
95 struct switchdev_attr attr;
96};
97
98static void switchdev_port_attr_set_work(struct work_struct *work)
99{
100 struct switchdev_attr_set_work *asw =
101 container_of(work, struct switchdev_attr_set_work, work);
102 int err;
103
104 rtnl_lock();
105 err = switchdev_port_attr_set(asw->dev, &asw->attr);
106 BUG_ON(err);
107 rtnl_unlock();
108
109 dev_put(asw->dev);
110 kfree(work);
111}
112
113static int switchdev_port_attr_set_defer(struct net_device *dev,
114 struct switchdev_attr *attr)
115{
116 struct switchdev_attr_set_work *asw;
117
118 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
119 if (!asw)
120 return -ENOMEM;
121
122 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
123
124 dev_hold(dev);
125 asw->dev = dev;
126 memcpy(&asw->attr, attr, sizeof(asw->attr));
127
128 schedule_work(&asw->work);
129
130 return 0;
131}
132
133/**
134 * switchdev_port_attr_set - Set port attribute
135 *
136 * @dev: port device
137 * @attr: attribute to set
138 *
139 * Use a 2-phase prepare-commit transaction model to ensure
140 * system is not left in a partially updated state due to
141 * failure from driver/device.
142 */
143int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
144{
145 int err;
146
147 if (!rtnl_is_locked()) {
148 /* Running prepare-commit transaction across stacked
149 * devices requires nothing moves, so if rtnl_lock is
150 * not held, schedule a worker thread to hold rtnl_lock
151 * while setting attr.
152 */
153
154 return switchdev_port_attr_set_defer(dev, attr);
155 }
156
157 /* Phase I: prepare for attr set. Driver/device should fail
158 * here if there are going to be issues in the commit phase,
159 * such as lack of resources or support. The driver/device
160 * should reserve resources needed for the commit phase here,
161 * but should not commit the attr.
162 */
163
164 attr->trans = SWITCHDEV_TRANS_PREPARE;
165 err = __switchdev_port_attr_set(dev, attr);
166 if (err) {
167 /* Prepare phase failed: abort the transaction. Any
168 * resources reserved in the prepare phase are
169 * released.
170 */
171
172 attr->trans = SWITCHDEV_TRANS_ABORT;
173 __switchdev_port_attr_set(dev, attr);
174
175 return err;
176 }
177
178 /* Phase II: commit attr set. This cannot fail as a fault
179 * of driver/device. If it does, it's a bug in the driver/device
180 * because the driver said everythings was OK in phase I.
181 */
182
183 attr->trans = SWITCHDEV_TRANS_COMMIT;
184 err = __switchdev_port_attr_set(dev, attr);
185 BUG_ON(err);
186
187 return err;
188}
189EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
190
491d0f15
SF
191int __switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
192{
193 const struct switchdev_ops *ops = dev->switchdev_ops;
194 struct net_device *lower_dev;
195 struct list_head *iter;
196 int err = -EOPNOTSUPP;
197
198 if (ops && ops->switchdev_port_obj_add)
199 return ops->switchdev_port_obj_add(dev, obj);
200
201 /* Switch device port(s) may be stacked under
202 * bond/team/vlan dev, so recurse down to add object on
203 * each port.
204 */
205
206 netdev_for_each_lower_dev(dev, lower_dev, iter) {
207 err = __switchdev_port_obj_add(lower_dev, obj);
208 if (err)
209 break;
210 }
211
212 return err;
213}
214
215/**
216 * switchdev_port_obj_add - Add port object
217 *
218 * @dev: port device
219 * @obj: object to add
220 *
221 * Use a 2-phase prepare-commit transaction model to ensure
222 * system is not left in a partially updated state due to
223 * failure from driver/device.
224 *
225 * rtnl_lock must be held.
226 */
227int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
228{
229 int err;
230
231 ASSERT_RTNL();
232
233 /* Phase I: prepare for obj add. Driver/device should fail
234 * here if there are going to be issues in the commit phase,
235 * such as lack of resources or support. The driver/device
236 * should reserve resources needed for the commit phase here,
237 * but should not commit the obj.
238 */
239
240 obj->trans = SWITCHDEV_TRANS_PREPARE;
241 err = __switchdev_port_obj_add(dev, obj);
242 if (err) {
243 /* Prepare phase failed: abort the transaction. Any
244 * resources reserved in the prepare phase are
245 * released.
246 */
247
248 obj->trans = SWITCHDEV_TRANS_ABORT;
249 __switchdev_port_obj_add(dev, obj);
250
251 return err;
252 }
253
254 /* Phase II: commit obj add. This cannot fail as a fault
255 * of driver/device. If it does, it's a bug in the driver/device
256 * because the driver said everythings was OK in phase I.
257 */
258
259 obj->trans = SWITCHDEV_TRANS_COMMIT;
260 err = __switchdev_port_obj_add(dev, obj);
261 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
262
263 return err;
264}
265EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
266
267/**
268 * switchdev_port_obj_del - Delete port object
269 *
270 * @dev: port device
271 * @obj: object to delete
272 */
273int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
274{
275 const struct switchdev_ops *ops = dev->switchdev_ops;
276 struct net_device *lower_dev;
277 struct list_head *iter;
278 int err = -EOPNOTSUPP;
279
280 if (ops && ops->switchdev_port_obj_del)
281 return ops->switchdev_port_obj_del(dev, obj);
282
283 /* Switch device port(s) may be stacked under
284 * bond/team/vlan dev, so recurse down to delete object on
285 * each port.
286 */
287
288 netdev_for_each_lower_dev(dev, lower_dev, iter) {
289 err = switchdev_port_obj_del(lower_dev, obj);
290 if (err)
291 break;
292 }
293
294 return err;
295}
296EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
297
ebb9a03a
JP
298static DEFINE_MUTEX(switchdev_mutex);
299static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
03bf0c28
JP
300
301/**
ebb9a03a 302 * register_switchdev_notifier - Register notifier
03bf0c28
JP
303 * @nb: notifier_block
304 *
305 * Register switch device notifier. This should be used by code
306 * which needs to monitor events happening in particular device.
307 * Return values are same as for atomic_notifier_chain_register().
308 */
ebb9a03a 309int register_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
310{
311 int err;
312
ebb9a03a
JP
313 mutex_lock(&switchdev_mutex);
314 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
315 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
316 return err;
317}
ebb9a03a 318EXPORT_SYMBOL_GPL(register_switchdev_notifier);
03bf0c28
JP
319
320/**
ebb9a03a 321 * unregister_switchdev_notifier - Unregister notifier
03bf0c28
JP
322 * @nb: notifier_block
323 *
324 * Unregister switch device notifier.
325 * Return values are same as for atomic_notifier_chain_unregister().
326 */
ebb9a03a 327int unregister_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
328{
329 int err;
330
ebb9a03a
JP
331 mutex_lock(&switchdev_mutex);
332 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
333 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
334 return err;
335}
ebb9a03a 336EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
03bf0c28
JP
337
338/**
ebb9a03a 339 * call_switchdev_notifiers - Call notifiers
03bf0c28
JP
340 * @val: value passed unmodified to notifier function
341 * @dev: port device
342 * @info: notifier information data
343 *
344 * Call all network notifier blocks. This should be called by driver
345 * when it needs to propagate hardware event.
346 * Return values are same as for atomic_notifier_call_chain().
347 */
ebb9a03a
JP
348int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
349 struct switchdev_notifier_info *info)
03bf0c28
JP
350{
351 int err;
352
353 info->dev = dev;
ebb9a03a
JP
354 mutex_lock(&switchdev_mutex);
355 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
356 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
357 return err;
358}
ebb9a03a 359EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
8a44dbb2 360
47f8328b
SF
361static int switchdev_port_br_setflag(struct net_device *dev,
362 struct nlattr *nlattr,
363 unsigned long brport_flag)
364{
365 struct switchdev_attr attr = {
366 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
367 };
368 u8 flag = nla_get_u8(nlattr);
369 int err;
370
371 err = switchdev_port_attr_get(dev, &attr);
372 if (err)
373 return err;
374
375 if (flag)
376 attr.brport_flags |= brport_flag;
377 else
378 attr.brport_flags &= ~brport_flag;
379
380 return switchdev_port_attr_set(dev, &attr);
381}
382
383static const struct nla_policy
384switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
385 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
386 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
387 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
388 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
389 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
390 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
391 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
392 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
393 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
394 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
395};
396
397static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
398 struct nlattr *protinfo)
399{
400 struct nlattr *attr;
401 int rem;
402 int err;
403
404 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
405 switchdev_port_bridge_policy);
406 if (err)
407 return err;
408
409 nla_for_each_nested(attr, protinfo, rem) {
410 switch (nla_type(attr)) {
411 case IFLA_BRPORT_LEARNING:
412 err = switchdev_port_br_setflag(dev, attr,
413 BR_LEARNING);
414 break;
415 case IFLA_BRPORT_LEARNING_SYNC:
416 err = switchdev_port_br_setflag(dev, attr,
417 BR_LEARNING_SYNC);
418 break;
419 default:
420 err = -EOPNOTSUPP;
421 break;
422 }
423 if (err)
424 return err;
425 }
426
427 return 0;
428}
429
430static int switchdev_port_br_afspec(struct net_device *dev,
431 struct nlattr *afspec,
432 int (*f)(struct net_device *dev,
433 struct switchdev_obj *obj))
434{
435 struct nlattr *attr;
436 struct bridge_vlan_info *vinfo;
437 struct switchdev_obj obj = {
438 .id = SWITCHDEV_OBJ_PORT_VLAN,
439 };
440 int rem;
441 int err;
442
443 nla_for_each_nested(attr, afspec, rem) {
444 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
445 continue;
446 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
447 return -EINVAL;
448 vinfo = nla_data(attr);
449 obj.vlan.flags = vinfo->flags;
450 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
451 if (obj.vlan.vid_start)
452 return -EINVAL;
453 obj.vlan.vid_start = vinfo->vid;
454 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
455 if (!obj.vlan.vid_start)
456 return -EINVAL;
457 obj.vlan.vid_end = vinfo->vid;
458 if (obj.vlan.vid_end <= obj.vlan.vid_start)
459 return -EINVAL;
460 err = f(dev, &obj);
461 if (err)
462 return err;
463 memset(&obj.vlan, 0, sizeof(obj.vlan));
464 } else {
465 if (obj.vlan.vid_start)
466 return -EINVAL;
467 obj.vlan.vid_start = vinfo->vid;
468 obj.vlan.vid_end = vinfo->vid;
469 err = f(dev, &obj);
470 if (err)
471 return err;
472 memset(&obj.vlan, 0, sizeof(obj.vlan));
473 }
474 }
475
476 return 0;
477}
478
8a44dbb2 479/**
47f8328b 480 * switchdev_port_bridge_setlink - Set bridge port attributes
8a44dbb2
RP
481 *
482 * @dev: port device
47f8328b
SF
483 * @nlh: netlink header
484 * @flags: netlink flags
8a44dbb2 485 *
47f8328b
SF
486 * Called for SELF on rtnl_bridge_setlink to set bridge port
487 * attributes.
8a44dbb2 488 */
ebb9a03a
JP
489int switchdev_port_bridge_setlink(struct net_device *dev,
490 struct nlmsghdr *nlh, u16 flags)
8a44dbb2 491{
47f8328b
SF
492 struct nlattr *protinfo;
493 struct nlattr *afspec;
494 int err = 0;
495
496 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
497 IFLA_PROTINFO);
498 if (protinfo) {
499 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
500 if (err)
501 return err;
502 }
8a44dbb2 503
47f8328b
SF
504 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
505 IFLA_AF_SPEC);
506 if (afspec)
507 err = switchdev_port_br_afspec(dev, afspec,
508 switchdev_port_obj_add);
8a44dbb2 509
47f8328b 510 return err;
8a44dbb2 511}
ebb9a03a 512EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
8a44dbb2
RP
513
514/**
5c34e022 515 * switchdev_port_bridge_dellink - Set bridge port attributes
8a44dbb2
RP
516 *
517 * @dev: port device
5c34e022
SF
518 * @nlh: netlink header
519 * @flags: netlink flags
8a44dbb2 520 *
5c34e022
SF
521 * Called for SELF on rtnl_bridge_dellink to set bridge port
522 * attributes.
8a44dbb2 523 */
ebb9a03a
JP
524int switchdev_port_bridge_dellink(struct net_device *dev,
525 struct nlmsghdr *nlh, u16 flags)
8a44dbb2 526{
5c34e022 527 struct nlattr *afspec;
8a44dbb2 528
5c34e022
SF
529 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
530 IFLA_AF_SPEC);
531 if (afspec)
532 return switchdev_port_br_afspec(dev, afspec,
533 switchdev_port_obj_del);
8a44dbb2 534
5c34e022 535 return 0;
8a44dbb2 536}
ebb9a03a 537EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
8a44dbb2 538
8a44dbb2 539/**
ebb9a03a
JP
540 * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
541 * op for master devices
8a44dbb2
RP
542 *
543 * @dev: port device
544 * @nlh: netlink msg with bridge port attributes
545 * @flags: bridge dellink flags
546 *
547 * Notify master device slaves of bridge port attribute deletes
548 */
ebb9a03a
JP
549int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
550 struct nlmsghdr *nlh, u16 flags)
8a44dbb2
RP
551{
552 struct net_device *lower_dev;
553 struct list_head *iter;
554 int ret = 0, err = 0;
555
556 if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
557 return ret;
558
559 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 560 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
8a44dbb2
RP
561 if (err && err != -EOPNOTSUPP)
562 ret = err;
563 }
564
565 return ret;
566}
ebb9a03a 567EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
5e8d9049 568
ebb9a03a 569static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
b5d6fbde 570{
9d47c0a2 571 const struct switchdev_ops *ops = dev->switchdev_ops;
b5d6fbde
SF
572 struct net_device *lower_dev;
573 struct net_device *port_dev;
574 struct list_head *iter;
575
576 /* Recusively search down until we find a sw port dev.
f8e20a9f 577 * (A sw port dev supports switchdev_port_attr_get).
b5d6fbde
SF
578 */
579
f8e20a9f 580 if (ops && ops->switchdev_port_attr_get)
b5d6fbde
SF
581 return dev;
582
583 netdev_for_each_lower_dev(dev, lower_dev, iter) {
ebb9a03a 584 port_dev = switchdev_get_lowest_dev(lower_dev);
b5d6fbde
SF
585 if (port_dev)
586 return port_dev;
587 }
588
589 return NULL;
590}
591
ebb9a03a 592static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
b5d6fbde 593{
f8e20a9f
SF
594 struct switchdev_attr attr = {
595 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
596 };
597 struct switchdev_attr prev_attr;
b5d6fbde
SF
598 struct net_device *dev = NULL;
599 int nhsel;
600
601 /* For this route, all nexthop devs must be on the same switch. */
602
603 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
604 const struct fib_nh *nh = &fi->fib_nh[nhsel];
605
606 if (!nh->nh_dev)
607 return NULL;
608
ebb9a03a 609 dev = switchdev_get_lowest_dev(nh->nh_dev);
b5d6fbde
SF
610 if (!dev)
611 return NULL;
612
f8e20a9f 613 if (switchdev_port_attr_get(dev, &attr))
b5d6fbde
SF
614 return NULL;
615
616 if (nhsel > 0) {
f8e20a9f 617 if (prev_attr.ppid.id_len != attr.ppid.id_len)
b5d6fbde 618 return NULL;
f8e20a9f
SF
619 if (memcmp(prev_attr.ppid.id, attr.ppid.id,
620 attr.ppid.id_len))
b5d6fbde
SF
621 return NULL;
622 }
623
f8e20a9f 624 prev_attr = attr;
b5d6fbde
SF
625 }
626
627 return dev;
628}
629
5e8d9049 630/**
ebb9a03a 631 * switchdev_fib_ipv4_add - Add IPv4 route entry to switch
5e8d9049
SF
632 *
633 * @dst: route's IPv4 destination address
634 * @dst_len: destination address length (prefix length)
635 * @fi: route FIB info structure
636 * @tos: route TOS
637 * @type: route type
f8f21471 638 * @nlflags: netlink flags passed in (NLM_F_*)
5e8d9049
SF
639 * @tb_id: route table ID
640 *
641 * Add IPv4 route entry to switch device.
642 */
ebb9a03a
JP
643int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
644 u8 tos, u8 type, u32 nlflags, u32 tb_id)
5e8d9049 645{
b5d6fbde 646 struct net_device *dev;
9d47c0a2 647 const struct switchdev_ops *ops;
b5d6fbde
SF
648 int err = 0;
649
8e05fd71
SF
650 /* Don't offload route if using custom ip rules or if
651 * IPv4 FIB offloading has been disabled completely.
652 */
653
e1315db1
SF
654#ifdef CONFIG_IP_MULTIPLE_TABLES
655 if (fi->fib_net->ipv4.fib_has_custom_rules)
656 return 0;
657#endif
658
659 if (fi->fib_net->ipv4.fib_offload_disabled)
104616e7
SF
660 return 0;
661
ebb9a03a 662 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
663 if (!dev)
664 return 0;
9d47c0a2 665 ops = dev->switchdev_ops;
b5d6fbde 666
9d47c0a2
JP
667 if (ops->switchdev_fib_ipv4_add) {
668 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
669 fi, tos, type, nlflags,
670 tb_id);
b5d6fbde
SF
671 if (!err)
672 fi->fib_flags |= RTNH_F_EXTERNAL;
673 }
674
675 return err;
5e8d9049 676}
ebb9a03a 677EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
5e8d9049
SF
678
679/**
ebb9a03a 680 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
5e8d9049
SF
681 *
682 * @dst: route's IPv4 destination address
683 * @dst_len: destination address length (prefix length)
684 * @fi: route FIB info structure
685 * @tos: route TOS
686 * @type: route type
687 * @tb_id: route table ID
688 *
689 * Delete IPv4 route entry from switch device.
690 */
ebb9a03a
JP
691int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
692 u8 tos, u8 type, u32 tb_id)
5e8d9049 693{
b5d6fbde 694 struct net_device *dev;
9d47c0a2 695 const struct switchdev_ops *ops;
b5d6fbde
SF
696 int err = 0;
697
698 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
699 return 0;
700
ebb9a03a 701 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
702 if (!dev)
703 return 0;
9d47c0a2 704 ops = dev->switchdev_ops;
b5d6fbde 705
9d47c0a2
JP
706 if (ops->switchdev_fib_ipv4_del) {
707 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
708 fi, tos, type, tb_id);
b5d6fbde
SF
709 if (!err)
710 fi->fib_flags &= ~RTNH_F_EXTERNAL;
711 }
712
713 return err;
5e8d9049 714}
ebb9a03a 715EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
8e05fd71
SF
716
717/**
ebb9a03a 718 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
8e05fd71
SF
719 *
720 * @fi: route FIB info structure
721 */
ebb9a03a 722void switchdev_fib_ipv4_abort(struct fib_info *fi)
8e05fd71
SF
723{
724 /* There was a problem installing this route to the offload
725 * device. For now, until we come up with more refined
726 * policy handling, abruptly end IPv4 fib offloading for
727 * for entire net by flushing offload device(s) of all
728 * IPv4 routes, and mark IPv4 fib offloading broken from
729 * this point forward.
730 */
731
732 fib_flush_external(fi->fib_net);
733 fi->fib_net->ipv4.fib_offload_disabled = true;
734}
ebb9a03a 735EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
This page took 0.083752 seconds and 5 git commands to generate.