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 IP Fragmentation and Reassembly
41 * Implementation of IP packet fragmentation and reassembly.
51 #include <rte_malloc.h>
53 #include <rte_memory.h>
55 #include <rte_byteorder.h>
58 IP_LAST_FRAG_IDX, /**< index of last fragment */
59 IP_FIRST_FRAG_IDX, /**< index of first fragment */
60 IP_MIN_FRAG_NUM, /**< minimum number of fragments */
61 IP_MAX_FRAG_NUM = RTE_LIBRTE_IP_FRAG_MAX_FRAG,
62 /**< maximum number of fragments per packet */
65 /** @internal fragmented mbuf */
67 uint16_t ofs; /**< offset into the packet */
68 uint16_t len; /**< length of fragment */
69 struct rte_mbuf *mb; /**< fragment mbuf */
72 /** @internal <src addr, dst_addr, id> to uniquely indetify fragmented datagram. */
74 uint64_t src_dst[4]; /**< src address, first 8 bytes used for IPv4 */
75 uint32_t id; /**< dst address */
76 uint32_t key_len; /**< src/dst key length */
80 * @internal Fragmented packet to reassemble.
81 * First two entries in the frags[] array are for the last and first fragments.
84 TAILQ_ENTRY(ip_frag_pkt) lru; /**< LRU list */
85 struct ip_frag_key key; /**< fragmentation key */
86 uint64_t start; /**< creation timestamp */
87 uint32_t total_size; /**< expected reassembled size */
88 uint32_t frag_size; /**< size of fragments received */
89 uint32_t last_idx; /**< index of next entry to fill */
90 struct ip_frag frags[IP_MAX_FRAG_NUM]; /**< fragments */
91 } __rte_cache_aligned;
93 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
95 /** mbuf death row (packets to be freed) */
96 struct rte_ip_frag_death_row {
97 uint32_t cnt; /**< number of mbufs currently on death row */
98 struct rte_mbuf *row[IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1)];
99 /**< mbufs to be freed */
102 TAILQ_HEAD(ip_pkt_list, ip_frag_pkt); /**< @internal fragments tailq */
104 /** fragmentation table statistics */
105 struct ip_frag_tbl_stat {
106 uint64_t find_num; /**< total # of find/insert attempts. */
107 uint64_t add_num; /**< # of add ops. */
108 uint64_t del_num; /**< # of del ops. */
109 uint64_t reuse_num; /**< # of reuse (del/add) ops. */
110 uint64_t fail_total; /**< total # of add failures. */
111 uint64_t fail_nospace; /**< # of 'no space' add failures. */
112 } __rte_cache_aligned;
114 /** fragmentation table */
115 struct rte_ip_frag_tbl {
116 uint64_t max_cycles; /**< ttl for table entries. */
117 uint32_t entry_mask; /**< hash value mask. */
118 uint32_t max_entries; /**< max entries allowed. */
119 uint32_t use_entries; /**< entries in use. */
120 uint32_t bucket_entries; /**< hash assocaitivity. */
121 uint32_t nb_entries; /**< total size of the table. */
122 uint32_t nb_buckets; /**< num of associativity lines. */
123 struct ip_frag_pkt *last; /**< last used entry. */
124 struct ip_pkt_list lru; /**< LRU list for table entries. */
125 struct ip_frag_tbl_stat stat; /**< statistics counters. */
126 struct ip_frag_pkt pkt[0]; /**< hash table. */
129 /** IPv6 fragment extension header */
130 struct ipv6_extension_fragment {
131 uint8_t next_header; /**< Next header type */
132 uint8_t reserved1; /**< Reserved */
135 uint16_t frag_offset:13; /**< Offset from the start of the packet */
136 uint16_t reserved2:2; /**< Reserved */
137 uint16_t more_frags:1;
138 /**< 1 if more fragments left, 0 if last fragment */
141 /**< union of all fragmentation data */
143 uint32_t id; /**< Packet ID */
144 } __attribute__((__packed__));
149 * Create a new IP fragmentation table.
152 * Number of buckets in the hash table.
153 * @param bucket_entries
154 * Number of entries per bucket (e.g. hash associativity).
155 * Should be power of two.
157 * Maximum number of entries that could be stored in the table.
158 * The value should be less or equal then bucket_num * bucket_entries.
160 * Maximum TTL in cycles for each fragmented packet.
162 * The *socket_id* argument is the socket identifier in the case of
163 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
165 * The pointer to the new allocated fragmentation table, on success. NULL on error.
167 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
168 uint32_t bucket_entries, uint32_t max_entries,
169 uint64_t max_cycles, int socket_id);
172 * Free allocated IP fragmentation table.
175 * Fragmentation table to free.
178 rte_ip_frag_table_destroy( struct rte_ip_frag_tbl *tbl)
184 * This function implements the fragmentation of IPv6 packets.
189 * Array storing the output fragments.
191 * Number of fragments.
193 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
194 * datagrams. This value includes the size of the IPv6 header.
196 * MBUF pool used for allocating direct buffers for the output fragments.
197 * @param pool_indirect
198 * MBUF pool used for allocating indirect buffers for the output fragments.
200 * Upon successful completion - number of output fragments placed
201 * in the pkts_out array.
202 * Otherwise - (-1) * errno.
205 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
206 struct rte_mbuf **pkts_out,
207 uint16_t nb_pkts_out,
209 struct rte_mempool *pool_direct,
210 struct rte_mempool *pool_indirect);
213 * This function implements reassembly of fragmented IPv6 packets.
214 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
217 * Table where to lookup/add the fragmented packet.
219 * Death row to free buffers to
221 * Incoming mbuf with IPv6 fragment.
223 * Fragment arrival timestamp.
225 * Pointer to the IPv6 header.
227 * Pointer to the IPv6 fragment extension header.
229 * Pointer to mbuf for reassembled packet, or NULL if:
230 * - an error occured.
231 * - not all fragments of the packet are collected yet.
233 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
234 struct rte_ip_frag_death_row *dr,
235 struct rte_mbuf *mb, uint64_t tms, struct ipv6_hdr *ip_hdr,
236 struct ipv6_extension_fragment *frag_hdr);
239 * Return a pointer to the packet's fragment header, if found.
240 * It only looks at the extension header that's right after the fixed IPv6
241 * header, and doesn't follow the whole chain of extension headers.
244 * Pointer to the IPv6 header.
246 * Pointer to the IPv6 fragment extension header, or NULL if it's not
249 static inline struct ipv6_extension_fragment *
250 rte_ipv6_frag_get_ipv6_fragment_header(struct ipv6_hdr *hdr)
252 if (hdr->proto == IPPROTO_FRAGMENT) {
253 return (struct ipv6_extension_fragment *) ++hdr;
260 * IPv4 fragmentation.
262 * This function implements the fragmentation of IPv4 packets.
267 * Array storing the output fragments.
269 * Number of fragments.
271 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
272 * datagrams. This value includes the size of the IPv4 header.
274 * MBUF pool used for allocating direct buffers for the output fragments.
275 * @param pool_indirect
276 * MBUF pool used for allocating indirect buffers for the output fragments.
278 * Upon successful completion - number of output fragments placed
279 * in the pkts_out array.
280 * Otherwise - (-1) * errno.
282 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
283 struct rte_mbuf **pkts_out,
284 uint16_t nb_pkts_out, uint16_t mtu_size,
285 struct rte_mempool *pool_direct,
286 struct rte_mempool *pool_indirect);
289 * This function implements reassembly of fragmented IPv4 packets.
290 * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
293 * Table where to lookup/add the fragmented packet.
295 * Death row to free buffers to
297 * Incoming mbuf with IPv4 fragment.
299 * Fragment arrival timestamp.
301 * Pointer to the IPV4 header inside the fragment.
303 * Pointer to mbuf for reassebled packet, or NULL if:
304 * - an error occured.
305 * - not all fragments of the packet are collected yet.
307 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
308 struct rte_ip_frag_death_row *dr,
309 struct rte_mbuf *mb, uint64_t tms, struct ipv4_hdr *ip_hdr);
312 * Check if the IPv4 packet is fragmented
315 * IPv4 header of the packet
317 * 1 if fragmented, 0 if not fragmented
320 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
321 uint16_t flag_offset, ip_flag, ip_ofs;
323 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
324 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
325 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
327 return ip_flag != 0 || ip_ofs != 0;
331 * Free mbufs on a given death row.
334 * Death row to free mbufs in.
336 * How many buffers to prefetch before freeing.
338 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
343 * Dump fragmentation table statistics to file.
346 * File to dump statistics to
348 * Fragmentation table to dump statistics from
351 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
357 #endif /* _RTE_IP_FRAG_H_ */