blob: 8e5ead366e7f93988a50589365eee758db12a694 [file] [log] [blame]
David Ahern1b69c6d2015-09-29 20:07:11 -07001/*
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
20int l3mdev_master_ifindex_rcu(struct net_device *dev)
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 Ahernfee6d4c2015-10-05 08:51:24 -070029 } else if (netif_is_l3_slave(dev)) {
David Ahern1b69c6d2015-09-29 20:07:11 -070030 struct net_device *master;
31
32 master = netdev_master_upper_dev_get_rcu(dev);
David Ahernfee6d4c2015-10-05 08:51:24 -070033 if (master)
David Ahern1b69c6d2015-09-29 20:07:11 -070034 ifindex = master->ifindex;
35 }
36
37 return ifindex;
38}
39EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);
40
41/**
42 * l3mdev_fib_table - get FIB table id associated with an L3
43 * master interface
44 * @dev: targeted interface
45 */
46
47u32 l3mdev_fib_table_rcu(const struct net_device *dev)
48{
49 u32 tb_id = 0;
50
51 if (!dev)
52 return 0;
53
54 if (netif_is_l3_master(dev)) {
55 if (dev->l3mdev_ops->l3mdev_fib_table)
56 tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev);
David Ahernfee6d4c2015-10-05 08:51:24 -070057 } else if (netif_is_l3_slave(dev)) {
David Ahern1b69c6d2015-09-29 20:07:11 -070058 /* Users of netdev_master_upper_dev_get_rcu need non-const,
59 * but current inet_*type functions take a const
60 */
61 struct net_device *_dev = (struct net_device *) dev;
62 const struct net_device *master;
63
64 master = netdev_master_upper_dev_get_rcu(_dev);
David Ahernfee6d4c2015-10-05 08:51:24 -070065 if (master &&
David Ahern1b69c6d2015-09-29 20:07:11 -070066 master->l3mdev_ops->l3mdev_fib_table)
67 tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
68 }
69
70 return tb_id;
71}
72EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
73
74u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
75{
76 struct net_device *dev;
77 u32 tb_id = 0;
78
79 if (!ifindex)
80 return 0;
81
82 rcu_read_lock();
83
84 dev = dev_get_by_index_rcu(net, ifindex);
85 if (dev)
86 tb_id = l3mdev_fib_table_rcu(dev);
87
88 rcu_read_unlock();
89
90 return tb_id;
91}
92EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);