4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_FBK_HASH_H_
35 #define _RTE_FBK_HASH_H_
40 * This is a hash table implementation for four byte keys (fbk).
42 * Note that the return value of the add function should always be checked as,
43 * if a bucket is full, the key is not added even if there is space in other
44 * buckets. This keeps the lookup function very simple and therefore fast.
49 #include <sys/queue.h>
57 #ifndef RTE_FBK_HASH_FUNC_DEFAULT
58 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_CRC32)
59 #include <rte_hash_crc.h>
60 /** Default four-byte key hash function if none is specified. */
61 #define RTE_FBK_HASH_FUNC_DEFAULT rte_hash_crc_4byte
63 #include <rte_jhash.h>
64 #define RTE_FBK_HASH_FUNC_DEFAULT rte_jhash_1word
68 #ifndef RTE_FBK_HASH_INIT_VAL_DEFAULT
69 /** Initialising value used when calculating hash. */
70 #define RTE_FBK_HASH_INIT_VAL_DEFAULT 0xFFFFFFFF
73 /** The maximum number of entries in the hash table that is supported. */
74 #define RTE_FBK_HASH_ENTRIES_MAX (1 << 20)
76 /** The maximum number of entries in each bucket that is supported. */
77 #define RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX 256
79 /** Maximum size of string for naming the hash. */
80 #define RTE_FBK_HASH_NAMESIZE 32
82 /** Type of function that can be used for calculating the hash value. */
83 typedef uint32_t (*rte_fbk_hash_fn)(uint32_t key, uint32_t init_val);
85 /** Parameters used when creating four-byte key hash table. */
86 struct rte_fbk_hash_params {
87 const char *name; /**< Name of the hash table. */
88 uint32_t entries; /**< Total number of entries. */
89 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
90 int socket_id; /**< Socket to allocate memory on. */
91 rte_fbk_hash_fn hash_func; /**< The hash function. */
92 uint32_t init_val; /**< For initialising hash function. */
95 /** Individual entry in the four-byte key hash table. */
96 union rte_fbk_hash_entry {
97 uint64_t whole_entry; /**< For accessing entire entry. */
99 uint16_t is_entry; /**< Non-zero if entry is active. */
100 uint16_t value; /**< Value returned by lookup. */
101 uint32_t key; /**< Key used to find value. */
102 } entry; /**< For accessing each entry part. */
106 /** The four-byte key hash table structure. */
107 struct rte_fbk_hash_table {
108 char name[RTE_FBK_HASH_NAMESIZE]; /**< Name of the hash. */
109 uint32_t entries; /**< Total number of entries. */
110 uint32_t entries_per_bucket; /**< Number of entries in a bucket. */
111 uint32_t used_entries; /**< How many entries are used. */
112 uint32_t bucket_mask; /**< To find which bucket the key is in. */
113 uint32_t bucket_shift; /**< Convert bucket to table offset. */
114 rte_fbk_hash_fn hash_func; /**< The hash function. */
115 uint32_t init_val; /**< For initialising hash function. */
117 /** A flat table of all buckets. */
118 union rte_fbk_hash_entry t[];
122 * Find the offset into hash table of the bucket containing a particular key.
125 * Pointer to hash table.
127 * Key to calculate bucket for.
129 * Offset into hash table.
131 static inline uint32_t
132 rte_fbk_hash_get_bucket(const struct rte_fbk_hash_table *ht, uint32_t key)
134 return (ht->hash_func(key, ht->init_val) & ht->bucket_mask) <<
139 * Add a key to an existing hash table with bucket id.
140 * This operation is not multi-thread safe
141 * and should only be called from one thread.
144 * Hash table to add the key to.
146 * Key to add to the hash table.
148 * Value to associate with key.
150 * Bucket to associate with key.
152 * 0 if ok, or negative value on error.
155 rte_fbk_hash_add_key_with_bucket(struct rte_fbk_hash_table *ht,
156 uint32_t key, uint16_t value, uint32_t bucket)
159 * The writing of a new value to the hash table is done as a single
160 * 64bit operation. This should help prevent individual entries being
161 * corrupted due to race conditions, but it's still possible to
162 * overwrite entries that have just been made valid.
164 const uint64_t new_entry = ((uint64_t)(key) << 32) |
165 ((uint64_t)(value) << 16) |
166 1; /* 1 = is_entry bit. */
169 for (i = 0; i < ht->entries_per_bucket; i++) {
170 /* Set entry if unused. */
171 if (! ht->t[bucket + i].entry.is_entry) {
172 ht->t[bucket + i].whole_entry = new_entry;
176 /* Change value if key already exists. */
177 if (ht->t[bucket + i].entry.key == key) {
178 ht->t[bucket + i].entry.value = value;
183 return -ENOSPC; /* No space in bucket. */
187 * Add a key to an existing hash table. This operation is not multi-thread safe
188 * and should only be called from one thread.
191 * Hash table to add the key to.
193 * Key to add to the hash table.
195 * Value to associate with key.
197 * 0 if ok, or negative value on error.
200 rte_fbk_hash_add_key(struct rte_fbk_hash_table *ht,
201 uint32_t key, uint16_t value)
203 return rte_fbk_hash_add_key_with_bucket(ht,
204 key, value, rte_fbk_hash_get_bucket(ht, key));
208 * Remove a key with a given bucket id from an existing hash table.
209 * This operation is not multi-thread
210 * safe and should only be called from one thread.
213 * Hash table to remove the key from.
215 * Key to remove from the hash table.
217 * Bucket id associate with key.
219 * 0 if ok, or negative value on error.
222 rte_fbk_hash_delete_key_with_bucket(struct rte_fbk_hash_table *ht,
223 uint32_t key, uint32_t bucket)
225 uint32_t last_entry = ht->entries_per_bucket - 1;
228 for (i = 0; i < ht->entries_per_bucket; i++) {
229 if (ht->t[bucket + i].entry.key == key) {
230 /* Find last key in bucket. */
231 for (j = ht->entries_per_bucket - 1; j > i; j-- ) {
232 if (! ht->t[bucket + j].entry.is_entry) {
237 * Move the last key to the deleted key's position, and
238 * delete the last key. lastEntry and i may be same but
241 ht->t[bucket + i].whole_entry =
242 ht->t[bucket + last_entry].whole_entry;
243 ht->t[bucket + last_entry].whole_entry = 0;
250 return -ENOENT; /* Key didn't exist. */
254 * Remove a key from an existing hash table. This operation is not multi-thread
255 * safe and should only be called from one thread.
258 * Hash table to remove the key from.
260 * Key to remove from the hash table.
262 * 0 if ok, or negative value on error.
265 rte_fbk_hash_delete_key(struct rte_fbk_hash_table *ht, uint32_t key)
267 return rte_fbk_hash_delete_key_with_bucket(ht,
268 key, rte_fbk_hash_get_bucket(ht, key));
272 * Find a key in the hash table with a given bucketid.
273 * This operation is multi-thread safe.
276 * Hash table to look in.
280 * Bucket associate to the key.
282 * The value that was associated with the key, or negative value on error.
285 rte_fbk_hash_lookup_with_bucket(const struct rte_fbk_hash_table *ht,
286 uint32_t key, uint32_t bucket)
288 union rte_fbk_hash_entry current_entry;
291 for (i = 0; i < ht->entries_per_bucket; i++) {
292 /* Single read of entry, which should be atomic. */
293 current_entry.whole_entry = ht->t[bucket + i].whole_entry;
294 if (! current_entry.entry.is_entry) {
295 return -ENOENT; /* Error once we hit an empty field. */
297 if (current_entry.entry.key == key) {
298 return current_entry.entry.value;
301 return -ENOENT; /* Key didn't exist. */
305 * Find a key in the hash table. This operation is multi-thread safe.
308 * Hash table to look in.
312 * The value that was associated with the key, or negative value on error.
315 rte_fbk_hash_lookup(const struct rte_fbk_hash_table *ht, uint32_t key)
317 return rte_fbk_hash_lookup_with_bucket(ht,
318 key, rte_fbk_hash_get_bucket(ht, key));
322 * Delete all entries in a hash table. This operation is not multi-thread
323 * safe and should only be called from one thread.
326 * Hash table to delete entries in.
329 rte_fbk_hash_clear_all(struct rte_fbk_hash_table *ht)
331 memset(ht->t, 0, sizeof(ht->t[0]) * ht->entries);
332 ht->used_entries = 0;
336 * Find what fraction of entries are being used.
339 * Hash table to find how many entries are being used in.
341 * Load factor of the hash table, or negative value on error.
344 rte_fbk_hash_get_load_factor(struct rte_fbk_hash_table *ht)
346 return (double)ht->used_entries / (double)ht->entries;
350 * Performs a lookup for an existing hash table, and returns a pointer to
351 * the table if found.
354 * Name of the hash table to find
357 * pointer to hash table structure or NULL on error with rte_errno
358 * set appropriately. Possible rte_errno values include:
359 * - ENOENT - required entry not available to return.
361 struct rte_fbk_hash_table *rte_fbk_hash_find_existing(const char *name);
364 * Create a new hash table for use with four byte keys.
367 * Parameters used in creation of hash table.
370 * Pointer to hash table structure that is used in future hash table
371 * operations, or NULL on error with rte_errno set appropriately.
372 * Possible rte_errno error values include:
373 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
374 * - E_RTE_SECONDARY - function was called from a secondary process instance
375 * - EINVAL - invalid parameter value passed to function
376 * - ENOSPC - the maximum number of memzones has already been allocated
377 * - EEXIST - a memzone with the same name already exists
378 * - ENOMEM - no appropriate memory area found in which to create memzone
380 struct rte_fbk_hash_table * \
381 rte_fbk_hash_create(const struct rte_fbk_hash_params *params);
384 * Free all memory used by a hash table.
385 * Has no effect on hash tables allocated in memory zones
388 * Hash table to deallocate.
390 void rte_fbk_hash_free(struct rte_fbk_hash_table *ht);
396 #endif /* _RTE_FBK_HASH_H_ */