vppinfra: make first bihash add thread-safe
[vpp.git] / src / vppinfra / bihash_template.h
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   };
76 } BVT (clib_bihash_value);
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;
132   u64 memory_size;
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 extern void **clib_all_bihashes;
163
164 #if BIHASH_32_64_SVM
165 #undef alloc_arena_next
166 #undef alloc_arena_size
167 #undef alloc_arena
168 #undef CLIB_BIHASH_READY_MAGIC
169 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
170 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
171 #define alloc_arena(h) ((h)->alloc_arena)
172 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
173 #else
174 #undef alloc_arena_next
175 #undef alloc_arena_size
176 #undef alloc_arena
177 #undef CLIB_BIHASH_READY_MAGIC
178 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
179 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
180 #define alloc_arena(h) ((h)->alloc_arena)
181 #define CLIB_BIHASH_READY_MAGIC 0
182 #endif
183
184 #ifndef BIHASH_STAT_IDS
185 #define BIHASH_STAT_IDS 1
186
187 #define foreach_bihash_stat                     \
188 _(alloc_add)                                    \
189 _(add)                                          \
190 _(split_add)                                    \
191 _(replace)                                      \
192 _(update)                                       \
193 _(del)                                          \
194 _(del_free)                                     \
195 _(linear)                                       \
196 _(resplit)                                      \
197 _(working_copy_lost)                            \
198 _(splits)                       /* must be last */
199
200 typedef enum
201 {
202 #define _(a) BIHASH_STAT_##a,
203   foreach_bihash_stat
204 #undef _
205     BIHASH_STAT_N_STATS,
206 } BVT (clib_bihash_stat_id);
207 #endif /* BIHASH_STAT_IDS */
208
209 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
210                                                     int stat_id, u64 count)
211 {
212 #if BIHASH_ENABLE_STATS
213   if (PREDICT_FALSE (h->inc_stats_callback != 0))
214     h->inc_stats_callback (h, stat_id, count);
215 #endif
216 }
217
218 #if BIHASH_ENABLE_STATS
219 static inline void BV (clib_bihash_set_stats_callback)
220   (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
221    void *ctx)
222 {
223   h->inc_stats_callback = cb;
224   h->inc_stats_context = ctx;
225 }
226 #endif
227
228
229 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
230 {
231   while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
232     CLIB_PAUSE ();
233 }
234
235 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
236 {
237   __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
238 }
239
240 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
241 {
242   BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
243
244   do
245     {
246       locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
247       unlocked_bucket.lock = 0;
248       locked_bucket.lock = 1;
249       CLIB_PAUSE ();
250     }
251   while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
252                                       locked_bucket.as_u64, 1 /* weak */ ,
253                                       __ATOMIC_ACQUIRE,
254                                       __ATOMIC_ACQUIRE) == 0);
255 }
256
257 static inline void BV (clib_bihash_unlock_bucket)
258   (BVT (clib_bihash_bucket) * b)
259 {
260   CLIB_MEMORY_BARRIER ();
261   b->lock = 0;
262 }
263
264 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
265                                                 uword offset)
266 {
267   u8 *hp = (u8 *) (uword) alloc_arena (h);
268   u8 *vp = hp + offset;
269
270   return (void *) vp;
271 }
272
273 static inline int BV (clib_bihash_bucket_is_empty)
274   (BVT (clib_bihash_bucket) * b)
275 {
276   /* Note: applied to locked buckets, test offset */
277   return b->offset == 0;
278 }
279
280 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
281                                                  void *v)
282 {
283   u8 *hp, *vp;
284
285   hp = (u8 *) (uword) alloc_arena (h);
286   vp = (u8 *) v;
287
288   return vp - hp;
289 }
290
291 void BV (clib_bihash_init)
292   (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
293
294 #if BIHASH_32_64_SVM
295 void BV (clib_bihash_master_init_svm)
296   (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
297 void BV (clib_bihash_slave_init_svm)
298   (BVT (clib_bihash) * h, char *name, int fd);
299 #endif
300
301 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
302                                          format_function_t * fmt_fn);
303
304 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
305
306 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
307                               BVT (clib_bihash_kv) * add_v, int is_add);
308 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
309                                              BVT (clib_bihash_kv) * add_v,
310                                              int (*is_stale_cb) (BVT
311                                                                  (clib_bihash_kv)
312                                                                  *, void *),
313                                              void *arg);
314 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
315                              BVT (clib_bihash_kv) * search_v,
316                              BVT (clib_bihash_kv) * return_v);
317
318 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
319                                               void *callback, void *arg);
320 void *clib_all_bihash_set_heap (void);
321 void clib_bihash_copied (void *dst, void *src);
322
323 format_function_t BV (format_bihash);
324 format_function_t BV (format_bihash_kvp);
325 format_function_t BV (format_bihash_lru);
326
327 static inline int BV (clib_bihash_search_inline_with_hash)
328   (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
329 {
330   u32 bucket_index;
331   BVT (clib_bihash_value) * v;
332   BVT (clib_bihash_bucket) * b;
333   int i, limit;
334
335   if (PREDICT_FALSE (alloc_arena (h) == 0))
336     return -1;
337
338   bucket_index = hash & (h->nbuckets - 1);
339   b = &h->buckets[bucket_index];
340
341   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
342     return -1;
343
344   if (PREDICT_FALSE (b->lock))
345     {
346       volatile BVT (clib_bihash_bucket) * bv = b;
347       while (bv->lock)
348         CLIB_PAUSE ();
349     }
350
351   hash >>= h->log2_nbuckets;
352
353   v = BV (clib_bihash_get_value) (h, b->offset);
354
355   /* If the bucket has unresolvable collisions, use linear search */
356   limit = BIHASH_KVP_PER_PAGE;
357   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
358   if (PREDICT_FALSE (b->linear_search))
359     limit <<= b->log2_pages;
360
361   for (i = 0; i < limit; i++)
362     {
363       if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
364         {
365           *key_result = v->kvp[i];
366           return 0;
367         }
368     }
369   return -1;
370 }
371
372 static inline int BV (clib_bihash_search_inline)
373   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
374 {
375   u64 hash;
376
377   hash = BV (clib_bihash_hash) (key_result);
378
379   return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
380 }
381
382 static inline void BV (clib_bihash_prefetch_bucket)
383   (BVT (clib_bihash) * h, u64 hash)
384 {
385   u32 bucket_index;
386   BVT (clib_bihash_bucket) * b;
387
388   bucket_index = hash & (h->nbuckets - 1);
389   b = &h->buckets[bucket_index];
390
391   CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
392 }
393
394 static inline void BV (clib_bihash_prefetch_data)
395   (BVT (clib_bihash) * h, u64 hash)
396 {
397   u32 bucket_index;
398   BVT (clib_bihash_value) * v;
399   BVT (clib_bihash_bucket) * b;
400
401   if (PREDICT_FALSE (alloc_arena (h) == 0))
402     return;
403
404   bucket_index = hash & (h->nbuckets - 1);
405   b = &h->buckets[bucket_index];
406
407   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
408     return;
409
410   hash >>= h->log2_nbuckets;
411   v = BV (clib_bihash_get_value) (h, b->offset);
412
413   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
414
415   CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
416 }
417
418 static inline int BV (clib_bihash_search_inline_2_with_hash)
419   (BVT (clib_bihash) * h,
420    u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
421 {
422   u32 bucket_index;
423   BVT (clib_bihash_value) * v;
424   BVT (clib_bihash_bucket) * b;
425   int i, limit;
426
427   ASSERT (valuep);
428
429   if (PREDICT_FALSE (alloc_arena (h) == 0))
430     return -1;
431
432   bucket_index = hash & (h->nbuckets - 1);
433   b = &h->buckets[bucket_index];
434
435   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
436     return -1;
437
438   if (PREDICT_FALSE (b->lock))
439     {
440       volatile BVT (clib_bihash_bucket) * bv = b;
441       while (bv->lock)
442         CLIB_PAUSE ();
443     }
444
445   hash >>= h->log2_nbuckets;
446   v = BV (clib_bihash_get_value) (h, b->offset);
447
448   /* If the bucket has unresolvable collisions, use linear search */
449   limit = BIHASH_KVP_PER_PAGE;
450   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
451   if (PREDICT_FALSE (b->linear_search))
452     limit <<= b->log2_pages;
453
454   for (i = 0; i < limit; i++)
455     {
456       if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
457         {
458           *valuep = v->kvp[i];
459           return 0;
460         }
461     }
462   return -1;
463 }
464
465 static inline int BV (clib_bihash_search_inline_2)
466   (BVT (clib_bihash) * h,
467    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
468 {
469   u64 hash;
470
471   hash = BV (clib_bihash_hash) (search_key);
472
473   return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
474                                                      valuep);
475 }
476
477
478 #endif /* __included_bihash_template_h__ */
479
480 /** @endcond */
481
482 /*
483  * fd.io coding-style-patch-verification: ON
484  *
485  * Local Variables:
486  * eval: (c-set-style "gnu")
487  * End:
488  */