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