aa29f5fc10ee696825927039d961a30f87a2e301
[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_launch.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_mempool.h>
59 #include <rte_mbuf.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
64 #include <rte_ip.h>
65 #include <rte_tcp.h>
66 #include <rte_udp.h>
67 #include <rte_sctp.h>
68 #include <rte_prefetch.h>
69 #include <rte_string_fns.h>
70 #include <rte_flow.h>
71 #include <rte_gro.h>
72 #include <rte_gso.h>
73
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         uint8_t gso_enable;
96         uint16_t l2_len;
97         uint16_t l3_len;
98         uint16_t l4_len;
99         uint8_t l4_proto;
100         uint8_t is_tunnel;
101         uint16_t outer_ethertype;
102         uint16_t outer_l2_len;
103         uint16_t outer_l3_len;
104         uint8_t outer_l4_proto;
105         uint16_t tso_segsz;
106         uint16_t tunnel_tso_segsz;
107         uint32_t pkt_len;
108 };
109
110 /* simplified GRE header */
111 struct simple_gre_hdr {
112         uint16_t flags;
113         uint16_t proto;
114 } __attribute__((__packed__));
115
116 static uint16_t
117 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
118 {
119         if (ethertype == _htons(ETHER_TYPE_IPv4))
120                 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
121         else /* assume ethertype == ETHER_TYPE_IPv6 */
122                 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
123 }
124
125 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
126 static void
127 parse_ipv4(struct ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
128 {
129         struct tcp_hdr *tcp_hdr;
130
131         info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
132         info->l4_proto = ipv4_hdr->next_proto_id;
133
134         /* only fill l4_len for TCP, it's useful for TSO */
135         if (info->l4_proto == IPPROTO_TCP) {
136                 tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + info->l3_len);
137                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
138         } else
139                 info->l4_len = 0;
140 }
141
142 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
143 static void
144 parse_ipv6(struct ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
145 {
146         struct tcp_hdr *tcp_hdr;
147
148         info->l3_len = sizeof(struct ipv6_hdr);
149         info->l4_proto = ipv6_hdr->proto;
150
151         /* only fill l4_len for TCP, it's useful for TSO */
152         if (info->l4_proto == IPPROTO_TCP) {
153                 tcp_hdr = (struct tcp_hdr *)((char *)ipv6_hdr + info->l3_len);
154                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
155         } else
156                 info->l4_len = 0;
157 }
158
159 /*
160  * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
161  * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
162  * header. The l4_len argument is only set in case of TCP (useful for TSO).
163  */
164 static void
165 parse_ethernet(struct ether_hdr *eth_hdr, struct testpmd_offload_info *info)
166 {
167         struct ipv4_hdr *ipv4_hdr;
168         struct ipv6_hdr *ipv6_hdr;
169
170         info->l2_len = sizeof(struct ether_hdr);
171         info->ethertype = eth_hdr->ether_type;
172
173         if (info->ethertype == _htons(ETHER_TYPE_VLAN)) {
174                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
175
176                 info->l2_len  += sizeof(struct vlan_hdr);
177                 info->ethertype = vlan_hdr->eth_proto;
178         }
179
180         switch (info->ethertype) {
181         case _htons(ETHER_TYPE_IPv4):
182                 ipv4_hdr = (struct ipv4_hdr *) ((char *)eth_hdr + info->l2_len);
183                 parse_ipv4(ipv4_hdr, info);
184                 break;
185         case _htons(ETHER_TYPE_IPv6):
186                 ipv6_hdr = (struct ipv6_hdr *) ((char *)eth_hdr + info->l2_len);
187                 parse_ipv6(ipv6_hdr, info);
188                 break;
189         default:
190                 info->l4_len = 0;
191                 info->l3_len = 0;
192                 info->l4_proto = 0;
193                 break;
194         }
195 }
196
197 /* Parse a vxlan header */
198 static void
199 parse_vxlan(struct udp_hdr *udp_hdr,
200             struct testpmd_offload_info *info,
201             uint32_t pkt_type)
202 {
203         struct ether_hdr *eth_hdr;
204
205         /* check udp destination port, 4789 is the default vxlan port
206          * (rfc7348) or that the rx offload flag is set (i40e only
207          * currently) */
208         if (udp_hdr->dst_port != _htons(4789) &&
209                 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
210                 return;
211
212         info->is_tunnel = 1;
213         info->outer_ethertype = info->ethertype;
214         info->outer_l2_len = info->l2_len;
215         info->outer_l3_len = info->l3_len;
216         info->outer_l4_proto = info->l4_proto;
217
218         eth_hdr = (struct ether_hdr *)((char *)udp_hdr +
219                 sizeof(struct udp_hdr) +
220                 sizeof(struct vxlan_hdr));
221
222         parse_ethernet(eth_hdr, info);
223         info->l2_len += ETHER_VXLAN_HLEN; /* add udp + vxlan */
224 }
225
226 /* Parse a gre header */
227 static void
228 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
229 {
230         struct ether_hdr *eth_hdr;
231         struct ipv4_hdr *ipv4_hdr;
232         struct ipv6_hdr *ipv6_hdr;
233         uint8_t gre_len = 0;
234
235         /* check which fields are supported */
236         if ((gre_hdr->flags & _htons(~GRE_SUPPORTED_FIELDS)) != 0)
237                 return;
238
239         gre_len += sizeof(struct simple_gre_hdr);
240
241         if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
242                 gre_len += GRE_KEY_LEN;
243
244         if (gre_hdr->proto == _htons(ETHER_TYPE_IPv4)) {
245                 info->is_tunnel = 1;
246                 info->outer_ethertype = info->ethertype;
247                 info->outer_l2_len = info->l2_len;
248                 info->outer_l3_len = info->l3_len;
249                 info->outer_l4_proto = info->l4_proto;
250
251                 ipv4_hdr = (struct ipv4_hdr *)((char *)gre_hdr + gre_len);
252
253                 parse_ipv4(ipv4_hdr, info);
254                 info->ethertype = _htons(ETHER_TYPE_IPv4);
255                 info->l2_len = 0;
256
257         } else if (gre_hdr->proto == _htons(ETHER_TYPE_IPv6)) {
258                 info->is_tunnel = 1;
259                 info->outer_ethertype = info->ethertype;
260                 info->outer_l2_len = info->l2_len;
261                 info->outer_l3_len = info->l3_len;
262                 info->outer_l4_proto = info->l4_proto;
263
264                 ipv6_hdr = (struct ipv6_hdr *)((char *)gre_hdr + gre_len);
265
266                 info->ethertype = _htons(ETHER_TYPE_IPv6);
267                 parse_ipv6(ipv6_hdr, info);
268                 info->l2_len = 0;
269
270         } else if (gre_hdr->proto == _htons(ETHER_TYPE_TEB)) {
271                 info->is_tunnel = 1;
272                 info->outer_ethertype = info->ethertype;
273                 info->outer_l2_len = info->l2_len;
274                 info->outer_l3_len = info->l3_len;
275                 info->outer_l4_proto = info->l4_proto;
276
277                 eth_hdr = (struct ether_hdr *)((char *)gre_hdr + gre_len);
278
279                 parse_ethernet(eth_hdr, info);
280         } else
281                 return;
282
283         info->l2_len += gre_len;
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 /* if possible, calculate the checksum of a packet in hw or sw,
316  * depending on the testpmd command line configuration */
317 static uint64_t
318 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
319         uint16_t testpmd_ol_flags)
320 {
321         struct ipv4_hdr *ipv4_hdr = l3_hdr;
322         struct udp_hdr *udp_hdr;
323         struct tcp_hdr *tcp_hdr;
324         struct sctp_hdr *sctp_hdr;
325         uint64_t ol_flags = 0;
326         uint32_t max_pkt_len, tso_segsz = 0;
327
328         /* ensure packet is large enough to require tso */
329         if (!info->is_tunnel) {
330                 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
331                         info->tso_segsz;
332                 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
333                         tso_segsz = info->tso_segsz;
334         } else {
335                 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
336                         info->l2_len + info->l3_len + info->l4_len +
337                         info->tunnel_tso_segsz;
338                 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
339                         tso_segsz = info->tunnel_tso_segsz;
340         }
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->l4_proto == IPPROTO_TCP && tso_segsz) {
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                         else {
369                                 udp_hdr->dgram_cksum =
370                                         get_udptcp_checksum(l3_hdr, udp_hdr,
371                                                 info->ethertype);
372                         }
373                 }
374         } else if (info->l4_proto == IPPROTO_TCP) {
375                 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
376                 tcp_hdr->cksum = 0;
377                 if (tso_segsz)
378                         ol_flags |= PKT_TX_TCP_SEG;
379                 else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_TCP_CKSUM)
380                         ol_flags |= PKT_TX_TCP_CKSUM;
381                 else {
382                         tcp_hdr->cksum =
383                                 get_udptcp_checksum(l3_hdr, tcp_hdr,
384                                         info->ethertype);
385                 }
386                 if (info->gso_enable)
387                         ol_flags |= PKT_TX_TCP_SEG;
388         } else if (info->l4_proto == IPPROTO_SCTP) {
389                 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
390                 sctp_hdr->cksum = 0;
391                 /* sctp payload must be a multiple of 4 to be
392                  * offloaded */
393                 if ((testpmd_ol_flags & TESTPMD_TX_OFFLOAD_SCTP_CKSUM) &&
394                         ((ipv4_hdr->total_length & 0x3) == 0)) {
395                         ol_flags |= PKT_TX_SCTP_CKSUM;
396                 } else {
397                         /* XXX implement CRC32c, example available in
398                          * RFC3309 */
399                 }
400         }
401
402         return ol_flags;
403 }
404
405 /* Calculate the checksum of outer header */
406 static uint64_t
407 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
408         uint16_t testpmd_ol_flags, int tso_enabled)
409 {
410         struct ipv4_hdr *ipv4_hdr = outer_l3_hdr;
411         struct ipv6_hdr *ipv6_hdr = outer_l3_hdr;
412         struct udp_hdr *udp_hdr;
413         uint64_t ol_flags = 0;
414
415         if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4)) {
416                 ipv4_hdr->hdr_checksum = 0;
417                 ol_flags |= PKT_TX_OUTER_IPV4;
418
419                 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
420                         ol_flags |= PKT_TX_OUTER_IP_CKSUM;
421                 else
422                         ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
423         } else
424                 ol_flags |= PKT_TX_OUTER_IPV6;
425
426         if (info->outer_l4_proto != IPPROTO_UDP)
427                 return ol_flags;
428
429         udp_hdr = (struct udp_hdr *)((char *)outer_l3_hdr + info->outer_l3_len);
430
431         /* outer UDP checksum is done in software as we have no hardware
432          * supporting it today, and no API for it. In the other side, for
433          * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
434          * set to zero.
435          *
436          * If a packet will be TSOed into small packets by NIC, we cannot
437          * set/calculate a non-zero checksum, because it will be a wrong
438          * value after the packet be split into several small packets.
439          */
440         if (tso_enabled)
441                 udp_hdr->dgram_cksum = 0;
442
443         /* do not recalculate udp cksum if it was 0 */
444         if (udp_hdr->dgram_cksum != 0) {
445                 udp_hdr->dgram_cksum = 0;
446                 if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4))
447                         udp_hdr->dgram_cksum =
448                                 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
449                 else
450                         udp_hdr->dgram_cksum =
451                                 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
452         }
453
454         return ol_flags;
455 }
456
457 /*
458  * Helper function.
459  * Performs actual copying.
460  * Returns number of segments in the destination mbuf on success,
461  * or negative error code on failure.
462  */
463 static int
464 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
465         uint16_t seglen[], uint8_t nb_seg)
466 {
467         uint32_t dlen, slen, tlen;
468         uint32_t i, len;
469         const struct rte_mbuf *m;
470         const uint8_t *src;
471         uint8_t *dst;
472
473         dlen = 0;
474         slen = 0;
475         tlen = 0;
476
477         dst = NULL;
478         src = NULL;
479
480         m = ms;
481         i = 0;
482         while (ms != NULL && i != nb_seg) {
483
484                 if (slen == 0) {
485                         slen = rte_pktmbuf_data_len(ms);
486                         src = rte_pktmbuf_mtod(ms, const uint8_t *);
487                 }
488
489                 if (dlen == 0) {
490                         dlen = RTE_MIN(seglen[i], slen);
491                         md[i]->data_len = dlen;
492                         md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
493                         dst = rte_pktmbuf_mtod(md[i], uint8_t *);
494                 }
495
496                 len = RTE_MIN(slen, dlen);
497                 memcpy(dst, src, len);
498                 tlen += len;
499                 slen -= len;
500                 dlen -= len;
501                 src += len;
502                 dst += len;
503
504                 if (slen == 0)
505                         ms = ms->next;
506                 if (dlen == 0)
507                         i++;
508         }
509
510         if (ms != NULL)
511                 return -ENOBUFS;
512         else if (tlen != m->pkt_len)
513                 return -EINVAL;
514
515         md[0]->nb_segs = nb_seg;
516         md[0]->pkt_len = tlen;
517         md[0]->vlan_tci = m->vlan_tci;
518         md[0]->vlan_tci_outer = m->vlan_tci_outer;
519         md[0]->ol_flags = m->ol_flags;
520         md[0]->tx_offload = m->tx_offload;
521
522         return nb_seg;
523 }
524
525 /*
526  * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
527  * Copy packet contents and offload information into then new segmented mbuf.
528  */
529 static struct rte_mbuf *
530 pkt_copy_split(const struct rte_mbuf *pkt)
531 {
532         int32_t n, rc;
533         uint32_t i, len, nb_seg;
534         struct rte_mempool *mp;
535         uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
536         struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
537
538         mp = current_fwd_lcore()->mbp;
539
540         if (tx_pkt_split == TX_PKT_SPLIT_RND)
541                 nb_seg = random() % tx_pkt_nb_segs + 1;
542         else
543                 nb_seg = tx_pkt_nb_segs;
544
545         memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
546
547         /* calculate number of segments to use and their length. */
548         len = 0;
549         for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
550                 len += seglen[i];
551                 md[i] = NULL;
552         }
553
554         n = pkt->pkt_len - len;
555
556         /* update size of the last segment to fit rest of the packet */
557         if (n >= 0) {
558                 seglen[i - 1] += n;
559                 len += n;
560         }
561
562         nb_seg = i;
563         while (i != 0) {
564                 p = rte_pktmbuf_alloc(mp);
565                 if (p == NULL) {
566                         RTE_LOG(ERR, USER1,
567                                 "failed to allocate %u-th of %u mbuf "
568                                 "from mempool: %s\n",
569                                 nb_seg - i, nb_seg, mp->name);
570                         break;
571                 }
572
573                 md[--i] = p;
574                 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
575                         RTE_LOG(ERR, USER1, "mempool %s, %u-th segment: "
576                                 "expected seglen: %u, "
577                                 "actual mbuf tailroom: %u\n",
578                                 mp->name, i, seglen[i],
579                                 rte_pktmbuf_tailroom(md[i]));
580                         break;
581                 }
582         }
583
584         /* all mbufs successfully allocated, do copy */
585         if (i == 0) {
586                 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
587                 if (rc < 0)
588                         RTE_LOG(ERR, USER1,
589                                 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
590                                 "into %u segments failed with error code: %d\n",
591                                 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
592
593                 /* figure out how many mbufs to free. */
594                 i = RTE_MAX(rc, 0);
595         }
596
597         /* free unused mbufs */
598         for (; i != nb_seg; i++) {
599                 rte_pktmbuf_free_seg(md[i]);
600                 md[i] = NULL;
601         }
602
603         return md[0];
604 }
605
606 /*
607  * Receive a burst of packets, and for each packet:
608  *  - parse packet, and try to recognize a supported packet type (1)
609  *  - if it's not a supported packet type, don't touch the packet, else:
610  *  - reprocess the checksum of all supported layers. This is done in SW
611  *    or HW, depending on testpmd command line configuration
612  *  - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
613  *    segmentation offload (this implies HW TCP checksum)
614  * Then transmit packets on the output port.
615  *
616  * (1) Supported packets are:
617  *   Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
618  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
619  *           UDP|TCP|SCTP
620  *   Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
621  *   Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
622  *   Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
623  *
624  * The testpmd command line for this forward engine sets the flags
625  * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
626  * wether a checksum must be calculated in software or in hardware. The
627  * IP, UDP, TCP and SCTP flags always concern the inner layer. The
628  * OUTER_IP is only useful for tunnel packets.
629  */
630 static void
631 pkt_burst_checksum_forward(struct fwd_stream *fs)
632 {
633         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
634         struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
635         struct rte_gso_ctx *gso_ctx;
636         struct rte_mbuf **tx_pkts_burst;
637         struct rte_port *txp;
638         struct rte_mbuf *m, *p;
639         struct ether_hdr *eth_hdr;
640         void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
641         void **gro_ctx;
642         uint16_t gro_pkts_num;
643         uint8_t gro_enable;
644         uint16_t nb_rx;
645         uint16_t nb_tx;
646         uint16_t nb_prep;
647         uint16_t i;
648         uint64_t rx_ol_flags, tx_ol_flags;
649         uint16_t testpmd_ol_flags;
650         uint32_t retry;
651         uint32_t rx_bad_ip_csum;
652         uint32_t rx_bad_l4_csum;
653         struct testpmd_offload_info info;
654         uint16_t nb_segments = 0;
655         int ret;
656
657 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
658         uint64_t start_tsc;
659         uint64_t end_tsc;
660         uint64_t core_cycles;
661 #endif
662
663 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
664         start_tsc = rte_rdtsc();
665 #endif
666
667         /* receive a burst of packet */
668         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
669                                  nb_pkt_per_burst);
670         if (unlikely(nb_rx == 0))
671                 return;
672 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
673         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
674 #endif
675         fs->rx_packets += nb_rx;
676         rx_bad_ip_csum = 0;
677         rx_bad_l4_csum = 0;
678         gro_enable = gro_ports[fs->rx_port].enable;
679
680         txp = &ports[fs->tx_port];
681         testpmd_ol_flags = txp->tx_ol_flags;
682         memset(&info, 0, sizeof(info));
683         info.tso_segsz = txp->tso_segsz;
684         info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
685         if (gso_ports[fs->tx_port].enable)
686                 info.gso_enable = 1;
687
688         for (i = 0; i < nb_rx; i++) {
689                 if (likely(i < nb_rx - 1))
690                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
691                                                        void *));
692
693                 m = pkts_burst[i];
694                 info.is_tunnel = 0;
695                 info.pkt_len = rte_pktmbuf_pkt_len(m);
696                 tx_ol_flags = 0;
697                 rx_ol_flags = m->ol_flags;
698
699                 /* Update the L3/L4 checksum error packet statistics */
700                 if ((rx_ol_flags & PKT_RX_IP_CKSUM_MASK) == PKT_RX_IP_CKSUM_BAD)
701                         rx_bad_ip_csum += 1;
702                 if ((rx_ol_flags & PKT_RX_L4_CKSUM_MASK) == PKT_RX_L4_CKSUM_BAD)
703                         rx_bad_l4_csum += 1;
704
705                 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
706                  * and inner headers */
707
708                 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
709                 ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
710                                 &eth_hdr->d_addr);
711                 ether_addr_copy(&ports[fs->tx_port].eth_addr,
712                                 &eth_hdr->s_addr);
713                 parse_ethernet(eth_hdr, &info);
714                 l3_hdr = (char *)eth_hdr + info.l2_len;
715
716                 /* check if it's a supported tunnel */
717                 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_PARSE_TUNNEL) {
718                         if (info.l4_proto == IPPROTO_UDP) {
719                                 struct udp_hdr *udp_hdr;
720
721                                 udp_hdr = (struct udp_hdr *)((char *)l3_hdr +
722                                         info.l3_len);
723                                 parse_vxlan(udp_hdr, &info, m->packet_type);
724                                 if (info.is_tunnel)
725                                         tx_ol_flags |= PKT_TX_TUNNEL_VXLAN;
726                         } else if (info.l4_proto == IPPROTO_GRE) {
727                                 struct simple_gre_hdr *gre_hdr;
728
729                                 gre_hdr = (struct simple_gre_hdr *)
730                                         ((char *)l3_hdr + info.l3_len);
731                                 parse_gre(gre_hdr, &info);
732                                 if (info.is_tunnel)
733                                         tx_ol_flags |= PKT_TX_TUNNEL_GRE;
734                         } else if (info.l4_proto == IPPROTO_IPIP) {
735                                 void *encap_ip_hdr;
736
737                                 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
738                                 parse_encap_ip(encap_ip_hdr, &info);
739                                 if (info.is_tunnel)
740                                         tx_ol_flags |= PKT_TX_TUNNEL_IPIP;
741                         }
742                 }
743
744                 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
745                 if (info.is_tunnel) {
746                         outer_l3_hdr = l3_hdr;
747                         l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
748                 }
749
750                 /* step 2: depending on user command line configuration,
751                  * recompute checksum either in software or flag the
752                  * mbuf to offload the calculation to the NIC. If TSO
753                  * is configured, prepare the mbuf for TCP segmentation. */
754
755                 /* process checksums of inner headers first */
756                 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
757                         testpmd_ol_flags);
758
759                 /* Then process outer headers if any. Note that the software
760                  * checksum will be wrong if one of the inner checksums is
761                  * processed in hardware. */
762                 if (info.is_tunnel == 1) {
763                         tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
764                                         testpmd_ol_flags,
765                                         !!(tx_ol_flags & PKT_TX_TCP_SEG));
766                 }
767
768                 /* step 3: fill the mbuf meta data (flags and header lengths) */
769
770                 if (info.is_tunnel == 1) {
771                         if (info.tunnel_tso_segsz ||
772                             (testpmd_ol_flags &
773                             TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) ||
774                             (tx_ol_flags & PKT_TX_OUTER_IPV6)) {
775                                 m->outer_l2_len = info.outer_l2_len;
776                                 m->outer_l3_len = info.outer_l3_len;
777                                 m->l2_len = info.l2_len;
778                                 m->l3_len = info.l3_len;
779                                 m->l4_len = info.l4_len;
780                                 m->tso_segsz = info.tunnel_tso_segsz;
781                         }
782                         else {
783                                 /* if there is a outer UDP cksum
784                                    processed in sw and the inner in hw,
785                                    the outer checksum will be wrong as
786                                    the payload will be modified by the
787                                    hardware */
788                                 m->l2_len = info.outer_l2_len +
789                                         info.outer_l3_len + info.l2_len;
790                                 m->l3_len = info.l3_len;
791                                 m->l4_len = info.l4_len;
792                         }
793                 } else {
794                         /* this is only useful if an offload flag is
795                          * set, but it does not hurt to fill it in any
796                          * case */
797                         m->l2_len = info.l2_len;
798                         m->l3_len = info.l3_len;
799                         m->l4_len = info.l4_len;
800                         m->tso_segsz = info.tso_segsz;
801                 }
802                 m->ol_flags = tx_ol_flags;
803
804                 /* Do split & copy for the packet. */
805                 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
806                         p = pkt_copy_split(m);
807                         if (p != NULL) {
808                                 rte_pktmbuf_free(m);
809                                 m = p;
810                                 pkts_burst[i] = m;
811                         }
812                 }
813
814                 /* if verbose mode is enabled, dump debug info */
815                 if (verbose_level > 0) {
816                         char buf[256];
817
818                         printf("-----------------\n");
819                         printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
820                                 fs->rx_port, m, m->pkt_len, m->nb_segs);
821                         /* dump rx parsed packet info */
822                         rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
823                         printf("rx: l2_len=%d ethertype=%x l3_len=%d "
824                                 "l4_proto=%d l4_len=%d flags=%s\n",
825                                 info.l2_len, rte_be_to_cpu_16(info.ethertype),
826                                 info.l3_len, info.l4_proto, info.l4_len, buf);
827                         if (rx_ol_flags & PKT_RX_LRO)
828                                 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
829                         if (info.is_tunnel == 1)
830                                 printf("rx: outer_l2_len=%d outer_ethertype=%x "
831                                         "outer_l3_len=%d\n", info.outer_l2_len,
832                                         rte_be_to_cpu_16(info.outer_ethertype),
833                                         info.outer_l3_len);
834                         /* dump tx packet info */
835                         if ((testpmd_ol_flags & (TESTPMD_TX_OFFLOAD_IP_CKSUM |
836                                                 TESTPMD_TX_OFFLOAD_UDP_CKSUM |
837                                                 TESTPMD_TX_OFFLOAD_TCP_CKSUM |
838                                                 TESTPMD_TX_OFFLOAD_SCTP_CKSUM)) ||
839                                 info.tso_segsz != 0)
840                                 printf("tx: m->l2_len=%d m->l3_len=%d "
841                                         "m->l4_len=%d\n",
842                                         m->l2_len, m->l3_len, m->l4_len);
843                         if (info.is_tunnel == 1) {
844                                 if ((testpmd_ol_flags &
845                                     TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) ||
846                                     (tx_ol_flags & PKT_TX_OUTER_IPV6))
847                                         printf("tx: m->outer_l2_len=%d "
848                                                 "m->outer_l3_len=%d\n",
849                                                 m->outer_l2_len,
850                                                 m->outer_l3_len);
851                                 if (info.tunnel_tso_segsz != 0 &&
852                                                 (m->ol_flags & PKT_TX_TCP_SEG))
853                                         printf("tx: m->tso_segsz=%d\n",
854                                                 m->tso_segsz);
855                         } else if (info.tso_segsz != 0 &&
856                                         (m->ol_flags & PKT_TX_TCP_SEG))
857                                 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
858                         rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
859                         printf("tx: flags=%s", buf);
860                         printf("\n");
861                 }
862         }
863
864         if (unlikely(gro_enable)) {
865                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
866                         nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
867                                         &(gro_ports[fs->rx_port].param));
868                 } else {
869                         gro_ctx = current_fwd_lcore()->gro_ctx;
870                         nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
871
872                         if (++fs->gro_times >= gro_flush_cycles) {
873                                 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
874                                 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
875                                         gro_pkts_num = MAX_PKT_BURST - nb_rx;
876
877                                 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
878                                                 RTE_GRO_TCP_IPV4,
879                                                 &pkts_burst[nb_rx],
880                                                 gro_pkts_num);
881                                 fs->gro_times = 0;
882                         }
883                 }
884         }
885
886         if (gso_ports[fs->tx_port].enable == 0)
887                 tx_pkts_burst = pkts_burst;
888         else {
889                 gso_ctx = &(current_fwd_lcore()->gso_ctx);
890                 gso_ctx->gso_size = gso_max_segment_size;
891                 for (i = 0; i < nb_rx; i++) {
892                         ret = rte_gso_segment(pkts_burst[i], gso_ctx,
893                                         &gso_segments[nb_segments],
894                                         GSO_MAX_PKT_BURST - nb_segments);
895                         if (ret >= 0)
896                                 nb_segments += ret;
897                         else {
898                                 RTE_LOG(DEBUG, USER1,
899                                                 "Unable to segment packet");
900                                 rte_pktmbuf_free(pkts_burst[i]);
901                         }
902                 }
903
904                 tx_pkts_burst = gso_segments;
905                 nb_rx = nb_segments;
906         }
907
908         nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
909                         tx_pkts_burst, nb_rx);
910         if (nb_prep != nb_rx)
911                 printf("Preparing packet burst to transmit failed: %s\n",
912                                 rte_strerror(rte_errno));
913
914         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
915                         nb_prep);
916
917         /*
918          * Retry if necessary
919          */
920         if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
921                 retry = 0;
922                 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
923                         rte_delay_us(burst_tx_delay_time);
924                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
925                                         &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
926                 }
927         }
928         fs->tx_packets += nb_tx;
929         fs->rx_bad_ip_csum += rx_bad_ip_csum;
930         fs->rx_bad_l4_csum += rx_bad_l4_csum;
931
932 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
933         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
934 #endif
935         if (unlikely(nb_tx < nb_rx)) {
936                 fs->fwd_dropped += (nb_rx - nb_tx);
937                 do {
938                         rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
939                 } while (++nb_tx < nb_rx);
940         }
941
942 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
943         end_tsc = rte_rdtsc();
944         core_cycles = (end_tsc - start_tsc);
945         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
946 #endif
947 }
948
949 struct fwd_engine csum_fwd_engine = {
950         .fwd_mode_name  = "csum",
951         .port_fwd_begin = NULL,
952         .port_fwd_end   = NULL,
953         .packet_fwd     = pkt_burst_checksum_forward,
954 };