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