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