-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrte_cuckoo_hash.c
2620 lines (2289 loc) · 70.9 KB
/
rte_cuckoo_hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2016 Intel Corporation
* Copyright(c) 2018 Arm Limited
*/
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <sys/queue.h>
#include <rte_common.h>
#include <rte_log.h>
#include <rte_prefetch.h>
#include <rte_branch_prediction.h>
#include <rte_malloc.h>
#include <rte_eal_memconfig.h>
#include <rte_errno.h>
#include <rte_string_fns.h>
#include <rte_cpuflags.h>
#include <rte_rwlock.h>
#include <rte_ring_elem.h>
#include <rte_vect.h>
#include <rte_tailq.h>
#include "rte_hash.h"
/* needs to be before rte_cuckoo_hash.h */
RTE_LOG_REGISTER_DEFAULT(hash_logtype, INFO);
#define RTE_LOGTYPE_HASH hash_logtype
#define HASH_LOG(level, ...) \
RTE_LOG_LINE(level, HASH, "" __VA_ARGS__)
#include "rte_cuckoo_hash.h"
/* Enum used to select the implementation of the signature comparison function to use
* eg: a system supporting SVE might want to use a NEON or scalar implementation.
*/
enum rte_hash_sig_compare_function {
RTE_HASH_COMPARE_SCALAR = 0,
RTE_HASH_COMPARE_SSE,
RTE_HASH_COMPARE_NEON,
RTE_HASH_COMPARE_SVE,
};
#if defined(__ARM_NEON)
#include "compare_signatures_arm.h"
#elif defined(__SSE2__)
#include "compare_signatures_x86.h"
#else
#include "compare_signatures_generic.h"
#endif
/* Mask of all flags supported by this version */
#define RTE_HASH_EXTRA_FLAGS_MASK (RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT | \
RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD | \
RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY | \
RTE_HASH_EXTRA_FLAGS_EXT_TABLE | \
RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL | \
RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF)
#define FOR_EACH_BUCKET(CURRENT_BKT, START_BUCKET) \
for (CURRENT_BKT = START_BUCKET; \
CURRENT_BKT != NULL; \
CURRENT_BKT = CURRENT_BKT->next)
TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
static struct rte_tailq_elem rte_hash_tailq = {
.name = "RTE_HASH",
};
EAL_REGISTER_TAILQ(rte_hash_tailq)
struct __rte_hash_rcu_dq_entry {
uint32_t key_idx;
uint32_t ext_bkt_idx;
};
struct rte_hash *
rte_hash_find_existing(const char *name)
{
struct rte_hash *h = NULL;
struct rte_tailq_entry *te;
struct rte_hash_list *hash_list;
hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
rte_mcfg_tailq_read_lock();
TAILQ_FOREACH(te, hash_list, next) {
h = (struct rte_hash *) te->data;
if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
break;
}
rte_mcfg_tailq_read_unlock();
if (te == NULL) {
rte_errno = ENOENT;
return NULL;
}
return h;
}
static inline struct rte_hash_bucket *
rte_hash_get_last_bkt(struct rte_hash_bucket *lst_bkt)
{
while (lst_bkt->next != NULL)
lst_bkt = lst_bkt->next;
return lst_bkt;
}
void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
{
h->cmp_jump_table_idx = KEY_CUSTOM;
h->rte_hash_custom_cmp_eq = func;
}
static inline int
rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
{
if (h->cmp_jump_table_idx == KEY_CUSTOM)
return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
else
return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
}
/*
* We use higher 16 bits of hash as the signature value stored in table.
* We use the lower bits for the primary bucket
* location. Then we XOR primary bucket location and the signature
* to get the secondary bucket location. This is same as
* proposed in Bin Fan, et al's paper
* "MemC3: Compact and Concurrent MemCache with Dumber Caching and
* Smarter Hashing". The benefit to use
* XOR is that one could derive the alternative bucket location
* by only using the current bucket location and the signature.
*/
static inline uint16_t
get_short_sig(const hash_sig_t hash)
{
return hash >> 16;
}
static inline uint32_t
get_prim_bucket_index(const struct rte_hash *h, const hash_sig_t hash)
{
return hash & h->bucket_bitmask;
}
static inline uint32_t
get_alt_bucket_index(const struct rte_hash *h,
uint32_t cur_bkt_idx, uint16_t sig)
{
return (cur_bkt_idx ^ sig) & h->bucket_bitmask;
}
struct rte_hash *
rte_hash_create(const struct rte_hash_parameters *params)
{
struct rte_hash *h = NULL;
struct rte_tailq_entry *te = NULL;
struct rte_hash_list *hash_list;
struct rte_ring *r = NULL;
struct rte_ring *r_ext = NULL;
char hash_name[RTE_HASH_NAMESIZE];
void *k = NULL;
void *buckets = NULL;
void *buckets_ext = NULL;
char ring_name[RTE_RING_NAMESIZE];
char ext_ring_name[RTE_RING_NAMESIZE];
unsigned num_key_slots;
unsigned int hw_trans_mem_support = 0, use_local_cache = 0;
unsigned int ext_table_support = 0;
unsigned int readwrite_concur_support = 0;
unsigned int writer_takes_lock = 0;
unsigned int no_free_on_del = 0;
uint32_t *ext_bkt_to_free = NULL;
RTE_ATOMIC(uint32_t) *tbl_chng_cnt = NULL;
struct lcore_cache *local_free_slots = NULL;
unsigned int readwrite_concur_lf_support = 0;
uint32_t i;
rte_hash_function default_hash_func = (rte_hash_function)rte_jhash;
hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
if (params == NULL) {
rte_errno = EINVAL;
HASH_LOG(ERR, "%s has no parameters", __func__);
return NULL;
}
/* Check for valid parameters */
if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
(params->entries < RTE_HASH_BUCKET_ENTRIES)) {
rte_errno = EINVAL;
HASH_LOG(ERR, "%s() entries (%u) must be in range [%d, %d] inclusive",
__func__, params->entries, RTE_HASH_BUCKET_ENTRIES,
RTE_HASH_ENTRIES_MAX);
return NULL;
}
if (params->key_len == 0) {
rte_errno = EINVAL;
HASH_LOG(ERR, "%s() key_len must be greater than 0", __func__);
return NULL;
}
if (params->extra_flag & ~RTE_HASH_EXTRA_FLAGS_MASK) {
rte_errno = EINVAL;
HASH_LOG(ERR, "%s: unsupported extra flags", __func__);
return NULL;
}
if (params->name == NULL) {
rte_errno = EINVAL;
HASH_LOG(ERR, "%s() has invalid parameters, name can't be NULL",
__func__);
return NULL;
}
/* Validate correct usage of extra options */
if ((params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) &&
(params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF)) {
rte_errno = EINVAL;
HASH_LOG(ERR, "%s: choose rw concurrency or rw concurrency lock free",
__func__);
return NULL;
}
/* Check extra flags field to check extra options. */
if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
hw_trans_mem_support = 1;
if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
use_local_cache = 1;
writer_takes_lock = 1;
}
if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) {
readwrite_concur_support = 1;
writer_takes_lock = 1;
}
if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_EXT_TABLE)
ext_table_support = 1;
if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL)
no_free_on_del = 1;
if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF) {
readwrite_concur_lf_support = 1;
/* Enable not freeing internal memory/index on delete.
* If internal RCU is enabled, freeing of internal memory/index
* is done on delete
*/
no_free_on_del = 1;
}
/* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
if (use_local_cache)
/*
* Increase number of slots by total number of indices
* that can be stored in the lcore caches
* except for the first cache
*/
num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
(LCORE_CACHE_SIZE - 1) + 1;
else
num_key_slots = params->entries + 1;
snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
/* Create ring (Dummy slot index is not enqueued) */
r = rte_ring_create_elem(ring_name, sizeof(uint32_t),
rte_align32pow2(num_key_slots), params->socket_id, 0);
if (r == NULL) {
HASH_LOG(ERR, "memory allocation failed");
goto err;
}
const uint32_t num_buckets = rte_align32pow2(params->entries) /
RTE_HASH_BUCKET_ENTRIES;
/* Create ring for extendable buckets. */
if (ext_table_support) {
snprintf(ext_ring_name, sizeof(ext_ring_name), "HT_EXT_%s",
params->name);
r_ext = rte_ring_create_elem(ext_ring_name, sizeof(uint32_t),
rte_align32pow2(num_buckets + 1),
params->socket_id, 0);
if (r_ext == NULL) {
HASH_LOG(ERR, "ext buckets memory allocation "
"failed");
goto err;
}
}
snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
rte_mcfg_tailq_write_lock();
/* guarantee there's no existing: this is normally already checked
* by ring creation above */
TAILQ_FOREACH(te, hash_list, next) {
h = (struct rte_hash *) te->data;
if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
break;
}
h = NULL;
if (te != NULL) {
rte_errno = EEXIST;
te = NULL;
goto err_unlock;
}
te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
if (te == NULL) {
HASH_LOG(ERR, "tailq entry allocation failed");
goto err_unlock;
}
h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
RTE_CACHE_LINE_SIZE, params->socket_id);
if (h == NULL) {
HASH_LOG(ERR, "memory allocation failed");
goto err_unlock;
}
buckets = rte_zmalloc_socket(NULL,
num_buckets * sizeof(struct rte_hash_bucket),
RTE_CACHE_LINE_SIZE, params->socket_id);
if (buckets == NULL) {
HASH_LOG(ERR, "buckets memory allocation failed");
goto err_unlock;
}
/* Allocate same number of extendable buckets */
if (ext_table_support) {
buckets_ext = rte_zmalloc_socket(NULL,
num_buckets * sizeof(struct rte_hash_bucket),
RTE_CACHE_LINE_SIZE, params->socket_id);
if (buckets_ext == NULL) {
HASH_LOG(ERR, "ext buckets memory allocation "
"failed");
goto err_unlock;
}
/* Populate ext bkt ring. We reserve 0 similar to the
* key-data slot, just in case in future we want to
* use bucket index for the linked list and 0 means NULL
* for next bucket
*/
for (i = 1; i <= num_buckets; i++)
rte_ring_sp_enqueue_elem(r_ext, &i, sizeof(uint32_t));
if (readwrite_concur_lf_support) {
ext_bkt_to_free = rte_zmalloc(NULL, sizeof(uint32_t) *
num_key_slots, 0);
if (ext_bkt_to_free == NULL) {
HASH_LOG(ERR, "ext bkt to free memory allocation "
"failed");
goto err_unlock;
}
}
}
const uint32_t key_entry_size =
RTE_ALIGN(sizeof(struct rte_hash_key) + params->key_len,
KEY_ALIGNMENT);
const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
k = rte_zmalloc_socket(NULL, key_tbl_size,
RTE_CACHE_LINE_SIZE, params->socket_id);
if (k == NULL) {
HASH_LOG(ERR, "memory allocation failed");
goto err_unlock;
}
tbl_chng_cnt = rte_zmalloc_socket(NULL, sizeof(uint32_t),
RTE_CACHE_LINE_SIZE, params->socket_id);
if (tbl_chng_cnt == NULL) {
HASH_LOG(ERR, "memory allocation failed");
goto err_unlock;
}
/*
* If x86 architecture is used, select appropriate compare function,
* which may use x86 intrinsics, otherwise use memcmp
*/
#if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
/* Select function to compare keys */
switch (params->key_len) {
case 16:
h->cmp_jump_table_idx = KEY_16_BYTES;
break;
case 32:
h->cmp_jump_table_idx = KEY_32_BYTES;
break;
case 48:
h->cmp_jump_table_idx = KEY_48_BYTES;
break;
case 64:
h->cmp_jump_table_idx = KEY_64_BYTES;
break;
case 80:
h->cmp_jump_table_idx = KEY_80_BYTES;
break;
case 96:
h->cmp_jump_table_idx = KEY_96_BYTES;
break;
case 112:
h->cmp_jump_table_idx = KEY_112_BYTES;
break;
case 128:
h->cmp_jump_table_idx = KEY_128_BYTES;
break;
default:
/* If key is not multiple of 16, use generic memcmp */
h->cmp_jump_table_idx = KEY_OTHER_BYTES;
}
#else
h->cmp_jump_table_idx = KEY_OTHER_BYTES;
#endif
if (use_local_cache) {
local_free_slots = rte_zmalloc_socket(NULL,
sizeof(struct lcore_cache) * RTE_MAX_LCORE,
RTE_CACHE_LINE_SIZE, params->socket_id);
if (local_free_slots == NULL) {
HASH_LOG(ERR, "local free slots memory allocation failed");
goto err_unlock;
}
}
/* Default hash function */
#if defined(RTE_ARCH_X86)
default_hash_func = (rte_hash_function)rte_hash_crc;
#elif defined(RTE_ARCH_ARM64)
if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
default_hash_func = (rte_hash_function)rte_hash_crc;
#endif
/* Setup hash context */
strlcpy(h->name, params->name, sizeof(h->name));
h->entries = params->entries;
h->key_len = params->key_len;
h->key_entry_size = key_entry_size;
h->hash_func_init_val = params->hash_func_init_val;
h->num_buckets = num_buckets;
h->bucket_bitmask = h->num_buckets - 1;
h->buckets = buckets;
h->buckets_ext = buckets_ext;
h->free_ext_bkts = r_ext;
h->hash_func = (params->hash_func == NULL) ?
default_hash_func : params->hash_func;
h->key_store = k;
h->free_slots = r;
h->ext_bkt_to_free = ext_bkt_to_free;
h->tbl_chng_cnt = tbl_chng_cnt;
*h->tbl_chng_cnt = 0;
h->hw_trans_mem_support = hw_trans_mem_support;
h->use_local_cache = use_local_cache;
h->local_free_slots = local_free_slots;
h->readwrite_concur_support = readwrite_concur_support;
h->ext_table_support = ext_table_support;
h->writer_takes_lock = writer_takes_lock;
h->no_free_on_del = no_free_on_del;
h->readwrite_concur_lf_support = readwrite_concur_lf_support;
#if defined(RTE_ARCH_X86)
if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
else
#elif defined(RTE_ARCH_ARM64)
if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
h->sig_cmp_fn = RTE_HASH_COMPARE_NEON;
#if defined(RTE_HAS_SVE_ACLE)
if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SVE))
h->sig_cmp_fn = RTE_HASH_COMPARE_SVE;
#endif
}
else
#endif
h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
/* Writer threads need to take the lock when:
* 1) RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY is enabled OR
* 2) RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD is enabled
*/
if (h->writer_takes_lock) {
h->readwrite_lock = rte_malloc(NULL, sizeof(rte_rwlock_t),
RTE_CACHE_LINE_SIZE);
if (h->readwrite_lock == NULL)
goto err_unlock;
rte_rwlock_init(h->readwrite_lock);
}
/* Populate free slots ring. Entry zero is reserved for key misses. */
for (i = 1; i < num_key_slots; i++)
rte_ring_sp_enqueue_elem(r, &i, sizeof(uint32_t));
te->data = (void *) h;
TAILQ_INSERT_TAIL(hash_list, te, next);
rte_mcfg_tailq_write_unlock();
return h;
err_unlock:
rte_mcfg_tailq_write_unlock();
err:
rte_ring_free(r);
rte_ring_free(r_ext);
rte_free(te);
rte_free(local_free_slots);
rte_free(h);
rte_free(buckets);
rte_free(buckets_ext);
rte_free(k);
rte_free((void *)(uintptr_t)tbl_chng_cnt);
rte_free(ext_bkt_to_free);
return NULL;
}
void
rte_hash_free(struct rte_hash *h)
{
struct rte_tailq_entry *te;
struct rte_hash_list *hash_list;
if (h == NULL)
return;
hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
rte_mcfg_tailq_write_lock();
/* find out tailq entry */
TAILQ_FOREACH(te, hash_list, next) {
if (te->data == (void *) h)
break;
}
if (te == NULL) {
rte_mcfg_tailq_write_unlock();
return;
}
TAILQ_REMOVE(hash_list, te, next);
rte_mcfg_tailq_write_unlock();
if (h->dq)
rte_rcu_qsbr_dq_delete(h->dq);
if (h->use_local_cache)
rte_free(h->local_free_slots);
if (h->writer_takes_lock)
rte_free(h->readwrite_lock);
rte_ring_free(h->free_slots);
rte_ring_free(h->free_ext_bkts);
rte_free(h->key_store);
rte_free(h->buckets);
rte_free(h->buckets_ext);
rte_free((void *)(uintptr_t)h->tbl_chng_cnt);
rte_free(h->ext_bkt_to_free);
rte_free(h->hash_rcu_cfg);
rte_free(h);
rte_free(te);
}
hash_sig_t
rte_hash_hash(const struct rte_hash *h, const void *key)
{
/* calc hash result by key */
return h->hash_func(key, h->key_len, h->hash_func_init_val);
}
int32_t
rte_hash_max_key_id(const struct rte_hash *h)
{
RETURN_IF_TRUE((h == NULL), -EINVAL);
if (h->use_local_cache)
/*
* Increase number of slots by total number of indices
* that can be stored in the lcore caches
*/
return (h->entries + ((RTE_MAX_LCORE - 1) *
(LCORE_CACHE_SIZE - 1)));
else
return h->entries;
}
int32_t
rte_hash_count(const struct rte_hash *h)
{
uint32_t tot_ring_cnt, cached_cnt = 0;
uint32_t i, ret;
if (h == NULL)
return -EINVAL;
if (h->use_local_cache) {
tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
(LCORE_CACHE_SIZE - 1);
for (i = 0; i < RTE_MAX_LCORE; i++)
cached_cnt += h->local_free_slots[i].len;
ret = tot_ring_cnt - rte_ring_count(h->free_slots) -
cached_cnt;
} else {
tot_ring_cnt = h->entries;
ret = tot_ring_cnt - rte_ring_count(h->free_slots);
}
return ret;
}
/* Read write locks implemented using rte_rwlock */
static inline void
__hash_rw_writer_lock(const struct rte_hash *h)
__rte_exclusive_lock_function(&h->readwrite_lock)
__rte_no_thread_safety_analysis
{
if (h->writer_takes_lock && h->hw_trans_mem_support)
rte_rwlock_write_lock_tm(h->readwrite_lock);
else if (h->writer_takes_lock)
rte_rwlock_write_lock(h->readwrite_lock);
}
static inline void
__hash_rw_reader_lock(const struct rte_hash *h)
__rte_shared_lock_function(&h->readwrite_lock)
__rte_no_thread_safety_analysis
{
if (h->readwrite_concur_support && h->hw_trans_mem_support)
rte_rwlock_read_lock_tm(h->readwrite_lock);
else if (h->readwrite_concur_support)
rte_rwlock_read_lock(h->readwrite_lock);
}
static inline void
__hash_rw_writer_unlock(const struct rte_hash *h)
__rte_unlock_function(&h->readwrite_lock)
__rte_no_thread_safety_analysis
{
if (h->writer_takes_lock && h->hw_trans_mem_support)
rte_rwlock_write_unlock_tm(h->readwrite_lock);
else if (h->writer_takes_lock)
rte_rwlock_write_unlock(h->readwrite_lock);
}
static inline void
__hash_rw_reader_unlock(const struct rte_hash *h)
__rte_unlock_function(&h->readwrite_lock)
__rte_no_thread_safety_analysis
{
if (h->readwrite_concur_support && h->hw_trans_mem_support)
rte_rwlock_read_unlock_tm(h->readwrite_lock);
else if (h->readwrite_concur_support)
rte_rwlock_read_unlock(h->readwrite_lock);
}
void
rte_hash_reset(struct rte_hash *h)
{
uint32_t tot_ring_cnt, i;
unsigned int pending;
if (h == NULL)
return;
__hash_rw_writer_lock(h);
if (h->dq) {
/* Reclaim all the resources */
rte_rcu_qsbr_dq_reclaim(h->dq, ~0, NULL, &pending, NULL);
if (pending != 0)
HASH_LOG(ERR, "RCU reclaim all resources failed");
}
memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
*h->tbl_chng_cnt = 0;
/* reset the free ring */
rte_ring_reset(h->free_slots);
/* flush free extendable bucket ring and memory */
if (h->ext_table_support) {
memset(h->buckets_ext, 0, h->num_buckets *
sizeof(struct rte_hash_bucket));
rte_ring_reset(h->free_ext_bkts);
}
/* Repopulate the free slots ring. Entry zero is reserved for key misses */
if (h->use_local_cache)
tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
(LCORE_CACHE_SIZE - 1);
else
tot_ring_cnt = h->entries;
for (i = 1; i < tot_ring_cnt + 1; i++)
rte_ring_sp_enqueue_elem(h->free_slots, &i, sizeof(uint32_t));
/* Repopulate the free ext bkt ring. */
if (h->ext_table_support) {
for (i = 1; i <= h->num_buckets; i++)
rte_ring_sp_enqueue_elem(h->free_ext_bkts, &i,
sizeof(uint32_t));
}
if (h->use_local_cache) {
/* Reset local caches per lcore */
for (i = 0; i < RTE_MAX_LCORE; i++)
h->local_free_slots[i].len = 0;
}
__hash_rw_writer_unlock(h);
}
/*
* Function called to enqueue back an index in the cache/ring,
* as slot has not being used and it can be used in the
* next addition attempt.
*/
static inline void
enqueue_slot_back(const struct rte_hash *h,
struct lcore_cache *cached_free_slots,
uint32_t slot_id)
{
if (h->use_local_cache) {
cached_free_slots->objs[cached_free_slots->len] = slot_id;
cached_free_slots->len++;
} else
rte_ring_sp_enqueue_elem(h->free_slots, &slot_id,
sizeof(uint32_t));
}
/* Search a key from bucket and update its data.
* Writer holds the lock before calling this.
*/
static inline int32_t
search_and_update(const struct rte_hash *h, void *data, const void *key,
struct rte_hash_bucket *bkt, uint16_t sig)
{
int i;
struct rte_hash_key *k, *keys = h->key_store;
for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
if (bkt->sig_current[i] == sig) {
k = (struct rte_hash_key *) ((char *)keys +
bkt->key_idx[i] * h->key_entry_size);
if (rte_hash_cmp_eq(key, k->key, h) == 0) {
/* The store to application data at *data
* should not leak after the store to pdata
* in the key store. i.e. pdata is the guard
* variable. Release the application data
* to the readers.
*/
rte_atomic_store_explicit(&k->pdata,
data,
rte_memory_order_release);
/*
* Return index where key is stored,
* subtracting the first dummy index
*/
return bkt->key_idx[i] - 1;
}
}
}
return -1;
}
/* Only tries to insert at one bucket (@prim_bkt) without trying to push
* buckets around.
* return 1 if matching existing key, return 0 if succeeds, return -1 for no
* empty entry.
*/
static inline int32_t
rte_hash_cuckoo_insert_mw(const struct rte_hash *h,
struct rte_hash_bucket *prim_bkt,
struct rte_hash_bucket *sec_bkt,
const struct rte_hash_key *key, void *data,
uint16_t sig, uint32_t new_idx,
int32_t *ret_val)
{
unsigned int i;
struct rte_hash_bucket *cur_bkt;
int32_t ret;
__hash_rw_writer_lock(h);
/* Check if key was inserted after last check but before this
* protected region in case of inserting duplicated keys.
*/
ret = search_and_update(h, data, key, prim_bkt, sig);
if (ret != -1) {
__hash_rw_writer_unlock(h);
*ret_val = ret;
return 1;
}
FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
ret = search_and_update(h, data, key, cur_bkt, sig);
if (ret != -1) {
__hash_rw_writer_unlock(h);
*ret_val = ret;
return 1;
}
}
/* Insert new entry if there is room in the primary
* bucket.
*/
for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
/* Check if slot is available */
if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
prim_bkt->sig_current[i] = sig;
/* Store to signature and key should not
* leak after the store to key_idx. i.e.
* key_idx is the guard variable for signature
* and key.
*/
rte_atomic_store_explicit(&prim_bkt->key_idx[i],
new_idx,
rte_memory_order_release);
break;
}
}
__hash_rw_writer_unlock(h);
if (i != RTE_HASH_BUCKET_ENTRIES)
return 0;
/* no empty entry */
return -1;
}
/* Shift buckets along provided cuckoo_path (@leaf and @leaf_slot) and fill
* the path head with new entry (sig, alt_hash, new_idx)
* return 1 if matched key found, return -1 if cuckoo path invalided and fail,
* return 0 if succeeds.
*/
static inline int
rte_hash_cuckoo_move_insert_mw(const struct rte_hash *h,
struct rte_hash_bucket *bkt,
struct rte_hash_bucket *alt_bkt,
const struct rte_hash_key *key, void *data,
struct queue_node *leaf, uint32_t leaf_slot,
uint16_t sig, uint32_t new_idx,
int32_t *ret_val)
{
uint32_t prev_alt_bkt_idx;
struct rte_hash_bucket *cur_bkt;
struct queue_node *prev_node, *curr_node = leaf;
struct rte_hash_bucket *prev_bkt, *curr_bkt = leaf->bkt;
uint32_t prev_slot, curr_slot = leaf_slot;
int32_t ret;
__hash_rw_writer_lock(h);
/* In case empty slot was gone before entering protected region */
if (curr_bkt->key_idx[curr_slot] != EMPTY_SLOT) {
__hash_rw_writer_unlock(h);
return -1;
}
/* Check if key was inserted after last check but before this
* protected region.
*/
ret = search_and_update(h, data, key, bkt, sig);
if (ret != -1) {
__hash_rw_writer_unlock(h);
*ret_val = ret;
return 1;
}
FOR_EACH_BUCKET(cur_bkt, alt_bkt) {
ret = search_and_update(h, data, key, cur_bkt, sig);
if (ret != -1) {
__hash_rw_writer_unlock(h);
*ret_val = ret;
return 1;
}
}
while (likely(curr_node->prev != NULL)) {
prev_node = curr_node->prev;
prev_bkt = prev_node->bkt;
prev_slot = curr_node->prev_slot;
prev_alt_bkt_idx = get_alt_bucket_index(h,
prev_node->cur_bkt_idx,
prev_bkt->sig_current[prev_slot]);
if (unlikely(&h->buckets[prev_alt_bkt_idx]
!= curr_bkt)) {
/* revert it to empty, otherwise duplicated keys */
rte_atomic_store_explicit(&curr_bkt->key_idx[curr_slot],
EMPTY_SLOT,
rte_memory_order_release);
__hash_rw_writer_unlock(h);
return -1;
}
if (h->readwrite_concur_lf_support) {
/* Inform the previous move. The current move need
* not be informed now as the current bucket entry
* is present in both primary and secondary.
* Since there is one writer, load acquires on
* tbl_chng_cnt are not required.
*/
rte_atomic_store_explicit(h->tbl_chng_cnt,
*h->tbl_chng_cnt + 1,
rte_memory_order_release);
/* The store to sig_current should not
* move above the store to tbl_chng_cnt.
*/
rte_atomic_thread_fence(rte_memory_order_release);
}
/* Need to swap current/alt sig to allow later
* Cuckoo insert to move elements back to its
* primary bucket if available
*/
curr_bkt->sig_current[curr_slot] =
prev_bkt->sig_current[prev_slot];
/* Release the updated bucket entry */
rte_atomic_store_explicit(&curr_bkt->key_idx[curr_slot],
prev_bkt->key_idx[prev_slot],
rte_memory_order_release);
curr_slot = prev_slot;
curr_node = prev_node;
curr_bkt = curr_node->bkt;
}
if (h->readwrite_concur_lf_support) {
/* Inform the previous move. The current move need
* not be informed now as the current bucket entry
* is present in both primary and secondary.
* Since there is one writer, load acquires on
* tbl_chng_cnt are not required.
*/
rte_atomic_store_explicit(h->tbl_chng_cnt,
*h->tbl_chng_cnt + 1,
rte_memory_order_release);
/* The store to sig_current should not
* move above the store to tbl_chng_cnt.
*/
rte_atomic_thread_fence(rte_memory_order_release);
}
curr_bkt->sig_current[curr_slot] = sig;
/* Release the new bucket entry */
rte_atomic_store_explicit(&curr_bkt->key_idx[curr_slot],
new_idx,
rte_memory_order_release);
__hash_rw_writer_unlock(h);
return 0;
}
/*
* Make space for new key, using bfs Cuckoo Search and Multi-Writer safe
* Cuckoo
*/
static inline int
rte_hash_cuckoo_make_space_mw(const struct rte_hash *h,
struct rte_hash_bucket *bkt,
struct rte_hash_bucket *sec_bkt,
const struct rte_hash_key *key, void *data,
uint16_t sig, uint32_t bucket_idx,
uint32_t new_idx, int32_t *ret_val)
{
unsigned int i;
struct queue_node queue[RTE_HASH_BFS_QUEUE_MAX_LEN];
struct queue_node *tail, *head;
struct rte_hash_bucket *curr_bkt, *alt_bkt;
uint32_t cur_idx, alt_idx;
tail = queue;
head = queue + 1;
tail->bkt = bkt;
tail->prev = NULL;
tail->prev_slot = -1;
tail->cur_bkt_idx = bucket_idx;
/* Cuckoo bfs Search */
while (likely(tail != head && head <
queue + RTE_HASH_BFS_QUEUE_MAX_LEN -
RTE_HASH_BUCKET_ENTRIES)) {
curr_bkt = tail->bkt;
cur_idx = tail->cur_bkt_idx;
for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
if (curr_bkt->key_idx[i] == EMPTY_SLOT) {
int32_t ret = rte_hash_cuckoo_move_insert_mw(h,
bkt, sec_bkt, key, data,