4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_common.h>
39 #include <rte_malloc.h>
42 #include "rte_table_hash.h"
44 #define KEYS_PER_BUCKET 4
51 uint16_t sig[KEYS_PER_BUCKET];
52 uint32_t key_pos[KEYS_PER_BUCKET];
55 #define BUCKET_NEXT(bucket) \
56 ((void *) ((bucket)->next & (~1LU)))
58 #define BUCKET_NEXT_VALID(bucket) \
59 ((bucket)->next & 1LU)
61 #define BUCKET_NEXT_SET(bucket, bucket_next) \
63 (bucket)->next = (((uintptr_t) ((void *) (bucket_next))) | 1LU);\
66 #define BUCKET_NEXT_SET_NULL(bucket) \
71 #define BUCKET_NEXT_COPY(bucket, bucket2) \
73 (bucket)->next = (bucket2)->next; \
83 struct rte_table_hash {
84 /* Input parameters */
89 uint32_t n_buckets_ext;
90 rte_table_hash_op_hash f_hash;
92 uint32_t signature_offset;
97 uint32_t key_size_shl;
98 uint32_t data_size_shl;
99 uint32_t key_stack_tos;
100 uint32_t bkt_ext_stack_tos;
103 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
106 struct bucket *buckets;
107 struct bucket *buckets_ext;
111 uint32_t *bkt_ext_stack;
114 uint8_t memory[0] __rte_cache_aligned;
118 check_params_create(struct rte_table_hash_ext_params *params)
120 uint32_t n_buckets_min;
123 if ((params->key_size == 0) ||
124 (!rte_is_power_of_2(params->key_size))) {
125 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
130 if ((params->n_keys == 0) ||
131 (!rte_is_power_of_2(params->n_keys))) {
132 RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
137 n_buckets_min = (params->n_keys + KEYS_PER_BUCKET - 1) / params->n_keys;
138 if ((params->n_buckets == 0) ||
139 (!rte_is_power_of_2(params->n_keys)) ||
140 (params->n_buckets < n_buckets_min)) {
141 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
146 if (params->f_hash == NULL) {
147 RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
151 /* signature offset */
152 if ((params->signature_offset & 0x3) != 0) {
153 RTE_LOG(ERR, TABLE, "%s: signature_offset invalid value\n",
159 if ((params->key_offset & 0x7) != 0) {
160 RTE_LOG(ERR, TABLE, "%s: key_offset invalid value\n", __func__);
168 rte_table_hash_ext_create(void *params, int socket_id, uint32_t entry_size)
170 struct rte_table_hash_ext_params *p =
171 (struct rte_table_hash_ext_params *) params;
172 struct rte_table_hash *t;
173 uint32_t total_size, table_meta_sz;
174 uint32_t bucket_sz, bucket_ext_sz, key_sz;
175 uint32_t key_stack_sz, bkt_ext_stack_sz, data_sz;
176 uint32_t bucket_offset, bucket_ext_offset, key_offset;
177 uint32_t key_stack_offset, bkt_ext_stack_offset, data_offset;
180 /* Check input parameters */
181 if ((check_params_create(p) != 0) ||
182 (!rte_is_power_of_2(entry_size)) ||
183 ((sizeof(struct rte_table_hash) % CACHE_LINE_SIZE) != 0) ||
184 (sizeof(struct bucket) != (CACHE_LINE_SIZE / 2)))
187 /* Memory allocation */
188 table_meta_sz = CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
189 bucket_sz = CACHE_LINE_ROUNDUP(p->n_buckets * sizeof(struct bucket));
191 CACHE_LINE_ROUNDUP(p->n_buckets_ext * sizeof(struct bucket));
192 key_sz = CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
193 key_stack_sz = CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
195 CACHE_LINE_ROUNDUP(p->n_buckets_ext * sizeof(uint32_t));
196 data_sz = CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
197 total_size = table_meta_sz + bucket_sz + bucket_ext_sz + key_sz +
198 key_stack_sz + bkt_ext_stack_sz + data_sz;
200 t = rte_zmalloc_socket("TABLE", total_size, CACHE_LINE_SIZE, socket_id);
203 "%s: Cannot allocate %u bytes for hash table\n",
204 __func__, total_size);
207 RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table memory footprint is "
208 "%u bytes\n", __func__, p->key_size, total_size);
210 /* Memory initialization */
211 t->key_size = p->key_size;
212 t->entry_size = entry_size;
213 t->n_keys = p->n_keys;
214 t->n_buckets = p->n_buckets;
215 t->n_buckets_ext = p->n_buckets_ext;
216 t->f_hash = p->f_hash;
218 t->signature_offset = p->signature_offset;
219 t->key_offset = p->key_offset;
222 t->bucket_mask = t->n_buckets - 1;
223 t->key_size_shl = __builtin_ctzl(p->key_size);
224 t->data_size_shl = __builtin_ctzl(entry_size);
228 bucket_ext_offset = bucket_offset + bucket_sz;
229 key_offset = bucket_ext_offset + bucket_ext_sz;
230 key_stack_offset = key_offset + key_sz;
231 bkt_ext_stack_offset = key_stack_offset + key_stack_sz;
232 data_offset = bkt_ext_stack_offset + bkt_ext_stack_sz;
234 t->buckets = (struct bucket *) &t->memory[bucket_offset];
235 t->buckets_ext = (struct bucket *) &t->memory[bucket_ext_offset];
236 t->key_mem = &t->memory[key_offset];
237 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
238 t->bkt_ext_stack = (uint32_t *) &t->memory[bkt_ext_stack_offset];
239 t->data_mem = &t->memory[data_offset];
242 for (i = 0; i < t->n_keys; i++)
243 t->key_stack[i] = t->n_keys - 1 - i;
244 t->key_stack_tos = t->n_keys;
246 /* Bucket ext stack */
247 for (i = 0; i < t->n_buckets_ext; i++)
248 t->bkt_ext_stack[i] = t->n_buckets_ext - 1 - i;
249 t->bkt_ext_stack_tos = t->n_buckets_ext;
255 rte_table_hash_ext_free(void *table)
257 struct rte_table_hash *t = (struct rte_table_hash *) table;
259 /* Check input parameters */
268 rte_table_hash_ext_entry_add(void *table, void *key, void *entry,
269 int *key_found, void **entry_ptr)
271 struct rte_table_hash *t = (struct rte_table_hash *) table;
272 struct bucket *bkt0, *bkt, *bkt_prev;
274 uint32_t bkt_index, i;
276 sig = t->f_hash(key, t->key_size, t->seed);
277 bkt_index = sig & t->bucket_mask;
278 bkt0 = &t->buckets[bkt_index];
279 sig = (sig >> 16) | 1LLU;
281 /* Key is present in the bucket */
282 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
283 for (i = 0; i < KEYS_PER_BUCKET; i++) {
284 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
285 uint32_t bkt_key_index = bkt->key_pos[i];
287 &t->key_mem[bkt_key_index << t->key_size_shl];
289 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
290 t->key_size) == 0)) {
291 uint8_t *data = &t->data_mem[bkt_key_index <<
294 memcpy(data, entry, t->entry_size);
296 *entry_ptr = (void *) data;
301 /* Key is not present in the bucket */
302 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
303 bkt = BUCKET_NEXT(bkt))
304 for (i = 0; i < KEYS_PER_BUCKET; i++) {
305 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
308 uint32_t bkt_key_index;
309 uint8_t *bkt_key, *data;
311 /* Allocate new key */
312 if (t->key_stack_tos == 0) /* No free keys */
315 bkt_key_index = t->key_stack[
318 /* Install new key */
319 bkt_key = &t->key_mem[bkt_key_index <<
321 data = &t->data_mem[bkt_key_index <<
324 bkt->sig[i] = (uint16_t) sig;
325 bkt->key_pos[i] = bkt_key_index;
326 memcpy(bkt_key, key, t->key_size);
327 memcpy(data, entry, t->entry_size);
330 *entry_ptr = (void *) data;
335 /* Bucket full: extend bucket */
336 if ((t->bkt_ext_stack_tos > 0) && (t->key_stack_tos > 0)) {
337 uint32_t bkt_key_index;
338 uint8_t *bkt_key, *data;
340 /* Allocate new bucket ext */
341 bkt_index = t->bkt_ext_stack[--t->bkt_ext_stack_tos];
342 bkt = &t->buckets_ext[bkt_index];
344 /* Chain the new bucket ext */
345 BUCKET_NEXT_SET(bkt_prev, bkt);
346 BUCKET_NEXT_SET_NULL(bkt);
348 /* Allocate new key */
349 bkt_key_index = t->key_stack[--t->key_stack_tos];
350 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
352 data = &t->data_mem[bkt_key_index << t->data_size_shl];
354 /* Install new key into bucket */
355 bkt->sig[0] = (uint16_t) sig;
356 bkt->key_pos[0] = bkt_key_index;
357 memcpy(bkt_key, key, t->key_size);
358 memcpy(data, entry, t->entry_size);
361 *entry_ptr = (void *) data;
369 rte_table_hash_ext_entry_delete(void *table, void *key, int *key_found,
372 struct rte_table_hash *t = (struct rte_table_hash *) table;
373 struct bucket *bkt0, *bkt, *bkt_prev;
375 uint32_t bkt_index, i;
377 sig = t->f_hash(key, t->key_size, t->seed);
378 bkt_index = sig & t->bucket_mask;
379 bkt0 = &t->buckets[bkt_index];
380 sig = (sig >> 16) | 1LLU;
382 /* Key is present in the bucket */
383 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
384 bkt = BUCKET_NEXT(bkt))
385 for (i = 0; i < KEYS_PER_BUCKET; i++) {
386 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
387 uint32_t bkt_key_index = bkt->key_pos[i];
388 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
391 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
392 t->key_size) == 0)) {
393 uint8_t *data = &t->data_mem[bkt_key_index <<
396 /* Uninstall key from bucket */
400 memcpy(entry, data, t->entry_size);
403 t->key_stack[t->key_stack_tos++] =
406 /*Check if bucket is unused */
407 if ((bkt_prev != NULL) &&
408 (bkt->sig[0] == 0) && (bkt->sig[1] == 0) &&
409 (bkt->sig[2] == 0) && (bkt->sig[3] == 0)) {
411 BUCKET_NEXT_COPY(bkt_prev, bkt);
414 memset(bkt, 0, sizeof(struct bucket));
416 /* Free bucket back to buckets ext */
417 bkt_index = bkt - t->buckets_ext;
418 t->bkt_ext_stack[t->bkt_ext_stack_tos++]
426 /* Key is not present in the bucket */
431 static int rte_table_hash_ext_lookup_unoptimized(
433 struct rte_mbuf **pkts,
435 uint64_t *lookup_hit_mask,
439 struct rte_table_hash *t = (struct rte_table_hash *) table;
440 uint64_t pkts_mask_out = 0;
442 for ( ; pkts_mask; ) {
443 struct bucket *bkt0, *bkt;
444 struct rte_mbuf *pkt;
446 uint64_t pkt_mask, sig;
447 uint32_t pkt_index, bkt_index, i;
449 pkt_index = __builtin_ctzll(pkts_mask);
450 pkt_mask = 1LLU << pkt_index;
451 pkts_mask &= ~pkt_mask;
453 pkt = pkts[pkt_index];
454 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
456 sig = (uint64_t) t->f_hash(key, t->key_size, t->seed);
458 sig = RTE_MBUF_METADATA_UINT32(pkt,
459 t->signature_offset);
461 bkt_index = sig & t->bucket_mask;
462 bkt0 = &t->buckets[bkt_index];
463 sig = (sig >> 16) | 1LLU;
465 /* Key is present in the bucket */
466 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
467 for (i = 0; i < KEYS_PER_BUCKET; i++) {
468 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
469 uint32_t bkt_key_index = bkt->key_pos[i];
470 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
473 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
474 t->key_size) == 0)) {
475 uint8_t *data = &t->data_mem[
476 bkt_key_index << t->data_size_shl];
478 pkts_mask_out |= pkt_mask;
479 entries[pkt_index] = (void *) data;
485 *lookup_hit_mask = pkts_mask_out;
491 * mask = match bitmask
492 * match = at least one match
493 * match_many = more than one match
494 * match_pos = position of first match
496 *----------------------------------------
497 * mask match match_many match_pos
498 *----------------------------------------
503 *----------------------------------------
508 *----------------------------------------
513 *----------------------------------------
518 *----------------------------------------
520 * match = 1111_1111_1111_1110
521 * match_many = 1111_1110_1110_1000
522 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
525 * match_many = 0xFEE8LLU
526 * match_pos = 0x12131210LLU
530 #define LUT_MATCH 0xFFFELLU
531 #define LUT_MATCH_MANY 0xFEE8LLU
532 #define LUT_MATCH_POS 0x12131210LLU
534 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos) \
536 uint64_t bucket_sig[4], mask[4], mask_all; \
538 bucket_sig[0] = bucket->sig[0]; \
539 bucket_sig[1] = bucket->sig[1]; \
540 bucket_sig[2] = bucket->sig[2]; \
541 bucket_sig[3] = bucket->sig[3]; \
543 bucket_sig[0] ^= mbuf_sig; \
544 bucket_sig[1] ^= mbuf_sig; \
545 bucket_sig[2] ^= mbuf_sig; \
546 bucket_sig[3] ^= mbuf_sig; \
553 if (bucket_sig[0] == 0) \
555 if (bucket_sig[1] == 0) \
557 if (bucket_sig[2] == 0) \
559 if (bucket_sig[3] == 0) \
562 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
564 match = (LUT_MATCH >> mask_all) & 1; \
565 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
566 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
569 #define lookup_cmp_key(mbuf, key, match_key, f) \
571 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
572 uint64_t *bkt_key = (uint64_t *) key; \
574 switch (f->key_size) { \
577 uint64_t xor = pkt_key[0] ^ bkt_key[0]; \
586 uint64_t xor[2], or; \
588 xor[0] = pkt_key[0] ^ bkt_key[0]; \
589 xor[1] = pkt_key[1] ^ bkt_key[1]; \
590 or = xor[0] | xor[1]; \
599 uint64_t xor[4], or; \
601 xor[0] = pkt_key[0] ^ bkt_key[0]; \
602 xor[1] = pkt_key[1] ^ bkt_key[1]; \
603 xor[2] = pkt_key[2] ^ bkt_key[2]; \
604 xor[3] = pkt_key[3] ^ bkt_key[3]; \
605 or = xor[0] | xor[1] | xor[2] | xor[3]; \
614 uint64_t xor[8], or; \
616 xor[0] = pkt_key[0] ^ bkt_key[0]; \
617 xor[1] = pkt_key[1] ^ bkt_key[1]; \
618 xor[2] = pkt_key[2] ^ bkt_key[2]; \
619 xor[3] = pkt_key[3] ^ bkt_key[3]; \
620 xor[4] = pkt_key[4] ^ bkt_key[4]; \
621 xor[5] = pkt_key[5] ^ bkt_key[5]; \
622 xor[6] = pkt_key[6] ^ bkt_key[6]; \
623 xor[7] = pkt_key[7] ^ bkt_key[7]; \
624 or = xor[0] | xor[1] | xor[2] | xor[3] | \
625 xor[4] | xor[5] | xor[6] | xor[7]; \
634 if (memcmp(pkt_key, bkt_key, f->key_size) == 0) \
639 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index) \
641 uint64_t pkt00_mask, pkt01_mask; \
642 struct rte_mbuf *mbuf00, *mbuf01; \
644 pkt00_index = __builtin_ctzll(pkts_mask); \
645 pkt00_mask = 1LLU << pkt00_index; \
646 pkts_mask &= ~pkt00_mask; \
647 mbuf00 = pkts[pkt00_index]; \
649 pkt01_index = __builtin_ctzll(pkts_mask); \
650 pkt01_mask = 1LLU << pkt01_index; \
651 pkts_mask &= ~pkt01_mask; \
652 mbuf01 = pkts[pkt01_index]; \
654 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
655 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
658 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
661 uint64_t pkt00_mask, pkt01_mask; \
662 struct rte_mbuf *mbuf00, *mbuf01; \
664 pkt00_index = __builtin_ctzll(pkts_mask); \
665 pkt00_mask = 1LLU << pkt00_index; \
666 pkts_mask &= ~pkt00_mask; \
667 mbuf00 = pkts[pkt00_index]; \
669 pkt01_index = __builtin_ctzll(pkts_mask); \
670 if (pkts_mask == 0) \
671 pkt01_index = pkt00_index; \
672 pkt01_mask = 1LLU << pkt01_index; \
673 pkts_mask &= ~pkt01_mask; \
674 mbuf01 = pkts[pkt01_index]; \
676 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
677 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
680 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index) \
682 struct grinder *g10, *g11; \
683 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
684 struct rte_mbuf *mbuf10, *mbuf11; \
685 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
686 uint64_t bucket_mask = t->bucket_mask; \
687 uint32_t signature_offset = t->signature_offset; \
689 mbuf10 = pkts[pkt10_index]; \
690 sig10 = (uint64_t) RTE_MBUF_METADATA_UINT32(mbuf10, signature_offset);\
691 bkt10_index = sig10 & bucket_mask; \
692 bkt10 = &buckets[bkt10_index]; \
694 mbuf11 = pkts[pkt11_index]; \
695 sig11 = (uint64_t) RTE_MBUF_METADATA_UINT32(mbuf11, signature_offset);\
696 bkt11_index = sig11 & bucket_mask; \
697 bkt11 = &buckets[bkt11_index]; \
699 rte_prefetch0(bkt10); \
700 rte_prefetch0(bkt11); \
702 g10 = &g[pkt10_index]; \
706 g11 = &g[pkt11_index]; \
711 #define lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index) \
713 struct grinder *g10, *g11; \
714 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
715 struct rte_mbuf *mbuf10, *mbuf11; \
716 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
717 uint8_t *key10, *key11; \
718 uint64_t bucket_mask = t->bucket_mask; \
719 rte_table_hash_op_hash f_hash = t->f_hash; \
720 uint64_t seed = t->seed; \
721 uint32_t key_size = t->key_size; \
722 uint32_t key_offset = t->key_offset; \
724 mbuf10 = pkts[pkt10_index]; \
725 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset); \
726 sig10 = (uint64_t) f_hash(key10, key_size, seed); \
727 bkt10_index = sig10 & bucket_mask; \
728 bkt10 = &buckets[bkt10_index]; \
730 mbuf11 = pkts[pkt11_index]; \
731 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset); \
732 sig11 = (uint64_t) f_hash(key11, key_size, seed); \
733 bkt11_index = sig11 & bucket_mask; \
734 bkt11 = &buckets[bkt11_index]; \
736 rte_prefetch0(bkt10); \
737 rte_prefetch0(bkt11); \
739 g10 = &g[pkt10_index]; \
743 g11 = &g[pkt11_index]; \
748 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
750 struct grinder *g20, *g21; \
751 uint64_t sig20, sig21; \
752 struct bucket *bkt20, *bkt21; \
753 uint8_t *key20, *key21, *key_mem = t->key_mem; \
754 uint64_t match20, match21, match_many20, match_many21; \
755 uint64_t match_pos20, match_pos21; \
756 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
758 g20 = &g[pkt20_index]; \
761 sig20 = (sig20 >> 16) | 1LLU; \
762 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
763 match20 <<= pkt20_index; \
764 match_many20 |= BUCKET_NEXT_VALID(bkt20); \
765 match_many20 <<= pkt20_index; \
766 key20_index = bkt20->key_pos[match_pos20]; \
767 key20 = &key_mem[key20_index << key_size_shl]; \
769 g21 = &g[pkt21_index]; \
772 sig21 = (sig21 >> 16) | 1LLU; \
773 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
774 match21 <<= pkt21_index; \
775 match_many21 |= BUCKET_NEXT_VALID(bkt21); \
776 match_many21 <<= pkt21_index; \
777 key21_index = bkt21->key_pos[match_pos21]; \
778 key21 = &key_mem[key21_index << key_size_shl]; \
780 rte_prefetch0(key20); \
781 rte_prefetch0(key21); \
783 pkts_mask_match_many |= match_many20 | match_many21; \
785 g20->match = match20; \
786 g20->key_index = key20_index; \
788 g21->match = match21; \
789 g21->key_index = key21_index; \
792 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
795 struct grinder *g30, *g31; \
796 struct rte_mbuf *mbuf30, *mbuf31; \
797 uint8_t *key30, *key31, *key_mem = t->key_mem; \
798 uint8_t *data30, *data31, *data_mem = t->data_mem; \
799 uint64_t match30, match31, match_key30, match_key31, match_keys;\
800 uint32_t key30_index, key31_index; \
801 uint32_t key_size_shl = t->key_size_shl; \
802 uint32_t data_size_shl = t->data_size_shl; \
804 mbuf30 = pkts[pkt30_index]; \
805 g30 = &g[pkt30_index]; \
806 match30 = g30->match; \
807 key30_index = g30->key_index; \
808 key30 = &key_mem[key30_index << key_size_shl]; \
809 lookup_cmp_key(mbuf30, key30, match_key30, t); \
810 match_key30 <<= pkt30_index; \
811 match_key30 &= match30; \
812 data30 = &data_mem[key30_index << data_size_shl]; \
813 entries[pkt30_index] = data30; \
815 mbuf31 = pkts[pkt31_index]; \
816 g31 = &g[pkt31_index]; \
817 match31 = g31->match; \
818 key31_index = g31->key_index; \
819 key31 = &key_mem[key31_index << key_size_shl]; \
820 lookup_cmp_key(mbuf31, key31, match_key31, t); \
821 match_key31 <<= pkt31_index; \
822 match_key31 &= match31; \
823 data31 = &data_mem[key31_index << data_size_shl]; \
824 entries[pkt31_index] = data31; \
826 rte_prefetch0(data30); \
827 rte_prefetch0(data31); \
829 match_keys = match_key30 | match_key31; \
830 pkts_mask_out |= match_keys; \
834 * The lookup function implements a 4-stage pipeline, with each stage processing
835 * two different packets. The purpose of pipelined implementation is to hide the
836 * latency of prefetching the data structures and loosen the data dependency
837 * between instructions.
839 * p00 _______ p10 _______ p20 _______ p30 _______
840 *----->| |----->| |----->| |----->| |----->
841 * | 0 | | 1 | | 2 | | 3 |
842 *----->|_______|----->|_______|----->|_______|----->|_______|----->
845 * The naming convention is:
846 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
849 static int rte_table_hash_ext_lookup(
851 struct rte_mbuf **pkts,
853 uint64_t *lookup_hit_mask,
856 struct rte_table_hash *t = (struct rte_table_hash *) table;
857 struct grinder *g = t->grinders;
858 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
859 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
860 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
863 /* Cannot run the pipeline with less than 7 packets */
864 if (__builtin_popcountll(pkts_mask) < 7)
865 return rte_table_hash_ext_lookup_unoptimized(table, pkts,
866 pkts_mask, lookup_hit_mask, entries, 0);
868 /* Pipeline stage 0 */
869 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
872 pkt10_index = pkt00_index;
873 pkt11_index = pkt01_index;
875 /* Pipeline stage 0 */
876 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
878 /* Pipeline stage 1 */
879 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
882 pkt20_index = pkt10_index;
883 pkt21_index = pkt11_index;
884 pkt10_index = pkt00_index;
885 pkt11_index = pkt01_index;
887 /* Pipeline stage 0 */
888 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
890 /* Pipeline stage 1 */
891 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
893 /* Pipeline stage 2 */
894 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
900 for ( ; pkts_mask; ) {
902 pkt30_index = pkt20_index;
903 pkt31_index = pkt21_index;
904 pkt20_index = pkt10_index;
905 pkt21_index = pkt11_index;
906 pkt10_index = pkt00_index;
907 pkt11_index = pkt01_index;
909 /* Pipeline stage 0 */
910 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
911 pkt00_index, pkt01_index);
913 /* Pipeline stage 1 */
914 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
916 /* Pipeline stage 2 */
917 lookup2_stage2(t, g, pkt20_index, pkt21_index,
918 pkts_mask_match_many);
920 /* Pipeline stage 3 */
921 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
922 pkts_mask_out, entries);
926 pkt30_index = pkt20_index;
927 pkt31_index = pkt21_index;
928 pkt20_index = pkt10_index;
929 pkt21_index = pkt11_index;
930 pkt10_index = pkt00_index;
931 pkt11_index = pkt01_index;
933 /* Pipeline stage 1 */
934 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
936 /* Pipeline stage 2 */
937 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
939 /* Pipeline stage 3 */
940 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
944 pkt30_index = pkt20_index;
945 pkt31_index = pkt21_index;
946 pkt20_index = pkt10_index;
947 pkt21_index = pkt11_index;
949 /* Pipeline stage 2 */
950 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
952 /* Pipeline stage 3 */
953 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
957 pkt30_index = pkt20_index;
958 pkt31_index = pkt21_index;
960 /* Pipeline stage 3 */
961 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
965 pkts_mask_match_many &= ~pkts_mask_out;
966 if (pkts_mask_match_many) {
967 uint64_t pkts_mask_out_slow = 0;
969 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
970 pkts_mask_match_many, &pkts_mask_out_slow, entries, 0);
971 pkts_mask_out |= pkts_mask_out_slow;
974 *lookup_hit_mask = pkts_mask_out;
978 static int rte_table_hash_ext_lookup_dosig(
980 struct rte_mbuf **pkts,
982 uint64_t *lookup_hit_mask,
985 struct rte_table_hash *t = (struct rte_table_hash *) table;
986 struct grinder *g = t->grinders;
987 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
988 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
989 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
992 /* Cannot run the pipeline with less than 7 packets */
993 if (__builtin_popcountll(pkts_mask) < 7)
994 return rte_table_hash_ext_lookup_unoptimized(table, pkts,
995 pkts_mask, lookup_hit_mask, entries, 1);
997 /* Pipeline stage 0 */
998 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
1001 pkt10_index = pkt00_index;
1002 pkt11_index = pkt01_index;
1004 /* Pipeline stage 0 */
1005 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
1007 /* Pipeline stage 1 */
1008 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1011 pkt20_index = pkt10_index;
1012 pkt21_index = pkt11_index;
1013 pkt10_index = pkt00_index;
1014 pkt11_index = pkt01_index;
1016 /* Pipeline stage 0 */
1017 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
1019 /* Pipeline stage 1 */
1020 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1022 /* Pipeline stage 2 */
1023 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
1029 for ( ; pkts_mask; ) {
1031 pkt30_index = pkt20_index;
1032 pkt31_index = pkt21_index;
1033 pkt20_index = pkt10_index;
1034 pkt21_index = pkt11_index;
1035 pkt10_index = pkt00_index;
1036 pkt11_index = pkt01_index;
1038 /* Pipeline stage 0 */
1039 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
1040 pkt00_index, pkt01_index);
1042 /* Pipeline stage 1 */
1043 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1045 /* Pipeline stage 2 */
1046 lookup2_stage2(t, g, pkt20_index, pkt21_index,
1047 pkts_mask_match_many);
1049 /* Pipeline stage 3 */
1050 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
1051 pkts_mask_out, entries);
1055 pkt30_index = pkt20_index;
1056 pkt31_index = pkt21_index;
1057 pkt20_index = pkt10_index;
1058 pkt21_index = pkt11_index;
1059 pkt10_index = pkt00_index;
1060 pkt11_index = pkt01_index;
1062 /* Pipeline stage 1 */
1063 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1065 /* Pipeline stage 2 */
1066 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
1068 /* Pipeline stage 3 */
1069 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
1073 pkt30_index = pkt20_index;
1074 pkt31_index = pkt21_index;
1075 pkt20_index = pkt10_index;
1076 pkt21_index = pkt11_index;
1078 /* Pipeline stage 2 */
1079 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
1081 /* Pipeline stage 3 */
1082 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
1086 pkt30_index = pkt20_index;
1087 pkt31_index = pkt21_index;
1089 /* Pipeline stage 3 */
1090 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
1094 pkts_mask_match_many &= ~pkts_mask_out;
1095 if (pkts_mask_match_many) {
1096 uint64_t pkts_mask_out_slow = 0;
1098 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
1099 pkts_mask_match_many, &pkts_mask_out_slow, entries, 1);
1100 pkts_mask_out |= pkts_mask_out_slow;
1103 *lookup_hit_mask = pkts_mask_out;
1107 struct rte_table_ops rte_table_hash_ext_ops = {
1108 .f_create = rte_table_hash_ext_create,
1109 .f_free = rte_table_hash_ext_free,
1110 .f_add = rte_table_hash_ext_entry_add,
1111 .f_delete = rte_table_hash_ext_entry_delete,
1112 .f_lookup = rte_table_hash_ext_lookup,
1115 struct rte_table_ops rte_table_hash_ext_dosig_ops = {
1116 .f_create = rte_table_hash_ext_create,
1117 .f_free = rte_table_hash_ext_free,
1118 .f_add = rte_table_hash_ext_entry_add,
1119 .f_delete = rte_table_hash_ext_entry_delete,
1120 .f_lookup = rte_table_hash_ext_lookup_dosig,