blob: f8a416ec674cd0a4074f7a32a7879475dc2e8aa6 [file] [log] [blame]
David Ahern1b69c6d2015-09-29 20:07:11 -07001/*
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
David Ahern96c63fa2016-06-08 10:55:39 -070014#include <net/fib_rules.h>
15
David Ahern1b69c6d2015-09-29 20:07:11 -070016/**
17 * struct l3mdev_ops - l3mdev operations
18 *
19 * @l3mdev_fib_table: Get FIB table id to use for lookups
20 *
21 * @l3mdev_get_rtable: Get cached IPv4 rtable (dst_entry) for device
David Ahern8cbb512c2015-10-05 08:51:26 -070022 *
23 * @l3mdev_get_saddr: Get source address for a flow
David Ahernccf3c8c2015-10-12 11:47:07 -070024 *
25 * @l3mdev_get_rt6_dst: Get cached IPv6 rt6_info (dst_entry) for device
David Ahern1b69c6d2015-09-29 20:07:11 -070026 */
27
28struct l3mdev_ops {
29 u32 (*l3mdev_fib_table)(const struct net_device *dev);
David Ahern74b20582016-05-10 11:19:50 -070030 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
31 struct sk_buff *skb, u16 proto);
David Ahernccf3c8c2015-10-12 11:47:07 -070032
33 /* IPv4 ops */
David Ahern1b69c6d2015-09-29 20:07:11 -070034 struct rtable * (*l3mdev_get_rtable)(const struct net_device *dev,
35 const struct flowi4 *fl4);
David Ahernb5bdacf2016-01-04 09:09:27 -080036 int (*l3mdev_get_saddr)(struct net_device *dev,
David Ahern8cbb512c2015-10-05 08:51:26 -070037 struct flowi4 *fl4);
David Ahernccf3c8c2015-10-12 11:47:07 -070038
39 /* IPv6 ops */
40 struct dst_entry * (*l3mdev_get_rt6_dst)(const struct net_device *dev,
David Aherncd2a9e62016-06-13 13:44:17 -070041 struct flowi6 *fl6);
David Ahern1b69c6d2015-09-29 20:07:11 -070042};
43
44#ifdef CONFIG_NET_L3_MASTER_DEV
45
David Ahern96c63fa2016-06-08 10:55:39 -070046int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
47 struct fib_lookup_arg *arg);
48
David Ahern3f2fb9a2016-02-24 11:47:02 -080049int l3mdev_master_ifindex_rcu(const struct net_device *dev);
David Ahern1b69c6d2015-09-29 20:07:11 -070050static inline int l3mdev_master_ifindex(struct net_device *dev)
51{
52 int ifindex;
53
54 rcu_read_lock();
55 ifindex = l3mdev_master_ifindex_rcu(dev);
56 rcu_read_unlock();
57
58 return ifindex;
59}
60
David Ahern1a852472015-12-16 13:20:43 -080061static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
62{
63 struct net_device *dev;
64 int rc = 0;
65
66 if (likely(ifindex)) {
67 rcu_read_lock();
68
69 dev = dev_get_by_index_rcu(net, ifindex);
70 if (dev)
71 rc = l3mdev_master_ifindex_rcu(dev);
72
73 rcu_read_unlock();
74 }
75
76 return rc;
77}
78
David Ahern1b69c6d2015-09-29 20:07:11 -070079/* get index of an interface to use for FIB lookups. For devices
80 * enslaved to an L3 master device FIB lookups are based on the
81 * master index
82 */
83static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
84{
85 return l3mdev_master_ifindex_rcu(dev) ? : dev->ifindex;
86}
87
88static inline int l3mdev_fib_oif(struct net_device *dev)
89{
90 int oif;
91
92 rcu_read_lock();
93 oif = l3mdev_fib_oif_rcu(dev);
94 rcu_read_unlock();
95
96 return oif;
97}
98
99u32 l3mdev_fib_table_rcu(const struct net_device *dev);
100u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
101static inline u32 l3mdev_fib_table(const struct net_device *dev)
102{
103 u32 tb_id;
104
105 rcu_read_lock();
106 tb_id = l3mdev_fib_table_rcu(dev);
107 rcu_read_unlock();
108
109 return tb_id;
110}
111
112static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
113 const struct flowi4 *fl4)
114{
115 if (netif_is_l3_master(dev) && dev->l3mdev_ops->l3mdev_get_rtable)
116 return dev->l3mdev_ops->l3mdev_get_rtable(dev, fl4);
117
118 return NULL;
119}
120
David Ahern9478d122015-09-29 20:07:18 -0700121static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
122{
123 struct net_device *dev;
124 bool rc = false;
125
126 if (ifindex == 0)
127 return false;
128
129 rcu_read_lock();
130
131 dev = dev_get_by_index_rcu(net, ifindex);
132 if (dev)
133 rc = netif_is_l3_master(dev);
134
135 rcu_read_unlock();
136
137 return rc;
138}
139
David Ahern4a658962016-05-07 16:48:59 -0700140int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4);
David Ahern8cbb512c2015-10-05 08:51:26 -0700141
David Aherncd2a9e62016-06-13 13:44:17 -0700142struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6);
David Ahernccf3c8c2015-10-12 11:47:07 -0700143
David Ahern74b20582016-05-10 11:19:50 -0700144static inline
145struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
146{
147 struct net_device *master = NULL;
148
149 if (netif_is_l3_slave(skb->dev))
150 master = netdev_master_upper_dev_get_rcu(skb->dev);
151 else if (netif_is_l3_master(skb->dev))
152 master = skb->dev;
153
154 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
155 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
156
157 return skb;
158}
159
160static inline
161struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
162{
163 return l3mdev_l3_rcv(skb, AF_INET);
164}
165
166static inline
167struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
168{
169 return l3mdev_l3_rcv(skb, AF_INET6);
170}
171
David Ahern1b69c6d2015-09-29 20:07:11 -0700172#else
173
David Ahern3f2fb9a2016-02-24 11:47:02 -0800174static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
David Ahern1b69c6d2015-09-29 20:07:11 -0700175{
176 return 0;
177}
178static inline int l3mdev_master_ifindex(struct net_device *dev)
179{
180 return 0;
181}
182
David Ahern1a852472015-12-16 13:20:43 -0800183static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
184{
185 return 0;
186}
187
David Ahern1b69c6d2015-09-29 20:07:11 -0700188static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
189{
190 return dev ? dev->ifindex : 0;
191}
192static inline int l3mdev_fib_oif(struct net_device *dev)
193{
194 return dev ? dev->ifindex : 0;
195}
196
197static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
198{
199 return 0;
200}
201static inline u32 l3mdev_fib_table(const struct net_device *dev)
202{
203 return 0;
204}
205static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
206{
207 return 0;
208}
209
210static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
211 const struct flowi4 *fl4)
212{
213 return NULL;
214}
215
David Ahern9478d122015-09-29 20:07:18 -0700216static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
217{
218 return false;
219}
220
David Ahernb5bdacf2016-01-04 09:09:27 -0800221static inline int l3mdev_get_saddr(struct net *net, int ifindex,
222 struct flowi4 *fl4)
David Ahern8cbb512c2015-10-05 08:51:26 -0700223{
David Ahernb5bdacf2016-01-04 09:09:27 -0800224 return 0;
David Ahern8cbb512c2015-10-05 08:51:26 -0700225}
David Ahernccf3c8c2015-10-12 11:47:07 -0700226
227static inline
David Aherncd2a9e62016-06-13 13:44:17 -0700228struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6)
David Ahernccf3c8c2015-10-12 11:47:07 -0700229{
230 return NULL;
231}
David Ahern74b20582016-05-10 11:19:50 -0700232
233static inline
234struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
235{
236 return skb;
237}
238
239static inline
240struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
241{
242 return skb;
243}
David Ahern96c63fa2016-06-08 10:55:39 -0700244
245static inline
246int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
247 struct fib_lookup_arg *arg)
248{
249 return 1;
250}
David Ahern1b69c6d2015-09-29 20:07:11 -0700251#endif
252
253#endif /* _NET_L3MDEV_H_ */