xref: /dpdk/lib/gso/gso_udp4.c (revision 99a2dd955fba6e4cc23b77d590a033650ced9c45)
1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson  * Copyright(c) 2018 Intel Corporation
3*99a2dd95SBruce Richardson  */
4*99a2dd95SBruce Richardson 
5*99a2dd95SBruce Richardson #include "gso_common.h"
6*99a2dd95SBruce Richardson #include "gso_udp4.h"
7*99a2dd95SBruce Richardson 
8*99a2dd95SBruce Richardson #define IPV4_HDR_MF_BIT (1U << 13)
9*99a2dd95SBruce Richardson 
10*99a2dd95SBruce Richardson static inline void
update_ipv4_udp_headers(struct rte_mbuf * pkt,struct rte_mbuf ** segs,uint16_t nb_segs)11*99a2dd95SBruce Richardson update_ipv4_udp_headers(struct rte_mbuf *pkt, struct rte_mbuf **segs,
12*99a2dd95SBruce Richardson 		uint16_t nb_segs)
13*99a2dd95SBruce Richardson {
14*99a2dd95SBruce Richardson 	struct rte_ipv4_hdr *ipv4_hdr;
15*99a2dd95SBruce Richardson 	uint16_t frag_offset = 0, is_mf;
16*99a2dd95SBruce Richardson 	uint16_t l2_hdrlen = pkt->l2_len, l3_hdrlen = pkt->l3_len;
17*99a2dd95SBruce Richardson 	uint16_t tail_idx = nb_segs - 1, length, i;
18*99a2dd95SBruce Richardson 
19*99a2dd95SBruce Richardson 	/*
20*99a2dd95SBruce Richardson 	 * Update IP header fields for output segments. Specifically,
21*99a2dd95SBruce Richardson 	 * keep the same IP id, update fragment offset and total
22*99a2dd95SBruce Richardson 	 * length.
23*99a2dd95SBruce Richardson 	 */
24*99a2dd95SBruce Richardson 	for (i = 0; i < nb_segs; i++) {
25*99a2dd95SBruce Richardson 		ipv4_hdr = rte_pktmbuf_mtod_offset(segs[i],
26*99a2dd95SBruce Richardson 			struct rte_ipv4_hdr *, l2_hdrlen);
27*99a2dd95SBruce Richardson 		length = segs[i]->pkt_len - l2_hdrlen;
28*99a2dd95SBruce Richardson 		ipv4_hdr->total_length = rte_cpu_to_be_16(length);
29*99a2dd95SBruce Richardson 
30*99a2dd95SBruce Richardson 		is_mf = i < tail_idx ? IPV4_HDR_MF_BIT : 0;
31*99a2dd95SBruce Richardson 		ipv4_hdr->fragment_offset =
32*99a2dd95SBruce Richardson 			rte_cpu_to_be_16(frag_offset | is_mf);
33*99a2dd95SBruce Richardson 		frag_offset += ((length - l3_hdrlen) >> 3);
34*99a2dd95SBruce Richardson 	}
35*99a2dd95SBruce Richardson }
36*99a2dd95SBruce Richardson 
37*99a2dd95SBruce Richardson int
gso_udp4_segment(struct rte_mbuf * pkt,uint16_t gso_size,struct rte_mempool * direct_pool,struct rte_mempool * indirect_pool,struct rte_mbuf ** pkts_out,uint16_t nb_pkts_out)38*99a2dd95SBruce Richardson gso_udp4_segment(struct rte_mbuf *pkt,
39*99a2dd95SBruce Richardson 		uint16_t gso_size,
40*99a2dd95SBruce Richardson 		struct rte_mempool *direct_pool,
41*99a2dd95SBruce Richardson 		struct rte_mempool *indirect_pool,
42*99a2dd95SBruce Richardson 		struct rte_mbuf **pkts_out,
43*99a2dd95SBruce Richardson 		uint16_t nb_pkts_out)
44*99a2dd95SBruce Richardson {
45*99a2dd95SBruce Richardson 	struct rte_ipv4_hdr *ipv4_hdr;
46*99a2dd95SBruce Richardson 	uint16_t pyld_unit_size, hdr_offset;
47*99a2dd95SBruce Richardson 	uint16_t frag_off;
48*99a2dd95SBruce Richardson 	int ret;
49*99a2dd95SBruce Richardson 
50*99a2dd95SBruce Richardson 	/* Don't process the fragmented packet */
51*99a2dd95SBruce Richardson 	ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_ipv4_hdr *,
52*99a2dd95SBruce Richardson 			pkt->l2_len);
53*99a2dd95SBruce Richardson 	frag_off = rte_be_to_cpu_16(ipv4_hdr->fragment_offset);
54*99a2dd95SBruce Richardson 	if (unlikely(IS_FRAGMENTED(frag_off))) {
55*99a2dd95SBruce Richardson 		return 0;
56*99a2dd95SBruce Richardson 	}
57*99a2dd95SBruce Richardson 
58*99a2dd95SBruce Richardson 	/*
59*99a2dd95SBruce Richardson 	 * UDP fragmentation is the same as IP fragmentation.
60*99a2dd95SBruce Richardson 	 * Except the first one, other output packets just have l2
61*99a2dd95SBruce Richardson 	 * and l3 headers.
62*99a2dd95SBruce Richardson 	 */
63*99a2dd95SBruce Richardson 	hdr_offset = pkt->l2_len + pkt->l3_len;
64*99a2dd95SBruce Richardson 
65*99a2dd95SBruce Richardson 	/* Don't process the packet without data. */
66*99a2dd95SBruce Richardson 	if (unlikely(hdr_offset + pkt->l4_len >= pkt->pkt_len)) {
67*99a2dd95SBruce Richardson 		return 0;
68*99a2dd95SBruce Richardson 	}
69*99a2dd95SBruce Richardson 
70*99a2dd95SBruce Richardson 	/* pyld_unit_size must be a multiple of 8 because frag_off
71*99a2dd95SBruce Richardson 	 * uses 8 bytes as unit.
72*99a2dd95SBruce Richardson 	 */
73*99a2dd95SBruce Richardson 	pyld_unit_size = (gso_size - hdr_offset) & ~7U;
74*99a2dd95SBruce Richardson 
75*99a2dd95SBruce Richardson 	/* Segment the payload */
76*99a2dd95SBruce Richardson 	ret = gso_do_segment(pkt, hdr_offset, pyld_unit_size, direct_pool,
77*99a2dd95SBruce Richardson 			indirect_pool, pkts_out, nb_pkts_out);
78*99a2dd95SBruce Richardson 	if (ret > 1)
79*99a2dd95SBruce Richardson 		update_ipv4_udp_headers(pkt, pkts_out, ret);
80*99a2dd95SBruce Richardson 
81*99a2dd95SBruce Richardson 	return ret;
82*99a2dd95SBruce Richardson }
83