1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2014 6WIND S.A.
13 #include <sys/queue.h>
16 #include <rte_common.h>
17 #include <rte_byteorder.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>
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>
31 #include <rte_interrupts.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
38 #include <rte_vxlan.h>
41 #include <rte_prefetch.h>
42 #include <rte_string_fns.h>
49 #define IP_DEFTTL 64 /* from RFC 1340. */
50 #define IP_VERSION 0x40
51 #define IP_HDRLEN 0x05 /* default IP header length == five 32-bits words. */
52 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
54 #define GRE_CHECKSUM_PRESENT 0x8000
55 #define GRE_KEY_PRESENT 0x2000
56 #define GRE_SEQUENCE_PRESENT 0x1000
58 #define GRE_SUPPORTED_FIELDS (GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
61 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
62 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
63 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
68 uint16_t vxlan_gpe_udp_port = 4790;
70 /* structure that caches offload info for the current packet */
71 struct testpmd_offload_info {
79 uint16_t outer_ethertype;
80 uint16_t outer_l2_len;
81 uint16_t outer_l3_len;
82 uint8_t outer_l4_proto;
84 uint16_t tunnel_tso_segsz;
88 /* simplified GRE header */
89 struct simple_gre_hdr {
92 } __attribute__((__packed__));
95 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
97 if (ethertype == _htons(RTE_ETHER_TYPE_IPV4))
98 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
99 else /* assume ethertype == RTE_ETHER_TYPE_IPV6 */
100 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
103 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
105 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
107 struct rte_tcp_hdr *tcp_hdr;
109 info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
110 info->l4_proto = ipv4_hdr->next_proto_id;
112 /* only fill l4_len for TCP, it's useful for TSO */
113 if (info->l4_proto == IPPROTO_TCP) {
114 tcp_hdr = (struct rte_tcp_hdr *)
115 ((char *)ipv4_hdr + info->l3_len);
116 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
117 } else if (info->l4_proto == IPPROTO_UDP)
118 info->l4_len = sizeof(struct rte_udp_hdr);
123 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
125 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
127 struct rte_tcp_hdr *tcp_hdr;
129 info->l3_len = sizeof(struct rte_ipv6_hdr);
130 info->l4_proto = ipv6_hdr->proto;
132 /* only fill l4_len for TCP, it's useful for TSO */
133 if (info->l4_proto == IPPROTO_TCP) {
134 tcp_hdr = (struct rte_tcp_hdr *)
135 ((char *)ipv6_hdr + info->l3_len);
136 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
137 } else if (info->l4_proto == IPPROTO_UDP)
138 info->l4_len = sizeof(struct rte_udp_hdr);
144 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
145 * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
146 * header. The l4_len argument is only set in case of TCP (useful for TSO).
149 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
151 struct rte_ipv4_hdr *ipv4_hdr;
152 struct rte_ipv6_hdr *ipv6_hdr;
154 info->l2_len = sizeof(struct rte_ether_hdr);
155 info->ethertype = eth_hdr->ether_type;
157 if (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN)) {
158 struct rte_vlan_hdr *vlan_hdr = (
159 struct rte_vlan_hdr *)(eth_hdr + 1);
161 info->l2_len += sizeof(struct rte_vlan_hdr);
162 info->ethertype = vlan_hdr->eth_proto;
165 switch (info->ethertype) {
166 case _htons(RTE_ETHER_TYPE_IPV4):
167 ipv4_hdr = (struct rte_ipv4_hdr *)
168 ((char *)eth_hdr + info->l2_len);
169 parse_ipv4(ipv4_hdr, info);
171 case _htons(RTE_ETHER_TYPE_IPV6):
172 ipv6_hdr = (struct rte_ipv6_hdr *)
173 ((char *)eth_hdr + info->l2_len);
174 parse_ipv6(ipv6_hdr, info);
185 * Parse a GTP protocol header.
186 * No optional fields and next extension header type.
189 parse_gtp(struct rte_udp_hdr *udp_hdr,
190 struct testpmd_offload_info *info)
192 struct rte_ipv4_hdr *ipv4_hdr;
193 struct rte_ipv6_hdr *ipv6_hdr;
194 struct rte_gtp_hdr *gtp_hdr;
195 uint8_t gtp_len = sizeof(*gtp_hdr);
198 /* Check udp destination port. */
199 if (udp_hdr->dst_port != _htons(RTE_GTPC_UDP_PORT) &&
200 udp_hdr->src_port != _htons(RTE_GTPC_UDP_PORT) &&
201 udp_hdr->dst_port != _htons(RTE_GTPU_UDP_PORT))
205 info->outer_ethertype = info->ethertype;
206 info->outer_l2_len = info->l2_len;
207 info->outer_l3_len = info->l3_len;
208 info->outer_l4_proto = info->l4_proto;
211 gtp_hdr = (struct rte_gtp_hdr *)((char *)udp_hdr +
212 sizeof(struct rte_udp_hdr));
215 * Check message type. If message type is 0xff, it is
216 * a GTP data packet. If not, it is a GTP control packet
218 if (gtp_hdr->msg_type == 0xff) {
219 ip_ver = *(uint8_t *)((char *)udp_hdr +
220 sizeof(struct rte_udp_hdr) +
221 sizeof(struct rte_gtp_hdr));
222 ip_ver = (ip_ver) & 0xf0;
224 if (ip_ver == RTE_GTP_TYPE_IPV4) {
225 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gtp_hdr +
227 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
228 parse_ipv4(ipv4_hdr, info);
229 } else if (ip_ver == RTE_GTP_TYPE_IPV6) {
230 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gtp_hdr +
232 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
233 parse_ipv6(ipv6_hdr, info);
242 info->l2_len += RTE_ETHER_GTP_HLEN;
245 /* Parse a vxlan header */
247 parse_vxlan(struct rte_udp_hdr *udp_hdr,
248 struct testpmd_offload_info *info,
251 struct rte_ether_hdr *eth_hdr;
253 /* check udp destination port, 4789 is the default vxlan port
254 * (rfc7348) or that the rx offload flag is set (i40e only
256 if (udp_hdr->dst_port != _htons(4789) &&
257 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
261 info->outer_ethertype = info->ethertype;
262 info->outer_l2_len = info->l2_len;
263 info->outer_l3_len = info->l3_len;
264 info->outer_l4_proto = info->l4_proto;
266 eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
267 sizeof(struct rte_udp_hdr) +
268 sizeof(struct rte_vxlan_hdr));
270 parse_ethernet(eth_hdr, info);
271 info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
274 /* Parse a vxlan-gpe header */
276 parse_vxlan_gpe(struct rte_udp_hdr *udp_hdr,
277 struct testpmd_offload_info *info)
279 struct rte_ether_hdr *eth_hdr;
280 struct rte_ipv4_hdr *ipv4_hdr;
281 struct rte_ipv6_hdr *ipv6_hdr;
282 struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
283 uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
285 /* Check udp destination port. */
286 if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
289 vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
290 sizeof(struct rte_udp_hdr));
292 if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
293 RTE_VXLAN_GPE_TYPE_IPV4) {
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;
300 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)vxlan_gpe_hdr +
303 parse_ipv4(ipv4_hdr, info);
304 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
307 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_IPV6) {
309 info->outer_ethertype = info->ethertype;
310 info->outer_l2_len = info->l2_len;
311 info->outer_l3_len = info->l3_len;
312 info->outer_l4_proto = info->l4_proto;
314 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
317 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
318 parse_ipv6(ipv6_hdr, info);
321 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
323 info->outer_ethertype = info->ethertype;
324 info->outer_l2_len = info->l2_len;
325 info->outer_l3_len = info->l3_len;
326 info->outer_l4_proto = info->l4_proto;
328 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
331 parse_ethernet(eth_hdr, info);
335 info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
338 /* Parse a gre header */
340 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
342 struct rte_ether_hdr *eth_hdr;
343 struct rte_ipv4_hdr *ipv4_hdr;
344 struct rte_ipv6_hdr *ipv6_hdr;
347 gre_len += sizeof(struct simple_gre_hdr);
349 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
350 gre_len += GRE_EXT_LEN;
351 if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
352 gre_len += GRE_EXT_LEN;
353 if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
354 gre_len += GRE_EXT_LEN;
356 if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV4)) {
358 info->outer_ethertype = info->ethertype;
359 info->outer_l2_len = info->l2_len;
360 info->outer_l3_len = info->l3_len;
361 info->outer_l4_proto = info->l4_proto;
363 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
365 parse_ipv4(ipv4_hdr, info);
366 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
369 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
371 info->outer_ethertype = info->ethertype;
372 info->outer_l2_len = info->l2_len;
373 info->outer_l3_len = info->l3_len;
374 info->outer_l4_proto = info->l4_proto;
376 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
378 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
379 parse_ipv6(ipv6_hdr, info);
382 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
384 info->outer_ethertype = info->ethertype;
385 info->outer_l2_len = info->l2_len;
386 info->outer_l3_len = info->l3_len;
387 info->outer_l4_proto = info->l4_proto;
389 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
391 parse_ethernet(eth_hdr, info);
395 info->l2_len += gre_len;
399 /* Parse an encapsulated ip or ipv6 header */
401 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
403 struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
404 struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
407 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
409 if (ip_version != 4 && ip_version != 6)
413 info->outer_ethertype = info->ethertype;
414 info->outer_l2_len = info->l2_len;
415 info->outer_l3_len = info->l3_len;
417 if (ip_version == 4) {
418 parse_ipv4(ipv4_hdr, info);
419 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
421 parse_ipv6(ipv6_hdr, info);
422 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
427 /* if possible, calculate the checksum of a packet in hw or sw,
428 * depending on the testpmd command line configuration */
430 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
431 uint64_t tx_offloads)
433 struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
434 struct rte_udp_hdr *udp_hdr;
435 struct rte_tcp_hdr *tcp_hdr;
436 struct rte_sctp_hdr *sctp_hdr;
437 uint64_t ol_flags = 0;
438 uint32_t max_pkt_len, tso_segsz = 0;
440 /* ensure packet is large enough to require tso */
441 if (!info->is_tunnel) {
442 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
444 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
445 tso_segsz = info->tso_segsz;
447 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
448 info->l2_len + info->l3_len + info->l4_len +
449 info->tunnel_tso_segsz;
450 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
451 tso_segsz = info->tunnel_tso_segsz;
454 if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
456 ipv4_hdr->hdr_checksum = 0;
458 ol_flags |= PKT_TX_IPV4;
459 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
460 ol_flags |= PKT_TX_IP_CKSUM;
462 if (tx_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
463 ol_flags |= PKT_TX_IP_CKSUM;
465 ipv4_hdr->hdr_checksum =
466 rte_ipv4_cksum(ipv4_hdr);
468 } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
469 ol_flags |= PKT_TX_IPV6;
471 return 0; /* packet type not supported, nothing to do */
473 if (info->l4_proto == IPPROTO_UDP) {
474 udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
475 /* do not recalculate udp cksum if it was 0 */
476 if (udp_hdr->dgram_cksum != 0) {
477 udp_hdr->dgram_cksum = 0;
478 if (tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM)
479 ol_flags |= PKT_TX_UDP_CKSUM;
481 udp_hdr->dgram_cksum =
482 get_udptcp_checksum(l3_hdr, udp_hdr,
486 if (info->gso_enable)
487 ol_flags |= PKT_TX_UDP_SEG;
488 } else if (info->l4_proto == IPPROTO_TCP) {
489 tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
492 ol_flags |= PKT_TX_TCP_SEG;
493 else if (tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM)
494 ol_flags |= PKT_TX_TCP_CKSUM;
497 get_udptcp_checksum(l3_hdr, tcp_hdr,
500 if (info->gso_enable)
501 ol_flags |= PKT_TX_TCP_SEG;
502 } else if (info->l4_proto == IPPROTO_SCTP) {
503 sctp_hdr = (struct rte_sctp_hdr *)
504 ((char *)l3_hdr + info->l3_len);
506 /* sctp payload must be a multiple of 4 to be
508 if ((tx_offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
509 ((ipv4_hdr->total_length & 0x3) == 0)) {
510 ol_flags |= PKT_TX_SCTP_CKSUM;
512 /* XXX implement CRC32c, example available in
520 /* Calculate the checksum of outer header */
522 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
523 uint64_t tx_offloads, int tso_enabled)
525 struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
526 struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
527 struct rte_udp_hdr *udp_hdr;
528 uint64_t ol_flags = 0;
530 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
531 ipv4_hdr->hdr_checksum = 0;
532 ol_flags |= PKT_TX_OUTER_IPV4;
534 if (tx_offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)
535 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
537 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
539 ol_flags |= PKT_TX_OUTER_IPV6;
541 if (info->outer_l4_proto != IPPROTO_UDP)
544 udp_hdr = (struct rte_udp_hdr *)
545 ((char *)outer_l3_hdr + info->outer_l3_len);
548 ol_flags |= PKT_TX_TCP_SEG;
550 /* Skip SW outer UDP checksum generation if HW supports it */
551 if (tx_offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) {
552 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
554 = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
557 = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
559 ol_flags |= PKT_TX_OUTER_UDP_CKSUM;
563 /* outer UDP checksum is done in software. In the other side, for
564 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
567 * If a packet will be TSOed into small packets by NIC, we cannot
568 * set/calculate a non-zero checksum, because it will be a wrong
569 * value after the packet be split into several small packets.
572 udp_hdr->dgram_cksum = 0;
574 /* do not recalculate udp cksum if it was 0 */
575 if (udp_hdr->dgram_cksum != 0) {
576 udp_hdr->dgram_cksum = 0;
577 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
578 udp_hdr->dgram_cksum =
579 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
581 udp_hdr->dgram_cksum =
582 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
590 * Performs actual copying.
591 * Returns number of segments in the destination mbuf on success,
592 * or negative error code on failure.
595 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
596 uint16_t seglen[], uint8_t nb_seg)
598 uint32_t dlen, slen, tlen;
600 const struct rte_mbuf *m;
613 while (ms != NULL && i != nb_seg) {
616 slen = rte_pktmbuf_data_len(ms);
617 src = rte_pktmbuf_mtod(ms, const uint8_t *);
621 dlen = RTE_MIN(seglen[i], slen);
622 md[i]->data_len = dlen;
623 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
624 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
627 len = RTE_MIN(slen, dlen);
628 memcpy(dst, src, len);
643 else if (tlen != m->pkt_len)
646 md[0]->nb_segs = nb_seg;
647 md[0]->pkt_len = tlen;
648 md[0]->vlan_tci = m->vlan_tci;
649 md[0]->vlan_tci_outer = m->vlan_tci_outer;
650 md[0]->ol_flags = m->ol_flags;
651 md[0]->tx_offload = m->tx_offload;
657 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
658 * Copy packet contents and offload information into the new segmented mbuf.
660 static struct rte_mbuf *
661 pkt_copy_split(const struct rte_mbuf *pkt)
664 uint32_t i, len, nb_seg;
665 struct rte_mempool *mp;
666 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
667 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
669 mp = current_fwd_lcore()->mbp;
671 if (tx_pkt_split == TX_PKT_SPLIT_RND)
672 nb_seg = random() % tx_pkt_nb_segs + 1;
674 nb_seg = tx_pkt_nb_segs;
676 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
678 /* calculate number of segments to use and their length. */
680 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
685 n = pkt->pkt_len - len;
687 /* update size of the last segment to fit rest of the packet */
695 p = rte_pktmbuf_alloc(mp);
698 "failed to allocate %u-th of %u mbuf "
699 "from mempool: %s\n",
700 nb_seg - i, nb_seg, mp->name);
705 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
706 TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
707 "expected seglen: %u, "
708 "actual mbuf tailroom: %u\n",
709 mp->name, i, seglen[i],
710 rte_pktmbuf_tailroom(md[i]));
715 /* all mbufs successfully allocated, do copy */
717 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
720 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
721 "into %u segments failed with error code: %d\n",
722 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
724 /* figure out how many mbufs to free. */
728 /* free unused mbufs */
729 for (; i != nb_seg; i++) {
730 rte_pktmbuf_free_seg(md[i]);
738 * Receive a burst of packets, and for each packet:
739 * - parse packet, and try to recognize a supported packet type (1)
740 * - if it's not a supported packet type, don't touch the packet, else:
741 * - reprocess the checksum of all supported layers. This is done in SW
742 * or HW, depending on testpmd command line configuration
743 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
744 * segmentation offload (this implies HW TCP checksum)
745 * Then transmit packets on the output port.
747 * (1) Supported packets are:
748 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
749 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
751 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
753 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
755 * Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
756 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
757 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
758 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
760 * The testpmd command line for this forward engine sets the flags
761 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
762 * wether a checksum must be calculated in software or in hardware. The
763 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
764 * OUTER_IP is only useful for tunnel packets.
767 pkt_burst_checksum_forward(struct fwd_stream *fs)
769 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
770 struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
771 struct rte_gso_ctx *gso_ctx;
772 struct rte_mbuf **tx_pkts_burst;
773 struct rte_port *txp;
774 struct rte_mbuf *m, *p;
775 struct rte_ether_hdr *eth_hdr;
776 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
778 uint16_t gro_pkts_num;
784 uint64_t rx_ol_flags, tx_ol_flags;
785 uint64_t tx_offloads;
787 uint32_t rx_bad_ip_csum;
788 uint32_t rx_bad_l4_csum;
789 uint32_t rx_bad_outer_l4_csum;
790 struct testpmd_offload_info info;
791 uint16_t nb_segments = 0;
794 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
797 uint64_t core_cycles;
800 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
801 start_tsc = rte_rdtsc();
804 /* receive a burst of packet */
805 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
807 if (unlikely(nb_rx == 0))
809 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
810 fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
812 fs->rx_packets += nb_rx;
815 rx_bad_outer_l4_csum = 0;
816 gro_enable = gro_ports[fs->rx_port].enable;
818 txp = &ports[fs->tx_port];
819 tx_offloads = txp->dev_conf.txmode.offloads;
820 memset(&info, 0, sizeof(info));
821 info.tso_segsz = txp->tso_segsz;
822 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
823 if (gso_ports[fs->tx_port].enable)
826 for (i = 0; i < nb_rx; i++) {
827 if (likely(i < nb_rx - 1))
828 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
833 info.pkt_len = rte_pktmbuf_pkt_len(m);
834 tx_ol_flags = m->ol_flags &
835 (IND_ATTACHED_MBUF | EXT_ATTACHED_MBUF);
836 rx_ol_flags = m->ol_flags;
838 /* Update the L3/L4 checksum error packet statistics */
839 if ((rx_ol_flags & PKT_RX_IP_CKSUM_MASK) == PKT_RX_IP_CKSUM_BAD)
841 if ((rx_ol_flags & PKT_RX_L4_CKSUM_MASK) == PKT_RX_L4_CKSUM_BAD)
843 if (rx_ol_flags & PKT_RX_OUTER_L4_CKSUM_BAD)
844 rx_bad_outer_l4_csum += 1;
846 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
847 * and inner headers */
849 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
850 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
852 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
854 parse_ethernet(eth_hdr, &info);
855 l3_hdr = (char *)eth_hdr + info.l2_len;
857 /* check if it's a supported tunnel */
858 if (txp->parse_tunnel) {
859 if (info.l4_proto == IPPROTO_UDP) {
860 struct rte_udp_hdr *udp_hdr;
862 udp_hdr = (struct rte_udp_hdr *)
863 ((char *)l3_hdr + info.l3_len);
864 parse_gtp(udp_hdr, &info);
865 if (info.is_tunnel) {
866 tx_ol_flags |= PKT_TX_TUNNEL_GTP;
869 parse_vxlan_gpe(udp_hdr, &info);
870 if (info.is_tunnel) {
872 PKT_TX_TUNNEL_VXLAN_GPE;
875 parse_vxlan(udp_hdr, &info,
880 } else if (info.l4_proto == IPPROTO_GRE) {
881 struct simple_gre_hdr *gre_hdr;
883 gre_hdr = (struct simple_gre_hdr *)
884 ((char *)l3_hdr + info.l3_len);
885 parse_gre(gre_hdr, &info);
887 tx_ol_flags |= PKT_TX_TUNNEL_GRE;
888 } else if (info.l4_proto == IPPROTO_IPIP) {
891 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
892 parse_encap_ip(encap_ip_hdr, &info);
894 tx_ol_flags |= PKT_TX_TUNNEL_IPIP;
899 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
900 if (info.is_tunnel) {
901 outer_l3_hdr = l3_hdr;
902 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
905 /* step 2: depending on user command line configuration,
906 * recompute checksum either in software or flag the
907 * mbuf to offload the calculation to the NIC. If TSO
908 * is configured, prepare the mbuf for TCP segmentation. */
910 /* process checksums of inner headers first */
911 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
914 /* Then process outer headers if any. Note that the software
915 * checksum will be wrong if one of the inner checksums is
916 * processed in hardware. */
917 if (info.is_tunnel == 1) {
918 tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
920 !!(tx_ol_flags & PKT_TX_TCP_SEG));
923 /* step 3: fill the mbuf meta data (flags and header lengths) */
926 if (info.is_tunnel == 1) {
927 if (info.tunnel_tso_segsz ||
929 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
931 DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
932 (tx_ol_flags & PKT_TX_OUTER_IPV6)) {
933 m->outer_l2_len = info.outer_l2_len;
934 m->outer_l3_len = info.outer_l3_len;
935 m->l2_len = info.l2_len;
936 m->l3_len = info.l3_len;
937 m->l4_len = info.l4_len;
938 m->tso_segsz = info.tunnel_tso_segsz;
941 /* if there is a outer UDP cksum
942 processed in sw and the inner in hw,
943 the outer checksum will be wrong as
944 the payload will be modified by the
946 m->l2_len = info.outer_l2_len +
947 info.outer_l3_len + info.l2_len;
948 m->l3_len = info.l3_len;
949 m->l4_len = info.l4_len;
952 /* this is only useful if an offload flag is
953 * set, but it does not hurt to fill it in any
955 m->l2_len = info.l2_len;
956 m->l3_len = info.l3_len;
957 m->l4_len = info.l4_len;
958 m->tso_segsz = info.tso_segsz;
960 m->ol_flags = tx_ol_flags;
962 /* Do split & copy for the packet. */
963 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
964 p = pkt_copy_split(m);
972 /* if verbose mode is enabled, dump debug info */
973 if (verbose_level > 0) {
976 printf("-----------------\n");
977 printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
978 fs->rx_port, m, m->pkt_len, m->nb_segs);
979 /* dump rx parsed packet info */
980 rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
981 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
982 "l4_proto=%d l4_len=%d flags=%s\n",
983 info.l2_len, rte_be_to_cpu_16(info.ethertype),
984 info.l3_len, info.l4_proto, info.l4_len, buf);
985 if (rx_ol_flags & PKT_RX_LRO)
986 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
987 if (info.is_tunnel == 1)
988 printf("rx: outer_l2_len=%d outer_ethertype=%x "
989 "outer_l3_len=%d\n", info.outer_l2_len,
990 rte_be_to_cpu_16(info.outer_ethertype),
992 /* dump tx packet info */
993 if ((tx_offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
994 DEV_TX_OFFLOAD_UDP_CKSUM |
995 DEV_TX_OFFLOAD_TCP_CKSUM |
996 DEV_TX_OFFLOAD_SCTP_CKSUM)) ||
998 printf("tx: m->l2_len=%d m->l3_len=%d "
1000 m->l2_len, m->l3_len, m->l4_len);
1001 if (info.is_tunnel == 1) {
1003 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1005 DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
1006 (tx_ol_flags & PKT_TX_OUTER_IPV6))
1007 printf("tx: m->outer_l2_len=%d "
1008 "m->outer_l3_len=%d\n",
1011 if (info.tunnel_tso_segsz != 0 &&
1012 (m->ol_flags & PKT_TX_TCP_SEG))
1013 printf("tx: m->tso_segsz=%d\n",
1015 } else if (info.tso_segsz != 0 &&
1016 (m->ol_flags & PKT_TX_TCP_SEG))
1017 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
1018 rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
1019 printf("tx: flags=%s", buf);
1024 if (unlikely(gro_enable)) {
1025 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
1026 nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
1027 &(gro_ports[fs->rx_port].param));
1029 gro_ctx = current_fwd_lcore()->gro_ctx;
1030 nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
1032 if (++fs->gro_times >= gro_flush_cycles) {
1033 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
1034 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
1035 gro_pkts_num = MAX_PKT_BURST - nb_rx;
1037 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
1046 if (gso_ports[fs->tx_port].enable == 0)
1047 tx_pkts_burst = pkts_burst;
1049 gso_ctx = &(current_fwd_lcore()->gso_ctx);
1050 gso_ctx->gso_size = gso_max_segment_size;
1051 for (i = 0; i < nb_rx; i++) {
1052 ret = rte_gso_segment(pkts_burst[i], gso_ctx,
1053 &gso_segments[nb_segments],
1054 GSO_MAX_PKT_BURST - nb_segments);
1058 TESTPMD_LOG(DEBUG, "Unable to segment packet");
1059 rte_pktmbuf_free(pkts_burst[i]);
1063 tx_pkts_burst = gso_segments;
1064 nb_rx = nb_segments;
1067 nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
1068 tx_pkts_burst, nb_rx);
1069 if (nb_prep != nb_rx)
1070 printf("Preparing packet burst to transmit failed: %s\n",
1071 rte_strerror(rte_errno));
1073 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
1077 * Retry if necessary
1079 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
1081 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
1082 rte_delay_us(burst_tx_delay_time);
1083 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1084 &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1087 fs->tx_packets += nb_tx;
1088 fs->rx_bad_ip_csum += rx_bad_ip_csum;
1089 fs->rx_bad_l4_csum += rx_bad_l4_csum;
1090 fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1092 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1093 fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
1095 if (unlikely(nb_tx < nb_rx)) {
1096 fs->fwd_dropped += (nb_rx - nb_tx);
1098 rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1099 } while (++nb_tx < nb_rx);
1102 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1103 end_tsc = rte_rdtsc();
1104 core_cycles = (end_tsc - start_tsc);
1105 fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
1109 struct fwd_engine csum_fwd_engine = {
1110 .fwd_mode_name = "csum",
1111 .port_fwd_begin = NULL,
1112 .port_fwd_end = NULL,
1113 .packet_fwd = pkt_burst_checksum_forward,