1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef _RTE_IP_FRAG_H_
6 #define _RTE_IP_FRAG_H_
10 * RTE IP Fragmentation and Reassembly
12 * Implementation of IP packet fragmentation and reassembly.
22 #include <rte_config.h>
23 #include <rte_malloc.h>
24 #include <rte_memory.h>
26 #include <rte_byteorder.h>
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 */
38 /** @internal fragmented mbuf */
40 uint16_t ofs; /**< offset into the packet */
41 uint16_t len; /**< length of fragment */
42 struct rte_mbuf *mb; /**< fragment mbuf */
45 /** @internal <src addr, dst_addr, id> to uniquely identify fragmented datagram. */
48 /**< src and dst address, only first 8 bytes used for IPv4 */
51 uint64_t id_key_len; /**< combined for easy fetch */
54 uint32_t id; /**< packet id */
55 uint32_t key_len; /**< src/dst key length */
61 * @internal Fragmented packet to reassemble.
62 * First two entries in the frags[] array are for the last and first fragments.
65 TAILQ_ENTRY(ip_frag_pkt) lru; /**< LRU list */
66 struct ip_frag_key key; /**< fragmentation key */
67 uint64_t start; /**< creation timestamp */
68 uint32_t total_size; /**< expected reassembled size */
69 uint32_t frag_size; /**< size of fragments received */
70 uint32_t last_idx; /**< index of next entry to fill */
71 struct ip_frag frags[IP_MAX_FRAG_NUM]; /**< fragments */
72 } __rte_cache_aligned;
74 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
76 /* death row size in mbufs */
77 #define IP_FRAG_DEATH_ROW_MBUF_LEN (IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1))
79 /** mbuf death row (packets to be freed) */
80 struct rte_ip_frag_death_row {
81 uint32_t cnt; /**< number of mbufs currently on death row */
82 struct rte_mbuf *row[IP_FRAG_DEATH_ROW_MBUF_LEN];
83 /**< mbufs to be freed */
86 TAILQ_HEAD(ip_pkt_list, ip_frag_pkt); /**< @internal fragments tailq */
88 /** fragmentation table statistics */
89 struct ip_frag_tbl_stat {
90 uint64_t find_num; /**< total # of find/insert attempts. */
91 uint64_t add_num; /**< # of add ops. */
92 uint64_t del_num; /**< # of del ops. */
93 uint64_t reuse_num; /**< # of reuse (del/add) ops. */
94 uint64_t fail_total; /**< total # of add failures. */
95 uint64_t fail_nospace; /**< # of 'no space' add failures. */
96 } __rte_cache_aligned;
98 /** fragmentation table */
99 struct rte_ip_frag_tbl {
100 uint64_t max_cycles; /**< ttl for table entries. */
101 uint32_t entry_mask; /**< hash value mask. */
102 uint32_t max_entries; /**< max entries allowed. */
103 uint32_t use_entries; /**< entries in use. */
104 uint32_t bucket_entries; /**< hash associativity. */
105 uint32_t nb_entries; /**< total size of the table. */
106 uint32_t nb_buckets; /**< num of associativity lines. */
107 struct ip_frag_pkt *last; /**< last used entry. */
108 struct ip_pkt_list lru; /**< LRU list for table entries. */
109 struct ip_frag_tbl_stat stat; /**< statistics counters. */
110 __extension__ struct ip_frag_pkt pkt[0]; /**< hash table. */
113 /** IPv6 fragment extension header */
114 #define RTE_IPV6_EHDR_MF_SHIFT 0
115 #define RTE_IPV6_EHDR_MF_MASK 1
116 #define RTE_IPV6_EHDR_FO_SHIFT 3
117 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
119 #define RTE_IPV6_FRAG_USED_MASK \
120 (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
122 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
123 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
125 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
126 (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
128 struct ipv6_extension_fragment {
129 uint8_t next_header; /**< Next header type */
130 uint8_t reserved; /**< Reserved */
131 uint16_t frag_data; /**< All fragmentation data */
132 uint32_t id; /**< Packet ID */
133 } __attribute__((__packed__));
138 * Create a new IP fragmentation table.
141 * Number of buckets in the hash table.
142 * @param bucket_entries
143 * Number of entries per bucket (e.g. hash associativity).
144 * Should be power of two.
146 * Maximum number of entries that could be stored in the table.
147 * The value should be less or equal then bucket_num * bucket_entries.
149 * Maximum TTL in cycles for each fragmented packet.
151 * The *socket_id* argument is the socket identifier in the case of
152 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
154 * The pointer to the new allocated fragmentation table, on success. NULL on error.
156 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
157 uint32_t bucket_entries, uint32_t max_entries,
158 uint64_t max_cycles, int socket_id);
161 * Free allocated IP fragmentation table.
164 * Fragmentation table to free.
167 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
170 * This function implements the fragmentation of IPv6 packets.
175 * Array storing the output fragments.
177 * Number of fragments.
179 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
180 * datagrams. This value includes the size of the IPv6 header.
182 * MBUF pool used for allocating direct buffers for the output fragments.
183 * @param pool_indirect
184 * MBUF pool used for allocating indirect buffers for the output fragments.
186 * Upon successful completion - number of output fragments placed
187 * in the pkts_out array.
188 * Otherwise - (-1) * errno.
191 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
192 struct rte_mbuf **pkts_out,
193 uint16_t nb_pkts_out,
195 struct rte_mempool *pool_direct,
196 struct rte_mempool *pool_indirect);
199 * This function implements reassembly of fragmented IPv6 packets.
200 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
203 * Table where to lookup/add the fragmented packet.
205 * Death row to free buffers to
207 * Incoming mbuf with IPv6 fragment.
209 * Fragment arrival timestamp.
211 * Pointer to the IPv6 header.
213 * Pointer to the IPv6 fragment extension header.
215 * Pointer to mbuf for reassembled packet, or NULL if:
216 * - an error occurred.
217 * - not all fragments of the packet are collected yet.
219 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
220 struct rte_ip_frag_death_row *dr,
221 struct rte_mbuf *mb, uint64_t tms, struct ipv6_hdr *ip_hdr,
222 struct ipv6_extension_fragment *frag_hdr);
225 * Return a pointer to the packet's fragment header, if found.
226 * It only looks at the extension header that's right after the fixed IPv6
227 * header, and doesn't follow the whole chain of extension headers.
230 * Pointer to the IPv6 header.
232 * Pointer to the IPv6 fragment extension header, or NULL if it's not
235 static inline struct ipv6_extension_fragment *
236 rte_ipv6_frag_get_ipv6_fragment_header(struct ipv6_hdr *hdr)
238 if (hdr->proto == IPPROTO_FRAGMENT) {
239 return (struct ipv6_extension_fragment *) ++hdr;
246 * IPv4 fragmentation.
248 * This function implements the fragmentation of IPv4 packets.
253 * Array storing the output fragments.
255 * Number of fragments.
257 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
258 * datagrams. This value includes the size of the IPv4 header.
260 * MBUF pool used for allocating direct buffers for the output fragments.
261 * @param pool_indirect
262 * MBUF pool used for allocating indirect buffers for the output fragments.
264 * Upon successful completion - number of output fragments placed
265 * in the pkts_out array.
266 * Otherwise - (-1) * errno.
268 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
269 struct rte_mbuf **pkts_out,
270 uint16_t nb_pkts_out, uint16_t mtu_size,
271 struct rte_mempool *pool_direct,
272 struct rte_mempool *pool_indirect);
275 * This function implements reassembly of fragmented IPv4 packets.
276 * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
279 * Table where to lookup/add the fragmented packet.
281 * Death row to free buffers to
283 * Incoming mbuf with IPv4 fragment.
285 * Fragment arrival timestamp.
287 * Pointer to the IPV4 header inside the fragment.
289 * Pointer to mbuf for reassembled packet, or NULL if:
290 * - an error occurred.
291 * - not all fragments of the packet are collected yet.
293 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
294 struct rte_ip_frag_death_row *dr,
295 struct rte_mbuf *mb, uint64_t tms, struct ipv4_hdr *ip_hdr);
298 * Check if the IPv4 packet is fragmented
301 * IPv4 header of the packet
303 * 1 if fragmented, 0 if not fragmented
306 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
307 uint16_t flag_offset, ip_flag, ip_ofs;
309 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
310 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
311 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
313 return ip_flag != 0 || ip_ofs != 0;
317 * Free mbufs on a given death row.
320 * Death row to free mbufs in.
322 * How many buffers to prefetch before freeing.
324 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
329 * Dump fragmentation table statistics to file.
332 * File to dump statistics to
334 * Fragmentation table to dump statistics from
337 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
340 * Delete expired fragments
343 * Table to delete expired fragments from
345 * Death row to free buffers to
349 void __rte_experimental
350 rte_frag_table_del_expired_entries(struct rte_ip_frag_tbl *tbl,
351 struct rte_ip_frag_death_row *dr, uint64_t tms);
357 #endif /* _RTE_IP_FRAG_H_ */