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_config.h>
28 #include <rte_hash_crc.h>
29 #include <rte_jhash.h>
31 #ifndef RTE_FBK_HASH_INIT_VAL_DEFAULT
32 /** Initialising value used when calculating hash. */
33 #define RTE_FBK_HASH_INIT_VAL_DEFAULT 0xFFFFFFFF
36 /** The maximum number of entries in the hash table that is supported. */
37 #define RTE_FBK_HASH_ENTRIES_MAX (1 << 20)
39 /** The maximum number of entries in each bucket that is supported. */
40 #define RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX 256
42 /** Maximum size of string for naming the hash. */
43 #define RTE_FBK_HASH_NAMESIZE 32
45 /** Type of function that can be used for calculating the hash value. */
46 typedef uint32_t (*rte_fbk_hash_fn)(uint32_t key, uint32_t init_val);
48 /** Parameters used when creating four-byte key hash table. */
49 struct rte_fbk_hash_params {
50 const char *name; /**< Name of the hash table. */
51 uint32_t entries; /**< Total number of entries. */
52 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
53 int socket_id; /**< Socket to allocate memory on. */
54 rte_fbk_hash_fn hash_func; /**< The hash function. */
55 uint32_t init_val; /**< For initialising hash function. */
58 /** Individual entry in the four-byte key hash table. */
59 union rte_fbk_hash_entry {
60 uint64_t whole_entry; /**< For accessing entire entry. */
62 uint16_t is_entry; /**< Non-zero if entry is active. */
63 uint16_t value; /**< Value returned by lookup. */
64 uint32_t key; /**< Key used to find value. */
65 } entry; /**< For accessing each entry part. */
69 /** The four-byte key hash table structure. */
70 struct rte_fbk_hash_table {
71 char name[RTE_FBK_HASH_NAMESIZE]; /**< Name of the hash. */
72 uint32_t entries; /**< Total number of entries. */
73 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
74 uint32_t used_entries; /**< How many entries are used. */
75 uint32_t bucket_mask; /**< To find which bucket the key is in. */
76 uint32_t bucket_shift; /**< Convert bucket to table offset. */
77 rte_fbk_hash_fn hash_func; /**< The hash function. */
78 uint32_t init_val; /**< For initialising hash function. */
80 /** A flat table of all buckets. */
81 union rte_fbk_hash_entry t[];
85 * Find the offset into hash table of the bucket containing a particular key.
88 * Pointer to hash table.
90 * Key to calculate bucket for.
92 * Offset into hash table.
94 static inline uint32_t
95 rte_fbk_hash_get_bucket(const struct rte_fbk_hash_table *ht, uint32_t key)
97 return (ht->hash_func(key, ht->init_val) & ht->bucket_mask) <<
102 * Add a key to an existing hash table with bucket id.
103 * This operation is not multi-thread safe
104 * and should only be called from one thread.
107 * Hash table to add the key to.
109 * Key to add to the hash table.
111 * Value to associate with key.
113 * Bucket to associate with key.
115 * 0 if ok, or negative value on error.
118 rte_fbk_hash_add_key_with_bucket(struct rte_fbk_hash_table *ht,
119 uint32_t key, uint16_t value, uint32_t bucket)
122 * The writing of a new value to the hash table is done as a single
123 * 64bit operation. This should help prevent individual entries being
124 * corrupted due to race conditions, but it's still possible to
125 * overwrite entries that have just been made valid.
127 const uint64_t new_entry = ((uint64_t)(key) << 32) |
128 ((uint64_t)(value) << 16) |
129 1; /* 1 = is_entry bit. */
132 for (i = 0; i < ht->entries_per_bucket; i++) {
133 /* Set entry if unused. */
134 if (! ht->t[bucket + i].entry.is_entry) {
135 ht->t[bucket + i].whole_entry = new_entry;
139 /* Change value if key already exists. */
140 if (ht->t[bucket + i].entry.key == key) {
141 ht->t[bucket + i].entry.value = value;
146 return -ENOSPC; /* No space in bucket. */
150 * Add a key to an existing hash table. This operation is not multi-thread safe
151 * and should only be called from one thread.
154 * Hash table to add the key to.
156 * Key to add to the hash table.
158 * Value to associate with key.
160 * 0 if ok, or negative value on error.
163 rte_fbk_hash_add_key(struct rte_fbk_hash_table *ht,
164 uint32_t key, uint16_t value)
166 return rte_fbk_hash_add_key_with_bucket(ht,
167 key, value, rte_fbk_hash_get_bucket(ht, key));
171 * Remove a key with a given bucket id from an existing hash table.
172 * This operation is not multi-thread
173 * safe and should only be called from one thread.
176 * Hash table to remove the key from.
178 * Key to remove from the hash table.
180 * Bucket id associate with key.
182 * 0 if ok, or negative value on error.
185 rte_fbk_hash_delete_key_with_bucket(struct rte_fbk_hash_table *ht,
186 uint32_t key, uint32_t bucket)
188 uint32_t last_entry = ht->entries_per_bucket - 1;
191 for (i = 0; i < ht->entries_per_bucket; i++) {
192 if (ht->t[bucket + i].entry.key == key) {
193 /* Find last key in bucket. */
194 for (j = ht->entries_per_bucket - 1; j > i; j-- ) {
195 if (! ht->t[bucket + j].entry.is_entry) {
200 * Move the last key to the deleted key's position, and
201 * delete the last key. lastEntry and i may be same but
204 ht->t[bucket + i].whole_entry =
205 ht->t[bucket + last_entry].whole_entry;
206 ht->t[bucket + last_entry].whole_entry = 0;
213 return -ENOENT; /* Key didn't exist. */
217 * Remove a key from an existing hash table. This operation is not multi-thread
218 * safe and should only be called from one thread.
221 * Hash table to remove the key from.
223 * Key to remove from the hash table.
225 * 0 if ok, or negative value on error.
228 rte_fbk_hash_delete_key(struct rte_fbk_hash_table *ht, uint32_t key)
230 return rte_fbk_hash_delete_key_with_bucket(ht,
231 key, rte_fbk_hash_get_bucket(ht, key));
235 * Find a key in the hash table with a given bucketid.
236 * This operation is multi-thread safe.
239 * Hash table to look in.
243 * Bucket associate to the key.
245 * The value that was associated with the key, or negative value on error.
248 rte_fbk_hash_lookup_with_bucket(const struct rte_fbk_hash_table *ht,
249 uint32_t key, uint32_t bucket)
251 union rte_fbk_hash_entry current_entry;
254 for (i = 0; i < ht->entries_per_bucket; i++) {
255 /* Single read of entry, which should be atomic. */
256 current_entry.whole_entry = ht->t[bucket + i].whole_entry;
257 if (! current_entry.entry.is_entry) {
258 return -ENOENT; /* Error once we hit an empty field. */
260 if (current_entry.entry.key == key) {
261 return current_entry.entry.value;
264 return -ENOENT; /* Key didn't exist. */
268 * Find a key in the hash table. This operation is multi-thread safe.
271 * Hash table to look in.
275 * The value that was associated with the key, or negative value on error.
278 rte_fbk_hash_lookup(const struct rte_fbk_hash_table *ht, uint32_t key)
280 return rte_fbk_hash_lookup_with_bucket(ht,
281 key, rte_fbk_hash_get_bucket(ht, key));
285 * Delete all entries in a hash table. This operation is not multi-thread
286 * safe and should only be called from one thread.
289 * Hash table to delete entries in.
292 rte_fbk_hash_clear_all(struct rte_fbk_hash_table *ht)
294 memset(ht->t, 0, sizeof(ht->t[0]) * ht->entries);
295 ht->used_entries = 0;
299 * Find what fraction of entries are being used.
302 * Hash table to find how many entries are being used in.
304 * Load factor of the hash table, or negative value on error.
307 rte_fbk_hash_get_load_factor(struct rte_fbk_hash_table *ht)
309 return (double)ht->used_entries / (double)ht->entries;
313 * Performs a lookup for an existing hash table, and returns a pointer to
314 * the table if found.
317 * Name of the hash table to find
320 * pointer to hash table structure or NULL on error with rte_errno
321 * set appropriately. Possible rte_errno values include:
322 * - ENOENT - required entry not available to return.
324 struct rte_fbk_hash_table *rte_fbk_hash_find_existing(const char *name);
327 * Create a new hash table for use with four byte keys.
330 * Parameters used in creation of hash table.
333 * Pointer to hash table structure that is used in future hash table
334 * operations, or NULL on error with rte_errno set appropriately.
335 * Possible rte_errno error values include:
336 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
337 * - E_RTE_SECONDARY - function was called from a secondary process instance
338 * - EINVAL - invalid parameter value passed to function
339 * - ENOSPC - the maximum number of memzones has already been allocated
340 * - EEXIST - a memzone with the same name already exists
341 * - ENOMEM - no appropriate memory area found in which to create memzone
343 struct rte_fbk_hash_table * \
344 rte_fbk_hash_create(const struct rte_fbk_hash_params *params);
347 * Free all memory used by a hash table.
348 * Has no effect on hash tables allocated in memory zones
351 * Hash table to deallocate.
353 void rte_fbk_hash_free(struct rte_fbk_hash_table *ht);
359 #endif /* _RTE_FBK_HASH_H_ */