44a19a1711048edf24e56c4f298f86a7cdd77789
[cascardo/linux.git] / include / net / l3mdev.h
1 /*
2  * include/net/l3mdev.h - L3 master device API
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 #ifndef _NET_L3MDEV_H_
12 #define _NET_L3MDEV_H_
13
14 /**
15  * struct l3mdev_ops - l3mdev operations
16  *
17  * @l3mdev_fib_table: Get FIB table id to use for lookups
18  *
19  * @l3mdev_get_rtable: Get cached IPv4 rtable (dst_entry) for device
20  *
21  * @l3mdev_get_saddr: Get source address for a flow
22  */
23
24 struct l3mdev_ops {
25         u32             (*l3mdev_fib_table)(const struct net_device *dev);
26         struct rtable * (*l3mdev_get_rtable)(const struct net_device *dev,
27                                              const struct flowi4 *fl4);
28         void            (*l3mdev_get_saddr)(struct net_device *dev,
29                                             struct flowi4 *fl4);
30 };
31
32 #ifdef CONFIG_NET_L3_MASTER_DEV
33
34 int l3mdev_master_ifindex_rcu(struct net_device *dev);
35 static inline int l3mdev_master_ifindex(struct net_device *dev)
36 {
37         int ifindex;
38
39         rcu_read_lock();
40         ifindex = l3mdev_master_ifindex_rcu(dev);
41         rcu_read_unlock();
42
43         return ifindex;
44 }
45
46 /* get index of an interface to use for FIB lookups. For devices
47  * enslaved to an L3 master device FIB lookups are based on the
48  * master index
49  */
50 static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
51 {
52         return l3mdev_master_ifindex_rcu(dev) ? : dev->ifindex;
53 }
54
55 static inline int l3mdev_fib_oif(struct net_device *dev)
56 {
57         int oif;
58
59         rcu_read_lock();
60         oif = l3mdev_fib_oif_rcu(dev);
61         rcu_read_unlock();
62
63         return oif;
64 }
65
66 u32 l3mdev_fib_table_rcu(const struct net_device *dev);
67 u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
68 static inline u32 l3mdev_fib_table(const struct net_device *dev)
69 {
70         u32 tb_id;
71
72         rcu_read_lock();
73         tb_id = l3mdev_fib_table_rcu(dev);
74         rcu_read_unlock();
75
76         return tb_id;
77 }
78
79 static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
80                                                const struct flowi4 *fl4)
81 {
82         if (netif_is_l3_master(dev) && dev->l3mdev_ops->l3mdev_get_rtable)
83                 return dev->l3mdev_ops->l3mdev_get_rtable(dev, fl4);
84
85         return NULL;
86 }
87
88 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
89 {
90         struct net_device *dev;
91         bool rc = false;
92
93         if (ifindex == 0)
94                 return false;
95
96         rcu_read_lock();
97
98         dev = dev_get_by_index_rcu(net, ifindex);
99         if (dev)
100                 rc = netif_is_l3_master(dev);
101
102         rcu_read_unlock();
103
104         return rc;
105 }
106
107 static inline void l3mdev_get_saddr(struct net *net, int ifindex,
108                                     struct flowi4 *fl4)
109 {
110         struct net_device *dev;
111
112         if (ifindex) {
113
114                 rcu_read_lock();
115
116                 dev = dev_get_by_index_rcu(net, ifindex);
117                 if (dev && netif_is_l3_master(dev) &&
118                     dev->l3mdev_ops->l3mdev_get_saddr) {
119                         dev->l3mdev_ops->l3mdev_get_saddr(dev, fl4);
120                 }
121
122                 rcu_read_unlock();
123         }
124 }
125
126 #else
127
128 static inline int l3mdev_master_ifindex_rcu(struct net_device *dev)
129 {
130         return 0;
131 }
132 static inline int l3mdev_master_ifindex(struct net_device *dev)
133 {
134         return 0;
135 }
136
137 static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
138 {
139         return dev ? dev->ifindex : 0;
140 }
141 static inline int l3mdev_fib_oif(struct net_device *dev)
142 {
143         return dev ? dev->ifindex : 0;
144 }
145
146 static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
147 {
148         return 0;
149 }
150 static inline u32 l3mdev_fib_table(const struct net_device *dev)
151 {
152         return 0;
153 }
154 static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
155 {
156         return 0;
157 }
158
159 static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
160                                                const struct flowi4 *fl4)
161 {
162         return NULL;
163 }
164
165 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
166 {
167         return false;
168 }
169
170 static inline void l3mdev_get_saddr(struct net *net, int ifindex,
171                                     struct flowi4 *fl4)
172 {
173 }
174 #endif
175
176 #endif /* _NET_L3MDEV_H_ */