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