1 /****************************************************************************
2 * Solarflare driver for Xen network acceleration
4 * Copyright 2006-2008: Solarflare Communications Inc,
5 * 9501 Jeronimo Road, Suite 250,
6 * Irvine, CA 92618, USA
8 * Maintained by Solarflare Communications <linux-xen-drivers@solarflare.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation, incorporated herein by reference.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ****************************************************************************
25 #include <linux/types.h> /* needed for linux/random.h */
26 #include <linux/random.h>
27 #include <linux/slab.h>
29 #include "accel_cuckoo_hash.h"
30 #include "accel_util.h"
32 static inline int cuckoo_hash_key_compare(cuckoo_hash_table *hashtab,
33 cuckoo_hash_key *key1,
34 cuckoo_hash_key *key2)
36 return !memcmp(key1, key2, hashtab->key_length);
40 static inline void cuckoo_hash_key_set(cuckoo_hash_key *key1,
41 cuckoo_hash_key *key2)
48 * Sets hash function parameters. Chooses "a" to be odd, 0 < a < 2^w
49 * where w is the length of the key
51 static void set_hash_parameters(cuckoo_hash_table *hashtab)
54 hashtab->a0 = hashtab->a1 = 0;
56 /* Make sure random */
57 get_random_bytes(&hashtab->a0, hashtab->key_length);
58 get_random_bytes(&hashtab->a1, hashtab->key_length);
64 /* Being different is good */
65 if (hashtab->a0 != hashtab->a1)
71 int cuckoo_hash_init(cuckoo_hash_table *hashtab, unsigned length_bits,
75 unsigned length = 1 << length_bits;
77 BUG_ON(length_bits >= sizeof(unsigned) * 8);
78 BUG_ON(key_length > sizeof(cuckoo_hash_key));
80 table_mem = kzalloc(sizeof(cuckoo_hash_entry) * 2 * length, GFP_KERNEL);
82 if (table_mem == NULL)
85 hashtab->length = length;
86 hashtab->length_bits = length_bits;
87 hashtab->key_length = key_length;
90 hashtab->table0 = (cuckoo_hash_entry *)table_mem;
91 hashtab->table1 = (cuckoo_hash_entry *)
92 (table_mem + length * sizeof(cuckoo_hash_entry));
94 set_hash_parameters(hashtab);
98 EXPORT_SYMBOL_GPL(cuckoo_hash_init);
100 void cuckoo_hash_destroy(cuckoo_hash_table *hashtab)
102 if (hashtab->table0 != NULL)
103 kfree(hashtab->table0);
106 EXPORT_SYMBOL_GPL(cuckoo_hash_destroy);
109 * This computes sizeof(cuckoo_hash) bits of hash, not all will be
110 * necessarily used, but the hash function throws away any that
113 static inline void cuckoo_compute_hash_helper(cuckoo_hash_table *hashtab,
118 u64 multiply_result = 0, a_temp, x_temp;
125 * As the mod and div operations in the function effectively
126 * reduce and shift the bits of the product down to just the
127 * third word, we need only compute that and return it as a
130 * Do enough long multiplication to get the word we need
133 /* This assumes things about the sizes of the key and hash */
134 BUG_ON(hashtab->key_length % sizeof(u32) != 0);
135 BUG_ON(sizeof(cuckoo_hash) != sizeof(u32));
140 for (i = 0; i < hashtab->key_length / sizeof(u32); i++) {
144 multiply_result = (a_temp * x_temp) + carry;
145 carry = (multiply_result >> 32) & 0xffffffff;
148 *result = multiply_result & 0xffffffff;
153 * Want to implement (ax mod 2^w) div 2^(w-q) for odd a, 0 < a < 2^w;
154 * w is the length of the key, q is the length of the hash, I think.
155 * See http://www.it-c.dk/people/pagh/papers/cuckoo-jour.pdf
157 static cuckoo_hash cuckoo_compute_hash(cuckoo_hash_table *hashtab,
158 cuckoo_hash_key *key,
161 unsigned q = hashtab->length_bits;
162 unsigned shift = 32 - q;
163 unsigned mask = ((1 << q) - 1) << shift;
166 cuckoo_compute_hash_helper(hashtab, a, key, &hash);
169 * Take the top few bits to get the right length for this
172 hash = (hash & mask) >> shift;
174 BUG_ON(hash >= hashtab->length);
180 static int cuckoo_hash_lookup0(cuckoo_hash_table *hashtab,
181 cuckoo_hash_key *key,
182 cuckoo_hash_value *value)
184 cuckoo_hash hash = cuckoo_compute_hash(hashtab, key, &hashtab->a0);
186 if ((hashtab->table0[hash].state == CUCKOO_HASH_STATE_OCCUPIED)
187 && cuckoo_hash_key_compare(hashtab, &(hashtab->table0[hash].key),
189 *value = hashtab->table0[hash].value;
196 static int cuckoo_hash_lookup1(cuckoo_hash_table *hashtab,
197 cuckoo_hash_key *key,
198 cuckoo_hash_value *value)
200 cuckoo_hash hash = cuckoo_compute_hash(hashtab, key, &hashtab->a1);
202 if ((hashtab->table1[hash].state == CUCKOO_HASH_STATE_OCCUPIED)
203 && cuckoo_hash_key_compare(hashtab, &(hashtab->table1[hash].key),
205 *value = hashtab->table1[hash].value;
213 int cuckoo_hash_lookup(cuckoo_hash_table *hashtab, cuckoo_hash_key *key,
214 cuckoo_hash_value *value)
216 return cuckoo_hash_lookup0(hashtab, key, value)
217 || cuckoo_hash_lookup1(hashtab, key, value);
219 EXPORT_SYMBOL_GPL(cuckoo_hash_lookup);
222 /* Transfer any active entries from "old_table" into hashtab */
223 static int cuckoo_hash_transfer_entries(cuckoo_hash_table *hashtab,
224 cuckoo_hash_entry *old_table,
228 cuckoo_hash_entry *entry;
230 hashtab->entries = 0;
232 for (i = 0; i < capacity; i++) {
233 entry = &old_table[i];
234 if (entry->state == CUCKOO_HASH_STATE_OCCUPIED) {
235 rc = cuckoo_hash_add(hashtab, &(entry->key),
247 int cuckoo_hash_rehash(cuckoo_hash_table *hashtab)
249 cuckoo_hash_entry *new_table;
250 cuckoo_hash_table old_hashtab;
251 int resize = 0, rc, rehash_count;
254 * Store old tables so we can access the existing values and
257 memcpy(&old_hashtab, hashtab, sizeof(cuckoo_hash_table));
259 /* resize if hashtable is more than half full */
260 if (old_hashtab.entries > old_hashtab.length &&
261 old_hashtab.length_bits < 32)
266 new_table = kmalloc(sizeof(cuckoo_hash_entry) * 4 * hashtab->length,
268 if (new_table == NULL) {
273 hashtab->length = 2 * hashtab->length;
274 hashtab->length_bits++;
276 new_table = kmalloc(sizeof(cuckoo_hash_entry) * 2 * hashtab->length,
278 if (new_table == NULL) {
285 * Point hashtab to new memory region so we can try to
286 * construct new table
288 hashtab->table0 = new_table;
289 hashtab->table1 = (cuckoo_hash_entry *)
290 ((char *)new_table + hashtab->length * sizeof(cuckoo_hash_entry));
295 /* Zero the new tables */
296 memset(new_table, 0, hashtab->length * 2 * sizeof(cuckoo_hash_entry));
298 /* Choose new parameters for the hash functions */
299 set_hash_parameters(hashtab);
302 * Multiply old_table_length by 2 as the length refers to each
303 * table, and there are two of them. This assumes that they
304 * are arranged sequentially in memory, so assert it
306 BUG_ON(((char *)old_hashtab.table1) !=
307 ((char *)old_hashtab.table0 + old_hashtab.length
308 * sizeof(cuckoo_hash_entry)));
309 rc = cuckoo_hash_transfer_entries(hashtab, old_hashtab.table0,
310 old_hashtab.length * 2);
315 if (rehash_count < CUCKOO_HASH_MAX_LOOP) {
317 * Wanted to rehash, but rather than
318 * recurse we can just do it here
323 * Didn't manage to rehash, so let's
324 * go up a size (if we haven't already
327 if (!resize && hashtab->length_bits < 32) {
340 /* Success, I think. Free up the old table */
341 kfree(old_hashtab.table0);
343 /* We should have put all the entries from old table in the new one */
344 BUG_ON(hashtab->entries != old_hashtab.entries);
348 EPRINTK("%s: Rehash failed, giving up\n", __FUNCTION__);
349 /* Some other error, give up, at least restore table to how it was */
350 memcpy(hashtab, &old_hashtab, sizeof(cuckoo_hash_table));
355 EXPORT_SYMBOL_GPL(cuckoo_hash_rehash);
359 cuckoo_hash_insert_or_displace(cuckoo_hash_entry *table, unsigned hash,
360 cuckoo_hash_key *key,
361 cuckoo_hash_value value,
362 cuckoo_hash_key *displaced_key,
363 cuckoo_hash_value *displaced_value)
365 if (table[hash].state == CUCKOO_HASH_STATE_VACANT) {
366 cuckoo_hash_key_set(&(table[hash].key), key);
367 table[hash].value = value;
368 table[hash].state = CUCKOO_HASH_STATE_OCCUPIED;
372 cuckoo_hash_key_set(displaced_key, &(table[hash].key));
373 *displaced_value = table[hash].value;
374 cuckoo_hash_key_set(&(table[hash].key), key);
375 table[hash].value = value;
382 int cuckoo_hash_add(cuckoo_hash_table *hashtab, cuckoo_hash_key *key,
383 cuckoo_hash_value value, int can_rehash)
385 cuckoo_hash hash0, hash1;
387 cuckoo_hash_key key1, key2;
389 cuckoo_hash_key_set(&key1, key);
394 hash0 = cuckoo_compute_hash(hashtab, &key1, &hashtab->a0);
395 if (cuckoo_hash_insert_or_displace(hashtab->table0, hash0,
403 hash1 = cuckoo_compute_hash(hashtab, &key2, &hashtab->a1);
404 if (cuckoo_hash_insert_or_displace(hashtab->table1, hash1,
411 } while (++i < CUCKOO_HASH_MAX_LOOP);
414 if ((rc = cuckoo_hash_rehash(hashtab)) < 0) {
416 * Give up - this will drop whichever
417 * key/value pair we have currently displaced
425 EPRINTK("%s: failed hash add\n", __FUNCTION__);
427 * Couldn't do it - bad as we've now removed some random thing
428 * from the table, and will just drop it on the floor. Better
429 * would be to somehow revert the table to the state it was in
434 EXPORT_SYMBOL_GPL(cuckoo_hash_add);
437 int cuckoo_hash_add_check(cuckoo_hash_table *hashtab,
438 cuckoo_hash_key *key, cuckoo_hash_value value,
443 if (cuckoo_hash_lookup(hashtab, key, &stored_value))
446 return cuckoo_hash_add(hashtab, key, value, can_rehash);
448 EXPORT_SYMBOL_GPL(cuckoo_hash_add_check);
451 int cuckoo_hash_remove(cuckoo_hash_table *hashtab, cuckoo_hash_key *key)
455 hash = cuckoo_compute_hash(hashtab, key, &hashtab->a0);
456 if ((hashtab->table0[hash].state == CUCKOO_HASH_STATE_OCCUPIED) &&
457 cuckoo_hash_key_compare(hashtab, &(hashtab->table0[hash].key),
459 hashtab->table0[hash].state = CUCKOO_HASH_STATE_VACANT;
464 hash = cuckoo_compute_hash(hashtab, key, &hashtab->a1);
465 if ((hashtab->table1[hash].state == CUCKOO_HASH_STATE_OCCUPIED) &&
466 cuckoo_hash_key_compare(hashtab, &(hashtab->table1[hash].key),
468 hashtab->table1[hash].state = CUCKOO_HASH_STATE_VACANT;
475 EXPORT_SYMBOL_GPL(cuckoo_hash_remove);
478 int cuckoo_hash_update(cuckoo_hash_table *hashtab, cuckoo_hash_key *key,
479 cuckoo_hash_value value)
483 hash = cuckoo_compute_hash(hashtab, key, &hashtab->a0);
484 if ((hashtab->table0[hash].state == CUCKOO_HASH_STATE_OCCUPIED) &&
485 cuckoo_hash_key_compare(hashtab, &(hashtab->table0[hash].key),
487 hashtab->table0[hash].value = value;
491 hash = cuckoo_compute_hash(hashtab, key, &hashtab->a1);
492 if ((hashtab->table1[hash].state == CUCKOO_HASH_STATE_OCCUPIED) &&
493 cuckoo_hash_key_compare(hashtab, &(hashtab->table1[hash].key),
495 hashtab->table1[hash].value = value;
501 EXPORT_SYMBOL_GPL(cuckoo_hash_update);
504 void cuckoo_hash_iterate_reset(cuckoo_hash_table *hashtab)
506 hashtab->iterate_index = 0;
508 EXPORT_SYMBOL_GPL(cuckoo_hash_iterate_reset);
511 int cuckoo_hash_iterate(cuckoo_hash_table *hashtab,
512 cuckoo_hash_key *key, cuckoo_hash_value *value)
516 while (hashtab->iterate_index < hashtab->length) {
517 index = hashtab->iterate_index;
518 ++hashtab->iterate_index;
519 if (hashtab->table0[index].state == CUCKOO_HASH_STATE_OCCUPIED) {
520 *key = hashtab->table0[index].key;
521 *value = hashtab->table0[index].value;
526 while (hashtab->iterate_index >= hashtab->length &&
527 hashtab->iterate_index < hashtab->length * 2) {
528 index = hashtab->iterate_index - hashtab->length;
529 ++hashtab->iterate_index;
530 if (hashtab->table1[index].state == CUCKOO_HASH_STATE_OCCUPIED) {
531 *key = hashtab->table1[index].key;
532 *value = hashtab->table1[index].value;
539 EXPORT_SYMBOL_GPL(cuckoo_hash_iterate);
543 void cuckoo_hash_valid(cuckoo_hash_table *hashtab)
545 int i, entry_count = 0;
547 for (i=0; i < hashtab->length; i++) {
548 EPRINTK_ON(hashtab->table0[i].state != CUCKOO_HASH_STATE_VACANT &&
549 hashtab->table0[i].state != CUCKOO_HASH_STATE_OCCUPIED);
550 if (hashtab->table0[i].state == CUCKOO_HASH_STATE_OCCUPIED)
552 EPRINTK_ON(hashtab->table1[i].state != CUCKOO_HASH_STATE_VACANT &&
553 hashtab->table1[i].state != CUCKOO_HASH_STATE_OCCUPIED);
554 if (hashtab->table1[i].state == CUCKOO_HASH_STATE_OCCUPIED)
558 if (entry_count != hashtab->entries) {
559 EPRINTK("%s: bad count\n", __FUNCTION__);
560 cuckoo_hash_dump(hashtab);
564 for (i=0; i< hashtab->length; i++) {
565 if (hashtab->table0[i].state == CUCKOO_HASH_STATE_OCCUPIED)
566 if (i != cuckoo_compute_hash(hashtab,
567 &hashtab->table0[i].key,
569 EPRINTK("%s: Bad key table 0 index %d\n",
571 cuckoo_hash_dump(hashtab);
574 if (hashtab->table1[i].state == CUCKOO_HASH_STATE_OCCUPIED)
575 if (i != cuckoo_compute_hash(hashtab,
576 &hashtab->table1[i].key,
578 EPRINTK("%s: Bad key table 1 index %d\n",
580 cuckoo_hash_dump(hashtab);
586 EXPORT_SYMBOL_GPL(cuckoo_hash_valid);
589 void cuckoo_hash_dump(cuckoo_hash_table *hashtab)
594 for (i=0; i < hashtab->length; i++) {
595 EPRINTK_ON(hashtab->table0[i].state != CUCKOO_HASH_STATE_VACANT &&
596 hashtab->table0[i].state != CUCKOO_HASH_STATE_OCCUPIED);
597 if (hashtab->table0[i].state == CUCKOO_HASH_STATE_OCCUPIED)
599 EPRINTK_ON(hashtab->table1[i].state != CUCKOO_HASH_STATE_VACANT &&
600 hashtab->table1[i].state != CUCKOO_HASH_STATE_OCCUPIED);
601 if (hashtab->table1[i].state == CUCKOO_HASH_STATE_OCCUPIED)
605 EPRINTK("======================\n");
606 EPRINTK("Cuckoo hash table dump\n");
607 EPRINTK("======================\n");
608 EPRINTK("length: %d; length_bits: %d; key_length: %d\n", hashtab->length,
609 hashtab->length_bits, hashtab->key_length);
610 EPRINTK("Recorded entries: %d\n", hashtab->entries);
611 EPRINTK("Counted entries: %d\n", entry_count);
612 EPRINTK("a0: %llx; a1: %llx\n", hashtab->a0, hashtab->a1);
613 EPRINTK("-----------------------------------------\n");
614 EPRINTK("Index Occupied Key Value Index0 Index1\n");
615 EPRINTK("-----------------------------------------\n");
616 for (i=0; i< hashtab->length; i++) {
617 if (hashtab->table0[i].state == CUCKOO_HASH_STATE_OCCUPIED)
618 EPRINTK("%d %d %llx %d %d %d\n", i,
619 hashtab->table0[i].state == CUCKOO_HASH_STATE_OCCUPIED,
620 hashtab->table0[i].key, hashtab->table0[i].value,
621 cuckoo_compute_hash(hashtab, &hashtab->table0[i].key,
623 cuckoo_compute_hash(hashtab, &hashtab->table0[i].key,
626 EPRINTK("%d %d - - - -\n", i,
627 hashtab->table0[i].state == CUCKOO_HASH_STATE_OCCUPIED);
630 EPRINTK("-----------------------------------------\n");
631 EPRINTK("Index Occupied Key Value Index0 Index1\n");
632 EPRINTK("-----------------------------------------\n");
633 for (i=0; i< hashtab->length; i++) {
634 if (hashtab->table1[i].state == CUCKOO_HASH_STATE_OCCUPIED)
635 EPRINTK("%d %d %llx %d %d %d\n", i,
636 hashtab->table1[i].state == CUCKOO_HASH_STATE_OCCUPIED,
637 hashtab->table1[i].key, hashtab->table1[i].value,
638 cuckoo_compute_hash(hashtab, &hashtab->table1[i].key,
640 cuckoo_compute_hash(hashtab, &hashtab->table1[i].key,
643 EPRINTK("%d %d - - - -\n", i,
644 hashtab->table1[i].state == CUCKOO_HASH_STATE_OCCUPIED);
646 EPRINTK("======================\n");
648 EXPORT_SYMBOL_GPL(cuckoo_hash_dump);