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