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.
27 #include <rte_hash_crc.h>
28 #include <rte_jhash.h>
30 #ifndef RTE_FBK_HASH_INIT_VAL_DEFAULT
31 /** Initialising value used when calculating hash. */
32 #define RTE_FBK_HASH_INIT_VAL_DEFAULT 0xFFFFFFFF
35 /** The maximum number of entries in the hash table that is supported. */
36 #define RTE_FBK_HASH_ENTRIES_MAX (1 << 20)
38 /** The maximum number of entries in each bucket that is supported. */
39 #define RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX 256
41 /** Maximum size of string for naming the hash. */
42 #define RTE_FBK_HASH_NAMESIZE 32
44 /** Type of function that can be used for calculating the hash value. */
45 typedef uint32_t (*rte_fbk_hash_fn)(uint32_t key, uint32_t init_val);
47 /** Parameters used when creating four-byte key hash table. */
48 struct rte_fbk_hash_params {
49 const char *name; /**< Name of the hash table. */
50 uint32_t entries; /**< Total number of entries. */
51 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
52 int socket_id; /**< Socket to allocate memory on. */
53 rte_fbk_hash_fn hash_func; /**< The hash function. */
54 uint32_t init_val; /**< For initialising hash function. */
57 /** Individual entry in the four-byte key hash table. */
58 union rte_fbk_hash_entry {
59 uint64_t whole_entry; /**< For accessing entire entry. */
61 uint16_t is_entry; /**< Non-zero if entry is active. */
62 uint16_t value; /**< Value returned by lookup. */
63 uint32_t key; /**< Key used to find value. */
64 } entry; /**< For accessing each entry part. */
68 /** The four-byte key hash table structure. */
69 struct rte_fbk_hash_table {
70 char name[RTE_FBK_HASH_NAMESIZE]; /**< Name of the hash. */
71 uint32_t entries; /**< Total number of entries. */
72 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
73 uint32_t used_entries; /**< How many entries are used. */
74 uint32_t bucket_mask; /**< To find which bucket the key is in. */
75 uint32_t bucket_shift; /**< Convert bucket to table offset. */
76 rte_fbk_hash_fn hash_func; /**< The hash function. */
77 uint32_t init_val; /**< For initialising hash function. */
79 /** A flat table of all buckets. */
80 union rte_fbk_hash_entry t[];
84 * Find the offset into hash table of the bucket containing a particular key.
87 * Pointer to hash table.
89 * Key to calculate bucket for.
91 * Offset into hash table.
93 static inline uint32_t
94 rte_fbk_hash_get_bucket(const struct rte_fbk_hash_table *ht, uint32_t key)
96 return (ht->hash_func(key, ht->init_val) & ht->bucket_mask) <<
101 * Add a key to an existing hash table with bucket id.
102 * This operation is not multi-thread safe
103 * and should only be called from one thread.
106 * Hash table to add the key to.
108 * Key to add to the hash table.
110 * Value to associate with key.
112 * Bucket to associate with key.
114 * 0 if ok, or negative value on error.
117 rte_fbk_hash_add_key_with_bucket(struct rte_fbk_hash_table *ht,
118 uint32_t key, uint16_t value, uint32_t bucket)
121 * The writing of a new value to the hash table is done as a single
122 * 64bit operation. This should help prevent individual entries being
123 * corrupted due to race conditions, but it's still possible to
124 * overwrite entries that have just been made valid.
126 const uint64_t new_entry = ((uint64_t)(key) << 32) |
127 ((uint64_t)(value) << 16) |
128 1; /* 1 = is_entry bit. */
131 for (i = 0; i < ht->entries_per_bucket; i++) {
132 /* Set entry if unused. */
133 if (! ht->t[bucket + i].entry.is_entry) {
134 ht->t[bucket + i].whole_entry = new_entry;
138 /* Change value if key already exists. */
139 if (ht->t[bucket + i].entry.key == key) {
140 ht->t[bucket + i].entry.value = value;
145 return -ENOSPC; /* No space in bucket. */
149 * Add a key to an existing hash table. This operation is not multi-thread safe
150 * and should only be called from one thread.
153 * Hash table to add the key to.
155 * Key to add to the hash table.
157 * Value to associate with key.
159 * 0 if ok, or negative value on error.
162 rte_fbk_hash_add_key(struct rte_fbk_hash_table *ht,
163 uint32_t key, uint16_t value)
165 return rte_fbk_hash_add_key_with_bucket(ht,
166 key, value, rte_fbk_hash_get_bucket(ht, key));
170 * Remove a key with a given bucket id from an existing hash table.
171 * This operation is not multi-thread
172 * safe and should only be called from one thread.
175 * Hash table to remove the key from.
177 * Key to remove from the hash table.
179 * Bucket id associate with key.
181 * 0 if ok, or negative value on error.
184 rte_fbk_hash_delete_key_with_bucket(struct rte_fbk_hash_table *ht,
185 uint32_t key, uint32_t bucket)
187 uint32_t last_entry = ht->entries_per_bucket - 1;
190 for (i = 0; i < ht->entries_per_bucket; i++) {
191 if (ht->t[bucket + i].entry.key == key) {
192 /* Find last key in bucket. */
193 for (j = ht->entries_per_bucket - 1; j > i; j-- ) {
194 if (! ht->t[bucket + j].entry.is_entry) {
199 * Move the last key to the deleted key's position, and
200 * delete the last key. lastEntry and i may be same but
203 ht->t[bucket + i].whole_entry =
204 ht->t[bucket + last_entry].whole_entry;
205 ht->t[bucket + last_entry].whole_entry = 0;
212 return -ENOENT; /* Key didn't exist. */
216 * Remove a key from an existing hash table. This operation is not multi-thread
217 * safe and should only be called from one thread.
220 * Hash table to remove the key from.
222 * Key to remove from the hash table.
224 * 0 if ok, or negative value on error.
227 rte_fbk_hash_delete_key(struct rte_fbk_hash_table *ht, uint32_t key)
229 return rte_fbk_hash_delete_key_with_bucket(ht,
230 key, rte_fbk_hash_get_bucket(ht, key));
234 * Find a key in the hash table with a given bucketid.
235 * This operation is multi-thread safe.
238 * Hash table to look in.
242 * Bucket associate to the key.
244 * The value that was associated with the key, or negative value on error.
247 rte_fbk_hash_lookup_with_bucket(const struct rte_fbk_hash_table *ht,
248 uint32_t key, uint32_t bucket)
250 union rte_fbk_hash_entry current_entry;
253 for (i = 0; i < ht->entries_per_bucket; i++) {
254 /* Single read of entry, which should be atomic. */
255 current_entry.whole_entry = ht->t[bucket + i].whole_entry;
256 if (! current_entry.entry.is_entry) {
257 return -ENOENT; /* Error once we hit an empty field. */
259 if (current_entry.entry.key == key) {
260 return current_entry.entry.value;
263 return -ENOENT; /* Key didn't exist. */
267 * Find a key in the hash table. This operation is multi-thread safe.
270 * Hash table to look in.
274 * The value that was associated with the key, or negative value on error.
277 rte_fbk_hash_lookup(const struct rte_fbk_hash_table *ht, uint32_t key)
279 return rte_fbk_hash_lookup_with_bucket(ht,
280 key, rte_fbk_hash_get_bucket(ht, key));
284 * Delete all entries in a hash table. This operation is not multi-thread
285 * safe and should only be called from one thread.
288 * Hash table to delete entries in.
291 rte_fbk_hash_clear_all(struct rte_fbk_hash_table *ht)
293 memset(ht->t, 0, sizeof(ht->t[0]) * ht->entries);
294 ht->used_entries = 0;
298 * Find what fraction of entries are being used.
301 * Hash table to find how many entries are being used in.
303 * Load factor of the hash table, or negative value on error.
306 rte_fbk_hash_get_load_factor(struct rte_fbk_hash_table *ht)
308 return (double)ht->used_entries / (double)ht->entries;
312 * Performs a lookup for an existing hash table, and returns a pointer to
313 * the table if found.
316 * Name of the hash table to find
319 * pointer to hash table structure or NULL on error with rte_errno
320 * set appropriately. Possible rte_errno values include:
321 * - ENOENT - required entry not available to return.
323 struct rte_fbk_hash_table *rte_fbk_hash_find_existing(const char *name);
326 * Create a new hash table for use with four byte keys.
329 * Parameters used in creation of hash table.
332 * Pointer to hash table structure that is used in future hash table
333 * operations, or NULL on error with rte_errno set appropriately.
334 * Possible rte_errno error values include:
335 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
336 * - E_RTE_SECONDARY - function was called from a secondary process instance
337 * - EINVAL - invalid parameter value passed to function
338 * - ENOSPC - the maximum number of memzones has already been allocated
339 * - EEXIST - a memzone with the same name already exists
340 * - ENOMEM - no appropriate memory area found in which to create memzone
342 struct rte_fbk_hash_table * \
343 rte_fbk_hash_create(const struct rte_fbk_hash_params *params);
346 * Free all memory used by a hash table.
347 * Has no effect on hash tables allocated in memory zones
350 * Hash table to deallocate.
352 void rte_fbk_hash_free(struct rte_fbk_hash_table *ht);
358 #endif /* _RTE_FBK_HASH_H_ */