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_memory.h>
40 #include <rte_malloc.h>
43 #include "rte_table_hash.h"
45 #define KEYS_PER_BUCKET 4
52 uint16_t sig[KEYS_PER_BUCKET];
53 uint32_t key_pos[KEYS_PER_BUCKET];
56 #define BUCKET_NEXT(bucket) \
57 ((void *) ((bucket)->next & (~1LU)))
59 #define BUCKET_NEXT_VALID(bucket) \
60 ((bucket)->next & 1LU)
62 #define BUCKET_NEXT_SET(bucket, bucket_next) \
64 (bucket)->next = (((uintptr_t) ((void *) (bucket_next))) | 1LU);\
67 #define BUCKET_NEXT_SET_NULL(bucket) \
72 #define BUCKET_NEXT_COPY(bucket, bucket2) \
74 (bucket)->next = (bucket2)->next; \
77 #ifdef RTE_TABLE_STATS_COLLECT
79 #define RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(table, val) \
80 table->stats.n_pkts_in += val
81 #define RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(table, val) \
82 table->stats.n_pkts_lookup_miss += val
86 #define RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(table, val)
87 #define RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(table, val)
98 struct rte_table_hash {
99 struct rte_table_stats stats;
101 /* Input parameters */
106 uint32_t n_buckets_ext;
107 rte_table_hash_op_hash f_hash;
109 uint32_t signature_offset;
113 uint64_t bucket_mask;
114 uint32_t key_size_shl;
115 uint32_t data_size_shl;
116 uint32_t key_stack_tos;
117 uint32_t bkt_ext_stack_tos;
120 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
123 struct bucket *buckets;
124 struct bucket *buckets_ext;
128 uint32_t *bkt_ext_stack;
131 uint8_t memory[0] __rte_cache_aligned;
135 check_params_create(struct rte_table_hash_ext_params *params)
137 uint32_t n_buckets_min;
140 if ((params->key_size == 0) ||
141 (!rte_is_power_of_2(params->key_size))) {
142 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
147 if ((params->n_keys == 0) ||
148 (!rte_is_power_of_2(params->n_keys))) {
149 RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
154 n_buckets_min = (params->n_keys + KEYS_PER_BUCKET - 1) / params->n_keys;
155 if ((params->n_buckets == 0) ||
156 (!rte_is_power_of_2(params->n_keys)) ||
157 (params->n_buckets < n_buckets_min)) {
158 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
163 if (params->f_hash == NULL) {
164 RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
172 rte_table_hash_ext_create(void *params, int socket_id, uint32_t entry_size)
174 struct rte_table_hash_ext_params *p =
176 struct rte_table_hash *t;
177 uint32_t total_size, table_meta_sz;
178 uint32_t bucket_sz, bucket_ext_sz, key_sz;
179 uint32_t key_stack_sz, bkt_ext_stack_sz, data_sz;
180 uint32_t bucket_offset, bucket_ext_offset, key_offset;
181 uint32_t key_stack_offset, bkt_ext_stack_offset, data_offset;
184 /* Check input parameters */
185 if ((check_params_create(p) != 0) ||
186 (!rte_is_power_of_2(entry_size)) ||
187 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
188 (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2)))
191 /* Memory allocation */
192 table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
193 bucket_sz = RTE_CACHE_LINE_ROUNDUP(p->n_buckets * sizeof(struct bucket));
195 RTE_CACHE_LINE_ROUNDUP(p->n_buckets_ext * sizeof(struct bucket));
196 key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
197 key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
199 RTE_CACHE_LINE_ROUNDUP(p->n_buckets_ext * sizeof(uint32_t));
200 data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
201 total_size = table_meta_sz + bucket_sz + bucket_ext_sz + key_sz +
202 key_stack_sz + bkt_ext_stack_sz + data_sz;
204 t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
207 "%s: Cannot allocate %u bytes for hash table\n",
208 __func__, total_size);
211 RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table memory footprint is "
212 "%u bytes\n", __func__, p->key_size, total_size);
214 /* Memory initialization */
215 t->key_size = p->key_size;
216 t->entry_size = entry_size;
217 t->n_keys = p->n_keys;
218 t->n_buckets = p->n_buckets;
219 t->n_buckets_ext = p->n_buckets_ext;
220 t->f_hash = p->f_hash;
222 t->signature_offset = p->signature_offset;
223 t->key_offset = p->key_offset;
226 t->bucket_mask = t->n_buckets - 1;
227 t->key_size_shl = __builtin_ctzl(p->key_size);
228 t->data_size_shl = __builtin_ctzl(entry_size);
232 bucket_ext_offset = bucket_offset + bucket_sz;
233 key_offset = bucket_ext_offset + bucket_ext_sz;
234 key_stack_offset = key_offset + key_sz;
235 bkt_ext_stack_offset = key_stack_offset + key_stack_sz;
236 data_offset = bkt_ext_stack_offset + bkt_ext_stack_sz;
238 t->buckets = (struct bucket *) &t->memory[bucket_offset];
239 t->buckets_ext = (struct bucket *) &t->memory[bucket_ext_offset];
240 t->key_mem = &t->memory[key_offset];
241 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
242 t->bkt_ext_stack = (uint32_t *) &t->memory[bkt_ext_stack_offset];
243 t->data_mem = &t->memory[data_offset];
246 for (i = 0; i < t->n_keys; i++)
247 t->key_stack[i] = t->n_keys - 1 - i;
248 t->key_stack_tos = t->n_keys;
250 /* Bucket ext stack */
251 for (i = 0; i < t->n_buckets_ext; i++)
252 t->bkt_ext_stack[i] = t->n_buckets_ext - 1 - i;
253 t->bkt_ext_stack_tos = t->n_buckets_ext;
259 rte_table_hash_ext_free(void *table)
261 struct rte_table_hash *t = table;
263 /* Check input parameters */
272 rte_table_hash_ext_entry_add(void *table, void *key, void *entry,
273 int *key_found, void **entry_ptr)
275 struct rte_table_hash *t = table;
276 struct bucket *bkt0, *bkt, *bkt_prev;
278 uint32_t bkt_index, i;
280 sig = t->f_hash(key, t->key_size, t->seed);
281 bkt_index = sig & t->bucket_mask;
282 bkt0 = &t->buckets[bkt_index];
283 sig = (sig >> 16) | 1LLU;
285 /* Key is present in the bucket */
286 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
287 for (i = 0; i < KEYS_PER_BUCKET; i++) {
288 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
289 uint32_t bkt_key_index = bkt->key_pos[i];
291 &t->key_mem[bkt_key_index << t->key_size_shl];
293 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
294 t->key_size) == 0)) {
295 uint8_t *data = &t->data_mem[bkt_key_index <<
298 memcpy(data, entry, t->entry_size);
300 *entry_ptr = (void *) data;
305 /* Key is not present in the bucket */
306 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
307 bkt = BUCKET_NEXT(bkt))
308 for (i = 0; i < KEYS_PER_BUCKET; i++) {
309 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
312 uint32_t bkt_key_index;
313 uint8_t *bkt_key, *data;
315 /* Allocate new key */
316 if (t->key_stack_tos == 0) /* No free keys */
319 bkt_key_index = t->key_stack[
322 /* Install new key */
323 bkt_key = &t->key_mem[bkt_key_index <<
325 data = &t->data_mem[bkt_key_index <<
328 bkt->sig[i] = (uint16_t) sig;
329 bkt->key_pos[i] = bkt_key_index;
330 memcpy(bkt_key, key, t->key_size);
331 memcpy(data, entry, t->entry_size);
334 *entry_ptr = (void *) data;
339 /* Bucket full: extend bucket */
340 if ((t->bkt_ext_stack_tos > 0) && (t->key_stack_tos > 0)) {
341 uint32_t bkt_key_index;
342 uint8_t *bkt_key, *data;
344 /* Allocate new bucket ext */
345 bkt_index = t->bkt_ext_stack[--t->bkt_ext_stack_tos];
346 bkt = &t->buckets_ext[bkt_index];
348 /* Chain the new bucket ext */
349 BUCKET_NEXT_SET(bkt_prev, bkt);
350 BUCKET_NEXT_SET_NULL(bkt);
352 /* Allocate new key */
353 bkt_key_index = t->key_stack[--t->key_stack_tos];
354 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
356 data = &t->data_mem[bkt_key_index << t->data_size_shl];
358 /* Install new key into bucket */
359 bkt->sig[0] = (uint16_t) sig;
360 bkt->key_pos[0] = bkt_key_index;
361 memcpy(bkt_key, key, t->key_size);
362 memcpy(data, entry, t->entry_size);
365 *entry_ptr = (void *) data;
373 rte_table_hash_ext_entry_delete(void *table, void *key, int *key_found,
376 struct rte_table_hash *t = table;
377 struct bucket *bkt0, *bkt, *bkt_prev;
379 uint32_t bkt_index, i;
381 sig = t->f_hash(key, t->key_size, t->seed);
382 bkt_index = sig & t->bucket_mask;
383 bkt0 = &t->buckets[bkt_index];
384 sig = (sig >> 16) | 1LLU;
386 /* Key is present in the bucket */
387 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
388 bkt = BUCKET_NEXT(bkt))
389 for (i = 0; i < KEYS_PER_BUCKET; i++) {
390 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
391 uint32_t bkt_key_index = bkt->key_pos[i];
392 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
395 if ((sig == bkt_sig) && (memcmp(key, bkt_key,
396 t->key_size) == 0)) {
397 uint8_t *data = &t->data_mem[bkt_key_index <<
400 /* Uninstall key from bucket */
404 memcpy(entry, data, t->entry_size);
407 t->key_stack[t->key_stack_tos++] =
410 /*Check if bucket is unused */
411 if ((bkt_prev != NULL) &&
412 (bkt->sig[0] == 0) && (bkt->sig[1] == 0) &&
413 (bkt->sig[2] == 0) && (bkt->sig[3] == 0)) {
415 BUCKET_NEXT_COPY(bkt_prev, bkt);
418 memset(bkt, 0, sizeof(struct bucket));
420 /* Free bucket back to buckets ext */
421 bkt_index = bkt - t->buckets_ext;
422 t->bkt_ext_stack[t->bkt_ext_stack_tos++]
430 /* Key is not present in the bucket */
435 static int rte_table_hash_ext_lookup_unoptimized(
437 struct rte_mbuf **pkts,
439 uint64_t *lookup_hit_mask,
442 struct rte_table_hash *t = (struct rte_table_hash *) table;
443 uint64_t pkts_mask_out = 0;
445 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
447 for ( ; pkts_mask; ) {
448 struct bucket *bkt0, *bkt;
449 struct rte_mbuf *pkt;
451 uint64_t pkt_mask, sig;
452 uint32_t pkt_index, bkt_index, i;
454 pkt_index = __builtin_ctzll(pkts_mask);
455 pkt_mask = 1LLU << pkt_index;
456 pkts_mask &= ~pkt_mask;
458 pkt = pkts[pkt_index];
459 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
460 sig = (uint64_t) t->f_hash(key, t->key_size, t->seed);
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; \
644 uint32_t key_offset = t->key_offset; \
646 pkt00_index = __builtin_ctzll(pkts_mask); \
647 pkt00_mask = 1LLU << pkt00_index; \
648 pkts_mask &= ~pkt00_mask; \
649 mbuf00 = pkts[pkt00_index]; \
651 pkt01_index = __builtin_ctzll(pkts_mask); \
652 pkt01_mask = 1LLU << pkt01_index; \
653 pkts_mask &= ~pkt01_mask; \
654 mbuf01 = pkts[pkt01_index]; \
656 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
657 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
660 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
663 uint64_t pkt00_mask, pkt01_mask; \
664 struct rte_mbuf *mbuf00, *mbuf01; \
665 uint32_t key_offset = t->key_offset; \
667 pkt00_index = __builtin_ctzll(pkts_mask); \
668 pkt00_mask = 1LLU << pkt00_index; \
669 pkts_mask &= ~pkt00_mask; \
670 mbuf00 = pkts[pkt00_index]; \
672 pkt01_index = __builtin_ctzll(pkts_mask); \
673 if (pkts_mask == 0) \
674 pkt01_index = pkt00_index; \
675 pkt01_mask = 1LLU << pkt01_index; \
676 pkts_mask &= ~pkt01_mask; \
677 mbuf01 = pkts[pkt01_index]; \
679 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
680 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
683 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index) \
685 struct grinder *g10, *g11; \
686 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
687 struct rte_mbuf *mbuf10, *mbuf11; \
688 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
689 uint8_t *key10, *key11; \
690 uint64_t bucket_mask = t->bucket_mask; \
691 rte_table_hash_op_hash f_hash = t->f_hash; \
692 uint64_t seed = t->seed; \
693 uint32_t key_size = t->key_size; \
694 uint32_t key_offset = t->key_offset; \
696 mbuf10 = pkts[pkt10_index]; \
697 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset); \
698 sig10 = (uint64_t) f_hash(key10, key_size, seed); \
699 bkt10_index = sig10 & bucket_mask; \
700 bkt10 = &buckets[bkt10_index]; \
702 mbuf11 = pkts[pkt11_index]; \
703 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset); \
704 sig11 = (uint64_t) f_hash(key11, key_size, seed); \
705 bkt11_index = sig11 & bucket_mask; \
706 bkt11 = &buckets[bkt11_index]; \
708 rte_prefetch0(bkt10); \
709 rte_prefetch0(bkt11); \
711 g10 = &g[pkt10_index]; \
715 g11 = &g[pkt11_index]; \
720 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
722 struct grinder *g20, *g21; \
723 uint64_t sig20, sig21; \
724 struct bucket *bkt20, *bkt21; \
725 uint8_t *key20, *key21, *key_mem = t->key_mem; \
726 uint64_t match20, match21, match_many20, match_many21; \
727 uint64_t match_pos20, match_pos21; \
728 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
730 g20 = &g[pkt20_index]; \
733 sig20 = (sig20 >> 16) | 1LLU; \
734 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
735 match20 <<= pkt20_index; \
736 match_many20 |= BUCKET_NEXT_VALID(bkt20); \
737 match_many20 <<= pkt20_index; \
738 key20_index = bkt20->key_pos[match_pos20]; \
739 key20 = &key_mem[key20_index << key_size_shl]; \
741 g21 = &g[pkt21_index]; \
744 sig21 = (sig21 >> 16) | 1LLU; \
745 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
746 match21 <<= pkt21_index; \
747 match_many21 |= BUCKET_NEXT_VALID(bkt21); \
748 match_many21 <<= pkt21_index; \
749 key21_index = bkt21->key_pos[match_pos21]; \
750 key21 = &key_mem[key21_index << key_size_shl]; \
752 rte_prefetch0(key20); \
753 rte_prefetch0(key21); \
755 pkts_mask_match_many |= match_many20 | match_many21; \
757 g20->match = match20; \
758 g20->key_index = key20_index; \
760 g21->match = match21; \
761 g21->key_index = key21_index; \
764 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
767 struct grinder *g30, *g31; \
768 struct rte_mbuf *mbuf30, *mbuf31; \
769 uint8_t *key30, *key31, *key_mem = t->key_mem; \
770 uint8_t *data30, *data31, *data_mem = t->data_mem; \
771 uint64_t match30, match31, match_key30, match_key31, match_keys;\
772 uint32_t key30_index, key31_index; \
773 uint32_t key_size_shl = t->key_size_shl; \
774 uint32_t data_size_shl = t->data_size_shl; \
776 mbuf30 = pkts[pkt30_index]; \
777 g30 = &g[pkt30_index]; \
778 match30 = g30->match; \
779 key30_index = g30->key_index; \
780 key30 = &key_mem[key30_index << key_size_shl]; \
781 lookup_cmp_key(mbuf30, key30, match_key30, t); \
782 match_key30 <<= pkt30_index; \
783 match_key30 &= match30; \
784 data30 = &data_mem[key30_index << data_size_shl]; \
785 entries[pkt30_index] = data30; \
787 mbuf31 = pkts[pkt31_index]; \
788 g31 = &g[pkt31_index]; \
789 match31 = g31->match; \
790 key31_index = g31->key_index; \
791 key31 = &key_mem[key31_index << key_size_shl]; \
792 lookup_cmp_key(mbuf31, key31, match_key31, t); \
793 match_key31 <<= pkt31_index; \
794 match_key31 &= match31; \
795 data31 = &data_mem[key31_index << data_size_shl]; \
796 entries[pkt31_index] = data31; \
798 rte_prefetch0(data30); \
799 rte_prefetch0(data31); \
801 match_keys = match_key30 | match_key31; \
802 pkts_mask_out |= match_keys; \
806 * The lookup function implements a 4-stage pipeline, with each stage processing
807 * two different packets. The purpose of pipelined implementation is to hide the
808 * latency of prefetching the data structures and loosen the data dependency
809 * between instructions.
811 * p00 _______ p10 _______ p20 _______ p30 _______
812 *----->| |----->| |----->| |----->| |----->
813 * | 0 | | 1 | | 2 | | 3 |
814 *----->|_______|----->|_______|----->|_______|----->|_______|----->
817 * The naming convention is:
818 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
821 static int rte_table_hash_ext_lookup(
823 struct rte_mbuf **pkts,
825 uint64_t *lookup_hit_mask,
828 struct rte_table_hash *t = (struct rte_table_hash *) table;
829 struct grinder *g = t->grinders;
830 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
831 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
832 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
835 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
836 RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(t, n_pkts_in);
838 /* Cannot run the pipeline with less than 7 packets */
839 if (__builtin_popcountll(pkts_mask) < 7) {
840 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
841 pkts_mask, lookup_hit_mask, entries);
842 RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in -
843 __builtin_popcountll(*lookup_hit_mask));
847 /* Pipeline stage 0 */
848 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
851 pkt10_index = pkt00_index;
852 pkt11_index = pkt01_index;
854 /* Pipeline stage 0 */
855 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
857 /* Pipeline stage 1 */
858 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
861 pkt20_index = pkt10_index;
862 pkt21_index = pkt11_index;
863 pkt10_index = pkt00_index;
864 pkt11_index = pkt01_index;
866 /* Pipeline stage 0 */
867 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
869 /* Pipeline stage 1 */
870 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
872 /* Pipeline stage 2 */
873 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
879 for ( ; pkts_mask; ) {
881 pkt30_index = pkt20_index;
882 pkt31_index = pkt21_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_with_odd_support(t, g, pkts, pkts_mask,
890 pkt00_index, pkt01_index);
892 /* Pipeline stage 1 */
893 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
895 /* Pipeline stage 2 */
896 lookup2_stage2(t, g, pkt20_index, pkt21_index,
897 pkts_mask_match_many);
899 /* Pipeline stage 3 */
900 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
901 pkts_mask_out, entries);
905 pkt30_index = pkt20_index;
906 pkt31_index = pkt21_index;
907 pkt20_index = pkt10_index;
908 pkt21_index = pkt11_index;
909 pkt10_index = pkt00_index;
910 pkt11_index = pkt01_index;
912 /* Pipeline stage 1 */
913 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
915 /* Pipeline stage 2 */
916 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
918 /* Pipeline stage 3 */
919 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
923 pkt30_index = pkt20_index;
924 pkt31_index = pkt21_index;
925 pkt20_index = pkt10_index;
926 pkt21_index = pkt11_index;
928 /* Pipeline stage 2 */
929 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
931 /* Pipeline stage 3 */
932 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
936 pkt30_index = pkt20_index;
937 pkt31_index = pkt21_index;
939 /* Pipeline stage 3 */
940 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
944 pkts_mask_match_many &= ~pkts_mask_out;
945 if (pkts_mask_match_many) {
946 uint64_t pkts_mask_out_slow = 0;
948 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
949 pkts_mask_match_many, &pkts_mask_out_slow, entries);
950 pkts_mask_out |= pkts_mask_out_slow;
953 *lookup_hit_mask = pkts_mask_out;
954 RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
959 rte_table_hash_ext_stats_read(void *table, struct rte_table_stats *stats, int clear)
961 struct rte_table_hash *t = table;
964 memcpy(stats, &t->stats, sizeof(t->stats));
967 memset(&t->stats, 0, sizeof(t->stats));
972 struct rte_table_ops rte_table_hash_ext_ops = {
973 .f_create = rte_table_hash_ext_create,
974 .f_free = rte_table_hash_ext_free,
975 .f_add = rte_table_hash_ext_entry_add,
976 .f_delete = rte_table_hash_ext_entry_delete,
978 .f_delete_bulk = NULL,
979 .f_lookup = rte_table_hash_ext_lookup,
980 .f_stats = rte_table_hash_ext_stats_read,