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