FD.io VPP  v16.06
Vector Packet Processing
phash.h
Go to the documentation of this file.
1 /*
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) 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_phash_h
39 #define included_phash_h
40 
41 #include <vppinfra/hash.h> /* for Bob's mixing functions */
42 
43 typedef struct {
44  /* Maybe either pointer to vector or inline word. */
46 
47  /* Hash code (A, B). */
48  u32 a, b;
49 } phash_key_t;
50 
51 /* Table indexed by B. */
52 typedef struct {
53  /* Vector of key indices with this same value of B. */
54  u32 * keys;
55 
56  /* hash=a^tabb[b].val_b */
58 
59  /* High watermark of who has visited this map node. */
61 } phash_tabb_t;
62 
63 always_inline void
65 {
66  vec_free (b->keys);
67  b->val_b = b->water_b = 0;
68 }
69 
70 typedef struct
71 {
72  /* b that currently occupies this hash */
74 
75  /* Queue position of parent that could use this hash. */
77 
78  /* What to change parent tab[b] to use this hash. */
80 
81  /* Original value of tab[b]. */
83 } phash_tabq_t;
84 
85 typedef struct {
86  u8 a_bits, b_bits, s_bits, a_shift;
88  u32 * tab;
90 
91  /* Seed value for hash mixer. */
93 
95 
96  /* Key functions want 64 bit keys.
97  Use hash_mix64 rather than hash_mix32. */
98 #define PHASH_FLAG_MIX64 (1 << 0)
99 #define PHASH_FLAG_MIX32 (0 << 0)
100 
101  /* When b_bits is large enough (>= 12) we scramble. */
102 #define PHASH_FLAG_USE_SCRAMBLE (1 << 1)
103 
104  /* Slow mode gives smaller tables but at the expense of more run time. */
105 #define PHASH_FLAG_SLOW_MODE (0 << 2)
106 #define PHASH_FLAG_FAST_MODE (1 << 2)
107 
108  /* Generate minimal perfect hash instead of perfect hash. */
109 #define PHASH_FLAG_NON_MINIMAL (0 << 3)
110 #define PHASH_FLAG_MINIMAL (1 << 3)
111 
112  /* vec_len (keys) for minimal hash;
113  1 << s_bits for non-minimal hash. */
115 
116  /* Vector of keys. */
118 
119  /* Used by callbacks to identify keys. */
120  void * private;
121 
122  /* Key comparison callback. */
123  int (* key_is_equal) (void * private, uword key1, uword key2);
124 
125  /* Callback to reduce single key -> hash seeds. */
126  void (* key_seed1) (void * private, uword key, void * seed);
127 
128  /* Callback to reduce two key2 -> hash seeds. */
129  void (* key_seed2) (void * private, uword key1, uword key2, void * seed);
130 
131  /* Stuff used to compute perfect hash. */
133 
134  /* Stuff indexed by B. */
136 
137  /* Table of B ordered by number of keys in tabb[b]. */
139 
140  /* Unique key (or ~0 if none) for a given hash
141  H = A ^ scramble[tab[B].val_b]. */
143 
144  /* Stuff indexed by q. */
146 
147  /* Stats. */
148  u32 n_seed_trials, n_perfect_calls;
149 } phash_main_t;
150 
151 always_inline void
153 {
154  vec_free (pm->tabb);
155  vec_free (pm->tabq);
156  vec_free (pm->tabh);
157  vec_free (pm->tabb_sort);
158  if (! (pm->flags & PHASH_FLAG_USE_SCRAMBLE))
159  vec_free (pm->scramble);
160 }
161 
162 always_inline void
164 {
166  vec_free (pm->tab);
167  vec_free (pm->keys);
168  memset (pm, 0, sizeof (pm[0]));
169 }
170 
171 /* Slow hash computation for general keys. */
173 
174 /* Main routine to compute perfect hash. */
176 
177 /* Validates that hash is indeed perfect. */
179 
180 /* Unit test. */
181 int phash_test_main (unformat_input_t * input);
182 
183 #endif /* included_phash_h */
u32 water_b
Definition: phash.h:60
a
Definition: bitmap.h:393
static void(BVT(clib_bihash)*h, BVT(clib_bihash_value)*v)
clib_error_t * phash_find_perfect_hash(phash_main_t *pm)
Definition: phash.c:776
u32 random_seed
Definition: phash.h:132
uword key
Definition: phash.h:45
u32 * tab
Definition: phash.h:88
u32 b
Definition: phash.h:48
u32 parent_q
Definition: phash.h:76
u32 * tabh
Definition: phash.h:142
u32 oldval_q
Definition: phash.h:82
#define always_inline
Definition: clib.h:84
unsigned long u64
Definition: types.h:89
u32 n_seed_trials
Definition: phash.h:148
u8 s_bits
Definition: phash.h:86
always_inline void phash_main_free_working_memory(phash_main_t *pm)
Definition: phash.h:152
phash_key_t * keys
Definition: phash.h:117
u32 hash_max
Definition: phash.h:114
u32 flags
Definition: phash.h:94
u32 val_b
Definition: phash.h:57
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
u64 hash_seed
Definition: phash.h:92
u32 * keys
Definition: phash.h:54
uword phash_hash_slow(phash_main_t *pm, uword key)
Definition: phash.c:877
int phash_test_main(unformat_input_t *input)
clib_error_t * phash_validate(phash_main_t *pm)
Definition: phash.c:930
unsigned int u32
Definition: types.h:88
phash_tabq_t * tabq
Definition: phash.h:145
u32 b_mask
Definition: phash.h:87
u64 uword
Definition: types.h:112
u32 b_q
Definition: phash.h:73
unsigned char u8
Definition: types.h:56
always_inline void phash_main_free(phash_main_t *pm)
Definition: phash.h:163
u32 * tabb_sort
Definition: phash.h:138
u32 newval_q
Definition: phash.h:79
struct _unformat_input_t unformat_input_t
always_inline void phash_tabb_free(phash_tabb_t *b)
Definition: phash.h:64
#define PHASH_FLAG_USE_SCRAMBLE
Definition: phash.h:102
phash_tabb_t * tabb
Definition: phash.h:135
u32 * scramble
Definition: phash.h:89