From patchwork Thu Apr 16 03:55:53 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jijiang Liu X-Patchwork-Id: 4323 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 18836C37E; Thu, 16 Apr 2015 05:56:20 +0200 (CEST) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by dpdk.org (Postfix) with ESMTP id 88411C37C for ; Thu, 16 Apr 2015 05:56:17 +0200 (CEST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP; 15 Apr 2015 20:56:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.11,585,1422950400"; d="scan'208";a="709921955" Received: from shvmail01.sh.intel.com ([10.239.29.42]) by fmsmga002.fm.intel.com with ESMTP; 15 Apr 2015 20:56:16 -0700 Received: from shecgisg004.sh.intel.com (shecgisg004.sh.intel.com [10.239.29.89]) by shvmail01.sh.intel.com with ESMTP id t3G3uENU005207; Thu, 16 Apr 2015 11:56:14 +0800 Received: from shecgisg004.sh.intel.com (localhost [127.0.0.1]) by shecgisg004.sh.intel.com (8.13.6/8.13.6/SuSE Linux 0.8) with ESMTP id t3G3uAML028996; Thu, 16 Apr 2015 11:56:12 +0800 Received: (from jijiangl@localhost) by shecgisg004.sh.intel.com (8.13.6/8.13.6/Submit) id t3G3uAOX028992; Thu, 16 Apr 2015 11:56:10 +0800 From: Jijiang Liu To: dev@dpdk.org, walter.e.gilmore@intel.com, thomas.long@intel.com Date: Thu, 16 Apr 2015 11:55:53 +0800 Message-Id: <1429156558-28548-6-git-send-email-jijiang.liu@intel.com> X-Mailer: git-send-email 1.7.12.2 In-Reply-To: <1429156558-28548-1-git-send-email-jijiang.liu@intel.com> References: <1429156558-28548-1-git-send-email-jijiang.liu@intel.com> Subject: [dpdk-dev] [PATCH RFC 05/10] examples/tep_termination:implement the APIs of encapsulation and decapsulation for VXLAN X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Fill the APIs of encapsulation and decapsulation for VXLAN packet; for the encapsulation operation, IPv6 is not supported now. Signed-off-by: Jijiang Liu Signed-off-by: Thomas Long --- 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#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);