FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
bihash_template.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014 Cisco and/or its affiliates.
3 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16 
17 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
18 
19 /*
20  * Note: to instantiate the template multiple times in a single file,
21  * #undef __included_bihash_template_h__...
22  */
23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
25 
26 #include <vppinfra/heap.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/pool.h>
29 #include <vppinfra/cache.h>
30 #include <vppinfra/lock.h>
31 
32 #ifndef BIHASH_TYPE
33 #error BIHASH_TYPE not defined
34 #endif
35 
36 #ifdef BIHASH_32_64_SVM
37 #undef HAVE_MEMFD_CREATE
38 #include <vppinfra/linux/syscall.h>
39 #include <fcntl.h>
40 #define F_LINUX_SPECIFIC_BASE 1024
41 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
42 #define F_SEAL_SHRINK (2)
43 /* Max page size 2**16 due to refcount width */
44 #define BIHASH_FREELIST_LENGTH 17
45 #endif
46 
47 #define _bv(a,b) a##b
48 #define __bv(a,b) _bv(a,b)
49 #define BV(a) __bv(a,BIHASH_TYPE)
50 
51 #define _bvt(a,b) a##b##_t
52 #define __bvt(a,b) _bvt(a,b)
53 #define BVT(a) __bvt(a,BIHASH_TYPE)
54 
55 #if _LP64 == 0
56 #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
57 #define u64_to_pointer(x) (void *)(u32)((x))
58 #define pointer_to_u64(x) (u64)(u32)((x))
59 #else
60 #define OVERFLOW_ASSERT(x)
61 #define u64_to_pointer(x) (void *)((x))
62 #define pointer_to_u64(x) (u64)((x))
63 #endif
64 
65 typedef struct BV (clib_bihash_value)
66 {
67  union
68  {
69  BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
70  u64 next_free_as_u64;
71  };
72 } BVT (clib_bihash_value);
73 
74 #define BIHASH_BUCKET_OFFSET_BITS 36
75 
76 typedef struct
77 {
78  union
79  {
80  struct
81  {
82  u64 offset:BIHASH_BUCKET_OFFSET_BITS;
83  u64 lock:1;
84  u64 linear_search:1;
85  u64 log2_pages:8;
86  u64 refcnt:16;
87  };
88  u64 as_u64;
89  };
90 } BVT (clib_bihash_bucket);
91 
92 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
93 
94 /* *INDENT-OFF* */
95 typedef CLIB_PACKED (struct {
96  /*
97  * Backing store allocation. Since bihash manages its own
98  * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
99  */
100  u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
101  u64 alloc_arena_size; /* Size of the arena */
102  /* Two SVM pointers stored as 8-byte integers */
103  u64 alloc_lock_as_u64;
104  u64 buckets_as_u64;
105  /* freelist list-head arrays/vectors */
106  u64 freelists_as_u64;
107  u32 nbuckets; /* Number of buckets */
108  /* Set when header valid */
109  volatile u32 ready;
110  u64 pad[2];
111 }) BVT (clib_bihash_shared_header);
112 /* *INDENT-ON* */
113 
114 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
115 
116 typedef struct
117 {
118  BVT (clib_bihash_bucket) * buckets;
119  volatile u32 *alloc_lock;
120 
121  BVT (clib_bihash_value) ** working_copies;
122  int *working_copy_lengths;
123  BVT (clib_bihash_bucket) saved_bucket;
124 
125  u32 nbuckets;
126  u32 log2_nbuckets;
127  u8 *name;
128 
129  u64 *freelists;
130 
131 #if BIHASH_32_64_SVM
132  BVT (clib_bihash_shared_header) * sh;
133  int memfd;
134 #else
135  BVT (clib_bihash_shared_header) sh;
136 #endif
137 
138  u64 alloc_arena; /* Base of the allocation arena */
139 
140  /**
141  * A custom format function to print the Key and Value of bihash_key instead of default hexdump
142  */
143  format_function_t *fmt_fn;
144 
145 } BVT (clib_bihash);
146 
147 #if BIHASH_32_64_SVM
148 #undef alloc_arena_next
149 #undef alloc_arena_size
150 #undef alloc_arena
151 #undef CLIB_BIHASH_READY_MAGIC
152 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
153 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
154 #define alloc_arena(h) ((h)->alloc_arena)
155 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
156 #else
157 #undef alloc_arena_next
158 #undef alloc_arena_size
159 #undef alloc_arena
160 #undef CLIB_BIHASH_READY_MAGIC
161 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
162 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
163 #define alloc_arena(h) ((h)->alloc_arena)
164 #define CLIB_BIHASH_READY_MAGIC 0
165 #endif
166 
167 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
168 {
169  while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
170  CLIB_PAUSE ();
171 }
172 
173 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
174 {
175  __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
176 }
177 
178 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
179 {
180  BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
181 
182  do
183  {
184  locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
185  unlocked_bucket.lock = 0;
186  locked_bucket.lock = 1;
187  CLIB_PAUSE ();
188  }
189  while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
190  locked_bucket.as_u64, 1 /* weak */ ,
191  __ATOMIC_ACQUIRE,
192  __ATOMIC_ACQUIRE) == 0);
193 }
194 
195 static inline void BV (clib_bihash_unlock_bucket)
196  (BVT (clib_bihash_bucket) * b)
197 {
199  b->lock = 0;
200 }
201 
202 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
203  uword offset)
204 {
205  u8 *hp = (u8 *) (uword) alloc_arena (h);
206  u8 *vp = hp + offset;
207 
208  return (void *) vp;
209 }
210 
211 static inline int BV (clib_bihash_bucket_is_empty)
212  (BVT (clib_bihash_bucket) * b)
213 {
214  /* Note: applied to locked buckets, test offset */
215  return b->offset == 0;
216 }
217 
218 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
219  void *v)
220 {
221  u8 *hp, *vp;
222 
223  hp = (u8 *) (uword) alloc_arena (h);
224  vp = (u8 *) v;
225 
226  return vp - hp;
227 }
228 
229 void BV (clib_bihash_init)
230  (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
231 
232 #if BIHASH_32_64_SVM
233 void BV (clib_bihash_master_init_svm)
234  (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
235 void BV (clib_bihash_slave_init_svm)
236  (BVT (clib_bihash) * h, char *name, int fd);
237 #endif
238 
239 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
240  format_function_t * fmt_fn);
241 
242 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
243 
244 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
245  BVT (clib_bihash_kv) * add_v, int is_add);
246 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
247  BVT (clib_bihash_kv) * add_v,
248  int (*is_stale_cb) (BVT
249  (clib_bihash_kv)
250  *, void *),
251  void *arg);
252 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
253  BVT (clib_bihash_kv) * search_v,
254  BVT (clib_bihash_kv) * return_v);
255 
256 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
257  void *callback, void *arg);
258 
259 format_function_t BV (format_bihash);
260 format_function_t BV (format_bihash_kvp);
261 format_function_t BV (format_bihash_lru);
262 
263 static inline int BV (clib_bihash_search_inline_with_hash)
264  (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
265 {
266  u32 bucket_index;
267  BVT (clib_bihash_value) * v;
268  BVT (clib_bihash_bucket) * b;
269  int i, limit;
270 
271  bucket_index = hash & (h->nbuckets - 1);
272  b = &h->buckets[bucket_index];
273 
274  if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
275  return -1;
276 
277  if (PREDICT_FALSE (b->lock))
278  {
279  volatile BVT (clib_bihash_bucket) * bv = b;
280  while (bv->lock)
281  CLIB_PAUSE ();
282  }
283 
284  hash >>= h->log2_nbuckets;
285 
286  v = BV (clib_bihash_get_value) (h, b->offset);
287 
288  /* If the bucket has unresolvable collisions, use linear search */
289  limit = BIHASH_KVP_PER_PAGE;
290  v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
291  if (PREDICT_FALSE (b->linear_search))
292  limit <<= b->log2_pages;
293 
294  for (i = 0; i < limit; i++)
295  {
296  if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
297  {
298  *key_result = v->kvp[i];
299  return 0;
300  }
301  }
302  return -1;
303 }
304 
305 static inline int BV (clib_bihash_search_inline)
306  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
307 {
308  u64 hash;
309 
310  hash = BV (clib_bihash_hash) (key_result);
311 
312  return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
313 }
314 
315 static inline void BV (clib_bihash_prefetch_bucket)
316  (BVT (clib_bihash) * h, u64 hash)
317 {
318  u32 bucket_index;
319  BVT (clib_bihash_bucket) * b;
320 
321  bucket_index = hash & (h->nbuckets - 1);
322  b = &h->buckets[bucket_index];
323 
325 }
326 
327 static inline void BV (clib_bihash_prefetch_data)
328  (BVT (clib_bihash) * h, u64 hash)
329 {
330  u32 bucket_index;
331  BVT (clib_bihash_value) * v;
332  BVT (clib_bihash_bucket) * b;
333 
334  bucket_index = hash & (h->nbuckets - 1);
335  b = &h->buckets[bucket_index];
336 
337  if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
338  return;
339 
340  hash >>= h->log2_nbuckets;
341  v = BV (clib_bihash_get_value) (h, b->offset);
342 
343  v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
344 
346 }
347 
348 static inline int BV (clib_bihash_search_inline_2_with_hash)
349  (BVT (clib_bihash) * h,
350  u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
351 {
352  u32 bucket_index;
353  BVT (clib_bihash_value) * v;
354  BVT (clib_bihash_bucket) * b;
355  int i, limit;
356 
357  ASSERT (valuep);
358 
359  bucket_index = hash & (h->nbuckets - 1);
360  b = &h->buckets[bucket_index];
361 
362  if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
363  return -1;
364 
365  if (PREDICT_FALSE (b->lock))
366  {
367  volatile BVT (clib_bihash_bucket) * bv = b;
368  while (bv->lock)
369  CLIB_PAUSE ();
370  }
371 
372  hash >>= h->log2_nbuckets;
373  v = BV (clib_bihash_get_value) (h, b->offset);
374 
375  /* If the bucket has unresolvable collisions, use linear search */
376  limit = BIHASH_KVP_PER_PAGE;
377  v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
378  if (PREDICT_FALSE (b->linear_search))
379  limit <<= b->log2_pages;
380 
381  for (i = 0; i < limit; i++)
382  {
383  if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
384  {
385  *valuep = v->kvp[i];
386  return 0;
387  }
388  }
389  return -1;
390 }
391 
392 static inline int BV (clib_bihash_search_inline_2)
393  (BVT (clib_bihash) * h,
394  BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
395 {
396  u64 hash;
397 
398  hash = BV (clib_bihash_hash) (search_key);
399 
400  return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
401  valuep);
402 }
403 
404 
405 #endif /* __included_bihash_template_h__ */
406 
407 /** @endcond */
408 
409 /*
410  * fd.io coding-style-patch-verification: ON
411  *
412  * Local Variables:
413  * eval: (c-set-style "gnu")
414  * End:
415  */
#define CLIB_PAUSE()
Definition: lock.h:22
int clib_bihash_search_inline_with_hash(clib_bihash *h, u64 hash, clib_bihash_kv *in_out_kv)
Search a bi-hash table, use supplied hash code.
u8 pad[3]
log2 (size of the packing page block)
Definition: bihash_doc.h:61
#define BIHASH_KVP_PER_PAGE
Definition: bihash_16_8.h:19
u64 as_u64
Definition: bihash_doc.h:63
void clib_bihash_free(clib_bihash *h)
Destroy a bounded index extensible hash table.
unsigned long u64
Definition: types.h:89
Fixed length block allocator.
for(i=1;i<=collision_buckets;i++)
int i
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
unsigned char u8
Definition: types.h:56
int clib_bihash_add_del(clib_bihash *h, clib_bihash_kv *add_v, int is_add)
Add or delete a (key,value) pair from a bi-hash table.
static BVT(clib_bihash)
Definition: adj_nbr.c:28
unsigned int u32
Definition: types.h:88
static uword clib_bihash_get_offset(clib_bihash *h, void *v)
Get clib mheap offset given a pointer.
u64 memory_size
Definition: vhost_user.h:115
#define PREDICT_FALSE(x)
Definition: clib.h:111
int clib_bihash_search_inline(clib_bihash *h, clib_bihash_kv *in_out_kv)
Search a bi-hash table.
void clib_bihash_init(clib_bihash *h, char *name, u32 nbuckets, uword memory_size)
initialize a bounded index extensible hash table
u8 name[64]
Definition: memclnt.api:152
void clib_bihash_prefetch_bucket(clib_bihash *h, u64 hash)
Prefetch a bi-hash bucket given a hash code.
void clib_bihash_foreach_key_value_pair(clib_bihash *h, void *callback, void *arg)
Visit active (key,value) pairs in a bi-hash table.
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
void clib_bihash_prefetch_data(clib_bihash *h, u64 hash)
Prefetch bi-hash (key,value) data given a hash code.
#define ASSERT(truth)
int clib_bihash_search_inline_2(clib_bihash *h, clib_bihash_kv *search_key, clib_bihash_kv *valuep)
Search a bi-hash table.
u8 is_add
Definition: ipsec_gre.api:36
u8 log2_pages
Definition: bihash_doc.h:62
u64 uword
Definition: types.h:112
struct clib_bihash_value offset
template key/value backing page structure
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:115
static void * clib_bihash_get_value(clib_bihash *h, uword offset)
Get pointer to value page given its clib mheap offset.
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define STATIC_ASSERT_SIZEOF(d, s)
#define CLIB_PACKED(x)
Definition: clib.h:81