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_memory.h>
60 #include <rte_mempool.h>
62 #include <rte_memcpy.h>
63 #include <rte_interrupts.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
71 #include <rte_prefetch.h>
72 #include <rte_string_fns.h>
75 #define IP_DEFTTL 64 /* from RFC 1340. */
76 #define IP_VERSION 0x40
77 #define IP_HDRLEN 0x05 /* default IP header length == five 32-bits words. */
78 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
80 #define GRE_KEY_PRESENT 0x2000
82 #define GRE_SUPPORTED_FIELDS GRE_KEY_PRESENT
84 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
85 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
86 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
91 /* structure that caches offload info for the current packet */
92 struct testpmd_offload_info {
99 uint16_t outer_ethertype;
100 uint16_t outer_l2_len;
101 uint16_t outer_l3_len;
102 uint8_t outer_l4_proto;
104 uint16_t tunnel_tso_segsz;
107 /* simplified GRE header */
108 struct simple_gre_hdr {
111 } __attribute__((__packed__));
114 get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags)
116 if (ethertype == _htons(ETHER_TYPE_IPv4))
117 return rte_ipv4_phdr_cksum(l3_hdr, ol_flags);
118 else /* assume ethertype == ETHER_TYPE_IPv6 */
119 return rte_ipv6_phdr_cksum(l3_hdr, ol_flags);
123 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
125 if (ethertype == _htons(ETHER_TYPE_IPv4))
126 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
127 else /* assume ethertype == ETHER_TYPE_IPv6 */
128 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
131 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
133 parse_ipv4(struct ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
135 struct tcp_hdr *tcp_hdr;
137 info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
138 info->l4_proto = ipv4_hdr->next_proto_id;
140 /* only fill l4_len for TCP, it's useful for TSO */
141 if (info->l4_proto == IPPROTO_TCP) {
142 tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + info->l3_len);
143 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
148 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
150 parse_ipv6(struct ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
152 struct tcp_hdr *tcp_hdr;
154 info->l3_len = sizeof(struct ipv6_hdr);
155 info->l4_proto = ipv6_hdr->proto;
157 /* only fill l4_len for TCP, it's useful for TSO */
158 if (info->l4_proto == IPPROTO_TCP) {
159 tcp_hdr = (struct tcp_hdr *)((char *)ipv6_hdr + info->l3_len);
160 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
166 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
167 * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
168 * header. The l4_len argument is only set in case of TCP (useful for TSO).
171 parse_ethernet(struct ether_hdr *eth_hdr, struct testpmd_offload_info *info)
173 struct ipv4_hdr *ipv4_hdr;
174 struct ipv6_hdr *ipv6_hdr;
176 info->l2_len = sizeof(struct ether_hdr);
177 info->ethertype = eth_hdr->ether_type;
179 if (info->ethertype == _htons(ETHER_TYPE_VLAN)) {
180 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
182 info->l2_len += sizeof(struct vlan_hdr);
183 info->ethertype = vlan_hdr->eth_proto;
186 switch (info->ethertype) {
187 case _htons(ETHER_TYPE_IPv4):
188 ipv4_hdr = (struct ipv4_hdr *) ((char *)eth_hdr + info->l2_len);
189 parse_ipv4(ipv4_hdr, info);
191 case _htons(ETHER_TYPE_IPv6):
192 ipv6_hdr = (struct ipv6_hdr *) ((char *)eth_hdr + info->l2_len);
193 parse_ipv6(ipv6_hdr, info);
203 /* Parse a vxlan header */
205 parse_vxlan(struct udp_hdr *udp_hdr,
206 struct testpmd_offload_info *info,
209 struct ether_hdr *eth_hdr;
211 /* check udp destination port, 4789 is the default vxlan port
212 * (rfc7348) or that the rx offload flag is set (i40e only
214 if (udp_hdr->dst_port != _htons(4789) &&
215 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
219 info->outer_ethertype = info->ethertype;
220 info->outer_l2_len = info->l2_len;
221 info->outer_l3_len = info->l3_len;
222 info->outer_l4_proto = info->l4_proto;
224 eth_hdr = (struct ether_hdr *)((char *)udp_hdr +
225 sizeof(struct udp_hdr) +
226 sizeof(struct vxlan_hdr));
228 parse_ethernet(eth_hdr, info);
229 info->l2_len += ETHER_VXLAN_HLEN; /* add udp + vxlan */
232 /* Parse a gre header */
234 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
236 struct ether_hdr *eth_hdr;
237 struct ipv4_hdr *ipv4_hdr;
238 struct ipv6_hdr *ipv6_hdr;
241 /* check which fields are supported */
242 if ((gre_hdr->flags & _htons(~GRE_SUPPORTED_FIELDS)) != 0)
245 gre_len += sizeof(struct simple_gre_hdr);
247 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
248 gre_len += GRE_KEY_LEN;
250 if (gre_hdr->proto == _htons(ETHER_TYPE_IPv4)) {
252 info->outer_ethertype = info->ethertype;
253 info->outer_l2_len = info->l2_len;
254 info->outer_l3_len = info->l3_len;
255 info->outer_l4_proto = info->l4_proto;
257 ipv4_hdr = (struct ipv4_hdr *)((char *)gre_hdr + gre_len);
259 parse_ipv4(ipv4_hdr, info);
260 info->ethertype = _htons(ETHER_TYPE_IPv4);
263 } else if (gre_hdr->proto == _htons(ETHER_TYPE_IPv6)) {
265 info->outer_ethertype = info->ethertype;
266 info->outer_l2_len = info->l2_len;
267 info->outer_l3_len = info->l3_len;
268 info->outer_l4_proto = info->l4_proto;
270 ipv6_hdr = (struct ipv6_hdr *)((char *)gre_hdr + gre_len);
272 info->ethertype = _htons(ETHER_TYPE_IPv6);
273 parse_ipv6(ipv6_hdr, info);
276 } else if (gre_hdr->proto == _htons(ETHER_TYPE_TEB)) {
278 info->outer_ethertype = info->ethertype;
279 info->outer_l2_len = info->l2_len;
280 info->outer_l3_len = info->l3_len;
281 info->outer_l4_proto = info->l4_proto;
283 eth_hdr = (struct ether_hdr *)((char *)gre_hdr + gre_len);
285 parse_ethernet(eth_hdr, info);
289 info->l2_len += gre_len;
293 /* Parse an encapsulated ip or ipv6 header */
295 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
297 struct ipv4_hdr *ipv4_hdr = encap_ip;
298 struct ipv6_hdr *ipv6_hdr = encap_ip;
301 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
303 if (ip_version != 4 && ip_version != 6)
307 info->outer_ethertype = info->ethertype;
308 info->outer_l2_len = info->l2_len;
309 info->outer_l3_len = info->l3_len;
311 if (ip_version == 4) {
312 parse_ipv4(ipv4_hdr, info);
313 info->ethertype = _htons(ETHER_TYPE_IPv4);
315 parse_ipv6(ipv6_hdr, info);
316 info->ethertype = _htons(ETHER_TYPE_IPv6);
321 /* modify the IPv4 or IPv4 source address of a packet */
323 change_ip_addresses(void *l3_hdr, uint16_t ethertype)
325 struct ipv4_hdr *ipv4_hdr = l3_hdr;
326 struct ipv6_hdr *ipv6_hdr = l3_hdr;
328 if (ethertype == _htons(ETHER_TYPE_IPv4)) {
330 rte_cpu_to_be_32(rte_be_to_cpu_32(ipv4_hdr->src_addr) + 1);
331 } else if (ethertype == _htons(ETHER_TYPE_IPv6)) {
332 ipv6_hdr->src_addr[15] = ipv6_hdr->src_addr[15] + 1;
336 /* if possible, calculate the checksum of a packet in hw or sw,
337 * depending on the testpmd command line configuration */
339 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
340 uint16_t testpmd_ol_flags)
342 struct ipv4_hdr *ipv4_hdr = l3_hdr;
343 struct udp_hdr *udp_hdr;
344 struct tcp_hdr *tcp_hdr;
345 struct sctp_hdr *sctp_hdr;
346 uint64_t ol_flags = 0;
348 if (info->ethertype == _htons(ETHER_TYPE_IPv4)) {
350 ipv4_hdr->hdr_checksum = 0;
352 ol_flags |= PKT_TX_IPV4;
353 if (info->l4_proto == IPPROTO_TCP &&
354 ((info->is_tunnel && info->tunnel_tso_segsz != 0) ||
355 (!info->is_tunnel && info->tso_segsz != 0))) {
356 ol_flags |= PKT_TX_IP_CKSUM;
358 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_IP_CKSUM)
359 ol_flags |= PKT_TX_IP_CKSUM;
361 ipv4_hdr->hdr_checksum =
362 rte_ipv4_cksum(ipv4_hdr);
364 } else if (info->ethertype == _htons(ETHER_TYPE_IPv6))
365 ol_flags |= PKT_TX_IPV6;
367 return 0; /* packet type not supported, nothing to do */
369 if (info->l4_proto == IPPROTO_UDP) {
370 udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info->l3_len);
371 /* do not recalculate udp cksum if it was 0 */
372 if (udp_hdr->dgram_cksum != 0) {
373 udp_hdr->dgram_cksum = 0;
374 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_UDP_CKSUM) {
375 ol_flags |= PKT_TX_UDP_CKSUM;
376 udp_hdr->dgram_cksum = get_psd_sum(l3_hdr,
377 info->ethertype, ol_flags);
379 udp_hdr->dgram_cksum =
380 get_udptcp_checksum(l3_hdr, udp_hdr,
384 } else if (info->l4_proto == IPPROTO_TCP) {
385 tcp_hdr = (struct tcp_hdr *)((char *)l3_hdr + info->l3_len);
387 if ((info->is_tunnel && info->tunnel_tso_segsz != 0) ||
388 (!info->is_tunnel && info->tso_segsz != 0)) {
389 ol_flags |= PKT_TX_TCP_SEG;
390 tcp_hdr->cksum = get_psd_sum(l3_hdr, info->ethertype,
392 } else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_TCP_CKSUM) {
393 ol_flags |= PKT_TX_TCP_CKSUM;
394 tcp_hdr->cksum = get_psd_sum(l3_hdr, info->ethertype,
398 get_udptcp_checksum(l3_hdr, tcp_hdr,
401 } else if (info->l4_proto == IPPROTO_SCTP) {
402 sctp_hdr = (struct sctp_hdr *)((char *)l3_hdr + info->l3_len);
404 /* sctp payload must be a multiple of 4 to be
406 if ((testpmd_ol_flags & TESTPMD_TX_OFFLOAD_SCTP_CKSUM) &&
407 ((ipv4_hdr->total_length & 0x3) == 0)) {
408 ol_flags |= PKT_TX_SCTP_CKSUM;
410 /* XXX implement CRC32c, example available in
418 /* Calculate the checksum of outer header */
420 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
421 uint16_t testpmd_ol_flags, int tso_enabled)
423 struct ipv4_hdr *ipv4_hdr = outer_l3_hdr;
424 struct ipv6_hdr *ipv6_hdr = outer_l3_hdr;
425 struct udp_hdr *udp_hdr;
426 uint64_t ol_flags = 0;
428 if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4)) {
429 ipv4_hdr->hdr_checksum = 0;
430 ol_flags |= PKT_TX_OUTER_IPV4;
432 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
433 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
435 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
436 } else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
437 ol_flags |= PKT_TX_OUTER_IPV6;
439 if (info->outer_l4_proto != IPPROTO_UDP)
442 udp_hdr = (struct udp_hdr *)((char *)outer_l3_hdr + info->outer_l3_len);
444 /* outer UDP checksum is done in software as we have no hardware
445 * supporting it today, and no API for it. In the other side, for
446 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
449 * If a packet will be TSOed into small packets by NIC, we cannot
450 * set/calculate a non-zero checksum, because it will be a wrong
451 * value after the packet be split into several small packets.
454 udp_hdr->dgram_cksum = 0;
456 /* do not recalculate udp cksum if it was 0 */
457 if (udp_hdr->dgram_cksum != 0) {
458 udp_hdr->dgram_cksum = 0;
459 if (info->outer_ethertype == _htons(ETHER_TYPE_IPv4))
460 udp_hdr->dgram_cksum =
461 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
463 udp_hdr->dgram_cksum =
464 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
472 * Performs actual copying.
473 * Returns number of segments in the destination mbuf on success,
474 * or negative error code on failure.
477 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
478 uint16_t seglen[], uint8_t nb_seg)
480 uint32_t dlen, slen, tlen;
482 const struct rte_mbuf *m;
495 while (ms != NULL && i != nb_seg) {
498 slen = rte_pktmbuf_data_len(ms);
499 src = rte_pktmbuf_mtod(ms, const uint8_t *);
503 dlen = RTE_MIN(seglen[i], slen);
504 md[i]->data_len = dlen;
505 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
506 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
509 len = RTE_MIN(slen, dlen);
510 memcpy(dst, src, len);
525 else if (tlen != m->pkt_len)
528 md[0]->nb_segs = nb_seg;
529 md[0]->pkt_len = tlen;
530 md[0]->vlan_tci = m->vlan_tci;
531 md[0]->vlan_tci_outer = m->vlan_tci_outer;
532 md[0]->ol_flags = m->ol_flags;
533 md[0]->tx_offload = m->tx_offload;
539 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
540 * Copy packet contents and offload information into then new segmented mbuf.
542 static struct rte_mbuf *
543 pkt_copy_split(const struct rte_mbuf *pkt)
546 uint32_t i, len, nb_seg;
547 struct rte_mempool *mp;
548 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
549 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
551 mp = current_fwd_lcore()->mbp;
553 if (tx_pkt_split == TX_PKT_SPLIT_RND)
554 nb_seg = random() % tx_pkt_nb_segs + 1;
556 nb_seg = tx_pkt_nb_segs;
558 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
560 /* calculate number of segments to use and their length. */
562 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
567 n = pkt->pkt_len - len;
569 /* update size of the last segment to fit rest of the packet */
577 p = rte_pktmbuf_alloc(mp);
580 "failed to allocate %u-th of %u mbuf "
581 "from mempool: %s\n",
582 nb_seg - i, nb_seg, mp->name);
587 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
588 RTE_LOG(ERR, USER1, "mempool %s, %u-th segment: "
589 "expected seglen: %u, "
590 "actual mbuf tailroom: %u\n",
591 mp->name, i, seglen[i],
592 rte_pktmbuf_tailroom(md[i]));
597 /* all mbufs successfully allocated, do copy */
599 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
602 "mbuf_copy_split for %p(len=%u, nb_seg=%hhu) "
603 "into %u segments failed with error code: %d\n",
604 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
606 /* figure out how many mbufs to free. */
610 /* free unused mbufs */
611 for (; i != nb_seg; i++) {
612 rte_pktmbuf_free_seg(md[i]);
620 * Receive a burst of packets, and for each packet:
621 * - parse packet, and try to recognize a supported packet type (1)
622 * - if it's not a supported packet type, don't touch the packet, else:
623 * - modify the IPs in inner headers and in outer headers if any
624 * - reprocess the checksum of all supported layers. This is done in SW
625 * or HW, depending on testpmd command line configuration
626 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
627 * segmentation offload (this implies HW TCP checksum)
628 * Then transmit packets on the output port.
630 * (1) Supported packets are:
631 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
632 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
634 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
635 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
636 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
638 * The testpmd command line for this forward engine sets the flags
639 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
640 * wether a checksum must be calculated in software or in hardware. The
641 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
642 * OUTER_IP is only useful for tunnel packets.
645 pkt_burst_checksum_forward(struct fwd_stream *fs)
647 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
648 struct rte_port *txp;
649 struct rte_mbuf *m, *p;
650 struct ether_hdr *eth_hdr;
651 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
656 uint16_t testpmd_ol_flags;
658 uint32_t rx_bad_ip_csum;
659 uint32_t rx_bad_l4_csum;
660 struct testpmd_offload_info info;
662 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
665 uint64_t core_cycles;
668 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
669 start_tsc = rte_rdtsc();
672 /* receive a burst of packet */
673 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
675 if (unlikely(nb_rx == 0))
678 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
679 fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
681 fs->rx_packets += nb_rx;
685 txp = &ports[fs->tx_port];
686 testpmd_ol_flags = txp->tx_ol_flags;
687 memset(&info, 0, sizeof(info));
688 info.tso_segsz = txp->tso_segsz;
689 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
691 for (i = 0; i < nb_rx; i++) {
692 if (likely(i < nb_rx - 1))
693 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
700 /* Update the L3/L4 checksum error packet statistics */
701 rx_bad_ip_csum += ((m->ol_flags & PKT_RX_IP_CKSUM_BAD) != 0);
702 rx_bad_l4_csum += ((m->ol_flags & PKT_RX_L4_CKSUM_BAD) != 0);
704 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
705 * and inner headers */
707 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
708 ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
710 ether_addr_copy(&ports[fs->tx_port].eth_addr,
712 parse_ethernet(eth_hdr, &info);
713 l3_hdr = (char *)eth_hdr + info.l2_len;
715 /* check if it's a supported tunnel */
716 if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_PARSE_TUNNEL) {
717 if (info.l4_proto == IPPROTO_UDP) {
718 struct udp_hdr *udp_hdr;
720 udp_hdr = (struct udp_hdr *)((char *)l3_hdr +
722 parse_vxlan(udp_hdr, &info, m->packet_type);
724 ol_flags |= PKT_TX_TUNNEL_VXLAN;
725 } else if (info.l4_proto == IPPROTO_GRE) {
726 struct simple_gre_hdr *gre_hdr;
728 gre_hdr = (struct simple_gre_hdr *)
729 ((char *)l3_hdr + info.l3_len);
730 parse_gre(gre_hdr, &info);
732 ol_flags |= PKT_TX_TUNNEL_GRE;
733 } else if (info.l4_proto == IPPROTO_IPIP) {
736 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
737 parse_encap_ip(encap_ip_hdr, &info);
739 ol_flags |= PKT_TX_TUNNEL_IPIP;
743 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
744 if (info.is_tunnel) {
745 outer_l3_hdr = l3_hdr;
746 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
749 /* step 2: change all source IPs (v4 or v6) so we need
750 * to recompute the chksums even if they were correct */
752 change_ip_addresses(l3_hdr, info.ethertype);
753 if (info.is_tunnel == 1)
754 change_ip_addresses(outer_l3_hdr, info.outer_ethertype);
756 /* step 3: depending on user command line configuration,
757 * recompute checksum either in software or flag the
758 * mbuf to offload the calculation to the NIC. If TSO
759 * is configured, prepare the mbuf for TCP segmentation. */
761 /* process checksums of inner headers first */
762 ol_flags |= process_inner_cksums(l3_hdr, &info, testpmd_ol_flags);
764 /* Then process outer headers if any. Note that the software
765 * checksum will be wrong if one of the inner checksums is
766 * processed in hardware. */
767 if (info.is_tunnel == 1) {
768 ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
770 !!(ol_flags & PKT_TX_TCP_SEG));
773 /* step 4: fill the mbuf meta data (flags and header lengths) */
775 if (info.is_tunnel == 1) {
776 if (info.tunnel_tso_segsz ||
777 testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) {
778 m->outer_l2_len = info.outer_l2_len;
779 m->outer_l3_len = info.outer_l3_len;
780 m->l2_len = info.l2_len;
781 m->l3_len = info.l3_len;
782 m->l4_len = info.l4_len;
783 m->tso_segsz = info.tunnel_tso_segsz;
786 /* if there is a outer UDP cksum
787 processed in sw and the inner in hw,
788 the outer checksum will be wrong as
789 the payload will be modified by the
791 m->l2_len = info.outer_l2_len +
792 info.outer_l3_len + info.l2_len;
793 m->l3_len = info.l3_len;
794 m->l4_len = info.l4_len;
797 /* this is only useful if an offload flag is
798 * set, but it does not hurt to fill it in any
800 m->l2_len = info.l2_len;
801 m->l3_len = info.l3_len;
802 m->l4_len = info.l4_len;
803 m->tso_segsz = info.tso_segsz;
805 m->ol_flags = ol_flags;
807 /* Do split & copy for the packet. */
808 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
809 p = pkt_copy_split(m);
817 /* if verbose mode is enabled, dump debug info */
818 if (verbose_level > 0) {
823 { PKT_TX_IP_CKSUM, PKT_TX_IP_CKSUM },
824 { PKT_TX_UDP_CKSUM, PKT_TX_L4_MASK },
825 { PKT_TX_TCP_CKSUM, PKT_TX_L4_MASK },
826 { PKT_TX_SCTP_CKSUM, PKT_TX_L4_MASK },
827 { PKT_TX_IPV4, PKT_TX_IPV4 },
828 { PKT_TX_IPV6, PKT_TX_IPV6 },
829 { PKT_TX_OUTER_IP_CKSUM, PKT_TX_OUTER_IP_CKSUM },
830 { PKT_TX_OUTER_IPV4, PKT_TX_OUTER_IPV4 },
831 { PKT_TX_OUTER_IPV6, PKT_TX_OUTER_IPV6 },
832 { PKT_TX_TCP_SEG, PKT_TX_TCP_SEG },
833 { PKT_TX_TUNNEL_VXLAN, PKT_TX_TUNNEL_MASK },
834 { PKT_TX_TUNNEL_GRE, PKT_TX_TUNNEL_MASK },
835 { PKT_TX_TUNNEL_IPIP, PKT_TX_TUNNEL_MASK },
836 { PKT_TX_TUNNEL_GENEVE, PKT_TX_TUNNEL_MASK },
841 printf("-----------------\n");
842 printf("mbuf=%p, pkt_len=%u, nb_segs=%hhu:\n",
843 m, m->pkt_len, m->nb_segs);
844 /* dump rx parsed packet info */
845 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
846 "l4_proto=%d l4_len=%d\n",
847 info.l2_len, rte_be_to_cpu_16(info.ethertype),
848 info.l3_len, info.l4_proto, info.l4_len);
849 if (info.is_tunnel == 1)
850 printf("rx: outer_l2_len=%d outer_ethertype=%x "
851 "outer_l3_len=%d\n", info.outer_l2_len,
852 rte_be_to_cpu_16(info.outer_ethertype),
854 /* dump tx packet info */
855 if ((testpmd_ol_flags & (TESTPMD_TX_OFFLOAD_IP_CKSUM |
856 TESTPMD_TX_OFFLOAD_UDP_CKSUM |
857 TESTPMD_TX_OFFLOAD_TCP_CKSUM |
858 TESTPMD_TX_OFFLOAD_SCTP_CKSUM)) ||
860 printf("tx: m->l2_len=%d m->l3_len=%d "
862 m->l2_len, m->l3_len, m->l4_len);
863 if (info.is_tunnel == 1) {
864 if (testpmd_ol_flags &
865 TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
866 printf("tx: m->outer_l2_len=%d "
867 "m->outer_l3_len=%d\n",
870 if (info.tunnel_tso_segsz != 0)
871 printf("tx: m->tso_segsz=%d\n",
873 } else if (info.tso_segsz != 0)
874 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
875 printf("tx: flags=");
876 for (j = 0; j < sizeof(tx_flags)/sizeof(*tx_flags); j++) {
877 name = rte_get_tx_ol_flag_name(tx_flags[j].flag);
878 if ((m->ol_flags & tx_flags[j].mask) ==
885 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_rx);
889 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
891 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
892 rte_delay_us(burst_tx_delay_time);
893 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
894 &pkts_burst[nb_tx], nb_rx - nb_tx);
897 fs->tx_packets += nb_tx;
898 fs->rx_bad_ip_csum += rx_bad_ip_csum;
899 fs->rx_bad_l4_csum += rx_bad_l4_csum;
901 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
902 fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
904 if (unlikely(nb_tx < nb_rx)) {
905 fs->fwd_dropped += (nb_rx - nb_tx);
907 rte_pktmbuf_free(pkts_burst[nb_tx]);
908 } while (++nb_tx < nb_rx);
910 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
911 end_tsc = rte_rdtsc();
912 core_cycles = (end_tsc - start_tsc);
913 fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
917 struct fwd_engine csum_fwd_engine = {
918 .fwd_mode_name = "csum",
919 .port_fwd_begin = NULL,
920 .port_fwd_end = NULL,
921 .packet_fwd = pkt_burst_checksum_forward,