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(struct rte_mbuf *m, void *l3_hdr, uint16_t l4_off,
102 if (ethertype == _htons(RTE_ETHER_TYPE_IPV4))
103 return rte_ipv4_udptcp_cksum_mbuf(m, l3_hdr, l4_off);
104 else /* assume ethertype == RTE_ETHER_TYPE_IPV6 */
105 return rte_ipv6_udptcp_cksum_mbuf(m, l3_hdr, l4_off);
108 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
110 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
112 struct rte_tcp_hdr *tcp_hdr;
114 info->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
115 info->l4_proto = ipv4_hdr->next_proto_id;
117 /* only fill l4_len for TCP, it's useful for TSO */
118 if (info->l4_proto == IPPROTO_TCP) {
119 tcp_hdr = (struct rte_tcp_hdr *)
120 ((char *)ipv4_hdr + info->l3_len);
121 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
122 } else if (info->l4_proto == IPPROTO_UDP)
123 info->l4_len = sizeof(struct rte_udp_hdr);
128 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
130 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
132 struct rte_tcp_hdr *tcp_hdr;
134 info->l3_len = sizeof(struct rte_ipv6_hdr);
135 info->l4_proto = ipv6_hdr->proto;
137 /* only fill l4_len for TCP, it's useful for TSO */
138 if (info->l4_proto == IPPROTO_TCP) {
139 tcp_hdr = (struct rte_tcp_hdr *)
140 ((char *)ipv6_hdr + info->l3_len);
141 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
142 } else if (info->l4_proto == IPPROTO_UDP)
143 info->l4_len = sizeof(struct rte_udp_hdr);
149 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
150 * ipproto. This function is able to recognize IPv4/IPv6 with optional VLAN
151 * headers. The l4_len argument is only set in case of TCP (useful for TSO).
154 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
156 struct rte_ipv4_hdr *ipv4_hdr;
157 struct rte_ipv6_hdr *ipv6_hdr;
158 struct rte_vlan_hdr *vlan_hdr;
160 info->l2_len = sizeof(struct rte_ether_hdr);
161 info->ethertype = eth_hdr->ether_type;
163 while (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
164 info->ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
165 vlan_hdr = (struct rte_vlan_hdr *)
166 ((char *)eth_hdr + info->l2_len);
167 info->l2_len += sizeof(struct rte_vlan_hdr);
168 info->ethertype = vlan_hdr->eth_proto;
171 switch (info->ethertype) {
172 case _htons(RTE_ETHER_TYPE_IPV4):
173 ipv4_hdr = (struct rte_ipv4_hdr *)
174 ((char *)eth_hdr + info->l2_len);
175 parse_ipv4(ipv4_hdr, info);
177 case _htons(RTE_ETHER_TYPE_IPV6):
178 ipv6_hdr = (struct rte_ipv6_hdr *)
179 ((char *)eth_hdr + info->l2_len);
180 parse_ipv6(ipv6_hdr, info);
190 /* Fill in outer layers length */
192 update_tunnel_outer(struct testpmd_offload_info *info)
195 info->outer_ethertype = info->ethertype;
196 info->outer_l2_len = info->l2_len;
197 info->outer_l3_len = info->l3_len;
198 info->outer_l4_proto = info->l4_proto;
202 * Parse a GTP protocol header.
203 * No optional fields and next extension header type.
206 parse_gtp(struct rte_udp_hdr *udp_hdr,
207 struct testpmd_offload_info *info)
209 struct rte_ipv4_hdr *ipv4_hdr;
210 struct rte_ipv6_hdr *ipv6_hdr;
211 struct rte_gtp_hdr *gtp_hdr;
212 uint8_t gtp_len = sizeof(*gtp_hdr);
215 /* Check udp destination port. */
216 if (udp_hdr->dst_port != _htons(RTE_GTPC_UDP_PORT) &&
217 udp_hdr->src_port != _htons(RTE_GTPC_UDP_PORT) &&
218 udp_hdr->dst_port != _htons(RTE_GTPU_UDP_PORT))
221 update_tunnel_outer(info);
224 gtp_hdr = (struct rte_gtp_hdr *)((char *)udp_hdr +
225 sizeof(struct rte_udp_hdr));
228 * Check message type. If message type is 0xff, it is
229 * a GTP data packet. If not, it is a GTP control packet
231 if (gtp_hdr->msg_type == 0xff) {
232 ip_ver = *(uint8_t *)((char *)udp_hdr +
233 sizeof(struct rte_udp_hdr) +
234 sizeof(struct rte_gtp_hdr));
235 ip_ver = (ip_ver) & 0xf0;
237 if (ip_ver == RTE_GTP_TYPE_IPV4) {
238 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gtp_hdr +
240 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
241 parse_ipv4(ipv4_hdr, info);
242 } else if (ip_ver == RTE_GTP_TYPE_IPV6) {
243 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gtp_hdr +
245 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
246 parse_ipv6(ipv6_hdr, info);
255 info->l2_len += RTE_ETHER_GTP_HLEN;
258 /* Parse a vxlan header */
260 parse_vxlan(struct rte_udp_hdr *udp_hdr,
261 struct testpmd_offload_info *info,
264 struct rte_ether_hdr *eth_hdr;
266 /* check udp destination port, RTE_VXLAN_DEFAULT_PORT (4789) is the
267 * default vxlan port (rfc7348) or that the rx offload flag is set
268 * (i40e only currently)
270 if (udp_hdr->dst_port != _htons(RTE_VXLAN_DEFAULT_PORT) &&
271 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
274 update_tunnel_outer(info);
276 eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
277 sizeof(struct rte_udp_hdr) +
278 sizeof(struct rte_vxlan_hdr));
280 parse_ethernet(eth_hdr, info);
281 info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
284 /* Parse a vxlan-gpe header */
286 parse_vxlan_gpe(struct rte_udp_hdr *udp_hdr,
287 struct testpmd_offload_info *info)
289 struct rte_ether_hdr *eth_hdr;
290 struct rte_ipv4_hdr *ipv4_hdr;
291 struct rte_ipv6_hdr *ipv6_hdr;
292 struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
293 uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
295 /* Check udp destination port. */
296 if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
299 vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
300 sizeof(struct rte_udp_hdr));
302 if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
303 RTE_VXLAN_GPE_TYPE_IPV4) {
304 update_tunnel_outer(info);
306 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)vxlan_gpe_hdr +
309 parse_ipv4(ipv4_hdr, info);
310 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
313 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_IPV6) {
314 update_tunnel_outer(info);
316 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
319 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
320 parse_ipv6(ipv6_hdr, info);
323 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
324 update_tunnel_outer(info);
326 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
329 parse_ethernet(eth_hdr, info);
333 info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
336 /* Parse a geneve header */
338 parse_geneve(struct rte_udp_hdr *udp_hdr,
339 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;
344 struct rte_geneve_hdr *geneve_hdr;
347 /* Check udp destination port. */
348 if (udp_hdr->dst_port != _htons(geneve_udp_port))
351 geneve_hdr = (struct rte_geneve_hdr *)((char *)udp_hdr +
352 sizeof(struct rte_udp_hdr));
353 geneve_len = sizeof(struct rte_geneve_hdr) + geneve_hdr->opt_len * 4;
354 if (!geneve_hdr->proto || geneve_hdr->proto ==
355 _htons(RTE_ETHER_TYPE_IPV4)) {
356 update_tunnel_outer(info);
357 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)geneve_hdr +
359 parse_ipv4(ipv4_hdr, info);
360 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
362 } else if (geneve_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
363 update_tunnel_outer(info);
364 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)geneve_hdr +
366 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
367 parse_ipv6(ipv6_hdr, info);
370 } else if (geneve_hdr->proto == _htons(RTE_GENEVE_TYPE_ETH)) {
371 update_tunnel_outer(info);
372 eth_hdr = (struct rte_ether_hdr *)((char *)geneve_hdr +
374 parse_ethernet(eth_hdr, info);
379 (sizeof(struct rte_udp_hdr) + sizeof(struct rte_geneve_hdr) +
380 ((struct rte_geneve_hdr *)geneve_hdr)->opt_len * 4);
383 /* Parse a gre header */
385 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
387 struct rte_ether_hdr *eth_hdr;
388 struct rte_ipv4_hdr *ipv4_hdr;
389 struct rte_ipv6_hdr *ipv6_hdr;
392 gre_len += sizeof(struct simple_gre_hdr);
394 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
395 gre_len += GRE_EXT_LEN;
396 if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
397 gre_len += GRE_EXT_LEN;
398 if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
399 gre_len += GRE_EXT_LEN;
401 if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV4)) {
402 update_tunnel_outer(info);
404 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
406 parse_ipv4(ipv4_hdr, info);
407 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
410 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
411 update_tunnel_outer(info);
413 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
415 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
416 parse_ipv6(ipv6_hdr, info);
419 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
420 update_tunnel_outer(info);
422 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
424 parse_ethernet(eth_hdr, info);
428 info->l2_len += gre_len;
432 /* Parse an encapsulated ip or ipv6 header */
434 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
436 struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
437 struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
440 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
442 if (ip_version != 4 && ip_version != 6)
446 info->outer_ethertype = info->ethertype;
447 info->outer_l2_len = info->l2_len;
448 info->outer_l3_len = info->l3_len;
450 if (ip_version == 4) {
451 parse_ipv4(ipv4_hdr, info);
452 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
454 parse_ipv6(ipv6_hdr, info);
455 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
460 /* if possible, calculate the checksum of a packet in hw or sw,
461 * depending on the testpmd command line configuration */
463 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
464 uint64_t tx_offloads, struct rte_mbuf *m)
466 struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
467 struct rte_udp_hdr *udp_hdr;
468 struct rte_tcp_hdr *tcp_hdr;
469 struct rte_sctp_hdr *sctp_hdr;
470 uint64_t ol_flags = 0;
471 uint32_t max_pkt_len, tso_segsz = 0;
474 /* ensure packet is large enough to require tso */
475 if (!info->is_tunnel) {
476 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
478 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
479 tso_segsz = info->tso_segsz;
481 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
482 info->l2_len + info->l3_len + info->l4_len +
483 info->tunnel_tso_segsz;
484 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
485 tso_segsz = info->tunnel_tso_segsz;
488 if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
491 ol_flags |= RTE_MBUF_F_TX_IPV4;
492 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
493 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
495 if (tx_offloads & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM) {
496 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
498 ipv4_hdr->hdr_checksum = 0;
499 ipv4_hdr->hdr_checksum =
500 rte_ipv4_cksum(ipv4_hdr);
503 } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
504 ol_flags |= RTE_MBUF_F_TX_IPV6;
506 return 0; /* packet type not supported, nothing to do */
508 if (info->l4_proto == IPPROTO_UDP) {
509 udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
510 /* do not recalculate udp cksum if it was 0 */
511 if (udp_hdr->dgram_cksum != 0) {
512 if (tx_offloads & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
513 ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
516 l4_off = info->l2_len +
518 info->l2_len + info->l3_len;
520 l4_off = info->l2_len + info->l3_len;
521 udp_hdr->dgram_cksum = 0;
522 udp_hdr->dgram_cksum =
523 get_udptcp_checksum(m, l3_hdr, l4_off,
528 if (info->gso_enable)
529 ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
531 } else if (info->l4_proto == IPPROTO_TCP) {
532 tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
534 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
535 else if (tx_offloads & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
536 ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
539 l4_off = info->l2_len + info->outer_l3_len +
540 info->l2_len + info->l3_len;
542 l4_off = info->l2_len + info->l3_len;
545 get_udptcp_checksum(m, l3_hdr, l4_off,
549 if (info->gso_enable)
550 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
552 } else if (info->l4_proto == IPPROTO_SCTP) {
553 sctp_hdr = (struct rte_sctp_hdr *)
554 ((char *)l3_hdr + info->l3_len);
555 /* sctp payload must be a multiple of 4 to be
557 if ((tx_offloads & RTE_ETH_TX_OFFLOAD_SCTP_CKSUM) &&
558 ((ipv4_hdr->total_length & 0x3) == 0)) {
559 ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
562 /* XXX implement CRC32c, example available in
570 /* Calculate the checksum of outer header */
572 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
573 uint64_t tx_offloads, int tso_enabled, struct rte_mbuf *m)
575 struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
576 struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
577 struct rte_udp_hdr *udp_hdr;
578 uint64_t ol_flags = 0;
580 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
581 ipv4_hdr->hdr_checksum = 0;
582 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV4;
584 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM)
585 ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
587 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
589 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV6;
591 if (info->outer_l4_proto != IPPROTO_UDP)
594 udp_hdr = (struct rte_udp_hdr *)
595 ((char *)outer_l3_hdr + info->outer_l3_len);
598 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
600 /* Skip SW outer UDP checksum generation if HW supports it */
601 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) {
602 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
604 = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
607 = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
609 ol_flags |= RTE_MBUF_F_TX_OUTER_UDP_CKSUM;
613 /* outer UDP checksum is done in software. In the other side, for
614 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
617 * If a packet will be TSOed into small packets by NIC, we cannot
618 * set/calculate a non-zero checksum, because it will be a wrong
619 * value after the packet be split into several small packets.
622 udp_hdr->dgram_cksum = 0;
624 /* do not recalculate udp cksum if it was 0 */
625 if (udp_hdr->dgram_cksum != 0) {
626 udp_hdr->dgram_cksum = 0;
627 udp_hdr->dgram_cksum = get_udptcp_checksum(m, outer_l3_hdr,
628 info->l2_len + info->outer_l3_len,
629 info->outer_ethertype);
637 * Performs actual copying.
638 * Returns number of segments in the destination mbuf on success,
639 * or negative error code on failure.
642 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
643 uint16_t seglen[], uint8_t nb_seg)
645 uint32_t dlen, slen, tlen;
647 const struct rte_mbuf *m;
660 while (ms != NULL && i != nb_seg) {
663 slen = rte_pktmbuf_data_len(ms);
664 src = rte_pktmbuf_mtod(ms, const uint8_t *);
668 dlen = RTE_MIN(seglen[i], slen);
669 md[i]->data_len = dlen;
670 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
671 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
674 len = RTE_MIN(slen, dlen);
675 memcpy(dst, src, len);
690 else if (tlen != m->pkt_len)
693 md[0]->nb_segs = nb_seg;
694 md[0]->pkt_len = tlen;
695 md[0]->vlan_tci = m->vlan_tci;
696 md[0]->vlan_tci_outer = m->vlan_tci_outer;
697 md[0]->ol_flags = m->ol_flags;
698 md[0]->tx_offload = m->tx_offload;
704 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
705 * Copy packet contents and offload information into the new segmented mbuf.
707 static struct rte_mbuf *
708 pkt_copy_split(const struct rte_mbuf *pkt)
711 uint32_t i, len, nb_seg;
712 struct rte_mempool *mp;
713 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
714 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
716 mp = current_fwd_lcore()->mbp;
718 if (tx_pkt_split == TX_PKT_SPLIT_RND)
719 nb_seg = rte_rand() % tx_pkt_nb_segs + 1;
721 nb_seg = tx_pkt_nb_segs;
723 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
725 /* calculate number of segments to use and their length. */
727 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
732 n = pkt->pkt_len - len;
734 /* update size of the last segment to fit rest of the packet */
742 p = rte_pktmbuf_alloc(mp);
745 "failed to allocate %u-th of %u mbuf "
746 "from mempool: %s\n",
747 nb_seg - i, nb_seg, mp->name);
752 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
753 TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
754 "expected seglen: %u, "
755 "actual mbuf tailroom: %u\n",
756 mp->name, i, seglen[i],
757 rte_pktmbuf_tailroom(md[i]));
762 /* all mbufs successfully allocated, do copy */
764 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
767 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
768 "into %u segments failed with error code: %d\n",
769 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
771 /* figure out how many mbufs to free. */
775 /* free unused mbufs */
776 for (; i != nb_seg; i++) {
777 rte_pktmbuf_free_seg(md[i]);
785 * Receive a burst of packets, and for each packet:
786 * - parse packet, and try to recognize a supported packet type (1)
787 * - if it's not a supported packet type, don't touch the packet, else:
788 * - reprocess the checksum of all supported layers. This is done in SW
789 * or HW, depending on testpmd command line configuration
790 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
791 * segmentation offload (this implies HW TCP checksum)
792 * Then transmit packets on the output port.
794 * (1) Supported packets are:
795 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
796 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
798 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
800 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
802 * Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
803 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
804 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
805 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
807 * The testpmd command line for this forward engine sets the flags
808 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
809 * whether a checksum must be calculated in software or in hardware. The
810 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
811 * OUTER_IP is only useful for tunnel packets.
814 pkt_burst_checksum_forward(struct fwd_stream *fs)
816 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
818 struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
819 struct rte_gso_ctx *gso_ctx;
821 struct rte_mbuf **tx_pkts_burst;
822 struct rte_port *txp;
823 struct rte_mbuf *m, *p;
824 struct rte_ether_hdr *eth_hdr;
825 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
828 uint16_t gro_pkts_num;
835 uint64_t rx_ol_flags, tx_ol_flags;
836 uint64_t tx_offloads;
838 uint32_t rx_bad_ip_csum;
839 uint32_t rx_bad_l4_csum;
840 uint32_t rx_bad_outer_l4_csum;
841 uint32_t rx_bad_outer_ip_csum;
842 struct testpmd_offload_info info;
844 uint64_t start_tsc = 0;
846 get_start_cycles(&start_tsc);
848 /* receive a burst of packet */
849 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
851 inc_rx_burst_stats(fs, nb_rx);
852 if (unlikely(nb_rx == 0))
855 fs->rx_packets += nb_rx;
858 rx_bad_outer_l4_csum = 0;
859 rx_bad_outer_ip_csum = 0;
861 gro_enable = gro_ports[fs->rx_port].enable;
864 txp = &ports[fs->tx_port];
865 tx_offloads = txp->dev_conf.txmode.offloads;
866 memset(&info, 0, sizeof(info));
867 info.tso_segsz = txp->tso_segsz;
868 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
870 if (gso_ports[fs->tx_port].enable)
874 for (i = 0; i < nb_rx; i++) {
875 if (likely(i < nb_rx - 1))
876 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
881 info.pkt_len = rte_pktmbuf_pkt_len(m);
882 tx_ol_flags = m->ol_flags &
883 (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL);
884 rx_ol_flags = m->ol_flags;
886 /* Update the L3/L4 checksum error packet statistics */
887 if ((rx_ol_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) == RTE_MBUF_F_RX_IP_CKSUM_BAD)
889 if ((rx_ol_flags & RTE_MBUF_F_RX_L4_CKSUM_MASK) == RTE_MBUF_F_RX_L4_CKSUM_BAD)
891 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD)
892 rx_bad_outer_l4_csum += 1;
893 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD)
894 rx_bad_outer_ip_csum += 1;
896 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
897 * and inner headers */
899 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
900 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
902 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
904 parse_ethernet(eth_hdr, &info);
905 l3_hdr = (char *)eth_hdr + info.l2_len;
907 /* check if it's a supported tunnel */
908 if (txp->parse_tunnel) {
909 if (info.l4_proto == IPPROTO_UDP) {
910 struct rte_udp_hdr *udp_hdr;
912 udp_hdr = (struct rte_udp_hdr *)
913 ((char *)l3_hdr + info.l3_len);
914 parse_gtp(udp_hdr, &info);
915 if (info.is_tunnel) {
916 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GTP;
919 parse_vxlan_gpe(udp_hdr, &info);
920 if (info.is_tunnel) {
922 RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE;
925 parse_vxlan(udp_hdr, &info,
927 if (info.is_tunnel) {
929 RTE_MBUF_F_TX_TUNNEL_VXLAN;
932 parse_geneve(udp_hdr, &info);
933 if (info.is_tunnel) {
935 RTE_MBUF_F_TX_TUNNEL_GENEVE;
938 } else if (info.l4_proto == IPPROTO_GRE) {
939 struct simple_gre_hdr *gre_hdr;
941 gre_hdr = (struct simple_gre_hdr *)
942 ((char *)l3_hdr + info.l3_len);
943 parse_gre(gre_hdr, &info);
945 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GRE;
946 } else if (info.l4_proto == IPPROTO_IPIP) {
949 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
950 parse_encap_ip(encap_ip_hdr, &info);
952 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_IPIP;
957 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
958 if (info.is_tunnel) {
959 outer_l3_hdr = l3_hdr;
960 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
963 /* step 2: depending on user command line configuration,
964 * recompute checksum either in software or flag the
965 * mbuf to offload the calculation to the NIC. If TSO
966 * is configured, prepare the mbuf for TCP segmentation. */
968 /* process checksums of inner headers first */
969 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
972 /* Then process outer headers if any. Note that the software
973 * checksum will be wrong if one of the inner checksums is
974 * processed in hardware. */
975 if (info.is_tunnel == 1) {
976 tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
978 !!(tx_ol_flags & RTE_MBUF_F_TX_TCP_SEG),
982 /* step 3: fill the mbuf meta data (flags and header lengths) */
985 if (info.is_tunnel == 1) {
986 if (info.tunnel_tso_segsz ||
988 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
990 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM)) {
991 m->outer_l2_len = info.outer_l2_len;
992 m->outer_l3_len = info.outer_l3_len;
993 m->l2_len = info.l2_len;
994 m->l3_len = info.l3_len;
995 m->l4_len = info.l4_len;
996 m->tso_segsz = info.tunnel_tso_segsz;
999 /* if there is a outer UDP cksum
1000 processed in sw and the inner in hw,
1001 the outer checksum will be wrong as
1002 the payload will be modified by the
1004 m->l2_len = info.outer_l2_len +
1005 info.outer_l3_len + info.l2_len;
1006 m->l3_len = info.l3_len;
1007 m->l4_len = info.l4_len;
1010 /* this is only useful if an offload flag is
1011 * set, but it does not hurt to fill it in any
1013 m->l2_len = info.l2_len;
1014 m->l3_len = info.l3_len;
1015 m->l4_len = info.l4_len;
1016 m->tso_segsz = info.tso_segsz;
1018 m->ol_flags = tx_ol_flags;
1020 /* Do split & copy for the packet. */
1021 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
1022 p = pkt_copy_split(m);
1024 rte_pktmbuf_free(m);
1030 /* if verbose mode is enabled, dump debug info */
1031 if (verbose_level > 0) {
1034 printf("-----------------\n");
1035 printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
1036 fs->rx_port, m, m->pkt_len, m->nb_segs);
1037 /* dump rx parsed packet info */
1038 rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
1039 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
1040 "l4_proto=%d l4_len=%d flags=%s\n",
1041 info.l2_len, rte_be_to_cpu_16(info.ethertype),
1042 info.l3_len, info.l4_proto, info.l4_len, buf);
1043 if (rx_ol_flags & RTE_MBUF_F_RX_LRO)
1044 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
1045 if (info.is_tunnel == 1)
1046 printf("rx: outer_l2_len=%d outer_ethertype=%x "
1047 "outer_l3_len=%d\n", info.outer_l2_len,
1048 rte_be_to_cpu_16(info.outer_ethertype),
1050 /* dump tx packet info */
1051 if ((tx_offloads & (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1052 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1053 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
1054 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM)) ||
1055 info.tso_segsz != 0)
1056 printf("tx: m->l2_len=%d m->l3_len=%d "
1058 m->l2_len, m->l3_len, m->l4_len);
1059 if (info.is_tunnel == 1) {
1061 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1063 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
1064 (tx_ol_flags & RTE_MBUF_F_TX_OUTER_IPV6))
1065 printf("tx: m->outer_l2_len=%d "
1066 "m->outer_l3_len=%d\n",
1069 if (info.tunnel_tso_segsz != 0 &&
1070 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1071 printf("tx: m->tso_segsz=%d\n",
1073 } else if (info.tso_segsz != 0 &&
1074 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1075 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
1076 rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
1077 printf("tx: flags=%s", buf);
1083 if (unlikely(gro_enable)) {
1084 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
1085 nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
1086 &(gro_ports[fs->rx_port].param));
1088 gro_ctx = current_fwd_lcore()->gro_ctx;
1089 nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
1091 if (++fs->gro_times >= gro_flush_cycles) {
1092 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
1093 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
1094 gro_pkts_num = MAX_PKT_BURST - nb_rx;
1096 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
1107 if (gso_ports[fs->tx_port].enable != 0) {
1108 uint16_t nb_segments = 0;
1110 gso_ctx = &(current_fwd_lcore()->gso_ctx);
1111 gso_ctx->gso_size = gso_max_segment_size;
1112 for (i = 0; i < nb_rx; i++) {
1115 ret = rte_gso_segment(pkts_burst[i], gso_ctx,
1116 &gso_segments[nb_segments],
1117 GSO_MAX_PKT_BURST - nb_segments);
1119 /* pkts_burst[i] can be freed safely here. */
1120 rte_pktmbuf_free(pkts_burst[i]);
1122 } else if (ret == 0) {
1123 /* 0 means it can be transmitted directly
1126 gso_segments[nb_segments] = pkts_burst[i];
1129 TESTPMD_LOG(DEBUG, "Unable to segment packet");
1130 rte_pktmbuf_free(pkts_burst[i]);
1134 tx_pkts_burst = gso_segments;
1135 nb_rx = nb_segments;
1138 tx_pkts_burst = pkts_burst;
1140 nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
1141 tx_pkts_burst, nb_rx);
1142 if (nb_prep != nb_rx)
1144 "Preparing packet burst to transmit failed: %s\n",
1145 rte_strerror(rte_errno));
1147 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
1151 * Retry if necessary
1153 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
1155 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
1156 rte_delay_us(burst_tx_delay_time);
1157 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1158 &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1161 fs->tx_packets += nb_tx;
1162 fs->rx_bad_ip_csum += rx_bad_ip_csum;
1163 fs->rx_bad_l4_csum += rx_bad_l4_csum;
1164 fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1165 fs->rx_bad_outer_ip_csum += rx_bad_outer_ip_csum;
1167 inc_tx_burst_stats(fs, nb_tx);
1168 if (unlikely(nb_tx < nb_rx)) {
1169 fs->fwd_dropped += (nb_rx - nb_tx);
1171 rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1172 } while (++nb_tx < nb_rx);
1175 get_end_cycles(fs, start_tsc);
1178 struct fwd_engine csum_fwd_engine = {
1179 .fwd_mode_name = "csum",
1180 .port_fwd_begin = NULL,
1181 .port_fwd_end = NULL,
1182 .packet_fwd = pkt_burst_checksum_forward,