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