1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_hash_crc.h>
10 #include "ip_frag_common.h"
12 #define PRIME_VALUE 0xeaad8405
14 #define IP_FRAG_TBL_POS(tbl, sig) \
15 ((tbl)->pkt + ((sig) & (tbl)->entry_mask))
18 ip_frag_tbl_add(struct rte_ip_frag_tbl *tbl, struct ip_frag_pkt *fp,
19 const struct ip_frag_key *key, uint64_t tms)
22 ip_frag_reset(fp, tms);
23 TAILQ_INSERT_TAIL(&tbl->lru, fp, lru);
25 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, add_num, 1);
29 ip_frag_tbl_reuse(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
30 struct ip_frag_pkt *fp, uint64_t tms)
33 ip_frag_reset(fp, tms);
34 TAILQ_REMOVE(&tbl->lru, fp, lru);
35 TAILQ_INSERT_TAIL(&tbl->lru, fp, lru);
36 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, reuse_num, 1);
41 ipv4_frag_hash(const struct ip_frag_key *key, uint32_t *v1, uint32_t *v2)
46 p = (const uint32_t *)&key->src_dst;
49 v = rte_hash_crc_4byte(p[0], PRIME_VALUE);
50 v = rte_hash_crc_4byte(p[1], v);
51 v = rte_hash_crc_4byte(key->id, v);
54 v = rte_jhash_3words(p[0], p[1], key->id, PRIME_VALUE);
55 #endif /* RTE_ARCH_X86 */
58 *v2 = (v << 7) + (v >> 14);
62 ipv6_frag_hash(const struct ip_frag_key *key, uint32_t *v1, uint32_t *v2)
67 p = (const uint32_t *) &key->src_dst;
70 v = rte_hash_crc_4byte(p[0], PRIME_VALUE);
71 v = rte_hash_crc_4byte(p[1], v);
72 v = rte_hash_crc_4byte(p[2], v);
73 v = rte_hash_crc_4byte(p[3], v);
74 v = rte_hash_crc_4byte(p[4], v);
75 v = rte_hash_crc_4byte(p[5], v);
76 v = rte_hash_crc_4byte(p[6], v);
77 v = rte_hash_crc_4byte(p[7], v);
78 v = rte_hash_crc_4byte(key->id, v);
81 v = rte_jhash_3words(p[0], p[1], p[2], PRIME_VALUE);
82 v = rte_jhash_3words(p[3], p[4], p[5], v);
83 v = rte_jhash_3words(p[6], p[7], key->id, v);
84 #endif /* RTE_ARCH_X86 */
87 *v2 = (v << 7) + (v >> 14);
91 ip_frag_process(struct ip_frag_pkt *fp, struct rte_ip_frag_death_row *dr,
92 struct rte_mbuf *mb, uint16_t ofs, uint16_t len, uint16_t more_frags)
98 /* this is the first fragment. */
100 idx = (fp->frags[IP_FIRST_FRAG_IDX].mb == NULL) ?
101 IP_FIRST_FRAG_IDX : UINT32_MAX;
103 /* this is the last fragment. */
104 } else if (more_frags == 0) {
105 fp->total_size = ofs + len;
106 idx = (fp->frags[IP_LAST_FRAG_IDX].mb == NULL) ?
107 IP_LAST_FRAG_IDX : UINT32_MAX;
109 /* this is the intermediate fragment. */
110 } else if ((idx = fp->last_idx) <
111 sizeof (fp->frags) / sizeof (fp->frags[0])) {
116 * erroneous packet: either exceed max allowed number of fragments,
117 * or duplicate first/last fragment encountered.
119 if (idx >= sizeof (fp->frags) / sizeof (fp->frags[0])) {
121 /* report an error. */
122 if (fp->key.key_len == IPV4_KEYLEN)
123 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
124 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, "
125 "total_size: %u, frag_size: %u, last_idx: %u\n"
126 "first fragment: ofs: %u, len: %u\n"
127 "last fragment: ofs: %u, len: %u\n\n",
129 fp, fp->key.src_dst[0], fp->key.id,
130 fp->total_size, fp->frag_size, fp->last_idx,
131 fp->frags[IP_FIRST_FRAG_IDX].ofs,
132 fp->frags[IP_FIRST_FRAG_IDX].len,
133 fp->frags[IP_LAST_FRAG_IDX].ofs,
134 fp->frags[IP_LAST_FRAG_IDX].len);
136 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
137 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
138 "total_size: %u, frag_size: %u, last_idx: %u\n"
139 "first fragment: ofs: %u, len: %u\n"
140 "last fragment: ofs: %u, len: %u\n\n",
142 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id,
143 fp->total_size, fp->frag_size, fp->last_idx,
144 fp->frags[IP_FIRST_FRAG_IDX].ofs,
145 fp->frags[IP_FIRST_FRAG_IDX].len,
146 fp->frags[IP_LAST_FRAG_IDX].ofs,
147 fp->frags[IP_LAST_FRAG_IDX].len);
149 /* free all fragments, invalidate the entry. */
150 ip_frag_free(fp, dr);
151 ip_frag_key_invalidate(&fp->key);
152 IP_FRAG_MBUF2DR(dr, mb);
157 fp->frags[idx].ofs = ofs;
158 fp->frags[idx].len = len;
159 fp->frags[idx].mb = mb;
163 /* not all fragments are collected yet. */
164 if (likely (fp->frag_size < fp->total_size)) {
167 /* if we collected all fragments, then try to reassemble. */
168 } else if (fp->frag_size == fp->total_size &&
169 fp->frags[IP_FIRST_FRAG_IDX].mb != NULL) {
170 if (fp->key.key_len == IPV4_KEYLEN)
171 mb = ipv4_frag_reassemble(fp);
173 mb = ipv6_frag_reassemble(fp);
176 /* errorenous set of fragments. */
179 /* report an error. */
180 if (fp->key.key_len == IPV4_KEYLEN)
181 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
182 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, "
183 "total_size: %u, frag_size: %u, last_idx: %u\n"
184 "first fragment: ofs: %u, len: %u\n"
185 "last fragment: ofs: %u, len: %u\n\n",
187 fp, fp->key.src_dst[0], fp->key.id,
188 fp->total_size, fp->frag_size, fp->last_idx,
189 fp->frags[IP_FIRST_FRAG_IDX].ofs,
190 fp->frags[IP_FIRST_FRAG_IDX].len,
191 fp->frags[IP_LAST_FRAG_IDX].ofs,
192 fp->frags[IP_LAST_FRAG_IDX].len);
194 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
195 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
196 "total_size: %u, frag_size: %u, last_idx: %u\n"
197 "first fragment: ofs: %u, len: %u\n"
198 "last fragment: ofs: %u, len: %u\n\n",
200 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id,
201 fp->total_size, fp->frag_size, fp->last_idx,
202 fp->frags[IP_FIRST_FRAG_IDX].ofs,
203 fp->frags[IP_FIRST_FRAG_IDX].len,
204 fp->frags[IP_LAST_FRAG_IDX].ofs,
205 fp->frags[IP_LAST_FRAG_IDX].len);
207 /* free associated resources. */
208 ip_frag_free(fp, dr);
211 /* we are done with that entry, invalidate it. */
212 ip_frag_key_invalidate(&fp->key);
218 * Find an entry in the table for the corresponding fragment.
219 * If such entry is not present, then allocate a new one.
220 * If the entry is stale, then free and reuse it.
223 ip_frag_find(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
224 const struct ip_frag_key *key, uint64_t tms)
226 struct ip_frag_pkt *pkt, *free, *stale, *lru;
230 * Actually the two line below are totally redundant.
231 * they are here, just to make gcc 4.6 happy.
235 max_cycles = tbl->max_cycles;
237 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, find_num, 1);
239 if ((pkt = ip_frag_lookup(tbl, key, tms, &free, &stale)) == NULL) {
241 /*timed-out entry, free and invalidate it*/
243 ip_frag_tbl_del(tbl, dr, stale);
247 * we found a free entry, check if we can use it.
248 * If we run out of free entries in the table, then
249 * check if we have a timed out entry to delete.
251 } else if (free != NULL &&
252 tbl->max_entries <= tbl->use_entries) {
253 lru = TAILQ_FIRST(&tbl->lru);
254 if (max_cycles + lru->start < tms) {
255 ip_frag_tbl_del(tbl, dr, lru);
258 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat,
263 /* found a free entry to reuse. */
265 ip_frag_tbl_add(tbl, free, key, tms);
270 * we found the flow, but it is already timed out,
271 * so free associated resources, reposition it in the LRU list,
274 } else if (max_cycles + pkt->start < tms) {
275 ip_frag_tbl_reuse(tbl, dr, pkt, tms);
278 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, fail_total, (pkt == NULL));
285 ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
286 const struct ip_frag_key *key, uint64_t tms,
287 struct ip_frag_pkt **free, struct ip_frag_pkt **stale)
289 struct ip_frag_pkt *p1, *p2;
290 struct ip_frag_pkt *empty, *old;
292 uint32_t i, assoc, sig1, sig2;
297 max_cycles = tbl->max_cycles;
298 assoc = tbl->bucket_entries;
300 if (tbl->last != NULL && ip_frag_key_cmp(key, &tbl->last->key) == 0)
303 /* different hashing methods for IPv4 and IPv6 */
304 if (key->key_len == IPV4_KEYLEN)
305 ipv4_frag_hash(key, &sig1, &sig2);
307 ipv6_frag_hash(key, &sig1, &sig2);
309 p1 = IP_FRAG_TBL_POS(tbl, sig1);
310 p2 = IP_FRAG_TBL_POS(tbl, sig2);
312 for (i = 0; i != assoc; i++) {
313 if (p1->key.key_len == IPV4_KEYLEN)
314 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
315 "tbl: %p, max_entries: %u, use_entries: %u\n"
316 "ipv4_frag_pkt line0: %p, index: %u from %u\n"
317 "key: <%" PRIx64 ", %#x>, start: %" PRIu64 "\n",
319 tbl, tbl->max_entries, tbl->use_entries,
321 p1[i].key.src_dst[0], p1[i].key.id, p1[i].start);
323 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
324 "tbl: %p, max_entries: %u, use_entries: %u\n"
325 "ipv6_frag_pkt line0: %p, index: %u from %u\n"
326 "key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64 "\n",
328 tbl, tbl->max_entries, tbl->use_entries,
330 IPv6_KEY_BYTES(p1[i].key.src_dst), p1[i].key.id, p1[i].start);
332 if (ip_frag_key_cmp(key, &p1[i].key) == 0)
334 else if (ip_frag_key_is_empty(&p1[i].key))
335 empty = (empty == NULL) ? (p1 + i) : empty;
336 else if (max_cycles + p1[i].start < tms)
337 old = (old == NULL) ? (p1 + i) : old;
339 if (p2->key.key_len == IPV4_KEYLEN)
340 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
341 "tbl: %p, max_entries: %u, use_entries: %u\n"
342 "ipv4_frag_pkt line1: %p, index: %u from %u\n"
343 "key: <%" PRIx64 ", %#x>, start: %" PRIu64 "\n",
345 tbl, tbl->max_entries, tbl->use_entries,
347 p2[i].key.src_dst[0], p2[i].key.id, p2[i].start);
349 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
350 "tbl: %p, max_entries: %u, use_entries: %u\n"
351 "ipv6_frag_pkt line1: %p, index: %u from %u\n"
352 "key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64 "\n",
354 tbl, tbl->max_entries, tbl->use_entries,
356 IPv6_KEY_BYTES(p2[i].key.src_dst), p2[i].key.id, p2[i].start);
358 if (ip_frag_key_cmp(key, &p2[i].key) == 0)
360 else if (ip_frag_key_is_empty(&p2[i].key))
361 empty = (empty == NULL) ?( p2 + i) : empty;
362 else if (max_cycles + p2[i].start < tms)
363 old = (old == NULL) ? (p2 + i) : old;