[dpdk-dev,RFC,04/10] examples/tep_termination:implement VXLAN packet processing

Message ID 1429156558-28548-5-git-send-email-jijiang.liu@intel.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Jijiang Liu April 16, 2015, 3:55 a.m. UTC
  Implement the following functions:

1> VXLAN port configuration
 
2> VXLAN tunnel setup
 
3> VXLAN tunnel destroying
 
4> VXLAN packet processing for Rx side
 
5> VXLAN packet processing for Tx side
 

Signed-off-by: Jijiang Liu <jijiang.liu@intel.com>
Signed-off-by: Thomas Long <thomas.long@intel.com>
---
 examples/tep_termination/Makefile      |    2 +-
 examples/tep_termination/main.c        |   26 ++-
 examples/tep_termination/vxlan_setup.c |  391 ++++++++++++++++++++++++++++++++
 3 files changed, 415 insertions(+), 4 deletions(-)
 create mode 100644 examples/tep_termination/vxlan_setup.c
  

Patch

diff --git a/examples/tep_termination/Makefile b/examples/tep_termination/Makefile
index 23fb647..ed4fab2 100644
--- a/examples/tep_termination/Makefile
+++ b/examples/tep_termination/Makefile
@@ -48,7 +48,7 @@  else
 APP = tep_termination
 
 # all source are stored in SRCS-y
-SRCS-y :=  main.c
+SRCS-y :=  main.c vxlan_setup.c
 
 CFLAGS += -O2 -D_FILE_OFFSET_BITS=64
 CFLAGS += $(WERROR_FLAGS)
diff --git a/examples/tep_termination/main.c b/examples/tep_termination/main.c
index f846053..60a825e 100644
--- a/examples/tep_termination/main.c
+++ b/examples/tep_termination/main.c
@@ -113,6 +113,16 @@  struct vpool {
 /* number of devices */
 uint16_t num_devices;
 
+/* overlay packet operation */
+struct ol_switch_ops overlay_options = {
+	.port_configure = vxlan_port_init,
+	.tunnel_setup = vxlan_link,
+	.tunnel_destroy = vxlan_unlink,
+	.tx_handle = vxlan_tx_pkts,
+	.rx_handle = vxlan_rx_pkts,
+	.param_handle = NULL,
+};
+
 /* Enable stats. */
 static uint32_t enable_stats = 0;
 
@@ -350,7 +360,7 @@  virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, __attribute__((unuse
 {
 	struct mbuf_table *tx_q;
 	struct rte_mbuf **m_table;
-	unsigned len, ret = 0;
+	unsigned len, ret;
 
 	const uint16_t lcore_id = rte_lcore_id();
 	struct virtio_net *dev = vdev->dev;
@@ -369,6 +379,8 @@  virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, __attribute__((unuse
 
 	if (unlikely(len == MAX_PKT_BURST)) {
 		m_table = (struct rte_mbuf **)tx_q->m_table;
+		ret = overlay_options.tx_handle(ports[0], (uint16_t)tx_q->txq_id,
+			(struct rte_mbuf **)tx_q->m_table, (uint16_t)tx_q->len);
 		/* Free any buffers not handled by TX and update the port stats. */
 		if (unlikely(ret < len)) {
 			do {
@@ -401,7 +413,7 @@  switch_worker(__attribute__((unused)) void *arg)
 	volatile struct lcore_ll_info *lcore_ll;
 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
 	uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
-	unsigned ret = 0, i;
+	unsigned ret, i;
 	const uint16_t lcore_id = rte_lcore_id();
 	const uint16_t num_cores = (uint16_t)rte_lcore_count();
 	uint16_t rx_count = 0;
@@ -429,6 +441,9 @@  switch_worker(__attribute__((unused)) void *arg)
 
 			if (tx_q->len) {
 				LOG_DEBUG(VHOST_DATA, "TX queue drained after timeout with burst size %u \n", tx_q->len);
+				ret = overlay_options.tx_handle(ports[0], (uint16_t)tx_q->txq_id,
+									   (struct rte_mbuf **)tx_q->m_table,
+									   (uint16_t)tx_q->len);
 				if (unlikely(ret < tx_q->len)) {
 					do {
 						rte_pktmbuf_free(tx_q->m_table[ret]);
@@ -462,6 +477,7 @@  switch_worker(__attribute__((unused)) void *arg)
 
 			if (unlikely(vdev->remove)) {
 				dev_ll = dev_ll->next;
+				overlay_options.tunnel_destroy(vdev);
 				vdev->ready = DEVICE_SAFE_REMOVE;
 				continue;
 			}
@@ -472,6 +488,7 @@  switch_worker(__attribute__((unused)) void *arg)
 					vdev->rx_q, pkts_burst, MAX_PKT_BURST);
 
 				if (rx_count) {
+					ret_count = overlay_options.rx_handle(dev, pkts_burst, rx_count);
 					if (enable_stats) {
 						rte_atomic64_add(
 						&dev_statistics[dev_ll->vdev->dev->device_fh].rx_total_atomic,
@@ -492,7 +509,7 @@  switch_worker(__attribute__((unused)) void *arg)
 				tx_count = rte_vhost_dequeue_burst(dev, VIRTIO_TXQ, mbuf_pool, pkts_burst, MAX_PKT_BURST);
 				/* If this is the first received packet we need to learn the MAC */
 				if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && tx_count) {
-					if (vdev->remove) {
+					if (vdev->remove || (overlay_options.tunnel_setup(vdev, pkts_burst[0]) == -1)) {
 						while (tx_count) {
 							mbuf_tmp = pkts_burst[--tx_count];
 							if (mbuf_tmp)
@@ -966,6 +983,9 @@  main(int argc, char *argv[])
 				"Skipping disabled port %d\n", portid);
 			continue;
 		}
+
+		if (overlay_options.port_configure(portid, mbuf_pool) != 0)
+			rte_exit(EXIT_FAILURE, "Cannot initialize network ports\n");
 	}
 
 	/* Initialise all linked lists. */
diff --git a/examples/tep_termination/vxlan_setup.c b/examples/tep_termination/vxlan_setup.c
new file mode 100644
index 0000000..874c502
--- /dev/null
+++ b/examples/tep_termination/vxlan_setup.c
@@ -0,0 +1,391 @@ 
+/*-
+ *   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 <getopt.h>
+#include <linux/if_ether.h>
+#include <linux/if_vlan.h>
+#include <linux/virtio_net.h>
+#include <linux/virtio_ring.h>
+#include <signal.h>
+#include <stdint.h>
+#include <sys/eventfd.h>
+#include <sys/param.h>
+#include <unistd.h>
+
+#include <rte_atomic.h>
+#include <rte_cycles.h>
+#include <rte_ethdev.h>
+#include <rte_log.h>
+#include <rte_string_fns.h>
+#include <rte_mbuf.h>
+#include <rte_malloc.h>
+#include <rte_ip.h>
+#include <rte_udp.h>
+#include <rte_tcp.h>
+
+#include "main.h"
+#include "rte_virtio_net.h"
+#include "vxlan.h"
+#include "vxlan_setup.h"
+
+#define VXLAN_ENCAP_HDR_SIZE (sizeof(struct ether_hdr)  + IPV4_HEADER_LEN + \
+                UDP_HEADER_LEN + VXLAN_HEADER_LEN)
+
+#define IPV4_HEADER_LEN 20
+#define UDP_HEADER_LEN  8
+#define VXLAN_HEADER_LEN 8
+
+#define IP_VERSION 0x40
+#define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
+#define IP_DEFTTL  64   /* from RFC 1340. */
+#define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
+
+#define IP_DN_FRAGMENT_FLAG 0x0040
+
+/* Used to compare MAC addresses. */
+#define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
+
+/* Configurable number of RX/TX ring descriptors */
+#define RTE_TEST_RX_DESC_DEFAULT 1024
+#define RTE_TEST_TX_DESC_DEFAULT 512
+
+extern uint16_t num_devices;
+extern uint8_t ports[RTE_MAX_ETHPORTS];
+
+/* ethernet addresses of ports */
+extern struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
+
+/* heads for the main used and free linked lists for the data path. */
+static struct virtio_net_data_ll *ll_root_used = NULL;
+
+/* VXLAN device */
+struct vxlan_conf vxdev;
+
+struct ipv4_hdr app_ip_hdr[VXLAN_N_PORTS];
+struct ether_hdr app_l2_hdr[VXLAN_N_PORTS];
+
+/* local VTEP IP address */
+static uint8_t vxlan_multicast_ips[2][4] = {{239, 1, 1, 1}, {239, 1, 2, 1}};
+
+/* remote VTEP IP address */
+static uint8_t vxlan_overlay_ips[2][4] = {{192, 168, 10, 1}, {192, 168, 30, 1}};
+
+/* remote VTEP MAC address */
+static uint8_t peer_mac[6] = {0x00, 0x11, 0x01, 0x00, 0x00, 0x01};
+
+/* VXLAN termination filter type */
+uint8_t tep_filter_type[] = {RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID,
+			     RTE_TUNNEL_FILTER_IMAC_TENID,
+			     RTE_TUNNEL_FILTER_OMAC_TENID_IMAC,};
+
+/* Options for configuring ethernet port */
+static const struct rte_eth_conf port_conf = {
+	.rxmode = {
+		.split_hdr_size = 0,
+		.header_split   = 0, /**< Header Split disabled */
+		.hw_ip_checksum = 0, /**< IP checksum offload disabled */
+		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
+		.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
+		.hw_strip_crc   = 0, /**< CRC stripped by hardware */
+	},
+	.txmode = {
+		.mq_mode = ETH_MQ_TX_NONE,
+	},
+};
+
+const uint16_t tenant_id_conf[] = {
+	1000, 1000, 1001, 1001, 1002, 1002, 1003, 1003,
+	1004, 1004, 1005, 1005, 1006, 1006, 1007, 1007,
+	1008, 1008, 1009, 1009, 1010, 1010, 1011, 1011,
+	1012, 1012, 1013, 1013, 1014, 1014, 1015, 1015,
+	1016, 1016, 1017, 1017, 1018, 1018, 1019, 1019,
+	1020, 1020, 1021, 1021, 1022, 1022, 1023, 1023,
+	1024, 1024, 1025, 1025, 1026, 1026, 1027, 1027,
+	1028, 1028, 1029, 1029, 1030, 1030, 1031, 1031,
+};
+
+/* *
+ * Compares a packet destination MAC address to a device MAC address.
+ */
+static inline int __attribute__((always_inline))
+ether_addr_cmp(struct ether_addr *ea, struct ether_addr *eb)
+{
+	return (((*(uint64_t *)ea ^ *(uint64_t *)eb) & MAC_ADDR_CMP) == 0);
+}
+
+/*
+ * Initialises a given port using global settings and with the rx buffers
+ * coming from the mbuf_pool passed as parameter
+ */
+int
+vxlan_port_init(uint8_t port, struct rte_mempool *mbuf_pool)
+{
+	struct rte_eth_dev_info dev_info;
+	uint16_t rx_rings, tx_rings = (uint16_t)rte_lcore_count();
+	const uint16_t rx_ring_size = RTE_TEST_RX_DESC_DEFAULT;
+	const uint16_t tx_ring_size = RTE_TEST_TX_DESC_DEFAULT;
+	int retval;
+	uint16_t num_queues, q;
+        //struct vxlan_conf *pconf = &vxdev;
+	struct rte_eth_rxconf *rxconf;
+	struct rte_eth_txconf *txconf;
+
+	rte_eth_dev_info_get (port, &dev_info);
+
+	dev_info.max_rx_queues = num_devices;
+
+	if (dev_info.max_rx_queues > MAX_QUEUES) {
+		rte_exit(EXIT_FAILURE,
+			"please define MAX_QUEUES no less than %u in %s\n",
+			dev_info.max_rx_queues, __FILE__);
+	}
+
+	rxconf = &dev_info.default_rxconf;
+	txconf = &dev_info.default_txconf;
+	
+	txconf->txq_flags = 0;
+	num_queues = num_devices;
+
+	if (port >= rte_eth_dev_count()) return -1;
+
+	rx_rings = (uint16_t)num_queues;
+
+	/* Configure ethernet device. */
+	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
+	if (retval != 0)
+		return retval;
+	/* Setup the queues. */
+	for (q = 0; q < rx_rings; q ++) {
+		retval = rte_eth_rx_queue_setup(port, q, rx_ring_size,
+						rte_eth_dev_socket_id(port), rxconf,
+						mbuf_pool);
+		if (retval < 0)
+			return retval;
+	}
+	for (q = 0; q < tx_rings; q ++) {
+		retval = rte_eth_tx_queue_setup(port, q, tx_ring_size,
+						rte_eth_dev_socket_id(port), txconf);
+		if (retval < 0)
+			return retval;
+	}
+
+	/* Start the device. */
+	retval  = rte_eth_dev_start(port);
+	if (retval < 0)
+		return retval;
+
+	rte_eth_macaddr_get(port, &ports_eth_addr[port]);
+	RTE_LOG(INFO, PORT, "Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
+			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
+			(unsigned)port,
+			ports_eth_addr[port].addr_bytes[0],
+			ports_eth_addr[port].addr_bytes[1],
+			ports_eth_addr[port].addr_bytes[2],
+			ports_eth_addr[port].addr_bytes[3],
+			ports_eth_addr[port].addr_bytes[4],
+			ports_eth_addr[port].addr_bytes[5]);
+
+	return 0;
+}
+
+static int
+vxlan_rx_process(struct rte_mbuf *pkt)
+{
+	int ret = 0;
+	if ((pkt->ol_flags & (PKT_RX_TUNNEL_IPV4_HDR
+		| PKT_RX_TUNNEL_IPV6_HDR)) == 0)
+		return -1;
+
+	return ret;
+}
+
+static int 
+vxlan_tx_process(struct rte_mbuf *pkt)
+{
+	int ret = 0;
+
+	if ((pkt->pkt_len + VXLAN_ENCAP_HDR_SIZE) > ETHER_MAX_LEN ){
+		rte_pktmbuf_free(pkt);
+		return -1;
+	}
+
+	return ret;
+}
+
+/*
+ * This function learns the MAC address of the device
+ */
+int
+vxlan_link(struct vhost_dev *vdev, struct rte_mbuf *m)
+{
+	int i;
+	struct ether_hdr *pkt_hdr;
+	struct virtio_net_data_ll *dev_ll;
+	struct virtio_net *dev = vdev->dev;
+	uint64_t portid = dev->device_fh;
+
+	dev_ll = ll_root_used;
+
+	if (unlikely(portid > VXLAN_N_PORTS)){
+		RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") WARNING: Not configuring device,"
+			"as already have %d ports for VXLAN.", dev->device_fh, VXLAN_N_PORTS);
+		return -1;
+	}
+
+	/* Learn MAC address of guest device from packet */
+	pkt_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
+	while (dev_ll != NULL) {
+		if (ether_addr_cmp(&(pkt_hdr->s_addr), &dev_ll->vdev->mac_address)) {
+		RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") WARNING: This device is using an existing"
+				" MAC address and has not been registered.\n", dev->device_fh);
+			return -1;
+		}
+		dev_ll = dev_ll->next;
+	}
+
+	for (i = 0; i < ETHER_ADDR_LEN; i++) {
+		vxdev.port[portid].portmac.addr_bytes[i] = pkt_hdr->s_addr.addr_bytes[i];
+		vxdev.port[portid].peermac.addr_bytes[i] = peer_mac[i];
+	}
+
+	/* Print out inner MAC and VNI info. */
+	RTE_LOG(INFO, VHOST_DATA, "(%d) MAC_ADDRESS %02x:%02x:%02x:%02x:%02x:%02x and VNI %d registered\n",
+		vdev->rx_q,
+		vdev->mac_address.addr_bytes[0], vdev->mac_address.addr_bytes[1],
+		vdev->mac_address.addr_bytes[2], vdev->mac_address.addr_bytes[3],
+		vdev->mac_address.addr_bytes[4], vdev->mac_address.addr_bytes[5],
+		tenant_id_conf[vdev->rx_q]);
+
+	vxdev.port[portid].portid = portid;
+
+	for (i = 0; i < 4; i++) {
+		/*Local VTEP IP */
+		vxdev.portip |= vxlan_multicast_ips[portid][i] << (8 * i);
+		/* remote VTEP IP */
+		vxdev.port[portid].peerip |= vxlan_overlay_ips[portid][i] << (8 * i);
+	}
+	
+	vxdev.out_key = tenant_id_conf[vdev->rx_q];
+	ether_addr_copy(&vxdev.port[portid].peermac, &app_l2_hdr[portid].d_addr);
+	ether_addr_copy(&ports_eth_addr[0], &app_l2_hdr[portid].s_addr);
+	app_l2_hdr[portid].ether_type = rte_cpu_to_be_32(ETHER_TYPE_IPv4);
+
+	struct ipv4_hdr *ip;
+	ip = &app_ip_hdr[portid];
+	ip->version_ihl = IP_VHL_DEF;
+	ip->type_of_service = 0;
+	ip->total_length = 0;
+	ip->packet_id = 0;
+	ip->fragment_offset = IP_DN_FRAGMENT_FLAG;
+	ip->time_to_live = IP_DEFTTL;
+	ip->next_proto_id = IPPROTO_UDP;
+	ip->hdr_checksum = 0;
+	ip->src_addr = vxdev.portip;
+	ip->dst_addr = vxdev.port[portid].peerip;
+	/* Set device as ready for RX. */
+
+	vdev->ready = DEVICE_RX;
+
+	return 0;
+}
+
+/*
+ * Removes cloud filter. Ensures that nothing is adding buffers to the RX
+ * queue before disabling RX on the device.
+ */
+void
+vxlan_unlink(struct vhost_dev *vdev)
+{
+	int ret = 1;
+	unsigned i = 0, rx_count;
+	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
+
+	if (vdev->ready == DEVICE_RX) {
+		for (i = 0; i < ETHER_ADDR_LEN; i++)
+			vdev->mac_address.addr_bytes[i] = 0;
+
+		/*Clear out the receive buffers*/
+		rx_count = rte_eth_rx_burst(ports[0],
+					(uint16_t)vdev->rx_q, pkts_burst, MAX_PKT_BURST);
+
+		while (rx_count) {
+		if (ret < 0)
+			continue;
+
+			for (i = 0; i < rx_count; i++)
+				rte_pktmbuf_free(pkts_burst[i]);
+
+			rx_count = rte_eth_rx_burst(ports[0],
+					(uint16_t)vdev->rx_q, pkts_burst, MAX_PKT_BURST);
+		}
+		vdev->ready = DEVICE_MAC_LEARNING;
+	}
+}
+
+/*transmit packets after encapsulating*/
+int
+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;
+
+	for (i = 0; i < count; i++){
+		ret = vxlan_tx_process(tx_pkts[i]);
+	}
+
+	ret = rte_eth_tx_burst(port_id, queue_id, tx_pkts, nb_pkts);
+
+	return ret;
+
+}
+
+/*Check for decapsulation and pass packets directly to VIRTIO device*/
+int
+vxlan_rx_pkts (struct virtio_net *dev, struct rte_mbuf **pkts_burst, uint32_t rx_count)
+{
+
+	uint32_t i = 0, count = 0;
+	int ret;
+	struct rte_mbuf *pkts_valid[rx_count];
+	for(i = 0; i < rx_count; i++) {
+		ret = vxlan_rx_process(pkts_burst[i]);
+		if(unlikely(ret < 0)){
+			continue;
+		}
+		pkts_valid[count] = pkts_burst[i];
+			count++;
+	}
+
+	ret = rte_vhost_enqueue_burst(dev, VIRTIO_RXQ, pkts_valid, rx_count);
+	return ret;
+}