Commit 02d793c5 authored by WANG Cong's avatar WANG Cong Committed by David S. Miller

fou: add network namespace support

Also convert the spinlock to a mutex.

Cc: Tom Herbert <tom@herbertland.com>
Signed-off-by: default avatarCong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 4cbcdf2b
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
#include <uapi/linux/fou.h> #include <uapi/linux/fou.h>
#include <uapi/linux/genetlink.h> #include <uapi/linux/genetlink.h>
static DEFINE_SPINLOCK(fou_lock);
static LIST_HEAD(fou_list);
struct fou { struct fou {
struct socket *sock; struct socket *sock;
u8 protocol; u8 protocol;
...@@ -37,6 +34,13 @@ struct fou_cfg { ...@@ -37,6 +34,13 @@ struct fou_cfg {
struct udp_port_cfg udp_config; struct udp_port_cfg udp_config;
}; };
static unsigned int fou_net_id;
struct fou_net {
struct list_head fou_list;
struct mutex fou_lock;
};
static inline struct fou *fou_from_sock(struct sock *sk) static inline struct fou *fou_from_sock(struct sock *sk)
{ {
return sk->sk_user_data; return sk->sk_user_data;
...@@ -387,20 +391,21 @@ static int gue_gro_complete(struct sk_buff *skb, int nhoff, ...@@ -387,20 +391,21 @@ static int gue_gro_complete(struct sk_buff *skb, int nhoff,
return err; return err;
} }
static int fou_add_to_port_list(struct fou *fou) static int fou_add_to_port_list(struct net *net, struct fou *fou)
{ {
struct fou_net *fn = net_generic(net, fou_net_id);
struct fou *fout; struct fou *fout;
spin_lock(&fou_lock); mutex_lock(&fn->fou_lock);
list_for_each_entry(fout, &fou_list, list) { list_for_each_entry(fout, &fn->fou_list, list) {
if (fou->port == fout->port) { if (fou->port == fout->port) {
spin_unlock(&fou_lock); mutex_unlock(&fn->fou_lock);
return -EALREADY; return -EALREADY;
} }
} }
list_add(&fou->list, &fou_list); list_add(&fou->list, &fn->fou_list);
spin_unlock(&fou_lock); mutex_unlock(&fn->fou_lock);
return 0; return 0;
} }
...@@ -412,13 +417,8 @@ static void fou_release(struct fou *fou) ...@@ -412,13 +417,8 @@ static void fou_release(struct fou *fou)
if (sk->sk_family == AF_INET) if (sk->sk_family == AF_INET)
udp_del_offload(&fou->udp_offloads); udp_del_offload(&fou->udp_offloads);
list_del(&fou->list); list_del(&fou->list);
udp_tunnel_sock_release(sock);
/* Remove hooks into tunnel socket */
sk->sk_user_data = NULL;
sock_release(sock);
kfree(fou); kfree(fou);
} }
...@@ -448,10 +448,10 @@ static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg) ...@@ -448,10 +448,10 @@ static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
static int fou_create(struct net *net, struct fou_cfg *cfg, static int fou_create(struct net *net, struct fou_cfg *cfg,
struct socket **sockp) struct socket **sockp)
{ {
struct fou *fou = NULL;
int err;
struct socket *sock = NULL; struct socket *sock = NULL;
struct fou *fou = NULL;
struct sock *sk; struct sock *sk;
int err;
/* Open UDP socket */ /* Open UDP socket */
err = udp_sock_create(net, &cfg->udp_config, &sock); err = udp_sock_create(net, &cfg->udp_config, &sock);
...@@ -503,7 +503,7 @@ static int fou_create(struct net *net, struct fou_cfg *cfg, ...@@ -503,7 +503,7 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
goto error; goto error;
} }
err = fou_add_to_port_list(fou); err = fou_add_to_port_list(net, fou);
if (err) if (err)
goto error; goto error;
...@@ -515,26 +515,27 @@ static int fou_create(struct net *net, struct fou_cfg *cfg, ...@@ -515,26 +515,27 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
error: error:
kfree(fou); kfree(fou);
if (sock) if (sock)
sock_release(sock); udp_tunnel_sock_release(sock);
return err; return err;
} }
static int fou_destroy(struct net *net, struct fou_cfg *cfg) static int fou_destroy(struct net *net, struct fou_cfg *cfg)
{ {
struct fou *fou; struct fou_net *fn = net_generic(net, fou_net_id);
__be16 port = cfg->udp_config.local_udp_port; __be16 port = cfg->udp_config.local_udp_port;
int err = -EINVAL; int err = -EINVAL;
struct fou *fou;
spin_lock(&fou_lock); mutex_lock(&fn->fou_lock);
list_for_each_entry(fou, &fou_list, list) { list_for_each_entry(fou, &fn->fou_list, list) {
if (fou->port == port) { if (fou->port == port) {
fou_release(fou); fou_release(fou);
err = 0; err = 0;
break; break;
} }
} }
spin_unlock(&fou_lock); mutex_unlock(&fn->fou_lock);
return err; return err;
} }
...@@ -592,6 +593,7 @@ static int parse_nl_config(struct genl_info *info, ...@@ -592,6 +593,7 @@ static int parse_nl_config(struct genl_info *info,
static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info) static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
{ {
struct net *net = genl_info_net(info);
struct fou_cfg cfg; struct fou_cfg cfg;
int err; int err;
...@@ -599,11 +601,12 @@ static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info) ...@@ -599,11 +601,12 @@ static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
if (err) if (err)
return err; return err;
return fou_create(&init_net, &cfg, NULL); return fou_create(net, &cfg, NULL);
} }
static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info) static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
{ {
struct net *net = genl_info_net(info);
struct fou_cfg cfg; struct fou_cfg cfg;
int err; int err;
...@@ -611,7 +614,7 @@ static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info) ...@@ -611,7 +614,7 @@ static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
if (err) if (err)
return err; return err;
return fou_destroy(&init_net, &cfg); return fou_destroy(net, &cfg);
} }
static const struct genl_ops fou_nl_ops[] = { static const struct genl_ops fou_nl_ops[] = {
...@@ -823,38 +826,63 @@ static void ip_tunnel_encap_del_fou_ops(void) ...@@ -823,38 +826,63 @@ static void ip_tunnel_encap_del_fou_ops(void)
#endif #endif
static __net_init int fou_init_net(struct net *net)
{
struct fou_net *fn = net_generic(net, fou_net_id);
INIT_LIST_HEAD(&fn->fou_list);
mutex_init(&fn->fou_lock);
return 0;
}
static __net_exit void fou_exit_net(struct net *net)
{
struct fou_net *fn = net_generic(net, fou_net_id);
struct fou *fou, *next;
/* Close all the FOU sockets */
mutex_lock(&fn->fou_lock);
list_for_each_entry_safe(fou, next, &fn->fou_list, list)
fou_release(fou);
mutex_unlock(&fn->fou_lock);
}
static struct pernet_operations fou_net_ops = {
.init = fou_init_net,
.exit = fou_exit_net,
.id = &fou_net_id,
.size = sizeof(struct fou_net),
};
static int __init fou_init(void) static int __init fou_init(void)
{ {
int ret; int ret;
ret = register_pernet_device(&fou_net_ops);
if (ret)
goto exit;
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) if (ret < 0)
goto exit; goto unregister;
ret = ip_tunnel_encap_add_fou_ops(); ret = ip_tunnel_encap_add_fou_ops();
if (ret < 0) if (ret == 0)
genl_unregister_family(&fou_nl_family); return 0;
genl_unregister_family(&fou_nl_family);
unregister:
unregister_pernet_device(&fou_net_ops);
exit: exit:
return ret; return ret;
} }
static void __exit fou_fini(void) static void __exit fou_fini(void)
{ {
struct fou *fou, *next;
ip_tunnel_encap_del_fou_ops(); ip_tunnel_encap_del_fou_ops();
genl_unregister_family(&fou_nl_family); genl_unregister_family(&fou_nl_family);
unregister_pernet_device(&fou_net_ops);
/* Close all the FOU sockets */
spin_lock(&fou_lock);
list_for_each_entry_safe(fou, next, &fou_list, list)
fou_release(fou);
spin_unlock(&fou_lock);
} }
module_init(fou_init); module_init(fou_init);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment