xref: /dpdk/lib/ip_frag/rte_ip_frag.h (revision 02d36ef6a9528e0f4a3403956e66bcea5fadbf8c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef _RTE_IP_FRAG_H_
6 #define _RTE_IP_FRAG_H_
7 
8 /**
9  * @file
10  * RTE IP Fragmentation and Reassembly
11  *
12  * Implementation of IP packet fragmentation and reassembly.
13  */
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 
22 #include <rte_compat.h>
23 #include <rte_config.h>
24 #include <rte_malloc.h>
25 #include <rte_memory.h>
26 #include <rte_ip.h>
27 #include <rte_byteorder.h>
28 
29 struct rte_mbuf;
30 
31 /** death row size (in packets) */
32 #define RTE_IP_FRAG_DEATH_ROW_LEN 32
33 
34 /** death row size in mbufs */
35 #define RTE_IP_FRAG_DEATH_ROW_MBUF_LEN \
36 	(RTE_IP_FRAG_DEATH_ROW_LEN * (RTE_LIBRTE_IP_FRAG_MAX_FRAG + 1))
37 
38 /** mbuf death row (packets to be freed) */
39 struct rte_ip_frag_death_row {
40 	uint32_t cnt;          /**< number of mbufs currently on death row */
41 	struct rte_mbuf *row[RTE_IP_FRAG_DEATH_ROW_MBUF_LEN];
42 	/**< mbufs to be freed */
43 };
44 
45 /**
46  * Create a new IP fragmentation table.
47  *
48  * @param bucket_num
49  *   Number of buckets in the hash table.
50  * @param bucket_entries
51  *   Number of entries per bucket (e.g. hash associativity).
52  *   Should be power of two.
53  * @param max_entries
54  *   Maximum number of entries that could be stored in the table.
55  *   The value should be less or equal then bucket_num * bucket_entries.
56  * @param max_cycles
57  *   Maximum TTL in cycles for each fragmented packet.
58  * @param socket_id
59  *   The *socket_id* argument is the socket identifier in the case of
60  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
61  * @return
62  *   The pointer to the new allocated fragmentation table, on success. NULL on error.
63  */
64 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
65 		uint32_t bucket_entries,  uint32_t max_entries,
66 		uint64_t max_cycles, int socket_id);
67 
68 /**
69  * Free allocated IP fragmentation table.
70  *
71  * @param tbl
72  *   Fragmentation table to free.
73  */
74 void
75 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
76 
77 /**
78  * This function implements the fragmentation of IPv6 packets.
79  *
80  * @param pkt_in
81  *   The input packet.
82  * @param pkts_out
83  *   Array storing the output fragments.
84  * @param nb_pkts_out
85  *   Number of fragments.
86  * @param mtu_size
87  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
88  *   datagrams. This value includes the size of the IPv6 header.
89  * @param pool_direct
90  *   MBUF pool used for allocating direct buffers for the output fragments.
91  * @param pool_indirect
92  *   MBUF pool used for allocating indirect buffers for the output fragments.
93  * @return
94  *   Upon successful completion - number of output fragments placed
95  *   in the pkts_out array.
96  *   Otherwise - (-1) * errno.
97  */
98 int32_t
99 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
100 		struct rte_mbuf **pkts_out,
101 		uint16_t nb_pkts_out,
102 		uint16_t mtu_size,
103 		struct rte_mempool *pool_direct,
104 		struct rte_mempool *pool_indirect);
105 
106 /**
107  * This function implements reassembly of fragmented IPv6 packets.
108  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
109  *
110  * @param tbl
111  *   Table where to lookup/add the fragmented packet.
112  * @param dr
113  *   Death row to free buffers to
114  * @param mb
115  *   Incoming mbuf with IPv6 fragment.
116  * @param tms
117  *   Fragment arrival timestamp.
118  * @param ip_hdr
119  *   Pointer to the IPv6 header.
120  * @param frag_hdr
121  *   Pointer to the IPv6 fragment extension header.
122  * @return
123  *   Pointer to mbuf for reassembled packet, or NULL if:
124  *   - an error occurred.
125  *   - not all fragments of the packet are collected yet.
126  */
127 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
128 		struct rte_ip_frag_death_row *dr,
129 		struct rte_mbuf *mb, uint64_t tms, struct rte_ipv6_hdr *ip_hdr,
130 		struct rte_ipv6_fragment_ext *frag_hdr);
131 
132 /**
133  * Return a pointer to the packet's fragment header, if found.
134  * It only looks at the extension header that's right after the fixed IPv6
135  * header, and doesn't follow the whole chain of extension headers.
136  *
137  * @param hdr
138  *   Pointer to the IPv6 header.
139  * @return
140  *   Pointer to the IPv6 fragment extension header, or NULL if it's not
141  *   present.
142  */
143 static inline struct rte_ipv6_fragment_ext *
144 rte_ipv6_frag_get_ipv6_fragment_header(struct rte_ipv6_hdr *hdr)
145 {
146 	if (hdr->proto == IPPROTO_FRAGMENT) {
147 		return (struct rte_ipv6_fragment_ext *) ++hdr;
148 	}
149 	else
150 		return NULL;
151 }
152 
153 /**
154  * IPv4 fragmentation.
155  *
156  * This function implements the fragmentation of IPv4 packets.
157  *
158  * @param pkt_in
159  *   The input packet.
160  * @param pkts_out
161  *   Array storing the output fragments.
162  * @param nb_pkts_out
163  *   Number of fragments.
164  * @param mtu_size
165  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
166  *   datagrams. This value includes the size of the IPv4 header.
167  * @param pool_direct
168  *   MBUF pool used for allocating direct buffers for the output fragments.
169  * @param pool_indirect
170  *   MBUF pool used for allocating indirect buffers for the output fragments.
171  * @return
172  *   Upon successful completion - number of output fragments placed
173  *   in the pkts_out array.
174  *   Otherwise - (-1) * errno.
175  */
176 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
177 			struct rte_mbuf **pkts_out,
178 			uint16_t nb_pkts_out, uint16_t mtu_size,
179 			struct rte_mempool *pool_direct,
180 			struct rte_mempool *pool_indirect);
181 
182 /**
183  * IPv4 fragmentation by copy.
184  *
185  * This function implements the fragmentation of IPv4 packets by copy
186  * non-segmented mbuf.
187  * This function is mainly used to adapt Tx MBUF_FAST_FREE offload.
188  * MBUF_FAST_FREE: Device supports optimization for fast release of mbufs.
189  * When set, application must guarantee that per-queue all mbufs comes from
190  * the same mempool, has refcnt = 1, direct and non-segmented.
191  *
192  * @param pkt_in
193  *   The input packet.
194  * @param pkts_out
195  *   Array storing the output fragments.
196  * @param nb_pkts_out
197  *   Number of fragments.
198  * @param mtu_size
199  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
200  *   datagrams. This value includes the size of the IPv4 header.
201  * @param pool_direct
202  *   MBUF pool used for allocating direct buffers for the output fragments.
203  * @return
204  *   Upon successful completion - number of output fragments placed
205  *   in the pkts_out array.
206  *   Otherwise - (-1) * errno.
207  */
208 __rte_experimental
209 int32_t
210 rte_ipv4_fragment_copy_nonseg_packet(struct rte_mbuf *pkt_in,
211 	struct rte_mbuf **pkts_out,
212 	uint16_t nb_pkts_out,
213 	uint16_t mtu_size,
214 	struct rte_mempool *pool_direct);
215 
216 /**
217  * This function implements reassembly of fragmented IPv4 packets.
218  * Incoming mbufs should have its l2_len/l3_len fields setup correctly.
219  *
220  * @param tbl
221  *   Table where to lookup/add the fragmented packet.
222  * @param dr
223  *   Death row to free buffers to
224  * @param mb
225  *   Incoming mbuf with IPv4 fragment.
226  * @param tms
227  *   Fragment arrival timestamp.
228  * @param ip_hdr
229  *   Pointer to the IPV4 header inside the fragment.
230  * @return
231  *   Pointer to mbuf for reassembled packet, or NULL if:
232  *   - an error occurred.
233  *   - not all fragments of the packet are collected yet.
234  */
235 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
236 		struct rte_ip_frag_death_row *dr,
237 		struct rte_mbuf *mb, uint64_t tms, struct rte_ipv4_hdr *ip_hdr);
238 
239 /**
240  * Check if the IPv4 packet is fragmented
241  *
242  * @param hdr
243  *   IPv4 header of the packet
244  * @return
245  *   1 if fragmented, 0 if not fragmented
246  */
247 static inline int
248 rte_ipv4_frag_pkt_is_fragmented(const struct rte_ipv4_hdr *hdr)
249 {
250 	uint16_t flag_offset, ip_flag, ip_ofs;
251 
252 	flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
253 	ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
254 	ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
255 
256 	return ip_flag != 0 || ip_ofs  != 0;
257 }
258 
259 /**
260  * Free mbufs on a given death row.
261  *
262  * @param dr
263  *   Death row to free mbufs in.
264  * @param prefetch
265  *   How many buffers to prefetch before freeing.
266  */
267 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
268 		uint32_t prefetch);
269 
270 
271 /**
272  * Dump fragmentation table statistics to file.
273  *
274  * @param f
275  *   File to dump statistics to
276  * @param tbl
277  *   Fragmentation table to dump statistics from
278  */
279 void
280 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
281 
282 /**
283  * Delete expired fragments
284  *
285  * @param tbl
286  *   Table to delete expired fragments from
287  * @param dr
288  *   Death row to free buffers to
289  * @param tms
290  *   Current timestamp
291  */
292 __rte_experimental
293 void
294 rte_ip_frag_table_del_expired_entries(struct rte_ip_frag_tbl *tbl,
295 	struct rte_ip_frag_death_row *dr, uint64_t tms);
296 
297 /**@{@name Obsolete macros, kept here for compatibility reasons.
298  * Will be deprecated/removed in future DPDK releases.
299  */
300 /** Obsolete */
301 #define IP_FRAG_DEATH_ROW_LEN		RTE_IP_FRAG_DEATH_ROW_LEN
302 /** Obsolete */
303 #define IP_FRAG_DEATH_ROW_MBUF_LEN	RTE_IP_FRAG_DEATH_ROW_MBUF_LEN
304 /** Obsolete */
305 #define ipv6_extension_fragment		rte_ipv6_fragment_ext
306 /**@}*/
307 
308 #ifdef __cplusplus
309 }
310 #endif
311 
312 #endif /* _RTE_IP_FRAG_H_ */
313