1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Inspur Corporation
5 #include "gso_common.h"
6 #include "gso_tunnel_udp4.h"
8 #define IPV4_HDR_MF_BIT (1U << 13)
11 update_tunnel_ipv4_udp_headers(struct rte_mbuf *pkt, struct rte_mbuf **segs,
14 struct rte_ipv4_hdr *ipv4_hdr;
15 uint16_t outer_id, inner_id, tail_idx, i, length;
16 uint16_t outer_ipv4_offset, inner_ipv4_offset;
17 uint16_t outer_udp_offset;
18 uint16_t frag_offset = 0, is_mf;
20 outer_ipv4_offset = pkt->outer_l2_len;
21 outer_udp_offset = outer_ipv4_offset + pkt->outer_l3_len;
22 inner_ipv4_offset = outer_udp_offset + pkt->l2_len;
24 /* Outer IPv4 header. */
25 ipv4_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
27 outer_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
29 /* Inner IPv4 header. */
30 ipv4_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
32 inner_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
34 tail_idx = nb_segs - 1;
36 for (i = 0; i < nb_segs; i++) {
37 update_ipv4_header(segs[i], outer_ipv4_offset, outer_id);
38 update_udp_header(segs[i], outer_udp_offset);
39 update_ipv4_header(segs[i], inner_ipv4_offset, inner_id);
40 /* For the case inner packet is UDP, we must keep UDP
41 * datagram boundary, it must be handled as IP fragment.
43 * Set IP fragment offset for inner IP header.
45 ipv4_hdr = (struct rte_ipv4_hdr *)
46 (rte_pktmbuf_mtod(segs[i], char *) +
48 is_mf = i < tail_idx ? IPV4_HDR_MF_BIT : 0;
49 ipv4_hdr->fragment_offset =
50 rte_cpu_to_be_16(frag_offset | is_mf);
51 length = segs[i]->pkt_len - inner_ipv4_offset - pkt->l3_len;
52 frag_offset += (length >> 3);
58 gso_tunnel_udp4_segment(struct rte_mbuf *pkt,
60 struct rte_mempool *direct_pool,
61 struct rte_mempool *indirect_pool,
62 struct rte_mbuf **pkts_out,
65 struct rte_ipv4_hdr *inner_ipv4_hdr;
66 uint16_t pyld_unit_size, hdr_offset, frag_off;
69 hdr_offset = pkt->outer_l2_len + pkt->outer_l3_len + pkt->l2_len;
70 inner_ipv4_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
73 * Don't process the packet whose MF bit or offset in the inner
74 * IPv4 header are non-zero.
76 frag_off = rte_be_to_cpu_16(inner_ipv4_hdr->fragment_offset);
77 if (unlikely(IS_FRAGMENTED(frag_off)))
80 hdr_offset += pkt->l3_len;
81 /* Don't process the packet without data */
82 if ((hdr_offset + pkt->l4_len) >= pkt->pkt_len)
85 /* pyld_unit_size must be a multiple of 8 because frag_off
86 * uses 8 bytes as unit.
88 pyld_unit_size = (gso_size - hdr_offset) & ~7U;
90 /* Segment the payload */
91 ret = gso_do_segment(pkt, hdr_offset, pyld_unit_size, direct_pool,
92 indirect_pool, pkts_out, nb_pkts_out);
94 update_tunnel_ipv4_udp_headers(pkt, pkts_out, ret);