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, table_meta_offset;
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);
227 table_meta_offset = 0;
228 bucket_offset = table_meta_offset + table_meta_sz;
229 bucket_ext_offset = bucket_offset + bucket_sz;
230 key_offset = bucket_ext_offset + bucket_ext_sz;
231 key_stack_offset = key_offset + key_sz;
232 bkt_ext_stack_offset = key_stack_offset + key_stack_sz;
233 data_offset = bkt_ext_stack_offset + bkt_ext_stack_sz;
235 t->buckets = (struct bucket *) &t->memory[bucket_offset];
236 t->buckets_ext = (struct bucket *) &t->memory[bucket_ext_offset];
237 t->key_mem = &t->memory[key_offset];
238 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
239 t->bkt_ext_stack = (uint32_t *) &t->memory[bkt_ext_stack_offset];
240 t->data_mem = &t->memory[data_offset];
243 for (i = 0; i < t->n_keys; i++)
244 t->key_stack[i] = t->n_keys - 1 - i;
245 t->key_stack_tos = t->n_keys;
247 /* Bucket ext stack */
248 for (i = 0; i < t->n_buckets_ext; i++)
249 t->bkt_ext_stack[i] = t->n_buckets_ext - 1 - i;
250 t->bkt_ext_stack_tos = t->n_buckets_ext;
256 rte_table_hash_ext_free(void *table)
258 struct rte_table_hash *t = (struct rte_table_hash *) table;
260 /* Check input parameters */
269 rte_table_hash_ext_entry_add(void *table, void *key, void *entry,
270 int *key_found, void **entry_ptr)
272 struct rte_table_hash *t = (struct rte_table_hash *) table;
273 struct bucket *bkt0, *bkt, *bkt_prev;
275 uint32_t bkt_index, i;
277 sig = t->f_hash(key, t->key_size, t->seed);
278 bkt_index = sig & t->bucket_mask;
279 bkt0 = &t->buckets[bkt_index];
280 sig = (sig >> 16) | 1LLU;
282 /* Key is present in the bucket */
283 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
284 for (i = 0; i < KEYS_PER_BUCKET; i++) {
285 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
286 uint32_t bkt_key_index = bkt->key_pos[i];
288 &t->key_mem[bkt_key_index << t->key_size_shl];
290 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
291 t->key_size) == 0)) {
292 uint8_t *data = &t->data_mem[bkt_key_index <<
295 memcpy(data, entry, t->entry_size);
297 *entry_ptr = (void *) data;
302 /* Key is not present in the bucket */
303 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
304 bkt = BUCKET_NEXT(bkt))
305 for (i = 0; i < KEYS_PER_BUCKET; i++) {
306 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
309 uint32_t bkt_key_index;
310 uint8_t *bkt_key, *data;
312 /* Allocate new key */
313 if (t->key_stack_tos == 0) /* No free keys */
316 bkt_key_index = t->key_stack[
319 /* Install new key */
320 bkt_key = &t->key_mem[bkt_key_index <<
322 data = &t->data_mem[bkt_key_index <<
325 bkt->sig[i] = (uint16_t) sig;
326 bkt->key_pos[i] = bkt_key_index;
327 memcpy(bkt_key, key, t->key_size);
328 memcpy(data, entry, t->entry_size);
331 *entry_ptr = (void *) data;
336 /* Bucket full: extend bucket */
337 if ((t->bkt_ext_stack_tos > 0) && (t->key_stack_tos > 0)) {
338 uint32_t bkt_key_index;
339 uint8_t *bkt_key, *data;
341 /* Allocate new bucket ext */
342 bkt_index = t->bkt_ext_stack[--t->bkt_ext_stack_tos];
343 bkt = &t->buckets_ext[bkt_index];
345 /* Chain the new bucket ext */
346 BUCKET_NEXT_SET(bkt_prev, bkt);
347 BUCKET_NEXT_SET_NULL(bkt);
349 /* Allocate new key */
350 bkt_key_index = t->key_stack[--t->key_stack_tos];
351 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
353 data = &t->data_mem[bkt_key_index << t->data_size_shl];
355 /* Install new key into bucket */
356 bkt->sig[0] = (uint16_t) sig;
357 bkt->key_pos[0] = bkt_key_index;
358 memcpy(bkt_key, key, t->key_size);
359 memcpy(data, entry, t->entry_size);
362 *entry_ptr = (void *) data;
370 rte_table_hash_ext_entry_delete(void *table, void *key, int *key_found,
373 struct rte_table_hash *t = (struct rte_table_hash *) table;
374 struct bucket *bkt0, *bkt, *bkt_prev;
376 uint32_t bkt_index, i;
378 sig = t->f_hash(key, t->key_size, t->seed);
379 bkt_index = sig & t->bucket_mask;
380 bkt0 = &t->buckets[bkt_index];
381 sig = (sig >> 16) | 1LLU;
383 /* Key is present in the bucket */
384 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
385 bkt = BUCKET_NEXT(bkt))
386 for (i = 0; i < KEYS_PER_BUCKET; i++) {
387 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
388 uint32_t bkt_key_index = bkt->key_pos[i];
389 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
392 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
393 t->key_size) == 0)) {
394 uint8_t *data = &t->data_mem[bkt_key_index <<
397 /* Uninstall key from bucket */
401 memcpy(entry, data, t->entry_size);
404 t->key_stack[t->key_stack_tos++] =
407 /*Check if bucket is unused */
408 if ((bkt_prev != NULL) &&
409 (bkt->sig[0] == 0) && (bkt->sig[1] == 0) &&
410 (bkt->sig[2] == 0) && (bkt->sig[3] == 0)) {
412 BUCKET_NEXT_COPY(bkt_prev, bkt);
415 memset(bkt, 0, sizeof(struct bucket));
417 /* Free bucket back to buckets ext */
418 bkt_index = bkt - t->buckets_ext;
419 t->bkt_ext_stack[t->bkt_ext_stack_tos++]
427 /* Key is not present in the bucket */
432 static int rte_table_hash_ext_lookup_unoptimized(
434 struct rte_mbuf **pkts,
436 uint64_t *lookup_hit_mask,
440 struct rte_table_hash *t = (struct rte_table_hash *) table;
441 uint64_t pkts_mask_out = 0;
443 for ( ; pkts_mask; ) {
444 struct bucket *bkt0, *bkt;
445 struct rte_mbuf *pkt;
447 uint64_t pkt_mask, sig;
448 uint32_t pkt_index, bkt_index, i;
450 pkt_index = __builtin_ctzll(pkts_mask);
451 pkt_mask = 1LLU << pkt_index;
452 pkts_mask &= ~pkt_mask;
454 pkt = pkts[pkt_index];
455 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
457 sig = (uint64_t) t->f_hash(key, t->key_size, t->seed);
459 sig = RTE_MBUF_METADATA_UINT32(pkt,
460 t->signature_offset);
462 bkt_index = sig & t->bucket_mask;
463 bkt0 = &t->buckets[bkt_index];
464 sig = (sig >> 16) | 1LLU;
466 /* Key is present in the bucket */
467 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
468 for (i = 0; i < KEYS_PER_BUCKET; i++) {
469 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
470 uint32_t bkt_key_index = bkt->key_pos[i];
471 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
474 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
475 t->key_size) == 0)) {
476 uint8_t *data = &t->data_mem[
477 bkt_key_index << t->data_size_shl];
479 pkts_mask_out |= pkt_mask;
480 entries[pkt_index] = (void *) data;
486 *lookup_hit_mask = pkts_mask_out;
492 * mask = match bitmask
493 * match = at least one match
494 * match_many = more than one match
495 * match_pos = position of first match
497 *----------------------------------------
498 * mask match match_many match_pos
499 *----------------------------------------
504 *----------------------------------------
509 *----------------------------------------
514 *----------------------------------------
519 *----------------------------------------
521 * match = 1111_1111_1111_1110
522 * match_many = 1111_1110_1110_1000
523 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
526 * match_many = 0xFEE8LLU
527 * match_pos = 0x12131210LLU
531 #define LUT_MATCH 0xFFFELLU
532 #define LUT_MATCH_MANY 0xFEE8LLU
533 #define LUT_MATCH_POS 0x12131210LLU
535 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos) \
537 uint64_t bucket_sig[4], mask[4], mask_all; \
539 bucket_sig[0] = bucket->sig[0]; \
540 bucket_sig[1] = bucket->sig[1]; \
541 bucket_sig[2] = bucket->sig[2]; \
542 bucket_sig[3] = bucket->sig[3]; \
544 bucket_sig[0] ^= mbuf_sig; \
545 bucket_sig[1] ^= mbuf_sig; \
546 bucket_sig[2] ^= mbuf_sig; \
547 bucket_sig[3] ^= mbuf_sig; \
554 if (bucket_sig[0] == 0) \
556 if (bucket_sig[1] == 0) \
558 if (bucket_sig[2] == 0) \
560 if (bucket_sig[3] == 0) \
563 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
565 match = (LUT_MATCH >> mask_all) & 1; \
566 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
567 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
570 #define lookup_cmp_key(mbuf, key, match_key, f) \
572 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
573 uint64_t *bkt_key = (uint64_t *) key; \
575 switch (f->key_size) { \
578 uint64_t xor = pkt_key[0] ^ bkt_key[0]; \
587 uint64_t xor[2], or; \
589 xor[0] = pkt_key[0] ^ bkt_key[0]; \
590 xor[1] = pkt_key[1] ^ bkt_key[1]; \
591 or = xor[0] | xor[1]; \
600 uint64_t xor[4], or; \
602 xor[0] = pkt_key[0] ^ bkt_key[0]; \
603 xor[1] = pkt_key[1] ^ bkt_key[1]; \
604 xor[2] = pkt_key[2] ^ bkt_key[2]; \
605 xor[3] = pkt_key[3] ^ bkt_key[3]; \
606 or = xor[0] | xor[1] | xor[2] | xor[3]; \
615 uint64_t xor[8], or; \
617 xor[0] = pkt_key[0] ^ bkt_key[0]; \
618 xor[1] = pkt_key[1] ^ bkt_key[1]; \
619 xor[2] = pkt_key[2] ^ bkt_key[2]; \
620 xor[3] = pkt_key[3] ^ bkt_key[3]; \
621 xor[4] = pkt_key[4] ^ bkt_key[4]; \
622 xor[5] = pkt_key[5] ^ bkt_key[5]; \
623 xor[6] = pkt_key[6] ^ bkt_key[6]; \
624 xor[7] = pkt_key[7] ^ bkt_key[7]; \
625 or = xor[0] | xor[1] | xor[2] | xor[3] | \
626 xor[4] | xor[5] | xor[6] | xor[7]; \
635 if (memcmp(pkt_key, bkt_key, f->key_size) == 0) \
640 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index) \
642 uint64_t pkt00_mask, pkt01_mask; \
643 struct rte_mbuf *mbuf00, *mbuf01; \
645 pkt00_index = __builtin_ctzll(pkts_mask); \
646 pkt00_mask = 1LLU << pkt00_index; \
647 pkts_mask &= ~pkt00_mask; \
648 mbuf00 = pkts[pkt00_index]; \
650 pkt01_index = __builtin_ctzll(pkts_mask); \
651 pkt01_mask = 1LLU << pkt01_index; \
652 pkts_mask &= ~pkt01_mask; \
653 mbuf01 = pkts[pkt01_index]; \
655 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
656 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
659 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
662 uint64_t pkt00_mask, pkt01_mask; \
663 struct rte_mbuf *mbuf00, *mbuf01; \
665 pkt00_index = __builtin_ctzll(pkts_mask); \
666 pkt00_mask = 1LLU << pkt00_index; \
667 pkts_mask &= ~pkt00_mask; \
668 mbuf00 = pkts[pkt00_index]; \
670 pkt01_index = __builtin_ctzll(pkts_mask); \
671 if (pkts_mask == 0) \
672 pkt01_index = pkt00_index; \
673 pkt01_mask = 1LLU << pkt01_index; \
674 pkts_mask &= ~pkt01_mask; \
675 mbuf01 = pkts[pkt01_index]; \
677 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
678 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
681 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index) \
683 struct grinder *g10, *g11; \
684 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
685 struct rte_mbuf *mbuf10, *mbuf11; \
686 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
687 uint64_t bucket_mask = t->bucket_mask; \
688 uint32_t signature_offset = t->signature_offset; \
690 mbuf10 = pkts[pkt10_index]; \
691 sig10 = (uint64_t) RTE_MBUF_METADATA_UINT32(mbuf10, signature_offset);\
692 bkt10_index = sig10 & bucket_mask; \
693 bkt10 = &buckets[bkt10_index]; \
695 mbuf11 = pkts[pkt11_index]; \
696 sig11 = (uint64_t) RTE_MBUF_METADATA_UINT32(mbuf11, signature_offset);\
697 bkt11_index = sig11 & bucket_mask; \
698 bkt11 = &buckets[bkt11_index]; \
700 rte_prefetch0(bkt10); \
701 rte_prefetch0(bkt11); \
703 g10 = &g[pkt10_index]; \
707 g11 = &g[pkt11_index]; \
712 #define lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index) \
714 struct grinder *g10, *g11; \
715 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
716 struct rte_mbuf *mbuf10, *mbuf11; \
717 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
718 uint8_t *key10, *key11; \
719 uint64_t bucket_mask = t->bucket_mask; \
720 rte_table_hash_op_hash f_hash = t->f_hash; \
721 uint64_t seed = t->seed; \
722 uint32_t key_size = t->key_size; \
723 uint32_t key_offset = t->key_offset; \
725 mbuf10 = pkts[pkt10_index]; \
726 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset); \
727 sig10 = (uint64_t) f_hash(key10, key_size, seed); \
728 bkt10_index = sig10 & bucket_mask; \
729 bkt10 = &buckets[bkt10_index]; \
731 mbuf11 = pkts[pkt11_index]; \
732 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset); \
733 sig11 = (uint64_t) f_hash(key11, key_size, seed); \
734 bkt11_index = sig11 & bucket_mask; \
735 bkt11 = &buckets[bkt11_index]; \
737 rte_prefetch0(bkt10); \
738 rte_prefetch0(bkt11); \
740 g10 = &g[pkt10_index]; \
744 g11 = &g[pkt11_index]; \
749 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
751 struct grinder *g20, *g21; \
752 uint64_t sig20, sig21; \
753 struct bucket *bkt20, *bkt21; \
754 uint8_t *key20, *key21, *key_mem = t->key_mem; \
755 uint64_t match20, match21, match_many20, match_many21; \
756 uint64_t match_pos20, match_pos21; \
757 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
759 g20 = &g[pkt20_index]; \
762 sig20 = (sig20 >> 16) | 1LLU; \
763 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
764 match20 <<= pkt20_index; \
765 match_many20 |= BUCKET_NEXT_VALID(bkt20); \
766 match_many20 <<= pkt20_index; \
767 key20_index = bkt20->key_pos[match_pos20]; \
768 key20 = &key_mem[key20_index << key_size_shl]; \
770 g21 = &g[pkt21_index]; \
773 sig21 = (sig21 >> 16) | 1LLU; \
774 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
775 match21 <<= pkt21_index; \
776 match_many21 |= BUCKET_NEXT_VALID(bkt21); \
777 match_many21 <<= pkt21_index; \
778 key21_index = bkt21->key_pos[match_pos21]; \
779 key21 = &key_mem[key21_index << key_size_shl]; \
781 rte_prefetch0(key20); \
782 rte_prefetch0(key21); \
784 pkts_mask_match_many |= match_many20 | match_many21; \
786 g20->match = match20; \
787 g20->key_index = key20_index; \
789 g21->match = match21; \
790 g21->key_index = key21_index; \
793 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
796 struct grinder *g30, *g31; \
797 struct rte_mbuf *mbuf30, *mbuf31; \
798 uint8_t *key30, *key31, *key_mem = t->key_mem; \
799 uint8_t *data30, *data31, *data_mem = t->data_mem; \
800 uint64_t match30, match31, match_key30, match_key31, match_keys;\
801 uint32_t key30_index, key31_index; \
802 uint32_t key_size_shl = t->key_size_shl; \
803 uint32_t data_size_shl = t->data_size_shl; \
805 mbuf30 = pkts[pkt30_index]; \
806 g30 = &g[pkt30_index]; \
807 match30 = g30->match; \
808 key30_index = g30->key_index; \
809 key30 = &key_mem[key30_index << key_size_shl]; \
810 lookup_cmp_key(mbuf30, key30, match_key30, t); \
811 match_key30 <<= pkt30_index; \
812 match_key30 &= match30; \
813 data30 = &data_mem[key30_index << data_size_shl]; \
814 entries[pkt30_index] = data30; \
816 mbuf31 = pkts[pkt31_index]; \
817 g31 = &g[pkt31_index]; \
818 match31 = g31->match; \
819 key31_index = g31->key_index; \
820 key31 = &key_mem[key31_index << key_size_shl]; \
821 lookup_cmp_key(mbuf31, key31, match_key31, t); \
822 match_key31 <<= pkt31_index; \
823 match_key31 &= match31; \
824 data31 = &data_mem[key31_index << data_size_shl]; \
825 entries[pkt31_index] = data31; \
827 rte_prefetch0(data30); \
828 rte_prefetch0(data31); \
830 match_keys = match_key30 | match_key31; \
831 pkts_mask_out |= match_keys; \
835 * The lookup function implements a 4-stage pipeline, with each stage processing
836 * two different packets. The purpose of pipelined implementation is to hide the
837 * latency of prefetching the data structures and loosen the data dependency
838 * between instructions.
840 * p00 _______ p10 _______ p20 _______ p30 _______
841 *----->| |----->| |----->| |----->| |----->
842 * | 0 | | 1 | | 2 | | 3 |
843 *----->|_______|----->|_______|----->|_______|----->|_______|----->
846 * The naming convention is:
847 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
850 static int rte_table_hash_ext_lookup(
852 struct rte_mbuf **pkts,
854 uint64_t *lookup_hit_mask,
857 struct rte_table_hash *t = (struct rte_table_hash *) table;
858 struct grinder *g = t->grinders;
859 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
860 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
861 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
864 /* Cannot run the pipeline with less than 7 packets */
865 if (__builtin_popcountll(pkts_mask) < 7)
866 return rte_table_hash_ext_lookup_unoptimized(table, pkts,
867 pkts_mask, lookup_hit_mask, entries, 0);
869 /* Pipeline stage 0 */
870 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
873 pkt10_index = pkt00_index;
874 pkt11_index = pkt01_index;
876 /* Pipeline stage 0 */
877 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
879 /* Pipeline stage 1 */
880 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
883 pkt20_index = pkt10_index;
884 pkt21_index = pkt11_index;
885 pkt10_index = pkt00_index;
886 pkt11_index = pkt01_index;
888 /* Pipeline stage 0 */
889 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
891 /* Pipeline stage 1 */
892 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
894 /* Pipeline stage 2 */
895 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
901 for ( ; pkts_mask; ) {
903 pkt30_index = pkt20_index;
904 pkt31_index = pkt21_index;
905 pkt20_index = pkt10_index;
906 pkt21_index = pkt11_index;
907 pkt10_index = pkt00_index;
908 pkt11_index = pkt01_index;
910 /* Pipeline stage 0 */
911 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
912 pkt00_index, pkt01_index);
914 /* Pipeline stage 1 */
915 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
917 /* Pipeline stage 2 */
918 lookup2_stage2(t, g, pkt20_index, pkt21_index,
919 pkts_mask_match_many);
921 /* Pipeline stage 3 */
922 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
923 pkts_mask_out, entries);
927 pkt30_index = pkt20_index;
928 pkt31_index = pkt21_index;
929 pkt20_index = pkt10_index;
930 pkt21_index = pkt11_index;
931 pkt10_index = pkt00_index;
932 pkt11_index = pkt01_index;
934 /* Pipeline stage 1 */
935 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
937 /* Pipeline stage 2 */
938 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
940 /* Pipeline stage 3 */
941 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
945 pkt30_index = pkt20_index;
946 pkt31_index = pkt21_index;
947 pkt20_index = pkt10_index;
948 pkt21_index = pkt11_index;
950 /* Pipeline stage 2 */
951 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
953 /* Pipeline stage 3 */
954 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
958 pkt30_index = pkt20_index;
959 pkt31_index = pkt21_index;
961 /* Pipeline stage 3 */
962 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
966 pkts_mask_match_many &= ~pkts_mask_out;
967 if (pkts_mask_match_many) {
968 uint64_t pkts_mask_out_slow = 0;
970 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
971 pkts_mask_match_many, &pkts_mask_out_slow, entries, 0);
972 pkts_mask_out |= pkts_mask_out_slow;
975 *lookup_hit_mask = pkts_mask_out;
979 static int rte_table_hash_ext_lookup_dosig(
981 struct rte_mbuf **pkts,
983 uint64_t *lookup_hit_mask,
986 struct rte_table_hash *t = (struct rte_table_hash *) table;
987 struct grinder *g = t->grinders;
988 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
989 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
990 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
993 /* Cannot run the pipeline with less than 7 packets */
994 if (__builtin_popcountll(pkts_mask) < 7)
995 return rte_table_hash_ext_lookup_unoptimized(table, pkts,
996 pkts_mask, lookup_hit_mask, entries, 1);
998 /* Pipeline stage 0 */
999 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
1002 pkt10_index = pkt00_index;
1003 pkt11_index = pkt01_index;
1005 /* Pipeline stage 0 */
1006 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
1008 /* Pipeline stage 1 */
1009 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1012 pkt20_index = pkt10_index;
1013 pkt21_index = pkt11_index;
1014 pkt10_index = pkt00_index;
1015 pkt11_index = pkt01_index;
1017 /* Pipeline stage 0 */
1018 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
1020 /* Pipeline stage 1 */
1021 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1023 /* Pipeline stage 2 */
1024 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
1030 for ( ; pkts_mask; ) {
1032 pkt30_index = pkt20_index;
1033 pkt31_index = pkt21_index;
1034 pkt20_index = pkt10_index;
1035 pkt21_index = pkt11_index;
1036 pkt10_index = pkt00_index;
1037 pkt11_index = pkt01_index;
1039 /* Pipeline stage 0 */
1040 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
1041 pkt00_index, pkt01_index);
1043 /* Pipeline stage 1 */
1044 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1046 /* Pipeline stage 2 */
1047 lookup2_stage2(t, g, pkt20_index, pkt21_index,
1048 pkts_mask_match_many);
1050 /* Pipeline stage 3 */
1051 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
1052 pkts_mask_out, entries);
1056 pkt30_index = pkt20_index;
1057 pkt31_index = pkt21_index;
1058 pkt20_index = pkt10_index;
1059 pkt21_index = pkt11_index;
1060 pkt10_index = pkt00_index;
1061 pkt11_index = pkt01_index;
1063 /* Pipeline stage 1 */
1064 lookup2_stage1_dosig(t, g, pkts, pkt10_index, pkt11_index);
1066 /* Pipeline stage 2 */
1067 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
1069 /* Pipeline stage 3 */
1070 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
1074 pkt30_index = pkt20_index;
1075 pkt31_index = pkt21_index;
1076 pkt20_index = pkt10_index;
1077 pkt21_index = pkt11_index;
1079 /* Pipeline stage 2 */
1080 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
1082 /* Pipeline stage 3 */
1083 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
1087 pkt30_index = pkt20_index;
1088 pkt31_index = pkt21_index;
1090 /* Pipeline stage 3 */
1091 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
1095 pkts_mask_match_many &= ~pkts_mask_out;
1096 if (pkts_mask_match_many) {
1097 uint64_t pkts_mask_out_slow = 0;
1099 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
1100 pkts_mask_match_many, &pkts_mask_out_slow, entries, 1);
1101 pkts_mask_out |= pkts_mask_out_slow;
1104 *lookup_hit_mask = pkts_mask_out;
1108 struct rte_table_ops rte_table_hash_ext_ops = {
1109 .f_create = rte_table_hash_ext_create,
1110 .f_free = rte_table_hash_ext_free,
1111 .f_add = rte_table_hash_ext_entry_add,
1112 .f_delete = rte_table_hash_ext_entry_delete,
1113 .f_lookup = rte_table_hash_ext_lookup,
1116 struct rte_table_ops rte_table_hash_ext_dosig_ops = {
1117 .f_create = rte_table_hash_ext_create,
1118 .f_free = rte_table_hash_ext_free,
1119 .f_add = rte_table_hash_ext_entry_add,
1120 .f_delete = rte_table_hash_ext_entry_delete,
1121 .f_lookup = rte_table_hash_ext_lookup_dosig,