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>
30 #define IP_FRAG_DEATH_ROW_LEN 32 /**< death row size (in packets) */
32 /* death row size in mbufs */
33 #define IP_FRAG_DEATH_ROW_MBUF_LEN \
34 (IP_FRAG_DEATH_ROW_LEN * (RTE_LIBRTE_IP_FRAG_MAX_FRAG + 1))
36 /** mbuf death row (packets to be freed) */
37 struct rte_ip_frag_death_row {
38 uint32_t cnt; /**< number of mbufs currently on death row */
39 struct rte_mbuf *row[IP_FRAG_DEATH_ROW_MBUF_LEN];
40 /**< mbufs to be freed */
43 /* struct ipv6_extension_fragment moved to librte_net/rte_ip.h and renamed. */
44 #define ipv6_extension_fragment rte_ipv6_fragment_ext
47 * Create a new IP fragmentation table.
50 * Number of buckets in the hash table.
51 * @param bucket_entries
52 * Number of entries per bucket (e.g. hash associativity).
53 * Should be power of two.
55 * Maximum number of entries that could be stored in the table.
56 * The value should be less or equal then bucket_num * bucket_entries.
58 * Maximum TTL in cycles for each fragmented packet.
60 * The *socket_id* argument is the socket identifier in the case of
61 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA constraints.
63 * The pointer to the new allocated fragmentation table, on success. NULL on error.
65 struct rte_ip_frag_tbl * rte_ip_frag_table_create(uint32_t bucket_num,
66 uint32_t bucket_entries, uint32_t max_entries,
67 uint64_t max_cycles, int socket_id);
70 * Free allocated IP fragmentation table.
73 * Fragmentation table to free.
76 rte_ip_frag_table_destroy(struct rte_ip_frag_tbl *tbl);
79 * This function implements the fragmentation of IPv6 packets.
84 * Array storing the output fragments.
86 * Number of fragments.
88 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
89 * datagrams. This value includes the size of the IPv6 header.
91 * MBUF pool used for allocating direct buffers for the output fragments.
92 * @param pool_indirect
93 * MBUF pool used for allocating indirect buffers for the output fragments.
95 * Upon successful completion - number of output fragments placed
96 * in the pkts_out array.
97 * Otherwise - (-1) * errno.
100 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
101 struct rte_mbuf **pkts_out,
102 uint16_t nb_pkts_out,
104 struct rte_mempool *pool_direct,
105 struct rte_mempool *pool_indirect);
108 * This function implements reassembly of fragmented IPv6 packets.
109 * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
112 * Table where to lookup/add the fragmented packet.
114 * Death row to free buffers to
116 * Incoming mbuf with IPv6 fragment.
118 * Fragment arrival timestamp.
120 * Pointer to the IPv6 header.
122 * Pointer to the IPv6 fragment extension header.
124 * Pointer to mbuf for reassembled packet, or NULL if:
125 * - an error occurred.
126 * - not all fragments of the packet are collected yet.
128 struct rte_mbuf *rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
129 struct rte_ip_frag_death_row *dr,
130 struct rte_mbuf *mb, uint64_t tms, struct rte_ipv6_hdr *ip_hdr,
131 struct ipv6_extension_fragment *frag_hdr);
134 * Return a pointer to the packet's fragment header, if found.
135 * It only looks at the extension header that's right after the fixed IPv6
136 * header, and doesn't follow the whole chain of extension headers.
139 * Pointer to the IPv6 header.
141 * Pointer to the IPv6 fragment extension header, or NULL if it's not
144 static inline struct ipv6_extension_fragment *
145 rte_ipv6_frag_get_ipv6_fragment_header(struct rte_ipv6_hdr *hdr)
147 if (hdr->proto == IPPROTO_FRAGMENT) {
148 return (struct ipv6_extension_fragment *) ++hdr;
155 * IPv4 fragmentation.
157 * This function implements the fragmentation of IPv4 packets.
162 * Array storing the output fragments.
164 * Number of fragments.
166 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
167 * datagrams. This value includes the size of the IPv4 header.
169 * MBUF pool used for allocating direct buffers for the output fragments.
170 * @param pool_indirect
171 * MBUF pool used for allocating indirect buffers for the output fragments.
173 * Upon successful completion - number of output fragments placed
174 * in the pkts_out array.
175 * Otherwise - (-1) * errno.
177 int32_t rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
178 struct rte_mbuf **pkts_out,
179 uint16_t nb_pkts_out, uint16_t mtu_size,
180 struct rte_mempool *pool_direct,
181 struct rte_mempool *pool_indirect);
184 * This function implements reassembly of fragmented IPv4 packets.
185 * Incoming mbufs should have its l2_len/l3_len fields setup correctly.
188 * Table where to lookup/add the fragmented packet.
190 * Death row to free buffers to
192 * Incoming mbuf with IPv4 fragment.
194 * Fragment arrival timestamp.
196 * Pointer to the IPV4 header inside the fragment.
198 * Pointer to mbuf for reassembled packet, or NULL if:
199 * - an error occurred.
200 * - not all fragments of the packet are collected yet.
202 struct rte_mbuf * rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
203 struct rte_ip_frag_death_row *dr,
204 struct rte_mbuf *mb, uint64_t tms, struct rte_ipv4_hdr *ip_hdr);
207 * Check if the IPv4 packet is fragmented
210 * IPv4 header of the packet
212 * 1 if fragmented, 0 if not fragmented
215 rte_ipv4_frag_pkt_is_fragmented(const struct rte_ipv4_hdr *hdr)
217 uint16_t flag_offset, ip_flag, ip_ofs;
219 flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
220 ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
221 ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
223 return ip_flag != 0 || ip_ofs != 0;
227 * Free mbufs on a given death row.
230 * Death row to free mbufs in.
232 * How many buffers to prefetch before freeing.
234 void rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
239 * Dump fragmentation table statistics to file.
242 * File to dump statistics to
244 * Fragmentation table to dump statistics from
247 rte_ip_frag_table_statistics_dump(FILE * f, const struct rte_ip_frag_tbl *tbl);
250 * Delete expired fragments
253 * Table to delete expired fragments from
255 * Death row to free buffers to
261 rte_frag_table_del_expired_entries(struct rte_ip_frag_tbl *tbl,
262 struct rte_ip_frag_death_row *dr, uint64_t tms);
268 #endif /* _RTE_IP_FRAG_H_ */