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>
45 #include <rte_geneve.h>
49 #define IP_DEFTTL 64 /* from RFC 1340. */
51 #define GRE_CHECKSUM_PRESENT 0x8000
52 #define GRE_KEY_PRESENT 0x2000
53 #define GRE_SEQUENCE_PRESENT 0x1000
55 #define GRE_SUPPORTED_FIELDS (GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
58 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
59 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
60 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
65 uint16_t vxlan_gpe_udp_port = RTE_VXLAN_GPE_DEFAULT_PORT;
66 uint16_t geneve_udp_port = RTE_GENEVE_DEFAULT_PORT;
68 /* structure that caches offload info for the current packet */
69 struct testpmd_offload_info {
77 uint16_t outer_ethertype;
78 uint16_t outer_l2_len;
79 uint16_t outer_l3_len;
80 uint8_t outer_l4_proto;
82 uint16_t tunnel_tso_segsz;
86 /* simplified GRE header */
87 struct simple_gre_hdr {
93 get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype)
95 if (ethertype == _htons(RTE_ETHER_TYPE_IPV4))
96 return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
97 else /* assume ethertype == RTE_ETHER_TYPE_IPV6 */
98 return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
101 /* Parse an IPv4 header to fill l3_len, l4_len, and l4_proto */
103 parse_ipv4(struct rte_ipv4_hdr *ipv4_hdr, struct testpmd_offload_info *info)
105 struct rte_tcp_hdr *tcp_hdr;
107 info->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
108 info->l4_proto = ipv4_hdr->next_proto_id;
110 /* only fill l4_len for TCP, it's useful for TSO */
111 if (info->l4_proto == IPPROTO_TCP) {
112 tcp_hdr = (struct rte_tcp_hdr *)
113 ((char *)ipv4_hdr + info->l3_len);
114 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
115 } else if (info->l4_proto == IPPROTO_UDP)
116 info->l4_len = sizeof(struct rte_udp_hdr);
121 /* Parse an IPv6 header to fill l3_len, l4_len, and l4_proto */
123 parse_ipv6(struct rte_ipv6_hdr *ipv6_hdr, struct testpmd_offload_info *info)
125 struct rte_tcp_hdr *tcp_hdr;
127 info->l3_len = sizeof(struct rte_ipv6_hdr);
128 info->l4_proto = ipv6_hdr->proto;
130 /* only fill l4_len for TCP, it's useful for TSO */
131 if (info->l4_proto == IPPROTO_TCP) {
132 tcp_hdr = (struct rte_tcp_hdr *)
133 ((char *)ipv6_hdr + info->l3_len);
134 info->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
135 } else if (info->l4_proto == IPPROTO_UDP)
136 info->l4_len = sizeof(struct rte_udp_hdr);
142 * Parse an ethernet header to fill the ethertype, l2_len, l3_len and
143 * ipproto. This function is able to recognize IPv4/IPv6 with optional VLAN
144 * headers. The l4_len argument is only set in case of TCP (useful for TSO).
147 parse_ethernet(struct rte_ether_hdr *eth_hdr, struct testpmd_offload_info *info)
149 struct rte_ipv4_hdr *ipv4_hdr;
150 struct rte_ipv6_hdr *ipv6_hdr;
151 struct rte_vlan_hdr *vlan_hdr;
153 info->l2_len = sizeof(struct rte_ether_hdr);
154 info->ethertype = eth_hdr->ether_type;
156 while (info->ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
157 info->ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
158 vlan_hdr = (struct rte_vlan_hdr *)
159 ((char *)eth_hdr + info->l2_len);
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);
183 /* Fill in outer layers length */
185 update_tunnel_outer(struct testpmd_offload_info *info)
188 info->outer_ethertype = info->ethertype;
189 info->outer_l2_len = info->l2_len;
190 info->outer_l3_len = info->l3_len;
191 info->outer_l4_proto = info->l4_proto;
195 * Parse a GTP protocol header.
196 * No optional fields and next extension header type.
199 parse_gtp(struct rte_udp_hdr *udp_hdr,
200 struct testpmd_offload_info *info)
202 struct rte_ipv4_hdr *ipv4_hdr;
203 struct rte_ipv6_hdr *ipv6_hdr;
204 struct rte_gtp_hdr *gtp_hdr;
205 uint8_t gtp_len = sizeof(*gtp_hdr);
208 /* Check udp destination port. */
209 if (udp_hdr->dst_port != _htons(RTE_GTPC_UDP_PORT) &&
210 udp_hdr->src_port != _htons(RTE_GTPC_UDP_PORT) &&
211 udp_hdr->dst_port != _htons(RTE_GTPU_UDP_PORT))
214 update_tunnel_outer(info);
217 gtp_hdr = (struct rte_gtp_hdr *)((char *)udp_hdr +
218 sizeof(struct rte_udp_hdr));
221 * Check message type. If message type is 0xff, it is
222 * a GTP data packet. If not, it is a GTP control packet
224 if (gtp_hdr->msg_type == 0xff) {
225 ip_ver = *(uint8_t *)((char *)udp_hdr +
226 sizeof(struct rte_udp_hdr) +
227 sizeof(struct rte_gtp_hdr));
228 ip_ver = (ip_ver) & 0xf0;
230 if (ip_ver == RTE_GTP_TYPE_IPV4) {
231 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gtp_hdr +
233 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
234 parse_ipv4(ipv4_hdr, info);
235 } else if (ip_ver == RTE_GTP_TYPE_IPV6) {
236 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gtp_hdr +
238 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
239 parse_ipv6(ipv6_hdr, info);
248 info->l2_len += RTE_ETHER_GTP_HLEN;
251 /* Parse a vxlan header */
253 parse_vxlan(struct rte_udp_hdr *udp_hdr,
254 struct testpmd_offload_info *info,
257 struct rte_ether_hdr *eth_hdr;
259 /* check udp destination port, RTE_VXLAN_DEFAULT_PORT (4789) is the
260 * default vxlan port (rfc7348) or that the rx offload flag is set
261 * (i40e only currently)
263 if (udp_hdr->dst_port != _htons(RTE_VXLAN_DEFAULT_PORT) &&
264 RTE_ETH_IS_TUNNEL_PKT(pkt_type) == 0)
267 update_tunnel_outer(info);
269 eth_hdr = (struct rte_ether_hdr *)((char *)udp_hdr +
270 sizeof(struct rte_udp_hdr) +
271 sizeof(struct rte_vxlan_hdr));
273 parse_ethernet(eth_hdr, info);
274 info->l2_len += RTE_ETHER_VXLAN_HLEN; /* add udp + vxlan */
277 /* Parse a vxlan-gpe header */
279 parse_vxlan_gpe(struct rte_udp_hdr *udp_hdr,
280 struct testpmd_offload_info *info)
282 struct rte_ether_hdr *eth_hdr;
283 struct rte_ipv4_hdr *ipv4_hdr;
284 struct rte_ipv6_hdr *ipv6_hdr;
285 struct rte_vxlan_gpe_hdr *vxlan_gpe_hdr;
286 uint8_t vxlan_gpe_len = sizeof(*vxlan_gpe_hdr);
288 /* Check udp destination port. */
289 if (udp_hdr->dst_port != _htons(vxlan_gpe_udp_port))
292 vxlan_gpe_hdr = (struct rte_vxlan_gpe_hdr *)((char *)udp_hdr +
293 sizeof(struct rte_udp_hdr));
295 if (!vxlan_gpe_hdr->proto || vxlan_gpe_hdr->proto ==
296 RTE_VXLAN_GPE_TYPE_IPV4) {
297 update_tunnel_outer(info);
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) {
307 update_tunnel_outer(info);
309 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)vxlan_gpe_hdr +
312 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
313 parse_ipv6(ipv6_hdr, info);
316 } else if (vxlan_gpe_hdr->proto == RTE_VXLAN_GPE_TYPE_ETH) {
317 update_tunnel_outer(info);
319 eth_hdr = (struct rte_ether_hdr *)((char *)vxlan_gpe_hdr +
322 parse_ethernet(eth_hdr, info);
326 info->l2_len += RTE_ETHER_VXLAN_GPE_HLEN;
329 /* Parse a geneve header */
331 parse_geneve(struct rte_udp_hdr *udp_hdr,
332 struct testpmd_offload_info *info)
334 struct rte_ether_hdr *eth_hdr;
335 struct rte_ipv4_hdr *ipv4_hdr;
336 struct rte_ipv6_hdr *ipv6_hdr;
337 struct rte_geneve_hdr *geneve_hdr;
340 /* Check udp destination port. */
341 if (udp_hdr->dst_port != _htons(geneve_udp_port))
344 geneve_hdr = (struct rte_geneve_hdr *)((char *)udp_hdr +
345 sizeof(struct rte_udp_hdr));
346 geneve_len = sizeof(struct rte_geneve_hdr) + geneve_hdr->opt_len * 4;
347 if (!geneve_hdr->proto || geneve_hdr->proto ==
348 _htons(RTE_ETHER_TYPE_IPV4)) {
349 update_tunnel_outer(info);
350 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)geneve_hdr +
352 parse_ipv4(ipv4_hdr, info);
353 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
355 } else if (geneve_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
356 update_tunnel_outer(info);
357 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)geneve_hdr +
359 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
360 parse_ipv6(ipv6_hdr, info);
363 } else if (geneve_hdr->proto == _htons(RTE_GENEVE_TYPE_ETH)) {
364 update_tunnel_outer(info);
365 eth_hdr = (struct rte_ether_hdr *)((char *)geneve_hdr +
367 parse_ethernet(eth_hdr, info);
372 (sizeof(struct rte_udp_hdr) + sizeof(struct rte_geneve_hdr) +
373 ((struct rte_geneve_hdr *)geneve_hdr)->opt_len * 4);
376 /* Parse a gre header */
378 parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
380 struct rte_ether_hdr *eth_hdr;
381 struct rte_ipv4_hdr *ipv4_hdr;
382 struct rte_ipv6_hdr *ipv6_hdr;
385 gre_len += sizeof(struct simple_gre_hdr);
387 if (gre_hdr->flags & _htons(GRE_KEY_PRESENT))
388 gre_len += GRE_EXT_LEN;
389 if (gre_hdr->flags & _htons(GRE_SEQUENCE_PRESENT))
390 gre_len += GRE_EXT_LEN;
391 if (gre_hdr->flags & _htons(GRE_CHECKSUM_PRESENT))
392 gre_len += GRE_EXT_LEN;
394 if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV4)) {
395 update_tunnel_outer(info);
397 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)gre_hdr + gre_len);
399 parse_ipv4(ipv4_hdr, info);
400 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
403 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_IPV6)) {
404 update_tunnel_outer(info);
406 ipv6_hdr = (struct rte_ipv6_hdr *)((char *)gre_hdr + gre_len);
408 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
409 parse_ipv6(ipv6_hdr, info);
412 } else if (gre_hdr->proto == _htons(RTE_ETHER_TYPE_TEB)) {
413 update_tunnel_outer(info);
415 eth_hdr = (struct rte_ether_hdr *)((char *)gre_hdr + gre_len);
417 parse_ethernet(eth_hdr, info);
421 info->l2_len += gre_len;
425 /* Parse an encapsulated ip or ipv6 header */
427 parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
429 struct rte_ipv4_hdr *ipv4_hdr = encap_ip;
430 struct rte_ipv6_hdr *ipv6_hdr = encap_ip;
433 ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
435 if (ip_version != 4 && ip_version != 6)
439 info->outer_ethertype = info->ethertype;
440 info->outer_l2_len = info->l2_len;
441 info->outer_l3_len = info->l3_len;
443 if (ip_version == 4) {
444 parse_ipv4(ipv4_hdr, info);
445 info->ethertype = _htons(RTE_ETHER_TYPE_IPV4);
447 parse_ipv6(ipv6_hdr, info);
448 info->ethertype = _htons(RTE_ETHER_TYPE_IPV6);
453 /* if possible, calculate the checksum of a packet in hw or sw,
454 * depending on the testpmd command line configuration */
456 process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
457 uint64_t tx_offloads)
459 struct rte_ipv4_hdr *ipv4_hdr = l3_hdr;
460 struct rte_udp_hdr *udp_hdr;
461 struct rte_tcp_hdr *tcp_hdr;
462 struct rte_sctp_hdr *sctp_hdr;
463 uint64_t ol_flags = 0;
464 uint32_t max_pkt_len, tso_segsz = 0;
466 /* ensure packet is large enough to require tso */
467 if (!info->is_tunnel) {
468 max_pkt_len = info->l2_len + info->l3_len + info->l4_len +
470 if (info->tso_segsz != 0 && info->pkt_len > max_pkt_len)
471 tso_segsz = info->tso_segsz;
473 max_pkt_len = info->outer_l2_len + info->outer_l3_len +
474 info->l2_len + info->l3_len + info->l4_len +
475 info->tunnel_tso_segsz;
476 if (info->tunnel_tso_segsz != 0 && info->pkt_len > max_pkt_len)
477 tso_segsz = info->tunnel_tso_segsz;
480 if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
483 ol_flags |= RTE_MBUF_F_TX_IPV4;
484 if (info->l4_proto == IPPROTO_TCP && tso_segsz) {
485 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
487 if (tx_offloads & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM) {
488 ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
490 ipv4_hdr->hdr_checksum = 0;
491 ipv4_hdr->hdr_checksum =
492 rte_ipv4_cksum(ipv4_hdr);
495 } else if (info->ethertype == _htons(RTE_ETHER_TYPE_IPV6))
496 ol_flags |= RTE_MBUF_F_TX_IPV6;
498 return 0; /* packet type not supported, nothing to do */
500 if (info->l4_proto == IPPROTO_UDP) {
501 udp_hdr = (struct rte_udp_hdr *)((char *)l3_hdr + info->l3_len);
502 /* do not recalculate udp cksum if it was 0 */
503 if (udp_hdr->dgram_cksum != 0) {
504 if (tx_offloads & RTE_ETH_TX_OFFLOAD_UDP_CKSUM) {
505 ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
507 udp_hdr->dgram_cksum = 0;
508 udp_hdr->dgram_cksum =
509 get_udptcp_checksum(l3_hdr, udp_hdr,
513 if (info->gso_enable)
514 ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
515 } else if (info->l4_proto == IPPROTO_TCP) {
516 tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + info->l3_len);
518 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
519 else if (tx_offloads & RTE_ETH_TX_OFFLOAD_TCP_CKSUM) {
520 ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
524 get_udptcp_checksum(l3_hdr, tcp_hdr,
527 if (info->gso_enable)
528 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
529 } else if (info->l4_proto == IPPROTO_SCTP) {
530 sctp_hdr = (struct rte_sctp_hdr *)
531 ((char *)l3_hdr + info->l3_len);
532 /* sctp payload must be a multiple of 4 to be
534 if ((tx_offloads & RTE_ETH_TX_OFFLOAD_SCTP_CKSUM) &&
535 ((ipv4_hdr->total_length & 0x3) == 0)) {
536 ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
539 /* XXX implement CRC32c, example available in
547 /* Calculate the checksum of outer header */
549 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
550 uint64_t tx_offloads, int tso_enabled)
552 struct rte_ipv4_hdr *ipv4_hdr = outer_l3_hdr;
553 struct rte_ipv6_hdr *ipv6_hdr = outer_l3_hdr;
554 struct rte_udp_hdr *udp_hdr;
555 uint64_t ol_flags = 0;
557 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4)) {
558 ipv4_hdr->hdr_checksum = 0;
559 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV4;
561 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM)
562 ol_flags |= RTE_MBUF_F_TX_OUTER_IP_CKSUM;
564 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
566 ol_flags |= RTE_MBUF_F_TX_OUTER_IPV6;
568 if (info->outer_l4_proto != IPPROTO_UDP)
571 udp_hdr = (struct rte_udp_hdr *)
572 ((char *)outer_l3_hdr + info->outer_l3_len);
575 ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
577 /* Skip SW outer UDP checksum generation if HW supports it */
578 if (tx_offloads & RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) {
579 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
581 = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
584 = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
586 ol_flags |= RTE_MBUF_F_TX_OUTER_UDP_CKSUM;
590 /* outer UDP checksum is done in software. In the other side, for
591 * UDP tunneling, like VXLAN or Geneve, outer UDP checksum can be
594 * If a packet will be TSOed into small packets by NIC, we cannot
595 * set/calculate a non-zero checksum, because it will be a wrong
596 * value after the packet be split into several small packets.
599 udp_hdr->dgram_cksum = 0;
601 /* do not recalculate udp cksum if it was 0 */
602 if (udp_hdr->dgram_cksum != 0) {
603 udp_hdr->dgram_cksum = 0;
604 if (info->outer_ethertype == _htons(RTE_ETHER_TYPE_IPV4))
605 udp_hdr->dgram_cksum =
606 rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
608 udp_hdr->dgram_cksum =
609 rte_ipv6_udptcp_cksum(ipv6_hdr, udp_hdr);
617 * Performs actual copying.
618 * Returns number of segments in the destination mbuf on success,
619 * or negative error code on failure.
622 mbuf_copy_split(const struct rte_mbuf *ms, struct rte_mbuf *md[],
623 uint16_t seglen[], uint8_t nb_seg)
625 uint32_t dlen, slen, tlen;
627 const struct rte_mbuf *m;
640 while (ms != NULL && i != nb_seg) {
643 slen = rte_pktmbuf_data_len(ms);
644 src = rte_pktmbuf_mtod(ms, const uint8_t *);
648 dlen = RTE_MIN(seglen[i], slen);
649 md[i]->data_len = dlen;
650 md[i]->next = (i + 1 == nb_seg) ? NULL : md[i + 1];
651 dst = rte_pktmbuf_mtod(md[i], uint8_t *);
654 len = RTE_MIN(slen, dlen);
655 memcpy(dst, src, len);
670 else if (tlen != m->pkt_len)
673 md[0]->nb_segs = nb_seg;
674 md[0]->pkt_len = tlen;
675 md[0]->vlan_tci = m->vlan_tci;
676 md[0]->vlan_tci_outer = m->vlan_tci_outer;
677 md[0]->ol_flags = m->ol_flags;
678 md[0]->tx_offload = m->tx_offload;
684 * Allocate a new mbuf with up to tx_pkt_nb_segs segments.
685 * Copy packet contents and offload information into the new segmented mbuf.
687 static struct rte_mbuf *
688 pkt_copy_split(const struct rte_mbuf *pkt)
691 uint32_t i, len, nb_seg;
692 struct rte_mempool *mp;
693 uint16_t seglen[RTE_MAX_SEGS_PER_PKT];
694 struct rte_mbuf *p, *md[RTE_MAX_SEGS_PER_PKT];
696 mp = current_fwd_lcore()->mbp;
698 if (tx_pkt_split == TX_PKT_SPLIT_RND)
699 nb_seg = rte_rand() % tx_pkt_nb_segs + 1;
701 nb_seg = tx_pkt_nb_segs;
703 memcpy(seglen, tx_pkt_seg_lengths, nb_seg * sizeof(seglen[0]));
705 /* calculate number of segments to use and their length. */
707 for (i = 0; i != nb_seg && len < pkt->pkt_len; i++) {
712 n = pkt->pkt_len - len;
714 /* update size of the last segment to fit rest of the packet */
722 p = rte_pktmbuf_alloc(mp);
725 "failed to allocate %u-th of %u mbuf "
726 "from mempool: %s\n",
727 nb_seg - i, nb_seg, mp->name);
732 if (rte_pktmbuf_tailroom(md[i]) < seglen[i]) {
733 TESTPMD_LOG(ERR, "mempool %s, %u-th segment: "
734 "expected seglen: %u, "
735 "actual mbuf tailroom: %u\n",
736 mp->name, i, seglen[i],
737 rte_pktmbuf_tailroom(md[i]));
742 /* all mbufs successfully allocated, do copy */
744 rc = mbuf_copy_split(pkt, md, seglen, nb_seg);
747 "mbuf_copy_split for %p(len=%u, nb_seg=%u) "
748 "into %u segments failed with error code: %d\n",
749 pkt, pkt->pkt_len, pkt->nb_segs, nb_seg, rc);
751 /* figure out how many mbufs to free. */
755 /* free unused mbufs */
756 for (; i != nb_seg; i++) {
757 rte_pktmbuf_free_seg(md[i]);
765 * Receive a burst of packets, and for each packet:
766 * - parse packet, and try to recognize a supported packet type (1)
767 * - if it's not a supported packet type, don't touch the packet, else:
768 * - reprocess the checksum of all supported layers. This is done in SW
769 * or HW, depending on testpmd command line configuration
770 * - if TSO is enabled in testpmd command line, also flag the mbuf for TCP
771 * segmentation offload (this implies HW TCP checksum)
772 * Then transmit packets on the output port.
774 * (1) Supported packets are:
775 * Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
776 * Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
778 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
780 * Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
782 * Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
783 * Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
784 * Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
785 * Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
787 * The testpmd command line for this forward engine sets the flags
788 * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
789 * wether a checksum must be calculated in software or in hardware. The
790 * IP, UDP, TCP and SCTP flags always concern the inner layer. The
791 * OUTER_IP is only useful for tunnel packets.
794 pkt_burst_checksum_forward(struct fwd_stream *fs)
796 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
797 struct rte_mbuf *gso_segments[GSO_MAX_PKT_BURST];
798 struct rte_gso_ctx *gso_ctx;
799 struct rte_mbuf **tx_pkts_burst;
800 struct rte_port *txp;
801 struct rte_mbuf *m, *p;
802 struct rte_ether_hdr *eth_hdr;
803 void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
805 uint16_t gro_pkts_num;
811 uint64_t rx_ol_flags, tx_ol_flags;
812 uint64_t tx_offloads;
814 uint32_t rx_bad_ip_csum;
815 uint32_t rx_bad_l4_csum;
816 uint32_t rx_bad_outer_l4_csum;
817 uint32_t rx_bad_outer_ip_csum;
818 struct testpmd_offload_info info;
819 uint16_t nb_segments = 0;
822 uint64_t start_tsc = 0;
824 get_start_cycles(&start_tsc);
826 /* receive a burst of packet */
827 nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
829 inc_rx_burst_stats(fs, nb_rx);
830 if (unlikely(nb_rx == 0))
833 fs->rx_packets += nb_rx;
836 rx_bad_outer_l4_csum = 0;
837 rx_bad_outer_ip_csum = 0;
838 gro_enable = gro_ports[fs->rx_port].enable;
840 txp = &ports[fs->tx_port];
841 tx_offloads = txp->dev_conf.txmode.offloads;
842 memset(&info, 0, sizeof(info));
843 info.tso_segsz = txp->tso_segsz;
844 info.tunnel_tso_segsz = txp->tunnel_tso_segsz;
845 if (gso_ports[fs->tx_port].enable)
848 for (i = 0; i < nb_rx; i++) {
849 if (likely(i < nb_rx - 1))
850 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i + 1],
855 info.pkt_len = rte_pktmbuf_pkt_len(m);
856 tx_ol_flags = m->ol_flags &
857 (RTE_MBUF_F_INDIRECT | RTE_MBUF_F_EXTERNAL);
858 rx_ol_flags = m->ol_flags;
860 /* Update the L3/L4 checksum error packet statistics */
861 if ((rx_ol_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) == RTE_MBUF_F_RX_IP_CKSUM_BAD)
863 if ((rx_ol_flags & RTE_MBUF_F_RX_L4_CKSUM_MASK) == RTE_MBUF_F_RX_L4_CKSUM_BAD)
865 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_L4_CKSUM_BAD)
866 rx_bad_outer_l4_csum += 1;
867 if (rx_ol_flags & RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD)
868 rx_bad_outer_ip_csum += 1;
870 /* step 1: dissect packet, parsing optional vlan, ip4/ip6, vxlan
871 * and inner headers */
873 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
874 rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
876 rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
878 parse_ethernet(eth_hdr, &info);
879 l3_hdr = (char *)eth_hdr + info.l2_len;
881 /* check if it's a supported tunnel */
882 if (txp->parse_tunnel) {
883 if (info.l4_proto == IPPROTO_UDP) {
884 struct rte_udp_hdr *udp_hdr;
886 udp_hdr = (struct rte_udp_hdr *)
887 ((char *)l3_hdr + info.l3_len);
888 parse_gtp(udp_hdr, &info);
889 if (info.is_tunnel) {
890 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GTP;
893 parse_vxlan_gpe(udp_hdr, &info);
894 if (info.is_tunnel) {
896 RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE;
899 parse_vxlan(udp_hdr, &info,
901 if (info.is_tunnel) {
903 RTE_MBUF_F_TX_TUNNEL_VXLAN;
906 parse_geneve(udp_hdr, &info);
907 if (info.is_tunnel) {
909 RTE_MBUF_F_TX_TUNNEL_GENEVE;
912 } else if (info.l4_proto == IPPROTO_GRE) {
913 struct simple_gre_hdr *gre_hdr;
915 gre_hdr = (struct simple_gre_hdr *)
916 ((char *)l3_hdr + info.l3_len);
917 parse_gre(gre_hdr, &info);
919 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_GRE;
920 } else if (info.l4_proto == IPPROTO_IPIP) {
923 encap_ip_hdr = (char *)l3_hdr + info.l3_len;
924 parse_encap_ip(encap_ip_hdr, &info);
926 tx_ol_flags |= RTE_MBUF_F_TX_TUNNEL_IPIP;
931 /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
932 if (info.is_tunnel) {
933 outer_l3_hdr = l3_hdr;
934 l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
937 /* step 2: depending on user command line configuration,
938 * recompute checksum either in software or flag the
939 * mbuf to offload the calculation to the NIC. If TSO
940 * is configured, prepare the mbuf for TCP segmentation. */
942 /* process checksums of inner headers first */
943 tx_ol_flags |= process_inner_cksums(l3_hdr, &info,
946 /* Then process outer headers if any. Note that the software
947 * checksum will be wrong if one of the inner checksums is
948 * processed in hardware. */
949 if (info.is_tunnel == 1) {
950 tx_ol_flags |= process_outer_cksums(outer_l3_hdr, &info,
952 !!(tx_ol_flags & RTE_MBUF_F_TX_TCP_SEG));
955 /* step 3: fill the mbuf meta data (flags and header lengths) */
958 if (info.is_tunnel == 1) {
959 if (info.tunnel_tso_segsz ||
961 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
963 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM)) {
964 m->outer_l2_len = info.outer_l2_len;
965 m->outer_l3_len = info.outer_l3_len;
966 m->l2_len = info.l2_len;
967 m->l3_len = info.l3_len;
968 m->l4_len = info.l4_len;
969 m->tso_segsz = info.tunnel_tso_segsz;
972 /* if there is a outer UDP cksum
973 processed in sw and the inner in hw,
974 the outer checksum will be wrong as
975 the payload will be modified by the
977 m->l2_len = info.outer_l2_len +
978 info.outer_l3_len + info.l2_len;
979 m->l3_len = info.l3_len;
980 m->l4_len = info.l4_len;
983 /* this is only useful if an offload flag is
984 * set, but it does not hurt to fill it in any
986 m->l2_len = info.l2_len;
987 m->l3_len = info.l3_len;
988 m->l4_len = info.l4_len;
989 m->tso_segsz = info.tso_segsz;
991 m->ol_flags = tx_ol_flags;
993 /* Do split & copy for the packet. */
994 if (tx_pkt_split != TX_PKT_SPLIT_OFF) {
995 p = pkt_copy_split(m);
1003 /* if verbose mode is enabled, dump debug info */
1004 if (verbose_level > 0) {
1007 printf("-----------------\n");
1008 printf("port=%u, mbuf=%p, pkt_len=%u, nb_segs=%u:\n",
1009 fs->rx_port, m, m->pkt_len, m->nb_segs);
1010 /* dump rx parsed packet info */
1011 rte_get_rx_ol_flag_list(rx_ol_flags, buf, sizeof(buf));
1012 printf("rx: l2_len=%d ethertype=%x l3_len=%d "
1013 "l4_proto=%d l4_len=%d flags=%s\n",
1014 info.l2_len, rte_be_to_cpu_16(info.ethertype),
1015 info.l3_len, info.l4_proto, info.l4_len, buf);
1016 if (rx_ol_flags & RTE_MBUF_F_RX_LRO)
1017 printf("rx: m->lro_segsz=%u\n", m->tso_segsz);
1018 if (info.is_tunnel == 1)
1019 printf("rx: outer_l2_len=%d outer_ethertype=%x "
1020 "outer_l3_len=%d\n", info.outer_l2_len,
1021 rte_be_to_cpu_16(info.outer_ethertype),
1023 /* dump tx packet info */
1024 if ((tx_offloads & (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
1025 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1026 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
1027 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM)) ||
1028 info.tso_segsz != 0)
1029 printf("tx: m->l2_len=%d m->l3_len=%d "
1031 m->l2_len, m->l3_len, m->l4_len);
1032 if (info.is_tunnel == 1) {
1034 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1036 RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM) ||
1037 (tx_ol_flags & RTE_MBUF_F_TX_OUTER_IPV6))
1038 printf("tx: m->outer_l2_len=%d "
1039 "m->outer_l3_len=%d\n",
1042 if (info.tunnel_tso_segsz != 0 &&
1043 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1044 printf("tx: m->tso_segsz=%d\n",
1046 } else if (info.tso_segsz != 0 &&
1047 (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG))
1048 printf("tx: m->tso_segsz=%d\n", m->tso_segsz);
1049 rte_get_tx_ol_flag_list(m->ol_flags, buf, sizeof(buf));
1050 printf("tx: flags=%s", buf);
1055 if (unlikely(gro_enable)) {
1056 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
1057 nb_rx = rte_gro_reassemble_burst(pkts_burst, nb_rx,
1058 &(gro_ports[fs->rx_port].param));
1060 gro_ctx = current_fwd_lcore()->gro_ctx;
1061 nb_rx = rte_gro_reassemble(pkts_burst, nb_rx, gro_ctx);
1063 if (++fs->gro_times >= gro_flush_cycles) {
1064 gro_pkts_num = rte_gro_get_pkt_count(gro_ctx);
1065 if (gro_pkts_num > MAX_PKT_BURST - nb_rx)
1066 gro_pkts_num = MAX_PKT_BURST - nb_rx;
1068 nb_rx += rte_gro_timeout_flush(gro_ctx, 0,
1077 if (gso_ports[fs->tx_port].enable == 0)
1078 tx_pkts_burst = pkts_burst;
1080 gso_ctx = &(current_fwd_lcore()->gso_ctx);
1081 gso_ctx->gso_size = gso_max_segment_size;
1082 for (i = 0; i < nb_rx; i++) {
1083 ret = rte_gso_segment(pkts_burst[i], gso_ctx,
1084 &gso_segments[nb_segments],
1085 GSO_MAX_PKT_BURST - nb_segments);
1087 /* pkts_burst[i] can be freed safely here. */
1088 rte_pktmbuf_free(pkts_burst[i]);
1090 } else if (ret == 0) {
1091 /* 0 means it can be transmitted directly
1094 gso_segments[nb_segments] = pkts_burst[i];
1097 TESTPMD_LOG(DEBUG, "Unable to segment packet");
1098 rte_pktmbuf_free(pkts_burst[i]);
1102 tx_pkts_burst = gso_segments;
1103 nb_rx = nb_segments;
1106 nb_prep = rte_eth_tx_prepare(fs->tx_port, fs->tx_queue,
1107 tx_pkts_burst, nb_rx);
1108 if (nb_prep != nb_rx)
1110 "Preparing packet burst to transmit failed: %s\n",
1111 rte_strerror(rte_errno));
1113 nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, tx_pkts_burst,
1117 * Retry if necessary
1119 if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
1121 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
1122 rte_delay_us(burst_tx_delay_time);
1123 nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
1124 &tx_pkts_burst[nb_tx], nb_rx - nb_tx);
1127 fs->tx_packets += nb_tx;
1128 fs->rx_bad_ip_csum += rx_bad_ip_csum;
1129 fs->rx_bad_l4_csum += rx_bad_l4_csum;
1130 fs->rx_bad_outer_l4_csum += rx_bad_outer_l4_csum;
1131 fs->rx_bad_outer_ip_csum += rx_bad_outer_ip_csum;
1133 inc_tx_burst_stats(fs, nb_tx);
1134 if (unlikely(nb_tx < nb_rx)) {
1135 fs->fwd_dropped += (nb_rx - nb_tx);
1137 rte_pktmbuf_free(tx_pkts_burst[nb_tx]);
1138 } while (++nb_tx < nb_rx);
1141 get_end_cycles(fs, start_tsc);
1144 struct fwd_engine csum_fwd_engine = {
1145 .fwd_mode_name = "csum",
1146 .port_fwd_begin = NULL,
1147 .port_fwd_end = NULL,
1148 .packet_fwd = pkt_burst_checksum_forward,