1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_malloc.h>
11 #include "rte_table_hash.h"
16 #define KEYS_PER_BUCKET 4
18 #define RTE_BUCKET_ENTRY_VALID 0x1LLU
20 #ifdef RTE_TABLE_STATS_COLLECT
22 #define RTE_TABLE_HASH_KEY32_STATS_PKTS_IN_ADD(table, val) \
23 table->stats.n_pkts_in += val
24 #define RTE_TABLE_HASH_KEY32_STATS_PKTS_LOOKUP_MISS(table, val) \
25 table->stats.n_pkts_lookup_miss += val
29 #define RTE_TABLE_HASH_KEY32_STATS_PKTS_IN_ADD(table, val)
30 #define RTE_TABLE_HASH_KEY32_STATS_PKTS_LOOKUP_MISS(table, val)
35 struct rte_bucket_4_32 {
37 uint64_t signature[4 + 1];
39 struct rte_bucket_4_32 *next;
42 /* Cache lines 1 and 2 */
49 struct rte_bucket_4_32 {
51 uint64_t signature[4 + 1];
53 struct rte_bucket_4_32 *next;
57 /* Cache lines 1 and 2 */
65 struct rte_table_hash {
66 struct rte_table_stats stats;
68 /* Input parameters */
75 rte_table_hash_op_hash f_hash;
78 /* Extendible buckets */
79 uint32_t n_buckets_ext;
84 uint8_t memory[0] __rte_cache_aligned;
88 keycmp(void *a, void *b, void *b_mask)
90 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
92 return (a64[0] != (b64[0] & b_mask64[0])) ||
93 (a64[1] != (b64[1] & b_mask64[1])) ||
94 (a64[2] != (b64[2] & b_mask64[2])) ||
95 (a64[3] != (b64[3] & b_mask64[3]));
99 keycpy(void *dst, void *src, void *src_mask)
101 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
103 dst64[0] = src64[0] & src_mask64[0];
104 dst64[1] = src64[1] & src_mask64[1];
105 dst64[2] = src64[2] & src_mask64[2];
106 dst64[3] = src64[3] & src_mask64[3];
110 check_params_create(struct rte_table_hash_params *params)
113 if (params->name == NULL) {
114 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
119 if (params->key_size != KEY_SIZE) {
120 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
125 if (params->n_keys == 0) {
126 RTE_LOG(ERR, TABLE, "%s: n_keys is zero\n", __func__);
131 if ((params->n_buckets == 0) ||
132 (!rte_is_power_of_2(params->n_buckets))) {
133 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
138 if (params->f_hash == NULL) {
139 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
148 rte_table_hash_create_key32_lru(void *params,
152 struct rte_table_hash_params *p = params;
153 struct rte_table_hash *f;
154 uint64_t bucket_size, total_size;
155 uint32_t n_buckets, i;
157 /* Check input parameters */
158 if ((check_params_create(p) != 0) ||
159 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
160 ((sizeof(struct rte_bucket_4_32) % 64) != 0))
166 * Objective: Pick the number of buckets (n_buckets) so that there a chance
167 * to store n_keys keys in the table.
169 * Note: Since the buckets do not get extended, it is not possible to
170 * guarantee that n_keys keys can be stored in the table at any time. In the
171 * worst case scenario when all the n_keys fall into the same bucket, only
172 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
173 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
174 * n_keys to n_buckets ratio.
176 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
178 n_buckets = rte_align32pow2(
179 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
180 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
182 /* Memory allocation */
183 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_32) +
184 KEYS_PER_BUCKET * entry_size);
185 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size;
186 if (total_size > SIZE_MAX) {
187 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
188 "for hash table %s\n",
189 __func__, total_size, p->name);
193 f = rte_zmalloc_socket(p->name,
198 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
199 "for hash table %s\n",
200 __func__, total_size, p->name);
204 "%s: Hash table %s memory footprint "
205 "is %" PRIu64 " bytes\n",
206 __func__, p->name, total_size);
208 /* Memory initialization */
209 f->n_buckets = n_buckets;
210 f->key_size = KEY_SIZE;
211 f->entry_size = entry_size;
212 f->bucket_size = bucket_size;
213 f->key_offset = p->key_offset;
214 f->f_hash = p->f_hash;
217 if (p->key_mask != NULL) {
218 f->key_mask[0] = ((uint64_t *)p->key_mask)[0];
219 f->key_mask[1] = ((uint64_t *)p->key_mask)[1];
220 f->key_mask[2] = ((uint64_t *)p->key_mask)[2];
221 f->key_mask[3] = ((uint64_t *)p->key_mask)[3];
223 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
224 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
225 f->key_mask[2] = 0xFFFFFFFFFFFFFFFFLLU;
226 f->key_mask[3] = 0xFFFFFFFFFFFFFFFFLLU;
229 for (i = 0; i < n_buckets; i++) {
230 struct rte_bucket_4_32 *bucket;
232 bucket = (struct rte_bucket_4_32 *) &f->memory[i *
234 bucket->lru_list = 0x0000000100020003LLU;
241 rte_table_hash_free_key32_lru(void *table)
243 struct rte_table_hash *f = table;
245 /* Check input parameters */
247 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
256 rte_table_hash_entry_add_key32_lru(
263 struct rte_table_hash *f = table;
264 struct rte_bucket_4_32 *bucket;
265 uint64_t signature, pos;
266 uint32_t bucket_index, i;
268 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
269 bucket_index = signature & (f->n_buckets - 1);
270 bucket = (struct rte_bucket_4_32 *)
271 &f->memory[bucket_index * f->bucket_size];
272 signature |= RTE_BUCKET_ENTRY_VALID;
274 /* Key is present in the bucket */
275 for (i = 0; i < 4; i++) {
276 uint64_t bucket_signature = bucket->signature[i];
277 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
279 if ((bucket_signature == signature) &&
280 (keycmp(bucket_key, key, f->key_mask) == 0)) {
281 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
283 memcpy(bucket_data, entry, f->entry_size);
284 lru_update(bucket, i);
286 *entry_ptr = (void *) bucket_data;
291 /* Key is not present in the bucket */
292 for (i = 0; i < 4; i++) {
293 uint64_t bucket_signature = bucket->signature[i];
294 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
296 if (bucket_signature == 0) {
297 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
299 bucket->signature[i] = signature;
300 keycpy(bucket_key, key, f->key_mask);
301 memcpy(bucket_data, entry, f->entry_size);
302 lru_update(bucket, i);
304 *entry_ptr = (void *) bucket_data;
310 /* Bucket full: replace LRU entry */
311 pos = lru_pos(bucket);
312 bucket->signature[pos] = signature;
313 keycpy(&bucket->key[pos], key, f->key_mask);
314 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
315 lru_update(bucket, pos);
317 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
323 rte_table_hash_entry_delete_key32_lru(
329 struct rte_table_hash *f = table;
330 struct rte_bucket_4_32 *bucket;
332 uint32_t bucket_index, i;
334 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
335 bucket_index = signature & (f->n_buckets - 1);
336 bucket = (struct rte_bucket_4_32 *)
337 &f->memory[bucket_index * f->bucket_size];
338 signature |= RTE_BUCKET_ENTRY_VALID;
340 /* Key is present in the bucket */
341 for (i = 0; i < 4; i++) {
342 uint64_t bucket_signature = bucket->signature[i];
343 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
345 if ((bucket_signature == signature) &&
346 (keycmp(bucket_key, key, f->key_mask) == 0)) {
347 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
349 bucket->signature[i] = 0;
352 memcpy(entry, bucket_data, f->entry_size);
358 /* Key is not present in the bucket */
364 rte_table_hash_create_key32_ext(void *params,
368 struct rte_table_hash_params *p = params;
369 struct rte_table_hash *f;
370 uint64_t bucket_size, stack_size, total_size;
371 uint32_t n_buckets_ext, i;
373 /* Check input parameters */
374 if ((check_params_create(p) != 0) ||
375 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
376 ((sizeof(struct rte_bucket_4_32) % 64) != 0))
382 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
383 * it is guaranteed that n_keys keys can be stored in the table at any time.
385 * The worst case scenario takes place when all the n_keys keys fall into
386 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
387 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
388 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
389 * into a different bucket. This case defeats the purpose of the hash table.
390 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
392 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
394 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
396 /* Memory allocation */
397 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_32) +
398 KEYS_PER_BUCKET * entry_size);
399 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
400 total_size = sizeof(struct rte_table_hash) +
401 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size;
402 if (total_size > SIZE_MAX) {
403 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
404 "for hash table %s\n",
405 __func__, total_size, p->name);
409 f = rte_zmalloc_socket(p->name,
414 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
415 "for hash table %s\n",
416 __func__, total_size, p->name);
420 "%s: Hash table %s memory footprint "
421 "is %" PRIu64" bytes\n",
422 __func__, p->name, total_size);
424 /* Memory initialization */
425 f->n_buckets = p->n_buckets;
426 f->key_size = KEY_SIZE;
427 f->entry_size = entry_size;
428 f->bucket_size = bucket_size;
429 f->key_offset = p->key_offset;
430 f->f_hash = p->f_hash;
433 f->n_buckets_ext = n_buckets_ext;
434 f->stack_pos = n_buckets_ext;
435 f->stack = (uint32_t *)
436 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size];
438 if (p->key_mask != NULL) {
439 f->key_mask[0] = (((uint64_t *)p->key_mask)[0]);
440 f->key_mask[1] = (((uint64_t *)p->key_mask)[1]);
441 f->key_mask[2] = (((uint64_t *)p->key_mask)[2]);
442 f->key_mask[3] = (((uint64_t *)p->key_mask)[3]);
444 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
445 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
446 f->key_mask[2] = 0xFFFFFFFFFFFFFFFFLLU;
447 f->key_mask[3] = 0xFFFFFFFFFFFFFFFFLLU;
450 for (i = 0; i < n_buckets_ext; i++)
457 rte_table_hash_free_key32_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_key32_ext(
479 struct rte_table_hash *f = table;
480 struct rte_bucket_4_32 *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_32 *)
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;
510 /* Key is not present in the bucket */
511 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
512 bucket_prev = bucket, bucket = bucket->next)
513 for (i = 0; i < 4; i++) {
514 uint64_t bucket_signature = bucket->signature[i];
515 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
517 if (bucket_signature == 0) {
518 uint8_t *bucket_data = &bucket->data[i *
521 bucket->signature[i] = signature;
522 keycpy(bucket_key, key, f->key_mask);
523 memcpy(bucket_data, entry, f->entry_size);
525 *entry_ptr = (void *) bucket_data;
531 /* Bucket full: extend bucket */
532 if (f->stack_pos > 0) {
533 bucket_index = f->stack[--f->stack_pos];
535 bucket = (struct rte_bucket_4_32 *)
536 &f->memory[(f->n_buckets + bucket_index) *
538 bucket_prev->next = bucket;
539 bucket_prev->next_valid = 1;
541 bucket->signature[0] = signature;
542 keycpy(&bucket->key[0], key, f->key_mask);
543 memcpy(&bucket->data[0], entry, f->entry_size);
545 *entry_ptr = (void *) &bucket->data[0];
553 rte_table_hash_entry_delete_key32_ext(
559 struct rte_table_hash *f = table;
560 struct rte_bucket_4_32 *bucket0, *bucket, *bucket_prev;
562 uint32_t bucket_index, i;
564 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
565 bucket_index = signature & (f->n_buckets - 1);
566 bucket0 = (struct rte_bucket_4_32 *)
567 &f->memory[bucket_index * f->bucket_size];
568 signature |= RTE_BUCKET_ENTRY_VALID;
570 /* Key is present in the bucket */
571 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
572 bucket_prev = bucket, bucket = bucket->next)
573 for (i = 0; i < 4; i++) {
574 uint64_t bucket_signature = bucket->signature[i];
575 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
577 if ((bucket_signature == signature) &&
578 (keycmp(bucket_key, key, f->key_mask) == 0)) {
579 uint8_t *bucket_data = &bucket->data[i *
582 bucket->signature[i] = 0;
585 memcpy(entry, bucket_data, f->entry_size);
587 if ((bucket->signature[0] == 0) &&
588 (bucket->signature[1] == 0) &&
589 (bucket->signature[2] == 0) &&
590 (bucket->signature[3] == 0) &&
591 (bucket_prev != NULL)) {
592 bucket_prev->next = bucket->next;
593 bucket_prev->next_valid =
597 sizeof(struct rte_bucket_4_32));
598 bucket_index = (((uint8_t *)bucket -
599 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
600 f->stack[f->stack_pos++] = bucket_index;
607 /* Key is not present in the bucket */
612 #define lookup_key32_cmp(key_in, bucket, pos, f) \
614 uint64_t xor[4][4], or[4], signature[4], k[4]; \
616 k[0] = key_in[0] & f->key_mask[0]; \
617 k[1] = key_in[1] & f->key_mask[1]; \
618 k[2] = key_in[2] & f->key_mask[2]; \
619 k[3] = key_in[3] & f->key_mask[3]; \
621 signature[0] = ((~bucket->signature[0]) & 1); \
622 signature[1] = ((~bucket->signature[1]) & 1); \
623 signature[2] = ((~bucket->signature[2]) & 1); \
624 signature[3] = ((~bucket->signature[3]) & 1); \
626 xor[0][0] = k[0] ^ bucket->key[0][0]; \
627 xor[0][1] = k[1] ^ bucket->key[0][1]; \
628 xor[0][2] = k[2] ^ bucket->key[0][2]; \
629 xor[0][3] = k[3] ^ bucket->key[0][3]; \
631 xor[1][0] = k[0] ^ bucket->key[1][0]; \
632 xor[1][1] = k[1] ^ bucket->key[1][1]; \
633 xor[1][2] = k[2] ^ bucket->key[1][2]; \
634 xor[1][3] = k[3] ^ bucket->key[1][3]; \
636 xor[2][0] = k[0] ^ bucket->key[2][0]; \
637 xor[2][1] = k[1] ^ bucket->key[2][1]; \
638 xor[2][2] = k[2] ^ bucket->key[2][2]; \
639 xor[2][3] = k[3] ^ bucket->key[2][3]; \
641 xor[3][0] = k[0] ^ bucket->key[3][0]; \
642 xor[3][1] = k[1] ^ bucket->key[3][1]; \
643 xor[3][2] = k[2] ^ bucket->key[3][2]; \
644 xor[3][3] = k[3] ^ bucket->key[3][3]; \
646 or[0] = xor[0][0] | xor[0][1] | xor[0][2] | xor[0][3] | signature[0];\
647 or[1] = xor[1][0] | xor[1][1] | xor[1][2] | xor[1][3] | signature[1];\
648 or[2] = xor[2][0] | xor[2][1] | xor[2][2] | xor[2][3] | signature[2];\
649 or[3] = xor[3][0] | xor[3][1] | xor[3][2] | xor[3][3] | signature[3];\
662 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
665 uint32_t key_offset = f->key_offset; \
667 pkt0_index = __builtin_ctzll(pkts_mask); \
668 pkt_mask = 1LLU << pkt0_index; \
669 pkts_mask &= ~pkt_mask; \
671 mbuf0 = pkts[pkt0_index]; \
672 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset));\
675 #define lookup1_stage1(mbuf1, bucket1, f) \
678 uint64_t signature; \
679 uint32_t bucket_index; \
681 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset); \
682 signature = f->f_hash(key, f->key_mask, KEY_SIZE, f->seed); \
684 bucket_index = signature & (f->n_buckets - 1); \
685 bucket1 = (struct rte_bucket_4_32 *) \
686 &f->memory[bucket_index * f->bucket_size]; \
687 rte_prefetch0(bucket1); \
688 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
689 rte_prefetch0((void *)(((uintptr_t) bucket1) + 2 * RTE_CACHE_LINE_SIZE));\
692 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
693 pkts_mask_out, entries, f) \
700 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
701 lookup_key32_cmp(key, bucket2, pos, f); \
703 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
704 pkts_mask_out |= pkt_mask; \
706 a = (void *) &bucket2->data[pos * f->entry_size]; \
708 entries[pkt2_index] = a; \
709 lru_update(bucket2, pos); \
712 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out,\
713 entries, buckets_mask, buckets, keys, f) \
715 struct rte_bucket_4_32 *bucket_next; \
717 uint64_t pkt_mask, bucket_mask; \
721 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
722 lookup_key32_cmp(key, bucket2, pos, f); \
724 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
725 pkts_mask_out |= pkt_mask; \
727 a = (void *) &bucket2->data[pos * f->entry_size]; \
729 entries[pkt2_index] = a; \
731 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
732 buckets_mask |= bucket_mask; \
733 bucket_next = bucket2->next; \
734 buckets[pkt2_index] = bucket_next; \
735 keys[pkt2_index] = key; \
738 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, \
739 entries, buckets_mask, f) \
741 struct rte_bucket_4_32 *bucket, *bucket_next; \
743 uint64_t pkt_mask, bucket_mask; \
747 bucket = buckets[pkt_index]; \
748 key = keys[pkt_index]; \
750 lookup_key32_cmp(key, bucket, pos, f); \
752 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
753 pkts_mask_out |= pkt_mask; \
755 a = (void *) &bucket->data[pos * f->entry_size]; \
757 entries[pkt_index] = a; \
759 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
760 buckets_mask |= bucket_mask; \
761 bucket_next = bucket->next; \
762 rte_prefetch0(bucket_next); \
763 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
764 rte_prefetch0((void *)(((uintptr_t) bucket_next) + \
765 2 * RTE_CACHE_LINE_SIZE)); \
766 buckets[pkt_index] = bucket_next; \
767 keys[pkt_index] = key; \
770 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
771 pkts, pkts_mask, f) \
773 uint64_t pkt00_mask, pkt01_mask; \
774 uint32_t key_offset = f->key_offset; \
776 pkt00_index = __builtin_ctzll(pkts_mask); \
777 pkt00_mask = 1LLU << pkt00_index; \
778 pkts_mask &= ~pkt00_mask; \
780 mbuf00 = pkts[pkt00_index]; \
781 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
783 pkt01_index = __builtin_ctzll(pkts_mask); \
784 pkt01_mask = 1LLU << pkt01_index; \
785 pkts_mask &= ~pkt01_mask; \
787 mbuf01 = pkts[pkt01_index]; \
788 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
791 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
792 mbuf00, mbuf01, pkts, pkts_mask, f) \
794 uint64_t pkt00_mask, pkt01_mask; \
795 uint32_t key_offset = f->key_offset; \
797 pkt00_index = __builtin_ctzll(pkts_mask); \
798 pkt00_mask = 1LLU << pkt00_index; \
799 pkts_mask &= ~pkt00_mask; \
801 mbuf00 = pkts[pkt00_index]; \
802 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset)); \
804 pkt01_index = __builtin_ctzll(pkts_mask); \
805 if (pkts_mask == 0) \
806 pkt01_index = pkt00_index; \
808 pkt01_mask = 1LLU << pkt01_index; \
809 pkts_mask &= ~pkt01_mask; \
811 mbuf01 = pkts[pkt01_index]; \
812 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset)); \
815 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
817 uint64_t *key10, *key11; \
818 uint64_t signature10, signature11; \
819 uint32_t bucket10_index, bucket11_index; \
821 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, f->key_offset); \
822 signature10 = f->f_hash(key10, f->key_mask, KEY_SIZE, f->seed); \
824 bucket10_index = signature10 & (f->n_buckets - 1); \
825 bucket10 = (struct rte_bucket_4_32 *) \
826 &f->memory[bucket10_index * f->bucket_size]; \
827 rte_prefetch0(bucket10); \
828 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
829 rte_prefetch0((void *)(((uintptr_t) bucket10) + 2 * RTE_CACHE_LINE_SIZE));\
831 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, f->key_offset); \
832 signature11 = f->f_hash(key11, f->key_mask, KEY_SIZE, f->seed);\
834 bucket11_index = signature11 & (f->n_buckets - 1); \
835 bucket11 = (struct rte_bucket_4_32 *) \
836 &f->memory[bucket11_index * f->bucket_size]; \
837 rte_prefetch0(bucket11); \
838 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
839 rte_prefetch0((void *)(((uintptr_t) bucket11) + 2 * RTE_CACHE_LINE_SIZE));\
842 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
843 bucket20, bucket21, pkts_mask_out, entries, f) \
846 uint64_t pkt20_mask, pkt21_mask; \
847 uint64_t *key20, *key21; \
848 uint32_t pos20, pos21; \
850 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
851 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
853 lookup_key32_cmp(key20, bucket20, pos20, f); \
854 lookup_key32_cmp(key21, bucket21, pos21, f); \
856 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
857 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
858 pkts_mask_out |= pkt20_mask | pkt21_mask; \
860 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
861 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
862 rte_prefetch0(a20); \
863 rte_prefetch0(a21); \
864 entries[pkt20_index] = a20; \
865 entries[pkt21_index] = a21; \
866 lru_update(bucket20, pos20); \
867 lru_update(bucket21, pos21); \
870 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
871 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f)\
873 struct rte_bucket_4_32 *bucket20_next, *bucket21_next; \
875 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
876 uint64_t *key20, *key21; \
877 uint32_t pos20, pos21; \
879 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
880 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
882 lookup_key32_cmp(key20, bucket20, pos20, f); \
883 lookup_key32_cmp(key21, bucket21, pos21, f); \
885 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
886 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
887 pkts_mask_out |= pkt20_mask | pkt21_mask; \
889 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
890 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
891 rte_prefetch0(a20); \
892 rte_prefetch0(a21); \
893 entries[pkt20_index] = a20; \
894 entries[pkt21_index] = a21; \
896 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
897 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
898 buckets_mask |= bucket20_mask | bucket21_mask; \
899 bucket20_next = bucket20->next; \
900 bucket21_next = bucket21->next; \
901 buckets[pkt20_index] = bucket20_next; \
902 buckets[pkt21_index] = bucket21_next; \
903 keys[pkt20_index] = key20; \
904 keys[pkt21_index] = key21; \
908 rte_table_hash_lookup_key32_lru(
910 struct rte_mbuf **pkts,
912 uint64_t *lookup_hit_mask,
915 struct rte_table_hash *f = (struct rte_table_hash *) table;
916 struct rte_bucket_4_32 *bucket10, *bucket11, *bucket20, *bucket21;
917 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
918 uint32_t pkt00_index, pkt01_index, pkt10_index;
919 uint32_t pkt11_index, pkt20_index, pkt21_index;
920 uint64_t pkts_mask_out = 0;
922 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
923 RTE_TABLE_HASH_KEY32_STATS_PKTS_IN_ADD(f, n_pkts_in);
925 /* Cannot run the pipeline with less than 5 packets */
926 if (__builtin_popcountll(pkts_mask) < 5) {
927 for ( ; pkts_mask; ) {
928 struct rte_bucket_4_32 *bucket;
929 struct rte_mbuf *mbuf;
932 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
933 lookup1_stage1(mbuf, bucket, f);
934 lookup1_stage2_lru(pkt_index, mbuf, bucket,
935 pkts_mask_out, entries, f);
938 *lookup_hit_mask = pkts_mask_out;
939 RTE_TABLE_HASH_KEY32_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
947 /* Pipeline stage 0 */
948 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
954 pkt10_index = pkt00_index;
955 pkt11_index = pkt01_index;
957 /* Pipeline stage 0 */
958 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
961 /* Pipeline stage 1 */
962 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
968 for ( ; pkts_mask; ) {
976 pkt20_index = pkt10_index;
977 pkt21_index = pkt11_index;
978 pkt10_index = pkt00_index;
979 pkt11_index = pkt01_index;
981 /* Pipeline stage 0 */
982 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
983 mbuf00, mbuf01, pkts, pkts_mask, f);
985 /* Pipeline stage 1 */
986 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
988 /* Pipeline stage 2 */
989 lookup2_stage2_lru(pkt20_index, pkt21_index,
990 mbuf20, mbuf21, bucket20, bucket21, pkts_mask_out,
1000 bucket21 = bucket11;
1005 pkt20_index = pkt10_index;
1006 pkt21_index = pkt11_index;
1007 pkt10_index = pkt00_index;
1008 pkt11_index = pkt01_index;
1010 /* Pipeline stage 1 */
1011 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1013 /* Pipeline stage 2 */
1014 lookup2_stage2_lru(pkt20_index, pkt21_index,
1015 mbuf20, mbuf21, bucket20, bucket21, pkts_mask_out, entries, f);
1018 bucket20 = bucket10;
1019 bucket21 = bucket11;
1022 pkt20_index = pkt10_index;
1023 pkt21_index = pkt11_index;
1025 /* Pipeline stage 2 */
1026 lookup2_stage2_lru(pkt20_index, pkt21_index,
1027 mbuf20, mbuf21, bucket20, bucket21, pkts_mask_out, entries, f);
1029 *lookup_hit_mask = pkts_mask_out;
1030 RTE_TABLE_HASH_KEY32_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
1032 } /* rte_table_hash_lookup_key32_lru() */
1035 rte_table_hash_lookup_key32_ext(
1037 struct rte_mbuf **pkts,
1039 uint64_t *lookup_hit_mask,
1042 struct rte_table_hash *f = (struct rte_table_hash *) table;
1043 struct rte_bucket_4_32 *bucket10, *bucket11, *bucket20, *bucket21;
1044 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
1045 uint32_t pkt00_index, pkt01_index, pkt10_index;
1046 uint32_t pkt11_index, pkt20_index, pkt21_index;
1047 uint64_t pkts_mask_out = 0, buckets_mask = 0;
1048 struct rte_bucket_4_32 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
1049 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
1051 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
1052 RTE_TABLE_HASH_KEY32_STATS_PKTS_IN_ADD(f, n_pkts_in);
1054 /* Cannot run the pipeline with less than 5 packets */
1055 if (__builtin_popcountll(pkts_mask) < 5) {
1056 for ( ; pkts_mask; ) {
1057 struct rte_bucket_4_32 *bucket;
1058 struct rte_mbuf *mbuf;
1061 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
1062 lookup1_stage1(mbuf, bucket, f);
1063 lookup1_stage2_ext(pkt_index, mbuf, bucket,
1064 pkts_mask_out, entries, buckets_mask, buckets,
1068 goto grind_next_buckets;
1075 /* Pipeline stage 0 */
1076 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1082 pkt10_index = pkt00_index;
1083 pkt11_index = pkt01_index;
1085 /* Pipeline stage 0 */
1086 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1089 /* Pipeline stage 1 */
1090 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1096 for ( ; pkts_mask; ) {
1098 bucket20 = bucket10;
1099 bucket21 = bucket11;
1104 pkt20_index = pkt10_index;
1105 pkt21_index = pkt11_index;
1106 pkt10_index = pkt00_index;
1107 pkt11_index = pkt01_index;
1109 /* Pipeline stage 0 */
1110 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1111 mbuf00, mbuf01, pkts, pkts_mask, f);
1113 /* Pipeline stage 1 */
1114 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1116 /* Pipeline stage 2 */
1117 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1118 bucket20, bucket21, pkts_mask_out, entries,
1119 buckets_mask, buckets, keys, f);
1127 bucket20 = bucket10;
1128 bucket21 = bucket11;
1133 pkt20_index = pkt10_index;
1134 pkt21_index = pkt11_index;
1135 pkt10_index = pkt00_index;
1136 pkt11_index = pkt01_index;
1138 /* Pipeline stage 1 */
1139 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1141 /* Pipeline stage 2 */
1142 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1143 bucket20, bucket21, pkts_mask_out, entries,
1144 buckets_mask, buckets, keys, f);
1147 bucket20 = bucket10;
1148 bucket21 = bucket11;
1151 pkt20_index = pkt10_index;
1152 pkt21_index = pkt11_index;
1154 /* Pipeline stage 2 */
1155 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1156 bucket20, bucket21, pkts_mask_out, entries,
1157 buckets_mask, buckets, keys, f);
1160 /* Grind next buckets */
1161 for ( ; buckets_mask; ) {
1162 uint64_t buckets_mask_next = 0;
1164 for ( ; buckets_mask; ) {
1168 pkt_index = __builtin_ctzll(buckets_mask);
1169 pkt_mask = 1LLU << pkt_index;
1170 buckets_mask &= ~pkt_mask;
1172 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1173 entries, buckets_mask_next, f);
1176 buckets_mask = buckets_mask_next;
1179 *lookup_hit_mask = pkts_mask_out;
1180 RTE_TABLE_HASH_KEY32_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
1182 } /* rte_table_hash_lookup_key32_ext() */
1185 rte_table_hash_key32_stats_read(void *table, struct rte_table_stats *stats, int clear)
1187 struct rte_table_hash *t = table;
1190 memcpy(stats, &t->stats, sizeof(t->stats));
1193 memset(&t->stats, 0, sizeof(t->stats));
1198 struct rte_table_ops rte_table_hash_key32_lru_ops = {
1199 .f_create = rte_table_hash_create_key32_lru,
1200 .f_free = rte_table_hash_free_key32_lru,
1201 .f_add = rte_table_hash_entry_add_key32_lru,
1202 .f_delete = rte_table_hash_entry_delete_key32_lru,
1204 .f_delete_bulk = NULL,
1205 .f_lookup = rte_table_hash_lookup_key32_lru,
1206 .f_stats = rte_table_hash_key32_stats_read,
1209 struct rte_table_ops rte_table_hash_key32_ext_ops = {
1210 .f_create = rte_table_hash_create_key32_ext,
1211 .f_free = rte_table_hash_free_key32_ext,
1212 .f_add = rte_table_hash_entry_add_key32_ext,
1213 .f_delete = rte_table_hash_entry_delete_key32_ext,
1215 .f_delete_bulk = NULL,
1216 .f_lookup = rte_table_hash_lookup_key32_ext,
1217 .f_stats = rte_table_hash_key32_stats_read,