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_malloc.h>
23 #include <rte_memory.h>
25 #include <rte_byteorder.h>
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 */
37 /** @internal fragmented mbuf */
39 uint16_t ofs; /**< offset into the packet */
40 uint16_t len; /**< length of fragment */
41 struct rte_mbuf *mb; /**< fragment mbuf */
44 /** @internal <src addr, dst_addr, id> to uniquely identify fragmented datagram. */
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 */
52 * @internal Fragmented packet to reassemble.
53 * First two entries in the frags[] array are for the last and first fragments.
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;
65 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
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 */
74 TAILQ_HEAD(ip_pkt_list, ip_frag_pkt); /**< @internal fragments tailq */
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;
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. */
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))
107 #define RTE_IPV6_FRAG_USED_MASK \
108 (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
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)
113 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
114 (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
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__));
126 * Create a new IP fragmentation table.
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.
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.
137 * Maximum TTL in cycles for each fragmented packet.
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.
142 * The pointer to the new allocated fragmentation table, on success. NULL on error.
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);
149 * Free allocated IP fragmentation table.
152 * Fragmentation table to free.
155 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
158 * This function implements the fragmentation of IPv6 packets.
163 * Array storing the output fragments.
165 * Number of fragments.
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.
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.
174 * Upon successful completion - number of output fragments placed
175 * in the pkts_out array.
176 * Otherwise - (-1) * errno.
179 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
180 struct rte_mbuf **pkts_out,
181 uint16_t nb_pkts_out,
183 struct rte_mempool *pool_direct,
184 struct rte_mempool *pool_indirect);
187 * This function implements reassembly of fragmented IPv6 packets.
188 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
191 * Table where to lookup/add the fragmented packet.
193 * Death row to free buffers to
195 * Incoming mbuf with IPv6 fragment.
197 * Fragment arrival timestamp.
199 * Pointer to the IPv6 header.
201 * Pointer to the IPv6 fragment extension header.
203 * Pointer to mbuf for reassembled packet, or NULL if:
204 * - an error occurred.
205 * - not all fragments of the packet are collected yet.
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);
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.
218 * Pointer to the IPv6 header.
220 * Pointer to the IPv6 fragment extension header, or NULL if it's not
223 static inline struct ipv6_extension_fragment *
224 rte_ipv6_frag_get_ipv6_fragment_header(struct ipv6_hdr *hdr)
226 if (hdr->proto == IPPROTO_FRAGMENT) {
227 return (struct ipv6_extension_fragment *) ++hdr;
234 * IPv4 fragmentation.
236 * This function implements the fragmentation of IPv4 packets.
241 * Array storing the output fragments.
243 * Number of fragments.
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.
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.
252 * Upon successful completion - number of output fragments placed
253 * in the pkts_out array.
254 * Otherwise - (-1) * errno.
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);
263 * This function implements reassembly of fragmented IPv4 packets.
264 * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
267 * Table where to lookup/add the fragmented packet.
269 * Death row to free buffers to
271 * Incoming mbuf with IPv4 fragment.
273 * Fragment arrival timestamp.
275 * Pointer to the IPV4 header inside the fragment.
277 * Pointer to mbuf for reassembled packet, or NULL if:
278 * - an error occurred.
279 * - not all fragments of the packet are collected yet.
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);
286 * Check if the IPv4 packet is fragmented
289 * IPv4 header of the packet
291 * 1 if fragmented, 0 if not fragmented
294 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
295 uint16_t flag_offset, ip_flag, ip_ofs;
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);
301 return ip_flag != 0 || ip_ofs != 0;
305 * Free mbufs on a given death row.
308 * Death row to free mbufs in.
310 * How many buffers to prefetch before freeing.
312 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
317 * Dump fragmentation table statistics to file.
320 * File to dump statistics to
322 * Fragmentation table to dump statistics from
325 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
331 #endif /* _RTE_IP_FRAG_H_ */