Skip to content

Commit

Permalink
fou: add network namespace support
Browse files Browse the repository at this point in the history
Also convert the spinlock to a mutex.

Cc: Tom Herbert <tom@herbertland.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
WANG Cong authored and David S. Miller committed Apr 13, 2015
1 parent 4cbcdf2 commit 02d793c
Showing 1 changed file with 67 additions and 39 deletions.
106 changes: 67 additions & 39 deletions net/ipv4/fou.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
#include <uapi/linux/fou.h>
#include <uapi/linux/genetlink.h>

static DEFINE_SPINLOCK(fou_lock);
static LIST_HEAD(fou_list);

struct fou {
struct socket *sock;
u8 protocol;
Expand All @@ -37,6 +34,13 @@ struct fou_cfg {
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)
{
return sk->sk_user_data;
Expand Down Expand Up @@ -387,20 +391,21 @@ static int gue_gro_complete(struct sk_buff *skb, int nhoff,
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;

spin_lock(&fou_lock);
list_for_each_entry(fout, &fou_list, list) {
mutex_lock(&fn->fou_lock);
list_for_each_entry(fout, &fn->fou_list, list) {
if (fou->port == fout->port) {
spin_unlock(&fou_lock);
mutex_unlock(&fn->fou_lock);
return -EALREADY;
}
}

list_add(&fou->list, &fou_list);
spin_unlock(&fou_lock);
list_add(&fou->list, &fn->fou_list);
mutex_unlock(&fn->fou_lock);

return 0;
}
Expand All @@ -412,13 +417,8 @@ static void fou_release(struct fou *fou)

if (sk->sk_family == AF_INET)
udp_del_offload(&fou->udp_offloads);

list_del(&fou->list);

/* Remove hooks into tunnel socket */
sk->sk_user_data = NULL;

sock_release(sock);
udp_tunnel_sock_release(sock);

kfree(fou);
}
Expand Down Expand Up @@ -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,
struct socket **sockp)
{
struct fou *fou = NULL;
int err;
struct socket *sock = NULL;
struct fou *fou = NULL;
struct sock *sk;
int err;

/* Open UDP socket */
err = udp_sock_create(net, &cfg->udp_config, &sock);
Expand Down Expand Up @@ -503,7 +503,7 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
goto error;
}

err = fou_add_to_port_list(fou);
err = fou_add_to_port_list(net, fou);
if (err)
goto error;

Expand All @@ -515,26 +515,27 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
error:
kfree(fou);
if (sock)
sock_release(sock);
udp_tunnel_sock_release(sock);

return err;
}

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;
int err = -EINVAL;
struct fou *fou;

spin_lock(&fou_lock);
list_for_each_entry(fou, &fou_list, list) {
mutex_lock(&fn->fou_lock);
list_for_each_entry(fou, &fn->fou_list, list) {
if (fou->port == port) {
fou_release(fou);
err = 0;
break;
}
}
spin_unlock(&fou_lock);
mutex_unlock(&fn->fou_lock);

return err;
}
Expand Down Expand Up @@ -592,26 +593,28 @@ static int parse_nl_config(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;
int err;

err = parse_nl_config(info, &cfg);
if (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)
{
struct net *net = genl_info_net(info);
struct fou_cfg cfg;
int err;

err = parse_nl_config(info, &cfg);
if (err)
return err;

return fou_destroy(&init_net, &cfg);
return fou_destroy(net, &cfg);
}

static const struct genl_ops fou_nl_ops[] = {
Expand Down Expand Up @@ -823,38 +826,63 @@ static void ip_tunnel_encap_del_fou_ops(void)

#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)
{
int ret;

ret = register_pernet_device(&fou_net_ops);
if (ret)
goto exit;

ret = genl_register_family_with_ops(&fou_nl_family,
fou_nl_ops);

if (ret < 0)
goto exit;
goto unregister;

ret = ip_tunnel_encap_add_fou_ops();
if (ret < 0)
genl_unregister_family(&fou_nl_family);
if (ret == 0)
return 0;

genl_unregister_family(&fou_nl_family);
unregister:
unregister_pernet_device(&fou_net_ops);
exit:
return ret;
}

static void __exit fou_fini(void)
{
struct fou *fou, *next;

ip_tunnel_encap_del_fou_ops();

genl_unregister_family(&fou_nl_family);

/* 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);
unregister_pernet_device(&fou_net_ops);
}

module_init(fou_init);
Expand Down

0 comments on commit 02d793c

Please sign in to comment.