gpudev: add alignment for memory allocation
[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_branch_prediction.h>
28 #include <rte_mempool.h>
29 #include <rte_mbuf.h>
30 #include <rte_interrupts.h>
31 #include <rte_pci.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
34 #include <rte_ip.h>
35 #include <rte_tcp.h>
36 #include <rte_udp.h>
37 #include <rte_vxlan.h>
38 #include <rte_sctp.h>
39 #include <rte_gtp.h>
40 #include <rte_prefetch.h>
41 #include <rte_string_fns.h>
42 #include <rte_flow.h>
43 #ifdef RTE_LIB_GRO
44 #include <rte_gro.h>
45 #endif
46 #ifdef RTE_LIB_GSO
47 #include <rte_gso.h>
48 #endif
49 #include <rte_geneve.h>
50
51 #include "testpmd.h"
52
53 #define IP_DEFTTL  64   /* from RFC 1340. */
54
55 #define GRE_CHECKSUM_PRESENT    0x8000
56 #define GRE_KEY_PRESENT         0x2000
57 #define GRE_SEQUENCE_PRESENT    0x1000
58 #define GRE_EXT_LEN             4
59 #define GRE_SUPPORTED_FIELDS    (GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
60                                  GRE_SEQUENCE_PRESENT)
61
62 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
63 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
64 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
65 #else
66 #define _htons(x) (x)
67 #endif
68
69 uint16_t vxlan_gpe_udp_port = RTE_VXLAN_GPE_DEFAULT_PORT;
70 uint16_t geneve_udp_port = RTE_GENEVE_DEFAULT_PORT;
71
72 /* structure that caches offload info for the current packet */
73 struct testpmd_offload_info {
74         uint16_t ethertype;
75 #ifdef RTE_LIB_GSO
76         uint8_t gso_enable;
77 #endif
78         uint16_t l2_len;
79         uint16_t l3_len;
80         uint16_t l4_len;
81         uint8_t l4_proto;
82         uint8_t is_tunnel;
83         uint16_t outer_ethertype;
84         uint16_t outer_l2_len;
85         uint16_t outer_l3_len;
86         uint8_t outer_l4_proto;
87         uint16_t tso_segsz;
88         uint16_t tunnel_tso_segsz;
89         uint32_t pkt_len;
90 };
91
92 /* simplified GRE header */
93 struct simple_gre_hdr {
94         uint16_t flags;
95         uint16_t proto;
96 } __rte_packed;
97
98 static uint16_t
99 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
100 {
101         if (ethertype == _htons(RTE_ETHER_TYPE_IPV4))
102                 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
103         else /* assume ethertype == RTE_ETHER_TYPE_IPV6 */
104                 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
105 }
106
107 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
108 static void
109 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
110 {
111         struct rte_tcp_hdr *tcp_hdr;
112
113         info->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
114         info->l4_proto = ipv4_hdr->next_proto_id;
115
116         /* only fill l4_len for TCP, it's useful for TSO */
117         if (info->l4_proto == IPPROTO_TCP) {
118                 tcp_hdr = (struct rte_tcp_hdr *)
119                         ((char *)ipv4_hdr + info->l3_len);
120                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
121         } else if (info->l4_proto == IPPROTO_UDP)
122                 info->l4_len = sizeof(struct rte_udp_hdr);
123         else
124                 info->l4_len = 0;
125 }
126
127 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
128 static void
129 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
130 {
131         struct rte_tcp_hdr *tcp_hdr;
132
133         info->l3_len = sizeof(struct rte_ipv6_hdr);
134         info->l4_proto = ipv6_hdr->proto;
135
136         /* only fill l4_len for TCP, it's useful for TSO */
137         if (info->l4_proto == IPPROTO_TCP) {
138                 tcp_hdr = (struct rte_tcp_hdr *)
139                         ((char *)ipv6_hdr + info->l3_len);
140                 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
141         } else if (info->l4_proto == IPPROTO_UDP)
142                 info->l4_len = sizeof(struct rte_udp_hdr);
143         else
144                 info->l4_len = 0;
145 }
146
147 /*
148  * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
149  * ipproto. This function is able to recognize IPv4/IPv6 with optional VLAN
150  * headers. The l4_len argument is only set in case of TCP (useful for TSO).
151  */
152 static void
153 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
154 {
155         struct rte_ipv4_hdr *ipv4_hdr;
156         struct rte_ipv6_hdr *ipv6_hdr;
157         struct rte_vlan_hdr *vlan_hdr;
158
159         info->l2_len = sizeof(struct rte_ether_hdr);
160         info->ethertype = eth_hdr->ether_type;
161
162         while (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
163                info->ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
164                 vlan_hdr = (struct rte_vlan_hdr *)
165                         ((char *)eth_hdr + info->l2_len);
166                 info->l2_len  += sizeof(struct rte_vlan_hdr);
167                 info->ethertype = vlan_hdr->eth_proto;
168         }
169
170         switch (info->ethertype) {
171         case _htons(RTE_ETHER_TYPE_IPV4):
172                 ipv4_hdr = (struct rte_ipv4_hdr *)
173                         ((char *)eth_hdr + info->l2_len);
174                 parse_ipv4(ipv4_hdr, info);
175                 break;
176         case _htons(RTE_ETHER_TYPE_IPV6):
177                 ipv6_hdr = (struct rte_ipv6_hdr *)
178                         ((char *)eth_hdr + info->l2_len);
179                 parse_ipv6(ipv6_hdr, info);
180                 break;
181         default:
182                 info->l4_len = 0;
183                 info->l3_len = 0;
184                 info->l4_proto = 0;
185                 break;
186         }
187 }
188
189 /* Fill in outer layers length */
190 static void
191 update_tunnel_outer(struct testpmd_offload_info *info)
192 {
193         info->is_tunnel = 1;
194         info->outer_ethertype = info->ethertype;
195         info->outer_l2_len = info->l2_len;
196         info->outer_l3_len = info->l3_len;
197         info->outer_l4_proto = info->l4_proto;
198 }
199
200 /*
201  * Parse a GTP protocol header.
202  * No optional fields and next extension header type.
203  */
204 static void
205 parse_gtp(struct rte_udp_hdr *udp_hdr,
206           struct testpmd_offload_info *info)
207 {
208         struct rte_ipv4_hdr *ipv4_hdr;
209         struct rte_ipv6_hdr *ipv6_hdr;
210         struct rte_gtp_hdr *gtp_hdr;
211         uint8_t gtp_len = sizeof(*gtp_hdr);
212         uint8_t ip_ver;
213
214         /* Check udp destination port. */
215         if (udp_hdr->dst_port != _htons(RTE_GTPC_UDP_PORT) &&
216             udp_hdr->src_port != _htons(RTE_GTPC_UDP_PORT) &&
217             udp_hdr->dst_port != _htons(RTE_GTPU_UDP_PORT))
218                 return;
219
220         update_tunnel_outer(info);
221         info->l2_len = 0;
222
223         gtp_hdr = (struct rte_gtp_hdr *)((char *)udp_hdr +
224                   sizeof(struct rte_udp_hdr));
225
226         /*
227          * Check message type. If message type is 0xff, it is
228          * a GTP data packet. If not, it is a GTP control packet
229          */
230         if (gtp_hdr->msg_type == 0xff) {
231                 ip_ver = *(uint8_t *)((char *)udp_hdr +
232                          sizeof(struct rte_udp_hdr) +
233                          sizeof(struct rte_gtp_hdr));
234                 ip_ver = (ip_ver) & 0xf0;
235
236                 if (ip_ver == RTE_GTP_TYPE_IPV4) {
237                         ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gtp_hdr +
238                                    gtp_len);
239                         info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
240                         parse_ipv4(ipv4_hdr, info);
241                 } else if (ip_ver == RTE_GTP_TYPE_IPV6) {
242                         ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gtp_hdr +
243                                    gtp_len);
244                         info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
245                         parse_ipv6(ipv6_hdr, info);
246                 }
247         } else {
248                 info->ethertype = 0;
249                 info->l4_len = 0;
250                 info->l3_len = 0;
251                 info->l4_proto = 0;
252         }
253
254         info->l2_len += RTE_ETHER_GTP_HLEN;
255 }
256
257 /* Parse a vxlan header */
258 static void
259 parse_vxlan(struct rte_udp_hdr *udp_hdr,
260             struct testpmd_offload_info *info,
261             uint32_t pkt_type)
262 {
263         struct rte_ether_hdr *eth_hdr;
264
265         /* check udp destination port, RTE_VXLAN_DEFAULT_PORT (4789) is the
266          * default vxlan port (rfc7348) or that the rx offload flag is set
267          * (i40e only currently)
268          */
269         if (udp_hdr->dst_port != _htons(RTE_VXLAN_DEFAULT_PORT) &&
270                 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
271                 return;
272
273         update_tunnel_outer(info);
274
275         eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
276                 sizeof(struct rte_udp_hdr) +
277                 sizeof(struct rte_vxlan_hdr));
278
279         parse_ethernet(eth_hdr, info);
280         info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
281 }
282
283 /* Parse a vxlan-gpe header */
284 static void
285 parse_vxlan_gpe(struct rte_udp_hdr *udp_hdr,
286             struct testpmd_offload_info *info)
287 {
288         struct rte_ether_hdr *eth_hdr;
289         struct rte_ipv4_hdr *ipv4_hdr;
290         struct rte_ipv6_hdr *ipv6_hdr;
291         struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
292         uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
293
294         /* Check udp destination port. */
295         if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
296                 return;
297
298         vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
299                                 sizeof(struct rte_udp_hdr));
300
301         if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
302             RTE_VXLAN_GPE_TYPE_IPV4) {
303                 update_tunnel_outer(info);
304
305                 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)vxlan_gpe_hdr +
306                            vxlan_gpe_len);
307
308                 parse_ipv4(ipv4_hdr, info);
309                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
310                 info->l2_len = 0;
311
312         } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_IPV6) {
313                 update_tunnel_outer(info);
314
315                 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
316                            vxlan_gpe_len);
317
318                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
319                 parse_ipv6(ipv6_hdr, info);
320                 info->l2_len = 0;
321
322         } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
323                 update_tunnel_outer(info);
324
325                 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
326                           vxlan_gpe_len);
327
328                 parse_ethernet(eth_hdr, info);
329         } else
330                 return;
331
332         info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
333 }
334
335 /* Parse a geneve header */
336 static void
337 parse_geneve(struct rte_udp_hdr *udp_hdr,
338             struct testpmd_offload_info *info)
339 {
340         struct rte_ether_hdr *eth_hdr;
341         struct rte_ipv4_hdr *ipv4_hdr;
342         struct rte_ipv6_hdr *ipv6_hdr;
343         struct rte_geneve_hdr *geneve_hdr;
344         uint16_t geneve_len;
345
346         /* Check udp destination port. */
347         if (udp_hdr->dst_port != _htons(geneve_udp_port))
348                 return;
349
350         geneve_hdr = (struct rte_geneve_hdr *)((char *)udp_hdr +
351                                 sizeof(struct rte_udp_hdr));
352         geneve_len = sizeof(struct rte_geneve_hdr) + geneve_hdr->opt_len * 4;
353         if (!geneve_hdr->proto || geneve_hdr->proto ==
354             _htons(RTE_ETHER_TYPE_IPV4)) {
355                 update_tunnel_outer(info);
356                 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)geneve_hdr +
357                            geneve_len);
358                 parse_ipv4(ipv4_hdr, info);
359                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
360                 info->l2_len = 0;
361         } else if (geneve_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
362                 update_tunnel_outer(info);
363                 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)geneve_hdr +
364                            geneve_len);
365                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
366                 parse_ipv6(ipv6_hdr, info);
367                 info->l2_len = 0;
368
369         } else if (geneve_hdr->proto == _htons(RTE_GENEVE_TYPE_ETH)) {
370                 update_tunnel_outer(info);
371                 eth_hdr = (struct rte_ether_hdr *)((char *)geneve_hdr +
372                           geneve_len);
373                 parse_ethernet(eth_hdr, info);
374         } else
375                 return;
376
377         info->l2_len +=
378                 (sizeof(struct rte_udp_hdr) + sizeof(struct rte_geneve_hdr) +
379                 ((struct rte_geneve_hdr *)geneve_hdr)->opt_len * 4);
380 }
381
382 /* Parse a gre header */
383 static void
384 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
385 {
386         struct rte_ether_hdr *eth_hdr;
387         struct rte_ipv4_hdr *ipv4_hdr;
388         struct rte_ipv6_hdr *ipv6_hdr;
389         uint8_t gre_len = 0;
390
391         gre_len += sizeof(struct simple_gre_hdr);
392
393         if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
394                 gre_len += GRE_EXT_LEN;
395         if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
396                 gre_len += GRE_EXT_LEN;
397         if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
398                 gre_len += GRE_EXT_LEN;
399
400         if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV4)) {
401                 update_tunnel_outer(info);
402
403                 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
404
405                 parse_ipv4(ipv4_hdr, info);
406                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
407                 info->l2_len = 0;
408
409         } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
410                 update_tunnel_outer(info);
411
412                 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
413
414                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
415                 parse_ipv6(ipv6_hdr, info);
416                 info->l2_len = 0;
417
418         } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
419                 update_tunnel_outer(info);
420
421                 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
422
423                 parse_ethernet(eth_hdr, info);
424         } else
425                 return;
426
427         info->l2_len += gre_len;
428 }
429
430
431 /* Parse an encapsulated ip or ipv6 header */
432 static void
433 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
434 {
435         struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
436         struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
437         uint8_t ip_version;
438
439         ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
440
441         if (ip_version != 4 && ip_version != 6)
442                 return;
443
444         info->is_tunnel = 1;
445         info->outer_ethertype = info->ethertype;
446         info->outer_l2_len = info->l2_len;
447         info->outer_l3_len = info->l3_len;
448
449         if (ip_version == 4) {
450                 parse_ipv4(ipv4_hdr, info);
451                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
452         } else {
453                 parse_ipv6(ipv6_hdr, info);
454                 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
455         }
456         info->l2_len = 0;
457 }
458
459 /* if possible, calculate the checksum of a packet in hw or sw,
460  * depending on the testpmd command line configuration */
461 static uint64_t
462 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
463         uint64_t tx_offloads)
464 {
465         struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
466         struct rte_udp_hdr *udp_hdr;
467         struct rte_tcp_hdr *tcp_hdr;
468         struct rte_sctp_hdr *sctp_hdr;
469         uint64_t ol_flags = 0;
470         uint32_t max_pkt_len, tso_segsz = 0;
471
472         /* ensure packet is large enough to require tso */
473         if (!info->is_tunnel) {
474                 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
475                         info->tso_segsz;
476                 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
477                         tso_segsz = info->tso_segsz;
478         } else {
479                 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
480                         info->l2_len + info->l3_len + info->l4_len +
481                         info->tunnel_tso_segsz;
482                 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
483                         tso_segsz = info->tunnel_tso_segsz;
484         }
485
486         if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
487                 ipv4_hdr = l3_hdr;
488
489                 ol_flags |= RTE_MBUF_F_TX_IPV4;
490                 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
491                         ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
492                 } else {
493                         if (tx_offloads & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM) {
494                                 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
495                         } else {
496                                 ipv4_hdr->hdr_checksum = 0;
497                                 ipv4_hdr->hdr_checksum =
498                                         rte_ipv4_cksum(ipv4_hdr);
499                         }
500                 }
501         } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
502                 ol_flags |= RTE_MBUF_F_TX_IPV6;
503         else
504                 return 0; /* packet type not supported, nothing to do */
505
506         if (info->l4_proto == IPPROTO_UDP) {
507                 udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
508                 /* do not recalculate udp cksum if it was 0 */
509                 if (udp_hdr->dgram_cksum != 0) {
510                         if (tx_offloads & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
511                                 ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
512                         } else {
513                                 udp_hdr->dgram_cksum = 0;
514                                 udp_hdr->dgram_cksum =
515                                         get_udptcp_checksum(l3_hdr, udp_hdr,
516                                                 info->ethertype);
517                         }
518                 }
519 #ifdef RTE_LIB_GSO
520                 if (info->gso_enable)
521                         ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
522 #endif
523         } else if (info->l4_proto == IPPROTO_TCP) {
524                 tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
525                 if (tso_segsz)
526                         ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
527                 else if (tx_offloads & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
528                         ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
529                 } else {
530                         tcp_hdr->cksum = 0;
531                         tcp_hdr->cksum =
532                                 get_udptcp_checksum(l3_hdr, tcp_hdr,
533                                         info->ethertype);
534                 }
535 #ifdef RTE_LIB_GSO
536                 if (info->gso_enable)
537                         ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
538 #endif
539         } else if (info->l4_proto == IPPROTO_SCTP) {
540                 sctp_hdr = (struct rte_sctp_hdr *)
541                         ((char *)l3_hdr + info->l3_len);
542                 /* sctp payload must be a multiple of 4 to be
543                  * offloaded */
544                 if ((tx_offloads & RTE_ETH_TX_OFFLOAD_SCTP_CKSUM) &&
545                         ((ipv4_hdr->total_length & 0x3) == 0)) {
546                         ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
547                 } else {
548                         sctp_hdr->cksum = 0;
549                         /* XXX implement CRC32c, example available in
550                          * RFC3309 */
551                 }
552         }
553
554         return ol_flags;
555 }
556
557 /* Calculate the checksum of outer header */
558 static uint64_t
559 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
560         uint64_t tx_offloads, int tso_enabled)
561 {
562         struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
563         struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
564         struct rte_udp_hdr *udp_hdr;
565         uint64_t ol_flags = 0;
566
567         if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
568                 ipv4_hdr->hdr_checksum = 0;
569                 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV4;
570
571                 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM)
572                         ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
573                 else
574                         ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
575         } else
576                 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV6;
577
578         if (info->outer_l4_proto != IPPROTO_UDP)
579                 return ol_flags;
580
581         udp_hdr = (struct rte_udp_hdr *)
582                 ((char *)outer_l3_hdr + info->outer_l3_len);
583
584         if (tso_enabled)
585                 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
586
587         /* Skip SW outer UDP checksum generation if HW supports it */
588         if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) {
589                 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
590                         udp_hdr->dgram_cksum
591                                 = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
592                 else
593                         udp_hdr->dgram_cksum
594                                 = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
595
596                 ol_flags |= RTE_MBUF_F_TX_OUTER_UDP_CKSUM;
597                 return ol_flags;
598         }
599
600         /* outer UDP checksum is done in software. In the other side, for
601          * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
602          * set to zero.
603          *
604          * If a packet will be TSOed into small packets by NIC, we cannot
605          * set/calculate a non-zero checksum, because it will be a wrong
606          * value after the packet be split into several small packets.
607          */
608         if (tso_enabled)
609                 udp_hdr->dgram_cksum = 0;
610
611         /* do not recalculate udp cksum if it was 0 */
612         if (udp_hdr->dgram_cksum != 0) {
613                 udp_hdr->dgram_cksum = 0;
614                 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
615                         udp_hdr->dgram_cksum =
616                                 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
617                 else
618                         udp_hdr->dgram_cksum =
619                                 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
620         }
621
622         return ol_flags;
623 }
624
625 /*
626  * Helper function.
627  * Performs actual copying.
628  * Returns number of segments in the destination mbuf on success,
629  * or negative error code on failure.
630  */
631 static int
632 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
633         uint16_t seglen[], uint8_t nb_seg)
634 {
635         uint32_t dlen, slen, tlen;
636         uint32_t i, len;
637         const struct rte_mbuf *m;
638         const uint8_t *src;
639         uint8_t *dst;
640
641         dlen = 0;
642         slen = 0;
643         tlen = 0;
644
645         dst = NULL;
646         src = NULL;
647
648         m = ms;
649         i = 0;
650         while (ms != NULL && i != nb_seg) {
651
652                 if (slen == 0) {
653                         slen = rte_pktmbuf_data_len(ms);
654                         src = rte_pktmbuf_mtod(ms, const uint8_t *);
655                 }
656
657                 if (dlen == 0) {
658                         dlen = RTE_MIN(seglen[i], slen);
659                         md[i]->data_len = dlen;
660                         md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
661                         dst = rte_pktmbuf_mtod(md[i], uint8_t *);
662                 }
663
664                 len = RTE_MIN(slen, dlen);
665                 memcpy(dst, src, len);
666                 tlen += len;
667                 slen -= len;
668                 dlen -= len;
669                 src += len;
670                 dst += len;
671
672                 if (slen == 0)
673                         ms = ms->next;
674                 if (dlen == 0)
675                         i++;
676         }
677
678         if (ms != NULL)
679                 return -ENOBUFS;
680         else if (tlen != m->pkt_len)
681                 return -EINVAL;
682
683         md[0]->nb_segs = nb_seg;
684         md[0]->pkt_len = tlen;
685         md[0]->vlan_tci = m->vlan_tci;
686         md[0]->vlan_tci_outer = m->vlan_tci_outer;
687         md[0]->ol_flags = m->ol_flags;
688         md[0]->tx_offload = m->tx_offload;
689
690         return nb_seg;
691 }
692
693 /*
694  * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
695  * Copy packet contents and offload information into the new segmented mbuf.
696  */
697 static struct rte_mbuf *
698 pkt_copy_split(const struct rte_mbuf *pkt)
699 {
700         int32_t n, rc;
701         uint32_t i, len, nb_seg;
702         struct rte_mempool *mp;
703         uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
704         struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
705
706         mp = current_fwd_lcore()->mbp;
707
708         if (tx_pkt_split == TX_PKT_SPLIT_RND)
709                 nb_seg = rte_rand() % tx_pkt_nb_segs + 1;
710         else
711                 nb_seg = tx_pkt_nb_segs;
712
713         memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
714
715         /* calculate number of segments to use and their length. */
716         len = 0;
717         for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
718                 len += seglen[i];
719                 md[i] = NULL;
720         }
721
722         n = pkt->pkt_len - len;
723
724         /* update size of the last segment to fit rest of the packet */
725         if (n >= 0) {
726                 seglen[i - 1] += n;
727                 len += n;
728         }
729
730         nb_seg = i;
731         while (i != 0) {
732                 p = rte_pktmbuf_alloc(mp);
733                 if (p == NULL) {
734                         TESTPMD_LOG(ERR,
735                                 "failed to allocate %u-th of %u mbuf "
736                                 "from mempool: %s\n",
737                                 nb_seg - i, nb_seg, mp->name);
738                         break;
739                 }
740
741                 md[--i] = p;
742                 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
743                         TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
744                                 "expected seglen: %u, "
745                                 "actual mbuf tailroom: %u\n",
746                                 mp->name, i, seglen[i],
747                                 rte_pktmbuf_tailroom(md[i]));
748                         break;
749                 }
750         }
751
752         /* all mbufs successfully allocated, do copy */
753         if (i == 0) {
754                 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
755                 if (rc < 0)
756                         TESTPMD_LOG(ERR,
757                                 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
758                                 "into %u segments failed with error code: %d\n",
759                                 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
760
761                 /* figure out how many mbufs to free. */
762                 i = RTE_MAX(rc, 0);
763         }
764
765         /* free unused mbufs */
766         for (; i != nb_seg; i++) {
767                 rte_pktmbuf_free_seg(md[i]);
768                 md[i] = NULL;
769         }
770
771         return md[0];
772 }
773
774 /*
775  * Receive a burst of packets, and for each packet:
776  *  - parse packet, and try to recognize a supported packet type (1)
777  *  - if it's not a supported packet type, don't touch the packet, else:
778  *  - reprocess the checksum of all supported layers. This is done in SW
779  *    or HW, depending on testpmd command line configuration
780  *  - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
781  *    segmentation offload (this implies HW TCP checksum)
782  * Then transmit packets on the output port.
783  *
784  * (1) Supported packets are:
785  *   Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
786  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
787  *           UDP|TCP|SCTP
788  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
789  *           UDP|TCP|SCTP
790  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
791  *           UDP|TCP|SCTP
792  *   Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
793  *   Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
794  *   Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
795  *   Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
796  *
797  * The testpmd command line for this forward engine sets the flags
798  * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
799  * whether a checksum must be calculated in software or in hardware. The
800  * IP, UDP, TCP and SCTP flags always concern the inner layer. The
801  * OUTER_IP is only useful for tunnel packets.
802  */
803 static void
804 pkt_burst_checksum_forward(struct fwd_stream *fs)
805 {
806         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
807 #ifdef RTE_LIB_GSO
808         struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
809         struct rte_gso_ctx *gso_ctx;
810 #endif
811         struct rte_mbuf **tx_pkts_burst;
812         struct rte_port *txp;
813         struct rte_mbuf *m, *p;
814         struct rte_ether_hdr *eth_hdr;
815         void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
816 #ifdef RTE_LIB_GRO
817         void **gro_ctx;
818         uint16_t gro_pkts_num;
819         uint8_t gro_enable;
820 #endif
821         uint16_t nb_rx;
822         uint16_t nb_tx;
823         uint16_t nb_prep;
824         uint16_t i;
825         uint64_t rx_ol_flags, tx_ol_flags;
826         uint64_t tx_offloads;
827         uint32_t retry;
828         uint32_t rx_bad_ip_csum;
829         uint32_t rx_bad_l4_csum;
830         uint32_t rx_bad_outer_l4_csum;
831         uint32_t rx_bad_outer_ip_csum;
832         struct testpmd_offload_info info;
833
834         uint64_t start_tsc = 0;
835
836         get_start_cycles(&start_tsc);
837
838         /* receive a burst of packet */
839         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
840                                  nb_pkt_per_burst);
841         inc_rx_burst_stats(fs, nb_rx);
842         if (unlikely(nb_rx == 0))
843                 return;
844
845         fs->rx_packets += nb_rx;
846         rx_bad_ip_csum = 0;
847         rx_bad_l4_csum = 0;
848         rx_bad_outer_l4_csum = 0;
849         rx_bad_outer_ip_csum = 0;
850 #ifdef RTE_LIB_GRO
851         gro_enable = gro_ports[fs->rx_port].enable;
852 #endif
853
854         txp = &ports[fs->tx_port];
855         tx_offloads = txp->dev_conf.txmode.offloads;
856         memset(&info, 0, sizeof(info));
857         info.tso_segsz = txp->tso_segsz;
858         info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
859 #ifdef RTE_LIB_GSO
860         if (gso_ports[fs->tx_port].enable)
861                 info.gso_enable = 1;
862 #endif
863
864         for (i = 0; i < nb_rx; i++) {
865                 if (likely(i < nb_rx - 1))
866                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
867                                                        void *));
868
869                 m = pkts_burst[i];
870                 info.is_tunnel = 0;
871                 info.pkt_len = rte_pktmbuf_pkt_len(m);
872                 tx_ol_flags = m->ol_flags &
873                               (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL);
874                 rx_ol_flags = m->ol_flags;
875
876                 /* Update the L3/L4 checksum error packet statistics */
877                 if ((rx_ol_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) == RTE_MBUF_F_RX_IP_CKSUM_BAD)
878                         rx_bad_ip_csum += 1;
879                 if ((rx_ol_flags & RTE_MBUF_F_RX_L4_CKSUM_MASK) == RTE_MBUF_F_RX_L4_CKSUM_BAD)
880                         rx_bad_l4_csum += 1;
881                 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD)
882                         rx_bad_outer_l4_csum += 1;
883                 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD)
884                         rx_bad_outer_ip_csum += 1;
885
886                 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
887                  * and inner headers */
888
889                 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
890                 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
891                                 &eth_hdr->dst_addr);
892                 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
893                                 &eth_hdr->src_addr);
894                 parse_ethernet(eth_hdr, &info);
895                 l3_hdr = (char *)eth_hdr + info.l2_len;
896
897                 /* check if it's a supported tunnel */
898                 if (txp->parse_tunnel) {
899                         if (info.l4_proto == IPPROTO_UDP) {
900                                 struct rte_udp_hdr *udp_hdr;
901
902                                 udp_hdr = (struct rte_udp_hdr *)
903                                         ((char *)l3_hdr + info.l3_len);
904                                 parse_gtp(udp_hdr, &info);
905                                 if (info.is_tunnel) {
906                                         tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GTP;
907                                         goto tunnel_update;
908                                 }
909                                 parse_vxlan_gpe(udp_hdr, &info);
910                                 if (info.is_tunnel) {
911                                         tx_ol_flags |=
912                                                 RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE;
913                                         goto tunnel_update;
914                                 }
915                                 parse_vxlan(udp_hdr, &info,
916                                             m->packet_type);
917                                 if (info.is_tunnel) {
918                                         tx_ol_flags |=
919                                                 RTE_MBUF_F_TX_TUNNEL_VXLAN;
920                                         goto tunnel_update;
921                                 }
922                                 parse_geneve(udp_hdr, &info);
923                                 if (info.is_tunnel) {
924                                         tx_ol_flags |=
925                                                 RTE_MBUF_F_TX_TUNNEL_GENEVE;
926                                         goto tunnel_update;
927                                 }
928                         } else if (info.l4_proto == IPPROTO_GRE) {
929                                 struct simple_gre_hdr *gre_hdr;
930
931                                 gre_hdr = (struct simple_gre_hdr *)
932                                         ((char *)l3_hdr + info.l3_len);
933                                 parse_gre(gre_hdr, &info);
934                                 if (info.is_tunnel)
935                                         tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GRE;
936                         } else if (info.l4_proto == IPPROTO_IPIP) {
937                                 void *encap_ip_hdr;
938
939                                 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
940                                 parse_encap_ip(encap_ip_hdr, &info);
941                                 if (info.is_tunnel)
942                                         tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_IPIP;
943                         }
944                 }
945
946 tunnel_update:
947                 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
948                 if (info.is_tunnel) {
949                         outer_l3_hdr = l3_hdr;
950                         l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
951                 }
952
953                 /* step 2: depending on user command line configuration,
954                  * recompute checksum either in software or flag the
955                  * mbuf to offload the calculation to the NIC. If TSO
956                  * is configured, prepare the mbuf for TCP segmentation. */
957
958                 /* process checksums of inner headers first */
959                 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
960                         tx_offloads);
961
962                 /* Then process outer headers if any. Note that the software
963                  * checksum will be wrong if one of the inner checksums is
964                  * processed in hardware. */
965                 if (info.is_tunnel == 1) {
966                         tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
967                                         tx_offloads,
968                                         !!(tx_ol_flags & RTE_MBUF_F_TX_TCP_SEG));
969                 }
970
971                 /* step 3: fill the mbuf meta data (flags and header lengths) */
972
973                 m->tx_offload = 0;
974                 if (info.is_tunnel == 1) {
975                         if (info.tunnel_tso_segsz ||
976                             (tx_offloads &
977                              RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
978                             (tx_offloads &
979                              RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM)) {
980                                 m->outer_l2_len = info.outer_l2_len;
981                                 m->outer_l3_len = info.outer_l3_len;
982                                 m->l2_len = info.l2_len;
983                                 m->l3_len = info.l3_len;
984                                 m->l4_len = info.l4_len;
985                                 m->tso_segsz = info.tunnel_tso_segsz;
986                         }
987                         else {
988                                 /* if there is a outer UDP cksum
989                                    processed in sw and the inner in hw,
990                                    the outer checksum will be wrong as
991                                    the payload will be modified by the
992                                    hardware */
993                                 m->l2_len = info.outer_l2_len +
994                                         info.outer_l3_len + info.l2_len;
995                                 m->l3_len = info.l3_len;
996                                 m->l4_len = info.l4_len;
997                         }
998                 } else {
999                         /* this is only useful if an offload flag is
1000                          * set, but it does not hurt to fill it in any
1001                          * case */
1002                         m->l2_len = info.l2_len;
1003                         m->l3_len = info.l3_len;
1004                         m->l4_len = info.l4_len;
1005                         m->tso_segsz = info.tso_segsz;
1006                 }
1007                 m->ol_flags = tx_ol_flags;
1008
1009                 /* Do split & copy for the packet. */
1010                 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
1011                         p = pkt_copy_split(m);
1012                         if (p != NULL) {
1013                                 rte_pktmbuf_free(m);
1014                                 m = p;
1015                                 pkts_burst[i] = m;
1016                         }
1017                 }
1018
1019                 /* if verbose mode is enabled, dump debug info */
1020                 if (verbose_level > 0) {
1021                         char buf[256];
1022
1023                         printf("-----------------\n");
1024                         printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
1025                                 fs->rx_port, m, m->pkt_len, m->nb_segs);
1026                         /* dump rx parsed packet info */
1027                         rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
1028                         printf("rx: l2_len=%d ethertype=%x l3_len=%d "
1029                                 "l4_proto=%d l4_len=%d flags=%s\n",
1030                                 info.l2_len, rte_be_to_cpu_16(info.ethertype),
1031                                 info.l3_len, info.l4_proto, info.l4_len, buf);
1032                         if (rx_ol_flags & RTE_MBUF_F_RX_LRO)
1033                                 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
1034                         if (info.is_tunnel == 1)
1035                                 printf("rx: outer_l2_len=%d outer_ethertype=%x "
1036                                         "outer_l3_len=%d\n", info.outer_l2_len,
1037                                         rte_be_to_cpu_16(info.outer_ethertype),
1038                                         info.outer_l3_len);
1039                         /* dump tx packet info */
1040                         if ((tx_offloads & (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1041                                             RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1042                                             RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
1043                                             RTE_ETH_TX_OFFLOAD_SCTP_CKSUM)) ||
1044                                 info.tso_segsz != 0)
1045                                 printf("tx: m->l2_len=%d m->l3_len=%d "
1046                                         "m->l4_len=%d\n",
1047                                         m->l2_len, m->l3_len, m->l4_len);
1048                         if (info.is_tunnel == 1) {
1049                                 if ((tx_offloads &
1050                                     RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1051                                     (tx_offloads &
1052                                     RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
1053                                     (tx_ol_flags & RTE_MBUF_F_TX_OUTER_IPV6))
1054                                         printf("tx: m->outer_l2_len=%d "
1055                                                 "m->outer_l3_len=%d\n",
1056                                                 m->outer_l2_len,
1057                                                 m->outer_l3_len);
1058                                 if (info.tunnel_tso_segsz != 0 &&
1059                                                 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1060                                         printf("tx: m->tso_segsz=%d\n",
1061                                                 m->tso_segsz);
1062                         } else if (info.tso_segsz != 0 &&
1063                                         (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1064                                 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
1065                         rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
1066                         printf("tx: flags=%s", buf);
1067                         printf("\n");
1068                 }
1069         }
1070
1071 #ifdef RTE_LIB_GRO
1072         if (unlikely(gro_enable)) {
1073                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
1074                         nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
1075                                         &(gro_ports[fs->rx_port].param));
1076                 } else {
1077                         gro_ctx = current_fwd_lcore()->gro_ctx;
1078                         nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
1079
1080                         if (++fs->gro_times >= gro_flush_cycles) {
1081                                 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
1082                                 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
1083                                         gro_pkts_num = MAX_PKT_BURST - nb_rx;
1084
1085                                 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
1086                                                 RTE_GRO_TCP_IPV4,
1087                                                 &pkts_burst[nb_rx],
1088                                                 gro_pkts_num);
1089                                 fs->gro_times = 0;
1090                         }
1091                 }
1092         }
1093 #endif
1094
1095 #ifdef RTE_LIB_GSO
1096         if (gso_ports[fs->tx_port].enable != 0) {
1097                 uint16_t nb_segments = 0;
1098
1099                 gso_ctx = &(current_fwd_lcore()->gso_ctx);
1100                 gso_ctx->gso_size = gso_max_segment_size;
1101                 for (i = 0; i < nb_rx; i++) {
1102                         int ret;
1103
1104                         ret = rte_gso_segment(pkts_burst[i], gso_ctx,
1105                                         &gso_segments[nb_segments],
1106                                         GSO_MAX_PKT_BURST - nb_segments);
1107                         if (ret >= 1) {
1108                                 /* pkts_burst[i] can be freed safely here. */
1109                                 rte_pktmbuf_free(pkts_burst[i]);
1110                                 nb_segments += ret;
1111                         } else if (ret == 0) {
1112                                 /* 0 means it can be transmitted directly
1113                                  * without gso.
1114                                  */
1115                                 gso_segments[nb_segments] = pkts_burst[i];
1116                                 nb_segments += 1;
1117                         } else {
1118                                 TESTPMD_LOG(DEBUG, "Unable to segment packet");
1119                                 rte_pktmbuf_free(pkts_burst[i]);
1120                         }
1121                 }
1122
1123                 tx_pkts_burst = gso_segments;
1124                 nb_rx = nb_segments;
1125         } else
1126 #endif
1127                 tx_pkts_burst = pkts_burst;
1128
1129         nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
1130                         tx_pkts_burst, nb_rx);
1131         if (nb_prep != nb_rx)
1132                 fprintf(stderr,
1133                         "Preparing packet burst to transmit failed: %s\n",
1134                         rte_strerror(rte_errno));
1135
1136         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
1137                         nb_prep);
1138
1139         /*
1140          * Retry if necessary
1141          */
1142         if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
1143                 retry = 0;
1144                 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
1145                         rte_delay_us(burst_tx_delay_time);
1146                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1147                                         &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1148                 }
1149         }
1150         fs->tx_packets += nb_tx;
1151         fs->rx_bad_ip_csum += rx_bad_ip_csum;
1152         fs->rx_bad_l4_csum += rx_bad_l4_csum;
1153         fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1154         fs->rx_bad_outer_ip_csum += rx_bad_outer_ip_csum;
1155
1156         inc_tx_burst_stats(fs, nb_tx);
1157         if (unlikely(nb_tx < nb_rx)) {
1158                 fs->fwd_dropped += (nb_rx - nb_tx);
1159                 do {
1160                         rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1161                 } while (++nb_tx < nb_rx);
1162         }
1163
1164         get_end_cycles(fs, start_tsc);
1165 }
1166
1167 struct fwd_engine csum_fwd_engine = {
1168         .fwd_mode_name  = "csum",
1169         .port_fwd_begin = NULL,
1170         .port_fwd_end   = NULL,
1171         .packet_fwd     = pkt_burst_checksum_forward,
1172 };