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 ****************************************************************************
26 * A cuckoo hash table consists of two sub tables. Each entry can
27 * hash to a position in each table. If, on entry, its position is
28 * found to be occupied, the existing element is moved to it's other
29 * location. This recurses until success or a loop is found. If a
30 * loop is found the table is rehashed.
32 * See http://www.it-c.dk/people/pagh/papers/cuckoo-jour.pdf
35 #ifndef NET_ACCEL_CUCKOO_HASH_H
36 #define NET_ACCEL_CUCKOO_HASH_H
38 /*! Type used for hash table keys of ip pairs */
44 /* Technically only 1 bit, but use 16 to make key a round
49 /*! Type used for hash table keys of mac addresses */
50 typedef u64 cuckoo_hash_mac_key;
52 /*! This type is designed to be large enough to hold all supported key
53 * sizes to avoid having to malloc storage for them.
55 typedef u64 cuckoo_hash_key;
57 /*! Type used for the values stored in the hash table */
58 typedef int cuckoo_hash_value;
60 /*! Type used for the hash used to index the table */
61 typedef u32 cuckoo_hash;
63 /*! How long to spend displacing values when adding before giving up
65 #define CUCKOO_HASH_MAX_LOOP (hashtab->length)
67 /*! State of hash table entry */
69 CUCKOO_HASH_STATE_VACANT = 0,
70 CUCKOO_HASH_STATE_OCCUPIED
73 /*! An entry in the hash table */
75 cuckoo_hash_state state;
77 cuckoo_hash_value value;
80 /*! A cuckoo hash table */
82 /*! The length of each table (NB. there are two tables of this
85 /*! The length of each table in bits */
87 /*! The length of the key in bytes */
89 /*! The number of entries currently stored in the table */
91 /*! Index into table used by cuckoo_hash_iterate */
92 unsigned iterate_index;
94 /* parameter of hash functions */
95 /*! The "a" parameter of the first hash function */
97 /*! The "a" parameter of the second hash function */
100 /*! The first table */
101 cuckoo_hash_entry *table0;
102 /*! The second table */
103 cuckoo_hash_entry *table1;
106 /*! Initialise the cuckoo has table
108 * \param hashtab A pointer to an unitialised hash table structure
109 * \param length_bits The number of elements in each table equals
111 * \param key_length The length of the key in bytes
113 * \return 0 on success, -ENOMEM if it couldn't allocate the tables
116 int cuckoo_hash_init(cuckoo_hash_table *hashtab, unsigned length_bits,
117 unsigned key_length);
120 /*! Destroy a hash table
122 * \param hashtab A hash table that has previously been passed to a
123 * successful call of cuckoo_hash_init()
126 void cuckoo_hash_destroy(cuckoo_hash_table *hashtab);
129 /*! Lookup an entry in the hash table
131 * \param hashtab The hash table in which to look.
132 * \param key Pointer to a mac address to use as the key
133 * \param value On exit set to the value stored if key was present
135 * \return 0 if not present in the table, non-zero if it is (and value
136 * is set accordingly)
139 int cuckoo_hash_lookup(cuckoo_hash_table *hashtab,
140 cuckoo_hash_key *key,
141 cuckoo_hash_value *value);
143 /*! Add an entry to the hash table. Key must not be a duplicate of
144 * anything already in the table. If this is a risk, see
145 * cuckoo_hash_add_check
147 * \param hashtab The hash table to add the entry to
148 * \param key Pointer to a mac address to use as a key
149 * \param value The value to store
150 * \param can_rehash Flag to allow the add function to rehash the
153 * \return 0 on success, non-zero on failure. -ENOSPC means it just
154 * couldn't find anywhere to put it - this is bad and probably means
155 * an entry has been dropped on the floor (but the entry you just
156 * tried to add may now be included)
159 int cuckoo_hash_add(cuckoo_hash_table *hashtab,
160 cuckoo_hash_key *key,
161 cuckoo_hash_value value,
164 /*! Same as cuckoo_hash_add but first checks to ensure entry is not
166 * \return -EBUSY if already there
170 int cuckoo_hash_add_check(cuckoo_hash_table *hashtab,
171 cuckoo_hash_key *key,
172 cuckoo_hash_value value,
174 /*! Remove an entry from the table
176 * \param hashtab The hash table to remove the entry from
177 * \param key The key that was used to previously add the entry
179 * \return 0 on success, -EINVAL if the entry couldn't be found
182 int cuckoo_hash_remove(cuckoo_hash_table *hashtab, cuckoo_hash_key *key);
185 /*! Helper for those using mac addresses to convert to a key for the
188 static inline cuckoo_hash_mac_key cuckoo_mac_to_key(const u8 *mac)
190 return (cuckoo_hash_mac_key)(mac[0])
191 | (cuckoo_hash_mac_key)(mac[1]) << 8
192 | (cuckoo_hash_mac_key)(mac[2]) << 16
193 | (cuckoo_hash_mac_key)(mac[3]) << 24
194 | (cuckoo_hash_mac_key)(mac[4]) << 32
195 | (cuckoo_hash_mac_key)(mac[5]) << 40;
199 /*! Update an entry already in the hash table to take a new value
201 * \param hashtab The hash table to add the entry to
202 * \param key Pointer to a mac address to use as a key
203 * \param value The value to store
205 * \return 0 on success, non-zero on failure.
207 int cuckoo_hash_update(cuckoo_hash_table *hashtab, cuckoo_hash_key *key,
208 cuckoo_hash_value value);
211 /*! Go through the hash table and return all used entries (one per call)
213 * \param hashtab The hash table to iterate over
214 * \param key Pointer to a key to take the returned key
215 * \param value Pointer to a value to take the returned value
217 * \return 0 on success (key, value set), non-zero on failure.
219 int cuckoo_hash_iterate(cuckoo_hash_table *hashtab,
220 cuckoo_hash_key *key, cuckoo_hash_value *value);
221 void cuckoo_hash_iterate_reset(cuckoo_hash_table *hashtab);
223 /* debug, not compiled by default */
224 void cuckoo_hash_valid(cuckoo_hash_table *hashtab);
225 void cuckoo_hash_dump(cuckoo_hash_table *hashtab);
227 #endif /* NET_ACCEL_CUCKOO_HASH_H */