2 * include/net/l3mdev.h - L3 master device API
3 * Copyright (c) 2015 Cumulus Networks
4 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
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.
11 #ifndef _NET_L3MDEV_H_
12 #define _NET_L3MDEV_H_
14 #include <net/fib_rules.h>
17 * struct l3mdev_ops - l3mdev operations
19 * @l3mdev_fib_table: Get FIB table id to use for lookups
21 * @l3mdev_get_rtable: Get cached IPv4 rtable (dst_entry) for device
23 * @l3mdev_get_saddr: Get source address for a flow
25 * @l3mdev_get_rt6_dst: Get cached IPv6 rt6_info (dst_entry) for device
29 u32 (*l3mdev_fib_table)(const struct net_device *dev);
30 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
31 struct sk_buff *skb, u16 proto);
34 struct rtable * (*l3mdev_get_rtable)(const struct net_device *dev,
35 const struct flowi4 *fl4);
36 int (*l3mdev_get_saddr)(struct net_device *dev,
40 struct dst_entry * (*l3mdev_get_rt6_dst)(const struct net_device *dev,
42 int (*l3mdev_get_saddr6)(struct net_device *dev,
43 const struct sock *sk,
47 #ifdef CONFIG_NET_L3_MASTER_DEV
49 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
50 struct fib_lookup_arg *arg);
52 void l3mdev_update_flow(struct net *net, struct flowi *fl);
54 int l3mdev_master_ifindex_rcu(const struct net_device *dev);
55 static inline int l3mdev_master_ifindex(struct net_device *dev)
60 ifindex = l3mdev_master_ifindex_rcu(dev);
66 static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
68 struct net_device *dev;
71 if (likely(ifindex)) {
74 dev = dev_get_by_index_rcu(net, ifindex);
76 rc = l3mdev_master_ifindex_rcu(dev);
85 const struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
87 /* netdev_master_upper_dev_get_rcu calls
88 * list_first_or_null_rcu to walk the upper dev list.
89 * list_first_or_null_rcu does not handle a const arg. We aren't
90 * making changes, just want the master device from that list so
91 * typecast to remove the const
93 struct net_device *dev = (struct net_device *)_dev;
94 const struct net_device *master;
99 if (netif_is_l3_master(dev))
101 else if (netif_is_l3_slave(dev))
102 master = netdev_master_upper_dev_get_rcu(dev);
109 /* get index of an interface to use for FIB lookups. For devices
110 * enslaved to an L3 master device FIB lookups are based on the
113 static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
115 return l3mdev_master_ifindex_rcu(dev) ? : dev->ifindex;
118 static inline int l3mdev_fib_oif(struct net_device *dev)
123 oif = l3mdev_fib_oif_rcu(dev);
129 u32 l3mdev_fib_table_rcu(const struct net_device *dev);
130 u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
131 static inline u32 l3mdev_fib_table(const struct net_device *dev)
136 tb_id = l3mdev_fib_table_rcu(dev);
142 static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
143 const struct flowi4 *fl4)
145 if (netif_is_l3_master(dev) && dev->l3mdev_ops->l3mdev_get_rtable)
146 return dev->l3mdev_ops->l3mdev_get_rtable(dev, fl4);
151 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
153 struct net_device *dev;
161 dev = dev_get_by_index_rcu(net, ifindex);
163 rc = netif_is_l3_master(dev);
170 int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4);
172 struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6);
173 int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
177 struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
179 struct net_device *master = NULL;
181 if (netif_is_l3_slave(skb->dev))
182 master = netdev_master_upper_dev_get_rcu(skb->dev);
183 else if (netif_is_l3_master(skb->dev))
186 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
187 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
193 struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
195 return l3mdev_l3_rcv(skb, AF_INET);
199 struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
201 return l3mdev_l3_rcv(skb, AF_INET6);
206 static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
210 static inline int l3mdev_master_ifindex(struct net_device *dev)
215 static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
221 const struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
226 static inline int l3mdev_fib_oif_rcu(struct net_device *dev)
228 return dev ? dev->ifindex : 0;
230 static inline int l3mdev_fib_oif(struct net_device *dev)
232 return dev ? dev->ifindex : 0;
235 static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
239 static inline u32 l3mdev_fib_table(const struct net_device *dev)
243 static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
248 static inline struct rtable *l3mdev_get_rtable(const struct net_device *dev,
249 const struct flowi4 *fl4)
254 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
259 static inline int l3mdev_get_saddr(struct net *net, int ifindex,
266 struct dst_entry *l3mdev_get_rt6_dst(struct net *net, struct flowi6 *fl6)
271 static inline int l3mdev_get_saddr6(struct net *net, const struct sock *sk,
278 struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
284 struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
290 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
291 struct fib_lookup_arg *arg)
296 void l3mdev_update_flow(struct net *net, struct flowi *fl)
301 #endif /* _NET_L3MDEV_H_ */