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; /**< src address */
69 uint32_t id; /**< dst address */
73 * @internal Fragmented packet to reassemble.
74 * First two entries in the frags[] array are for the last and first fragments.
76 struct rte_ip_frag_pkt {
77 TAILQ_ENTRY(rte_ip_frag_pkt) lru; /**< LRU list */
78 struct ip_frag_key key; /**< fragmentation key */
79 uint64_t start; /**< creation timestamp */
80 uint32_t total_size; /**< expected reassembled size */
81 uint32_t frag_size; /**< size of fragments received */
82 uint32_t last_idx; /**< index of next entry to fill */
83 struct ip_frag frags[IP_MAX_FRAG_NUM]; /**< fragments */
84 } __rte_cache_aligned;
86 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
88 /** mbuf death row (packets to be freed) */
89 struct rte_ip_frag_death_row {
90 uint32_t cnt; /**< number of mbufs currently on death row */
91 struct rte_mbuf *row[IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1)];
92 /**< mbufs to be freed */
95 TAILQ_HEAD(rte_ip_pkt_list, rte_ip_frag_pkt); /**< @internal fragments tailq */
97 /** fragmentation table statistics */
98 struct rte_ip_frag_tbl_stat {
99 uint64_t find_num; /**< total # of find/insert attempts. */
100 uint64_t add_num; /**< # of add ops. */
101 uint64_t del_num; /**< # of del ops. */
102 uint64_t reuse_num; /**< # of reuse (del/add) ops. */
103 uint64_t fail_total; /**< total # of add failures. */
104 uint64_t fail_nospace; /**< # of 'no space' add failures. */
105 } __rte_cache_aligned;
107 /** fragmentation table */
108 struct rte_ip_frag_tbl {
109 uint64_t max_cycles; /**< ttl for table entries. */
110 uint32_t entry_mask; /**< hash value mask. */
111 uint32_t max_entries; /**< max entries allowed. */
112 uint32_t use_entries; /**< entries in use. */
113 uint32_t bucket_entries; /**< hash assocaitivity. */
114 uint32_t nb_entries; /**< total size of the table. */
115 uint32_t nb_buckets; /**< num of associativity lines. */
116 struct rte_ip_frag_pkt *last; /**< last used entry. */
117 struct rte_ip_pkt_list lru; /**< LRU list for table entries. */
118 struct rte_ip_frag_tbl_stat stat; /**< statistics counters. */
119 struct rte_ip_frag_pkt pkt[0]; /**< hash table. */
122 /** IPv6 fragment extension header */
123 struct ipv6_extension_fragment {
124 uint8_t next_header; /**< Next header type */
125 uint8_t reserved1; /**< Reserved */
128 uint16_t frag_offset:13; /**< Offset from the start of the packet */
129 uint16_t reserved2:2; /**< Reserved */
130 uint16_t more_frags:1;
131 /**< 1 if more fragments left, 0 if last fragment */
134 /**< union of all fragmentation data */
136 uint32_t id; /**< Packet ID */
137 } __attribute__((__packed__));
142 * Create a new IP fragmentation table.
145 * Number of buckets in the hash table.
146 * @param bucket_entries
147 * Number of entries per bucket (e.g. hash associativity).
148 * Should be power of two.
150 * Maximum number of entries that could be stored in the table.
151 * The value should be less or equal then bucket_num * bucket_entries.
153 * Maximum TTL in cycles for each fragmented packet.
155 * The *socket_id* argument is the socket identifier in the case of
156 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
158 * The pointer to the new allocated fragmentation table, on success. NULL on error.
160 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
161 uint32_t bucket_entries, uint32_t max_entries,
162 uint64_t max_cycles, int socket_id);
165 * Free allocated IP fragmentation table.
168 * Fragmentation table to free.
171 rte_ip_frag_table_destroy( struct rte_ip_frag_tbl *tbl)
177 * This function implements the fragmentation of IPv6 packets.
182 * Array storing the output fragments.
184 * Number of fragments.
186 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
187 * datagrams. This value includes the size of the IPv6 header.
189 * MBUF pool used for allocating direct buffers for the output fragments.
190 * @param pool_indirect
191 * MBUF pool used for allocating indirect buffers for the output fragments.
193 * Upon successful completion - number of output fragments placed
194 * in the pkts_out array.
195 * Otherwise - (-1) * errno.
198 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
199 struct rte_mbuf **pkts_out,
200 uint16_t nb_pkts_out,
202 struct rte_mempool *pool_direct,
203 struct rte_mempool *pool_indirect);
206 * IPv4 fragmentation.
208 * This function implements the fragmentation of IPv4 packets.
213 * Array storing the output fragments.
215 * Number of fragments.
217 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
218 * datagrams. This value includes the size of the IPv4 header.
220 * MBUF pool used for allocating direct buffers for the output fragments.
221 * @param pool_indirect
222 * MBUF pool used for allocating indirect buffers for the output fragments.
224 * Upon successful completion - number of output fragments placed
225 * in the pkts_out array.
226 * Otherwise - (-1) * errno.
228 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
229 struct rte_mbuf **pkts_out,
230 uint16_t nb_pkts_out, uint16_t mtu_size,
231 struct rte_mempool *pool_direct,
232 struct rte_mempool *pool_indirect);
235 * This function implements reassembly of fragmented IPv4 packets.
236 * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
239 * Table where to lookup/add the fragmented packet.
241 * Death row to free buffers to
243 * Incoming mbuf with IPv4 fragment.
245 * Fragment arrival timestamp.
247 * Pointer to the IPV4 header inside the fragment.
249 * Pointer to mbuf for reassebled packet, or NULL if:
250 * - an error occured.
251 * - not all fragments of the packet are collected yet.
253 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
254 struct rte_ip_frag_death_row *dr,
255 struct rte_mbuf *mb, uint64_t tms, struct ipv4_hdr *ip_hdr);
258 * Check if the IPv4 packet is fragmented
261 * IPv4 header of the packet
263 * 1 if fragmented, 0 if not fragmented
266 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
267 uint16_t flag_offset, ip_flag, ip_ofs;
269 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
270 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
271 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
273 return ip_flag != 0 || ip_ofs != 0;
277 * Free mbufs on a given death row.
280 * Death row to free mbufs in.
282 * How many buffers to prefetch before freeing.
284 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
289 * Dump fragmentation table statistics to file.
292 * File to dump statistics to
294 * Fragmentation table to dump statistics from
297 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
299 #endif /* _RTE_IP_FRAG_H_ */