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