ip_frag: hide internal structures
[dpdk.git] / lib / ip_frag / rte_ip_frag.h
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_config.h>
23 #include <rte_malloc.h>
24 #include <rte_memory.h>
25 #include <rte_ip.h>
26 #include <rte_byteorder.h>
27
28 struct rte_mbuf;
29
30 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
31
32 /* death row size in mbufs */
33 #define IP_FRAG_DEATH_ROW_MBUF_LEN \
34         (IP_FRAG_DEATH_ROW_LEN * (RTE_LIBRTE_IP_FRAG_MAX_FRAG + 1))
35
36 /** mbuf death row (packets to be freed) */
37 struct rte_ip_frag_death_row {
38         uint32_t cnt;          /**< number of mbufs currently on death row */
39         struct rte_mbuf *row[IP_FRAG_DEATH_ROW_MBUF_LEN];
40         /**< mbufs to be freed */
41 };
42
43 /* struct ipv6_extension_fragment moved to librte_net/rte_ip.h and renamed. */
44 #define ipv6_extension_fragment rte_ipv6_fragment_ext
45
46 /**
47  * Create a new IP fragmentation table.
48  *
49  * @param bucket_num
50  *   Number of buckets in the hash table.
51  * @param bucket_entries
52  *   Number of entries per bucket (e.g. hash associativity).
53  *   Should be power of two.
54  * @param max_entries
55  *   Maximum number of entries that could be stored in the table.
56  *   The value should be less or equal then bucket_num * bucket_entries.
57  * @param max_cycles
58  *   Maximum TTL in cycles for each fragmented packet.
59  * @param socket_id
60  *   The *socket_id* argument is the socket identifier in the case of
61  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
62  * @return
63  *   The pointer to the new allocated fragmentation table, on success. NULL on error.
64  */
65 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
66                 uint32_t bucket_entries,  uint32_t max_entries,
67                 uint64_t max_cycles, int socket_id);
68
69 /**
70  * Free allocated IP fragmentation table.
71  *
72  * @param tbl
73  *   Fragmentation table to free.
74  */
75 void
76 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
77
78 /**
79  * This function implements the fragmentation of IPv6 packets.
80  *
81  * @param pkt_in
82  *   The input packet.
83  * @param pkts_out
84  *   Array storing the output fragments.
85  * @param nb_pkts_out
86  *   Number of fragments.
87  * @param mtu_size
88  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
89  *   datagrams. This value includes the size of the IPv6 header.
90  * @param pool_direct
91  *   MBUF pool used for allocating direct buffers for the output fragments.
92  * @param pool_indirect
93  *   MBUF pool used for allocating indirect buffers for the output fragments.
94  * @return
95  *   Upon successful completion - number of output fragments placed
96  *   in the pkts_out array.
97  *   Otherwise - (-1) * errno.
98  */
99 int32_t
100 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
101                 struct rte_mbuf **pkts_out,
102                 uint16_t nb_pkts_out,
103                 uint16_t mtu_size,
104                 struct rte_mempool *pool_direct,
105                 struct rte_mempool *pool_indirect);
106
107 /**
108  * This function implements reassembly of fragmented IPv6 packets.
109  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
110  *
111  * @param tbl
112  *   Table where to lookup/add the fragmented packet.
113  * @param dr
114  *   Death row to free buffers to
115  * @param mb
116  *   Incoming mbuf with IPv6 fragment.
117  * @param tms
118  *   Fragment arrival timestamp.
119  * @param ip_hdr
120  *   Pointer to the IPv6 header.
121  * @param frag_hdr
122  *   Pointer to the IPv6 fragment extension header.
123  * @return
124  *   Pointer to mbuf for reassembled packet, or NULL if:
125  *   - an error occurred.
126  *   - not all fragments of the packet are collected yet.
127  */
128 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
129                 struct rte_ip_frag_death_row *dr,
130                 struct rte_mbuf *mb, uint64_t tms, struct rte_ipv6_hdr *ip_hdr,
131                 struct ipv6_extension_fragment *frag_hdr);
132
133 /**
134  * Return a pointer to the packet's fragment header, if found.
135  * It only looks at the extension header that's right after the fixed IPv6
136  * header, and doesn't follow the whole chain of extension headers.
137  *
138  * @param hdr
139  *   Pointer to the IPv6 header.
140  * @return
141  *   Pointer to the IPv6 fragment extension header, or NULL if it's not
142  *   present.
143  */
144 static inline struct ipv6_extension_fragment *
145 rte_ipv6_frag_get_ipv6_fragment_header(struct rte_ipv6_hdr *hdr)
146 {
147         if (hdr->proto == IPPROTO_FRAGMENT) {
148                 return (struct ipv6_extension_fragment *) ++hdr;
149         }
150         else
151                 return NULL;
152 }
153
154 /**
155  * IPv4 fragmentation.
156  *
157  * This function implements the fragmentation of IPv4 packets.
158  *
159  * @param pkt_in
160  *   The input packet.
161  * @param pkts_out
162  *   Array storing the output fragments.
163  * @param nb_pkts_out
164  *   Number of fragments.
165  * @param mtu_size
166  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
167  *   datagrams. This value includes the size of the IPv4 header.
168  * @param pool_direct
169  *   MBUF pool used for allocating direct buffers for the output fragments.
170  * @param pool_indirect
171  *   MBUF pool used for allocating indirect buffers for the output fragments.
172  * @return
173  *   Upon successful completion - number of output fragments placed
174  *   in the pkts_out array.
175  *   Otherwise - (-1) * errno.
176  */
177 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
178                         struct rte_mbuf **pkts_out,
179                         uint16_t nb_pkts_out, uint16_t mtu_size,
180                         struct rte_mempool *pool_direct,
181                         struct rte_mempool *pool_indirect);
182
183 /**
184  * This function implements reassembly of fragmented IPv4 packets.
185  * Incoming mbufs should have its l2_len/l3_len fields setup correctly.
186  *
187  * @param tbl
188  *   Table where to lookup/add the fragmented packet.
189  * @param dr
190  *   Death row to free buffers to
191  * @param mb
192  *   Incoming mbuf with IPv4 fragment.
193  * @param tms
194  *   Fragment arrival timestamp.
195  * @param ip_hdr
196  *   Pointer to the IPV4 header inside the fragment.
197  * @return
198  *   Pointer to mbuf for reassembled packet, or NULL if:
199  *   - an error occurred.
200  *   - not all fragments of the packet are collected yet.
201  */
202 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
203                 struct rte_ip_frag_death_row *dr,
204                 struct rte_mbuf *mb, uint64_t tms, struct rte_ipv4_hdr *ip_hdr);
205
206 /**
207  * Check if the IPv4 packet is fragmented
208  *
209  * @param hdr
210  *   IPv4 header of the packet
211  * @return
212  *   1 if fragmented, 0 if not fragmented
213  */
214 static inline int
215 rte_ipv4_frag_pkt_is_fragmented(const struct rte_ipv4_hdr *hdr)
216 {
217         uint16_t flag_offset, ip_flag, ip_ofs;
218
219         flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
220         ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
221         ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
222
223         return ip_flag != 0 || ip_ofs  != 0;
224 }
225
226 /**
227  * Free mbufs on a given death row.
228  *
229  * @param dr
230  *   Death row to free mbufs in.
231  * @param prefetch
232  *   How many buffers to prefetch before freeing.
233  */
234 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
235                 uint32_t prefetch);
236
237
238 /**
239  * Dump fragmentation table statistics to file.
240  *
241  * @param f
242  *   File to dump statistics to
243  * @param tbl
244  *   Fragmentation table to dump statistics from
245  */
246 void
247 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
248
249 /**
250  * Delete expired fragments
251  *
252  * @param tbl
253  *   Table to delete expired fragments from
254  * @param dr
255  *   Death row to free buffers to
256  * @param tms
257  *   Current timestamp
258  */
259 __rte_experimental
260 void
261 rte_frag_table_del_expired_entries(struct rte_ip_frag_tbl *tbl,
262         struct rte_ip_frag_death_row *dr, uint64_t tms);
263
264 #ifdef __cplusplus
265 }
266 #endif
267
268 #endif /* _RTE_IP_FRAG_H_ */