2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-27 15:18:06 +00:00

datapath: Sync vxlan tunneling code with upstream ovs-vxlan.

Upstream vxlan implementation was changed according to few
comments.  Following patch brings back those changes to out
of tree ovs module.

Signed-off-by: Pravin B Shelar <pshelar@nicira.com>
Acked-by: Jesse Gross <jesse@nicira.com>
This commit is contained in:
Pravin B Shelar
2013-08-26 11:05:35 -07:00
parent b366c1f436
commit a109c9fbf5
3 changed files with 82 additions and 164 deletions

View File

@@ -5,35 +5,26 @@
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/udp.h> #include <linux/udp.h>
struct vxlan_sock;
typedef void (vxlan_rcv_t)(struct vxlan_sock *vs, struct sk_buff *skb, __be32 key);
/* per UDP socket information */ /* per UDP socket information */
struct vxlan_sock { struct vxlan_sock {
struct hlist_node hlist; struct hlist_node hlist;
struct rcu_head rcu; vxlan_rcv_t *rcv;
struct socket *sock; void *data;
struct list_head handler_list;
};
struct vxlan_handler;
typedef int (vxlan_rcv_t)(struct vxlan_handler *vh, struct sk_buff *skb, __be32 key);
struct vxlan_handler {
vxlan_rcv_t *rcv;
struct list_head node;
void *data;
struct vxlan_sock *vs;
atomic_t refcnt;
struct rcu_head rcu;
struct work_struct del_work; struct work_struct del_work;
int priority; struct socket *sock;
struct rcu_head rcu;
}; };
void vxlan_handler_put(struct vxlan_handler *vh); struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
vxlan_rcv_t *rcv, void *data,
bool no_share);
struct vxlan_handler *vxlan_handler_add(struct net *net, void vxlan_sock_release(struct vxlan_sock *vs);
__be16 portno, vxlan_rcv_t *rcv,
void *data, int priority, bool create);
int vxlan_xmit_skb(struct net *net, struct vxlan_handler *vh, int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
struct rtable *rt, struct sk_buff *skb, struct rtable *rt, struct sk_buff *skb,
__be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
__be16 src_port, __be16 dst_port, __be32 vni); __be16 src_port, __be16 dst_port, __be32 vni);

View File

@@ -59,8 +59,6 @@
#define PORT_HASH_BITS 8 #define PORT_HASH_BITS 8
#define PORT_HASH_SIZE (1<<PORT_HASH_BITS) #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
#define VXLAN_N_VID (1u << 24)
#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
/* IP header + UDP + VXLAN + Ethernet header */ /* IP header + UDP + VXLAN + Ethernet header */
#define VXLAN_HEADROOM (20 + 8 + 8 + 14) #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr)) #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
@@ -75,10 +73,13 @@ struct vxlanhdr {
static int vxlan_net_id; static int vxlan_net_id;
static int vxlan_init_module(void);
static void vxlan_cleanup_module(void);
/* per-network namespace private data for this module */ /* per-network namespace private data for this module */
struct vxlan_net { struct vxlan_net {
struct hlist_head sock_list[PORT_HASH_SIZE]; struct hlist_head sock_list[PORT_HASH_SIZE];
struct mutex sock_lock; /* RTNL lock nests inside this lock. */ spinlock_t sock_lock;
}; };
/* Socket hash table head */ /* Socket hash table head */
@@ -90,7 +91,8 @@ static inline struct hlist_head *vs_head(struct net *net, __be16 port)
} }
/* Find VXLAN socket based on network namespace and UDP port */ /* Find VXLAN socket based on network namespace and UDP port */
static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
{ {
struct vxlan_sock *vs; struct vxlan_sock *vs;
@@ -104,7 +106,6 @@ static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
/* Callback from net/ipv4/udp.c to receive packets */ /* Callback from net/ipv4/udp.c to receive packets */
static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
{ {
struct vxlan_handler *vh;
struct vxlan_sock *vs; struct vxlan_sock *vs;
struct vxlanhdr *vxh; struct vxlanhdr *vxh;
@@ -124,14 +125,12 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
goto drop; goto drop;
vs = vxlan_find_port(sock_net(sk), inet_sport(sk)); vs = vxlan_find_sock(sock_net(sk), inet_sport(sk));
if (!vs) if (!vs)
goto drop; goto drop;
list_for_each_entry_rcu(vh, &vs->handler_list, node) { vs->rcv(vs, skb, vxh->vx_vni);
if (vh->rcv(vh, skb, vxh->vx_vni) == PACKET_RCVD) return 0;
return 0;
}
drop: drop:
/* Consume bad packet */ /* Consume bad packet */
@@ -211,7 +210,7 @@ static int handle_offloads(struct sk_buff *skb)
return 0; return 0;
} }
int vxlan_xmit_skb(struct net *net, struct vxlan_handler *vh, int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
struct rtable *rt, struct sk_buff *skb, struct rtable *rt, struct sk_buff *skb,
__be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
__be16 src_port, __be16 dst_port, __be32 vni) __be16 src_port, __be16 dst_port, __be32 vni)
@@ -249,7 +248,7 @@ int vxlan_xmit_skb(struct net *net, struct vxlan_handler *vh,
uh->len = htons(skb->len); uh->len = htons(skb->len);
uh->check = 0; uh->check = 0;
vxlan_set_owner(vh->vs->sock->sk, skb); vxlan_set_owner(vs->sock->sk, skb);
err = handle_offloads(skb); err = handle_offloads(skb);
if (err) if (err)
@@ -259,8 +258,26 @@ int vxlan_xmit_skb(struct net *net, struct vxlan_handler *vh,
IPPROTO_UDP, tos, ttl, df); IPPROTO_UDP, tos, ttl, df);
} }
static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port) static void rcu_free_vs(struct rcu_head *rcu)
{ {
struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
kfree(vs);
}
static void vxlan_del_work(struct work_struct *work)
{
struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
sk_release_kernel(vs->sock->sk);
call_rcu(&vs->rcu, rcu_free_vs);
vxlan_cleanup_module();
}
static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
vxlan_rcv_t *rcv, void *data)
{
struct vxlan_net *vn = net_generic(net, vxlan_net_id);
struct vxlan_sock *vs; struct vxlan_sock *vs;
struct sock *sk; struct sock *sk;
struct sockaddr_in vxlan_addr = { struct sockaddr_in vxlan_addr = {
@@ -271,8 +288,12 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
int rc; int rc;
vs = kmalloc(sizeof(*vs), GFP_KERNEL); vs = kmalloc(sizeof(*vs), GFP_KERNEL);
if (!vs) if (!vs) {
pr_debug("memory alocation failure\n");
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
}
INIT_WORK(&vs->del_work, vxlan_del_work);
/* Create UDP socket for encapsulation receive. */ /* Create UDP socket for encapsulation receive. */
rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock); rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
@@ -287,81 +308,36 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
sk_change_net(sk, net); sk_change_net(sk, net);
rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr, rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
sizeof(vxlan_addr)); sizeof(vxlan_addr));
if (rc < 0) { if (rc < 0) {
pr_debug("bind for UDP socket %pI4:%u (%d)\n", pr_debug("bind for UDP socket %pI4:%u (%d)\n",
&vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
sk_release_kernel(sk); sk_release_kernel(sk);
kfree(vs); kfree(vs);
return ERR_PTR(rc); return ERR_PTR(rc);
} }
vs->rcv = rcv;
vs->data = data;
/* Disable multicast loopback */ /* Disable multicast loopback */
inet_sk(sk)->mc_loop = 0; inet_sk(sk)->mc_loop = 0;
INIT_LIST_HEAD(&vs->handler_list); spin_lock(&vn->sock_lock);
hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
spin_unlock(&vn->sock_lock);
/* Mark socket as an encapsulation socket. */ /* Mark socket as an encapsulation socket. */
udp_sk(sk)->encap_type = 1; udp_sk(sk)->encap_type = 1;
udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
udp_encap_enable(); udp_encap_enable();
return vs; return vs;
} }
static void rcu_free_vs_callback(struct rcu_head *rcu) struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
{ vxlan_rcv_t *rcv, void *data,
struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu); bool no_share)
kfree(vs);
}
static void vxlan_socket_del(struct vxlan_sock *vs)
{
if (list_empty(&vs->handler_list)) {
hlist_del_rcu(&vs->hlist);
sk_release_kernel(vs->sock->sk);
call_rcu(&vs->rcu, rcu_free_vs_callback);
}
}
static int vxlan_init_module(void);
static void vxlan_cleanup_module(void);
static void rcu_free_vh_callback(struct rcu_head *rcu)
{
struct vxlan_handler *vh = container_of(rcu, struct vxlan_handler, rcu);
kfree(vh);
}
static void vh_del_work(struct work_struct *work)
{
struct vxlan_handler *vh = container_of(work, struct vxlan_handler, del_work);
struct vxlan_sock *vs = vh->vs;
struct net *net = sock_net(vs->sock->sk);
struct vxlan_net *vn = net_generic(net, vxlan_net_id);
mutex_lock(&vn->sock_lock);
list_del_rcu(&vh->node);
call_rcu(&vh->rcu, rcu_free_vh_callback);
vxlan_socket_del(vs);
mutex_unlock(&vn->sock_lock);
vxlan_cleanup_module();
}
struct vxlan_handler *vxlan_handler_add(struct net *net,
__be16 portno, vxlan_rcv_t *rcv,
void *data, int priority, bool create)
{ {
struct vxlan_net *vn; struct vxlan_net *vn;
struct vxlan_sock *vs; struct vxlan_sock *vs;
struct vxlan_handler *vh;
struct vxlan_handler *new;
int err; int err;
err = vxlan_init_module(); err = vxlan_init_module();
@@ -369,64 +345,19 @@ struct vxlan_handler *vxlan_handler_add(struct net *net,
return ERR_PTR(err); return ERR_PTR(err);
vn = net_generic(net, vxlan_net_id); vn = net_generic(net, vxlan_net_id);
mutex_lock(&vn->sock_lock); vs = vxlan_socket_create(net, port, rcv, data);
/* Look to see if can reuse socket */ return vs;
vs = vxlan_find_port(net, portno);
if (!vs) {
vs = vxlan_socket_create(net, portno);
if (IS_ERR(vs)) {
new = (void *) vs;
goto out;
}
}
/* Try existing vxlan hanlders for this socket. */
list_for_each_entry(vh, &vs->handler_list, node) {
if (vh->rcv == rcv) {
if (create) {
vxlan_socket_del(vs);
new = ERR_PTR(-EEXIST);
goto out;
}
atomic_inc(&vh->refcnt);
new = vh;
goto out;
}
}
new = kzalloc(sizeof(*new), GFP_KERNEL);
if (!new) {
vxlan_socket_del(vs);
new = ERR_PTR(-ENOMEM);
goto out;
}
new->rcv = rcv;
new->vs = vs;
atomic_set(&new->refcnt, 1);
INIT_WORK(&new->del_work, vh_del_work);
new->data = data;
new->priority = priority;
list_for_each_entry(vh, &vs->handler_list, node) {
if (vh->priority > priority) {
list_add_tail_rcu(&new->node, &vh->node);
goto out;
}
}
list_add_tail_rcu(&new->node, &vs->handler_list);
out:
mutex_unlock(&vn->sock_lock);
return new;
} }
void vxlan_handler_put(struct vxlan_handler *vh) void vxlan_sock_release(struct vxlan_sock *vs)
{ {
BUG_ON(!vh->vs); struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
if (atomic_dec_and_test(&vh->refcnt)) spin_lock(&vn->sock_lock);
queue_work(&vh->del_work); hlist_del_rcu(&vs->hlist);
spin_unlock(&vn->sock_lock);
queue_work(&vs->del_work);
} }
static int vxlan_init_net(struct net *net) static int vxlan_init_net(struct net *net)
@@ -434,7 +365,7 @@ static int vxlan_init_net(struct net *net)
struct vxlan_net *vn = net_generic(net, vxlan_net_id); struct vxlan_net *vn = net_generic(net, vxlan_net_id);
unsigned int h; unsigned int h;
mutex_init(&vn->sock_lock); spin_lock_init(&vn->sock_lock);
for (h = 0; h < PORT_HASH_SIZE; ++h) for (h = 0; h < PORT_HASH_SIZE; ++h)
INIT_HLIST_HEAD(&vn->sock_list[h]); INIT_HLIST_HEAD(&vn->sock_list[h]);

View File

@@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2011 Nicira, Inc. * Copyright (c) 2013 Nicira, Inc.
* Copyright (c) 2012 Cisco Systems, Inc. * Copyright (c) 2013 Cisco Systems, Inc.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public * modify it under the terms of version 2 of the GNU General Public
@@ -44,15 +44,13 @@
#include "datapath.h" #include "datapath.h"
#include "vport.h" #include "vport.h"
#define OVS_VXLAN_RCV_PRIORITY 8
/** /**
* struct vxlan_port - Keeps track of open UDP ports * struct vxlan_port - Keeps track of open UDP ports
* @vh: vxlan_handler created for the port. * @vs: vxlan_sock created for the port.
* @name: vport name. * @name: vport name.
*/ */
struct vxlan_port { struct vxlan_port {
struct vxlan_handler *vh; struct vxlan_sock *vs;
char name[IFNAMSIZ]; char name[IFNAMSIZ];
}; };
@@ -62,11 +60,11 @@ static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
} }
/* Called with rcu_read_lock and BH disabled. */ /* Called with rcu_read_lock and BH disabled. */
static int vxlan_rcv(struct vxlan_handler *vh, struct sk_buff *skb, __be32 vx_vni) static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb, __be32 vx_vni)
{ {
struct vport *vport = vh->data;
struct iphdr *iph;
struct ovs_key_ipv4_tunnel tun_key; struct ovs_key_ipv4_tunnel tun_key;
struct vport *vport = vs->data;
struct iphdr *iph;
__be64 key; __be64 key;
/* Save outer tunnel values */ /* Save outer tunnel values */
@@ -75,13 +73,12 @@ static int vxlan_rcv(struct vxlan_handler *vh, struct sk_buff *skb, __be32 vx_vn
ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY); ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
ovs_vport_receive(vport, skb, &tun_key); ovs_vport_receive(vport, skb, &tun_key);
return PACKET_RCVD;
} }
static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb) static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
{ {
struct vxlan_port *vxlan_port = vxlan_vport(vport); struct vxlan_port *vxlan_port = vxlan_vport(vport);
__be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk); __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port))) if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
return -EMSGSIZE; return -EMSGSIZE;
@@ -92,7 +89,7 @@ static void vxlan_tnl_destroy(struct vport *vport)
{ {
struct vxlan_port *vxlan_port = vxlan_vport(vport); struct vxlan_port *vxlan_port = vxlan_vport(vport);
vxlan_handler_put(vxlan_port->vh); vxlan_sock_release(vxlan_port->vs);
ovs_vport_deferred_free(vport); ovs_vport_deferred_free(vport);
} }
@@ -102,7 +99,7 @@ static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
struct net *net = ovs_dp_get_net(parms->dp); struct net *net = ovs_dp_get_net(parms->dp);
struct nlattr *options = parms->options; struct nlattr *options = parms->options;
struct vxlan_port *vxlan_port; struct vxlan_port *vxlan_port;
struct vxlan_handler *vh; struct vxlan_sock *vs;
struct vport *vport; struct vport *vport;
struct nlattr *a; struct nlattr *a;
u16 dst_port; u16 dst_port;
@@ -129,13 +126,12 @@ static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
vxlan_port = vxlan_vport(vport); vxlan_port = vxlan_vport(vport);
strncpy(vxlan_port->name, parms->name, IFNAMSIZ); strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
vh = vxlan_handler_add(net, htons(dst_port), vxlan_rcv, vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true);
vport, OVS_VXLAN_RCV_PRIORITY, true); if (IS_ERR(vs)) {
if (IS_ERR(vh)) {
ovs_vport_free(vport); ovs_vport_free(vport);
return (void *)vh; return (void *)vs;
} }
vxlan_port->vh = vh; vxlan_port->vs = vs;
return vport; return vport;
@@ -146,7 +142,7 @@ error:
static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb) static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
{ {
struct vxlan_port *vxlan_port = vxlan_vport(vport); struct vxlan_port *vxlan_port = vxlan_vport(vport);
__be16 dst_port = inet_sport(vxlan_port->vh->vs->sock->sk); __be16 dst_port = inet_sport(vxlan_port->vs->sock->sk);
struct net *net = ovs_dp_get_net(vport->dp); struct net *net = ovs_dp_get_net(vport->dp);
struct rtable *rt; struct rtable *rt;
__be16 src_port; __be16 src_port;
@@ -184,7 +180,7 @@ static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
inet_get_local_port_range(&port_min, &port_max); inet_get_local_port_range(&port_min, &port_max);
src_port = vxlan_src_port(port_min, port_max, skb); src_port = vxlan_src_port(port_min, port_max, skb);
err = vxlan_xmit_skb(net, vxlan_port->vh, rt, skb, err = vxlan_xmit_skb(net, vxlan_port->vs, rt, skb,
saddr, OVS_CB(skb)->tun_key->ipv4_dst, saddr, OVS_CB(skb)->tun_key->ipv4_dst,
OVS_CB(skb)->tun_key->ipv4_tos, OVS_CB(skb)->tun_key->ipv4_tos,
OVS_CB(skb)->tun_key->ipv4_ttl, df, OVS_CB(skb)->tun_key->ipv4_ttl, df,