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"
45 #define RTE_TABLE_HASH_KEY_SIZE 32
47 #define RTE_BUCKET_ENTRY_VALID 0x1LLU
49 struct rte_bucket_4_32 {
51 uint64_t signature[4 + 1];
53 struct rte_bucket_4_32 *next;
56 /* Cache lines 1 and 2 */
63 struct rte_table_hash {
64 /* Input parameters */
66 uint32_t n_entries_per_bucket;
70 uint32_t signature_offset;
72 rte_table_hash_op_hash f_hash;
75 /* Extendible buckets */
76 uint32_t n_buckets_ext;
81 uint8_t memory[0] __rte_cache_aligned;
85 check_params_create_lru(struct rte_table_hash_key32_lru_params *params) {
87 if (params->n_entries == 0) {
88 RTE_LOG(ERR, TABLE, "%s: n_entries is zero\n", __func__);
93 if (params->f_hash == NULL) {
94 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
103 rte_table_hash_create_key32_lru(void *params,
107 struct rte_table_hash_key32_lru_params *p =
108 (struct rte_table_hash_key32_lru_params *) params;
109 struct rte_table_hash *f;
110 uint32_t n_buckets, n_entries_per_bucket, key_size, bucket_size_cl;
111 uint32_t total_size, i;
113 /* Check input parameters */
114 if ((check_params_create_lru(p) != 0) ||
115 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
116 ((sizeof(struct rte_bucket_4_32) % RTE_CACHE_LINE_SIZE) != 0)) {
119 n_entries_per_bucket = 4;
122 /* Memory allocation */
123 n_buckets = rte_align32pow2((p->n_entries + n_entries_per_bucket - 1) /
124 n_entries_per_bucket);
125 bucket_size_cl = (sizeof(struct rte_bucket_4_32) + n_entries_per_bucket
126 * entry_size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
127 total_size = sizeof(struct rte_table_hash) + n_buckets *
128 bucket_size_cl * RTE_CACHE_LINE_SIZE;
130 f = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
133 "%s: Cannot allocate %u bytes for hash table\n",
134 __func__, total_size);
138 "%s: Hash table memory footprint is %u bytes\n", __func__,
141 /* Memory initialization */
142 f->n_buckets = n_buckets;
143 f->n_entries_per_bucket = n_entries_per_bucket;
144 f->key_size = key_size;
145 f->entry_size = entry_size;
146 f->bucket_size = bucket_size_cl * RTE_CACHE_LINE_SIZE;
147 f->signature_offset = p->signature_offset;
148 f->key_offset = p->key_offset;
149 f->f_hash = p->f_hash;
152 for (i = 0; i < n_buckets; i++) {
153 struct rte_bucket_4_32 *bucket;
155 bucket = (struct rte_bucket_4_32 *) &f->memory[i *
157 bucket->lru_list = 0x0000000100020003LLU;
164 rte_table_hash_free_key32_lru(void *table)
166 struct rte_table_hash *f = (struct rte_table_hash *) table;
168 /* Check input parameters */
170 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
179 rte_table_hash_entry_add_key32_lru(
186 struct rte_table_hash *f = (struct rte_table_hash *) table;
187 struct rte_bucket_4_32 *bucket;
188 uint64_t signature, pos;
189 uint32_t bucket_index, i;
191 signature = f->f_hash(key, f->key_size, f->seed);
192 bucket_index = signature & (f->n_buckets - 1);
193 bucket = (struct rte_bucket_4_32 *)
194 &f->memory[bucket_index * f->bucket_size];
195 signature |= RTE_BUCKET_ENTRY_VALID;
197 /* Key is present in the bucket */
198 for (i = 0; i < 4; i++) {
199 uint64_t bucket_signature = bucket->signature[i];
200 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
202 if ((bucket_signature == signature) &&
203 (memcmp(key, bucket_key, f->key_size) == 0)) {
204 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
206 memcpy(bucket_data, entry, f->entry_size);
207 lru_update(bucket, i);
209 *entry_ptr = (void *) bucket_data;
214 /* Key is not present in the bucket */
215 for (i = 0; i < 4; i++) {
216 uint64_t bucket_signature = bucket->signature[i];
217 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
219 if (bucket_signature == 0) {
220 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
222 bucket->signature[i] = signature;
223 memcpy(bucket_key, key, f->key_size);
224 memcpy(bucket_data, entry, f->entry_size);
225 lru_update(bucket, i);
227 *entry_ptr = (void *) bucket_data;
233 /* Bucket full: replace LRU entry */
234 pos = lru_pos(bucket);
235 bucket->signature[pos] = signature;
236 memcpy(bucket->key[pos], key, f->key_size);
237 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
238 lru_update(bucket, pos);
240 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
246 rte_table_hash_entry_delete_key32_lru(
252 struct rte_table_hash *f = (struct rte_table_hash *) table;
253 struct rte_bucket_4_32 *bucket;
255 uint32_t bucket_index, i;
257 signature = f->f_hash(key, f->key_size, f->seed);
258 bucket_index = signature & (f->n_buckets - 1);
259 bucket = (struct rte_bucket_4_32 *)
260 &f->memory[bucket_index * f->bucket_size];
261 signature |= RTE_BUCKET_ENTRY_VALID;
263 /* Key is present in the bucket */
264 for (i = 0; i < 4; i++) {
265 uint64_t bucket_signature = bucket->signature[i];
266 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
268 if ((bucket_signature == signature) &&
269 (memcmp(key, bucket_key, f->key_size) == 0)) {
270 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
272 bucket->signature[i] = 0;
275 memcpy(entry, bucket_data, f->entry_size);
281 /* Key is not present in the bucket */
287 check_params_create_ext(struct rte_table_hash_key32_ext_params *params) {
289 if (params->n_entries == 0) {
290 RTE_LOG(ERR, TABLE, "%s: n_entries is zero\n", __func__);
295 if (params->n_entries_ext == 0) {
296 RTE_LOG(ERR, TABLE, "%s: n_entries_ext is zero\n", __func__);
301 if (params->f_hash == NULL) {
302 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
311 rte_table_hash_create_key32_ext(void *params,
315 struct rte_table_hash_key32_ext_params *p =
316 (struct rte_table_hash_key32_ext_params *) params;
317 struct rte_table_hash *f;
318 uint32_t n_buckets, n_buckets_ext, n_entries_per_bucket;
319 uint32_t key_size, bucket_size_cl, stack_size_cl, total_size, i;
321 /* Check input parameters */
322 if ((check_params_create_ext(p) != 0) ||
323 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
324 ((sizeof(struct rte_bucket_4_32) % RTE_CACHE_LINE_SIZE) != 0))
327 n_entries_per_bucket = 4;
330 /* Memory allocation */
331 n_buckets = rte_align32pow2((p->n_entries + n_entries_per_bucket - 1) /
332 n_entries_per_bucket);
333 n_buckets_ext = (p->n_entries_ext + n_entries_per_bucket - 1) /
334 n_entries_per_bucket;
335 bucket_size_cl = (sizeof(struct rte_bucket_4_32) + n_entries_per_bucket
336 * entry_size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
337 stack_size_cl = (n_buckets_ext * sizeof(uint32_t) + RTE_CACHE_LINE_SIZE - 1)
338 / RTE_CACHE_LINE_SIZE;
339 total_size = sizeof(struct rte_table_hash) +
340 ((n_buckets + n_buckets_ext) * bucket_size_cl + stack_size_cl) *
343 f = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
346 "%s: Cannot allocate %u bytes for hash table\n",
347 __func__, total_size);
351 "%s: Hash table memory footprint is %u bytes\n", __func__,
354 /* Memory initialization */
355 f->n_buckets = n_buckets;
356 f->n_entries_per_bucket = n_entries_per_bucket;
357 f->key_size = key_size;
358 f->entry_size = entry_size;
359 f->bucket_size = bucket_size_cl * RTE_CACHE_LINE_SIZE;
360 f->signature_offset = p->signature_offset;
361 f->key_offset = p->key_offset;
362 f->f_hash = p->f_hash;
365 f->n_buckets_ext = n_buckets_ext;
366 f->stack_pos = n_buckets_ext;
367 f->stack = (uint32_t *)
368 &f->memory[(n_buckets + n_buckets_ext) * f->bucket_size];
370 for (i = 0; i < n_buckets_ext; i++)
377 rte_table_hash_free_key32_ext(void *table)
379 struct rte_table_hash *f = (struct rte_table_hash *) table;
381 /* Check input parameters */
383 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
392 rte_table_hash_entry_add_key32_ext(
399 struct rte_table_hash *f = (struct rte_table_hash *) table;
400 struct rte_bucket_4_32 *bucket0, *bucket, *bucket_prev;
402 uint32_t bucket_index, i;
404 signature = f->f_hash(key, f->key_size, f->seed);
405 bucket_index = signature & (f->n_buckets - 1);
406 bucket0 = (struct rte_bucket_4_32 *)
407 &f->memory[bucket_index * f->bucket_size];
408 signature |= RTE_BUCKET_ENTRY_VALID;
410 /* Key is present in the bucket */
411 for (bucket = bucket0; bucket != NULL; bucket = bucket->next) {
412 for (i = 0; i < 4; i++) {
413 uint64_t bucket_signature = bucket->signature[i];
414 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
416 if ((bucket_signature == signature) &&
417 (memcmp(key, bucket_key, f->key_size) == 0)) {
418 uint8_t *bucket_data = &bucket->data[i *
421 memcpy(bucket_data, entry, f->entry_size);
423 *entry_ptr = (void *) bucket_data;
430 /* Key is not present in the bucket */
431 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
432 bucket_prev = bucket, bucket = bucket->next)
433 for (i = 0; i < 4; i++) {
434 uint64_t bucket_signature = bucket->signature[i];
435 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
437 if (bucket_signature == 0) {
438 uint8_t *bucket_data = &bucket->data[i *
441 bucket->signature[i] = signature;
442 memcpy(bucket_key, key, f->key_size);
443 memcpy(bucket_data, entry, f->entry_size);
445 *entry_ptr = (void *) bucket_data;
451 /* Bucket full: extend bucket */
452 if (f->stack_pos > 0) {
453 bucket_index = f->stack[--f->stack_pos];
455 bucket = (struct rte_bucket_4_32 *)
456 &f->memory[(f->n_buckets + bucket_index) *
458 bucket_prev->next = bucket;
459 bucket_prev->next_valid = 1;
461 bucket->signature[0] = signature;
462 memcpy(bucket->key[0], key, f->key_size);
463 memcpy(&bucket->data[0], entry, f->entry_size);
465 *entry_ptr = (void *) &bucket->data[0];
473 rte_table_hash_entry_delete_key32_ext(
479 struct rte_table_hash *f = (struct rte_table_hash *) 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_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_prev = NULL, bucket = bucket0; bucket != NULL;
492 bucket_prev = bucket, bucket = bucket->next)
493 for (i = 0; i < 4; i++) {
494 uint64_t bucket_signature = bucket->signature[i];
495 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
497 if ((bucket_signature == signature) &&
498 (memcmp(key, bucket_key, f->key_size) == 0)) {
499 uint8_t *bucket_data = &bucket->data[i *
502 bucket->signature[i] = 0;
505 memcpy(entry, bucket_data,
508 if ((bucket->signature[0] == 0) &&
509 (bucket->signature[1] == 0) &&
510 (bucket->signature[2] == 0) &&
511 (bucket->signature[3] == 0) &&
512 (bucket_prev != NULL)) {
513 bucket_prev->next = bucket->next;
514 bucket_prev->next_valid =
518 sizeof(struct rte_bucket_4_32));
519 bucket_index = (((uint8_t *)bucket -
520 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
521 f->stack[f->stack_pos++] = bucket_index;
528 /* Key is not present in the bucket */
533 #define lookup_key32_cmp(key_in, bucket, pos) \
535 uint64_t xor[4][4], or[4], signature[4]; \
537 signature[0] = ((~bucket->signature[0]) & 1); \
538 signature[1] = ((~bucket->signature[1]) & 1); \
539 signature[2] = ((~bucket->signature[2]) & 1); \
540 signature[3] = ((~bucket->signature[3]) & 1); \
542 xor[0][0] = key_in[0] ^ bucket->key[0][0]; \
543 xor[0][1] = key_in[1] ^ bucket->key[0][1]; \
544 xor[0][2] = key_in[2] ^ bucket->key[0][2]; \
545 xor[0][3] = key_in[3] ^ bucket->key[0][3]; \
547 xor[1][0] = key_in[0] ^ bucket->key[1][0]; \
548 xor[1][1] = key_in[1] ^ bucket->key[1][1]; \
549 xor[1][2] = key_in[2] ^ bucket->key[1][2]; \
550 xor[1][3] = key_in[3] ^ bucket->key[1][3]; \
552 xor[2][0] = key_in[0] ^ bucket->key[2][0]; \
553 xor[2][1] = key_in[1] ^ bucket->key[2][1]; \
554 xor[2][2] = key_in[2] ^ bucket->key[2][2]; \
555 xor[2][3] = key_in[3] ^ bucket->key[2][3]; \
557 xor[3][0] = key_in[0] ^ bucket->key[3][0]; \
558 xor[3][1] = key_in[1] ^ bucket->key[3][1]; \
559 xor[3][2] = key_in[2] ^ bucket->key[3][2]; \
560 xor[3][3] = key_in[3] ^ bucket->key[3][3]; \
562 or[0] = xor[0][0] | xor[0][1] | xor[0][2] | xor[0][3] | signature[0];\
563 or[1] = xor[1][0] | xor[1][1] | xor[1][2] | xor[1][3] | signature[1];\
564 or[2] = xor[2][0] | xor[2][1] | xor[2][2] | xor[2][3] | signature[2];\
565 or[3] = xor[3][0] | xor[3][1] | xor[3][2] | xor[3][3] | signature[3];\
578 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask) \
582 pkt0_index = __builtin_ctzll(pkts_mask); \
583 pkt_mask = 1LLU << pkt0_index; \
584 pkts_mask &= ~pkt_mask; \
586 mbuf0 = pkts[pkt0_index]; \
587 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, 0)); \
590 #define lookup1_stage1(mbuf1, bucket1, f) \
592 uint64_t signature; \
593 uint32_t bucket_index; \
595 signature = RTE_MBUF_METADATA_UINT32(mbuf1, f->signature_offset);\
596 bucket_index = signature & (f->n_buckets - 1); \
597 bucket1 = (struct rte_bucket_4_32 *) \
598 &f->memory[bucket_index * f->bucket_size]; \
599 rte_prefetch0(bucket1); \
600 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
601 rte_prefetch0((void *)(((uintptr_t) bucket1) + 2 * RTE_CACHE_LINE_SIZE));\
604 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
605 pkts_mask_out, entries, f) \
612 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
614 lookup_key32_cmp(key, bucket2, pos); \
616 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
617 pkts_mask_out |= pkt_mask; \
619 a = (void *) &bucket2->data[pos * f->entry_size]; \
621 entries[pkt2_index] = a; \
622 lru_update(bucket2, pos); \
625 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out,\
626 entries, buckets_mask, buckets, keys, f) \
628 struct rte_bucket_4_32 *bucket_next; \
630 uint64_t pkt_mask, bucket_mask; \
634 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
636 lookup_key32_cmp(key, bucket2, pos); \
638 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
639 pkts_mask_out |= pkt_mask; \
641 a = (void *) &bucket2->data[pos * f->entry_size]; \
643 entries[pkt2_index] = a; \
645 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
646 buckets_mask |= bucket_mask; \
647 bucket_next = bucket2->next; \
648 buckets[pkt2_index] = bucket_next; \
649 keys[pkt2_index] = key; \
652 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, \
653 entries, buckets_mask, f) \
655 struct rte_bucket_4_32 *bucket, *bucket_next; \
657 uint64_t pkt_mask, bucket_mask; \
661 bucket = buckets[pkt_index]; \
662 key = keys[pkt_index]; \
664 lookup_key32_cmp(key, bucket, pos); \
666 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
667 pkts_mask_out |= pkt_mask; \
669 a = (void *) &bucket->data[pos * f->entry_size]; \
671 entries[pkt_index] = a; \
673 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
674 buckets_mask |= bucket_mask; \
675 bucket_next = bucket->next; \
676 rte_prefetch0(bucket_next); \
677 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
678 rte_prefetch0((void *)(((uintptr_t) bucket_next) + \
679 2 * RTE_CACHE_LINE_SIZE)); \
680 buckets[pkt_index] = bucket_next; \
681 keys[pkt_index] = key; \
684 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
687 uint64_t pkt00_mask, pkt01_mask; \
689 pkt00_index = __builtin_ctzll(pkts_mask); \
690 pkt00_mask = 1LLU << pkt00_index; \
691 pkts_mask &= ~pkt00_mask; \
693 mbuf00 = pkts[pkt00_index]; \
694 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
696 pkt01_index = __builtin_ctzll(pkts_mask); \
697 pkt01_mask = 1LLU << pkt01_index; \
698 pkts_mask &= ~pkt01_mask; \
700 mbuf01 = pkts[pkt01_index]; \
701 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
704 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
705 mbuf00, mbuf01, pkts, pkts_mask) \
707 uint64_t pkt00_mask, pkt01_mask; \
709 pkt00_index = __builtin_ctzll(pkts_mask); \
710 pkt00_mask = 1LLU << pkt00_index; \
711 pkts_mask &= ~pkt00_mask; \
713 mbuf00 = pkts[pkt00_index]; \
714 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
716 pkt01_index = __builtin_ctzll(pkts_mask); \
717 if (pkts_mask == 0) \
718 pkt01_index = pkt00_index; \
720 pkt01_mask = 1LLU << pkt01_index; \
721 pkts_mask &= ~pkt01_mask; \
723 mbuf01 = pkts[pkt01_index]; \
724 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
727 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
729 uint64_t signature10, signature11; \
730 uint32_t bucket10_index, bucket11_index; \
732 signature10 = RTE_MBUF_METADATA_UINT32(mbuf10, f->signature_offset);\
733 bucket10_index = signature10 & (f->n_buckets - 1); \
734 bucket10 = (struct rte_bucket_4_32 *) \
735 &f->memory[bucket10_index * f->bucket_size]; \
736 rte_prefetch0(bucket10); \
737 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
738 rte_prefetch0((void *)(((uintptr_t) bucket10) + 2 * RTE_CACHE_LINE_SIZE));\
740 signature11 = RTE_MBUF_METADATA_UINT32(mbuf11, f->signature_offset);\
741 bucket11_index = signature11 & (f->n_buckets - 1); \
742 bucket11 = (struct rte_bucket_4_32 *) \
743 &f->memory[bucket11_index * f->bucket_size]; \
744 rte_prefetch0(bucket11); \
745 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
746 rte_prefetch0((void *)(((uintptr_t) bucket11) + 2 * RTE_CACHE_LINE_SIZE));\
749 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
750 bucket20, bucket21, pkts_mask_out, entries, f) \
753 uint64_t pkt20_mask, pkt21_mask; \
754 uint64_t *key20, *key21; \
755 uint32_t pos20, pos21; \
757 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
758 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
760 lookup_key32_cmp(key20, bucket20, pos20); \
761 lookup_key32_cmp(key21, bucket21, pos21); \
763 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
764 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
765 pkts_mask_out |= pkt20_mask | pkt21_mask; \
767 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
768 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
769 rte_prefetch0(a20); \
770 rte_prefetch0(a21); \
771 entries[pkt20_index] = a20; \
772 entries[pkt21_index] = a21; \
773 lru_update(bucket20, pos20); \
774 lru_update(bucket21, pos21); \
777 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
778 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f)\
780 struct rte_bucket_4_32 *bucket20_next, *bucket21_next; \
782 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
783 uint64_t *key20, *key21; \
784 uint32_t pos20, pos21; \
786 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
787 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
789 lookup_key32_cmp(key20, bucket20, pos20); \
790 lookup_key32_cmp(key21, bucket21, pos21); \
792 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
793 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
794 pkts_mask_out |= pkt20_mask | pkt21_mask; \
796 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
797 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
798 rte_prefetch0(a20); \
799 rte_prefetch0(a21); \
800 entries[pkt20_index] = a20; \
801 entries[pkt21_index] = a21; \
803 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
804 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
805 buckets_mask |= bucket20_mask | bucket21_mask; \
806 bucket20_next = bucket20->next; \
807 bucket21_next = bucket21->next; \
808 buckets[pkt20_index] = bucket20_next; \
809 buckets[pkt21_index] = bucket21_next; \
810 keys[pkt20_index] = key20; \
811 keys[pkt21_index] = key21; \
815 rte_table_hash_lookup_key32_lru(
817 struct rte_mbuf **pkts,
819 uint64_t *lookup_hit_mask,
822 struct rte_table_hash *f = (struct rte_table_hash *) table;
823 struct rte_bucket_4_32 *bucket10, *bucket11, *bucket20, *bucket21;
824 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
825 uint32_t pkt00_index, pkt01_index, pkt10_index;
826 uint32_t pkt11_index, pkt20_index, pkt21_index;
827 uint64_t pkts_mask_out = 0;
829 /* Cannot run the pipeline with less than 5 packets */
830 if (__builtin_popcountll(pkts_mask) < 5) {
831 for ( ; pkts_mask; ) {
832 struct rte_bucket_4_32 *bucket;
833 struct rte_mbuf *mbuf;
836 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask);
837 lookup1_stage1(mbuf, bucket, f);
838 lookup1_stage2_lru(pkt_index, mbuf, bucket,
839 pkts_mask_out, entries, f);
842 *lookup_hit_mask = pkts_mask_out;
850 /* Pipeline stage 0 */
851 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
857 pkt10_index = pkt00_index;
858 pkt11_index = pkt01_index;
860 /* Pipeline stage 0 */
861 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
864 /* Pipeline stage 1 */
865 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
871 for ( ; pkts_mask; ) {
879 pkt20_index = pkt10_index;
880 pkt21_index = pkt11_index;
881 pkt10_index = pkt00_index;
882 pkt11_index = pkt01_index;
884 /* Pipeline stage 0 */
885 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
886 mbuf00, mbuf01, pkts, pkts_mask);
888 /* Pipeline stage 1 */
889 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
891 /* Pipeline stage 2 */
892 lookup2_stage2_lru(pkt20_index, pkt21_index,
893 mbuf20, mbuf21, bucket20, bucket21, pkts_mask_out,
908 pkt20_index = pkt10_index;
909 pkt21_index = pkt11_index;
910 pkt10_index = pkt00_index;
911 pkt11_index = pkt01_index;
913 /* Pipeline stage 1 */
914 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
916 /* Pipeline stage 2 */
917 lookup2_stage2_lru(pkt20_index, pkt21_index,
918 mbuf20, mbuf21, bucket20, bucket21, pkts_mask_out, entries, f);
925 pkt20_index = pkt10_index;
926 pkt21_index = pkt11_index;
928 /* Pipeline stage 2 */
929 lookup2_stage2_lru(pkt20_index, pkt21_index,
930 mbuf20, mbuf21, bucket20, bucket21, pkts_mask_out, entries, f);
932 *lookup_hit_mask = pkts_mask_out;
934 } /* rte_table_hash_lookup_key32_lru() */
937 rte_table_hash_lookup_key32_ext(
939 struct rte_mbuf **pkts,
941 uint64_t *lookup_hit_mask,
944 struct rte_table_hash *f = (struct rte_table_hash *) table;
945 struct rte_bucket_4_32 *bucket10, *bucket11, *bucket20, *bucket21;
946 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
947 uint32_t pkt00_index, pkt01_index, pkt10_index;
948 uint32_t pkt11_index, pkt20_index, pkt21_index;
949 uint64_t pkts_mask_out = 0, buckets_mask = 0;
950 struct rte_bucket_4_32 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
951 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
953 /* Cannot run the pipeline with less than 5 packets */
954 if (__builtin_popcountll(pkts_mask) < 5) {
955 for ( ; pkts_mask; ) {
956 struct rte_bucket_4_32 *bucket;
957 struct rte_mbuf *mbuf;
960 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask);
961 lookup1_stage1(mbuf, bucket, f);
962 lookup1_stage2_ext(pkt_index, mbuf, bucket,
963 pkts_mask_out, entries, buckets_mask, buckets,
967 goto grind_next_buckets;
974 /* Pipeline stage 0 */
975 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
981 pkt10_index = pkt00_index;
982 pkt11_index = pkt01_index;
984 /* Pipeline stage 0 */
985 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
988 /* Pipeline stage 1 */
989 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
995 for ( ; pkts_mask; ) {
1003 pkt20_index = pkt10_index;
1004 pkt21_index = pkt11_index;
1005 pkt10_index = pkt00_index;
1006 pkt11_index = pkt01_index;
1008 /* Pipeline stage 0 */
1009 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1010 mbuf00, mbuf01, pkts, pkts_mask);
1012 /* Pipeline stage 1 */
1013 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1015 /* Pipeline stage 2 */
1016 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1017 bucket20, bucket21, pkts_mask_out, entries,
1018 buckets_mask, buckets, keys, f);
1026 bucket20 = bucket10;
1027 bucket21 = bucket11;
1032 pkt20_index = pkt10_index;
1033 pkt21_index = pkt11_index;
1034 pkt10_index = pkt00_index;
1035 pkt11_index = pkt01_index;
1037 /* Pipeline stage 1 */
1038 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1040 /* Pipeline stage 2 */
1041 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1042 bucket20, bucket21, pkts_mask_out, entries,
1043 buckets_mask, buckets, keys, f);
1046 bucket20 = bucket10;
1047 bucket21 = bucket11;
1050 pkt20_index = pkt10_index;
1051 pkt21_index = pkt11_index;
1053 /* Pipeline stage 2 */
1054 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1055 bucket20, bucket21, pkts_mask_out, entries,
1056 buckets_mask, buckets, keys, f);
1059 /* Grind next buckets */
1060 for ( ; buckets_mask; ) {
1061 uint64_t buckets_mask_next = 0;
1063 for ( ; buckets_mask; ) {
1067 pkt_index = __builtin_ctzll(buckets_mask);
1068 pkt_mask = 1LLU << pkt_index;
1069 buckets_mask &= ~pkt_mask;
1071 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1072 entries, buckets_mask_next, f);
1075 buckets_mask = buckets_mask_next;
1078 *lookup_hit_mask = pkts_mask_out;
1080 } /* rte_table_hash_lookup_key32_ext() */
1082 struct rte_table_ops rte_table_hash_key32_lru_ops = {
1083 .f_create = rte_table_hash_create_key32_lru,
1084 .f_free = rte_table_hash_free_key32_lru,
1085 .f_add = rte_table_hash_entry_add_key32_lru,
1086 .f_delete = rte_table_hash_entry_delete_key32_lru,
1087 .f_lookup = rte_table_hash_lookup_key32_lru,
1090 struct rte_table_ops rte_table_hash_key32_ext_ops = {
1091 .f_create = rte_table_hash_create_key32_ext,
1092 .f_free = rte_table_hash_free_key32_ext,
1093 .f_add = rte_table_hash_entry_add_key32_ext,
1094 .f_delete = rte_table_hash_entry_delete_key32_ext,
1095 .f_lookup = rte_table_hash_lookup_key32_ext,