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) < RTE_DIM(fp->frags)) {
115 * erroneous packet: either exceed max allowed number of fragments,
116 * or duplicate first/last fragment encountered.
118 if (idx >= RTE_DIM(fp->frags)) {
120 /* report an error. */
121 if (fp->key.key_len == IPV4_KEYLEN)
122 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
123 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, "
124 "total_size: %u, frag_size: %u, last_idx: %u\n"
125 "first fragment: ofs: %u, len: %u\n"
126 "last fragment: ofs: %u, len: %u\n\n",
128 fp, fp->key.src_dst[0], fp->key.id,
129 fp->total_size, fp->frag_size, fp->last_idx,
130 fp->frags[IP_FIRST_FRAG_IDX].ofs,
131 fp->frags[IP_FIRST_FRAG_IDX].len,
132 fp->frags[IP_LAST_FRAG_IDX].ofs,
133 fp->frags[IP_LAST_FRAG_IDX].len);
135 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
136 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
137 "total_size: %u, frag_size: %u, last_idx: %u\n"
138 "first fragment: ofs: %u, len: %u\n"
139 "last fragment: ofs: %u, len: %u\n\n",
141 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id,
142 fp->total_size, fp->frag_size, fp->last_idx,
143 fp->frags[IP_FIRST_FRAG_IDX].ofs,
144 fp->frags[IP_FIRST_FRAG_IDX].len,
145 fp->frags[IP_LAST_FRAG_IDX].ofs,
146 fp->frags[IP_LAST_FRAG_IDX].len);
148 /* free all fragments, invalidate the entry. */
149 ip_frag_free(fp, dr);
150 ip_frag_key_invalidate(&fp->key);
151 IP_FRAG_MBUF2DR(dr, mb);
156 fp->frags[idx].ofs = ofs;
157 fp->frags[idx].len = len;
158 fp->frags[idx].mb = mb;
162 /* not all fragments are collected yet. */
163 if (likely (fp->frag_size < fp->total_size)) {
166 /* if we collected all fragments, then try to reassemble. */
167 } else if (fp->frag_size == fp->total_size &&
168 fp->frags[IP_FIRST_FRAG_IDX].mb != NULL) {
169 if (fp->key.key_len == IPV4_KEYLEN)
170 mb = ipv4_frag_reassemble(fp);
172 mb = ipv6_frag_reassemble(fp);
175 /* errorenous set of fragments. */
178 /* report an error. */
179 if (fp->key.key_len == IPV4_KEYLEN)
180 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
181 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, "
182 "total_size: %u, frag_size: %u, last_idx: %u\n"
183 "first fragment: ofs: %u, len: %u\n"
184 "last fragment: ofs: %u, len: %u\n\n",
186 fp, fp->key.src_dst[0], fp->key.id,
187 fp->total_size, fp->frag_size, fp->last_idx,
188 fp->frags[IP_FIRST_FRAG_IDX].ofs,
189 fp->frags[IP_FIRST_FRAG_IDX].len,
190 fp->frags[IP_LAST_FRAG_IDX].ofs,
191 fp->frags[IP_LAST_FRAG_IDX].len);
193 IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
194 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
195 "total_size: %u, frag_size: %u, last_idx: %u\n"
196 "first fragment: ofs: %u, len: %u\n"
197 "last fragment: ofs: %u, len: %u\n\n",
199 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id,
200 fp->total_size, fp->frag_size, fp->last_idx,
201 fp->frags[IP_FIRST_FRAG_IDX].ofs,
202 fp->frags[IP_FIRST_FRAG_IDX].len,
203 fp->frags[IP_LAST_FRAG_IDX].ofs,
204 fp->frags[IP_LAST_FRAG_IDX].len);
206 /* free associated resources. */
207 ip_frag_free(fp, dr);
210 /* we are done with that entry, invalidate it. */
211 ip_frag_key_invalidate(&fp->key);
217 * Find an entry in the table for the corresponding fragment.
218 * If such entry is not present, then allocate a new one.
219 * If the entry is stale, then free and reuse it.
222 ip_frag_find(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
223 const struct ip_frag_key *key, uint64_t tms)
225 struct ip_frag_pkt *pkt, *free, *stale, *lru;
229 * Actually the two line below are totally redundant.
230 * they are here, just to make gcc 4.6 happy.
234 max_cycles = tbl->max_cycles;
236 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, find_num, 1);
238 if ((pkt = ip_frag_lookup(tbl, key, tms, &free, &stale)) == NULL) {
240 /*timed-out entry, free and invalidate it*/
242 ip_frag_tbl_del(tbl, dr, stale);
246 * we found a free entry, check if we can use it.
247 * If we run out of free entries in the table, then
248 * check if we have a timed out entry to delete.
250 } else if (free != NULL &&
251 tbl->max_entries <= tbl->use_entries) {
252 lru = TAILQ_FIRST(&tbl->lru);
253 if (max_cycles + lru->start < tms) {
254 ip_frag_tbl_del(tbl, dr, lru);
257 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat,
262 /* found a free entry to reuse. */
264 ip_frag_tbl_add(tbl, free, key, tms);
269 * we found the flow, but it is already timed out,
270 * so free associated resources, reposition it in the LRU list,
273 } else if (max_cycles + pkt->start < tms) {
274 ip_frag_tbl_reuse(tbl, dr, pkt, tms);
277 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, fail_total, (pkt == NULL));
284 ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
285 const struct ip_frag_key *key, uint64_t tms,
286 struct ip_frag_pkt **free, struct ip_frag_pkt **stale)
288 struct ip_frag_pkt *p1, *p2;
289 struct ip_frag_pkt *empty, *old;
291 uint32_t i, assoc, sig1, sig2;
296 max_cycles = tbl->max_cycles;
297 assoc = tbl->bucket_entries;
299 if (tbl->last != NULL && ip_frag_key_cmp(key, &tbl->last->key) == 0)
302 /* different hashing methods for IPv4 and IPv6 */
303 if (key->key_len == IPV4_KEYLEN)
304 ipv4_frag_hash(key, &sig1, &sig2);
306 ipv6_frag_hash(key, &sig1, &sig2);
308 p1 = IP_FRAG_TBL_POS(tbl, sig1);
309 p2 = IP_FRAG_TBL_POS(tbl, sig2);
311 for (i = 0; i != assoc; i++) {
312 if (p1->key.key_len == IPV4_KEYLEN)
313 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
314 "tbl: %p, max_entries: %u, use_entries: %u\n"
315 "ipv4_frag_pkt line0: %p, index: %u from %u\n"
316 "key: <%" PRIx64 ", %#x>, start: %" PRIu64 "\n",
318 tbl, tbl->max_entries, tbl->use_entries,
320 p1[i].key.src_dst[0], p1[i].key.id, p1[i].start);
322 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
323 "tbl: %p, max_entries: %u, use_entries: %u\n"
324 "ipv6_frag_pkt line0: %p, index: %u from %u\n"
325 "key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64 "\n",
327 tbl, tbl->max_entries, tbl->use_entries,
329 IPv6_KEY_BYTES(p1[i].key.src_dst), p1[i].key.id, p1[i].start);
331 if (ip_frag_key_cmp(key, &p1[i].key) == 0)
333 else if (ip_frag_key_is_empty(&p1[i].key))
334 empty = (empty == NULL) ? (p1 + i) : empty;
335 else if (max_cycles + p1[i].start < tms)
336 old = (old == NULL) ? (p1 + i) : old;
338 if (p2->key.key_len == IPV4_KEYLEN)
339 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
340 "tbl: %p, max_entries: %u, use_entries: %u\n"
341 "ipv4_frag_pkt line1: %p, index: %u from %u\n"
342 "key: <%" PRIx64 ", %#x>, start: %" PRIu64 "\n",
344 tbl, tbl->max_entries, tbl->use_entries,
346 p2[i].key.src_dst[0], p2[i].key.id, p2[i].start);
348 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
349 "tbl: %p, max_entries: %u, use_entries: %u\n"
350 "ipv6_frag_pkt line1: %p, index: %u from %u\n"
351 "key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64 "\n",
353 tbl, tbl->max_entries, tbl->use_entries,
355 IPv6_KEY_BYTES(p2[i].key.src_dst), p2[i].key.id, p2[i].start);
357 if (ip_frag_key_cmp(key, &p2[i].key) == 0)
359 else if (ip_frag_key_is_empty(&p2[i].key))
360 empty = (empty == NULL) ?( p2 + i) : empty;
361 else if (max_cycles + p2[i].start < tms)
362 old = (old == NULL) ? (p2 + i) : old;