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