net: add rte prefix to IP defines
[dpdk.git] / lib / librte_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 enum {
31         IP_LAST_FRAG_IDX,    /**< index of last fragment */
32         IP_FIRST_FRAG_IDX,   /**< index of first fragment */
33         IP_MIN_FRAG_NUM,     /**< minimum number of fragments */
34         IP_MAX_FRAG_NUM = RTE_LIBRTE_IP_FRAG_MAX_FRAG,
35         /**< maximum number of fragments per packet */
36 };
37
38 /** @internal fragmented mbuf */
39 struct ip_frag {
40         uint16_t ofs;          /**< offset into the packet */
41         uint16_t len;          /**< length of fragment */
42         struct rte_mbuf *mb;   /**< fragment mbuf */
43 };
44
45 /** @internal <src addr, dst_addr, id> to uniquely identify fragmented datagram. */
46 struct ip_frag_key {
47         uint64_t src_dst[4];
48         /**< src and dst address, only first 8 bytes used for IPv4 */
49         RTE_STD_C11
50         union {
51                 uint64_t id_key_len; /**< combined for easy fetch */
52                 __extension__
53                 struct {
54                         uint32_t id;       /**< packet id */
55                         uint32_t key_len;  /**< src/dst key length */
56                 };
57         };
58 };
59
60 /**
61  * @internal Fragmented packet to reassemble.
62  * First two entries in the frags[] array are for the last and first fragments.
63  */
64 struct ip_frag_pkt {
65         TAILQ_ENTRY(ip_frag_pkt) lru;   /**< LRU list */
66         struct ip_frag_key key;           /**< fragmentation key */
67         uint64_t             start;       /**< creation timestamp */
68         uint32_t             total_size;  /**< expected reassembled size */
69         uint32_t             frag_size;   /**< size of fragments received */
70         uint32_t             last_idx;    /**< index of next entry to fill */
71         struct ip_frag       frags[IP_MAX_FRAG_NUM]; /**< fragments */
72 } __rte_cache_aligned;
73
74 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
75
76 /* death row size in mbufs */
77 #define IP_FRAG_DEATH_ROW_MBUF_LEN (IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1))
78
79 /** mbuf death row (packets to be freed) */
80 struct rte_ip_frag_death_row {
81         uint32_t cnt;          /**< number of mbufs currently on death row */
82         struct rte_mbuf *row[IP_FRAG_DEATH_ROW_MBUF_LEN];
83         /**< mbufs to be freed */
84 };
85
86 TAILQ_HEAD(ip_pkt_list, ip_frag_pkt); /**< @internal fragments tailq */
87
88 /** fragmentation table statistics */
89 struct ip_frag_tbl_stat {
90         uint64_t find_num;      /**< total # of find/insert attempts. */
91         uint64_t add_num;       /**< # of add ops. */
92         uint64_t del_num;       /**< # of del ops. */
93         uint64_t reuse_num;     /**< # of reuse (del/add) ops. */
94         uint64_t fail_total;    /**< total # of add failures. */
95         uint64_t fail_nospace;  /**< # of 'no space' add failures. */
96 } __rte_cache_aligned;
97
98 /** fragmentation table */
99 struct rte_ip_frag_tbl {
100         uint64_t             max_cycles;      /**< ttl for table entries. */
101         uint32_t             entry_mask;      /**< hash value mask. */
102         uint32_t             max_entries;     /**< max entries allowed. */
103         uint32_t             use_entries;     /**< entries in use. */
104         uint32_t             bucket_entries;  /**< hash associativity. */
105         uint32_t             nb_entries;      /**< total size of the table. */
106         uint32_t             nb_buckets;      /**< num of associativity lines. */
107         struct ip_frag_pkt *last;         /**< last used entry. */
108         struct ip_pkt_list lru;           /**< LRU list for table entries. */
109         struct ip_frag_tbl_stat stat;     /**< statistics counters. */
110         __extension__ struct ip_frag_pkt pkt[0]; /**< hash table. */
111 };
112
113 /** IPv6 fragment extension header */
114 #define RTE_IPV6_EHDR_MF_SHIFT                  0
115 #define RTE_IPV6_EHDR_MF_MASK                   1
116 #define RTE_IPV6_EHDR_FO_SHIFT                  3
117 #define RTE_IPV6_EHDR_FO_MASK                   (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
118 #define RTE_IPV6_EHDR_FO_ALIGN                  (1 << RTE_IPV6_EHDR_FO_SHIFT)
119
120 #define RTE_IPV6_FRAG_USED_MASK                 \
121         (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
122
123 #define RTE_IPV6_GET_MF(x)                              ((x) & RTE_IPV6_EHDR_MF_MASK)
124 #define RTE_IPV6_GET_FO(x)                              ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
125
126 #define RTE_IPV6_SET_FRAG_DATA(fo, mf)  \
127         (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
128
129 struct ipv6_extension_fragment {
130         uint8_t next_header;            /**< Next header type */
131         uint8_t reserved;               /**< Reserved */
132         uint16_t frag_data;             /**< All fragmentation data */
133         uint32_t id;                    /**< Packet ID */
134 } __attribute__((__packed__));
135
136
137
138 /**
139  * Create a new IP fragmentation table.
140  *
141  * @param bucket_num
142  *   Number of buckets in the hash table.
143  * @param bucket_entries
144  *   Number of entries per bucket (e.g. hash associativity).
145  *   Should be power of two.
146  * @param max_entries
147  *   Maximum number of entries that could be stored in the table.
148  *   The value should be less or equal then bucket_num * bucket_entries.
149  * @param max_cycles
150  *   Maximum TTL in cycles for each fragmented packet.
151  * @param socket_id
152  *   The *socket_id* argument is the socket identifier in the case of
153  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
154  * @return
155  *   The pointer to the new allocated fragmentation table, on success. NULL on error.
156  */
157 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
158                 uint32_t bucket_entries,  uint32_t max_entries,
159                 uint64_t max_cycles, int socket_id);
160
161 /**
162  * Free allocated IP fragmentation table.
163  *
164  * @param tbl
165  *   Fragmentation table to free.
166  */
167 void
168 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
169
170 /**
171  * This function implements the fragmentation of IPv6 packets.
172  *
173  * @param pkt_in
174  *   The input packet.
175  * @param pkts_out
176  *   Array storing the output fragments.
177  * @param nb_pkts_out
178  *   Number of fragments.
179  * @param mtu_size
180  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
181  *   datagrams. This value includes the size of the IPv6 header.
182  * @param pool_direct
183  *   MBUF pool used for allocating direct buffers for the output fragments.
184  * @param pool_indirect
185  *   MBUF pool used for allocating indirect buffers for the output fragments.
186  * @return
187  *   Upon successful completion - number of output fragments placed
188  *   in the pkts_out array.
189  *   Otherwise - (-1) * errno.
190  */
191 int32_t
192 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
193                 struct rte_mbuf **pkts_out,
194                 uint16_t nb_pkts_out,
195                 uint16_t mtu_size,
196                 struct rte_mempool *pool_direct,
197                 struct rte_mempool *pool_indirect);
198
199 /**
200  * This function implements reassembly of fragmented IPv6 packets.
201  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
202  *
203  * @param tbl
204  *   Table where to lookup/add the fragmented packet.
205  * @param dr
206  *   Death row to free buffers to
207  * @param mb
208  *   Incoming mbuf with IPv6 fragment.
209  * @param tms
210  *   Fragment arrival timestamp.
211  * @param ip_hdr
212  *   Pointer to the IPv6 header.
213  * @param frag_hdr
214  *   Pointer to the IPv6 fragment extension header.
215  * @return
216  *   Pointer to mbuf for reassembled packet, or NULL if:
217  *   - an error occurred.
218  *   - not all fragments of the packet are collected yet.
219  */
220 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
221                 struct rte_ip_frag_death_row *dr,
222                 struct rte_mbuf *mb, uint64_t tms, struct rte_ipv6_hdr *ip_hdr,
223                 struct ipv6_extension_fragment *frag_hdr);
224
225 /**
226  * Return a pointer to the packet's fragment header, if found.
227  * It only looks at the extension header that's right after the fixed IPv6
228  * header, and doesn't follow the whole chain of extension headers.
229  *
230  * @param hdr
231  *   Pointer to the IPv6 header.
232  * @return
233  *   Pointer to the IPv6 fragment extension header, or NULL if it's not
234  *   present.
235  */
236 static inline struct ipv6_extension_fragment *
237 rte_ipv6_frag_get_ipv6_fragment_header(struct rte_ipv6_hdr *hdr)
238 {
239         if (hdr->proto == IPPROTO_FRAGMENT) {
240                 return (struct ipv6_extension_fragment *) ++hdr;
241         }
242         else
243                 return NULL;
244 }
245
246 /**
247  * IPv4 fragmentation.
248  *
249  * This function implements the fragmentation of IPv4 packets.
250  *
251  * @param pkt_in
252  *   The input packet.
253  * @param pkts_out
254  *   Array storing the output fragments.
255  * @param nb_pkts_out
256  *   Number of fragments.
257  * @param mtu_size
258  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
259  *   datagrams. This value includes the size of the IPv4 header.
260  * @param pool_direct
261  *   MBUF pool used for allocating direct buffers for the output fragments.
262  * @param pool_indirect
263  *   MBUF pool used for allocating indirect buffers for the output fragments.
264  * @return
265  *   Upon successful completion - number of output fragments placed
266  *   in the pkts_out array.
267  *   Otherwise - (-1) * errno.
268  */
269 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
270                         struct rte_mbuf **pkts_out,
271                         uint16_t nb_pkts_out, uint16_t mtu_size,
272                         struct rte_mempool *pool_direct,
273                         struct rte_mempool *pool_indirect);
274
275 /**
276  * This function implements reassembly of fragmented IPv4 packets.
277  * Incoming mbufs should have its l2_len/l3_len fields setup correctly.
278  *
279  * @param tbl
280  *   Table where to lookup/add the fragmented packet.
281  * @param dr
282  *   Death row to free buffers to
283  * @param mb
284  *   Incoming mbuf with IPv4 fragment.
285  * @param tms
286  *   Fragment arrival timestamp.
287  * @param ip_hdr
288  *   Pointer to the IPV4 header inside the fragment.
289  * @return
290  *   Pointer to mbuf for reassembled packet, or NULL if:
291  *   - an error occurred.
292  *   - not all fragments of the packet are collected yet.
293  */
294 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
295                 struct rte_ip_frag_death_row *dr,
296                 struct rte_mbuf *mb, uint64_t tms, struct rte_ipv4_hdr *ip_hdr);
297
298 /**
299  * Check if the IPv4 packet is fragmented
300  *
301  * @param hdr
302  *   IPv4 header of the packet
303  * @return
304  *   1 if fragmented, 0 if not fragmented
305  */
306 static inline int
307 rte_ipv4_frag_pkt_is_fragmented(const struct rte_ipv4_hdr *hdr)
308 {
309         uint16_t flag_offset, ip_flag, ip_ofs;
310
311         flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
312         ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
313         ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
314
315         return ip_flag != 0 || ip_ofs  != 0;
316 }
317
318 /**
319  * Free mbufs on a given death row.
320  *
321  * @param dr
322  *   Death row to free mbufs in.
323  * @param prefetch
324  *   How many buffers to prefetch before freeing.
325  */
326 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
327                 uint32_t prefetch);
328
329
330 /**
331  * Dump fragmentation table statistics to file.
332  *
333  * @param f
334  *   File to dump statistics to
335  * @param tbl
336  *   Fragmentation table to dump statistics from
337  */
338 void
339 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
340
341 /**
342  * Delete expired fragments
343  *
344  * @param tbl
345  *   Table to delete expired fragments from
346  * @param dr
347  *   Death row to free buffers to
348  * @param tms
349  *   Current timestamp
350  */
351 void __rte_experimental
352 rte_frag_table_del_expired_entries(struct rte_ip_frag_tbl *tbl,
353         struct rte_ip_frag_death_row *dr, uint64_t tms);
354
355 #ifdef __cplusplus
356 }
357 #endif
358
359 #endif /* _RTE_IP_FRAG_H_ */