David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/l3mdev/l3mdev.c - L3 master device implementation |
| 3 | * Copyright (c) 2015 Cumulus Networks |
| 4 | * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com> |
| 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/netdevice.h> |
| 13 | #include <net/l3mdev.h> |
| 14 | |
| 15 | /** |
| 16 | * l3mdev_master_ifindex - get index of L3 master device |
| 17 | * @dev: targeted interface |
| 18 | */ |
| 19 | |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 20 | int l3mdev_master_ifindex_rcu(const struct net_device *dev) |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 21 | { |
| 22 | int ifindex = 0; |
| 23 | |
| 24 | if (!dev) |
| 25 | return 0; |
| 26 | |
| 27 | if (netif_is_l3_master(dev)) { |
| 28 | ifindex = dev->ifindex; |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 29 | } else if (netif_is_l3_slave(dev)) { |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 30 | struct net_device *master; |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 31 | struct net_device *_dev = (struct net_device *)dev; |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 32 | |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 33 | /* netdev_master_upper_dev_get_rcu calls |
| 34 | * list_first_or_null_rcu to walk the upper dev list. |
| 35 | * list_first_or_null_rcu does not handle a const arg. We aren't |
| 36 | * making changes, just want the master device from that list so |
| 37 | * typecast to remove the const |
| 38 | */ |
| 39 | master = netdev_master_upper_dev_get_rcu(_dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 40 | if (master) |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 41 | ifindex = master->ifindex; |
| 42 | } |
| 43 | |
| 44 | return ifindex; |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu); |
| 47 | |
| 48 | /** |
| 49 | * l3mdev_fib_table - get FIB table id associated with an L3 |
| 50 | * master interface |
| 51 | * @dev: targeted interface |
| 52 | */ |
| 53 | |
| 54 | u32 l3mdev_fib_table_rcu(const struct net_device *dev) |
| 55 | { |
| 56 | u32 tb_id = 0; |
| 57 | |
| 58 | if (!dev) |
| 59 | return 0; |
| 60 | |
| 61 | if (netif_is_l3_master(dev)) { |
| 62 | if (dev->l3mdev_ops->l3mdev_fib_table) |
| 63 | tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 64 | } else if (netif_is_l3_slave(dev)) { |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 65 | /* Users of netdev_master_upper_dev_get_rcu need non-const, |
| 66 | * but current inet_*type functions take a const |
| 67 | */ |
| 68 | struct net_device *_dev = (struct net_device *) dev; |
| 69 | const struct net_device *master; |
| 70 | |
| 71 | master = netdev_master_upper_dev_get_rcu(_dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 72 | if (master && |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 73 | master->l3mdev_ops->l3mdev_fib_table) |
| 74 | tb_id = master->l3mdev_ops->l3mdev_fib_table(master); |
| 75 | } |
| 76 | |
| 77 | return tb_id; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu); |
| 80 | |
| 81 | u32 l3mdev_fib_table_by_index(struct net *net, int ifindex) |
| 82 | { |
| 83 | struct net_device *dev; |
| 84 | u32 tb_id = 0; |
| 85 | |
| 86 | if (!ifindex) |
| 87 | return 0; |
| 88 | |
| 89 | rcu_read_lock(); |
| 90 | |
| 91 | dev = dev_get_by_index_rcu(net, ifindex); |
| 92 | if (dev) |
| 93 | tb_id = l3mdev_fib_table_rcu(dev); |
| 94 | |
| 95 | rcu_read_unlock(); |
| 96 | |
| 97 | return tb_id; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index); |