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