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