examples/tep_term: add inner checksum Tx offload configuration
[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
148         } else if (l4_proto == IPPROTO_SCTP) {
149                 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
150                 sctp_hdr->cksum = 0;
151                 ol_flags |= PKT_TX_SCTP_CKSUM;
152         }
153
154         return ol_flags;
155 }
156
157 int
158 decapsulation(struct rte_mbuf *pkt)
159 {
160         uint8_t l4_proto = 0;
161         uint16_t outer_header_len;
162         struct udp_hdr *udp_hdr;
163         union tunnel_offload_info info = { .data = 0 };
164         struct ether_hdr *phdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
165
166         parse_ethernet(phdr, &info, &l4_proto);
167
168         if (l4_proto != IPPROTO_UDP)
169                 return -1;
170
171         udp_hdr = (struct udp_hdr *)((char *)phdr +
172                 info.outer_l2_len + info.outer_l3_len);
173
174         /** check udp destination port, 4789 is the default vxlan port
175          * (rfc7348) or that the rx offload flag is set (i40e only
176          * currently)*/
177         if (udp_hdr->dst_port != rte_cpu_to_be_16(DEFAULT_VXLAN_PORT) &&
178                         (pkt->ol_flags & (PKT_RX_TUNNEL_IPV4_HDR |
179                                 PKT_RX_TUNNEL_IPV6_HDR)) == 0)
180                 return -1;
181         outer_header_len = info.outer_l2_len + info.outer_l3_len
182                 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr);
183
184         rte_pktmbuf_adj(pkt, outer_header_len);
185
186         return 0;
187 }
188
189 void
190 encapsulation(struct rte_mbuf *m, uint8_t queue_id)
191 {
192         uint vport_id;
193         uint64_t ol_flags = 0;
194         uint32_t old_len = m->pkt_len, hash;
195         union tunnel_offload_info tx_offload = { .data = 0 };
196         struct ether_hdr *phdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
197
198         /*Allocate space for new ethernet, IPv4, UDP and VXLAN headers*/
199         struct ether_hdr *pneth = (struct ether_hdr *) rte_pktmbuf_prepend(m,
200                 sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr)
201                 + sizeof(struct udp_hdr) + sizeof(struct vxlan_hdr));
202
203         struct ipv4_hdr *ip = (struct ipv4_hdr *) &pneth[1];
204         struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
205         struct vxlan_hdr *vxlan = (struct vxlan_hdr *) &udp[1];
206
207         /* convert TX queue ID to vport ID */
208         vport_id = queue_id - 1;
209
210         /* replace original Ethernet header with ours */
211         pneth = rte_memcpy(pneth, &app_l2_hdr[vport_id],
212                 sizeof(struct ether_hdr));
213
214         /* copy in IP header */
215         ip = rte_memcpy(ip, &app_ip_hdr[vport_id],
216                 sizeof(struct ipv4_hdr));
217         ip->total_length = rte_cpu_to_be_16(m->data_len
218                                 - sizeof(struct ether_hdr));
219
220         /* outer IP checksum */
221         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
222         ip->hdr_checksum = 0;
223
224         /* inner IP checksum offload */
225         if (tx_checksum) {
226                 ol_flags |= process_inner_cksums(phdr, &tx_offload);
227                 m->l2_len = tx_offload.l2_len;
228                 m->l3_len = tx_offload.l3_len;
229                 m->l2_len += ETHER_VXLAN_HLEN;
230         }
231
232         m->outer_l2_len = sizeof(struct ether_hdr);
233         m->outer_l3_len = sizeof(struct ipv4_hdr);
234
235         m->ol_flags |= ol_flags;
236
237         /*VXLAN HEADER*/
238         vxlan->vx_flags = rte_cpu_to_be_32(VXLAN_HF_VNI);
239         vxlan->vx_vni = rte_cpu_to_be_32(vxdev.out_key << 8);
240
241         /*UDP HEADER*/
242         udp->dgram_cksum = 0;
243         udp->dgram_len = rte_cpu_to_be_16(old_len
244                                 + sizeof(struct udp_hdr)
245                                 + sizeof(struct vxlan_hdr));
246
247         udp->dst_port = rte_cpu_to_be_16(vxdev.dst_port);
248         hash = rte_hash_crc(phdr, 2 * ETHER_ADDR_LEN, phdr->ether_type);
249         udp->src_port = rte_cpu_to_be_16((((uint64_t) hash * PORT_RANGE) >> 32)
250                                         + PORT_MIN);
251
252         return;
253 }