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