FD.io VPP  v20.05.1-6-gf53edbc3b
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 /* default is 2MB, use 30 for 1GB */
48 #ifndef BIHASH_LOG2_HUGEPAGE_SIZE
49 #define BIHASH_LOG2_HUGEPAGE_SIZE 21
50 #endif
51 
52 #define _bv(a,b) a##b
53 #define __bv(a,b) _bv(a,b)
54 #define BV(a) __bv(a,BIHASH_TYPE)
55 
56 #define _bvt(a,b) a##b##_t
57 #define __bvt(a,b) _bvt(a,b)
58 #define BVT(a) __bvt(a,BIHASH_TYPE)
59 
60 #define _bvs(a,b) struct a##b
61 #define __bvs(a,b) _bvs(a,b)
62 #define BVS(a) __bvs(a,BIHASH_TYPE)
63 
64 #if _LP64 == 0
65 #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
66 #define u64_to_pointer(x) (void *)(u32)((x))
67 #define pointer_to_u64(x) (u64)(u32)((x))
68 #else
69 #define OVERFLOW_ASSERT(x)
70 #define u64_to_pointer(x) (void *)((x))
71 #define pointer_to_u64(x) (u64)((x))
72 #endif
73 
74 typedef struct BV (clib_bihash_value)
75 {
76  union
77  {
78  BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
79  u64 next_free_as_u64;
80  };
82 
83 #define BIHASH_BUCKET_OFFSET_BITS 36
84 
85 typedef struct
86 {
87  union
88  {
89  struct
90  {
91  u64 offset:BIHASH_BUCKET_OFFSET_BITS;
92  u64 lock:1;
93  u64 linear_search:1;
94  u64 log2_pages:8;
95  u64 refcnt:16;
96  };
97  u64 as_u64;
98  };
99 } BVT (clib_bihash_bucket);
100 
101 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
102 
103 /* *INDENT-OFF* */
104 typedef CLIB_PACKED (struct {
105  /*
106  * Backing store allocation. Since bihash manages its own
107  * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
108  */
109  u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
110  u64 alloc_arena_size; /* Size of the arena */
111  u64 alloc_arena_mapped; /* Size of the mapped memory in the arena */
112  /* Two SVM pointers stored as 8-byte integers */
113  u64 alloc_lock_as_u64;
114  u64 buckets_as_u64;
115  /* freelist list-head arrays/vectors */
116  u64 freelists_as_u64;
117  u32 nbuckets; /* Number of buckets */
118  /* Set when header valid */
119  volatile u32 ready;
120  u64 pad[1];
121 }) BVT (clib_bihash_shared_header);
122 /* *INDENT-ON* */
123 
124 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
125 
126 typedef
127 BVS (clib_bihash)
128 {
129  BVT (clib_bihash_bucket) * buckets;
130  volatile u32 *alloc_lock;
131 
132  BVT (clib_bihash_value) ** working_copies;
133  int *working_copy_lengths;
134  BVT (clib_bihash_bucket) saved_bucket;
135 
136  u32 nbuckets;
137  u32 log2_nbuckets;
139  u8 *name;
140 
141  u64 *freelists;
142 
143 #if BIHASH_32_64_SVM
144  BVT (clib_bihash_shared_header) * sh;
145  int memfd;
146 #else
147  BVT (clib_bihash_shared_header) sh;
148 #endif
149 
150  u64 alloc_arena; /* Base of the allocation arena */
151  volatile u8 instantiated;
152 
153  /**
154  * A custom format function to print the Key and Value of bihash_key instead of default hexdump
155  */
156  format_function_t *fmt_fn;
157 
158  /** Optional statistics-gathering callback */
159 #if BIHASH_ENABLE_STATS
160  void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
161 
162  /** Statistics callback context (e.g. address of stats data structure) */
163  void *inc_stats_context;
164 #endif
165 
166 } BVT (clib_bihash);
167 
168 typedef struct
169 {
170  BVT (clib_bihash) * h;
171  char *name;
172  u32 nbuckets;
174  format_function_t *fmt_fn;
175  u8 instantiate_immediately;
176  u8 dont_add_to_all_bihash_list;
177 } BVT (clib_bihash_init2_args);
178 
179 extern void **clib_all_bihashes;
180 
181 #if BIHASH_32_64_SVM
182 #undef alloc_arena_next
183 #undef alloc_arena_size
184 #undef alloc_arena_mapped
185 #undef alloc_arena
186 #undef CLIB_BIHASH_READY_MAGIC
187 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
188 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
189 #define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
190 #define alloc_arena(h) ((h)->alloc_arena)
191 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
192 #else
193 #undef alloc_arena_next
194 #undef alloc_arena_size
195 #undef alloc_arena_mapped
196 #undef alloc_arena
197 #undef CLIB_BIHASH_READY_MAGIC
198 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
199 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
200 #define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
201 #define alloc_arena(h) ((h)->alloc_arena)
202 #define CLIB_BIHASH_READY_MAGIC 0
203 #endif
204 
205 #ifndef BIHASH_STAT_IDS
206 #define BIHASH_STAT_IDS 1
207 
208 #define foreach_bihash_stat \
209 _(alloc_add) \
210 _(add) \
211 _(split_add) \
212 _(replace) \
213 _(update) \
214 _(del) \
215 _(del_free) \
216 _(linear) \
217 _(resplit) \
218 _(working_copy_lost) \
219 _(splits) /* must be last */
220 
221 typedef enum
222 {
223 #define _(a) BIHASH_STAT_##a,
224  foreach_bihash_stat
225 #undef _
226  BIHASH_STAT_N_STATS,
227 } BVT (clib_bihash_stat_id);
228 #endif /* BIHASH_STAT_IDS */
229 
230 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
231  int stat_id, u64 count)
232 {
233 #if BIHASH_ENABLE_STATS
234  if (PREDICT_FALSE (h->inc_stats_callback != 0))
235  h->inc_stats_callback (h, stat_id, count);
236 #endif
237 }
238 
239 #if BIHASH_ENABLE_STATS
240 static inline void BV (clib_bihash_set_stats_callback)
241  (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
242  void *ctx)
243 {
244  h->inc_stats_callback = cb;
245  h->inc_stats_context = ctx;
246 }
247 #endif
248 
249 
250 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
251 {
252  while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
253  CLIB_PAUSE ();
254 }
255 
256 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
257 {
258  __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
259 }
260 
261 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
262 {
263  /* *INDENT-OFF* */
264  BVT (clib_bihash_bucket) mask = { .lock = 1 };
265  /* *INDENT-ON* */
266  u64 old;
267 
268 try_again:
269  old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
270 
271  if (PREDICT_FALSE (old & mask.as_u64))
272  {
273  /* somebody else flipped the bit, try again */
274  CLIB_PAUSE ();
275  goto try_again;
276  }
277 }
278 
279 static inline void BV (clib_bihash_unlock_bucket)
280  (BVT (clib_bihash_bucket) * b)
281 {
282  b->lock = 0;
283 }
284 
285 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
286  uword offset)
287 {
288  u8 *hp = (u8 *) (uword) alloc_arena (h);
289  u8 *vp = hp + offset;
290 
291  return (void *) vp;
292 }
293 
294 static inline int BV (clib_bihash_bucket_is_empty)
295  (BVT (clib_bihash_bucket) * b)
296 {
297  /* Note: applied to locked buckets, test offset */
299  return b->offset == 0;
300  else
301  return (b->log2_pages == 0 && b->refcnt == 1);
302 }
303 
304 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
305  void *v)
306 {
307  u8 *hp, *vp;
308 
309  hp = (u8 *) (uword) alloc_arena (h);
310  vp = (u8 *) v;
311 
312  return vp - hp;
313 }
314 
315 void BV (clib_bihash_init)
316  (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
317 
318 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
319 
320 #if BIHASH_32_64_SVM
321 void BV (clib_bihash_master_init_svm)
322  (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
323 void BV (clib_bihash_slave_init_svm)
324  (BVT (clib_bihash) * h, char *name, int fd);
325 #endif
326 
327 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
328  format_function_t * fmt_fn);
329 
330 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
331 
332 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
333  BVT (clib_bihash_kv) * add_v, int is_add);
334 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
335  BVT (clib_bihash_kv) * add_v,
336  int (*is_stale_cb) (BVT
337  (clib_bihash_kv)
338  *, void *),
339  void *arg);
340 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
341  BVT (clib_bihash_kv) * search_v,
342  BVT (clib_bihash_kv) * return_v);
343 
344 #define BIHASH_WALK_STOP 0
345 #define BIHASH_WALK_CONTINUE 1
346 
347 typedef
348  int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
349  void *);
350 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
351  BV
353  cb, void *arg);
354 void *clib_all_bihash_set_heap (void);
355 void clib_bihash_copied (void *dst, void *src);
356 
357 format_function_t BV (format_bihash);
358 format_function_t BV (format_bihash_kvp);
359 format_function_t BV (format_bihash_lru);
360 
361 static inline
362 BVT (clib_bihash_bucket) *
363 BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
364 {
365 #if BIHASH_KVP_AT_BUCKET_LEVEL
366  uword offset;
367  offset = (hash & (h->nbuckets - 1));
368  offset = offset * (sizeof (BVT (clib_bihash_bucket))
369  + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
370  return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
371 #endif
372 
373  return h->buckets + (hash & (h->nbuckets - 1));
374 }
375 
376 static inline int BV (clib_bihash_search_inline_with_hash)
377  (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
378 {
379  BVT (clib_bihash_value) * v;
380  BVT (clib_bihash_bucket) * b;
381  int i, limit;
382 
383  /* *INDENT-OFF* */
384  static const BVT (clib_bihash_bucket) mask = {
385  .linear_search = 1,
386  .log2_pages = -1
387  };
388  /* *INDENT-ON* */
389 
390 #if BIHASH_LAZY_INSTANTIATE
391  if (PREDICT_FALSE (alloc_arena (h) == 0))
392  return -1;
393 #endif
394 
395  b = BV (clib_bihash_get_bucket) (h, hash);
396 
397  if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
398  return -1;
399 
400  if (PREDICT_FALSE (b->lock))
401  {
402  volatile BVT (clib_bihash_bucket) * bv = b;
403  while (bv->lock)
404  CLIB_PAUSE ();
405  }
406 
407  v = BV (clib_bihash_get_value) (h, b->offset);
408 
409  /* If the bucket has unresolvable collisions, use linear search */
410  limit = BIHASH_KVP_PER_PAGE;
411 
412  if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
413  {
414  if (PREDICT_FALSE (b->linear_search))
415  limit <<= b->log2_pages;
416  else
417  v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
418  }
419 
420  for (i = 0; i < limit; i++)
421  {
422  if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
423  {
424  *key_result = v->kvp[i];
425  return 0;
426  }
427  }
428  return -1;
429 }
430 
431 static inline int BV (clib_bihash_search_inline)
432  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
433 {
434  u64 hash;
435 
436  hash = BV (clib_bihash_hash) (key_result);
437 
438  return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
439 }
440 
441 static inline void BV (clib_bihash_prefetch_bucket)
442  (BVT (clib_bihash) * h, u64 hash)
443 {
444  CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
446  LOAD);
447 }
448 
449 static inline void BV (clib_bihash_prefetch_data)
450  (BVT (clib_bihash) * h, u64 hash)
451 {
452  BVT (clib_bihash_value) * v;
453  BVT (clib_bihash_bucket) * b;
454 
455 #if BIHASH_LAZY_INSTANTIATE
456  if (PREDICT_FALSE (alloc_arena (h) == 0))
457  return;
458 #endif
459 
460  b = BV (clib_bihash_get_bucket) (h, hash);
461 
462  if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
463  return;
464 
465  v = BV (clib_bihash_get_value) (h, b->offset);
466 
467  if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
468  v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
469 
470  CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
471  LOAD);
472 }
473 
474 static inline int BV (clib_bihash_search_inline_2_with_hash)
475  (BVT (clib_bihash) * h,
476  u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
477 {
478  BVT (clib_bihash_value) * v;
479  BVT (clib_bihash_bucket) * b;
480  int i, limit;
481 
482 /* *INDENT-OFF* */
483  static const BVT (clib_bihash_bucket) mask = {
484  .linear_search = 1,
485  .log2_pages = -1
486  };
487 /* *INDENT-ON* */
488 
489  ASSERT (valuep);
490 
491 #if BIHASH_LAZY_INSTANTIATE
492  if (PREDICT_FALSE (alloc_arena (h) == 0))
493  return -1;
494 #endif
495 
496  b = BV (clib_bihash_get_bucket) (h, hash);
497 
498  if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
499  return -1;
500 
501  if (PREDICT_FALSE (b->lock))
502  {
503  volatile BVT (clib_bihash_bucket) * bv = b;
504  while (bv->lock)
505  CLIB_PAUSE ();
506  }
507 
508  v = BV (clib_bihash_get_value) (h, b->offset);
509 
510  /* If the bucket has unresolvable collisions, use linear search */
511  limit = BIHASH_KVP_PER_PAGE;
512 
513  if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
514  {
515  if (PREDICT_FALSE (b->linear_search))
516  limit <<= b->log2_pages;
517  else
518  v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
519  }
520 
521  for (i = 0; i < limit; i++)
522  {
523  if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
524  {
525  *valuep = v->kvp[i];
526  return 0;
527  }
528  }
529  return -1;
530 }
531 
532 static inline int BV (clib_bihash_search_inline_2)
533  (BVT (clib_bihash) * h,
534  BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
535 {
536  u64 hash;
537 
538  hash = BV (clib_bihash_hash) (search_key);
539 
540  return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
541  valuep);
542 }
543 
544 
545 #endif /* __included_bihash_template_h__ */
546 
547 /** @endcond */
548 
549 /*
550  * fd.io coding-style-patch-verification: ON
551  *
552  * Local Variables:
553  * eval: (c-set-style "gnu")
554  * End:
555  */
u8 count
Definition: dhcp.api:208
#define CLIB_PAUSE()
Definition: lock.h:23
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:24
a
Definition: bitmap.h:538
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.
vl_api_address_t src
Definition: gre.api:54
unsigned char u8
Definition: types.h:56
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
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.
#define clib_atomic_fetch_or(a, b)
Definition: atomics.h:27
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.
int(* clib_bihash_foreach_key_value_pair_cb)(clib_bihash_kv *kv, void *ctx)
Definition: bihash_doc.h:175
void clib_bihash_foreach_key_value_pair(clib_bihash *h, clib_bihash_foreach_key_value_pair_cb *callback, void *arg)
Visit active (key,value) pairs in a bi-hash table.
long ctx[MAX_CONNS]
Definition: main.c:144
u64 memory_size
Definition: vhost_user.h:151
vec_header_t h
Definition: buffer.c:322
#define PREDICT_FALSE(x)
Definition: clib.h:118
int clib_bihash_search_inline(clib_bihash *h, clib_bihash_kv *in_out_kv)
Search a bi-hash table.
vl_api_address_t dst
Definition: gre.api:55
void clib_bihash_init(clib_bihash *h, char *name, u32 nbuckets, uword memory_size)
initialize a bounded index extensible hash table
void clib_bihash_prefetch_bucket(clib_bihash *h, u64 hash)
Prefetch a bi-hash bucket given a hash code.
BVT(clib_bihash)
Definition: l2_fib.c:972
#define BIHASH_KVP_AT_BUCKET_LEVEL
Definition: bihash_16_8.h:25
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
void clib_bihash_prefetch_data(clib_bihash *h, u64 hash)
Prefetch bi-hash (key,value) data given a hash code.
string name[64]
Definition: ip.api:44
#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.
#define BIHASH_BUCKET_PREFETCH_CACHE_LINES
Definition: bihash_16_8.h:27
u8 log2_pages
Definition: bihash_doc.h:62
static uword extract_bits(uword x, int start, int count)
Definition: clib.h:304
void clib_bihash_copied(void *dst, void *src)
template key/value backing page structure
Definition: bihash_doc.h:44
u64 uword
Definition: types.h:112
struct clib_bihash_value offset
template key/value backing page structure
static void * clib_bihash_get_value(clib_bihash *h, uword offset)
Get pointer to value page given its clib mheap offset.
void ** clib_all_bihashes
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
void * clib_all_bihash_set_heap(void)
#define STATIC_ASSERT_SIZEOF(d, s)
#define CLIB_PACKED(x)
Definition: clib.h:85