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. */
47 uint64_t src_dst[4]; /**< src address, first 8 bytes used for IPv4 */
48 uint32_t id; /**< dst address */
49 uint32_t key_len; /**< src/dst key length */
53 * @internal Fragmented packet to reassemble.
54 * First two entries in the frags[] array are for the last and first fragments.
57 TAILQ_ENTRY(ip_frag_pkt) lru; /**< LRU list */
58 struct ip_frag_key key; /**< fragmentation key */
59 uint64_t start; /**< creation timestamp */
60 uint32_t total_size; /**< expected reassembled size */
61 uint32_t frag_size; /**< size of fragments received */
62 uint32_t last_idx; /**< index of next entry to fill */
63 struct ip_frag frags[IP_MAX_FRAG_NUM]; /**< fragments */
64 } __rte_cache_aligned;
66 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
68 /** mbuf death row (packets to be freed) */
69 struct rte_ip_frag_death_row {
70 uint32_t cnt; /**< number of mbufs currently on death row */
71 struct rte_mbuf *row[IP_FRAG_DEATH_ROW_LEN * (IP_MAX_FRAG_NUM + 1)];
72 /**< mbufs to be freed */
75 TAILQ_HEAD(ip_pkt_list, ip_frag_pkt); /**< @internal fragments tailq */
77 /** fragmentation table statistics */
78 struct ip_frag_tbl_stat {
79 uint64_t find_num; /**< total # of find/insert attempts. */
80 uint64_t add_num; /**< # of add ops. */
81 uint64_t del_num; /**< # of del ops. */
82 uint64_t reuse_num; /**< # of reuse (del/add) ops. */
83 uint64_t fail_total; /**< total # of add failures. */
84 uint64_t fail_nospace; /**< # of 'no space' add failures. */
85 } __rte_cache_aligned;
87 /** fragmentation table */
88 struct rte_ip_frag_tbl {
89 uint64_t max_cycles; /**< ttl for table entries. */
90 uint32_t entry_mask; /**< hash value mask. */
91 uint32_t max_entries; /**< max entries allowed. */
92 uint32_t use_entries; /**< entries in use. */
93 uint32_t bucket_entries; /**< hash associativity. */
94 uint32_t nb_entries; /**< total size of the table. */
95 uint32_t nb_buckets; /**< num of associativity lines. */
96 struct ip_frag_pkt *last; /**< last used entry. */
97 struct ip_pkt_list lru; /**< LRU list for table entries. */
98 struct ip_frag_tbl_stat stat; /**< statistics counters. */
99 __extension__ struct ip_frag_pkt pkt[0]; /**< hash table. */
102 /** IPv6 fragment extension header */
103 #define RTE_IPV6_EHDR_MF_SHIFT 0
104 #define RTE_IPV6_EHDR_MF_MASK 1
105 #define RTE_IPV6_EHDR_FO_SHIFT 3
106 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
108 #define RTE_IPV6_FRAG_USED_MASK \
109 (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
111 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
112 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
114 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
115 (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
117 struct ipv6_extension_fragment {
118 uint8_t next_header; /**< Next header type */
119 uint8_t reserved; /**< Reserved */
120 uint16_t frag_data; /**< All fragmentation data */
121 uint32_t id; /**< Packet ID */
122 } __attribute__((__packed__));
127 * Create a new IP fragmentation table.
130 * Number of buckets in the hash table.
131 * @param bucket_entries
132 * Number of entries per bucket (e.g. hash associativity).
133 * Should be power of two.
135 * Maximum number of entries that could be stored in the table.
136 * The value should be less or equal then bucket_num * bucket_entries.
138 * Maximum TTL in cycles for each fragmented packet.
140 * The *socket_id* argument is the socket identifier in the case of
141 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
143 * The pointer to the new allocated fragmentation table, on success. NULL on error.
145 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
146 uint32_t bucket_entries, uint32_t max_entries,
147 uint64_t max_cycles, int socket_id);
150 * Free allocated IP fragmentation table.
153 * Fragmentation table to free.
156 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
159 * This function implements the fragmentation of IPv6 packets.
164 * Array storing the output fragments.
166 * Number of fragments.
168 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
169 * datagrams. This value includes the size of the IPv6 header.
171 * MBUF pool used for allocating direct buffers for the output fragments.
172 * @param pool_indirect
173 * MBUF pool used for allocating indirect buffers for the output fragments.
175 * Upon successful completion - number of output fragments placed
176 * in the pkts_out array.
177 * Otherwise - (-1) * errno.
180 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
181 struct rte_mbuf **pkts_out,
182 uint16_t nb_pkts_out,
184 struct rte_mempool *pool_direct,
185 struct rte_mempool *pool_indirect);
188 * This function implements reassembly of fragmented IPv6 packets.
189 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
192 * Table where to lookup/add the fragmented packet.
194 * Death row to free buffers to
196 * Incoming mbuf with IPv6 fragment.
198 * Fragment arrival timestamp.
200 * Pointer to the IPv6 header.
202 * Pointer to the IPv6 fragment extension header.
204 * Pointer to mbuf for reassembled packet, or NULL if:
205 * - an error occurred.
206 * - not all fragments of the packet are collected yet.
208 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
209 struct rte_ip_frag_death_row *dr,
210 struct rte_mbuf *mb, uint64_t tms, struct ipv6_hdr *ip_hdr,
211 struct ipv6_extension_fragment *frag_hdr);
214 * Return a pointer to the packet's fragment header, if found.
215 * It only looks at the extension header that's right after the fixed IPv6
216 * header, and doesn't follow the whole chain of extension headers.
219 * Pointer to the IPv6 header.
221 * Pointer to the IPv6 fragment extension header, or NULL if it's not
224 static inline struct ipv6_extension_fragment *
225 rte_ipv6_frag_get_ipv6_fragment_header(struct ipv6_hdr *hdr)
227 if (hdr->proto == IPPROTO_FRAGMENT) {
228 return (struct ipv6_extension_fragment *) ++hdr;
235 * IPv4 fragmentation.
237 * This function implements the fragmentation of IPv4 packets.
242 * Array storing the output fragments.
244 * Number of fragments.
246 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
247 * datagrams. This value includes the size of the IPv4 header.
249 * MBUF pool used for allocating direct buffers for the output fragments.
250 * @param pool_indirect
251 * MBUF pool used for allocating indirect buffers for the output fragments.
253 * Upon successful completion - number of output fragments placed
254 * in the pkts_out array.
255 * Otherwise - (-1) * errno.
257 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
258 struct rte_mbuf **pkts_out,
259 uint16_t nb_pkts_out, uint16_t mtu_size,
260 struct rte_mempool *pool_direct,
261 struct rte_mempool *pool_indirect);
264 * This function implements reassembly of fragmented IPv4 packets.
265 * Incoming mbufs should have its l2_len/l3_len fields setup correclty.
268 * Table where to lookup/add the fragmented packet.
270 * Death row to free buffers to
272 * Incoming mbuf with IPv4 fragment.
274 * Fragment arrival timestamp.
276 * Pointer to the IPV4 header inside the fragment.
278 * Pointer to mbuf for reassembled packet, or NULL if:
279 * - an error occurred.
280 * - not all fragments of the packet are collected yet.
282 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
283 struct rte_ip_frag_death_row *dr,
284 struct rte_mbuf *mb, uint64_t tms, struct ipv4_hdr *ip_hdr);
287 * Check if the IPv4 packet is fragmented
290 * IPv4 header of the packet
292 * 1 if fragmented, 0 if not fragmented
295 rte_ipv4_frag_pkt_is_fragmented(const struct ipv4_hdr * hdr) {
296 uint16_t flag_offset, ip_flag, ip_ofs;
298 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
299 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
300 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
302 return ip_flag != 0 || ip_ofs != 0;
306 * Free mbufs on a given death row.
309 * Death row to free mbufs in.
311 * How many buffers to prefetch before freeing.
313 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
318 * Dump fragmentation table statistics to file.
321 * File to dump statistics to
323 * Fragmentation table to dump statistics from
326 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
332 #endif /* _RTE_IP_FRAG_H_ */