Imported Upstream version 16.04
[deb_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 static uint16_t
45 get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags)
46 {
47         if (ethertype == ETHER_TYPE_IPv4)
48                 return rte_ipv4_phdr_cksum(l3_hdr, ol_flags);
49         else /* assume ethertype == ETHER_TYPE_IPv6 */
50                 return rte_ipv6_phdr_cksum(l3_hdr, ol_flags);
51 }
52
53 /**
54  * Parse an ethernet header to fill the ethertype, outer_l2_len, outer_l3_len and
55  * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
56  * header.
57  */
58 static void
59 parse_ethernet(struct ether_hdr *eth_hdr, union tunnel_offload_info *info,
60                 uint8_t *l4_proto)
61 {
62         struct ipv4_hdr *ipv4_hdr;
63         struct ipv6_hdr *ipv6_hdr;
64         uint16_t ethertype;
65
66         info->outer_l2_len = sizeof(struct ether_hdr);
67         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
68
69         if (ethertype == ETHER_TYPE_VLAN) {
70                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
71                 info->outer_l2_len  += sizeof(struct vlan_hdr);
72                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
73         }
74
75         switch (ethertype) {
76         case ETHER_TYPE_IPv4:
77                 ipv4_hdr = (struct ipv4_hdr *)
78                         ((char *)eth_hdr + info->outer_l2_len);
79                 info->outer_l3_len = sizeof(struct ipv4_hdr);
80                 *l4_proto = ipv4_hdr->next_proto_id;
81                 break;
82         case ETHER_TYPE_IPv6:
83                 ipv6_hdr = (struct ipv6_hdr *)
84                         ((char *)eth_hdr + info->outer_l2_len);
85                 info->outer_l3_len = sizeof(struct ipv6_hdr);
86                 *l4_proto = ipv6_hdr->proto;
87                 break;
88         default:
89                 info->outer_l3_len = 0;
90                 *l4_proto = 0;
91                 break;
92         }
93 }
94
95 /**
96  * Calculate the checksum of a packet in hardware
97  */
98 static uint64_t
99 process_inner_cksums(struct ether_hdr *eth_hdr, union tunnel_offload_info *info)
100 {
101         void *l3_hdr = NULL;
102         uint8_t l4_proto;
103         uint16_t ethertype;
104         struct ipv4_hdr *ipv4_hdr;
105         struct ipv6_hdr *ipv6_hdr;
106         struct udp_hdr *udp_hdr;
107         struct tcp_hdr *tcp_hdr;
108         struct sctp_hdr *sctp_hdr;
109         uint64_t ol_flags = 0;
110
111         info->l2_len = sizeof(struct ether_hdr);
112         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
113
114         if (ethertype == ETHER_TYPE_VLAN) {
115                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
116                 info->l2_len  += sizeof(struct vlan_hdr);
117                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
118         }
119
120         l3_hdr = (char *)eth_hdr + info->l2_len;
121
122         if (ethertype == ETHER_TYPE_IPv4) {
123                 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
124                 ipv4_hdr->hdr_checksum = 0;
125                 ol_flags |= PKT_TX_IPV4;
126                 ol_flags |= PKT_TX_IP_CKSUM;
127                 info->l3_len = sizeof(struct ipv4_hdr);
128                 l4_proto = ipv4_hdr->next_proto_id;
129         } else if (ethertype == ETHER_TYPE_IPv6) {
130                 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
131                 info->l3_len = sizeof(struct ipv6_hdr);
132                 l4_proto = ipv6_hdr->proto;
133                 ol_flags |= PKT_TX_IPV6;
134         } else
135                 return 0; /* packet type not supported, nothing to do */
136
137         if (l4_proto == IPPROTO_UDP) {
138                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
139                 ol_flags |= PKT_TX_UDP_CKSUM;
140                 udp_hdr->dgram_cksum = get_psd_sum(l3_hdr,
141                                 ethertype, ol_flags);
142         } else if (l4_proto == IPPROTO_TCP) {
143                 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
144                 ol_flags |= PKT_TX_TCP_CKSUM;
145                 tcp_hdr->cksum = get_psd_sum(l3_hdr, ethertype,
146                                 ol_flags);
147                 if (tso_segsz != 0) {
148                         ol_flags |= PKT_TX_TCP_SEG;
149                         info->tso_segsz = tso_segsz;
150                         info->l4_len = sizeof(struct tcp_hdr);
151                 }
152
153         } else if (l4_proto == IPPROTO_SCTP) {
154                 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
155                 sctp_hdr->cksum = 0;
156                 ol_flags |= PKT_TX_SCTP_CKSUM;
157         }
158
159         return ol_flags;
160 }
161
162 int
163 decapsulation(struct rte_mbuf *pkt)
164 {
165         uint8_t l4_proto = 0;
166         uint16_t outer_header_len;
167         struct udp_hdr *udp_hdr;
168         union tunnel_offload_info info = { .data = 0 };
169         struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
170
171         parse_ethernet(phdr, &info, &l4_proto);
172
173         if (l4_proto != IPPROTO_UDP)
174                 return -1;
175
176         udp_hdr = (struct udp_hdr *)((char *)phdr +
177                 info.outer_l2_len + info.outer_l3_len);
178
179         /** check udp destination port, 4789 is the default vxlan port
180          * (rfc7348) or that the rx offload flag is set (i40e only
181          * currently)*/
182         if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
183                 (pkt->packet_type & RTE_PTYPE_TUNNEL_MASK) == 0)
184                 return -1;
185         outer_header_len = info.outer_l2_len + info.outer_l3_len
186                 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
187
188         rte_pktmbuf_adj(pkt, outer_header_len);
189
190         return 0;
191 }
192
193 void
194 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
195 {
196         uint vport_id;
197         uint64_t ol_flags = 0;
198         uint32_t old_len = m->pkt_len, hash;
199         union tunnel_offload_info tx_offload = { .data = 0 };
200         struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
201
202         /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
203         struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
204                 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
205                 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
206
207         struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
208         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
209         struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
210
211         /* convert TX queue ID to vport ID */
212         vport_id = queue_id - 1;
213
214         /* replace original Ethernet header with ours */
215         pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
216                 sizeof(struct ether_hdr));
217
218         /* copy in IP header */
219         ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
220                 sizeof(struct ipv4_hdr));
221         ip->total_length = rte_cpu_to_be_16(m->data_len
222                                 - sizeof(struct ether_hdr));
223
224         /* outer IP checksum */
225         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
226         ip->hdr_checksum = 0;
227
228         /* inner IP checksum offload */
229         if (tx_checksum) {
230                 ol_flags |= process_inner_cksums(phdr, &tx_offload);
231                 m->l2_len = tx_offload.l2_len;
232                 m->l3_len = tx_offload.l3_len;
233                 m->l4_len = tx_offload.l4_len;
234                 m->l2_len += ETHER_VXLAN_HLEN;
235         }
236
237         m->outer_l2_len = sizeof(struct ether_hdr);
238         m->outer_l3_len = sizeof(struct ipv4_hdr);
239
240         m->ol_flags |= ol_flags;
241         m->tso_segsz = tx_offload.tso_segsz;
242
243         /*VXLAN HEADER*/
244         vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
245         vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
246
247         /*UDP HEADER*/
248         udp->dgram_cksum = 0;
249         udp->dgram_len = rte_cpu_to_be_16(old_len
250                                 + sizeof(struct udp_hdr)
251                                 + sizeof(struct vxlan_hdr));
252
253         udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
254         hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
255         udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)
256                                         + PORT_MIN);
257
258         return;
259 }