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