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_branch_prediction.h>
28 #include <rte_mempool.h>
30 #include <rte_interrupts.h>
32 #include <rte_ether.h>
33 #include <rte_ethdev.h>
37 #include <rte_vxlan.h>
40 #include <rte_prefetch.h>
41 #include <rte_string_fns.h>
49 #include <rte_geneve.h>
53 #define IP_DEFTTL 64 /* from RFC 1340. */
55 #define GRE_CHECKSUM_PRESENT 0x8000
56 #define GRE_KEY_PRESENT 0x2000
57 #define GRE_SEQUENCE_PRESENT 0x1000
59 #define GRE_SUPPORTED_FIELDS (GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
62 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
63 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
64 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
69 uint16_t vxlan_gpe_udp_port = RTE_VXLAN_GPE_DEFAULT_PORT;
70 uint16_t geneve_udp_port = RTE_GENEVE_DEFAULT_PORT;
72 /* structure that caches offload info for the current packet */
73 struct testpmd_offload_info {
83 uint16_t outer_ethertype;
84 uint16_t outer_l2_len;
85 uint16_t outer_l3_len;
86 uint8_t outer_l4_proto;
88 uint16_t tunnel_tso_segsz;
92 /* simplified GRE header */
93 struct simple_gre_hdr {
99 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
101 if (ethertype == _htons(RTE_ETHER_TYPE_IPV4))
102 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
103 else /* assume ethertype == RTE_ETHER_TYPE_IPV6 */
104 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
107 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
109 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
111 struct rte_tcp_hdr *tcp_hdr;
113 info->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
114 info->l4_proto = ipv4_hdr->next_proto_id;
116 /* only fill l4_len for TCP, it's useful for TSO */
117 if (info->l4_proto == IPPROTO_TCP) {
118 tcp_hdr = (struct rte_tcp_hdr *)
119 ((char *)ipv4_hdr + info->l3_len);
120 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
121 } else if (info->l4_proto == IPPROTO_UDP)
122 info->l4_len = sizeof(struct rte_udp_hdr);
127 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
129 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
131 struct rte_tcp_hdr *tcp_hdr;
133 info->l3_len = sizeof(struct rte_ipv6_hdr);
134 info->l4_proto = ipv6_hdr->proto;
136 /* only fill l4_len for TCP, it's useful for TSO */
137 if (info->l4_proto == IPPROTO_TCP) {
138 tcp_hdr = (struct rte_tcp_hdr *)
139 ((char *)ipv6_hdr + info->l3_len);
140 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
141 } else if (info->l4_proto == IPPROTO_UDP)
142 info->l4_len = sizeof(struct rte_udp_hdr);
148 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
149 * ipproto. This function is able to recognize IPv4/IPv6 with optional VLAN
150 * headers. The l4_len argument is only set in case of TCP (useful for TSO).
153 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
155 struct rte_ipv4_hdr *ipv4_hdr;
156 struct rte_ipv6_hdr *ipv6_hdr;
157 struct rte_vlan_hdr *vlan_hdr;
159 info->l2_len = sizeof(struct rte_ether_hdr);
160 info->ethertype = eth_hdr->ether_type;
162 while (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
163 info->ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
164 vlan_hdr = (struct rte_vlan_hdr *)
165 ((char *)eth_hdr + info->l2_len);
166 info->l2_len += sizeof(struct rte_vlan_hdr);
167 info->ethertype = vlan_hdr->eth_proto;
170 switch (info->ethertype) {
171 case _htons(RTE_ETHER_TYPE_IPV4):
172 ipv4_hdr = (struct rte_ipv4_hdr *)
173 ((char *)eth_hdr + info->l2_len);
174 parse_ipv4(ipv4_hdr, info);
176 case _htons(RTE_ETHER_TYPE_IPV6):
177 ipv6_hdr = (struct rte_ipv6_hdr *)
178 ((char *)eth_hdr + info->l2_len);
179 parse_ipv6(ipv6_hdr, info);
189 /* Fill in outer layers length */
191 update_tunnel_outer(struct testpmd_offload_info *info)
194 info->outer_ethertype = info->ethertype;
195 info->outer_l2_len = info->l2_len;
196 info->outer_l3_len = info->l3_len;
197 info->outer_l4_proto = info->l4_proto;
201 * Parse a GTP protocol header.
202 * No optional fields and next extension header type.
205 parse_gtp(struct rte_udp_hdr *udp_hdr,
206 struct testpmd_offload_info *info)
208 struct rte_ipv4_hdr *ipv4_hdr;
209 struct rte_ipv6_hdr *ipv6_hdr;
210 struct rte_gtp_hdr *gtp_hdr;
211 uint8_t gtp_len = sizeof(*gtp_hdr);
214 /* Check udp destination port. */
215 if (udp_hdr->dst_port != _htons(RTE_GTPC_UDP_PORT) &&
216 udp_hdr->src_port != _htons(RTE_GTPC_UDP_PORT) &&
217 udp_hdr->dst_port != _htons(RTE_GTPU_UDP_PORT))
220 update_tunnel_outer(info);
223 gtp_hdr = (struct rte_gtp_hdr *)((char *)udp_hdr +
224 sizeof(struct rte_udp_hdr));
227 * Check message type. If message type is 0xff, it is
228 * a GTP data packet. If not, it is a GTP control packet
230 if (gtp_hdr->msg_type == 0xff) {
231 ip_ver = *(uint8_t *)((char *)udp_hdr +
232 sizeof(struct rte_udp_hdr) +
233 sizeof(struct rte_gtp_hdr));
234 ip_ver = (ip_ver) & 0xf0;
236 if (ip_ver == RTE_GTP_TYPE_IPV4) {
237 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gtp_hdr +
239 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
240 parse_ipv4(ipv4_hdr, info);
241 } else if (ip_ver == RTE_GTP_TYPE_IPV6) {
242 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gtp_hdr +
244 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
245 parse_ipv6(ipv6_hdr, info);
254 info->l2_len += RTE_ETHER_GTP_HLEN;
257 /* Parse a vxlan header */
259 parse_vxlan(struct rte_udp_hdr *udp_hdr,
260 struct testpmd_offload_info *info,
263 struct rte_ether_hdr *eth_hdr;
265 /* check udp destination port, RTE_VXLAN_DEFAULT_PORT (4789) is the
266 * default vxlan port (rfc7348) or that the rx offload flag is set
267 * (i40e only currently)
269 if (udp_hdr->dst_port != _htons(RTE_VXLAN_DEFAULT_PORT) &&
270 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
273 update_tunnel_outer(info);
275 eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
276 sizeof(struct rte_udp_hdr) +
277 sizeof(struct rte_vxlan_hdr));
279 parse_ethernet(eth_hdr, info);
280 info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
283 /* Parse a vxlan-gpe header */
285 parse_vxlan_gpe(struct rte_udp_hdr *udp_hdr,
286 struct testpmd_offload_info *info)
288 struct rte_ether_hdr *eth_hdr;
289 struct rte_ipv4_hdr *ipv4_hdr;
290 struct rte_ipv6_hdr *ipv6_hdr;
291 struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
292 uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
294 /* Check udp destination port. */
295 if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
298 vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
299 sizeof(struct rte_udp_hdr));
301 if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
302 RTE_VXLAN_GPE_TYPE_IPV4) {
303 update_tunnel_outer(info);
305 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)vxlan_gpe_hdr +
308 parse_ipv4(ipv4_hdr, info);
309 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
312 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_IPV6) {
313 update_tunnel_outer(info);
315 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
318 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
319 parse_ipv6(ipv6_hdr, info);
322 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
323 update_tunnel_outer(info);
325 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
328 parse_ethernet(eth_hdr, info);
332 info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
335 /* Parse a geneve header */
337 parse_geneve(struct rte_udp_hdr *udp_hdr,
338 struct testpmd_offload_info *info)
340 struct rte_ether_hdr *eth_hdr;
341 struct rte_ipv4_hdr *ipv4_hdr;
342 struct rte_ipv6_hdr *ipv6_hdr;
343 struct rte_geneve_hdr *geneve_hdr;
346 /* Check udp destination port. */
347 if (udp_hdr->dst_port != _htons(geneve_udp_port))
350 geneve_hdr = (struct rte_geneve_hdr *)((char *)udp_hdr +
351 sizeof(struct rte_udp_hdr));
352 geneve_len = sizeof(struct rte_geneve_hdr) + geneve_hdr->opt_len * 4;
353 if (!geneve_hdr->proto || geneve_hdr->proto ==
354 _htons(RTE_ETHER_TYPE_IPV4)) {
355 update_tunnel_outer(info);
356 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)geneve_hdr +
358 parse_ipv4(ipv4_hdr, info);
359 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
361 } else if (geneve_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
362 update_tunnel_outer(info);
363 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)geneve_hdr +
365 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
366 parse_ipv6(ipv6_hdr, info);
369 } else if (geneve_hdr->proto == _htons(RTE_GENEVE_TYPE_ETH)) {
370 update_tunnel_outer(info);
371 eth_hdr = (struct rte_ether_hdr *)((char *)geneve_hdr +
373 parse_ethernet(eth_hdr, info);
378 (sizeof(struct rte_udp_hdr) + sizeof(struct rte_geneve_hdr) +
379 ((struct rte_geneve_hdr *)geneve_hdr)->opt_len * 4);
382 /* Parse a gre header */
384 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
386 struct rte_ether_hdr *eth_hdr;
387 struct rte_ipv4_hdr *ipv4_hdr;
388 struct rte_ipv6_hdr *ipv6_hdr;
391 gre_len += sizeof(struct simple_gre_hdr);
393 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
394 gre_len += GRE_EXT_LEN;
395 if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
396 gre_len += GRE_EXT_LEN;
397 if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
398 gre_len += GRE_EXT_LEN;
400 if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV4)) {
401 update_tunnel_outer(info);
403 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
405 parse_ipv4(ipv4_hdr, info);
406 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
409 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
410 update_tunnel_outer(info);
412 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
414 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
415 parse_ipv6(ipv6_hdr, info);
418 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
419 update_tunnel_outer(info);
421 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
423 parse_ethernet(eth_hdr, info);
427 info->l2_len += gre_len;
431 /* Parse an encapsulated ip or ipv6 header */
433 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
435 struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
436 struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
439 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
441 if (ip_version != 4 && ip_version != 6)
445 info->outer_ethertype = info->ethertype;
446 info->outer_l2_len = info->l2_len;
447 info->outer_l3_len = info->l3_len;
449 if (ip_version == 4) {
450 parse_ipv4(ipv4_hdr, info);
451 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
453 parse_ipv6(ipv6_hdr, info);
454 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
459 /* if possible, calculate the checksum of a packet in hw or sw,
460 * depending on the testpmd command line configuration */
462 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
463 uint64_t tx_offloads)
465 struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
466 struct rte_udp_hdr *udp_hdr;
467 struct rte_tcp_hdr *tcp_hdr;
468 struct rte_sctp_hdr *sctp_hdr;
469 uint64_t ol_flags = 0;
470 uint32_t max_pkt_len, tso_segsz = 0;
472 /* ensure packet is large enough to require tso */
473 if (!info->is_tunnel) {
474 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
476 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
477 tso_segsz = info->tso_segsz;
479 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
480 info->l2_len + info->l3_len + info->l4_len +
481 info->tunnel_tso_segsz;
482 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
483 tso_segsz = info->tunnel_tso_segsz;
486 if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
489 ol_flags |= RTE_MBUF_F_TX_IPV4;
490 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
491 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
493 if (tx_offloads & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM) {
494 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
496 ipv4_hdr->hdr_checksum = 0;
497 ipv4_hdr->hdr_checksum =
498 rte_ipv4_cksum(ipv4_hdr);
501 } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
502 ol_flags |= RTE_MBUF_F_TX_IPV6;
504 return 0; /* packet type not supported, nothing to do */
506 if (info->l4_proto == IPPROTO_UDP) {
507 udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
508 /* do not recalculate udp cksum if it was 0 */
509 if (udp_hdr->dgram_cksum != 0) {
510 if (tx_offloads & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
511 ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
513 udp_hdr->dgram_cksum = 0;
514 udp_hdr->dgram_cksum =
515 get_udptcp_checksum(l3_hdr, udp_hdr,
520 if (info->gso_enable)
521 ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
523 } else if (info->l4_proto == IPPROTO_TCP) {
524 tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
526 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
527 else if (tx_offloads & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
528 ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
532 get_udptcp_checksum(l3_hdr, tcp_hdr,
536 if (info->gso_enable)
537 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
539 } else if (info->l4_proto == IPPROTO_SCTP) {
540 sctp_hdr = (struct rte_sctp_hdr *)
541 ((char *)l3_hdr + info->l3_len);
542 /* sctp payload must be a multiple of 4 to be
544 if ((tx_offloads & RTE_ETH_TX_OFFLOAD_SCTP_CKSUM) &&
545 ((ipv4_hdr->total_length & 0x3) == 0)) {
546 ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
549 /* XXX implement CRC32c, example available in
557 /* Calculate the checksum of outer header */
559 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
560 uint64_t tx_offloads, int tso_enabled)
562 struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
563 struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
564 struct rte_udp_hdr *udp_hdr;
565 uint64_t ol_flags = 0;
567 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
568 ipv4_hdr->hdr_checksum = 0;
569 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV4;
571 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM)
572 ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
574 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
576 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV6;
578 if (info->outer_l4_proto != IPPROTO_UDP)
581 udp_hdr = (struct rte_udp_hdr *)
582 ((char *)outer_l3_hdr + info->outer_l3_len);
585 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
587 /* Skip SW outer UDP checksum generation if HW supports it */
588 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) {
589 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
591 = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
594 = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
596 ol_flags |= RTE_MBUF_F_TX_OUTER_UDP_CKSUM;
600 /* outer UDP checksum is done in software. In the other side, for
601 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
604 * If a packet will be TSOed into small packets by NIC, we cannot
605 * set/calculate a non-zero checksum, because it will be a wrong
606 * value after the packet be split into several small packets.
609 udp_hdr->dgram_cksum = 0;
611 /* do not recalculate udp cksum if it was 0 */
612 if (udp_hdr->dgram_cksum != 0) {
613 udp_hdr->dgram_cksum = 0;
614 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
615 udp_hdr->dgram_cksum =
616 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
618 udp_hdr->dgram_cksum =
619 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
627 * Performs actual copying.
628 * Returns number of segments in the destination mbuf on success,
629 * or negative error code on failure.
632 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
633 uint16_t seglen[], uint8_t nb_seg)
635 uint32_t dlen, slen, tlen;
637 const struct rte_mbuf *m;
650 while (ms != NULL && i != nb_seg) {
653 slen = rte_pktmbuf_data_len(ms);
654 src = rte_pktmbuf_mtod(ms, const uint8_t *);
658 dlen = RTE_MIN(seglen[i], slen);
659 md[i]->data_len = dlen;
660 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
661 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
664 len = RTE_MIN(slen, dlen);
665 memcpy(dst, src, len);
680 else if (tlen != m->pkt_len)
683 md[0]->nb_segs = nb_seg;
684 md[0]->pkt_len = tlen;
685 md[0]->vlan_tci = m->vlan_tci;
686 md[0]->vlan_tci_outer = m->vlan_tci_outer;
687 md[0]->ol_flags = m->ol_flags;
688 md[0]->tx_offload = m->tx_offload;
694 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
695 * Copy packet contents and offload information into the new segmented mbuf.
697 static struct rte_mbuf *
698 pkt_copy_split(const struct rte_mbuf *pkt)
701 uint32_t i, len, nb_seg;
702 struct rte_mempool *mp;
703 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
704 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
706 mp = current_fwd_lcore()->mbp;
708 if (tx_pkt_split == TX_PKT_SPLIT_RND)
709 nb_seg = rte_rand() % tx_pkt_nb_segs + 1;
711 nb_seg = tx_pkt_nb_segs;
713 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
715 /* calculate number of segments to use and their length. */
717 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
722 n = pkt->pkt_len - len;
724 /* update size of the last segment to fit rest of the packet */
732 p = rte_pktmbuf_alloc(mp);
735 "failed to allocate %u-th of %u mbuf "
736 "from mempool: %s\n",
737 nb_seg - i, nb_seg, mp->name);
742 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
743 TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
744 "expected seglen: %u, "
745 "actual mbuf tailroom: %u\n",
746 mp->name, i, seglen[i],
747 rte_pktmbuf_tailroom(md[i]));
752 /* all mbufs successfully allocated, do copy */
754 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
757 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
758 "into %u segments failed with error code: %d\n",
759 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
761 /* figure out how many mbufs to free. */
765 /* free unused mbufs */
766 for (; i != nb_seg; i++) {
767 rte_pktmbuf_free_seg(md[i]);
775 * Receive a burst of packets, and for each packet:
776 * - parse packet, and try to recognize a supported packet type (1)
777 * - if it's not a supported packet type, don't touch the packet, else:
778 * - reprocess the checksum of all supported layers. This is done in SW
779 * or HW, depending on testpmd command line configuration
780 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
781 * segmentation offload (this implies HW TCP checksum)
782 * Then transmit packets on the output port.
784 * (1) Supported packets are:
785 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
786 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
788 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
790 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
792 * Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
793 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
794 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
795 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
797 * The testpmd command line for this forward engine sets the flags
798 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
799 * whether a checksum must be calculated in software or in hardware. The
800 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
801 * OUTER_IP is only useful for tunnel packets.
804 pkt_burst_checksum_forward(struct fwd_stream *fs)
806 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
808 struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
809 struct rte_gso_ctx *gso_ctx;
811 struct rte_mbuf **tx_pkts_burst;
812 struct rte_port *txp;
813 struct rte_mbuf *m, *p;
814 struct rte_ether_hdr *eth_hdr;
815 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
818 uint16_t gro_pkts_num;
825 uint64_t rx_ol_flags, tx_ol_flags;
826 uint64_t tx_offloads;
828 uint32_t rx_bad_ip_csum;
829 uint32_t rx_bad_l4_csum;
830 uint32_t rx_bad_outer_l4_csum;
831 uint32_t rx_bad_outer_ip_csum;
832 struct testpmd_offload_info info;
834 uint64_t start_tsc = 0;
836 get_start_cycles(&start_tsc);
838 /* receive a burst of packet */
839 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
841 inc_rx_burst_stats(fs, nb_rx);
842 if (unlikely(nb_rx == 0))
845 fs->rx_packets += nb_rx;
848 rx_bad_outer_l4_csum = 0;
849 rx_bad_outer_ip_csum = 0;
851 gro_enable = gro_ports[fs->rx_port].enable;
854 txp = &ports[fs->tx_port];
855 tx_offloads = txp->dev_conf.txmode.offloads;
856 memset(&info, 0, sizeof(info));
857 info.tso_segsz = txp->tso_segsz;
858 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
860 if (gso_ports[fs->tx_port].enable)
864 for (i = 0; i < nb_rx; i++) {
865 if (likely(i < nb_rx - 1))
866 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
871 info.pkt_len = rte_pktmbuf_pkt_len(m);
872 tx_ol_flags = m->ol_flags &
873 (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL);
874 rx_ol_flags = m->ol_flags;
876 /* Update the L3/L4 checksum error packet statistics */
877 if ((rx_ol_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) == RTE_MBUF_F_RX_IP_CKSUM_BAD)
879 if ((rx_ol_flags & RTE_MBUF_F_RX_L4_CKSUM_MASK) == RTE_MBUF_F_RX_L4_CKSUM_BAD)
881 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD)
882 rx_bad_outer_l4_csum += 1;
883 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD)
884 rx_bad_outer_ip_csum += 1;
886 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
887 * and inner headers */
889 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
890 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
892 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
894 parse_ethernet(eth_hdr, &info);
895 l3_hdr = (char *)eth_hdr + info.l2_len;
897 /* check if it's a supported tunnel */
898 if (txp->parse_tunnel) {
899 if (info.l4_proto == IPPROTO_UDP) {
900 struct rte_udp_hdr *udp_hdr;
902 udp_hdr = (struct rte_udp_hdr *)
903 ((char *)l3_hdr + info.l3_len);
904 parse_gtp(udp_hdr, &info);
905 if (info.is_tunnel) {
906 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GTP;
909 parse_vxlan_gpe(udp_hdr, &info);
910 if (info.is_tunnel) {
912 RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE;
915 parse_vxlan(udp_hdr, &info,
917 if (info.is_tunnel) {
919 RTE_MBUF_F_TX_TUNNEL_VXLAN;
922 parse_geneve(udp_hdr, &info);
923 if (info.is_tunnel) {
925 RTE_MBUF_F_TX_TUNNEL_GENEVE;
928 } else if (info.l4_proto == IPPROTO_GRE) {
929 struct simple_gre_hdr *gre_hdr;
931 gre_hdr = (struct simple_gre_hdr *)
932 ((char *)l3_hdr + info.l3_len);
933 parse_gre(gre_hdr, &info);
935 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GRE;
936 } else if (info.l4_proto == IPPROTO_IPIP) {
939 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
940 parse_encap_ip(encap_ip_hdr, &info);
942 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_IPIP;
947 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
948 if (info.is_tunnel) {
949 outer_l3_hdr = l3_hdr;
950 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
953 /* step 2: depending on user command line configuration,
954 * recompute checksum either in software or flag the
955 * mbuf to offload the calculation to the NIC. If TSO
956 * is configured, prepare the mbuf for TCP segmentation. */
958 /* process checksums of inner headers first */
959 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
962 /* Then process outer headers if any. Note that the software
963 * checksum will be wrong if one of the inner checksums is
964 * processed in hardware. */
965 if (info.is_tunnel == 1) {
966 tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
968 !!(tx_ol_flags & RTE_MBUF_F_TX_TCP_SEG));
971 /* step 3: fill the mbuf meta data (flags and header lengths) */
974 if (info.is_tunnel == 1) {
975 if (info.tunnel_tso_segsz ||
977 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
979 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM)) {
980 m->outer_l2_len = info.outer_l2_len;
981 m->outer_l3_len = info.outer_l3_len;
982 m->l2_len = info.l2_len;
983 m->l3_len = info.l3_len;
984 m->l4_len = info.l4_len;
985 m->tso_segsz = info.tunnel_tso_segsz;
988 /* if there is a outer UDP cksum
989 processed in sw and the inner in hw,
990 the outer checksum will be wrong as
991 the payload will be modified by the
993 m->l2_len = info.outer_l2_len +
994 info.outer_l3_len + info.l2_len;
995 m->l3_len = info.l3_len;
996 m->l4_len = info.l4_len;
999 /* this is only useful if an offload flag is
1000 * set, but it does not hurt to fill it in any
1002 m->l2_len = info.l2_len;
1003 m->l3_len = info.l3_len;
1004 m->l4_len = info.l4_len;
1005 m->tso_segsz = info.tso_segsz;
1007 m->ol_flags = tx_ol_flags;
1009 /* Do split & copy for the packet. */
1010 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
1011 p = pkt_copy_split(m);
1013 rte_pktmbuf_free(m);
1019 /* if verbose mode is enabled, dump debug info */
1020 if (verbose_level > 0) {
1023 printf("-----------------\n");
1024 printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
1025 fs->rx_port, m, m->pkt_len, m->nb_segs);
1026 /* dump rx parsed packet info */
1027 rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
1028 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
1029 "l4_proto=%d l4_len=%d flags=%s\n",
1030 info.l2_len, rte_be_to_cpu_16(info.ethertype),
1031 info.l3_len, info.l4_proto, info.l4_len, buf);
1032 if (rx_ol_flags & RTE_MBUF_F_RX_LRO)
1033 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
1034 if (info.is_tunnel == 1)
1035 printf("rx: outer_l2_len=%d outer_ethertype=%x "
1036 "outer_l3_len=%d\n", info.outer_l2_len,
1037 rte_be_to_cpu_16(info.outer_ethertype),
1039 /* dump tx packet info */
1040 if ((tx_offloads & (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1041 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1042 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
1043 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM)) ||
1044 info.tso_segsz != 0)
1045 printf("tx: m->l2_len=%d m->l3_len=%d "
1047 m->l2_len, m->l3_len, m->l4_len);
1048 if (info.is_tunnel == 1) {
1050 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1052 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
1053 (tx_ol_flags & RTE_MBUF_F_TX_OUTER_IPV6))
1054 printf("tx: m->outer_l2_len=%d "
1055 "m->outer_l3_len=%d\n",
1058 if (info.tunnel_tso_segsz != 0 &&
1059 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1060 printf("tx: m->tso_segsz=%d\n",
1062 } else if (info.tso_segsz != 0 &&
1063 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1064 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
1065 rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
1066 printf("tx: flags=%s", buf);
1072 if (unlikely(gro_enable)) {
1073 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
1074 nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
1075 &(gro_ports[fs->rx_port].param));
1077 gro_ctx = current_fwd_lcore()->gro_ctx;
1078 nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
1080 if (++fs->gro_times >= gro_flush_cycles) {
1081 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
1082 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
1083 gro_pkts_num = MAX_PKT_BURST - nb_rx;
1085 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
1096 if (gso_ports[fs->tx_port].enable != 0) {
1097 uint16_t nb_segments = 0;
1099 gso_ctx = &(current_fwd_lcore()->gso_ctx);
1100 gso_ctx->gso_size = gso_max_segment_size;
1101 for (i = 0; i < nb_rx; i++) {
1104 ret = rte_gso_segment(pkts_burst[i], gso_ctx,
1105 &gso_segments[nb_segments],
1106 GSO_MAX_PKT_BURST - nb_segments);
1108 /* pkts_burst[i] can be freed safely here. */
1109 rte_pktmbuf_free(pkts_burst[i]);
1111 } else if (ret == 0) {
1112 /* 0 means it can be transmitted directly
1115 gso_segments[nb_segments] = pkts_burst[i];
1118 TESTPMD_LOG(DEBUG, "Unable to segment packet");
1119 rte_pktmbuf_free(pkts_burst[i]);
1123 tx_pkts_burst = gso_segments;
1124 nb_rx = nb_segments;
1127 tx_pkts_burst = pkts_burst;
1129 nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
1130 tx_pkts_burst, nb_rx);
1131 if (nb_prep != nb_rx)
1133 "Preparing packet burst to transmit failed: %s\n",
1134 rte_strerror(rte_errno));
1136 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
1140 * Retry if necessary
1142 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
1144 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
1145 rte_delay_us(burst_tx_delay_time);
1146 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1147 &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1150 fs->tx_packets += nb_tx;
1151 fs->rx_bad_ip_csum += rx_bad_ip_csum;
1152 fs->rx_bad_l4_csum += rx_bad_l4_csum;
1153 fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1154 fs->rx_bad_outer_ip_csum += rx_bad_outer_ip_csum;
1156 inc_tx_burst_stats(fs, nb_tx);
1157 if (unlikely(nb_tx < nb_rx)) {
1158 fs->fwd_dropped += (nb_rx - nb_tx);
1160 rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1161 } while (++nb_tx < nb_rx);
1164 get_end_cycles(fs, start_tsc);
1167 struct fwd_engine csum_fwd_engine = {
1168 .fwd_mode_name = "csum",
1169 .port_fwd_begin = NULL,
1170 .port_fwd_end = NULL,
1171 .packet_fwd = pkt_burst_checksum_forward,