Merge branch 'tcp_mem_pressure'
[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
22c1f67e
SF
191static int __switchdev_port_obj_add(struct net_device *dev,
192 struct switchdev_obj *obj)
491d0f15
SF
193{
194 const struct switchdev_ops *ops = dev->switchdev_ops;
195 struct net_device *lower_dev;
196 struct list_head *iter;
197 int err = -EOPNOTSUPP;
198
199 if (ops && ops->switchdev_port_obj_add)
200 return ops->switchdev_port_obj_add(dev, obj);
201
202 /* Switch device port(s) may be stacked under
203 * bond/team/vlan dev, so recurse down to add object on
204 * each port.
205 */
206
207 netdev_for_each_lower_dev(dev, lower_dev, iter) {
208 err = __switchdev_port_obj_add(lower_dev, obj);
209 if (err)
210 break;
211 }
212
213 return err;
214}
215
216/**
217 * switchdev_port_obj_add - Add port object
218 *
219 * @dev: port device
220 * @obj: object to add
221 *
222 * Use a 2-phase prepare-commit transaction model to ensure
223 * system is not left in a partially updated state due to
224 * failure from driver/device.
225 *
226 * rtnl_lock must be held.
227 */
228int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
229{
230 int err;
231
232 ASSERT_RTNL();
233
234 /* Phase I: prepare for obj add. Driver/device should fail
235 * here if there are going to be issues in the commit phase,
236 * such as lack of resources or support. The driver/device
237 * should reserve resources needed for the commit phase here,
238 * but should not commit the obj.
239 */
240
241 obj->trans = SWITCHDEV_TRANS_PREPARE;
242 err = __switchdev_port_obj_add(dev, obj);
243 if (err) {
244 /* Prepare phase failed: abort the transaction. Any
245 * resources reserved in the prepare phase are
246 * released.
247 */
248
249 obj->trans = SWITCHDEV_TRANS_ABORT;
250 __switchdev_port_obj_add(dev, obj);
251
252 return err;
253 }
254
255 /* Phase II: commit obj add. This cannot fail as a fault
256 * of driver/device. If it does, it's a bug in the driver/device
257 * because the driver said everythings was OK in phase I.
258 */
259
260 obj->trans = SWITCHDEV_TRANS_COMMIT;
261 err = __switchdev_port_obj_add(dev, obj);
262 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
263
264 return err;
265}
266EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
267
268/**
269 * switchdev_port_obj_del - Delete port object
270 *
271 * @dev: port device
272 * @obj: object to delete
273 */
274int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
275{
276 const struct switchdev_ops *ops = dev->switchdev_ops;
277 struct net_device *lower_dev;
278 struct list_head *iter;
279 int err = -EOPNOTSUPP;
280
281 if (ops && ops->switchdev_port_obj_del)
282 return ops->switchdev_port_obj_del(dev, obj);
283
284 /* Switch device port(s) may be stacked under
285 * bond/team/vlan dev, so recurse down to delete object on
286 * each port.
287 */
288
289 netdev_for_each_lower_dev(dev, lower_dev, iter) {
290 err = switchdev_port_obj_del(lower_dev, obj);
291 if (err)
292 break;
293 }
294
295 return err;
296}
297EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
298
ebb9a03a
JP
299static DEFINE_MUTEX(switchdev_mutex);
300static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
03bf0c28
JP
301
302/**
ebb9a03a 303 * register_switchdev_notifier - Register notifier
03bf0c28
JP
304 * @nb: notifier_block
305 *
306 * Register switch device notifier. This should be used by code
307 * which needs to monitor events happening in particular device.
308 * Return values are same as for atomic_notifier_chain_register().
309 */
ebb9a03a 310int register_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
311{
312 int err;
313
ebb9a03a
JP
314 mutex_lock(&switchdev_mutex);
315 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
316 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
317 return err;
318}
ebb9a03a 319EXPORT_SYMBOL_GPL(register_switchdev_notifier);
03bf0c28
JP
320
321/**
ebb9a03a 322 * unregister_switchdev_notifier - Unregister notifier
03bf0c28
JP
323 * @nb: notifier_block
324 *
325 * Unregister switch device notifier.
326 * Return values are same as for atomic_notifier_chain_unregister().
327 */
ebb9a03a 328int unregister_switchdev_notifier(struct notifier_block *nb)
03bf0c28
JP
329{
330 int err;
331
ebb9a03a
JP
332 mutex_lock(&switchdev_mutex);
333 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
334 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
335 return err;
336}
ebb9a03a 337EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
03bf0c28
JP
338
339/**
ebb9a03a 340 * call_switchdev_notifiers - Call notifiers
03bf0c28
JP
341 * @val: value passed unmodified to notifier function
342 * @dev: port device
343 * @info: notifier information data
344 *
345 * Call all network notifier blocks. This should be called by driver
346 * when it needs to propagate hardware event.
347 * Return values are same as for atomic_notifier_call_chain().
348 */
ebb9a03a
JP
349int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
350 struct switchdev_notifier_info *info)
03bf0c28
JP
351{
352 int err;
353
354 info->dev = dev;
ebb9a03a
JP
355 mutex_lock(&switchdev_mutex);
356 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
357 mutex_unlock(&switchdev_mutex);
03bf0c28
JP
358 return err;
359}
ebb9a03a 360EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
8a44dbb2 361
8793d0a6
SF
362/**
363 * switchdev_port_bridge_getlink - Get bridge port attributes
364 *
365 * @dev: port device
366 *
367 * Called for SELF on rtnl_bridge_getlink to get bridge port
368 * attributes.
369 */
370int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
371 struct net_device *dev, u32 filter_mask,
372 int nlflags)
373{
374 struct switchdev_attr attr = {
375 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
376 };
377 u16 mode = BRIDGE_MODE_UNDEF;
378 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
379 int err;
380
381 err = switchdev_port_attr_get(dev, &attr);
382 if (err)
383 return err;
384
385 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
42275bd8 386 attr.u.brport_flags, mask, nlflags);
8793d0a6
SF
387}
388EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
389
47f8328b
SF
390static int switchdev_port_br_setflag(struct net_device *dev,
391 struct nlattr *nlattr,
392 unsigned long brport_flag)
393{
394 struct switchdev_attr attr = {
395 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
396 };
397 u8 flag = nla_get_u8(nlattr);
398 int err;
399
400 err = switchdev_port_attr_get(dev, &attr);
401 if (err)
402 return err;
403
404 if (flag)
42275bd8 405 attr.u.brport_flags |= brport_flag;
47f8328b 406 else
42275bd8 407 attr.u.brport_flags &= ~brport_flag;
47f8328b
SF
408
409 return switchdev_port_attr_set(dev, &attr);
410}
411
412static const struct nla_policy
413switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
414 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
415 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
416 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
417 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
418 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
419 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
420 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
421 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
422 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
423 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
424};
425
426static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
427 struct nlattr *protinfo)
428{
429 struct nlattr *attr;
430 int rem;
431 int err;
432
433 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
434 switchdev_port_bridge_policy);
435 if (err)
436 return err;
437
438 nla_for_each_nested(attr, protinfo, rem) {
439 switch (nla_type(attr)) {
440 case IFLA_BRPORT_LEARNING:
441 err = switchdev_port_br_setflag(dev, attr,
442 BR_LEARNING);
443 break;
444 case IFLA_BRPORT_LEARNING_SYNC:
445 err = switchdev_port_br_setflag(dev, attr,
446 BR_LEARNING_SYNC);
447 break;
448 default:
449 err = -EOPNOTSUPP;
450 break;
451 }
452 if (err)
453 return err;
454 }
455
456 return 0;
457}
458
459static int switchdev_port_br_afspec(struct net_device *dev,
460 struct nlattr *afspec,
461 int (*f)(struct net_device *dev,
462 struct switchdev_obj *obj))
463{
464 struct nlattr *attr;
465 struct bridge_vlan_info *vinfo;
466 struct switchdev_obj obj = {
467 .id = SWITCHDEV_OBJ_PORT_VLAN,
468 };
42275bd8 469 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
47f8328b
SF
470 int rem;
471 int err;
472
473 nla_for_each_nested(attr, afspec, rem) {
474 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
475 continue;
476 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
477 return -EINVAL;
478 vinfo = nla_data(attr);
42275bd8 479 vlan->flags = vinfo->flags;
47f8328b 480 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
42275bd8 481 if (vlan->vid_start)
47f8328b 482 return -EINVAL;
42275bd8 483 vlan->vid_start = vinfo->vid;
47f8328b 484 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
42275bd8 485 if (!vlan->vid_start)
47f8328b 486 return -EINVAL;
42275bd8
SF
487 vlan->vid_end = vinfo->vid;
488 if (vlan->vid_end <= vlan->vid_start)
47f8328b
SF
489 return -EINVAL;
490 err = f(dev, &obj);
491 if (err)
492 return err;
42275bd8 493 memset(vlan, 0, sizeof(*vlan));
47f8328b 494 } else {
42275bd8 495 if (vlan->vid_start)
47f8328b 496 return -EINVAL;
42275bd8
SF
497 vlan->vid_start = vinfo->vid;
498 vlan->vid_end = vinfo->vid;
47f8328b
SF
499 err = f(dev, &obj);
500 if (err)
501 return err;
42275bd8 502 memset(vlan, 0, sizeof(*vlan));
47f8328b
SF
503 }
504 }
505
506 return 0;
507}
508
8a44dbb2 509/**
47f8328b 510 * switchdev_port_bridge_setlink - Set bridge port attributes
8a44dbb2
RP
511 *
512 * @dev: port device
47f8328b
SF
513 * @nlh: netlink header
514 * @flags: netlink flags
8a44dbb2 515 *
47f8328b
SF
516 * Called for SELF on rtnl_bridge_setlink to set bridge port
517 * attributes.
8a44dbb2 518 */
ebb9a03a
JP
519int switchdev_port_bridge_setlink(struct net_device *dev,
520 struct nlmsghdr *nlh, u16 flags)
8a44dbb2 521{
47f8328b
SF
522 struct nlattr *protinfo;
523 struct nlattr *afspec;
524 int err = 0;
525
526 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
527 IFLA_PROTINFO);
528 if (protinfo) {
529 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
530 if (err)
531 return err;
532 }
8a44dbb2 533
47f8328b
SF
534 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
535 IFLA_AF_SPEC);
536 if (afspec)
537 err = switchdev_port_br_afspec(dev, afspec,
538 switchdev_port_obj_add);
8a44dbb2 539
47f8328b 540 return err;
8a44dbb2 541}
ebb9a03a 542EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
8a44dbb2
RP
543
544/**
5c34e022 545 * switchdev_port_bridge_dellink - Set bridge port attributes
8a44dbb2
RP
546 *
547 * @dev: port device
5c34e022
SF
548 * @nlh: netlink header
549 * @flags: netlink flags
8a44dbb2 550 *
5c34e022
SF
551 * Called for SELF on rtnl_bridge_dellink to set bridge port
552 * attributes.
8a44dbb2 553 */
ebb9a03a
JP
554int switchdev_port_bridge_dellink(struct net_device *dev,
555 struct nlmsghdr *nlh, u16 flags)
8a44dbb2 556{
5c34e022 557 struct nlattr *afspec;
8a44dbb2 558
5c34e022
SF
559 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
560 IFLA_AF_SPEC);
561 if (afspec)
562 return switchdev_port_br_afspec(dev, afspec,
563 switchdev_port_obj_del);
8a44dbb2 564
5c34e022 565 return 0;
8a44dbb2 566}
ebb9a03a 567EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
8a44dbb2 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) {
42275bd8 617 if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len)
b5d6fbde 618 return NULL;
42275bd8
SF
619 if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id,
620 attr.u.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{
58c2cb16
SF
646 struct switchdev_obj fib_obj = {
647 .id = SWITCHDEV_OBJ_IPV4_FIB,
42275bd8 648 .u.ipv4_fib = {
7a7ee531 649 .dst = dst,
58c2cb16
SF
650 .dst_len = dst_len,
651 .fi = fi,
652 .tos = tos,
653 .type = type,
654 .nlflags = nlflags,
655 .tb_id = tb_id,
656 },
657 };
b5d6fbde 658 struct net_device *dev;
b5d6fbde
SF
659 int err = 0;
660
8e05fd71
SF
661 /* Don't offload route if using custom ip rules or if
662 * IPv4 FIB offloading has been disabled completely.
663 */
664
e1315db1
SF
665#ifdef CONFIG_IP_MULTIPLE_TABLES
666 if (fi->fib_net->ipv4.fib_has_custom_rules)
667 return 0;
668#endif
669
670 if (fi->fib_net->ipv4.fib_offload_disabled)
104616e7
SF
671 return 0;
672
ebb9a03a 673 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
674 if (!dev)
675 return 0;
58c2cb16
SF
676
677 err = switchdev_port_obj_add(dev, &fib_obj);
678 if (!err)
679 fi->fib_flags |= RTNH_F_EXTERNAL;
b5d6fbde
SF
680
681 return err;
5e8d9049 682}
ebb9a03a 683EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
5e8d9049
SF
684
685/**
ebb9a03a 686 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
5e8d9049
SF
687 *
688 * @dst: route's IPv4 destination address
689 * @dst_len: destination address length (prefix length)
690 * @fi: route FIB info structure
691 * @tos: route TOS
692 * @type: route type
693 * @tb_id: route table ID
694 *
695 * Delete IPv4 route entry from switch device.
696 */
ebb9a03a
JP
697int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
698 u8 tos, u8 type, u32 tb_id)
5e8d9049 699{
58c2cb16
SF
700 struct switchdev_obj fib_obj = {
701 .id = SWITCHDEV_OBJ_IPV4_FIB,
42275bd8 702 .u.ipv4_fib = {
7a7ee531 703 .dst = dst,
58c2cb16
SF
704 .dst_len = dst_len,
705 .fi = fi,
706 .tos = tos,
707 .type = type,
708 .nlflags = 0,
709 .tb_id = tb_id,
710 },
711 };
b5d6fbde 712 struct net_device *dev;
b5d6fbde
SF
713 int err = 0;
714
715 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
716 return 0;
717
ebb9a03a 718 dev = switchdev_get_dev_by_nhs(fi);
b5d6fbde
SF
719 if (!dev)
720 return 0;
b5d6fbde 721
58c2cb16
SF
722 err = switchdev_port_obj_del(dev, &fib_obj);
723 if (!err)
724 fi->fib_flags &= ~RTNH_F_EXTERNAL;
b5d6fbde
SF
725
726 return err;
5e8d9049 727}
ebb9a03a 728EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
8e05fd71
SF
729
730/**
ebb9a03a 731 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
8e05fd71
SF
732 *
733 * @fi: route FIB info structure
734 */
ebb9a03a 735void switchdev_fib_ipv4_abort(struct fib_info *fi)
8e05fd71
SF
736{
737 /* There was a problem installing this route to the offload
738 * device. For now, until we come up with more refined
739 * policy handling, abruptly end IPv4 fib offloading for
740 * for entire net by flushing offload device(s) of all
741 * IPv4 routes, and mark IPv4 fib offloading broken from
742 * this point forward.
743 */
744
745 fib_flush_external(fi->fib_net);
746 fi->fib_net->ipv4.fib_offload_disabled = true;
747}
ebb9a03a 748EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
This page took 0.085946 seconds and 5 git commands to generate.