xref: /dpdk/lib/gso/gso_common.c (revision e15331dd269da07f7d70a0c7c6b06bd07949c4d1)
1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson  * Copyright(c) 2017 Intel Corporation
3*99a2dd95SBruce Richardson  */
4*99a2dd95SBruce Richardson 
5*99a2dd95SBruce Richardson #include <stdbool.h>
6*99a2dd95SBruce Richardson #include <errno.h>
7*99a2dd95SBruce Richardson 
8*99a2dd95SBruce Richardson #include <rte_memcpy.h>
9*99a2dd95SBruce Richardson 
10*99a2dd95SBruce Richardson #include "gso_common.h"
11*99a2dd95SBruce Richardson 
12*99a2dd95SBruce Richardson static inline void
hdr_segment_init(struct rte_mbuf * hdr_segment,struct rte_mbuf * pkt,uint16_t pkt_hdr_offset)13*99a2dd95SBruce Richardson hdr_segment_init(struct rte_mbuf *hdr_segment, struct rte_mbuf *pkt,
14*99a2dd95SBruce Richardson 		uint16_t pkt_hdr_offset)
15*99a2dd95SBruce Richardson {
16*99a2dd95SBruce Richardson 	/* Copy MBUF metadata */
17*99a2dd95SBruce Richardson 	hdr_segment->nb_segs = 1;
18*99a2dd95SBruce Richardson 	hdr_segment->port = pkt->port;
19*99a2dd95SBruce Richardson 	hdr_segment->ol_flags = pkt->ol_flags;
20*99a2dd95SBruce Richardson 	hdr_segment->packet_type = pkt->packet_type;
21*99a2dd95SBruce Richardson 	hdr_segment->pkt_len = pkt_hdr_offset;
22*99a2dd95SBruce Richardson 	hdr_segment->data_len = pkt_hdr_offset;
23*99a2dd95SBruce Richardson 	hdr_segment->tx_offload = pkt->tx_offload;
24*99a2dd95SBruce Richardson 
25*99a2dd95SBruce Richardson 	/* Copy the packet header */
26*99a2dd95SBruce Richardson 	rte_memcpy(rte_pktmbuf_mtod(hdr_segment, char *),
27*99a2dd95SBruce Richardson 			rte_pktmbuf_mtod(pkt, char *),
28*99a2dd95SBruce Richardson 			pkt_hdr_offset);
29*99a2dd95SBruce Richardson }
30*99a2dd95SBruce Richardson 
31*99a2dd95SBruce Richardson static inline void
free_gso_segment(struct rte_mbuf ** pkts,uint16_t nb_pkts)32*99a2dd95SBruce Richardson free_gso_segment(struct rte_mbuf **pkts, uint16_t nb_pkts)
33*99a2dd95SBruce Richardson {
34*99a2dd95SBruce Richardson 	uint16_t i;
35*99a2dd95SBruce Richardson 
36*99a2dd95SBruce Richardson 	for (i = 0; i < nb_pkts; i++)
37*99a2dd95SBruce Richardson 		rte_pktmbuf_free(pkts[i]);
38*99a2dd95SBruce Richardson }
39*99a2dd95SBruce Richardson 
40*99a2dd95SBruce Richardson int
gso_do_segment(struct rte_mbuf * pkt,uint16_t pkt_hdr_offset,uint16_t pyld_unit_size,struct rte_mempool * direct_pool,struct rte_mempool * indirect_pool,struct rte_mbuf ** pkts_out,uint16_t nb_pkts_out)41*99a2dd95SBruce Richardson gso_do_segment(struct rte_mbuf *pkt,
42*99a2dd95SBruce Richardson 		uint16_t pkt_hdr_offset,
43*99a2dd95SBruce Richardson 		uint16_t pyld_unit_size,
44*99a2dd95SBruce Richardson 		struct rte_mempool *direct_pool,
45*99a2dd95SBruce Richardson 		struct rte_mempool *indirect_pool,
46*99a2dd95SBruce Richardson 		struct rte_mbuf **pkts_out,
47*99a2dd95SBruce Richardson 		uint16_t nb_pkts_out)
48*99a2dd95SBruce Richardson {
49*99a2dd95SBruce Richardson 	struct rte_mbuf *pkt_in;
50*99a2dd95SBruce Richardson 	struct rte_mbuf *hdr_segment, *pyld_segment, *prev_segment;
51*99a2dd95SBruce Richardson 	uint16_t pkt_in_data_pos, segment_bytes_remaining;
52*99a2dd95SBruce Richardson 	uint16_t pyld_len, nb_segs;
53*99a2dd95SBruce Richardson 	bool more_in_pkt, more_out_segs;
54*99a2dd95SBruce Richardson 
55*99a2dd95SBruce Richardson 	pkt_in = pkt;
56*99a2dd95SBruce Richardson 	nb_segs = 0;
57*99a2dd95SBruce Richardson 	more_in_pkt = 1;
58*99a2dd95SBruce Richardson 	pkt_in_data_pos = pkt_hdr_offset;
59*99a2dd95SBruce Richardson 
60*99a2dd95SBruce Richardson 	while (more_in_pkt) {
61*99a2dd95SBruce Richardson 		if (unlikely(nb_segs >= nb_pkts_out)) {
62*99a2dd95SBruce Richardson 			free_gso_segment(pkts_out, nb_segs);
63*99a2dd95SBruce Richardson 			return -EINVAL;
64*99a2dd95SBruce Richardson 		}
65*99a2dd95SBruce Richardson 
66*99a2dd95SBruce Richardson 		/* Allocate a direct MBUF */
67*99a2dd95SBruce Richardson 		hdr_segment = rte_pktmbuf_alloc(direct_pool);
68*99a2dd95SBruce Richardson 		if (unlikely(hdr_segment == NULL)) {
69*99a2dd95SBruce Richardson 			free_gso_segment(pkts_out, nb_segs);
70*99a2dd95SBruce Richardson 			return -ENOMEM;
71*99a2dd95SBruce Richardson 		}
72*99a2dd95SBruce Richardson 		/* Fill the packet header */
73*99a2dd95SBruce Richardson 		hdr_segment_init(hdr_segment, pkt, pkt_hdr_offset);
74*99a2dd95SBruce Richardson 
75*99a2dd95SBruce Richardson 		prev_segment = hdr_segment;
76*99a2dd95SBruce Richardson 		segment_bytes_remaining = pyld_unit_size;
77*99a2dd95SBruce Richardson 		more_out_segs = 1;
78*99a2dd95SBruce Richardson 
79*99a2dd95SBruce Richardson 		while (more_out_segs && more_in_pkt) {
80*99a2dd95SBruce Richardson 			/* Allocate an indirect MBUF */
81*99a2dd95SBruce Richardson 			pyld_segment = rte_pktmbuf_alloc(indirect_pool);
82*99a2dd95SBruce Richardson 			if (unlikely(pyld_segment == NULL)) {
83*99a2dd95SBruce Richardson 				rte_pktmbuf_free(hdr_segment);
84*99a2dd95SBruce Richardson 				free_gso_segment(pkts_out, nb_segs);
85*99a2dd95SBruce Richardson 				return -ENOMEM;
86*99a2dd95SBruce Richardson 			}
87*99a2dd95SBruce Richardson 			/* Attach to current MBUF segment of pkt */
88*99a2dd95SBruce Richardson 			rte_pktmbuf_attach(pyld_segment, pkt_in);
89*99a2dd95SBruce Richardson 
90*99a2dd95SBruce Richardson 			prev_segment->next = pyld_segment;
91*99a2dd95SBruce Richardson 			prev_segment = pyld_segment;
92*99a2dd95SBruce Richardson 
93*99a2dd95SBruce Richardson 			pyld_len = segment_bytes_remaining;
94*99a2dd95SBruce Richardson 			if (pyld_len + pkt_in_data_pos > pkt_in->data_len)
95*99a2dd95SBruce Richardson 				pyld_len = pkt_in->data_len - pkt_in_data_pos;
96*99a2dd95SBruce Richardson 
97*99a2dd95SBruce Richardson 			pyld_segment->data_off = pkt_in_data_pos +
98*99a2dd95SBruce Richardson 				pkt_in->data_off;
99*99a2dd95SBruce Richardson 			pyld_segment->data_len = pyld_len;
100*99a2dd95SBruce Richardson 
101*99a2dd95SBruce Richardson 			/* Update header segment */
102*99a2dd95SBruce Richardson 			hdr_segment->pkt_len += pyld_len;
103*99a2dd95SBruce Richardson 			hdr_segment->nb_segs++;
104*99a2dd95SBruce Richardson 
105*99a2dd95SBruce Richardson 			pkt_in_data_pos += pyld_len;
106*99a2dd95SBruce Richardson 			segment_bytes_remaining -= pyld_len;
107*99a2dd95SBruce Richardson 
108*99a2dd95SBruce Richardson 			/* Finish processing a MBUF segment of pkt */
109*99a2dd95SBruce Richardson 			if (pkt_in_data_pos == pkt_in->data_len) {
110*99a2dd95SBruce Richardson 				pkt_in = pkt_in->next;
111*99a2dd95SBruce Richardson 				pkt_in_data_pos = 0;
112*99a2dd95SBruce Richardson 				if (pkt_in == NULL)
113*99a2dd95SBruce Richardson 					more_in_pkt = 0;
114*99a2dd95SBruce Richardson 			}
115*99a2dd95SBruce Richardson 
116*99a2dd95SBruce Richardson 			/* Finish generating a GSO segment */
117*99a2dd95SBruce Richardson 			if (segment_bytes_remaining == 0)
118*99a2dd95SBruce Richardson 				more_out_segs = 0;
119*99a2dd95SBruce Richardson 		}
120*99a2dd95SBruce Richardson 		pkts_out[nb_segs++] = hdr_segment;
121*99a2dd95SBruce Richardson 	}
122*99a2dd95SBruce Richardson 	return nb_segs;
123*99a2dd95SBruce Richardson }
124