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