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