提交 a8c5f90f 编写于 作者: T Tom Herbert 提交者: David S. Miller

ip_tunnel: Ops registration for secondary encap (fou, gue)

Instead of calling fou and gue functions directly from ip_tunnel
use ops for these that were previously registered. This patch adds the
logic to add and remove encapsulation operations for ip_tunnel,
and modified fou (and gue) to register with ip_tunnels.

This patch also addresses a circular dependency between ip_tunnel
and fou that was causing link errors when CONFIG_NET_IP_TUNNEL=y
and CONFIG_NET_FOU=m. References to fou an gue have been removed from
ip_tunnel.c
Reported-by: NRandy Dunlap <rdunlap@infradead.org>
Signed-off-by: NTom Herbert <therbert@google.com>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 4243cdc2
...@@ -8,31 +8,12 @@ ...@@ -8,31 +8,12 @@
#include <net/ip_tunnels.h> #include <net/ip_tunnels.h>
#include <net/udp.h> #include <net/udp.h>
size_t fou_encap_hlen(struct ip_tunnel_encap *e);
static size_t gue_encap_hlen(struct ip_tunnel_encap *e);
int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
u8 *protocol, struct flowi4 *fl4); u8 *protocol, struct flowi4 *fl4);
int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
u8 *protocol, struct flowi4 *fl4); u8 *protocol, struct flowi4 *fl4);
static size_t fou_encap_hlen(struct ip_tunnel_encap *e)
{
return sizeof(struct udphdr);
}
static size_t gue_encap_hlen(struct ip_tunnel_encap *e)
{
size_t len;
bool need_priv = false;
len = sizeof(struct udphdr) + sizeof(struct guehdr);
if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
len += GUE_PLEN_REMCSUM;
need_priv = true;
}
len += need_priv ? GUE_LEN_PRIV : 0;
return len;
}
#endif #endif
...@@ -117,6 +117,22 @@ struct ip_tunnel_net { ...@@ -117,6 +117,22 @@ struct ip_tunnel_net {
struct hlist_head tunnels[IP_TNL_HASH_SIZE]; struct hlist_head tunnels[IP_TNL_HASH_SIZE];
}; };
struct ip_tunnel_encap_ops {
size_t (*encap_hlen)(struct ip_tunnel_encap *e);
int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
u8 *protocol, struct flowi4 *fl4);
};
#define MAX_IPTUN_ENCAP_OPS 8
extern const struct ip_tunnel_encap_ops __rcu *
iptun_encaps[MAX_IPTUN_ENCAP_OPS];
int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
unsigned int num);
int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
unsigned int num);
#ifdef CONFIG_INET #ifdef CONFIG_INET
int ip_tunnel_init(struct net_device *dev); int ip_tunnel_init(struct net_device *dev);
......
...@@ -668,6 +668,30 @@ static const struct genl_ops fou_nl_ops[] = { ...@@ -668,6 +668,30 @@ static const struct genl_ops fou_nl_ops[] = {
}, },
}; };
size_t fou_encap_hlen(struct ip_tunnel_encap *e)
{
return sizeof(struct udphdr);
}
EXPORT_SYMBOL(fou_encap_hlen);
size_t gue_encap_hlen(struct ip_tunnel_encap *e)
{
size_t len;
bool need_priv = false;
len = sizeof(struct udphdr) + sizeof(struct guehdr);
if (e->flags & TUNNEL_ENCAP_FLAG_REMCSUM) {
len += GUE_PLEN_REMCSUM;
need_priv = true;
}
len += need_priv ? GUE_LEN_PRIV : 0;
return len;
}
EXPORT_SYMBOL(gue_encap_hlen);
static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e, static void fou_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
struct flowi4 *fl4, u8 *protocol, __be16 sport) struct flowi4 *fl4, u8 *protocol, __be16 sport)
{ {
...@@ -787,6 +811,57 @@ int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e, ...@@ -787,6 +811,57 @@ int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
} }
EXPORT_SYMBOL(gue_build_header); EXPORT_SYMBOL(gue_build_header);
#ifdef CONFIG_NET_FOU_IP_TUNNELS
static const struct ip_tunnel_encap_ops __read_mostly fou_iptun_ops = {
.encap_hlen = fou_encap_hlen,
.build_header = fou_build_header,
};
static const struct ip_tunnel_encap_ops __read_mostly gue_iptun_ops = {
.encap_hlen = gue_encap_hlen,
.build_header = gue_build_header,
};
static int ip_tunnel_encap_add_fou_ops(void)
{
int ret;
ret = ip_tunnel_encap_add_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
if (ret < 0) {
pr_err("can't add fou ops\n");
return ret;
}
ret = ip_tunnel_encap_add_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
if (ret < 0) {
pr_err("can't add gue ops\n");
ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
return ret;
}
return 0;
}
static void ip_tunnel_encap_del_fou_ops(void)
{
ip_tunnel_encap_del_ops(&fou_iptun_ops, TUNNEL_ENCAP_FOU);
ip_tunnel_encap_del_ops(&gue_iptun_ops, TUNNEL_ENCAP_GUE);
}
#else
static int ip_tunnel_encap_add_fou_ops(void)
{
return 0;
}
static int ip_tunnel_encap_del_fou_ops(void)
{
}
#endif
static int __init fou_init(void) static int __init fou_init(void)
{ {
int ret; int ret;
...@@ -794,6 +869,14 @@ static int __init fou_init(void) ...@@ -794,6 +869,14 @@ static int __init fou_init(void)
ret = genl_register_family_with_ops(&fou_nl_family, ret = genl_register_family_with_ops(&fou_nl_family,
fou_nl_ops); fou_nl_ops);
if (ret < 0)
goto exit;
ret = ip_tunnel_encap_add_fou_ops();
if (ret < 0)
genl_unregister_family(&fou_nl_family);
exit:
return ret; return ret;
} }
...@@ -801,6 +884,8 @@ static void __exit fou_fini(void) ...@@ -801,6 +884,8 @@ static void __exit fou_fini(void)
{ {
struct fou *fou, *next; struct fou *fou, *next;
ip_tunnel_encap_del_fou_ops();
genl_unregister_family(&fou_nl_family); genl_unregister_family(&fou_nl_family);
/* Close all the FOU sockets */ /* Close all the FOU sockets */
......
...@@ -57,10 +57,6 @@ ...@@ -57,10 +57,6 @@
#include <net/rtnetlink.h> #include <net/rtnetlink.h>
#include <net/udp.h> #include <net/udp.h>
#if IS_ENABLED(CONFIG_NET_FOU)
#include <net/fou.h>
#endif
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
#include <net/ipv6.h> #include <net/ipv6.h>
#include <net/ip6_fib.h> #include <net/ip6_fib.h>
...@@ -494,19 +490,50 @@ EXPORT_SYMBOL_GPL(ip_tunnel_rcv); ...@@ -494,19 +490,50 @@ EXPORT_SYMBOL_GPL(ip_tunnel_rcv);
static int ip_encap_hlen(struct ip_tunnel_encap *e) static int ip_encap_hlen(struct ip_tunnel_encap *e)
{ {
switch (e->type) { const struct ip_tunnel_encap_ops *ops;
case TUNNEL_ENCAP_NONE: int hlen = -EINVAL;
if (e->type == TUNNEL_ENCAP_NONE)
return 0; return 0;
#if IS_ENABLED(CONFIG_NET_FOU)
case TUNNEL_ENCAP_FOU: if (e->type >= MAX_IPTUN_ENCAP_OPS)
return fou_encap_hlen(e);
case TUNNEL_ENCAP_GUE:
return gue_encap_hlen(e);
#endif
default:
return -EINVAL; return -EINVAL;
}
rcu_read_lock();
ops = rcu_dereference(iptun_encaps[e->type]);
if (likely(ops && ops->encap_hlen))
hlen = ops->encap_hlen(e);
rcu_read_unlock();
return hlen;
}
const struct ip_tunnel_encap_ops __rcu *
iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *ops,
unsigned int num)
{
return !cmpxchg((const struct ip_tunnel_encap_ops **)
&iptun_encaps[num],
NULL, ops) ? 0 : -1;
} }
EXPORT_SYMBOL(ip_tunnel_encap_add_ops);
int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *ops,
unsigned int num)
{
int ret;
ret = (cmpxchg((const struct ip_tunnel_encap_ops **)
&iptun_encaps[num],
ops, NULL) == ops) ? 0 : -1;
synchronize_net();
return ret;
}
EXPORT_SYMBOL(ip_tunnel_encap_del_ops);
int ip_tunnel_encap_setup(struct ip_tunnel *t, int ip_tunnel_encap_setup(struct ip_tunnel *t,
struct ip_tunnel_encap *ipencap) struct ip_tunnel_encap *ipencap)
...@@ -534,18 +561,19 @@ EXPORT_SYMBOL_GPL(ip_tunnel_encap_setup); ...@@ -534,18 +561,19 @@ EXPORT_SYMBOL_GPL(ip_tunnel_encap_setup);
int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t, int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
u8 *protocol, struct flowi4 *fl4) u8 *protocol, struct flowi4 *fl4)
{ {
switch (t->encap.type) { const struct ip_tunnel_encap_ops *ops;
case TUNNEL_ENCAP_NONE: int ret = -EINVAL;
if (t->encap.type == TUNNEL_ENCAP_NONE)
return 0; return 0;
#if IS_ENABLED(CONFIG_NET_FOU)
case TUNNEL_ENCAP_FOU: rcu_read_lock();
return fou_build_header(skb, &t->encap, protocol, fl4); ops = rcu_dereference(iptun_encaps[t->encap.type]);
case TUNNEL_ENCAP_GUE: if (likely(ops && ops->build_header))
return gue_build_header(skb, &t->encap, protocol, fl4); ret = ops->build_header(skb, &t->encap, protocol, fl4);
#endif rcu_read_unlock();
default:
return -EINVAL; return ret;
}
} }
EXPORT_SYMBOL(ip_tunnel_encap); EXPORT_SYMBOL(ip_tunnel_encap);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册