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