4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _RTE_IP_FRAG_H_
35 #define _RTE_IP_FRAG_H_
39 * RTE IPv4 Fragmentation and Reassembly
41 * Implementation of IPv4 packet fragmentation and reassembly.
47 #include <rte_malloc.h>
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 */
59 /** @internal fragmented mbuf */
61 uint16_t ofs; /**< offset into the packet */
62 uint16_t len; /**< length of fragment */
63 struct rte_mbuf *mb; /**< fragment mbuf */
66 /** @internal <src addr, dst_addr, id> to uniquely indetify fragmented datagram. */
68 uint64_t src_dst[4]; /**< src address, first 8 bytes used for IPv4 */
69 uint32_t id; /**< dst address */
70 uint32_t key_len; /**< src/dst key length */
74 * @internal Fragmented packet to reassemble.
75 * First two entries in the frags[] array are for the last and first fragments.
77 struct rte_ip_frag_pkt {
78 TAILQ_ENTRY(rte_ip_frag_pkt) lru; /**< LRU list */
79 struct ip_frag_key key; /**< fragmentation key */
80 uint64_t start; /**< creation timestamp */
81 uint32_t total_size; /**< expected reassembled size */
82 uint32_t frag_size; /**< size of fragments received */
83 uint32_t last_idx; /**< index of next entry to fill */
84 struct ip_frag frags[IP_MAX_FRAG_NUM]; /**< fragments */
85 } __rte_cache_aligned;
87 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
89 /** mbuf death row (packets to be freed) */
90 struct rte_ip_frag_death_row {
91 uint32_t cnt; /**< number of mbufs currently on death row */
92 struct rte_mbuf *row[IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1)];
93 /**< mbufs to be freed */
96 TAILQ_HEAD(rte_ip_pkt_list, rte_ip_frag_pkt); /**< @internal fragments tailq */
98 /** fragmentation table statistics */
99 struct rte_ip_frag_tbl_stat {
100 uint64_t find_num; /**< total # of find/insert attempts. */
101 uint64_t add_num; /**< # of add ops. */
102 uint64_t del_num; /**< # of del ops. */
103 uint64_t reuse_num; /**< # of reuse (del/add) ops. */
104 uint64_t fail_total; /**< total # of add failures. */
105 uint64_t fail_nospace; /**< # of 'no space' add failures. */
106 } __rte_cache_aligned;
108 /** fragmentation table */
109 struct rte_ip_frag_tbl {
110 uint64_t max_cycles; /**< ttl for table entries. */
111 uint32_t entry_mask; /**< hash value mask. */
112 uint32_t max_entries; /**< max entries allowed. */
113 uint32_t use_entries; /**< entries in use. */
114 uint32_t bucket_entries; /**< hash assocaitivity. */
115 uint32_t nb_entries; /**< total size of the table. */
116 uint32_t nb_buckets; /**< num of associativity lines. */
117 struct rte_ip_frag_pkt *last; /**< last used entry. */
118 struct rte_ip_pkt_list lru; /**< LRU list for table entries. */
119 struct rte_ip_frag_tbl_stat stat; /**< statistics counters. */
120 struct rte_ip_frag_pkt pkt[0]; /**< hash table. */
123 /** IPv6 fragment extension header */
124 struct ipv6_extension_fragment {
125 uint8_t next_header; /**< Next header type */
126 uint8_t reserved1; /**< Reserved */
129 uint16_t frag_offset:13; /**< Offset from the start of the packet */
130 uint16_t reserved2:2; /**< Reserved */
131 uint16_t more_frags:1;
132 /**< 1 if more fragments left, 0 if last fragment */
135 /**< union of all fragmentation data */
137 uint32_t id; /**< Packet ID */
138 } __attribute__((__packed__));
143 * Create a new IP fragmentation table.
146 * Number of buckets in the hash table.
147 * @param bucket_entries
148 * Number of entries per bucket (e.g. hash associativity).
149 * Should be power of two.
151 * Maximum number of entries that could be stored in the table.
152 * The value should be less or equal then bucket_num * bucket_entries.
154 * Maximum TTL in cycles for each fragmented packet.
156 * The *socket_id* argument is the socket identifier in the case of
157 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
159 * The pointer to the new allocated fragmentation table, on success. NULL on error.
161 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
162 uint32_t bucket_entries, uint32_t max_entries,
163 uint64_t max_cycles, int socket_id);
166 * Free allocated IP fragmentation table.
169 * Fragmentation table to free.
172 rte_ip_frag_table_destroy( struct rte_ip_frag_tbl *tbl)
178 * This function implements the fragmentation of IPv6 packets.
183 * Array storing the output fragments.
185 * Number of fragments.
187 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
188 * datagrams. This value includes the size of the IPv6 header.
190 * MBUF pool used for allocating direct buffers for the output fragments.
191 * @param pool_indirect
192 * MBUF pool used for allocating indirect buffers for the output fragments.
194 * Upon successful completion - number of output fragments placed
195 * in the pkts_out array.
196 * Otherwise - (-1) * errno.
199 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
200 struct rte_mbuf **pkts_out,
201 uint16_t nb_pkts_out,
203 struct rte_mempool *pool_direct,
204 struct rte_mempool *pool_indirect);
208 * This function implements reassembly of fragmented IPv6 packets.
209 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
212 * Table where to lookup/add the fragmented packet.
214 * Death row to free buffers to
216 * Incoming mbuf with IPv6 fragment.
218 * Fragment arrival timestamp.
220 * Pointer to the IPv6 header.
222 * Pointer to the IPv6 fragment extension header.
224 * Pointer to mbuf for reassembled packet, or NULL if:
225 * - an error occured.
226 * - not all fragments of the packet are collected yet.
228 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
229 struct rte_ip_frag_death_row *dr,
230 struct rte_mbuf *mb, uint64_t tms, struct ipv6_hdr *ip_hdr,
231 struct ipv6_extension_fragment *frag_hdr);
234 * Return a pointer to the packet's fragment header, if found.
235 * It only looks at the extension header that's right after the fixed IPv6
236 * header, and doesn't follow the whole chain of extension headers.
239 * Pointer to the IPv6 header.
241 * Pointer to the IPv6 fragment extension header, or NULL if it's not
244 static inline struct ipv6_extension_fragment *
245 rte_ipv6_frag_get_ipv6_fragment_header(struct ipv6_hdr *hdr)
247 if (hdr->proto == IPPROTO_FRAGMENT) {
248 return (struct ipv6_extension_fragment *) ++hdr;
255 * IPv4 fragmentation.
257 * This function implements the fragmentation of IPv4 packets.
262 * Array storing the output fragments.
264 * Number of fragments.
266 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
267 * datagrams. This value includes the size of the IPv4 header.
269 * MBUF pool used for allocating direct buffers for the output fragments.
270 * @param pool_indirect
271 * MBUF pool used for allocating indirect buffers for the output fragments.
273 * Upon successful completion - number of output fragments placed
274 * in the pkts_out array.
275 * Otherwise - (-1) * errno.
277 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
278 struct rte_mbuf **pkts_out,
279 uint16_t nb_pkts_out, uint16_t mtu_size,
280 struct rte_mempool *pool_direct,
281 struct rte_mempool *pool_indirect);
284 * This function implements reassembly of fragmented IPv4 packets.
285 * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
288 * Table where to lookup/add the fragmented packet.
290 * Death row to free buffers to
292 * Incoming mbuf with IPv4 fragment.
294 * Fragment arrival timestamp.
296 * Pointer to the IPV4 header inside the fragment.
298 * Pointer to mbuf for reassebled packet, or NULL if:
299 * - an error occured.
300 * - not all fragments of the packet are collected yet.
302 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
303 struct rte_ip_frag_death_row *dr,
304 struct rte_mbuf *mb, uint64_t tms, struct ipv4_hdr *ip_hdr);
307 * Check if the IPv4 packet is fragmented
310 * IPv4 header of the packet
312 * 1 if fragmented, 0 if not fragmented
315 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
316 uint16_t flag_offset, ip_flag, ip_ofs;
318 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
319 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
320 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
322 return ip_flag != 0 || ip_ofs != 0;
326 * Free mbufs on a given death row.
329 * Death row to free mbufs in.
331 * How many buffers to prefetch before freeing.
333 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
338 * Dump fragmentation table statistics to file.
341 * File to dump statistics to
343 * Fragmentation table to dump statistics from
346 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
348 #endif /* _RTE_IP_FRAG_H_ */