[dpdk-dev] [PATCH RFC 05/10] examples/tep_termination:implement the APIs of encapsulation and decapsulation for VXLAN

Jijiang Liu jijiang.liu at intel.com
Thu Apr 16 05:55:53 CEST 2015


Fill the APIs of encapsulation and decapsulation for VXLAN packet; for the encapsulation operation, IPv6 is not supported now.
 
Signed-off-by: Jijiang Liu <jijiang.liu at intel.com>
Signed-off-by: Thomas Long <thomas.long at intel.com>
---
 examples/tep_termination/Makefile      |    4 +-
 examples/tep_termination/vxlan.c       |  160 ++++++++++++++++++++++++++++++++
 examples/tep_termination/vxlan_setup.c |    7 +-
 3 files changed, 167 insertions(+), 4 deletions(-)
 create mode 100644 examples/tep_termination/vxlan.c

diff --git a/examples/tep_termination/Makefile b/examples/tep_termination/Makefile
index ed4fab2..03ba865 100644
--- a/examples/tep_termination/Makefile
+++ b/examples/tep_termination/Makefile
@@ -1,6 +1,6 @@
 #   BSD LICENSE
 #
-#   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+#   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
 #   All rights reserved.
 #
 #   Redistribution and use in source and binary forms, with or without
@@ -48,7 +48,7 @@ else
 APP = tep_termination
 
 # all source are stored in SRCS-y
-SRCS-y :=  main.c vxlan_setup.c
+SRCS-y :=  main.c vxlan_setup.c vxlan.c
 
 CFLAGS += -O2 -D_FILE_OFFSET_BITS=64
 CFLAGS += $(WERROR_FLAGS)
diff --git a/examples/tep_termination/vxlan.c b/examples/tep_termination/vxlan.c
new file mode 100644
index 0000000..9d86616
--- /dev/null
+++ b/examples/tep_termination/vxlan.c
@@ -0,0 +1,160 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <stdint.h>
+#include <rte_mbuf.h>
+#include <rte_ether.h>
+#include <rte_hash_crc.h>
+#include <rte_byteorder.h>
+#include <rte_udp.h>
+#include <rte_tcp.h>
+#include <rte_sctp.h>
+#include <rte_ip.h>
+
+#include "main.h"
+#include "vxlan.h"
+
+extern struct vxlan_conf vxdev;
+extern struct ipv4_hdr app_ip_hdr[VXLAN_N_PORTS];
+extern struct ether_hdr app_l2_hdr[VXLAN_N_PORTS];
+
+/*
+ * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
+ * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
+ * header. 
+ */
+static void
+parse_ethernet(struct ether_hdr *eth_hdr, struct offload_info *info)
+{
+        struct ipv4_hdr *ipv4_hdr;
+        struct ipv6_hdr *ipv6_hdr;
+ 
+        info->outer_l2_len = sizeof(struct ether_hdr);
+        info->ethertype = eth_hdr->ether_type;
+ 
+        if (info->ethertype == rte_cpu_to_be_16(ETHER_TYPE_VLAN)) {
+                struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
+                info->outer_l2_len  += sizeof(struct vlan_hdr);
+                info->ethertype = vlan_hdr->eth_proto;
+        }
+ 
+        switch (rte_cpu_to_be_16(info->ethertype)) {
+        case ETHER_TYPE_IPv4:
+                ipv4_hdr = (struct ipv4_hdr *) ((char *)eth_hdr + info->l2_len);
+                info->outer_l3_len = sizeof(struct ipv4_hdr);
+                info->l4_proto = ipv4_hdr->next_proto_id;
+                break;
+        case ETHER_TYPE_IPv6:
+                ipv6_hdr = (struct ipv6_hdr *) ((char *)eth_hdr + info->l2_len);
+                info->outer_l3_len = sizeof(struct ipv6_hdr);
+                info->l4_proto = ipv6_hdr->proto;
+                break;
+        default:
+                info->outer_l2_len = 0;
+                info->outer_l3_len = 0;
+                info->l4_proto = 0;
+                break;
+	}
+                
+}
+
+int decapsulation(struct rte_mbuf *pkt)
+{
+	struct offload_info info;
+	uint16_t outer_header_len;
+	
+	memset(&info, 0, sizeof(struct offload_info));
+
+	struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
+	parse_ethernet(phdr, &info);
+	if (info.l4_proto == IPPROTO_UDP) {
+		struct udp_hdr *udp_hdr;
+		udp_hdr = (struct udp_hdr *)((char *)phdr +
+			info.outer_l2_len + info.outer_l3_len);
+		if (udp_hdr->dst_port != rte_cpu_to_be_16(4789))
+			return -1;
+	}
+	outer_header_len = info.outer_l2_len + info.outer_l3_len
+		+ sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
+
+	rte_pktmbuf_adj(pkt, outer_header_len);
+	return 0;
+}
+
+int encapsulation(struct rte_mbuf *m, uint8_t vport_id)
+{
+	struct offload_info info;
+	uint64_t ol_flags = 0;
+	uint32_t old_len = m->pkt_len, hash;
+	struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
+
+	/*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
+	struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
+		sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
+		+ sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
+
+	struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
+	struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
+	struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
+
+	memset(&info, 0, sizeof(struct offload_info));
+	/* replace original Ethernet header with ours */
+	pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id], sizeof(struct ether_hdr));
+	/* copy in IP header */
+	ip = rte_memcpy(ip, &app_ip_hdr[vport_id], sizeof(struct ipv4_hdr));
+	ip->total_length = rte_cpu_to_be_16(m->data_len - sizeof(struct ether_hdr));
+
+	/* outer IP checksum */
+	ol_flags |= PKT_TX_OUTER_IP_CKSUM;
+	ip->hdr_checksum = 0;
+
+	m->outer_l2_len = sizeof(struct ether_hdr);
+	m->outer_l3_len = sizeof(struct ipv4_hdr);
+
+	m->ol_flags |= ol_flags;
+
+	/*VXLAN HEADER*/
+	vxlan->vx_flags = VXLAN_FLAGS;
+	vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
+
+	/*UDP HEADER*/
+	udp->dgram_cksum = 0;
+	udp->dgram_len = rte_cpu_to_be_16(old_len
+				+ sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
+
+	udp->dst_port = rte_cpu_to_be_16(vxdev.vxport);
+	hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
+	udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)+ PORT_MIN);
+
+	return 0;
+}
+
diff --git a/examples/tep_termination/vxlan_setup.c b/examples/tep_termination/vxlan_setup.c
index 874c502..7cb2660 100644
--- a/examples/tep_termination/vxlan_setup.c
+++ b/examples/tep_termination/vxlan_setup.c
@@ -226,11 +226,12 @@ vxlan_rx_process(struct rte_mbuf *pkt)
 		| PKT_RX_TUNNEL_IPV6_HDR)) == 0)
 		return -1;
 
+	ret = decapsulation(pkt);
 	return ret;
 }
 
 static int 
-vxlan_tx_process(struct rte_mbuf *pkt)
+vxlan_tx_process(uint8_t vport_id, struct rte_mbuf *pkt)
 {
 	int ret = 0;
 
@@ -239,6 +240,7 @@ vxlan_tx_process(struct rte_mbuf *pkt)
 		return -1;
 	}
 
+	ret = encapsulation(pkt, vport_id);
 	return ret;
 }
 
@@ -358,9 +360,10 @@ vxlan_tx_pkts (uint8_t port_id, uint16_t queue_id,
 			struct rte_mbuf **tx_pkts, uint16_t nb_pkts) {
 	int ret = 0;
 	uint16_t count = nb_pkts, i;
+	uint16_t vport_id = queue_id;
 
 	for (i = 0; i < count; i++){
-		ret = vxlan_tx_process(tx_pkts[i]);
+		ret = vxlan_tx_process(vport_id, tx_pkts[i]);
 	}
 
 	ret = rte_eth_tx_burst(port_id, queue_id, tx_pkts, nb_pkts);
-- 
1.7.7.6



More information about the dev mailing list