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