blob: 968c7781c36f433ca33d49b1219543b00a6a0eed [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2001-2005 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#ifndef included_hash_h
39#define included_hash_h
40
41#include <vppinfra/error.h>
42#include <vppinfra/format.h>
43#include <vppinfra/vec.h>
44#include <vppinfra/vector.h>
45
46struct hash_header;
47
Dave Barachc3799992016-08-15 11:12:27 -040048typedef uword (hash_key_sum_function_t) (struct hash_header *, uword key);
Ed Warnickecb9cada2015-12-08 15:45:58 -070049typedef uword (hash_key_equal_function_t)
50 (struct hash_header *, uword key1, uword key2);
51
52/* Vector header for hash tables. */
Dave Barachc3799992016-08-15 11:12:27 -040053typedef struct hash_header
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 /* Number of elements in hash table. */
56 uword elts;
57
58 /* Flags as follows. */
59 u32 flags;
60
61 /* Set if user does not want table to auto-resize when sufficiently full. */
62#define HASH_FLAG_NO_AUTO_GROW (1 << 0)
63 /* Set if user does not want table to auto-resize when sufficiently empty. */
64#define HASH_FLAG_NO_AUTO_SHRINK (1 << 1)
65 /* Set when hash_next is in the process of iterating through this hash table. */
66#define HASH_FLAG_HASH_NEXT_IN_PROGRESS (1 << 2)
67
68 u32 log2_pair_size;
69
70 /* Function to compute the "sum" of a hash key.
71 Hash function is this sum modulo the prime size of
72 the hash table (vec_len (v)). */
Dave Barachc3799992016-08-15 11:12:27 -040073 hash_key_sum_function_t *key_sum;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75 /* Special values for key_sum "function". */
Dave Barachc3799992016-08-15 11:12:27 -040076#define KEY_FUNC_NONE (0) /*< sum = key */
77#define KEY_FUNC_POINTER_UWORD (1) /*< sum = *(uword *) key */
78#define KEY_FUNC_POINTER_U32 (2) /*< sum = *(u32 *) key */
79#define KEY_FUNC_STRING (3) /*< sum = string_key_sum, etc. */
Ole Troan73710c72018-06-04 22:27:49 +020080#define KEY_FUNC_MEM (4) /*< sum = mem_key_sum */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82 /* key comparison function */
Dave Barachc3799992016-08-15 11:12:27 -040083 hash_key_equal_function_t *key_equal;
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 /* Hook for user's data. Used to parameterize sum/equal functions. */
86 any user;
87
88 /* Format a (k,v) pair */
Dave Barachc3799992016-08-15 11:12:27 -040089 format_function_t *format_pair;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
91 /* Format function arg */
Dave Barachc3799992016-08-15 11:12:27 -040092 void *format_pair_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
94 /* Bit i is set if pair i is a user object (as opposed to being
95 either zero or an indirect array of pairs). */
Damjan Marion2b702da2022-03-17 17:54:48 +010096 uword *is_user;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097} hash_t;
98
Ed Warnickecb9cada2015-12-08 15:45:58 -070099/* Returns a pointer to the hash header given the vector pointer */
Dave Barachc3799992016-08-15 11:12:27 -0400100always_inline hash_t *
101hash_header (void *v)
102{
Damjan Mariona4a28f02022-03-17 15:46:25 +0100103 return vec_header (v);
Dave Barachc3799992016-08-15 11:12:27 -0400104}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
106/* Number of elements in the hash table */
Dave Barachc3799992016-08-15 11:12:27 -0400107always_inline uword
108hash_elts (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109{
Dave Barachc3799992016-08-15 11:12:27 -0400110 hash_t *h = hash_header (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 return v ? h->elts : 0;
112}
113
114/* Number of elements the hash table can hold */
Dave Barachc3799992016-08-15 11:12:27 -0400115always_inline uword
116hash_capacity (void *v)
117{
118 return vec_len (v);
119}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
121/* Returns 1 if the hash pair contains user data */
Dave Barachc3799992016-08-15 11:12:27 -0400122always_inline uword
123hash_is_user (void *v, uword i)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Dave Barachc3799992016-08-15 11:12:27 -0400125 hash_t *h = hash_header (v);
126 uword i0 = i / BITS (h->is_user[0]);
127 uword i1 = i % BITS (h->is_user[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 return (h->is_user[i0] & ((uword) 1 << i1)) != 0;
129}
130
131/* Set the format function and format argument for a hash table */
132always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400133hash_set_pair_format (void *v,
134 format_function_t * format_pair, void *format_pair_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
Dave Barachc3799992016-08-15 11:12:27 -0400136 hash_t *h = hash_header (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 h->format_pair = format_pair;
138 h->format_pair_arg = format_pair_arg;
139}
140
141/* Set hash table flags */
Dave Barachc3799992016-08-15 11:12:27 -0400142always_inline void
143hash_set_flags (void *v, uword flags)
144{
145 hash_header (v)->flags |= flags;
146}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
148/* Key value pairs. */
Dave Barachc3799992016-08-15 11:12:27 -0400149typedef struct
150{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 /* The Key */
152 uword key;
153
154 /* The Value. Length is 2^log2_pair_size - 1. */
155 uword value[0];
156} hash_pair_t;
157
158/* The indirect pair structure
159
160 If log2_pair_size > 0 we overload hash pairs
161 with indirect pairs for buckets with more than one
162 pair. */
Dave Barachc3799992016-08-15 11:12:27 -0400163typedef struct
164{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 /* pair vector */
Dave Barachc3799992016-08-15 11:12:27 -0400166 hash_pair_t *pairs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 /* padding */
Dave Barachc3799992016-08-15 11:12:27 -0400168 u8 pad[sizeof (uword) - sizeof (hash_pair_t *)];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 /* allocated length */
170 uword alloc_len;
Dave Barachc3799992016-08-15 11:12:27 -0400171}
172hash_pair_indirect_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
174/* Direct / Indirect pair union */
Dave Barachc3799992016-08-15 11:12:27 -0400175typedef union
176{
177 hash_pair_t direct;
178 hash_pair_indirect_t indirect;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179} hash_pair_union_t;
180
181#define LOG2_ALLOC_BITS (5)
182#define PAIR_BITS (BITS (uword) - LOG2_ALLOC_BITS)
183
184/* Log2 number of bytes allocated in pairs array. */
Dave Barachc3799992016-08-15 11:12:27 -0400185always_inline uword
186indirect_pair_get_log2_bytes (hash_pair_indirect_t * p)
187{
188 return p->alloc_len >> PAIR_BITS;
189}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
191/* Get the length of an indirect pair */
192always_inline uword
193indirect_pair_get_len (hash_pair_indirect_t * p)
194{
Dave Barachc3799992016-08-15 11:12:27 -0400195 if (!p->pairs)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 return 0;
197 else
198 return p->alloc_len & (((uword) 1 << PAIR_BITS) - 1);
199}
200
201/* Set the length of an indirect pair */
202always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400203indirect_pair_set (hash_pair_indirect_t * p, uword log2_alloc, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204{
205 ASSERT (len < ((uword) 1 << PAIR_BITS));
206 ASSERT (log2_alloc < ((uword) 1 << LOG2_ALLOC_BITS));
207 p->alloc_len = (log2_alloc << PAIR_BITS) | len;
208}
209
210/* internal routine to fetch value for given key */
Dave Barachc3799992016-08-15 11:12:27 -0400211uword *_hash_get (void *v, uword key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
213/* internal routine to fetch value (key, value) pair for given key */
Dave Barachc3799992016-08-15 11:12:27 -0400214hash_pair_t *_hash_get_pair (void *v, uword key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
216/* internal routine to unset a (key, value) pair */
Dave Barachc3799992016-08-15 11:12:27 -0400217void *_hash_unset (void *v, uword key, void *old_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
219/* internal routine to set a (key, value) pair, return the old value */
Dave Barachc3799992016-08-15 11:12:27 -0400220void *_hash_set3 (void *v, uword key, void *value, void *old_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
222/* Resize a hash table */
Dave Barachc3799992016-08-15 11:12:27 -0400223void *hash_resize (void *old, uword new_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
225/* duplicate a hash table */
Dave Barachc3799992016-08-15 11:12:27 -0400226void *hash_dup (void *old);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
228/* Returns the number of bytes used by a hash table */
Dave Barachc3799992016-08-15 11:12:27 -0400229uword hash_bytes (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231/* Public macro to set a (key, value) pair, return the old value */
232#define hash_set3(h,key,value,old_value) \
233({ \
234 uword _v = (uword) (value); \
235 (h) = _hash_set3 ((h), (uword) (key), (void *) &_v, (old_value)); \
236})
237
238/* Public macro to fetch value for given key */
239#define hash_get(h,key) _hash_get ((h), (uword) (key))
240
241/* Public macro to fetch value (key, value) pair for given key */
242#define hash_get_pair(h,key) _hash_get_pair ((h), (uword) (key))
243
244/* Public macro to set a (key, value) pair */
245#define hash_set(h,key,value) hash_set3(h,key,value,0)
246
247/* Public macro to set (key, 0) pair */
248#define hash_set1(h,key) (h) = _hash_set3(h,(uword) (key),0,0)
249
250/* Public macro to unset a (key, value) pair */
251#define hash_unset(h,key) ((h) = _hash_unset ((h), (uword) (key),0))
252
253/* Public macro to unset a (key, value) pair, return the old value */
254#define hash_unset3(h,key,old_value) ((h) = _hash_unset ((h), (uword) (key), (void *) (old_value)))
255
256/* get/set/unset for pointer keys. */
257
258/* Public macro to fetch value for given pointer key */
259#define hash_get_mem(h,key) _hash_get ((h), pointer_to_uword (key))
260
261/* Public macro to fetch (key, value) for given pointer key */
262#define hash_get_pair_mem(h,key) _hash_get_pair ((h), pointer_to_uword (key))
263
264/* Public macro to set (key, value) for pointer key */
265#define hash_set_mem(h,key,value) hash_set3 (h, pointer_to_uword (key), (value), 0)
266
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700267/* Public inline function allocate and copy key to use in hash for pointer key */
John Loe6bfeab2018-01-04 16:39:42 -0500268always_inline void
Neale Rannsc190cf02020-01-06 21:09:12 +0000269hash_set_mem_alloc (uword ** h, const void *key, uword v)
John Loe6bfeab2018-01-04 16:39:42 -0500270{
Damjan Marion56f54af2021-10-12 20:30:02 +0200271 int objsize = __builtin_object_size (key, 0);
John Loe6bfeab2018-01-04 16:39:42 -0500272 size_t ksz = hash_header (*h)->user;
Damjan Marion56f54af2021-10-12 20:30:02 +0200273 void *copy;
274 if (objsize > 0)
275 {
276 ASSERT (objsize == ksz);
277 copy = clib_mem_alloc (objsize);
278 clib_memcpy_fast (copy, key, objsize);
279 }
280 else
281 {
282 copy = clib_mem_alloc (ksz);
283 clib_memcpy_fast (copy, key, ksz);
284 }
John Loe6bfeab2018-01-04 16:39:42 -0500285 hash_set_mem (*h, copy, v);
286}
287
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288/* Public macro to set (key, 0) for pointer key */
289#define hash_set1_mem(h,key) hash_set3 ((h), pointer_to_uword (key), 0, 0)
290
291/* Public macro to unset (key, value) for pointer key */
292#define hash_unset_mem(h,key) ((h) = _hash_unset ((h), pointer_to_uword (key),0))
293
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700294/* Public inline function to unset pointer key and then free the key memory */
John Loe6bfeab2018-01-04 16:39:42 -0500295always_inline void
Neale Rannsc190cf02020-01-06 21:09:12 +0000296hash_unset_mem_free (uword ** h, const void *key)
John Loe6bfeab2018-01-04 16:39:42 -0500297{
298 hash_pair_t *hp = hash_get_pair_mem (*h, key);
John Lo5957a142018-01-18 12:44:50 -0500299 if (PREDICT_TRUE (hp != NULL))
300 {
Neale Rannsc190cf02020-01-06 21:09:12 +0000301 void *_k = uword_to_pointer (hp->key, void *);
302 hash_unset_mem (*h, _k);
303 clib_mem_free (_k);
John Lo5957a142018-01-18 12:44:50 -0500304 }
John Loe6bfeab2018-01-04 16:39:42 -0500305}
306
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307/* internal routine to free a hash table */
Dave Barachc3799992016-08-15 11:12:27 -0400308extern void *_hash_free (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309
310/* Public macro to free a hash table */
311#define hash_free(h) (h) = _hash_free ((h))
312
Dave Barachc3799992016-08-15 11:12:27 -0400313clib_error_t *hash_validate (void *v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700315/* Public inline function to get the number of value bytes for a hash table */
Dave Barachc3799992016-08-15 11:12:27 -0400316always_inline uword
317hash_value_bytes (hash_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318{
Dave Barachc3799992016-08-15 11:12:27 -0400319 hash_pair_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 return (sizeof (p->value[0]) << h->log2_pair_size) - sizeof (p->key);
321}
322
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700323/* Public inline function to get log2(size of a (key,value) pair) */
Dave Barachc3799992016-08-15 11:12:27 -0400324always_inline uword
325hash_pair_log2_bytes (hash_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326{
327 uword log2_bytes = h->log2_pair_size;
Dave Barachc3799992016-08-15 11:12:27 -0400328 ASSERT (BITS (hash_pair_t) == 32 || BITS (hash_pair_t) == 64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 if (BITS (hash_pair_t) == 32)
330 log2_bytes += 2;
331 else if (BITS (hash_pair_t) == 64)
332 log2_bytes += 3;
333 return log2_bytes;
334}
335
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700336/* Public inline function to get size of a (key,value) pair */
Dave Barachc3799992016-08-15 11:12:27 -0400337always_inline uword
338hash_pair_bytes (hash_t * h)
339{
340 return (uword) 1 << hash_pair_log2_bytes (h);
341}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700343/* Public inline function to advance a pointer past one (key,value) pair */
Dave Barachc3799992016-08-15 11:12:27 -0400344always_inline void *
345hash_forward1 (hash_t * h, void *v)
346{
347 return (u8 *) v + hash_pair_bytes (h);
348}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700350/* Public inline function to advance a pointer past N (key,value) pairs */
Dave Barachc3799992016-08-15 11:12:27 -0400351always_inline void *
352hash_forward (hash_t * h, void *v, uword n)
353{
354 return (u8 *) v + ((n * sizeof (hash_pair_t)) << h->log2_pair_size);
355}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Chris Luke5cdaf632016-07-30 15:05:07 -0400357/** Iterate over hash pairs.
358
359 @param p The current (key,value) pair. This should be of type
360 <code>(hash_pair_t *)</code>.
361 @param v The hash table to iterate.
362 @param body The operation to perform on each (key,value) pair.
363
364 Executes the expression or code block @c body with each active hash pair.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365*/
Chris Luke5cdaf632016-07-30 15:05:07 -0400366/* A previous version of this macro made use of the hash_pair_union_t
367 * structure; this version does not since that approach mightily upset
368 * the static analysis tool. In the rare chance someone is reading this
369 * code, pretend that _p below is of type hash_pair_union_t and that when
370 * used as an rvalue it's really using one of the union members as the
371 * rvalue. If you were confused before you might be marginally less
372 * confused after.
373 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374#define hash_foreach_pair(p,v,body) \
375do { \
376 __label__ _hash_foreach_done; \
377 hash_t * _h = hash_header (v); \
Chris Luke5cdaf632016-07-30 15:05:07 -0400378 void * _p; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 hash_pair_t * _q, * _q_end; \
380 uword _i, _i1, _id, _pair_increment; \
381 \
Chris Luke5cdaf632016-07-30 15:05:07 -0400382 _p = (v); \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 _i = 0; \
384 _pair_increment = 1; \
385 if ((v)) \
386 _pair_increment = 1 << _h->log2_pair_size; \
387 while (_i < hash_capacity (v)) \
388 { \
389 _id = _h->is_user[_i / BITS (_h->is_user[0])]; \
390 _i1 = _i + BITS (_h->is_user[0]); \
391 \
392 do { \
393 if (_id & 1) \
394 { \
Chris Luke5cdaf632016-07-30 15:05:07 -0400395 _q = _p; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 _q_end = _q + _pair_increment; \
397 } \
398 else \
399 { \
Chris Luke5cdaf632016-07-30 15:05:07 -0400400 hash_pair_indirect_t * _pi = _p; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401 _q = _pi->pairs; \
402 if (_h->log2_pair_size > 0) \
403 _q_end = hash_forward (_h, _q, indirect_pair_get_len (_pi)); \
404 else \
405 _q_end = vec_end (_q); \
406 } \
407 \
408 /* Loop through all elements in bucket. \
409 Bucket may have 0 1 or more (indirect case) pairs. */ \
410 while (_q < _q_end) \
411 { \
412 uword _break_in_body = 1; \
413 (p) = _q; \
414 do { \
415 body; \
416 _break_in_body = 0; \
417 } while (0); \
418 if (_break_in_body) \
419 goto _hash_foreach_done; \
420 _q += _pair_increment; \
421 } \
422 \
Chris Luke5cdaf632016-07-30 15:05:07 -0400423 _p = (hash_pair_t *)_p + _pair_increment; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 _id = _id / 2; \
425 _i++; \
426 } while (_i < _i1); \
427 } \
428 _hash_foreach_done: \
429 /* Be silent Mr. Compiler-Warning. */ \
430 ; \
431 } while (0)
432
433/* Iterate over key/value pairs
434
435 @param key_var the current key
436 @param value_var the current value
437 @param h the hash table to iterate across
Dave Barachc3799992016-08-15 11:12:27 -0400438 @param body the operation to perform on each (key_var,value_var) pair.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
440 calls body with each active hash pair
441*/
Paul Vinciguerraec11b132018-09-24 05:25:00 -0700442/* Iterate over key/value pairs. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443#define hash_foreach(key_var,value_var,h,body) \
444do { \
445 hash_pair_t * _r; \
446 hash_foreach_pair (_r, (h), { \
447 (key_var) = (__typeof__ (key_var)) _r->key; \
448 (value_var) = (__typeof__ (value_var)) _r->value[0]; \
449 do { body; } while (0); \
450 }); \
451} while (0)
452
453/* Iterate over key/value pairs for pointer key hash tables
454
455 @param key_var the current key
456 @param value_var the current value
457 @param h the hash table to iterate across
Dave Barachc3799992016-08-15 11:12:27 -0400458 @param body the operation to perform on each (key_var,value_var) pair.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
460 calls body with each active hash pair
461*/
462#define hash_foreach_mem(key_var,value_var,h,body) \
463do { \
464 hash_pair_t * _r; \
465 hash_foreach_pair (_r, (h), { \
466 (key_var) = (__typeof__ (key_var)) uword_to_pointer (_r->key, void *); \
467 (value_var) = (__typeof__ (value_var)) _r->value[0]; \
468 do { body; } while (0); \
469 }); \
470} while (0)
471
472/* Support for iteration through hash table. */
473
474/* This struct saves iteration state for hash_next.
475 None of these fields are meant to be visible to the user.
476 Hence, the cryptic short-hand names. */
Dave Barachc3799992016-08-15 11:12:27 -0400477typedef struct
478{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 uword i, j, f;
480} hash_next_t;
481
Dave Barachc3799992016-08-15 11:12:27 -0400482hash_pair_t *hash_next (void *v, hash_next_t * hn);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
Dave Barachc3799992016-08-15 11:12:27 -0400484void *_hash_create (uword elts, hash_t * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
Dave Barachc3799992016-08-15 11:12:27 -0400486always_inline void
487hash_set_value_bytes (hash_t * h, uword value_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488{
Dave Barachc3799992016-08-15 11:12:27 -0400489 hash_pair_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490 h->log2_pair_size =
Dave Barachc3799992016-08-15 11:12:27 -0400491 max_log2 ((sizeof (p->key) + value_bytes + sizeof (p->key) -
492 1) / sizeof (p->key));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493}
494
495#define hash_create2(_elts,_user,_value_bytes, \
496 _key_sum,_key_equal, \
497 _format_pair,_format_pair_arg) \
498({ \
499 hash_t _h; \
Dave Barachb7b92992018-10-17 10:38:51 -0400500 clib_memset (&_h, 0, sizeof (_h)); \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 _h.user = (_user); \
502 _h.key_sum = (hash_key_sum_function_t *) (_key_sum); \
503 _h.key_equal = (_key_equal); \
504 hash_set_value_bytes (&_h, (_value_bytes)); \
505 _h.format_pair = (format_function_t *) (_format_pair); \
506 _h.format_pair_arg = (_format_pair_arg); \
507 _hash_create ((_elts), &_h); \
508})
509
510/* Hash function based on that of Bob Jenkins (bob_jenkins@compuserve.com).
511 Public domain per: http://www.burtleburtle.net/bob/hash/doobs.html
512 Thanks, Bob. */
513
514#define hash_mix_step(a,b,c,s0,s1,s2) \
515do { \
516 (a) -= (b) + (c); (a) ^= (c) >> (s0); \
517 (b) -= (c) + (a); (b) ^= (a) << (s1); \
518 (c) -= (a) + (b); (c) ^= (b) >> (s2); \
519} while (0)
520
521#define hash_mix32_step_1(a,b,c) hash_mix_step(a,b,c,13,8,13)
522#define hash_mix32_step_2(a,b,c) hash_mix_step(a,b,c,12,16,5)
523#define hash_mix32_step_3(a,b,c) hash_mix_step(a,b,c,3,10,15)
524
525#define hash_mix64_step_1(a,b,c) hash_mix_step(a,b,c,43,9,8)
526#define hash_mix64_step_2(a,b,c) hash_mix_step(a,b,c,38,23,5)
527#define hash_mix64_step_3(a,b,c) hash_mix_step(a,b,c,35,49,11)
528#define hash_mix64_step_4(a,b,c) hash_mix_step(a,b,c,12,18,22)
529
Damjan Marion87997682022-03-26 00:57:50 +0100530#if uword_bits == 64
531#define hash_mix(a, b, c) hash_mix64 (a, b, c)
532#else
533#define hash_mix(a, b, c) hash_mix32 (a, b, c)
534#endif
535
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536/* Hash function based on that of Bob Jenkins (bob_jenkins@compuserve.com).
537 Thanks, Bob. */
538#define hash_mix64(a0,b0,c0) \
539do { \
540 hash_mix64_step_1 (a0, b0, c0); \
541 hash_mix64_step_2 (a0, b0, c0); \
542 hash_mix64_step_3 (a0, b0, c0); \
543 hash_mix64_step_4 (a0, b0, c0); \
544} while (0) \
545
546#define hash_mix32(a0,b0,c0) \
547do { \
548 hash_mix32_step_1 (a0, b0, c0); \
549 hash_mix32_step_2 (a0, b0, c0); \
550 hash_mix32_step_3 (a0, b0, c0); \
551} while (0) \
552
553/* Finalize from Bob Jenkins lookup3.c */
554
555always_inline uword
556hash32_rotate_left (u32 x, u32 i)
Dave Barachc3799992016-08-15 11:12:27 -0400557{
558 return (x << i) | (x >> (BITS (i) - i));
559}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560
561#define hash_v3_mix32(a,b,c) \
562do { \
563 (a) -= (c); (a) ^= hash32_rotate_left ((c), 4); (c) += (b); \
564 (b) -= (a); (b) ^= hash32_rotate_left ((a), 6); (a) += (c); \
565 (c) -= (b); (c) ^= hash32_rotate_left ((b), 8); (b) += (a); \
566 (a) -= (c); (a) ^= hash32_rotate_left ((c),16); (c) += (b); \
567 (b) -= (a); (b) ^= hash32_rotate_left ((a),19); (a) += (c); \
568 (c) -= (b); (c) ^= hash32_rotate_left ((b), 4); (b) += (a); \
569} while (0)
570
571#define hash_v3_finalize32(a,b,c) \
572do { \
573 (c) ^= (b); (c) -= hash32_rotate_left ((b), 14); \
574 (a) ^= (c); (a) -= hash32_rotate_left ((c), 11); \
575 (b) ^= (a); (b) -= hash32_rotate_left ((a), 25); \
576 (c) ^= (b); (c) -= hash32_rotate_left ((b), 16); \
577 (a) ^= (c); (a) -= hash32_rotate_left ((c), 4); \
578 (b) ^= (a); (b) -= hash32_rotate_left ((a), 14); \
579 (c) ^= (b); (c) -= hash32_rotate_left ((b), 24); \
580} while (0)
581
582/* 32 bit mixing/finalize in steps. */
583
584#define hash_v3_mix32_step1(a,b,c) \
585do { \
586 (a) -= (c); (a) ^= hash32_rotate_left ((c), 4); (c) += (b); \
587 (b) -= (a); (b) ^= hash32_rotate_left ((a), 6); (a) += (c); \
588} while (0)
589
590#define hash_v3_mix32_step2(a,b,c) \
591do { \
592 (c) -= (b); (c) ^= hash32_rotate_left ((b), 8); (b) += (a); \
593 (a) -= (c); (a) ^= hash32_rotate_left ((c),16); (c) += (b); \
594} while (0)
595
596#define hash_v3_mix32_step3(a,b,c) \
597do { \
598 (b) -= (a); (b) ^= hash32_rotate_left ((a),19); (a) += (c); \
599 (c) -= (b); (c) ^= hash32_rotate_left ((b), 4); (b) += (a); \
600} while (0)
601
602#define hash_v3_finalize32_step1(a,b,c) \
603do { \
604 (c) ^= (b); (c) -= hash32_rotate_left ((b), 14); \
605 (a) ^= (c); (a) -= hash32_rotate_left ((c), 11); \
606} while (0)
607
608#define hash_v3_finalize32_step2(a,b,c) \
609do { \
610 (b) ^= (a); (b) -= hash32_rotate_left ((a), 25); \
611 (c) ^= (b); (c) -= hash32_rotate_left ((b), 16); \
612} while (0)
613
614#define hash_v3_finalize32_step3(a,b,c) \
615do { \
616 (a) ^= (c); (a) -= hash32_rotate_left ((c), 4); \
617 (b) ^= (a); (b) -= hash32_rotate_left ((a), 14); \
618 (c) ^= (b); (c) -= hash32_rotate_left ((b), 24); \
619} while (0)
620
621/* Vector v3 mixing/finalize. */
622#define hash_v3_mix_step_1_u32x(a,b,c) \
623do { \
624 (a) -= (c); (a) ^= u32x_irotate_left ((c), 4); (c) += (b); \
625 (b) -= (a); (b) ^= u32x_irotate_left ((a), 6); (a) += (c); \
626 (c) -= (b); (c) ^= u32x_irotate_left ((b), 8); (b) += (a); \
627} while (0)
628
629#define hash_v3_mix_step_2_u32x(a,b,c) \
630do { \
631 (a) -= (c); (a) ^= u32x_irotate_left ((c),16); (c) += (b); \
632 (b) -= (a); (b) ^= u32x_irotate_left ((a),19); (a) += (c); \
633 (c) -= (b); (c) ^= u32x_irotate_left ((b), 4); (b) += (a); \
634} while (0)
635
636#define hash_v3_finalize_step_1_u32x(a,b,c) \
637do { \
638 (c) ^= (b); (c) -= u32x_irotate_left ((b), 14); \
639 (a) ^= (c); (a) -= u32x_irotate_left ((c), 11); \
640 (b) ^= (a); (b) -= u32x_irotate_left ((a), 25); \
641} while (0)
642
643#define hash_v3_finalize_step_2_u32x(a,b,c) \
644do { \
645 (c) ^= (b); (c) -= u32x_irotate_left ((b), 16); \
646 (a) ^= (c); (a) -= u32x_irotate_left ((c), 4); \
647 (b) ^= (a); (b) -= u32x_irotate_left ((a), 14); \
648 (c) ^= (b); (c) -= u32x_irotate_left ((b), 24); \
649} while (0)
650
651#define hash_v3_mix_u32x(a,b,c) \
652do { \
653 hash_v3_mix_step_1_u32x(a,b,c); \
654 hash_v3_mix_step_2_u32x(a,b,c); \
655} while (0)
656
657#define hash_v3_finalize_u32x(a,b,c) \
658do { \
659 hash_v3_finalize_step_1_u32x(a,b,c); \
660 hash_v3_finalize_step_2_u32x(a,b,c); \
661} while (0)
662
Dave Barachc3799992016-08-15 11:12:27 -0400663extern uword hash_memory (void *p, word n_bytes, uword state);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664
665extern uword mem_key_sum (hash_t * h, uword key);
666extern uword mem_key_equal (hash_t * h, uword key1, uword key2);
667
668#define hash_create_mem(elts,key_bytes,value_bytes) \
669 hash_create2((elts),(key_bytes),(value_bytes),mem_key_sum,mem_key_equal,0,0)
670
671extern uword vec_key_sum (hash_t * h, uword key);
672extern uword vec_key_equal (hash_t * h, uword key1, uword key2);
Dave Barachc3799992016-08-15 11:12:27 -0400673extern u8 *vec_key_format_pair (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674
675#define hash_create_vec(elts,key_bytes,value_bytes) \
676 hash_create2((elts),(key_bytes),(value_bytes),\
677 vec_key_sum,vec_key_equal,vec_key_format_pair,0)
678
679extern uword string_key_sum (hash_t * h, uword key);
680extern uword string_key_equal (hash_t * h, uword key1, uword key2);
Dave Barachc3799992016-08-15 11:12:27 -0400681extern u8 *string_key_format_pair (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Ole Troan73710c72018-06-04 22:27:49 +0200683/*
684 * Note: if you plan to put a hash into shared memory,
685 * the key sum and key equal functions MUST be set to magic constants!
686 * PIC means that whichever process sets up the hash won't have
687 * the actual key sum functions at the same place, leading to
688 * very hard-to-find bugs...
689 */
690
691#define hash_create_shmem(elts,key_bytes,value_bytes) \
692 hash_create2((elts),(key_bytes),(value_bytes), \
693 (hash_key_sum_function_t *) KEY_FUNC_MEM, \
694 (hash_key_equal_function_t *)KEY_FUNC_MEM, \
695 0, 0)
696
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697#define hash_create_string(elts,value_bytes) \
698 hash_create2((elts),0,(value_bytes), \
699 (hash_key_sum_function_t *) KEY_FUNC_STRING, \
700 (hash_key_equal_function_t *)KEY_FUNC_STRING, \
701 0, 0)
702
703#define hash_create(elts,value_bytes) \
704 hash_create2((elts),0,(value_bytes), \
705 (hash_key_sum_function_t *) KEY_FUNC_NONE, \
706 (hash_key_equal_function_t *) KEY_FUNC_NONE, \
707 0,0)
708
709#define hash_create_uword(elts,value_bytes) \
710 hash_create2((elts),0,(value_bytes), \
711 (hash_key_sum_function_t *) KEY_FUNC_POINTER_UWORD, \
712 (hash_key_equal_function_t *) KEY_FUNC_POINTER_UWORD, \
713 0,0)
714
715#define hash_create_u32(elts,value_bytes) \
716 hash_create2((elts),0,(value_bytes), \
717 (hash_key_sum_function_t *) KEY_FUNC_POINTER_U32, \
718 (hash_key_equal_function_t *) KEY_FUNC_POINTER_U32, \
719 0,0)
720
Dave Barachc3799992016-08-15 11:12:27 -0400721u8 *format_hash (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
723/* Looks up input in hash table indexed by either vec string or
724 c string (null terminated). */
725unformat_function_t unformat_hash_vec_string;
726unformat_function_t unformat_hash_string;
727
Dave Barachc3799992016-08-15 11:12:27 -0400728/* Main test routine. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729int test_hash_main (unformat_input_t * input);
730
731#endif /* included_hash_h */
Dave Barachc3799992016-08-15 11:12:27 -0400732
733/*
734 * fd.io coding-style-patch-verification: ON
735 *
736 * Local Variables:
737 * eval: (c-set-style "gnu")
738 * End:
739 */