examples/tep_term: implement VXLAN processing
[dpdk.git] / examples / tep_termination / vxlan.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdint.h>
34 #include <rte_mbuf.h>
35 #include <rte_hash_crc.h>
36 #include <rte_byteorder.h>
37 #include <rte_udp.h>
38 #include <rte_tcp.h>
39 #include <rte_sctp.h>
40
41 #include "main.h"
42 #include "vxlan.h"
43
44 /**
45  * Parse an ethernet header to fill the ethertype, outer_l2_len, outer_l3_len and
46  * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
47  * header.
48  */
49 static void
50 parse_ethernet(struct ether_hdr *eth_hdr, union tunnel_offload_info *info,
51                 uint8_t *l4_proto)
52 {
53         struct ipv4_hdr *ipv4_hdr;
54         struct ipv6_hdr *ipv6_hdr;
55         uint16_t ethertype;
56
57         info->outer_l2_len = sizeof(struct ether_hdr);
58         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
59
60         if (ethertype == ETHER_TYPE_VLAN) {
61                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
62                 info->outer_l2_len  += sizeof(struct vlan_hdr);
63                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
64         }
65
66         switch (ethertype) {
67         case ETHER_TYPE_IPv4:
68                 ipv4_hdr = (struct ipv4_hdr *)
69                         ((char *)eth_hdr + info->outer_l2_len);
70                 info->outer_l3_len = sizeof(struct ipv4_hdr);
71                 *l4_proto = ipv4_hdr->next_proto_id;
72                 break;
73         case ETHER_TYPE_IPv6:
74                 ipv6_hdr = (struct ipv6_hdr *)
75                         ((char *)eth_hdr + info->outer_l2_len);
76                 info->outer_l3_len = sizeof(struct ipv6_hdr);
77                 *l4_proto = ipv6_hdr->proto;
78                 break;
79         default:
80                 info->outer_l3_len = 0;
81                 *l4_proto = 0;
82                 break;
83         }
84 }
85
86 int
87 decapsulation(struct rte_mbuf *pkt)
88 {
89         uint8_t l4_proto = 0;
90         uint16_t outer_header_len;
91         struct udp_hdr *udp_hdr;
92         union tunnel_offload_info info = { .data = 0 };
93         struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
94
95         parse_ethernet(phdr, &info, &l4_proto);
96         if (l4_proto != IPPROTO_UDP)
97                 return -1;
98
99         udp_hdr = (struct udp_hdr *)((char *)phdr +
100                 info.outer_l2_len + info.outer_l3_len);
101
102         /** check udp destination port, 4789 is the default vxlan port
103          * (rfc7348) or that the rx offload flag is set (i40e only
104          * currently)*/
105         if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
106                         (pkt->ol_flags & (PKT_RX_TUNNEL_IPV4_HDR |
107                                 PKT_RX_TUNNEL_IPV6_HDR)) == 0)
108                 return -1;
109         outer_header_len = info.outer_l2_len + info.outer_l3_len
110                 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
111
112         rte_pktmbuf_adj(pkt, outer_header_len);
113
114         return 0;
115 }
116
117 void
118 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
119 {
120         uint vport_id;
121         uint64_t ol_flags = 0;
122         uint32_t old_len = m->pkt_len, hash;
123         struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
124
125         /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
126         struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
127                 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
128                 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
129
130         struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
131         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
132         struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
133
134         /* convert TX queue ID to vport ID */
135         vport_id = queue_id - 1;
136
137         /* replace original Ethernet header with ours */
138         pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
139                 sizeof(struct ether_hdr));
140
141         /* copy in IP header */
142         ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
143                 sizeof(struct ipv4_hdr));
144         ip->total_length = rte_cpu_to_be_16(m->data_len
145                                 - sizeof(struct ether_hdr));
146
147         /* outer IP checksum */
148         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
149         ip->hdr_checksum = 0;
150
151         m->outer_l2_len = sizeof(struct ether_hdr);
152         m->outer_l3_len = sizeof(struct ipv4_hdr);
153
154         m->ol_flags |= ol_flags;
155
156         /*VXLAN HEADER*/
157         vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
158         vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
159
160         /*UDP HEADER*/
161         udp->dgram_cksum = 0;
162         udp->dgram_len = rte_cpu_to_be_16(old_len
163                                 + sizeof(struct udp_hdr)
164                                 + sizeof(struct vxlan_hdr));
165
166         udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
167         hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
168         udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)
169                                         + PORT_MIN);
170
171         return;
172 }