4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/queue.h>
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
48 #include <rte_debug.h>
49 #include <rte_cycles.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_mempool.h>
61 #include <rte_interrupts.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
69 #include <rte_prefetch.h>
70 #include <rte_string_fns.h>
77 #define IP_DEFTTL 64 /* from RFC 1340. */
78 #define IP_VERSION 0x40
79 #define IP_HDRLEN 0x05 /* default IP header length == five 32-bits words. */
80 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
82 #define GRE_KEY_PRESENT 0x2000
84 #define GRE_SUPPORTED_FIELDS GRE_KEY_PRESENT
86 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
87 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
88 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
93 /* structure that caches offload info for the current packet */
94 struct testpmd_offload_info {
102 uint16_t outer_ethertype;
103 uint16_t outer_l2_len;
104 uint16_t outer_l3_len;
105 uint8_t outer_l4_proto;
107 uint16_t tunnel_tso_segsz;
111 /* simplified GRE header */
112 struct simple_gre_hdr {
115 } __attribute__((__packed__));
118 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
120 if (ethertype == _htons(ETHER_TYPE_IPv4))
121 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
122 else /* assume ethertype == ETHER_TYPE_IPv6 */
123 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
126 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
128 parse_ipv4(struct ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
130 struct tcp_hdr *tcp_hdr;
132 info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
133 info->l4_proto = ipv4_hdr->next_proto_id;
135 /* only fill l4_len for TCP, it's useful for TSO */
136 if (info->l4_proto == IPPROTO_TCP) {
137 tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + info->l3_len);
138 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
143 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
145 parse_ipv6(struct ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
147 struct tcp_hdr *tcp_hdr;
149 info->l3_len = sizeof(struct ipv6_hdr);
150 info->l4_proto = ipv6_hdr->proto;
152 /* only fill l4_len for TCP, it's useful for TSO */
153 if (info->l4_proto == IPPROTO_TCP) {
154 tcp_hdr = (struct tcp_hdr *)((char *)ipv6_hdr + info->l3_len);
155 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
161 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
162 * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
163 * header. The l4_len argument is only set in case of TCP (useful for TSO).
166 parse_ethernet(struct ether_hdr *eth_hdr, struct testpmd_offload_info *info)
168 struct ipv4_hdr *ipv4_hdr;
169 struct ipv6_hdr *ipv6_hdr;
171 info->l2_len = sizeof(struct ether_hdr);
172 info->ethertype = eth_hdr->ether_type;
174 if (info->ethertype == _htons(ETHER_TYPE_VLAN)) {
175 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
177 info->l2_len += sizeof(struct vlan_hdr);
178 info->ethertype = vlan_hdr->eth_proto;
181 switch (info->ethertype) {
182 case _htons(ETHER_TYPE_IPv4):
183 ipv4_hdr = (struct ipv4_hdr *) ((char *)eth_hdr + info->l2_len);
184 parse_ipv4(ipv4_hdr, info);
186 case _htons(ETHER_TYPE_IPv6):
187 ipv6_hdr = (struct ipv6_hdr *) ((char *)eth_hdr + info->l2_len);
188 parse_ipv6(ipv6_hdr, info);
198 /* Parse a vxlan header */
200 parse_vxlan(struct udp_hdr *udp_hdr,
201 struct testpmd_offload_info *info,
204 struct ether_hdr *eth_hdr;
206 /* check udp destination port, 4789 is the default vxlan port
207 * (rfc7348) or that the rx offload flag is set (i40e only
209 if (udp_hdr->dst_port != _htons(4789) &&
210 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
214 info->outer_ethertype = info->ethertype;
215 info->outer_l2_len = info->l2_len;
216 info->outer_l3_len = info->l3_len;
217 info->outer_l4_proto = info->l4_proto;
219 eth_hdr = (struct ether_hdr *)((char *)udp_hdr +
220 sizeof(struct udp_hdr) +
221 sizeof(struct vxlan_hdr));
223 parse_ethernet(eth_hdr, info);
224 info->l2_len += ETHER_VXLAN_HLEN; /* add udp + vxlan */
227 /* Parse a gre header */
229 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
231 struct ether_hdr *eth_hdr;
232 struct ipv4_hdr *ipv4_hdr;
233 struct ipv6_hdr *ipv6_hdr;
236 /* check which fields are supported */
237 if ((gre_hdr->flags & _htons(~GRE_SUPPORTED_FIELDS)) != 0)
240 gre_len += sizeof(struct simple_gre_hdr);
242 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
243 gre_len += GRE_KEY_LEN;
245 if (gre_hdr->proto == _htons(ETHER_TYPE_IPv4)) {
247 info->outer_ethertype = info->ethertype;
248 info->outer_l2_len = info->l2_len;
249 info->outer_l3_len = info->l3_len;
250 info->outer_l4_proto = info->l4_proto;
252 ipv4_hdr = (struct ipv4_hdr *)((char *)gre_hdr + gre_len);
254 parse_ipv4(ipv4_hdr, info);
255 info->ethertype = _htons(ETHER_TYPE_IPv4);
258 } else if (gre_hdr->proto == _htons(ETHER_TYPE_IPv6)) {
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;
265 ipv6_hdr = (struct ipv6_hdr *)((char *)gre_hdr + gre_len);
267 info->ethertype = _htons(ETHER_TYPE_IPv6);
268 parse_ipv6(ipv6_hdr, info);
271 } else if (gre_hdr->proto == _htons(ETHER_TYPE_TEB)) {
273 info->outer_ethertype = info->ethertype;
274 info->outer_l2_len = info->l2_len;
275 info->outer_l3_len = info->l3_len;
276 info->outer_l4_proto = info->l4_proto;
278 eth_hdr = (struct ether_hdr *)((char *)gre_hdr + gre_len);
280 parse_ethernet(eth_hdr, info);
284 info->l2_len += gre_len;
288 /* Parse an encapsulated ip or ipv6 header */
290 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
292 struct ipv4_hdr *ipv4_hdr = encap_ip;
293 struct ipv6_hdr *ipv6_hdr = encap_ip;
296 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
298 if (ip_version != 4 && ip_version != 6)
302 info->outer_ethertype = info->ethertype;
303 info->outer_l2_len = info->l2_len;
304 info->outer_l3_len = info->l3_len;
306 if (ip_version == 4) {
307 parse_ipv4(ipv4_hdr, info);
308 info->ethertype = _htons(ETHER_TYPE_IPv4);
310 parse_ipv6(ipv6_hdr, info);
311 info->ethertype = _htons(ETHER_TYPE_IPv6);
316 /* if possible, calculate the checksum of a packet in hw or sw,
317 * depending on the testpmd command line configuration */
319 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
320 uint16_t testpmd_ol_flags)
322 struct ipv4_hdr *ipv4_hdr = l3_hdr;
323 struct udp_hdr *udp_hdr;
324 struct tcp_hdr *tcp_hdr;
325 struct sctp_hdr *sctp_hdr;
326 uint64_t ol_flags = 0;
327 uint32_t max_pkt_len, tso_segsz = 0;
329 /* ensure packet is large enough to require tso */
330 if (!info->is_tunnel) {
331 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
333 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
334 tso_segsz = info->tso_segsz;
336 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
337 info->l2_len + info->l3_len + info->l4_len +
338 info->tunnel_tso_segsz;
339 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
340 tso_segsz = info->tunnel_tso_segsz;
343 if (info->ethertype == _htons(ETHER_TYPE_IPv4)) {
345 ipv4_hdr->hdr_checksum = 0;
347 ol_flags |= PKT_TX_IPV4;
348 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
349 ol_flags |= PKT_TX_IP_CKSUM;
351 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_IP_CKSUM)
352 ol_flags |= PKT_TX_IP_CKSUM;
354 ipv4_hdr->hdr_checksum =
355 rte_ipv4_cksum(ipv4_hdr);
357 } else if (info->ethertype == _htons(ETHER_TYPE_IPv6))
358 ol_flags |= PKT_TX_IPV6;
360 return 0; /* packet type not supported, nothing to do */
362 if (info->l4_proto == IPPROTO_UDP) {
363 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
364 /* do not recalculate udp cksum if it was 0 */
365 if (udp_hdr->dgram_cksum != 0) {
366 udp_hdr->dgram_cksum = 0;
367 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_UDP_CKSUM)
368 ol_flags |= PKT_TX_UDP_CKSUM;
370 udp_hdr->dgram_cksum =
371 get_udptcp_checksum(l3_hdr, udp_hdr,
375 } else if (info->l4_proto == IPPROTO_TCP) {
376 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
379 ol_flags |= PKT_TX_TCP_SEG;
380 else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_TCP_CKSUM)
381 ol_flags |= PKT_TX_TCP_CKSUM;
384 get_udptcp_checksum(l3_hdr, tcp_hdr,
387 if (info->gso_enable)
388 ol_flags |= PKT_TX_TCP_SEG;
389 } else if (info->l4_proto == IPPROTO_SCTP) {
390 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
392 /* sctp payload must be a multiple of 4 to be
394 if ((testpmd_ol_flags & TESTPMD_TX_OFFLOAD_SCTP_CKSUM) &&
395 ((ipv4_hdr->total_length & 0x3) == 0)) {
396 ol_flags |= PKT_TX_SCTP_CKSUM;
398 /* XXX implement CRC32c, example available in
406 /* Calculate the checksum of outer header */
408 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
409 uint16_t testpmd_ol_flags, int tso_enabled)
411 struct ipv4_hdr *ipv4_hdr = outer_l3_hdr;
412 struct ipv6_hdr *ipv6_hdr = outer_l3_hdr;
413 struct udp_hdr *udp_hdr;
414 uint64_t ol_flags = 0;
416 if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4)) {
417 ipv4_hdr->hdr_checksum = 0;
418 ol_flags |= PKT_TX_OUTER_IPV4;
420 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
421 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
423 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
425 ol_flags |= PKT_TX_OUTER_IPV6;
427 if (info->outer_l4_proto != IPPROTO_UDP)
430 udp_hdr = (struct udp_hdr *)((char *)outer_l3_hdr + info->outer_l3_len);
432 /* outer UDP checksum is done in software as we have no hardware
433 * supporting it today, and no API for it. In the other side, for
434 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
437 * If a packet will be TSOed into small packets by NIC, we cannot
438 * set/calculate a non-zero checksum, because it will be a wrong
439 * value after the packet be split into several small packets.
442 udp_hdr->dgram_cksum = 0;
444 /* do not recalculate udp cksum if it was 0 */
445 if (udp_hdr->dgram_cksum != 0) {
446 udp_hdr->dgram_cksum = 0;
447 if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4))
448 udp_hdr->dgram_cksum =
449 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
451 udp_hdr->dgram_cksum =
452 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
460 * Performs actual copying.
461 * Returns number of segments in the destination mbuf on success,
462 * or negative error code on failure.
465 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
466 uint16_t seglen[], uint8_t nb_seg)
468 uint32_t dlen, slen, tlen;
470 const struct rte_mbuf *m;
483 while (ms != NULL && i != nb_seg) {
486 slen = rte_pktmbuf_data_len(ms);
487 src = rte_pktmbuf_mtod(ms, const uint8_t *);
491 dlen = RTE_MIN(seglen[i], slen);
492 md[i]->data_len = dlen;
493 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
494 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
497 len = RTE_MIN(slen, dlen);
498 memcpy(dst, src, len);
513 else if (tlen != m->pkt_len)
516 md[0]->nb_segs = nb_seg;
517 md[0]->pkt_len = tlen;
518 md[0]->vlan_tci = m->vlan_tci;
519 md[0]->vlan_tci_outer = m->vlan_tci_outer;
520 md[0]->ol_flags = m->ol_flags;
521 md[0]->tx_offload = m->tx_offload;
527 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
528 * Copy packet contents and offload information into then new segmented mbuf.
530 static struct rte_mbuf *
531 pkt_copy_split(const struct rte_mbuf *pkt)
534 uint32_t i, len, nb_seg;
535 struct rte_mempool *mp;
536 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
537 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
539 mp = current_fwd_lcore()->mbp;
541 if (tx_pkt_split == TX_PKT_SPLIT_RND)
542 nb_seg = random() % tx_pkt_nb_segs + 1;
544 nb_seg = tx_pkt_nb_segs;
546 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
548 /* calculate number of segments to use and their length. */
550 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
555 n = pkt->pkt_len - len;
557 /* update size of the last segment to fit rest of the packet */
565 p = rte_pktmbuf_alloc(mp);
568 "failed to allocate %u-th of %u mbuf "
569 "from mempool: %s\n",
570 nb_seg - i, nb_seg, mp->name);
575 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
576 RTE_LOG(ERR, USER1, "mempool %s, %u-th segment: "
577 "expected seglen: %u, "
578 "actual mbuf tailroom: %u\n",
579 mp->name, i, seglen[i],
580 rte_pktmbuf_tailroom(md[i]));
585 /* all mbufs successfully allocated, do copy */
587 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
590 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
591 "into %u segments failed with error code: %d\n",
592 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
594 /* figure out how many mbufs to free. */
598 /* free unused mbufs */
599 for (; i != nb_seg; i++) {
600 rte_pktmbuf_free_seg(md[i]);
608 * Receive a burst of packets, and for each packet:
609 * - parse packet, and try to recognize a supported packet type (1)
610 * - if it's not a supported packet type, don't touch the packet, else:
611 * - reprocess the checksum of all supported layers. This is done in SW
612 * or HW, depending on testpmd command line configuration
613 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
614 * segmentation offload (this implies HW TCP checksum)
615 * Then transmit packets on the output port.
617 * (1) Supported packets are:
618 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
619 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
621 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
622 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
623 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
625 * The testpmd command line for this forward engine sets the flags
626 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
627 * wether a checksum must be calculated in software or in hardware. The
628 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
629 * OUTER_IP is only useful for tunnel packets.
632 pkt_burst_checksum_forward(struct fwd_stream *fs)
634 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
635 struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
636 struct rte_gso_ctx *gso_ctx;
637 struct rte_mbuf **tx_pkts_burst;
638 struct rte_port *txp;
639 struct rte_mbuf *m, *p;
640 struct ether_hdr *eth_hdr;
641 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
643 uint16_t gro_pkts_num;
649 uint64_t rx_ol_flags, tx_ol_flags;
650 uint16_t testpmd_ol_flags;
652 uint32_t rx_bad_ip_csum;
653 uint32_t rx_bad_l4_csum;
654 struct testpmd_offload_info info;
655 uint16_t nb_segments = 0;
658 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
661 uint64_t core_cycles;
664 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
665 start_tsc = rte_rdtsc();
668 /* receive a burst of packet */
669 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
671 if (unlikely(nb_rx == 0))
673 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
674 fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
676 fs->rx_packets += nb_rx;
679 gro_enable = gro_ports[fs->rx_port].enable;
681 txp = &ports[fs->tx_port];
682 testpmd_ol_flags = txp->tx_ol_flags;
683 memset(&info, 0, sizeof(info));
684 info.tso_segsz = txp->tso_segsz;
685 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
686 if (gso_ports[fs->tx_port].enable)
689 for (i = 0; i < nb_rx; i++) {
690 if (likely(i < nb_rx - 1))
691 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
696 info.pkt_len = rte_pktmbuf_pkt_len(m);
698 rx_ol_flags = m->ol_flags;
700 /* Update the L3/L4 checksum error packet statistics */
701 if ((rx_ol_flags & PKT_RX_IP_CKSUM_MASK) == PKT_RX_IP_CKSUM_BAD)
703 if ((rx_ol_flags & PKT_RX_L4_CKSUM_MASK) == PKT_RX_L4_CKSUM_BAD)
706 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
707 * and inner headers */
709 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
710 ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
712 ether_addr_copy(&ports[fs->tx_port].eth_addr,
714 parse_ethernet(eth_hdr, &info);
715 l3_hdr = (char *)eth_hdr + info.l2_len;
717 /* check if it's a supported tunnel */
718 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_PARSE_TUNNEL) {
719 if (info.l4_proto == IPPROTO_UDP) {
720 struct udp_hdr *udp_hdr;
722 udp_hdr = (struct udp_hdr *)((char *)l3_hdr +
724 parse_vxlan(udp_hdr, &info, m->packet_type);
726 tx_ol_flags |= PKT_TX_TUNNEL_VXLAN;
727 } else if (info.l4_proto == IPPROTO_GRE) {
728 struct simple_gre_hdr *gre_hdr;
730 gre_hdr = (struct simple_gre_hdr *)
731 ((char *)l3_hdr + info.l3_len);
732 parse_gre(gre_hdr, &info);
734 tx_ol_flags |= PKT_TX_TUNNEL_GRE;
735 } else if (info.l4_proto == IPPROTO_IPIP) {
738 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
739 parse_encap_ip(encap_ip_hdr, &info);
741 tx_ol_flags |= PKT_TX_TUNNEL_IPIP;
745 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
746 if (info.is_tunnel) {
747 outer_l3_hdr = l3_hdr;
748 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
751 /* step 2: depending on user command line configuration,
752 * recompute checksum either in software or flag the
753 * mbuf to offload the calculation to the NIC. If TSO
754 * is configured, prepare the mbuf for TCP segmentation. */
756 /* process checksums of inner headers first */
757 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
760 /* Then process outer headers if any. Note that the software
761 * checksum will be wrong if one of the inner checksums is
762 * processed in hardware. */
763 if (info.is_tunnel == 1) {
764 tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
766 !!(tx_ol_flags & PKT_TX_TCP_SEG));
769 /* step 3: fill the mbuf meta data (flags and header lengths) */
771 if (info.is_tunnel == 1) {
772 if (info.tunnel_tso_segsz ||
774 TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) ||
775 (tx_ol_flags & PKT_TX_OUTER_IPV6)) {
776 m->outer_l2_len = info.outer_l2_len;
777 m->outer_l3_len = info.outer_l3_len;
778 m->l2_len = info.l2_len;
779 m->l3_len = info.l3_len;
780 m->l4_len = info.l4_len;
781 m->tso_segsz = info.tunnel_tso_segsz;
784 /* if there is a outer UDP cksum
785 processed in sw and the inner in hw,
786 the outer checksum will be wrong as
787 the payload will be modified by the
789 m->l2_len = info.outer_l2_len +
790 info.outer_l3_len + info.l2_len;
791 m->l3_len = info.l3_len;
792 m->l4_len = info.l4_len;
795 /* this is only useful if an offload flag is
796 * set, but it does not hurt to fill it in any
798 m->l2_len = info.l2_len;
799 m->l3_len = info.l3_len;
800 m->l4_len = info.l4_len;
801 m->tso_segsz = info.tso_segsz;
803 m->ol_flags = tx_ol_flags;
805 /* Do split & copy for the packet. */
806 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
807 p = pkt_copy_split(m);
815 /* if verbose mode is enabled, dump debug info */
816 if (verbose_level > 0) {
819 printf("-----------------\n");
820 printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
821 fs->rx_port, m, m->pkt_len, m->nb_segs);
822 /* dump rx parsed packet info */
823 rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
824 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
825 "l4_proto=%d l4_len=%d flags=%s\n",
826 info.l2_len, rte_be_to_cpu_16(info.ethertype),
827 info.l3_len, info.l4_proto, info.l4_len, buf);
828 if (rx_ol_flags & PKT_RX_LRO)
829 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
830 if (info.is_tunnel == 1)
831 printf("rx: outer_l2_len=%d outer_ethertype=%x "
832 "outer_l3_len=%d\n", info.outer_l2_len,
833 rte_be_to_cpu_16(info.outer_ethertype),
835 /* dump tx packet info */
836 if ((testpmd_ol_flags & (TESTPMD_TX_OFFLOAD_IP_CKSUM |
837 TESTPMD_TX_OFFLOAD_UDP_CKSUM |
838 TESTPMD_TX_OFFLOAD_TCP_CKSUM |
839 TESTPMD_TX_OFFLOAD_SCTP_CKSUM)) ||
841 printf("tx: m->l2_len=%d m->l3_len=%d "
843 m->l2_len, m->l3_len, m->l4_len);
844 if (info.is_tunnel == 1) {
845 if ((testpmd_ol_flags &
846 TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) ||
847 (tx_ol_flags & PKT_TX_OUTER_IPV6))
848 printf("tx: m->outer_l2_len=%d "
849 "m->outer_l3_len=%d\n",
852 if (info.tunnel_tso_segsz != 0 &&
853 (m->ol_flags & PKT_TX_TCP_SEG))
854 printf("tx: m->tso_segsz=%d\n",
856 } else if (info.tso_segsz != 0 &&
857 (m->ol_flags & PKT_TX_TCP_SEG))
858 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
859 rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
860 printf("tx: flags=%s", buf);
865 if (unlikely(gro_enable)) {
866 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
867 nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
868 &(gro_ports[fs->rx_port].param));
870 gro_ctx = current_fwd_lcore()->gro_ctx;
871 nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
873 if (++fs->gro_times >= gro_flush_cycles) {
874 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
875 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
876 gro_pkts_num = MAX_PKT_BURST - nb_rx;
878 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
887 if (gso_ports[fs->tx_port].enable == 0)
888 tx_pkts_burst = pkts_burst;
890 gso_ctx = &(current_fwd_lcore()->gso_ctx);
891 gso_ctx->gso_size = gso_max_segment_size;
892 for (i = 0; i < nb_rx; i++) {
893 ret = rte_gso_segment(pkts_burst[i], gso_ctx,
894 &gso_segments[nb_segments],
895 GSO_MAX_PKT_BURST - nb_segments);
899 RTE_LOG(DEBUG, USER1,
900 "Unable to segment packet");
901 rte_pktmbuf_free(pkts_burst[i]);
905 tx_pkts_burst = gso_segments;
909 nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
910 tx_pkts_burst, nb_rx);
911 if (nb_prep != nb_rx)
912 printf("Preparing packet burst to transmit failed: %s\n",
913 rte_strerror(rte_errno));
915 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
921 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
923 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
924 rte_delay_us(burst_tx_delay_time);
925 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
926 &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
929 fs->tx_packets += nb_tx;
930 fs->rx_bad_ip_csum += rx_bad_ip_csum;
931 fs->rx_bad_l4_csum += rx_bad_l4_csum;
933 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
934 fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
936 if (unlikely(nb_tx < nb_rx)) {
937 fs->fwd_dropped += (nb_rx - nb_tx);
939 rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
940 } while (++nb_tx < nb_rx);
943 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
944 end_tsc = rte_rdtsc();
945 core_cycles = (end_tsc - start_tsc);
946 fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
950 struct fwd_engine csum_fwd_engine = {
951 .fwd_mode_name = "csum",
952 .port_fwd_begin = NULL,
953 .port_fwd_end = NULL,
954 .packet_fwd = pkt_burst_checksum_forward,