[ovs-dev] [PATCH 09/13] Upstream VXLAN: Add VXLAN socket create API

Pravin B Shelar pshelar at nicira.com
Thu Nov 22 15:57:13 UTC 2012


From: Pravin Shelar <pshelar at nicira.com>

export vxlan_create_socket() so that other modules, like Open vSwitch,
can have configurable destination port for VXLAN.

Signed-off-by: Pravin B Shelar <pshelar at nicira.com>
---
 drivers/net/vxlan.c |   46 +++++++++++++++++++++++-----------------------
 include/net/vxlan.h |   17 +++++++++++++++++
 2 files changed, 40 insertions(+), 23 deletions(-)
 create mode 100644 include/net/vxlan.h

diff --git a/drivers/net/vxlan.c b/drivers/net/vxlan.c
index e18c334..8b0164a 100644
--- a/drivers/net/vxlan.c
+++ b/drivers/net/vxlan.c
@@ -41,6 +41,7 @@
 #include <net/inet_ecn.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
+#include <net/vxlan.h>
 
 #define VXLAN_VERSION	"0.1"
 
@@ -51,19 +52,6 @@
 #define FDB_AGE_DEFAULT 300 /* 5 min */
 #define FDB_AGE_INTERVAL (10 * HZ)	/* rescan interval */
 
-#define VXLAN_N_VID	(1u << 24)
-#define VXLAN_VID_MASK	(VXLAN_N_VID - 1)
-/* IP header + UDP + VXLAN + Ethernet header */
-#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
-
-#define VXLAN_FLAGS 0x08000000	/* struct vxlanhdr.vx_flags required value. */
-
-/* VXLAN protocol header */
-struct vxlanhdr {
-	__be32 vx_flags;
-	__be32 vx_vni;
-};
-
 /* UDP port for VXLAN traffic. */
 static unsigned int vxlan_port __read_mostly = 8472;
 module_param_named(udp_port, vxlan_port, uint, 0444);
@@ -1357,37 +1345,35 @@ static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
 	.fill_info	= vxlan_fill_info,
 };
 
-static __net_init int vxlan_init_net(struct net *net)
+struct socket *vxlan_create_socket(struct net *net, int portno)
 {
-	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
+	struct socket *sock;
 	struct sock *sk;
 	struct sockaddr_in vxlan_addr = {
 		.sin_family = AF_INET,
 		.sin_addr.s_addr = htonl(INADDR_ANY),
 	};
 	int rc;
-	unsigned h;
 
 	/* Create UDP socket for encapsulation receive. */
-	rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vn->sock);
+	rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
 	if (rc < 0) {
 		pr_debug("UDP socket create failed\n");
-		return rc;
+		return ERR_PTR(rc);
 	}
 	/* Put in proper namespace */
-	sk = vn->sock->sk;
+	sk = sock->sk;
 	sk_change_net(sk, net);
 
-	vxlan_addr.sin_port = htons(vxlan_port);
+	vxlan_addr.sin_port = htons(portno);
 
-	rc = kernel_bind(vn->sock, (struct sockaddr *) &vxlan_addr,
+	rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
 			 sizeof(vxlan_addr));
 	if (rc < 0) {
 		pr_debug("bind for UDP socket %pI4:%u (%d)\n",
 			 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
 		sk_release_kernel(sk);
-		vn->sock = NULL;
-		return rc;
+		return ERR_PTR(rc);
 	}
 
 	/* Disable multicast loopback */
@@ -1398,6 +1384,20 @@ static __net_init int vxlan_init_net(struct net *net)
 	udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
 	udp_encap_enable();
 
+	return sock;
+}
+EXPORT_SYMBOL(vxlan_create_socket);
+
+static __net_init int vxlan_init_net(struct net *net)
+{
+	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
+	unsigned h;
+
+	vn->sock = vxlan_create_socket(net, vxlan_port);
+	if (IS_ERR(vn->sock)) {
+		vn->sock = NULL;
+		return PTR_ERR(vn->sock);
+	}
 	for (h = 0; h < VNI_HASH_SIZE; ++h)
 		INIT_HLIST_HEAD(&vn->vni_list[h]);
 
diff --git a/include/net/vxlan.h b/include/net/vxlan.h
new file mode 100644
index 0000000..fbb1a2d
--- /dev/null
+++ b/include/net/vxlan.h
@@ -0,0 +1,17 @@
+#ifndef __LINUX_VXALN_H
+#define __LINUX_VXLAN_H
+
+#define VXLAN_N_VID	(1u << 24)
+#define VXLAN_VID_MASK	(VXLAN_N_VID - 1)
+/* VLAN + IP header + UDP + VXLAN */
+#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
+
+#define VXLAN_FLAGS 0x08000000	/* struct vxlanhdr.vx_flags required value. */
+/* VXLAN protocol header */
+struct vxlanhdr {
+	__be32 vx_flags;
+	__be32 vx_vni;
+};
+
+struct socket *vxlan_create_socket(struct net *net, int portno);
+#endif
-- 
1.7.1




More information about the dev mailing list