1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef _RTE_FBK_HASH_H_
6 #define _RTE_FBK_HASH_H_
11 * This is a hash table implementation for four byte keys (fbk).
13 * Note that the return value of the add function should always be checked as,
14 * if a bucket is full, the key is not added even if there is space in other
15 * buckets. This keeps the lookup function very simple and therefore fast.
20 #include <sys/queue.h>
28 #include <rte_config.h>
29 #include <rte_hash_crc.h>
30 #include <rte_jhash.h>
32 #ifndef RTE_FBK_HASH_INIT_VAL_DEFAULT
33 /** Initialising value used when calculating hash. */
34 #define RTE_FBK_HASH_INIT_VAL_DEFAULT 0xFFFFFFFF
37 /** The maximum number of entries in the hash table that is supported. */
38 #define RTE_FBK_HASH_ENTRIES_MAX (1 << 20)
40 /** The maximum number of entries in each bucket that is supported. */
41 #define RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX 256
43 /** Maximum size of string for naming the hash. */
44 #define RTE_FBK_HASH_NAMESIZE 32
46 /** Type of function that can be used for calculating the hash value. */
47 typedef uint32_t (*rte_fbk_hash_fn)(uint32_t key, uint32_t init_val);
49 /** Parameters used when creating four-byte key hash table. */
50 struct rte_fbk_hash_params {
51 const char *name; /**< Name of the hash table. */
52 uint32_t entries; /**< Total number of entries. */
53 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
54 int socket_id; /**< Socket to allocate memory on. */
55 rte_fbk_hash_fn hash_func; /**< The hash function. */
56 uint32_t init_val; /**< For initialising hash function. */
59 /** Individual entry in the four-byte key hash table. */
60 union rte_fbk_hash_entry {
61 uint64_t whole_entry; /**< For accessing entire entry. */
63 uint16_t is_entry; /**< Non-zero if entry is active. */
64 uint16_t value; /**< Value returned by lookup. */
65 uint32_t key; /**< Key used to find value. */
66 } entry; /**< For accessing each entry part. */
70 /** The four-byte key hash table structure. */
71 struct rte_fbk_hash_table {
72 char name[RTE_FBK_HASH_NAMESIZE]; /**< Name of the hash. */
73 uint32_t entries; /**< Total number of entries. */
74 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
75 uint32_t used_entries; /**< How many entries are used. */
76 uint32_t bucket_mask; /**< To find which bucket the key is in. */
77 uint32_t bucket_shift; /**< Convert bucket to table offset. */
78 rte_fbk_hash_fn hash_func; /**< The hash function. */
79 uint32_t init_val; /**< For initialising hash function. */
81 /** A flat table of all buckets. */
82 union rte_fbk_hash_entry t[];
86 * Find the offset into hash table of the bucket containing a particular key.
89 * Pointer to hash table.
91 * Key to calculate bucket for.
93 * Offset into hash table.
95 static inline uint32_t
96 rte_fbk_hash_get_bucket(const struct rte_fbk_hash_table *ht, uint32_t key)
98 return (ht->hash_func(key, ht->init_val) & ht->bucket_mask) <<
103 * Add a key to an existing hash table with bucket id.
104 * This operation is not multi-thread safe
105 * and should only be called from one thread.
108 * Hash table to add the key to.
110 * Key to add to the hash table.
112 * Value to associate with key.
114 * Bucket to associate with key.
116 * 0 if ok, or negative value on error.
119 rte_fbk_hash_add_key_with_bucket(struct rte_fbk_hash_table *ht,
120 uint32_t key, uint16_t value, uint32_t bucket)
123 * The writing of a new value to the hash table is done as a single
124 * 64bit operation. This should help prevent individual entries being
125 * corrupted due to race conditions, but it's still possible to
126 * overwrite entries that have just been made valid.
128 const uint64_t new_entry = ((uint64_t)(key) << 32) |
129 ((uint64_t)(value) << 16) |
130 1; /* 1 = is_entry bit. */
133 for (i = 0; i < ht->entries_per_bucket; i++) {
134 /* Set entry if unused. */
135 if (! ht->t[bucket + i].entry.is_entry) {
136 ht->t[bucket + i].whole_entry = new_entry;
140 /* Change value if key already exists. */
141 if (ht->t[bucket + i].entry.key == key) {
142 ht->t[bucket + i].entry.value = value;
147 return -ENOSPC; /* No space in bucket. */
151 * Add a key to an existing hash table. This operation is not multi-thread safe
152 * and should only be called from one thread.
155 * Hash table to add the key to.
157 * Key to add to the hash table.
159 * Value to associate with key.
161 * 0 if ok, or negative value on error.
164 rte_fbk_hash_add_key(struct rte_fbk_hash_table *ht,
165 uint32_t key, uint16_t value)
167 return rte_fbk_hash_add_key_with_bucket(ht,
168 key, value, rte_fbk_hash_get_bucket(ht, key));
172 * Remove a key with a given bucket id from an existing hash table.
173 * This operation is not multi-thread
174 * safe and should only be called from one thread.
177 * Hash table to remove the key from.
179 * Key to remove from the hash table.
181 * Bucket id associate with key.
183 * 0 if ok, or negative value on error.
186 rte_fbk_hash_delete_key_with_bucket(struct rte_fbk_hash_table *ht,
187 uint32_t key, uint32_t bucket)
189 uint32_t last_entry = ht->entries_per_bucket - 1;
192 for (i = 0; i < ht->entries_per_bucket; i++) {
193 if (ht->t[bucket + i].entry.key == key) {
194 /* Find last key in bucket. */
195 for (j = ht->entries_per_bucket - 1; j > i; j-- ) {
196 if (! ht->t[bucket + j].entry.is_entry) {
201 * Move the last key to the deleted key's position, and
202 * delete the last key. lastEntry and i may be same but
205 ht->t[bucket + i].whole_entry =
206 ht->t[bucket + last_entry].whole_entry;
207 ht->t[bucket + last_entry].whole_entry = 0;
214 return -ENOENT; /* Key didn't exist. */
218 * Remove a key from an existing hash table. This operation is not multi-thread
219 * safe and should only be called from one thread.
222 * Hash table to remove the key from.
224 * Key to remove from the hash table.
226 * 0 if ok, or negative value on error.
229 rte_fbk_hash_delete_key(struct rte_fbk_hash_table *ht, uint32_t key)
231 return rte_fbk_hash_delete_key_with_bucket(ht,
232 key, rte_fbk_hash_get_bucket(ht, key));
236 * Find a key in the hash table with a given bucketid.
237 * This operation is multi-thread safe.
240 * Hash table to look in.
244 * Bucket associate to the key.
246 * The value that was associated with the key, or negative value on error.
249 rte_fbk_hash_lookup_with_bucket(const struct rte_fbk_hash_table *ht,
250 uint32_t key, uint32_t bucket)
252 union rte_fbk_hash_entry current_entry;
255 for (i = 0; i < ht->entries_per_bucket; i++) {
256 /* Single read of entry, which should be atomic. */
257 current_entry.whole_entry = ht->t[bucket + i].whole_entry;
258 if (! current_entry.entry.is_entry) {
259 return -ENOENT; /* Error once we hit an empty field. */
261 if (current_entry.entry.key == key) {
262 return current_entry.entry.value;
265 return -ENOENT; /* Key didn't exist. */
269 * Find a key in the hash table. This operation is multi-thread safe.
272 * Hash table to look in.
276 * The value that was associated with the key, or negative value on error.
279 rte_fbk_hash_lookup(const struct rte_fbk_hash_table *ht, uint32_t key)
281 return rte_fbk_hash_lookup_with_bucket(ht,
282 key, rte_fbk_hash_get_bucket(ht, key));
286 * Delete all entries in a hash table. This operation is not multi-thread
287 * safe and should only be called from one thread.
290 * Hash table to delete entries in.
293 rte_fbk_hash_clear_all(struct rte_fbk_hash_table *ht)
295 memset(ht->t, 0, sizeof(ht->t[0]) * ht->entries);
296 ht->used_entries = 0;
300 * Find what fraction of entries are being used.
303 * Hash table to find how many entries are being used in.
305 * Load factor of the hash table, or negative value on error.
308 rte_fbk_hash_get_load_factor(struct rte_fbk_hash_table *ht)
310 return (double)ht->used_entries / (double)ht->entries;
314 * Performs a lookup for an existing hash table, and returns a pointer to
315 * the table if found.
318 * Name of the hash table to find
321 * pointer to hash table structure or NULL on error with rte_errno
322 * set appropriately. Possible rte_errno values include:
323 * - ENOENT - required entry not available to return.
325 struct rte_fbk_hash_table *rte_fbk_hash_find_existing(const char *name);
328 * Create a new hash table for use with four byte keys.
331 * Parameters used in creation of hash table.
334 * Pointer to hash table structure that is used in future hash table
335 * operations, or NULL on error with rte_errno set appropriately.
336 * Possible rte_errno error values include:
337 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
338 * - E_RTE_SECONDARY - function was called from a secondary process instance
339 * - EINVAL - invalid parameter value passed to function
340 * - ENOSPC - the maximum number of memzones has already been allocated
341 * - EEXIST - a memzone with the same name already exists
342 * - ENOMEM - no appropriate memory area found in which to create memzone
344 struct rte_fbk_hash_table * \
345 rte_fbk_hash_create(const struct rte_fbk_hash_params *params);
348 * Free all memory used by a hash table.
349 * Has no effect on hash tables allocated in memory zones
352 * Hash table to deallocate.
354 void rte_fbk_hash_free(struct rte_fbk_hash_table *ht);
360 #endif /* _RTE_FBK_HASH_H_ */