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>
40 #include <rte_prefetch.h>
41 #include <rte_string_fns.h>
48 #define IP_DEFTTL 64 /* from RFC 1340. */
49 #define IP_VERSION 0x40
50 #define IP_HDRLEN 0x05 /* default IP header length == five 32-bits words. */
51 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
53 #define GRE_CHECKSUM_PRESENT 0x8000
54 #define GRE_KEY_PRESENT 0x2000
55 #define GRE_SEQUENCE_PRESENT 0x1000
57 #define GRE_SUPPORTED_FIELDS (GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
60 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
61 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
62 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
67 uint16_t vxlan_gpe_udp_port = 4790;
69 /* structure that caches offload info for the current packet */
70 struct testpmd_offload_info {
78 uint16_t outer_ethertype;
79 uint16_t outer_l2_len;
80 uint16_t outer_l3_len;
81 uint8_t outer_l4_proto;
83 uint16_t tunnel_tso_segsz;
87 /* simplified GRE header */
88 struct simple_gre_hdr {
91 } __attribute__((__packed__));
94 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
96 if (ethertype == _htons(RTE_ETHER_TYPE_IPV4))
97 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
98 else /* assume ethertype == RTE_ETHER_TYPE_IPV6 */
99 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
102 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
104 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
106 struct rte_tcp_hdr *tcp_hdr;
108 info->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
109 info->l4_proto = ipv4_hdr->next_proto_id;
111 /* only fill l4_len for TCP, it's useful for TSO */
112 if (info->l4_proto == IPPROTO_TCP) {
113 tcp_hdr = (struct rte_tcp_hdr *)
114 ((char *)ipv4_hdr + info->l3_len);
115 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
116 } else if (info->l4_proto == IPPROTO_UDP)
117 info->l4_len = sizeof(struct rte_udp_hdr);
122 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
124 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
126 struct rte_tcp_hdr *tcp_hdr;
128 info->l3_len = sizeof(struct rte_ipv6_hdr);
129 info->l4_proto = ipv6_hdr->proto;
131 /* only fill l4_len for TCP, it's useful for TSO */
132 if (info->l4_proto == IPPROTO_TCP) {
133 tcp_hdr = (struct rte_tcp_hdr *)
134 ((char *)ipv6_hdr + info->l3_len);
135 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
136 } else if (info->l4_proto == IPPROTO_UDP)
137 info->l4_len = sizeof(struct rte_udp_hdr);
143 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
144 * ipproto. This function is able to recognize IPv4/IPv6 with one optional vlan
145 * header. The l4_len argument is only set in case of TCP (useful for TSO).
148 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
150 struct rte_ipv4_hdr *ipv4_hdr;
151 struct rte_ipv6_hdr *ipv6_hdr;
153 info->l2_len = sizeof(struct rte_ether_hdr);
154 info->ethertype = eth_hdr->ether_type;
156 if (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN)) {
157 struct rte_vlan_hdr *vlan_hdr = (
158 struct rte_vlan_hdr *)(eth_hdr + 1);
160 info->l2_len += sizeof(struct rte_vlan_hdr);
161 info->ethertype = vlan_hdr->eth_proto;
164 switch (info->ethertype) {
165 case _htons(RTE_ETHER_TYPE_IPV4):
166 ipv4_hdr = (struct rte_ipv4_hdr *)
167 ((char *)eth_hdr + info->l2_len);
168 parse_ipv4(ipv4_hdr, info);
170 case _htons(RTE_ETHER_TYPE_IPV6):
171 ipv6_hdr = (struct rte_ipv6_hdr *)
172 ((char *)eth_hdr + info->l2_len);
173 parse_ipv6(ipv6_hdr, info);
184 * Parse a GTP protocol header.
185 * No optional fields and next extension header type.
188 parse_gtp(struct rte_udp_hdr *udp_hdr,
189 struct testpmd_offload_info *info)
191 struct rte_ipv4_hdr *ipv4_hdr;
192 struct rte_ipv6_hdr *ipv6_hdr;
193 struct rte_gtp_hdr *gtp_hdr;
194 uint8_t gtp_len = sizeof(*gtp_hdr);
197 /* Check udp destination port. */
198 if (udp_hdr->dst_port != _htons(RTE_GTPC_UDP_PORT) &&
199 udp_hdr->src_port != _htons(RTE_GTPC_UDP_PORT) &&
200 udp_hdr->dst_port != _htons(RTE_GTPU_UDP_PORT))
204 info->outer_ethertype = info->ethertype;
205 info->outer_l2_len = info->l2_len;
206 info->outer_l3_len = info->l3_len;
207 info->outer_l4_proto = info->l4_proto;
210 gtp_hdr = (struct rte_gtp_hdr *)((char *)udp_hdr +
211 sizeof(struct rte_udp_hdr));
214 * Check message type. If message type is 0xff, it is
215 * a GTP data packet. If not, it is a GTP control packet
217 if (gtp_hdr->msg_type == 0xff) {
218 ip_ver = *(uint8_t *)((char *)udp_hdr +
219 sizeof(struct rte_udp_hdr) +
220 sizeof(struct rte_gtp_hdr));
221 ip_ver = (ip_ver) & 0xf0;
223 if (ip_ver == RTE_GTP_TYPE_IPV4) {
224 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gtp_hdr +
226 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
227 parse_ipv4(ipv4_hdr, info);
228 } else if (ip_ver == RTE_GTP_TYPE_IPV6) {
229 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gtp_hdr +
231 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
232 parse_ipv6(ipv6_hdr, info);
241 info->l2_len += RTE_ETHER_GTP_HLEN;
244 /* Parse a vxlan header */
246 parse_vxlan(struct rte_udp_hdr *udp_hdr,
247 struct testpmd_offload_info *info,
250 struct rte_ether_hdr *eth_hdr;
252 /* check udp destination port, 4789 is the default vxlan port
253 * (rfc7348) or that the rx offload flag is set (i40e only
255 if (udp_hdr->dst_port != _htons(4789) &&
256 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
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 eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
266 sizeof(struct rte_udp_hdr) +
267 sizeof(struct rte_vxlan_hdr));
269 parse_ethernet(eth_hdr, info);
270 info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
273 /* Parse a vxlan-gpe header */
275 parse_vxlan_gpe(struct rte_udp_hdr *udp_hdr,
276 struct testpmd_offload_info *info)
278 struct rte_ether_hdr *eth_hdr;
279 struct rte_ipv4_hdr *ipv4_hdr;
280 struct rte_ipv6_hdr *ipv6_hdr;
281 struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
282 uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
284 /* Check udp destination port. */
285 if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
288 vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
289 sizeof(struct rte_udp_hdr));
291 if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
292 RTE_VXLAN_GPE_TYPE_IPV4) {
294 info->outer_ethertype = info->ethertype;
295 info->outer_l2_len = info->l2_len;
296 info->outer_l3_len = info->l3_len;
297 info->outer_l4_proto = info->l4_proto;
299 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)vxlan_gpe_hdr +
302 parse_ipv4(ipv4_hdr, info);
303 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
306 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_IPV6) {
308 info->outer_ethertype = info->ethertype;
309 info->outer_l2_len = info->l2_len;
310 info->outer_l3_len = info->l3_len;
311 info->outer_l4_proto = info->l4_proto;
313 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
316 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
317 parse_ipv6(ipv6_hdr, info);
320 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
322 info->outer_ethertype = info->ethertype;
323 info->outer_l2_len = info->l2_len;
324 info->outer_l3_len = info->l3_len;
325 info->outer_l4_proto = info->l4_proto;
327 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
330 parse_ethernet(eth_hdr, info);
334 info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
337 /* Parse a gre header */
339 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
341 struct rte_ether_hdr *eth_hdr;
342 struct rte_ipv4_hdr *ipv4_hdr;
343 struct rte_ipv6_hdr *ipv6_hdr;
346 gre_len += sizeof(struct simple_gre_hdr);
348 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
349 gre_len += GRE_EXT_LEN;
350 if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
351 gre_len += GRE_EXT_LEN;
352 if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
353 gre_len += GRE_EXT_LEN;
355 if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV4)) {
357 info->outer_ethertype = info->ethertype;
358 info->outer_l2_len = info->l2_len;
359 info->outer_l3_len = info->l3_len;
360 info->outer_l4_proto = info->l4_proto;
362 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
364 parse_ipv4(ipv4_hdr, info);
365 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
368 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
370 info->outer_ethertype = info->ethertype;
371 info->outer_l2_len = info->l2_len;
372 info->outer_l3_len = info->l3_len;
373 info->outer_l4_proto = info->l4_proto;
375 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
377 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
378 parse_ipv6(ipv6_hdr, info);
381 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
383 info->outer_ethertype = info->ethertype;
384 info->outer_l2_len = info->l2_len;
385 info->outer_l3_len = info->l3_len;
386 info->outer_l4_proto = info->l4_proto;
388 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
390 parse_ethernet(eth_hdr, info);
394 info->l2_len += gre_len;
398 /* Parse an encapsulated ip or ipv6 header */
400 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
402 struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
403 struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
406 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
408 if (ip_version != 4 && ip_version != 6)
412 info->outer_ethertype = info->ethertype;
413 info->outer_l2_len = info->l2_len;
414 info->outer_l3_len = info->l3_len;
416 if (ip_version == 4) {
417 parse_ipv4(ipv4_hdr, info);
418 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
420 parse_ipv6(ipv6_hdr, info);
421 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
426 /* if possible, calculate the checksum of a packet in hw or sw,
427 * depending on the testpmd command line configuration */
429 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
430 uint64_t tx_offloads)
432 struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
433 struct rte_udp_hdr *udp_hdr;
434 struct rte_tcp_hdr *tcp_hdr;
435 struct rte_sctp_hdr *sctp_hdr;
436 uint64_t ol_flags = 0;
437 uint32_t max_pkt_len, tso_segsz = 0;
439 /* ensure packet is large enough to require tso */
440 if (!info->is_tunnel) {
441 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
443 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
444 tso_segsz = info->tso_segsz;
446 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
447 info->l2_len + info->l3_len + info->l4_len +
448 info->tunnel_tso_segsz;
449 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
450 tso_segsz = info->tunnel_tso_segsz;
453 if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
455 ipv4_hdr->hdr_checksum = 0;
457 ol_flags |= PKT_TX_IPV4;
458 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
459 ol_flags |= PKT_TX_IP_CKSUM;
461 if (tx_offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
462 ol_flags |= PKT_TX_IP_CKSUM;
464 ipv4_hdr->hdr_checksum =
465 rte_ipv4_cksum(ipv4_hdr);
467 } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
468 ol_flags |= PKT_TX_IPV6;
470 return 0; /* packet type not supported, nothing to do */
472 if (info->l4_proto == IPPROTO_UDP) {
473 udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
474 /* do not recalculate udp cksum if it was 0 */
475 if (udp_hdr->dgram_cksum != 0) {
476 udp_hdr->dgram_cksum = 0;
477 if (tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM)
478 ol_flags |= PKT_TX_UDP_CKSUM;
480 udp_hdr->dgram_cksum =
481 get_udptcp_checksum(l3_hdr, udp_hdr,
485 if (info->gso_enable)
486 ol_flags |= PKT_TX_UDP_SEG;
487 } else if (info->l4_proto == IPPROTO_TCP) {
488 tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
491 ol_flags |= PKT_TX_TCP_SEG;
492 else if (tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM)
493 ol_flags |= PKT_TX_TCP_CKSUM;
496 get_udptcp_checksum(l3_hdr, tcp_hdr,
499 if (info->gso_enable)
500 ol_flags |= PKT_TX_TCP_SEG;
501 } else if (info->l4_proto == IPPROTO_SCTP) {
502 sctp_hdr = (struct rte_sctp_hdr *)
503 ((char *)l3_hdr + info->l3_len);
505 /* sctp payload must be a multiple of 4 to be
507 if ((tx_offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
508 ((ipv4_hdr->total_length & 0x3) == 0)) {
509 ol_flags |= PKT_TX_SCTP_CKSUM;
511 /* XXX implement CRC32c, example available in
519 /* Calculate the checksum of outer header */
521 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
522 uint64_t tx_offloads, int tso_enabled)
524 struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
525 struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
526 struct rte_udp_hdr *udp_hdr;
527 uint64_t ol_flags = 0;
529 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
530 ipv4_hdr->hdr_checksum = 0;
531 ol_flags |= PKT_TX_OUTER_IPV4;
533 if (tx_offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)
534 ol_flags |= PKT_TX_OUTER_IP_CKSUM;
536 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
538 ol_flags |= PKT_TX_OUTER_IPV6;
540 if (info->outer_l4_proto != IPPROTO_UDP)
543 udp_hdr = (struct rte_udp_hdr *)
544 ((char *)outer_l3_hdr + info->outer_l3_len);
546 /* Skip SW outer UDP checksum generation if HW supports it */
547 if (tx_offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) {
548 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
550 = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
553 = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
555 ol_flags |= PKT_TX_OUTER_UDP_CKSUM;
559 /* outer UDP checksum is done in software. In the other side, for
560 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
563 * If a packet will be TSOed into small packets by NIC, we cannot
564 * set/calculate a non-zero checksum, because it will be a wrong
565 * value after the packet be split into several small packets.
568 udp_hdr->dgram_cksum = 0;
570 /* do not recalculate udp cksum if it was 0 */
571 if (udp_hdr->dgram_cksum != 0) {
572 udp_hdr->dgram_cksum = 0;
573 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
574 udp_hdr->dgram_cksum =
575 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
577 udp_hdr->dgram_cksum =
578 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
586 * Performs actual copying.
587 * Returns number of segments in the destination mbuf on success,
588 * or negative error code on failure.
591 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
592 uint16_t seglen[], uint8_t nb_seg)
594 uint32_t dlen, slen, tlen;
596 const struct rte_mbuf *m;
609 while (ms != NULL && i != nb_seg) {
612 slen = rte_pktmbuf_data_len(ms);
613 src = rte_pktmbuf_mtod(ms, const uint8_t *);
617 dlen = RTE_MIN(seglen[i], slen);
618 md[i]->data_len = dlen;
619 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
620 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
623 len = RTE_MIN(slen, dlen);
624 memcpy(dst, src, len);
639 else if (tlen != m->pkt_len)
642 md[0]->nb_segs = nb_seg;
643 md[0]->pkt_len = tlen;
644 md[0]->vlan_tci = m->vlan_tci;
645 md[0]->vlan_tci_outer = m->vlan_tci_outer;
646 md[0]->ol_flags = m->ol_flags;
647 md[0]->tx_offload = m->tx_offload;
653 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
654 * Copy packet contents and offload information into the new segmented mbuf.
656 static struct rte_mbuf *
657 pkt_copy_split(const struct rte_mbuf *pkt)
660 uint32_t i, len, nb_seg;
661 struct rte_mempool *mp;
662 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
663 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
665 mp = current_fwd_lcore()->mbp;
667 if (tx_pkt_split == TX_PKT_SPLIT_RND)
668 nb_seg = random() % tx_pkt_nb_segs + 1;
670 nb_seg = tx_pkt_nb_segs;
672 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
674 /* calculate number of segments to use and their length. */
676 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
681 n = pkt->pkt_len - len;
683 /* update size of the last segment to fit rest of the packet */
691 p = rte_pktmbuf_alloc(mp);
694 "failed to allocate %u-th of %u mbuf "
695 "from mempool: %s\n",
696 nb_seg - i, nb_seg, mp->name);
701 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
702 TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
703 "expected seglen: %u, "
704 "actual mbuf tailroom: %u\n",
705 mp->name, i, seglen[i],
706 rte_pktmbuf_tailroom(md[i]));
711 /* all mbufs successfully allocated, do copy */
713 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
716 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
717 "into %u segments failed with error code: %d\n",
718 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
720 /* figure out how many mbufs to free. */
724 /* free unused mbufs */
725 for (; i != nb_seg; i++) {
726 rte_pktmbuf_free_seg(md[i]);
734 * Receive a burst of packets, and for each packet:
735 * - parse packet, and try to recognize a supported packet type (1)
736 * - if it's not a supported packet type, don't touch the packet, else:
737 * - reprocess the checksum of all supported layers. This is done in SW
738 * or HW, depending on testpmd command line configuration
739 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
740 * segmentation offload (this implies HW TCP checksum)
741 * Then transmit packets on the output port.
743 * (1) Supported packets are:
744 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
745 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
747 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
749 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
751 * Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
752 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
753 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
754 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
756 * The testpmd command line for this forward engine sets the flags
757 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
758 * wether a checksum must be calculated in software or in hardware. The
759 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
760 * OUTER_IP is only useful for tunnel packets.
763 pkt_burst_checksum_forward(struct fwd_stream *fs)
765 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
766 struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
767 struct rte_gso_ctx *gso_ctx;
768 struct rte_mbuf **tx_pkts_burst;
769 struct rte_port *txp;
770 struct rte_mbuf *m, *p;
771 struct rte_ether_hdr *eth_hdr;
772 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
774 uint16_t gro_pkts_num;
780 uint64_t rx_ol_flags, tx_ol_flags;
781 uint64_t tx_offloads;
783 uint32_t rx_bad_ip_csum;
784 uint32_t rx_bad_l4_csum;
785 uint32_t rx_bad_outer_l4_csum;
786 struct testpmd_offload_info info;
787 uint16_t nb_segments = 0;
790 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
793 uint64_t core_cycles;
796 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
797 start_tsc = rte_rdtsc();
800 /* receive a burst of packet */
801 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
803 if (unlikely(nb_rx == 0))
805 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
806 fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
808 fs->rx_packets += nb_rx;
811 rx_bad_outer_l4_csum = 0;
812 gro_enable = gro_ports[fs->rx_port].enable;
814 txp = &ports[fs->tx_port];
815 tx_offloads = txp->dev_conf.txmode.offloads;
816 memset(&info, 0, sizeof(info));
817 info.tso_segsz = txp->tso_segsz;
818 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
819 if (gso_ports[fs->tx_port].enable)
822 for (i = 0; i < nb_rx; i++) {
823 if (likely(i < nb_rx - 1))
824 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
829 info.pkt_len = rte_pktmbuf_pkt_len(m);
830 tx_ol_flags = m->ol_flags &
831 (IND_ATTACHED_MBUF | EXT_ATTACHED_MBUF);
832 rx_ol_flags = m->ol_flags;
834 /* Update the L3/L4 checksum error packet statistics */
835 if ((rx_ol_flags & PKT_RX_IP_CKSUM_MASK) == PKT_RX_IP_CKSUM_BAD)
837 if ((rx_ol_flags & PKT_RX_L4_CKSUM_MASK) == PKT_RX_L4_CKSUM_BAD)
839 if (rx_ol_flags & PKT_RX_OUTER_L4_CKSUM_BAD)
840 rx_bad_outer_l4_csum += 1;
842 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
843 * and inner headers */
845 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
846 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
848 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
850 parse_ethernet(eth_hdr, &info);
851 l3_hdr = (char *)eth_hdr + info.l2_len;
853 /* check if it's a supported tunnel */
854 if (txp->parse_tunnel) {
855 if (info.l4_proto == IPPROTO_UDP) {
856 struct rte_udp_hdr *udp_hdr;
858 udp_hdr = (struct rte_udp_hdr *)
859 ((char *)l3_hdr + info.l3_len);
860 parse_gtp(udp_hdr, &info);
861 if (info.is_tunnel) {
862 tx_ol_flags |= PKT_TX_TUNNEL_GTP;
865 parse_vxlan_gpe(udp_hdr, &info);
866 if (info.is_tunnel) {
868 PKT_TX_TUNNEL_VXLAN_GPE;
871 parse_vxlan(udp_hdr, &info,
876 } else if (info.l4_proto == IPPROTO_GRE) {
877 struct simple_gre_hdr *gre_hdr;
879 gre_hdr = (struct simple_gre_hdr *)
880 ((char *)l3_hdr + info.l3_len);
881 parse_gre(gre_hdr, &info);
883 tx_ol_flags |= PKT_TX_TUNNEL_GRE;
884 } else if (info.l4_proto == IPPROTO_IPIP) {
887 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
888 parse_encap_ip(encap_ip_hdr, &info);
890 tx_ol_flags |= PKT_TX_TUNNEL_IPIP;
895 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
896 if (info.is_tunnel) {
897 outer_l3_hdr = l3_hdr;
898 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
901 /* step 2: depending on user command line configuration,
902 * recompute checksum either in software or flag the
903 * mbuf to offload the calculation to the NIC. If TSO
904 * is configured, prepare the mbuf for TCP segmentation. */
906 /* process checksums of inner headers first */
907 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
910 /* Then process outer headers if any. Note that the software
911 * checksum will be wrong if one of the inner checksums is
912 * processed in hardware. */
913 if (info.is_tunnel == 1) {
914 tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
916 !!(tx_ol_flags & PKT_TX_TCP_SEG));
919 /* step 3: fill the mbuf meta data (flags and header lengths) */
922 if (info.is_tunnel == 1) {
923 if (info.tunnel_tso_segsz ||
925 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
927 DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
928 (tx_ol_flags & PKT_TX_OUTER_IPV6)) {
929 m->outer_l2_len = info.outer_l2_len;
930 m->outer_l3_len = info.outer_l3_len;
931 m->l2_len = info.l2_len;
932 m->l3_len = info.l3_len;
933 m->l4_len = info.l4_len;
934 m->tso_segsz = info.tunnel_tso_segsz;
937 /* if there is a outer UDP cksum
938 processed in sw and the inner in hw,
939 the outer checksum will be wrong as
940 the payload will be modified by the
942 m->l2_len = info.outer_l2_len +
943 info.outer_l3_len + info.l2_len;
944 m->l3_len = info.l3_len;
945 m->l4_len = info.l4_len;
948 /* this is only useful if an offload flag is
949 * set, but it does not hurt to fill it in any
951 m->l2_len = info.l2_len;
952 m->l3_len = info.l3_len;
953 m->l4_len = info.l4_len;
954 m->tso_segsz = info.tso_segsz;
956 m->ol_flags = tx_ol_flags;
958 /* Do split & copy for the packet. */
959 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
960 p = pkt_copy_split(m);
968 /* if verbose mode is enabled, dump debug info */
969 if (verbose_level > 0) {
972 printf("-----------------\n");
973 printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
974 fs->rx_port, m, m->pkt_len, m->nb_segs);
975 /* dump rx parsed packet info */
976 rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
977 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
978 "l4_proto=%d l4_len=%d flags=%s\n",
979 info.l2_len, rte_be_to_cpu_16(info.ethertype),
980 info.l3_len, info.l4_proto, info.l4_len, buf);
981 if (rx_ol_flags & PKT_RX_LRO)
982 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
983 if (info.is_tunnel == 1)
984 printf("rx: outer_l2_len=%d outer_ethertype=%x "
985 "outer_l3_len=%d\n", info.outer_l2_len,
986 rte_be_to_cpu_16(info.outer_ethertype),
988 /* dump tx packet info */
989 if ((tx_offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
990 DEV_TX_OFFLOAD_UDP_CKSUM |
991 DEV_TX_OFFLOAD_TCP_CKSUM |
992 DEV_TX_OFFLOAD_SCTP_CKSUM)) ||
994 printf("tx: m->l2_len=%d m->l3_len=%d "
996 m->l2_len, m->l3_len, m->l4_len);
997 if (info.is_tunnel == 1) {
999 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1001 DEV_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
1002 (tx_ol_flags & PKT_TX_OUTER_IPV6))
1003 printf("tx: m->outer_l2_len=%d "
1004 "m->outer_l3_len=%d\n",
1007 if (info.tunnel_tso_segsz != 0 &&
1008 (m->ol_flags & PKT_TX_TCP_SEG))
1009 printf("tx: m->tso_segsz=%d\n",
1011 } else if (info.tso_segsz != 0 &&
1012 (m->ol_flags & PKT_TX_TCP_SEG))
1013 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
1014 rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
1015 printf("tx: flags=%s", buf);
1020 if (unlikely(gro_enable)) {
1021 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
1022 nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
1023 &(gro_ports[fs->rx_port].param));
1025 gro_ctx = current_fwd_lcore()->gro_ctx;
1026 nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
1028 if (++fs->gro_times >= gro_flush_cycles) {
1029 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
1030 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
1031 gro_pkts_num = MAX_PKT_BURST - nb_rx;
1033 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
1042 if (gso_ports[fs->tx_port].enable == 0)
1043 tx_pkts_burst = pkts_burst;
1045 gso_ctx = &(current_fwd_lcore()->gso_ctx);
1046 gso_ctx->gso_size = gso_max_segment_size;
1047 for (i = 0; i < nb_rx; i++) {
1048 ret = rte_gso_segment(pkts_burst[i], gso_ctx,
1049 &gso_segments[nb_segments],
1050 GSO_MAX_PKT_BURST - nb_segments);
1054 TESTPMD_LOG(DEBUG, "Unable to segment packet");
1055 rte_pktmbuf_free(pkts_burst[i]);
1059 tx_pkts_burst = gso_segments;
1060 nb_rx = nb_segments;
1063 nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
1064 tx_pkts_burst, nb_rx);
1065 if (nb_prep != nb_rx)
1066 printf("Preparing packet burst to transmit failed: %s\n",
1067 rte_strerror(rte_errno));
1069 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
1073 * Retry if necessary
1075 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
1077 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
1078 rte_delay_us(burst_tx_delay_time);
1079 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1080 &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1083 fs->tx_packets += nb_tx;
1084 fs->rx_bad_ip_csum += rx_bad_ip_csum;
1085 fs->rx_bad_l4_csum += rx_bad_l4_csum;
1086 fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1088 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1089 fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
1091 if (unlikely(nb_tx < nb_rx)) {
1092 fs->fwd_dropped += (nb_rx - nb_tx);
1094 rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1095 } while (++nb_tx < nb_rx);
1098 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1099 end_tsc = rte_rdtsc();
1100 core_cycles = (end_tsc - start_tsc);
1101 fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
1105 struct fwd_engine csum_fwd_engine = {
1106 .fwd_mode_name = "csum",
1107 .port_fwd_begin = NULL,
1108 .port_fwd_end = NULL,
1109 .packet_fwd = pkt_burst_checksum_forward,