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.
36 #include <rte_common.h>
38 #include <rte_memory.h>
39 #include <rte_malloc.h>
42 #include "rte_table_hash.h"
47 #define KEYS_PER_BUCKET 4
49 #define RTE_BUCKET_ENTRY_VALID 0x1LLU
51 #ifdef RTE_TABLE_STATS_COLLECT
53 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val) \
54 table->stats.n_pkts_in += val
55 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val) \
56 table->stats.n_pkts_lookup_miss += val
60 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val)
61 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val)
65 struct rte_bucket_4_16 {
67 uint64_t signature[4 + 1];
69 struct rte_bucket_4_16 *next;
79 struct rte_table_hash {
80 struct rte_table_stats stats;
82 /* Input parameters */
89 rte_table_hash_op_hash f_hash;
92 /* Extendible buckets */
93 uint32_t n_buckets_ext;
98 uint8_t memory[0] __rte_cache_aligned;
102 keycmp(void *a, void *b, void *b_mask)
104 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
106 return (a64[0] != (b64[0] & b_mask64[0])) ||
107 (a64[1] != (b64[1] & b_mask64[1]));
111 keycpy(void *dst, void *src, void *src_mask)
113 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
115 dst64[0] = src64[0] & src_mask64[0];
116 dst64[1] = src64[1] & src_mask64[1];
120 check_params_create(struct rte_table_hash_params *params)
123 if (params->name == NULL) {
124 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
129 if (params->key_size != KEY_SIZE) {
130 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
135 if (params->n_keys == 0) {
136 RTE_LOG(ERR, TABLE, "%s: n_keys is zero\n", __func__);
141 if ((params->n_buckets == 0) ||
142 (!rte_is_power_of_2(params->n_buckets))) {
143 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
148 if (params->f_hash == NULL) {
149 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
158 rte_table_hash_create_key16_lru(void *params,
162 struct rte_table_hash_params *p = params;
163 struct rte_table_hash *f;
164 uint64_t bucket_size, total_size;
165 uint32_t n_buckets, i;
167 /* Check input parameters */
168 if ((check_params_create(p) != 0) ||
169 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
170 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
176 * Objective: Pick the number of buckets (n_buckets) so that there a chance
177 * to store n_keys keys in the table.
179 * Note: Since the buckets do not get extended, it is not possible to
180 * guarantee that n_keys keys can be stored in the table at any time. In the
181 * worst case scenario when all the n_keys fall into the same bucket, only
182 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
183 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
184 * n_keys to n_buckets ratio.
186 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
188 n_buckets = rte_align32pow2(
189 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
190 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
192 /* Memory allocation */
193 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_16) +
194 KEYS_PER_BUCKET * entry_size);
195 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size;
197 if (total_size > SIZE_MAX) {
198 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
199 "for hash table %s\n",
200 __func__, total_size, p->name);
204 f = rte_zmalloc_socket(p->name,
209 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
210 "for hash table %s\n",
211 __func__, total_size, p->name);
214 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
215 "is %" PRIu64 " bytes\n",
216 __func__, p->name, total_size);
218 /* Memory initialization */
219 f->n_buckets = n_buckets;
220 f->key_size = KEY_SIZE;
221 f->entry_size = entry_size;
222 f->bucket_size = bucket_size;
223 f->key_offset = p->key_offset;
224 f->f_hash = p->f_hash;
227 if (p->key_mask != NULL) {
228 f->key_mask[0] = ((uint64_t *)p->key_mask)[0];
229 f->key_mask[1] = ((uint64_t *)p->key_mask)[1];
231 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
232 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
235 for (i = 0; i < n_buckets; i++) {
236 struct rte_bucket_4_16 *bucket;
238 bucket = (struct rte_bucket_4_16 *) &f->memory[i *
247 rte_table_hash_free_key16_lru(void *table)
249 struct rte_table_hash *f = table;
251 /* Check input parameters */
253 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
262 rte_table_hash_entry_add_key16_lru(
269 struct rte_table_hash *f = table;
270 struct rte_bucket_4_16 *bucket;
271 uint64_t signature, pos;
272 uint32_t bucket_index, i;
274 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
275 bucket_index = signature & (f->n_buckets - 1);
276 bucket = (struct rte_bucket_4_16 *)
277 &f->memory[bucket_index * f->bucket_size];
278 signature |= RTE_BUCKET_ENTRY_VALID;
280 /* Key is present in the bucket */
281 for (i = 0; i < 4; i++) {
282 uint64_t bucket_signature = bucket->signature[i];
283 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
285 if ((bucket_signature == signature) &&
286 (keycmp(bucket_key, key, f->key_mask) == 0)) {
287 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
289 memcpy(bucket_data, entry, f->entry_size);
290 lru_update(bucket, i);
292 *entry_ptr = (void *) bucket_data;
297 /* Key is not present in the bucket */
298 for (i = 0; i < 4; i++) {
299 uint64_t bucket_signature = bucket->signature[i];
300 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
302 if (bucket_signature == 0) {
303 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
305 bucket->signature[i] = signature;
306 keycpy(bucket_key, key, f->key_mask);
307 memcpy(bucket_data, entry, f->entry_size);
308 lru_update(bucket, i);
310 *entry_ptr = (void *) bucket_data;
316 /* Bucket full: replace LRU entry */
317 pos = lru_pos(bucket);
318 bucket->signature[pos] = signature;
319 keycpy(&bucket->key[pos], key, f->key_mask);
320 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
321 lru_update(bucket, pos);
323 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
329 rte_table_hash_entry_delete_key16_lru(
335 struct rte_table_hash *f = table;
336 struct rte_bucket_4_16 *bucket;
338 uint32_t bucket_index, i;
340 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
341 bucket_index = signature & (f->n_buckets - 1);
342 bucket = (struct rte_bucket_4_16 *)
343 &f->memory[bucket_index * f->bucket_size];
344 signature |= RTE_BUCKET_ENTRY_VALID;
346 /* Key is present in the bucket */
347 for (i = 0; i < 4; i++) {
348 uint64_t bucket_signature = bucket->signature[i];
349 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
351 if ((bucket_signature == signature) &&
352 (keycmp(bucket_key, key, f->key_mask) == 0)) {
353 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
355 bucket->signature[i] = 0;
358 memcpy(entry, bucket_data, f->entry_size);
363 /* Key is not present in the bucket */
369 rte_table_hash_create_key16_ext(void *params,
373 struct rte_table_hash_params *p = params;
374 struct rte_table_hash *f;
375 uint64_t bucket_size, stack_size, total_size;
376 uint32_t n_buckets_ext, i;
378 /* Check input parameters */
379 if ((check_params_create(p) != 0) ||
380 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
381 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
387 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
388 * it is guaranteed that n_keys keys can be stored in the table at any time.
390 * The worst case scenario takes place when all the n_keys keys fall into
391 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
392 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
393 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
394 * into a different bucket. This case defeats the purpose of the hash table.
395 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
397 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
399 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
401 /* Memory allocation */
402 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_16) +
403 KEYS_PER_BUCKET * entry_size);
404 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
405 total_size = sizeof(struct rte_table_hash) +
406 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size;
407 if (total_size > SIZE_MAX) {
408 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
409 "for hash table %s\n",
410 __func__, total_size, p->name);
414 f = rte_zmalloc_socket(p->name,
419 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
420 "for hash table %s\n",
421 __func__, total_size, p->name);
424 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
425 "is %" PRIu64 " bytes\n",
426 __func__, p->name, total_size);
428 /* Memory initialization */
429 f->n_buckets = p->n_buckets;
430 f->key_size = KEY_SIZE;
431 f->entry_size = entry_size;
432 f->bucket_size = bucket_size;
433 f->key_offset = p->key_offset;
434 f->f_hash = p->f_hash;
437 f->n_buckets_ext = n_buckets_ext;
438 f->stack_pos = n_buckets_ext;
439 f->stack = (uint32_t *)
440 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size];
442 if (p->key_mask != NULL) {
443 f->key_mask[0] = (((uint64_t *)p->key_mask)[0]);
444 f->key_mask[1] = (((uint64_t *)p->key_mask)[1]);
446 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
447 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
450 for (i = 0; i < n_buckets_ext; i++)
457 rte_table_hash_free_key16_ext(void *table)
459 struct rte_table_hash *f = table;
461 /* Check input parameters */
463 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
472 rte_table_hash_entry_add_key16_ext(
479 struct rte_table_hash *f = table;
480 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
482 uint32_t bucket_index, i;
484 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
485 bucket_index = signature & (f->n_buckets - 1);
486 bucket0 = (struct rte_bucket_4_16 *)
487 &f->memory[bucket_index * f->bucket_size];
488 signature |= RTE_BUCKET_ENTRY_VALID;
490 /* Key is present in the bucket */
491 for (bucket = bucket0; bucket != NULL; bucket = bucket->next)
492 for (i = 0; i < 4; i++) {
493 uint64_t bucket_signature = bucket->signature[i];
494 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
496 if ((bucket_signature == signature) &&
497 (keycmp(bucket_key, key, f->key_mask) == 0)) {
498 uint8_t *bucket_data = &bucket->data[i *
501 memcpy(bucket_data, entry, f->entry_size);
503 *entry_ptr = (void *) bucket_data;
508 /* Key is not present in the bucket */
509 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
510 bucket_prev = bucket, bucket = bucket->next)
511 for (i = 0; i < 4; i++) {
512 uint64_t bucket_signature = bucket->signature[i];
513 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
515 if (bucket_signature == 0) {
516 uint8_t *bucket_data = &bucket->data[i *
519 bucket->signature[i] = signature;
520 keycpy(bucket_key, key, f->key_mask);
521 memcpy(bucket_data, entry, f->entry_size);
523 *entry_ptr = (void *) bucket_data;
529 /* Bucket full: extend bucket */
530 if (f->stack_pos > 0) {
531 bucket_index = f->stack[--f->stack_pos];
533 bucket = (struct rte_bucket_4_16 *) &f->memory[(f->n_buckets +
534 bucket_index) * f->bucket_size];
535 bucket_prev->next = bucket;
536 bucket_prev->next_valid = 1;
538 bucket->signature[0] = signature;
539 keycpy(&bucket->key[0], key, f->key_mask);
540 memcpy(&bucket->data[0], entry, f->entry_size);
542 *entry_ptr = (void *) &bucket->data[0];
550 rte_table_hash_entry_delete_key16_ext(
556 struct rte_table_hash *f = table;
557 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
559 uint32_t bucket_index, i;
561 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
562 bucket_index = signature & (f->n_buckets - 1);
563 bucket0 = (struct rte_bucket_4_16 *)
564 &f->memory[bucket_index * f->bucket_size];
565 signature |= RTE_BUCKET_ENTRY_VALID;
567 /* Key is present in the bucket */
568 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
569 bucket_prev = bucket, bucket = bucket->next)
570 for (i = 0; i < 4; i++) {
571 uint64_t bucket_signature = bucket->signature[i];
572 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
574 if ((bucket_signature == signature) &&
575 (keycmp(bucket_key, key, f->key_mask) == 0)) {
576 uint8_t *bucket_data = &bucket->data[i *
579 bucket->signature[i] = 0;
582 memcpy(entry, bucket_data, f->entry_size);
584 if ((bucket->signature[0] == 0) &&
585 (bucket->signature[1] == 0) &&
586 (bucket->signature[2] == 0) &&
587 (bucket->signature[3] == 0) &&
588 (bucket_prev != NULL)) {
589 bucket_prev->next = bucket->next;
590 bucket_prev->next_valid =
594 sizeof(struct rte_bucket_4_16));
595 bucket_index = (((uint8_t *)bucket -
596 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
597 f->stack[f->stack_pos++] = bucket_index;
604 /* Key is not present in the bucket */
609 #define lookup_key16_cmp(key_in, bucket, pos, f) \
611 uint64_t xor[4][2], or[4], signature[4], k[2]; \
613 k[0] = key_in[0] & f->key_mask[0]; \
614 k[1] = key_in[1] & f->key_mask[1]; \
615 signature[0] = (~bucket->signature[0]) & 1; \
616 signature[1] = (~bucket->signature[1]) & 1; \
617 signature[2] = (~bucket->signature[2]) & 1; \
618 signature[3] = (~bucket->signature[3]) & 1; \
620 xor[0][0] = k[0] ^ bucket->key[0][0]; \
621 xor[0][1] = k[1] ^ bucket->key[0][1]; \
623 xor[1][0] = k[0] ^ bucket->key[1][0]; \
624 xor[1][1] = k[1] ^ bucket->key[1][1]; \
626 xor[2][0] = k[0] ^ bucket->key[2][0]; \
627 xor[2][1] = k[1] ^ bucket->key[2][1]; \
629 xor[3][0] = k[0] ^ bucket->key[3][0]; \
630 xor[3][1] = k[1] ^ bucket->key[3][1]; \
632 or[0] = xor[0][0] | xor[0][1] | signature[0]; \
633 or[1] = xor[1][0] | xor[1][1] | signature[1]; \
634 or[2] = xor[2][0] | xor[2][1] | signature[2]; \
635 or[3] = xor[3][0] | xor[3][1] | signature[3]; \
648 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
651 uint32_t key_offset = f->key_offset;\
653 pkt0_index = __builtin_ctzll(pkts_mask); \
654 pkt_mask = 1LLU << pkt0_index; \
655 pkts_mask &= ~pkt_mask; \
657 mbuf0 = pkts[pkt0_index]; \
658 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset));\
661 #define lookup1_stage1(mbuf1, bucket1, f) \
664 uint64_t signature = 0; \
665 uint32_t bucket_index; \
667 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\
668 signature = f->f_hash(key, f->key_mask, KEY_SIZE, f->seed); \
670 bucket_index = signature & (f->n_buckets - 1); \
671 bucket1 = (struct rte_bucket_4_16 *) \
672 &f->memory[bucket_index * f->bucket_size]; \
673 rte_prefetch0(bucket1); \
674 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
677 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
678 pkts_mask_out, entries, f) \
685 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
686 lookup_key16_cmp(key, bucket2, pos, f); \
688 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
689 pkts_mask_out |= pkt_mask; \
691 a = (void *) &bucket2->data[pos * f->entry_size]; \
693 entries[pkt2_index] = a; \
694 lru_update(bucket2, pos); \
697 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out, entries, \
698 buckets_mask, buckets, keys, f) \
700 struct rte_bucket_4_16 *bucket_next; \
702 uint64_t pkt_mask, bucket_mask; \
706 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
707 lookup_key16_cmp(key, bucket2, pos, f); \
709 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
710 pkts_mask_out |= pkt_mask; \
712 a = (void *) &bucket2->data[pos * f->entry_size]; \
714 entries[pkt2_index] = a; \
716 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
717 buckets_mask |= bucket_mask; \
718 bucket_next = bucket2->next; \
719 buckets[pkt2_index] = bucket_next; \
720 keys[pkt2_index] = key; \
723 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
726 struct rte_bucket_4_16 *bucket, *bucket_next; \
728 uint64_t pkt_mask, bucket_mask; \
732 bucket = buckets[pkt_index]; \
733 key = keys[pkt_index]; \
734 lookup_key16_cmp(key, bucket, pos, f); \
736 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
737 pkts_mask_out |= pkt_mask; \
739 a = (void *) &bucket->data[pos * f->entry_size]; \
741 entries[pkt_index] = a; \
743 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
744 buckets_mask |= bucket_mask; \
745 bucket_next = bucket->next; \
746 rte_prefetch0(bucket_next); \
747 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
748 buckets[pkt_index] = bucket_next; \
749 keys[pkt_index] = key; \
752 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
753 pkts, pkts_mask, f) \
755 uint64_t pkt00_mask, pkt01_mask; \
756 uint32_t key_offset = f->key_offset; \
758 pkt00_index = __builtin_ctzll(pkts_mask); \
759 pkt00_mask = 1LLU << pkt00_index; \
760 pkts_mask &= ~pkt00_mask; \
762 mbuf00 = pkts[pkt00_index]; \
763 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
765 pkt01_index = __builtin_ctzll(pkts_mask); \
766 pkt01_mask = 1LLU << pkt01_index; \
767 pkts_mask &= ~pkt01_mask; \
769 mbuf01 = pkts[pkt01_index]; \
770 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
773 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
774 mbuf00, mbuf01, pkts, pkts_mask, f) \
776 uint64_t pkt00_mask, pkt01_mask; \
777 uint32_t key_offset = f->key_offset; \
779 pkt00_index = __builtin_ctzll(pkts_mask); \
780 pkt00_mask = 1LLU << pkt00_index; \
781 pkts_mask &= ~pkt00_mask; \
783 mbuf00 = pkts[pkt00_index]; \
784 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset)); \
786 pkt01_index = __builtin_ctzll(pkts_mask); \
787 if (pkts_mask == 0) \
788 pkt01_index = pkt00_index; \
789 pkt01_mask = 1LLU << pkt01_index; \
790 pkts_mask &= ~pkt01_mask; \
792 mbuf01 = pkts[pkt01_index]; \
793 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset)); \
796 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
798 uint64_t *key10, *key11; \
799 uint64_t signature10, signature11; \
800 uint32_t bucket10_index, bucket11_index; \
802 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, f->key_offset);\
803 signature10 = f->f_hash(key10, f->key_mask, KEY_SIZE, f->seed);\
804 bucket10_index = signature10 & (f->n_buckets - 1); \
805 bucket10 = (struct rte_bucket_4_16 *) \
806 &f->memory[bucket10_index * f->bucket_size]; \
807 rte_prefetch0(bucket10); \
808 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
810 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, f->key_offset);\
811 signature11 = f->f_hash(key11, f->key_mask, KEY_SIZE, f->seed);\
812 bucket11_index = signature11 & (f->n_buckets - 1); \
813 bucket11 = (struct rte_bucket_4_16 *) \
814 &f->memory[bucket11_index * f->bucket_size]; \
815 rte_prefetch0(bucket11); \
816 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
819 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
820 bucket20, bucket21, pkts_mask_out, entries, f) \
823 uint64_t pkt20_mask, pkt21_mask; \
824 uint64_t *key20, *key21; \
825 uint32_t pos20, pos21; \
827 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
828 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
830 lookup_key16_cmp(key20, bucket20, pos20, f); \
831 lookup_key16_cmp(key21, bucket21, pos21, f); \
833 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
834 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
835 pkts_mask_out |= pkt20_mask | pkt21_mask; \
837 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
838 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
839 rte_prefetch0(a20); \
840 rte_prefetch0(a21); \
841 entries[pkt20_index] = a20; \
842 entries[pkt21_index] = a21; \
843 lru_update(bucket20, pos20); \
844 lru_update(bucket21, pos21); \
847 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
848 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f) \
850 struct rte_bucket_4_16 *bucket20_next, *bucket21_next; \
852 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
853 uint64_t *key20, *key21; \
854 uint32_t pos20, pos21; \
856 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
857 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
859 lookup_key16_cmp(key20, bucket20, pos20, f); \
860 lookup_key16_cmp(key21, bucket21, pos21, f); \
862 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
863 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
864 pkts_mask_out |= pkt20_mask | pkt21_mask; \
866 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
867 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
868 rte_prefetch0(a20); \
869 rte_prefetch0(a21); \
870 entries[pkt20_index] = a20; \
871 entries[pkt21_index] = a21; \
873 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
874 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
875 buckets_mask |= bucket20_mask | bucket21_mask; \
876 bucket20_next = bucket20->next; \
877 bucket21_next = bucket21->next; \
878 buckets[pkt20_index] = bucket20_next; \
879 buckets[pkt21_index] = bucket21_next; \
880 keys[pkt20_index] = key20; \
881 keys[pkt21_index] = key21; \
885 rte_table_hash_lookup_key16_lru(
887 struct rte_mbuf **pkts,
889 uint64_t *lookup_hit_mask,
892 struct rte_table_hash *f = (struct rte_table_hash *) table;
893 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
894 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
895 uint32_t pkt00_index, pkt01_index, pkt10_index;
896 uint32_t pkt11_index, pkt20_index, pkt21_index;
897 uint64_t pkts_mask_out = 0;
899 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
901 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
903 /* Cannot run the pipeline with less than 5 packets */
904 if (__builtin_popcountll(pkts_mask) < 5) {
905 for ( ; pkts_mask; ) {
906 struct rte_bucket_4_16 *bucket;
907 struct rte_mbuf *mbuf;
910 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
911 lookup1_stage1(mbuf, bucket, f);
912 lookup1_stage2_lru(pkt_index, mbuf, bucket,
913 pkts_mask_out, entries, f);
916 *lookup_hit_mask = pkts_mask_out;
917 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
918 __builtin_popcountll(pkts_mask_out));
926 /* Pipeline stage 0 */
927 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
933 pkt10_index = pkt00_index;
934 pkt11_index = pkt01_index;
936 /* Pipeline stage 0 */
937 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
940 /* Pipeline stage 1 */
941 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
947 for ( ; pkts_mask; ) {
955 pkt20_index = pkt10_index;
956 pkt21_index = pkt11_index;
957 pkt10_index = pkt00_index;
958 pkt11_index = pkt01_index;
960 /* Pipeline stage 0 */
961 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
962 mbuf00, mbuf01, pkts, pkts_mask, f);
964 /* Pipeline stage 1 */
965 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
967 /* Pipeline stage 2 */
968 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
969 bucket20, bucket21, pkts_mask_out, entries, f);
983 pkt20_index = pkt10_index;
984 pkt21_index = pkt11_index;
985 pkt10_index = pkt00_index;
986 pkt11_index = pkt01_index;
988 /* Pipeline stage 1 */
989 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
991 /* Pipeline stage 2 */
992 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
993 bucket20, bucket21, pkts_mask_out, entries, f);
1000 pkt20_index = pkt10_index;
1001 pkt21_index = pkt11_index;
1003 /* Pipeline stage 2 */
1004 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
1005 bucket20, bucket21, pkts_mask_out, entries, f);
1007 *lookup_hit_mask = pkts_mask_out;
1008 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
1009 __builtin_popcountll(pkts_mask_out));
1014 rte_table_hash_lookup_key16_ext(
1016 struct rte_mbuf **pkts,
1018 uint64_t *lookup_hit_mask,
1021 struct rte_table_hash *f = (struct rte_table_hash *) table;
1022 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
1023 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
1024 uint32_t pkt00_index, pkt01_index, pkt10_index;
1025 uint32_t pkt11_index, pkt20_index, pkt21_index;
1026 uint64_t pkts_mask_out = 0, buckets_mask = 0;
1027 struct rte_bucket_4_16 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
1028 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
1030 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
1032 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
1034 /* Cannot run the pipeline with less than 5 packets */
1035 if (__builtin_popcountll(pkts_mask) < 5) {
1036 for ( ; pkts_mask; ) {
1037 struct rte_bucket_4_16 *bucket;
1038 struct rte_mbuf *mbuf;
1041 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
1042 lookup1_stage1(mbuf, bucket, f);
1043 lookup1_stage2_ext(pkt_index, mbuf, bucket,
1044 pkts_mask_out, entries, buckets_mask,
1048 goto grind_next_buckets;
1055 /* Pipeline stage 0 */
1056 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1062 pkt10_index = pkt00_index;
1063 pkt11_index = pkt01_index;
1065 /* Pipeline stage 0 */
1066 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1069 /* Pipeline stage 1 */
1070 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1076 for ( ; pkts_mask; ) {
1078 bucket20 = bucket10;
1079 bucket21 = bucket11;
1084 pkt20_index = pkt10_index;
1085 pkt21_index = pkt11_index;
1086 pkt10_index = pkt00_index;
1087 pkt11_index = pkt01_index;
1089 /* Pipeline stage 0 */
1090 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1091 mbuf00, mbuf01, pkts, pkts_mask, f);
1093 /* Pipeline stage 1 */
1094 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1096 /* Pipeline stage 2 */
1097 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1098 bucket20, bucket21, pkts_mask_out, entries,
1099 buckets_mask, buckets, keys, f);
1107 bucket20 = bucket10;
1108 bucket21 = bucket11;
1113 pkt20_index = pkt10_index;
1114 pkt21_index = pkt11_index;
1115 pkt10_index = pkt00_index;
1116 pkt11_index = pkt01_index;
1118 /* Pipeline stage 1 */
1119 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1121 /* Pipeline stage 2 */
1122 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1123 bucket20, bucket21, pkts_mask_out, entries,
1124 buckets_mask, buckets, keys, f);
1127 bucket20 = bucket10;
1128 bucket21 = bucket11;
1131 pkt20_index = pkt10_index;
1132 pkt21_index = pkt11_index;
1134 /* Pipeline stage 2 */
1135 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1136 bucket20, bucket21, pkts_mask_out, entries,
1137 buckets_mask, buckets, keys, f);
1140 /* Grind next buckets */
1141 for ( ; buckets_mask; ) {
1142 uint64_t buckets_mask_next = 0;
1144 for ( ; buckets_mask; ) {
1148 pkt_index = __builtin_ctzll(buckets_mask);
1149 pkt_mask = 1LLU << pkt_index;
1150 buckets_mask &= ~pkt_mask;
1152 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1153 entries, buckets_mask_next, f);
1156 buckets_mask = buckets_mask_next;
1159 *lookup_hit_mask = pkts_mask_out;
1160 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
1161 __builtin_popcountll(pkts_mask_out));
1166 rte_table_hash_key16_stats_read(void *table, struct rte_table_stats *stats, int clear)
1168 struct rte_table_hash *t = table;
1171 memcpy(stats, &t->stats, sizeof(t->stats));
1174 memset(&t->stats, 0, sizeof(t->stats));
1179 struct rte_table_ops rte_table_hash_key16_lru_ops = {
1180 .f_create = rte_table_hash_create_key16_lru,
1181 .f_free = rte_table_hash_free_key16_lru,
1182 .f_add = rte_table_hash_entry_add_key16_lru,
1183 .f_delete = rte_table_hash_entry_delete_key16_lru,
1185 .f_delete_bulk = NULL,
1186 .f_lookup = rte_table_hash_lookup_key16_lru,
1187 .f_stats = rte_table_hash_key16_stats_read,
1190 struct rte_table_ops rte_table_hash_key16_ext_ops = {
1191 .f_create = rte_table_hash_create_key16_ext,
1192 .f_free = rte_table_hash_free_key16_ext,
1193 .f_add = rte_table_hash_entry_add_key16_ext,
1194 .f_delete = rte_table_hash_entry_delete_key16_ext,
1196 .f_delete_bulk = NULL,
1197 .f_lookup = rte_table_hash_lookup_key16_ext,
1198 .f_stats = rte_table_hash_key16_stats_read,