ip_frag: remove inclusion of mbuf header
[dpdk.git] / lib / librte_ip_frag / rte_ip_frag.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_IP_FRAG_H_
35 #define _RTE_IP_FRAG_H_
36
37 /**
38  * @file
39  * RTE IP Fragmentation and Reassembly
40  *
41  * Implementation of IP packet fragmentation and reassembly.
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <stdint.h>
49 #include <stdio.h>
50
51 #include <rte_malloc.h>
52 #include <rte_memory.h>
53 #include <rte_ip.h>
54 #include <rte_byteorder.h>
55
56 struct rte_mbuf;
57
58 enum {
59         IP_LAST_FRAG_IDX,    /**< index of last fragment */
60         IP_FIRST_FRAG_IDX,   /**< index of first fragment */
61         IP_MIN_FRAG_NUM,     /**< minimum number of fragments */
62         IP_MAX_FRAG_NUM = RTE_LIBRTE_IP_FRAG_MAX_FRAG,
63         /**< maximum number of fragments per packet */
64 };
65
66 /** @internal fragmented mbuf */
67 struct ip_frag {
68         uint16_t ofs;          /**< offset into the packet */
69         uint16_t len;          /**< length of fragment */
70         struct rte_mbuf *mb;   /**< fragment mbuf */
71 };
72
73 /** @internal <src addr, dst_addr, id> to uniquely indetify fragmented datagram. */
74 struct ip_frag_key {
75         uint64_t src_dst[4];      /**< src address, first 8 bytes used for IPv4 */
76         uint32_t id;           /**< dst address */
77         uint32_t key_len;      /**< src/dst key length */
78 };
79
80 /*
81  * @internal Fragmented packet to reassemble.
82  * First two entries in the frags[] array are for the last and first fragments.
83  */
84 struct ip_frag_pkt {
85         TAILQ_ENTRY(ip_frag_pkt) lru;   /**< LRU list */
86         struct ip_frag_key key;           /**< fragmentation key */
87         uint64_t             start;       /**< creation timestamp */
88         uint32_t             total_size;  /**< expected reassembled size */
89         uint32_t             frag_size;   /**< size of fragments received */
90         uint32_t             last_idx;    /**< index of next entry to fill */
91         struct ip_frag       frags[IP_MAX_FRAG_NUM]; /**< fragments */
92 } __rte_cache_aligned;
93
94 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
95
96 /** mbuf death row (packets to be freed) */
97 struct rte_ip_frag_death_row {
98         uint32_t cnt;          /**< number of mbufs currently on death row */
99         struct rte_mbuf *row[IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1)];
100         /**< mbufs to be freed */
101 };
102
103 TAILQ_HEAD(ip_pkt_list, ip_frag_pkt); /**< @internal fragments tailq */
104
105 /** fragmentation table statistics */
106 struct ip_frag_tbl_stat {
107         uint64_t find_num;      /**< total # of find/insert attempts. */
108         uint64_t add_num;       /**< # of add ops. */
109         uint64_t del_num;       /**< # of del ops. */
110         uint64_t reuse_num;     /**< # of reuse (del/add) ops. */
111         uint64_t fail_total;    /**< total # of add failures. */
112         uint64_t fail_nospace;  /**< # of 'no space' add failures. */
113 } __rte_cache_aligned;
114
115 /** fragmentation table */
116 struct rte_ip_frag_tbl {
117         uint64_t             max_cycles;      /**< ttl for table entries. */
118         uint32_t             entry_mask;      /**< hash value mask. */
119         uint32_t             max_entries;     /**< max entries allowed. */
120         uint32_t             use_entries;     /**< entries in use. */
121         uint32_t             bucket_entries;  /**< hash assocaitivity. */
122         uint32_t             nb_entries;      /**< total size of the table. */
123         uint32_t             nb_buckets;      /**< num of associativity lines. */
124         struct ip_frag_pkt *last;         /**< last used entry. */
125         struct ip_pkt_list lru;           /**< LRU list for table entries. */
126         struct ip_frag_tbl_stat stat;     /**< statistics counters. */
127         struct ip_frag_pkt pkt[0];        /**< hash table. */
128 };
129
130 /** IPv6 fragment extension header */
131 struct ipv6_extension_fragment {
132         uint8_t next_header;            /**< Next header type */
133         uint8_t reserved1;              /**< Reserved */
134         union {
135                 struct {
136                         uint16_t frag_offset:13; /**< Offset from the start of the packet */
137                         uint16_t reserved2:2; /**< Reserved */
138                         uint16_t more_frags:1;
139                         /**< 1 if more fragments left, 0 if last fragment */
140                 };
141                 uint16_t frag_data;
142                 /**< union of all fragmentation data */
143         };
144         uint32_t id;                    /**< Packet ID */
145 } __attribute__((__packed__));
146
147
148
149 /*
150  * Create a new IP fragmentation table.
151  *
152  * @param bucket_num
153  *   Number of buckets in the hash table.
154  * @param bucket_entries
155  *   Number of entries per bucket (e.g. hash associativity).
156  *   Should be power of two.
157  * @param max_entries
158  *   Maximum number of entries that could be stored in the table.
159  *   The value should be less or equal then bucket_num * bucket_entries.
160  * @param max_cycles
161  *   Maximum TTL in cycles for each fragmented packet.
162  * @param socket_id
163  *   The *socket_id* argument is the socket identifier in the case of
164  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
165  * @return
166  *   The pointer to the new allocated fragmentation table, on success. NULL on error.
167  */
168 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
169                 uint32_t bucket_entries,  uint32_t max_entries,
170                 uint64_t max_cycles, int socket_id);
171
172 /*
173  * Free allocated IP fragmentation table.
174  *
175  * @param btl
176  *   Fragmentation table to free.
177  */
178 static inline void
179 rte_ip_frag_table_destroy( struct rte_ip_frag_tbl *tbl)
180 {
181         rte_free(tbl);
182 }
183
184 /**
185  * This function implements the fragmentation of IPv6 packets.
186  *
187  * @param pkt_in
188  *   The input packet.
189  * @param pkts_out
190  *   Array storing the output fragments.
191  * @param nb_pkts_out
192  *   Number of fragments.
193  * @param mtu_size
194  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
195  *   datagrams. This value includes the size of the IPv6 header.
196  * @param pool_direct
197  *   MBUF pool used for allocating direct buffers for the output fragments.
198  * @param pool_indirect
199  *   MBUF pool used for allocating indirect buffers for the output fragments.
200  * @return
201  *   Upon successful completion - number of output fragments placed
202  *   in the pkts_out array.
203  *   Otherwise - (-1) * errno.
204  */
205 int32_t
206 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
207                 struct rte_mbuf **pkts_out,
208                 uint16_t nb_pkts_out,
209                 uint16_t mtu_size,
210                 struct rte_mempool *pool_direct,
211                 struct rte_mempool *pool_indirect);
212
213 /*
214  * This function implements reassembly of fragmented IPv6 packets.
215  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
216  *
217  * @param tbl
218  *   Table where to lookup/add the fragmented packet.
219  * @param dr
220  *   Death row to free buffers to
221  * @param mb
222  *   Incoming mbuf with IPv6 fragment.
223  * @param tms
224  *   Fragment arrival timestamp.
225  * @param ip_hdr
226  *   Pointer to the IPv6 header.
227  * @param frag_hdr
228  *   Pointer to the IPv6 fragment extension header.
229  * @return
230  *   Pointer to mbuf for reassembled packet, or NULL if:
231  *   - an error occured.
232  *   - not all fragments of the packet are collected yet.
233  */
234 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
235                 struct rte_ip_frag_death_row *dr,
236                 struct rte_mbuf *mb, uint64_t tms, struct ipv6_hdr *ip_hdr,
237                 struct ipv6_extension_fragment *frag_hdr);
238
239 /*
240  * Return a pointer to the packet's fragment header, if found.
241  * It only looks at the extension header that's right after the fixed IPv6
242  * header, and doesn't follow the whole chain of extension headers.
243  *
244  * @param hdr
245  *   Pointer to the IPv6 header.
246  * @return
247  *   Pointer to the IPv6 fragment extension header, or NULL if it's not
248  *   present.
249  */
250 static inline struct ipv6_extension_fragment *
251 rte_ipv6_frag_get_ipv6_fragment_header(struct ipv6_hdr *hdr)
252 {
253         if (hdr->proto == IPPROTO_FRAGMENT) {
254                 return (struct ipv6_extension_fragment *) ++hdr;
255         }
256         else
257                 return NULL;
258 }
259
260 /**
261  * IPv4 fragmentation.
262  *
263  * This function implements the fragmentation of IPv4 packets.
264  *
265  * @param pkt_in
266  *   The input packet.
267  * @param pkts_out
268  *   Array storing the output fragments.
269  * @param nb_pkts_out
270  *   Number of fragments.
271  * @param mtu_size
272  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
273  *   datagrams. This value includes the size of the IPv4 header.
274  * @param pool_direct
275  *   MBUF pool used for allocating direct buffers for the output fragments.
276  * @param pool_indirect
277  *   MBUF pool used for allocating indirect buffers for the output fragments.
278  * @return
279  *   Upon successful completion - number of output fragments placed
280  *   in the pkts_out array.
281  *   Otherwise - (-1) * errno.
282  */
283 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
284                         struct rte_mbuf **pkts_out,
285                         uint16_t nb_pkts_out, uint16_t mtu_size,
286                         struct rte_mempool *pool_direct,
287                         struct rte_mempool *pool_indirect);
288
289 /*
290  * This function implements reassembly of fragmented IPv4 packets.
291  * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
292  *
293  * @param tbl
294  *   Table where to lookup/add the fragmented packet.
295  * @param dr
296  *   Death row to free buffers to
297  * @param mb
298  *   Incoming mbuf with IPv4 fragment.
299  * @param tms
300  *   Fragment arrival timestamp.
301  * @param ip_hdr
302  *   Pointer to the IPV4 header inside the fragment.
303  * @return
304  *   Pointer to mbuf for reassebled packet, or NULL if:
305  *   - an error occured.
306  *   - not all fragments of the packet are collected yet.
307  */
308 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
309                 struct rte_ip_frag_death_row *dr,
310                 struct rte_mbuf *mb, uint64_t tms, struct ipv4_hdr *ip_hdr);
311
312 /*
313  * Check if the IPv4 packet is fragmented
314  *
315  * @param hdr
316  *   IPv4 header of the packet
317  * @return
318  *   1 if fragmented, 0 if not fragmented
319  */
320 static inline int
321 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
322         uint16_t flag_offset, ip_flag, ip_ofs;
323
324         flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
325         ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
326         ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
327
328         return ip_flag != 0 || ip_ofs  != 0;
329 }
330
331 /*
332  * Free mbufs on a given death row.
333  *
334  * @param dr
335  *   Death row to free mbufs in.
336  * @param prefetch
337  *   How many buffers to prefetch before freeing.
338  */
339 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
340                 uint32_t prefetch);
341
342
343 /*
344  * Dump fragmentation table statistics to file.
345  *
346  * @param f
347  *   File to dump statistics to
348  * @param tbl
349  *   Fragmentation table to dump statistics from
350  */
351 void
352 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
353
354 #ifdef __cplusplus
355 }
356 #endif
357
358 #endif /* _RTE_IP_FRAG_H_ */