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