app/testpmd: support IPIP tunnel in csum forward engine
[dpdk.git] / app / test-pmd / csumonly.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <stdint.h>
39 #include <unistd.h>
40 #include <inttypes.h>
41
42 #include <sys/queue.h>
43 #include <sys/stat.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_cycles.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_ring.h>
61 #include <rte_memory.h>
62 #include <rte_mempool.h>
63 #include <rte_mbuf.h>
64 #include <rte_memcpy.h>
65 #include <rte_interrupts.h>
66 #include <rte_pci.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
69 #include <rte_ip.h>
70 #include <rte_tcp.h>
71 #include <rte_udp.h>
72 #include <rte_sctp.h>
73 #include <rte_prefetch.h>
74 #include <rte_string_fns.h>
75 #include "testpmd.h"
76
77 #define IP_DEFTTL  64   /* from RFC 1340. */
78 #define IP_VERSION 0x40
79 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
80 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
81
82 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
83 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
84 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
85 #else
86 #define _htons(x) (x)
87 #endif
88
89 /* structure that caches offload info for the current packet */
90 struct testpmd_offload_info {
91         uint16_t ethertype;
92         uint16_t l2_len;
93         uint16_t l3_len;
94         uint16_t l4_len;
95         uint8_t l4_proto;
96         uint8_t is_tunnel;
97         uint16_t outer_ethertype;
98         uint16_t outer_l2_len;
99         uint16_t outer_l3_len;
100         uint8_t outer_l4_proto;
101         uint16_t tso_segsz;
102 };
103
104 /* simplified GRE header (flags must be 0) */
105 struct simple_gre_hdr {
106         uint16_t flags;
107         uint16_t proto;
108 };
109
110 static uint16_t
111 get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags)
112 {
113         if (ethertype == _htons(ETHER_TYPE_IPv4))
114                 return rte_ipv4_phdr_cksum(l3_hdr, ol_flags);
115         else /* assume ethertype == ETHER_TYPE_IPv6 */
116                 return rte_ipv6_phdr_cksum(l3_hdr, ol_flags);
117 }
118
119 static uint16_t
120 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
121 {
122         if (ethertype == _htons(ETHER_TYPE_IPv4))
123                 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
124         else /* assume ethertype == ETHER_TYPE_IPv6 */
125                 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
126 }
127
128 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
129 static void
130 parse_ipv4(struct ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
131 {
132         struct tcp_hdr *tcp_hdr;
133
134         info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
135         info->l4_proto = ipv4_hdr->next_proto_id;
136
137         /* only fill l4_len for TCP, it's useful for TSO */
138         if (info->l4_proto == IPPROTO_TCP) {
139                 tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + info->l3_len);
140                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
141         } else
142                 info->l4_len = 0;
143 }
144
145 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
146 static void
147 parse_ipv6(struct ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
148 {
149         struct tcp_hdr *tcp_hdr;
150
151         info->l3_len = sizeof(struct ipv6_hdr);
152         info->l4_proto = ipv6_hdr->proto;
153
154         /* only fill l4_len for TCP, it's useful for TSO */
155         if (info->l4_proto == IPPROTO_TCP) {
156                 tcp_hdr = (struct tcp_hdr *)((char *)ipv6_hdr + info->l3_len);
157                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
158         } else
159                 info->l4_len = 0;
160 }
161
162 /*
163  * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
164  * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
165  * header. The l4_len argument is only set in case of TCP (useful for TSO).
166  */
167 static void
168 parse_ethernet(struct ether_hdr *eth_hdr, struct testpmd_offload_info *info)
169 {
170         struct ipv4_hdr *ipv4_hdr;
171         struct ipv6_hdr *ipv6_hdr;
172
173         info->l2_len = sizeof(struct ether_hdr);
174         info->ethertype = eth_hdr->ether_type;
175
176         if (info->ethertype == _htons(ETHER_TYPE_VLAN)) {
177                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
178
179                 info->l2_len  += sizeof(struct vlan_hdr);
180                 info->ethertype = vlan_hdr->eth_proto;
181         }
182
183         switch (info->ethertype) {
184         case _htons(ETHER_TYPE_IPv4):
185                 ipv4_hdr = (struct ipv4_hdr *) ((char *)eth_hdr + info->l2_len);
186                 parse_ipv4(ipv4_hdr, info);
187                 break;
188         case _htons(ETHER_TYPE_IPv6):
189                 ipv6_hdr = (struct ipv6_hdr *) ((char *)eth_hdr + info->l2_len);
190                 parse_ipv6(ipv6_hdr, info);
191                 break;
192         default:
193                 info->l4_len = 0;
194                 info->l3_len = 0;
195                 info->l4_proto = 0;
196                 break;
197         }
198 }
199
200 /* Parse a vxlan header */
201 static void
202 parse_vxlan(struct udp_hdr *udp_hdr, struct testpmd_offload_info *info,
203         uint64_t mbuf_olflags)
204 {
205         struct ether_hdr *eth_hdr;
206
207         /* check udp destination port, 4789 is the default vxlan port
208          * (rfc7348) or that the rx offload flag is set (i40e only
209          * currently) */
210         if (udp_hdr->dst_port != _htons(4789) &&
211                 (mbuf_olflags & (PKT_RX_TUNNEL_IPV4_HDR |
212                         PKT_RX_TUNNEL_IPV6_HDR)) == 0)
213                 return;
214
215         info->is_tunnel = 1;
216         info->outer_ethertype = info->ethertype;
217         info->outer_l2_len = info->l2_len;
218         info->outer_l3_len = info->l3_len;
219         info->outer_l4_proto = info->l4_proto;
220
221         eth_hdr = (struct ether_hdr *)((char *)udp_hdr +
222                 sizeof(struct udp_hdr) +
223                 sizeof(struct vxlan_hdr));
224
225         parse_ethernet(eth_hdr, info);
226         info->l2_len += ETHER_VXLAN_HLEN; /* add udp + vxlan */
227 }
228
229 /* Parse a gre header */
230 static void
231 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
232 {
233         struct ether_hdr *eth_hdr;
234         struct ipv4_hdr *ipv4_hdr;
235         struct ipv6_hdr *ipv6_hdr;
236
237         /* if flags != 0; it's not supported */
238         if (gre_hdr->flags != 0)
239                 return;
240
241         if (gre_hdr->proto == _htons(ETHER_TYPE_IPv4)) {
242                 info->is_tunnel = 1;
243                 info->outer_ethertype = info->ethertype;
244                 info->outer_l2_len = info->l2_len;
245                 info->outer_l3_len = info->l3_len;
246                 info->outer_l4_proto = info->l4_proto;
247
248                 ipv4_hdr = (struct ipv4_hdr *)((char *)gre_hdr +
249                         sizeof(struct simple_gre_hdr));
250
251                 parse_ipv4(ipv4_hdr, info);
252                 info->ethertype = _htons(ETHER_TYPE_IPv4);
253                 info->l2_len = 0;
254
255         } else if (gre_hdr->proto == _htons(ETHER_TYPE_IPv6)) {
256                 info->is_tunnel = 1;
257                 info->outer_ethertype = info->ethertype;
258                 info->outer_l2_len = info->l2_len;
259                 info->outer_l3_len = info->l3_len;
260                 info->outer_l4_proto = info->l4_proto;
261
262                 ipv6_hdr = (struct ipv6_hdr *)((char *)gre_hdr +
263                         sizeof(struct simple_gre_hdr));
264
265                 info->ethertype = _htons(ETHER_TYPE_IPv6);
266                 parse_ipv6(ipv6_hdr, info);
267                 info->l2_len = 0;
268
269         } else if (gre_hdr->proto == _htons(0x6558)) { /* ETH_P_TEB in linux */
270                 info->is_tunnel = 1;
271                 info->outer_ethertype = info->ethertype;
272                 info->outer_l2_len = info->l2_len;
273                 info->outer_l3_len = info->l3_len;
274                 info->outer_l4_proto = info->l4_proto;
275
276                 eth_hdr = (struct ether_hdr *)((char *)gre_hdr +
277                         sizeof(struct simple_gre_hdr));
278
279                 parse_ethernet(eth_hdr, info);
280         } else
281                 return;
282
283         info->l2_len += sizeof(struct simple_gre_hdr);
284 }
285
286
287 /* Parse an encapsulated ip or ipv6 header */
288 static void
289 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
290 {
291         struct ipv4_hdr *ipv4_hdr = encap_ip;
292         struct ipv6_hdr *ipv6_hdr = encap_ip;
293         uint8_t ip_version;
294
295         ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
296
297         if (ip_version != 4 && ip_version != 6)
298                 return;
299
300         info->is_tunnel = 1;
301         info->outer_ethertype = info->ethertype;
302         info->outer_l2_len = info->l2_len;
303         info->outer_l3_len = info->l3_len;
304
305         if (ip_version == 4) {
306                 parse_ipv4(ipv4_hdr, info);
307                 info->ethertype = _htons(ETHER_TYPE_IPv4);
308         } else {
309                 parse_ipv6(ipv6_hdr, info);
310                 info->ethertype = _htons(ETHER_TYPE_IPv6);
311         }
312         info->l2_len = 0;
313 }
314
315 /* modify the IPv4 or IPv4 source address of a packet */
316 static void
317 change_ip_addresses(void *l3_hdr, uint16_t ethertype)
318 {
319         struct ipv4_hdr *ipv4_hdr = l3_hdr;
320         struct ipv6_hdr *ipv6_hdr = l3_hdr;
321
322         if (ethertype == _htons(ETHER_TYPE_IPv4)) {
323                 ipv4_hdr->src_addr =
324                         rte_cpu_to_be_32(rte_be_to_cpu_32(ipv4_hdr->src_addr) + 1);
325         } else if (ethertype == _htons(ETHER_TYPE_IPv6)) {
326                 ipv6_hdr->src_addr[15] = ipv6_hdr->src_addr[15] + 1;
327         }
328 }
329
330 /* if possible, calculate the checksum of a packet in hw or sw,
331  * depending on the testpmd command line configuration */
332 static uint64_t
333 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
334         uint16_t testpmd_ol_flags)
335 {
336         struct ipv4_hdr *ipv4_hdr = l3_hdr;
337         struct udp_hdr *udp_hdr;
338         struct tcp_hdr *tcp_hdr;
339         struct sctp_hdr *sctp_hdr;
340         uint64_t ol_flags = 0;
341
342         if (info->ethertype == _htons(ETHER_TYPE_IPv4)) {
343                 ipv4_hdr = l3_hdr;
344                 ipv4_hdr->hdr_checksum = 0;
345
346                 ol_flags |= PKT_TX_IPV4;
347                 if (info->tso_segsz != 0 && info->l4_proto == IPPROTO_TCP) {
348                         ol_flags |= PKT_TX_IP_CKSUM;
349                 } else {
350                         if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_IP_CKSUM)
351                                 ol_flags |= PKT_TX_IP_CKSUM;
352                         else
353                                 ipv4_hdr->hdr_checksum =
354                                         rte_ipv4_cksum(ipv4_hdr);
355                 }
356         } else if (info->ethertype == _htons(ETHER_TYPE_IPv6))
357                 ol_flags |= PKT_TX_IPV6;
358         else
359                 return 0; /* packet type not supported, nothing to do */
360
361         if (info->l4_proto == IPPROTO_UDP) {
362                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
363                 /* do not recalculate udp cksum if it was 0 */
364                 if (udp_hdr->dgram_cksum != 0) {
365                         udp_hdr->dgram_cksum = 0;
366                         if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_UDP_CKSUM) {
367                                 ol_flags |= PKT_TX_UDP_CKSUM;
368                                 udp_hdr->dgram_cksum = get_psd_sum(l3_hdr,
369                                         info->ethertype, ol_flags);
370                         } else {
371                                 udp_hdr->dgram_cksum =
372                                         get_udptcp_checksum(l3_hdr, udp_hdr,
373                                                 info->ethertype);
374                         }
375                 }
376         } else if (info->l4_proto == IPPROTO_TCP) {
377                 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
378                 tcp_hdr->cksum = 0;
379                 if (info->tso_segsz != 0) {
380                         ol_flags |= PKT_TX_TCP_SEG;
381                         tcp_hdr->cksum = get_psd_sum(l3_hdr, info->ethertype,
382                                 ol_flags);
383                 } else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_TCP_CKSUM) {
384                         ol_flags |= PKT_TX_TCP_CKSUM;
385                         tcp_hdr->cksum = get_psd_sum(l3_hdr, info->ethertype,
386                                 ol_flags);
387                 } else {
388                         tcp_hdr->cksum =
389                                 get_udptcp_checksum(l3_hdr, tcp_hdr,
390                                         info->ethertype);
391                 }
392         } else if (info->l4_proto == IPPROTO_SCTP) {
393                 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
394                 sctp_hdr->cksum = 0;
395                 /* sctp payload must be a multiple of 4 to be
396                  * offloaded */
397                 if ((testpmd_ol_flags & TESTPMD_TX_OFFLOAD_SCTP_CKSUM) &&
398                         ((ipv4_hdr->total_length & 0x3) == 0)) {
399                         ol_flags |= PKT_TX_SCTP_CKSUM;
400                 } else {
401                         /* XXX implement CRC32c, example available in
402                          * RFC3309 */
403                 }
404         }
405
406         return ol_flags;
407 }
408
409 /* Calculate the checksum of outer header (only vxlan is supported,
410  * meaning IP + UDP). The caller already checked that it's a vxlan
411  * packet */
412 static uint64_t
413 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
414         uint16_t testpmd_ol_flags)
415 {
416         struct ipv4_hdr *ipv4_hdr = outer_l3_hdr;
417         struct ipv6_hdr *ipv6_hdr = outer_l3_hdr;
418         struct udp_hdr *udp_hdr;
419         uint64_t ol_flags = 0;
420
421         if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4)) {
422                 ipv4_hdr->hdr_checksum = 0;
423                 ol_flags |= PKT_TX_OUTER_IPV4;
424
425                 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
426                         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
427                 else
428                         ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
429         } else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
430                 ol_flags |= PKT_TX_OUTER_IPV6;
431
432         if (info->outer_l4_proto != IPPROTO_UDP)
433                 return ol_flags;
434
435         /* outer UDP checksum is always done in software as we have no
436          * hardware supporting it today, and no API for it. */
437
438         udp_hdr = (struct udp_hdr *)((char *)outer_l3_hdr + info->outer_l3_len);
439         /* do not recalculate udp cksum if it was 0 */
440         if (udp_hdr->dgram_cksum != 0) {
441                 udp_hdr->dgram_cksum = 0;
442                 if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4))
443                         udp_hdr->dgram_cksum =
444                                 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
445                 else
446                         udp_hdr->dgram_cksum =
447                                 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
448         }
449
450         return ol_flags;
451 }
452
453 /*
454  * Receive a burst of packets, and for each packet:
455  *  - parse packet, and try to recognize a supported packet type (1)
456  *  - if it's not a supported packet type, don't touch the packet, else:
457  *  - modify the IPs in inner headers and in outer headers if any
458  *  - reprocess the checksum of all supported layers. This is done in SW
459  *    or HW, depending on testpmd command line configuration
460  *  - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
461  *    segmentation offload (this implies HW TCP checksum)
462  * Then transmit packets on the output port.
463  *
464  * (1) Supported packets are:
465  *   Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
466  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
467  *           UDP|TCP|SCTP
468  *   Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
469  *   Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
470  *   Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
471  *
472  * The testpmd command line for this forward engine sets the flags
473  * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
474  * wether a checksum must be calculated in software or in hardware. The
475  * IP, UDP, TCP and SCTP flags always concern the inner layer. The
476  * OUTER_IP is only useful for tunnel packets.
477  */
478 static void
479 pkt_burst_checksum_forward(struct fwd_stream *fs)
480 {
481         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
482         struct rte_port *txp;
483         struct rte_mbuf *m;
484         struct ether_hdr *eth_hdr;
485         void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
486         uint16_t nb_rx;
487         uint16_t nb_tx;
488         uint16_t i;
489         uint64_t ol_flags;
490         uint16_t testpmd_ol_flags;
491         uint32_t rx_bad_ip_csum;
492         uint32_t rx_bad_l4_csum;
493         struct testpmd_offload_info info;
494
495 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
496         uint64_t start_tsc;
497         uint64_t end_tsc;
498         uint64_t core_cycles;
499 #endif
500
501 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
502         start_tsc = rte_rdtsc();
503 #endif
504
505         /* receive a burst of packet */
506         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
507                                  nb_pkt_per_burst);
508         if (unlikely(nb_rx == 0))
509                 return;
510
511 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
512         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
513 #endif
514         fs->rx_packets += nb_rx;
515         rx_bad_ip_csum = 0;
516         rx_bad_l4_csum = 0;
517
518         txp = &ports[fs->tx_port];
519         testpmd_ol_flags = txp->tx_ol_flags;
520         memset(&info, 0, sizeof(info));
521         info.tso_segsz = txp->tso_segsz;
522
523         for (i = 0; i < nb_rx; i++) {
524
525                 ol_flags = 0;
526                 info.is_tunnel = 0;
527                 m = pkts_burst[i];
528
529                 /* Update the L3/L4 checksum error packet statistics */
530                 rx_bad_ip_csum += ((m->ol_flags & PKT_RX_IP_CKSUM_BAD) != 0);
531                 rx_bad_l4_csum += ((m->ol_flags & PKT_RX_L4_CKSUM_BAD) != 0);
532
533                 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
534                  * and inner headers */
535
536                 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
537                 parse_ethernet(eth_hdr, &info);
538                 l3_hdr = (char *)eth_hdr + info.l2_len;
539
540                 /* check if it's a supported tunnel */
541                 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_PARSE_TUNNEL) {
542                         if (info.l4_proto == IPPROTO_UDP) {
543                                 struct udp_hdr *udp_hdr;
544                                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr +
545                                         info.l3_len);
546                                 parse_vxlan(udp_hdr, &info, m->ol_flags);
547                         } else if (info.l4_proto == IPPROTO_GRE) {
548                                 struct simple_gre_hdr *gre_hdr;
549                                 gre_hdr = (struct simple_gre_hdr *)
550                                         ((char *)l3_hdr + info.l3_len);
551                                 parse_gre(gre_hdr, &info);
552                         } else if (info.l4_proto == IPPROTO_IPIP) {
553                                 void *encap_ip_hdr;
554                                 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
555                                 parse_encap_ip(encap_ip_hdr, &info);
556                         }
557                 }
558
559                 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
560                 if (info.is_tunnel) {
561                         outer_l3_hdr = l3_hdr;
562                         l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
563                 }
564
565                 /* step 2: change all source IPs (v4 or v6) so we need
566                  * to recompute the chksums even if they were correct */
567
568                 change_ip_addresses(l3_hdr, info.ethertype);
569                 if (info.is_tunnel == 1)
570                         change_ip_addresses(outer_l3_hdr, info.outer_ethertype);
571
572                 /* step 3: depending on user command line configuration,
573                  * recompute checksum either in software or flag the
574                  * mbuf to offload the calculation to the NIC. If TSO
575                  * is configured, prepare the mbuf for TCP segmentation. */
576
577                 /* process checksums of inner headers first */
578                 ol_flags |= process_inner_cksums(l3_hdr, &info, testpmd_ol_flags);
579
580                 /* Then process outer headers if any. Note that the software
581                  * checksum will be wrong if one of the inner checksums is
582                  * processed in hardware. */
583                 if (info.is_tunnel == 1) {
584                         ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
585                                 testpmd_ol_flags);
586                 }
587
588                 /* step 4: fill the mbuf meta data (flags and header lengths) */
589
590                 if (info.is_tunnel == 1) {
591                         if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) {
592                                 m->outer_l2_len = info.outer_l2_len;
593                                 m->outer_l3_len = info.outer_l3_len;
594                                 m->l2_len = info.l2_len;
595                                 m->l3_len = info.l3_len;
596                         }
597                         else {
598                                 /* if there is a outer UDP cksum
599                                    processed in sw and the inner in hw,
600                                    the outer checksum will be wrong as
601                                    the payload will be modified by the
602                                    hardware */
603                                 m->l2_len = info.outer_l2_len +
604                                         info.outer_l3_len + info.l2_len;
605                                 m->l3_len = info.l3_len;
606                                 m->l4_len = info.l4_len;
607                         }
608                 } else {
609                         /* this is only useful if an offload flag is
610                          * set, but it does not hurt to fill it in any
611                          * case */
612                         m->l2_len = info.l2_len;
613                         m->l3_len = info.l3_len;
614                         m->l4_len = info.l4_len;
615                 }
616                 m->tso_segsz = info.tso_segsz;
617                 m->ol_flags = ol_flags;
618
619                 /* if verbose mode is enabled, dump debug info */
620                 if (verbose_level > 0) {
621                         struct {
622                                 uint64_t flag;
623                                 uint64_t mask;
624                         } tx_flags[] = {
625                                 { PKT_TX_IP_CKSUM, PKT_TX_IP_CKSUM },
626                                 { PKT_TX_UDP_CKSUM, PKT_TX_L4_MASK },
627                                 { PKT_TX_TCP_CKSUM, PKT_TX_L4_MASK },
628                                 { PKT_TX_SCTP_CKSUM, PKT_TX_L4_MASK },
629                                 { PKT_TX_IPV4, PKT_TX_IPV4 },
630                                 { PKT_TX_IPV6, PKT_TX_IPV6 },
631                                 { PKT_TX_OUTER_IP_CKSUM, PKT_TX_OUTER_IP_CKSUM },
632                                 { PKT_TX_OUTER_IPV4, PKT_TX_OUTER_IPV4 },
633                                 { PKT_TX_OUTER_IPV6, PKT_TX_OUTER_IPV6 },
634                                 { PKT_TX_TCP_SEG, PKT_TX_TCP_SEG },
635                         };
636                         unsigned j;
637                         const char *name;
638
639                         printf("-----------------\n");
640                         /* dump rx parsed packet info */
641                         printf("rx: l2_len=%d ethertype=%x l3_len=%d "
642                                 "l4_proto=%d l4_len=%d\n",
643                                 info.l2_len, rte_be_to_cpu_16(info.ethertype),
644                                 info.l3_len, info.l4_proto, info.l4_len);
645                         if (info.is_tunnel == 1)
646                                 printf("rx: outer_l2_len=%d outer_ethertype=%x "
647                                         "outer_l3_len=%d\n", info.outer_l2_len,
648                                         rte_be_to_cpu_16(info.outer_ethertype),
649                                         info.outer_l3_len);
650                         /* dump tx packet info */
651                         if ((testpmd_ol_flags & (TESTPMD_TX_OFFLOAD_IP_CKSUM |
652                                                 TESTPMD_TX_OFFLOAD_UDP_CKSUM |
653                                                 TESTPMD_TX_OFFLOAD_TCP_CKSUM |
654                                                 TESTPMD_TX_OFFLOAD_SCTP_CKSUM)) ||
655                                 info.tso_segsz != 0)
656                                 printf("tx: m->l2_len=%d m->l3_len=%d "
657                                         "m->l4_len=%d\n",
658                                         m->l2_len, m->l3_len, m->l4_len);
659                         if ((info.is_tunnel == 1) &&
660                                 (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM))
661                                 printf("tx: m->outer_l2_len=%d m->outer_l3_len=%d\n",
662                                         m->outer_l2_len, m->outer_l3_len);
663                         if (info.tso_segsz != 0)
664                                 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
665                         printf("tx: flags=");
666                         for (j = 0; j < sizeof(tx_flags)/sizeof(*tx_flags); j++) {
667                                 name = rte_get_tx_ol_flag_name(tx_flags[j].flag);
668                                 if ((m->ol_flags & tx_flags[j].mask) ==
669                                         tx_flags[j].flag)
670                                         printf("%s ", name);
671                         }
672                         printf("\n");
673                 }
674         }
675         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_rx);
676         fs->tx_packets += nb_tx;
677         fs->rx_bad_ip_csum += rx_bad_ip_csum;
678         fs->rx_bad_l4_csum += rx_bad_l4_csum;
679
680 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
681         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
682 #endif
683         if (unlikely(nb_tx < nb_rx)) {
684                 fs->fwd_dropped += (nb_rx - nb_tx);
685                 do {
686                         rte_pktmbuf_free(pkts_burst[nb_tx]);
687                 } while (++nb_tx < nb_rx);
688         }
689 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
690         end_tsc = rte_rdtsc();
691         core_cycles = (end_tsc - start_tsc);
692         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
693 #endif
694 }
695
696 struct fwd_engine csum_fwd_engine = {
697         .fwd_mode_name  = "csum",
698         .port_fwd_begin = NULL,
699         .port_fwd_end   = NULL,
700         .packet_fwd     = pkt_burst_checksum_forward,
701 };
702