CNDP  22.08.0
cne_cuckoo_hash.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2016-2022 Intel Corporation
3  * Copyright (c) 2018 Arm Limited
4  */
5 
6 #ifndef _CNE_CUCKOO_HASH_H_
7 #define _CNE_CUCKOO_HASH_H_
14 #include "cne_cmp_x86.h" // for cne_hash_k112_cmp_eq, cne_hash_k128_cmp_eq
15 #include "cne_common.h" // for __cne_cache_aligned, CNE_IS_POWER_OF_2
16 #include "cne_hash.h" // for cne_hash_cmp_eq_t, CNE_HASH_NAMESIZE, cne_h...
17 #include "cne_ring.h" // for cne_ring_t
18 #include "cne_rwlock.h" // for cne_rwlock_t
19 
20 /* Macro to enable/disable run-time checking of function parameters */
21 #if defined(CNE_LIBCNE_HASH_DEBUG)
22 #define RETURN_IF_TRUE(cond, retval) \
23  do { \
24  if (cond) \
25  return retval; \
26  } while (0)
27 #else
28 #define RETURN_IF_TRUE(cond, retval)
29 #endif
30 
31 #if defined(CNE_LIBCNE_HASH_DEBUG)
32 #define ERR_IF_TRUE(cond, fmt, args...) \
33  do { \
34  if (cond) \
35  CNE_RET(fmt, ##args); \
36  } while (0)
37 #else
38 #define ERR_IF_TRUE(cond, fmt, args...)
39 #endif
40 
41 #include <cne_hash_crc.h>
42 #include <cne_jhash.h>
43 #include <stdint.h> // for uint32_t, uint8_t, uint16_t, uintptr_t
44 #include <string.h> // for memcmp, NULL
45 
46 /*
47  * All different options to select a key compare function,
48  * based on the key size and custom function.
49  */
50 // clang-format off
51 enum cmp_jump_table_case {
52  KEY_CUSTOM = 0,
53  KEY_16_BYTES,
54  KEY_32_BYTES,
55  KEY_48_BYTES,
56  KEY_64_BYTES,
57  KEY_80_BYTES,
58  KEY_96_BYTES,
59  KEY_112_BYTES,
60  KEY_128_BYTES,
61  KEY_OTHER_BYTES,
62  NUM_KEY_CMP_CASES,
63 };
64 // clang-format on
65 
66 /*
67  * Table storing all different key compare functions
68  * (multi-process supported)
69  */
70 // clang-format off
71 const cne_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
72  NULL,
73  cne_hash_k16_cmp_eq,
74  cne_hash_k32_cmp_eq,
75  cne_hash_k48_cmp_eq,
76  cne_hash_k64_cmp_eq,
77  cne_hash_k80_cmp_eq,
78  cne_hash_k96_cmp_eq,
79  cne_hash_k112_cmp_eq,
80  cne_hash_k128_cmp_eq,
81  memcmp
82 };
83 // clang-format on
84 
86 #define CNE_HASH_BUCKET_ENTRIES 8
87 
88 #if !CNE_IS_POWER_OF_2(CNE_HASH_BUCKET_ENTRIES)
89 #error CNE_HASH_BUCKET_ENTRIES must be a power of 2
90 #endif
91 
92 #define NULL_SIGNATURE 0
93 
94 #define EMPTY_SLOT 0
95 
96 #define KEY_ALIGNMENT 16
97 
98 #define LCORE_CACHE_SIZE 64
99 
100 #define CNE_HASH_BFS_QUEUE_MAX_LEN 1000
101 
102 #define CNE_XABORT_CUCKOO_PATH_INVALIDED 0x4
103 
104 #define CNE_HASH_TSX_MAX_RETRY 10
105 
106 struct lcore_cache {
107  unsigned len;
108  uint32_t objs[LCORE_CACHE_SIZE];
109 } __cne_cache_aligned;
110 
111 /* Structure that stores key-value pair */
112 struct cne_hash_key {
113  union {
114  uintptr_t idata;
115  void *pdata;
116  };
117  /* Variable key size */
118  char key[0];
119 };
120 
121 /* All different signature compare functions */
122 enum cne_hash_sig_compare_function {
123  CNE_HASH_COMPARE_SCALAR = 0,
124  CNE_HASH_COMPARE_SSE,
125  CNE_HASH_COMPARE_NEON,
126  CNE_HASH_COMPARE_NUM
127 };
128 
131  uint16_t sig_current[CNE_HASH_BUCKET_ENTRIES];
132 
133  uint32_t key_idx[CNE_HASH_BUCKET_ENTRIES];
134 
135  uint8_t flag[CNE_HASH_BUCKET_ENTRIES];
136 
137  void *next;
138 } __cne_cache_aligned;
139 
141 struct cne_hash {
143  uint32_t entries;
144  uint32_t num_buckets;
146  cne_ring_t *free_slots;
149  /* Fields used in lookup */
150  uint32_t key_len __cne_cache_aligned;
157  uint8_t no_free_on_del;
171  enum cmp_jump_table_case cmp_jump_table_idx;
173  enum cne_hash_sig_compare_function sig_cmp_fn;
175  uint32_t bucket_bitmask;
177  uint32_t key_entry_size;
179  void *key_store;
186  cne_ring_t *free_ext_bkts;
187  /* Stores index of an empty ext bkt to be recycled on calling
188  * cne_hash_del_xxx APIs. When lock free read-write concurrency is
189  * enabled, an empty ext bkt cannot be put into free list immediately
190  * (as readers might be using it still). Hence freeing of the ext bkt
191  * is piggy-backed to freeing of the key index.
192  */
193  uint32_t *ext_bkt_to_free;
194  uint32_t *tbl_chng_cnt;
196 } __cne_cache_aligned;
197 
198 struct queue_node {
199  struct cne_hash_bucket *bkt; /* Current bucket on the bfs search */
200  uint32_t cur_bkt_idx;
201 
202  struct queue_node *prev; /* Parent(bucket) in search path */
203  int prev_slot; /* Parent(slot) in search path */
204 };
205 
206 #endif
#define CNE_HASH_BUCKET_ENTRIES
int(* cne_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len)
Definition: cne_hash.h:64
uint32_t(* cne_hash_function)(const void *key, uint32_t key_len, uint32_t init_val)
Definition: cne_hash.h:61
#define CNE_HASH_NAMESIZE
Definition: cne_hash.h:24
uint32_t hash_func_init_val
void * key_store
struct cne_hash_bucket * buckets
uint8_t readwrite_concur_support
uint8_t no_free_on_del
cne_hash_function hash_func
char name[CNE_HASH_NAMESIZE]
uint32_t entries
uint32_t key_len __cne_cache_aligned
cne_hash_cmp_eq_t cne_hash_custom_cmp_eq
uint8_t readwrite_concur_lf_support
uint8_t writer_takes_lock
cne_ring_t * free_slots
cne_rwlock_t * readwrite_lock
uint32_t * tbl_chng_cnt
enum cmp_jump_table_case cmp_jump_table_idx
uint32_t num_buckets
uint32_t key_entry_size
enum cne_hash_sig_compare_function sig_cmp_fn
uint32_t bucket_bitmask
struct cne_hash_bucket * buckets_ext
cne_ring_t * free_ext_bkts
uint8_t ext_table_support
uint8_t hw_trans_mem_support