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