1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
Copyright (c) 2014 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
/*
* Note: to instantiate the template multiple times in a single file,
* #undef __included_bihash_template_h__...
*/
#ifndef __included_bihash_template_h__
#define __included_bihash_template_h__
#include <vppinfra/heap.h>
#include <vppinfra/format.h>
#include <vppinfra/pool.h>
#include <vppinfra/cache.h>
#include <vppinfra/lock.h>
#ifndef BIHASH_TYPE
#error BIHASH_TYPE not defined
#endif
#ifdef BIHASH_32_64_SVM
#undef HAVE_MEMFD_CREATE
#include <vppinfra/linux/syscall.h>
#include <fcntl.h>
#define F_LINUX_SPECIFIC_BASE 1024
#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
#define F_SEAL_SHRINK (2)
/* Max page size 2**16 due to refcount width */
#define BIHASH_FREELIST_LENGTH 17
#endif
#define _bv(a,b) a##b
#define __bv(a,b) _bv(a,b)
#define BV(a) __bv(a,BIHASH_TYPE)
#define _bvt(a,b) a##b##_t
#define __bvt(a,b) _bvt(a,b)
#define BVT(a) __bvt(a,BIHASH_TYPE)
#if _LP64 == 0
#define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
#define u64_to_pointer(x) (void *)(u32)((x))
#define pointer_to_u64(x) (u64)(u32)((x))
#else
#define OVERFLOW_ASSERT(x)
#define u64_to_pointer(x) (void *)((x))
#define pointer_to_u64(x) (u64)((x))
#endif
typedef struct BV (clib_bihash_value)
{
union
{
BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
u64 next_free_as_u64;
};
} BVT (clib_bihash_value);
#define BIHASH_BUCKET_OFFSET_BITS 36
typedef struct
{
union
{
struct
{
u64 offset:BIHASH_BUCKET_OFFSET_BITS;
u64 lock:1;
u64 linear_search:1;
u64 log2_pages:8;
u64 refcnt:16;
};
u64 as_u64;
};
} BVT (clib_bihash_bucket);
STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
/* *INDENT-OFF* */
typedef CLIB_PACKED (struct {
/*
* Backing store allocation. Since bihash manages its own
* freelists, we simple dole out memory at alloc_arena_next.
*/
u64 alloc_arena_next; /* Next VA to allocate, definitely NOT a constant */
u64 alloc_arena_size; /* Size of the arena */
u64 alloc_arena; /* Base VA of the arena */
/* Two SVM pointers stored as 8-byte integers */
u64 alloc_lock_as_u64;
u64 buckets_as_u64;
/* freelist list-head arrays/vectors */
u64 freelists_as_u64;
u32 nbuckets; /* Number of buckets */
/* Set when header valid */
volatile u32 ready;
u64 pad;
}) BVT (clib_bihash_shared_header);
/* *INDENT-ON* */
STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
typedef struct
{
BVT (clib_bihash_bucket) * buckets;
volatile u32 *alloc_lock;
BVT (clib_bihash_value) ** working_copies;
int *working_copy_lengths;
BVT (clib_bihash_bucket) saved_bucket;
u32 nbuckets;
u32 log2_nbuckets;
u8 *name;
u64 *freelists;
#if BIHASH_32_64_SVM
BVT (clib_bihash_shared_header) * sh;
int memfd;
#else
BVT (clib_bihash_shared_header) sh;
#endif
/**
* A custom format function to print the Key and Value of bihash_key instead of default hexdump
*/
format_function_t *fmt_fn;
} BVT (clib_bihash);
#if BIHASH_32_64_SVM
#undef alloc_arena_next
#undef alloc_arena_size
#undef alloc_arena
#undef CLIB_BIHASH_READY_MAGIC
#define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
#define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
#define alloc_arena(h) (((h)->sh)->alloc_arena)
#define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
#else
#undef alloc_arena_next
#undef alloc_arena_size
#undef alloc_arena
#undef CLIB_BIHASH_READY_MAGIC
#define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
#define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
#define alloc_arena(h) ((h)->sh.alloc_arena)
#define CLIB_BIHASH_READY_MAGIC 0
#endif
static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
{
while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
CLIB_PAUSE ();
}
static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
{
__atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
}
static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
{
BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
do
{
locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
unlocked_bucket.lock = 0;
locked_bucket.lock = 1;
CLIB_PAUSE ();
}
while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
locked_bucket.as_u64, 1 /* weak */ ,
__ATOMIC_ACQUIRE,
__ATOMIC_ACQUIRE) == 0);
}
static inline void BV (clib_bihash_unlock_bucket)
(BVT (clib_bihash_bucket) * b)
{
CLIB_MEMORY_BARRIER ();
b->lock = 0;
}
static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
uword offset)
{
u8 *hp = (u8 *) (uword) alloc_arena (h);
u8 *vp = hp + offset;
return (void *) vp;
}
static inline int BV (clib_bihash_bucket_is_empty)
(BVT (clib_bihash_bucket) * b)
{
/* Note: applied to locked buckets, test offset */
return b->offset == 0;
}
static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
void *v)
{
u8 *hp, *vp;
hp = (u8 *) (uword) alloc_arena (h);
vp = (u8 *) v;
return vp - hp;
}
void BV (clib_bihash_init)
(BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
#if BIHASH_32_64_SVM
void BV (clib_bihash_master_init_svm)
(BVT (clib_bihash) * h, char *name, u32 nbuckets,
u64 base_address, u64 memory_size);
void BV (clib_bihash_slave_init_svm)
(BVT (clib_bihash) * h, char *name, int fd);
#endif
void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
format_function_t * fmt_fn);
void BV (clib_bihash_free) (BVT (clib_bihash) * h);
int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
BVT (clib_bihash_kv) * add_v, int is_add);
int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
BVT (clib_bihash_kv) * add_v,
int (*is_stale_cb) (BVT
(clib_bihash_kv)
*, void *),
void *arg);
int BV (clib_bihash_search) (BVT (clib_bihash) * h,
BVT (clib_bihash_kv) * search_v,
BVT (clib_bihash_kv) * return_v);
void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
void *callback, void *arg);
format_function_t BV (format_bihash);
format_function_t BV (format_bihash_kvp);
format_function_t BV (format_bihash_lru);
static inline int BV (clib_bihash_search_inline_with_hash)
(BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
{
u32 bucket_index;
BVT (clib_bihash_value) * v;
BVT (clib_bihash_bucket) * b;
int i, limit;
bucket_index = hash & (h->nbuckets - 1);
b = &h->buckets[bucket_index];
if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
return -1;
if (PREDICT_FALSE (b->lock))
{
volatile BVT (clib_bihash_bucket) * bv = b;
while (bv->lock)
CLIB_PAUSE ();
}
hash >>= h->log2_nbuckets;
v = BV (clib_bihash_get_value) (h, b->offset);
/* If the bucket has unresolvable collisions, use linear search */
limit = BIHASH_KVP_PER_PAGE;
v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
if (PREDICT_FALSE (b->linear_search))
limit <<= b->log2_pages;
for (i = 0; i < limit; i++)
{
if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
{
*key_result = v->kvp[i];
return 0;
}
}
return -1;
}
static inline int BV (clib_bihash_search_inline)
(BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
{
u64 hash;
hash = BV (clib_bihash_hash) (key_result);
return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
}
static inline void BV (clib_bihash_prefetch_bucket)
(BVT (clib_bihash) * h, u64 hash)
{
u32 bucket_index;
BVT (clib_bihash_bucket) * b;
bucket_index = hash & (h->nbuckets - 1);
b = &h->buckets[bucket_index];
CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
}
static inline void BV (clib_bihash_prefetch_data)
(BVT (clib_bihash) * h, u64 hash)
{
u32 bucket_index;
BVT (clib_bihash_value) * v;
BVT (clib_bihash_bucket) * b;
bucket_index = hash & (h->nbuckets - 1);
b = &h->buckets[bucket_index];
if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
return;
hash >>= h->log2_nbuckets;
v = BV (clib_bihash_get_value) (h, b->offset);
v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
}
static inline int BV (clib_bihash_search_inline_2_with_hash)
(BVT (clib_bihash) * h,
u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
{
u32 bucket_index;
BVT (clib_bihash_value) * v;
BVT (clib_bihash_bucket) * b;
int i, limit;
ASSERT (valuep);
bucket_index = hash & (h->nbuckets - 1);
b = &h->buckets[bucket_index];
if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
return -1;
if (PREDICT_FALSE (b->lock))
{
volatile BVT (clib_bihash_bucket) * bv = b;
while (bv->lock)
CLIB_PAUSE ();
}
hash >>= h->log2_nbuckets;
v = BV (clib_bihash_get_value) (h, b->offset);
/* If the bucket has unresolvable collisions, use linear search */
limit = BIHASH_KVP_PER_PAGE;
v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
if (PREDICT_FALSE (b->linear_search))
limit <<= b->log2_pages;
for (i = 0; i < limit; i++)
{
if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
{
*valuep = v->kvp[i];
return 0;
}
}
return -1;
}
static inline int BV (clib_bihash_search_inline_2)
(BVT (clib_bihash) * h,
BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
{
u64 hash;
hash = BV (clib_bihash_hash) (search_key);
return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
valuep);
}
#endif /* __included_bihash_template_h__ */
/** @endcond */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
ss="n">i++)
if (vls->workers_subscribed[i] == wrk_index)
return 1;
return 0;
}
static void
vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
{
clib_bitmap_set (vls->listeners, wrk_index, is_active);
}
static u8
vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
{
return (clib_bitmap_get (vls->listeners, wrk_index) == 1);
}
static void
vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
{
vppcom_session_listen (vls_to_sh (vls), ~0);
vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
}
static void
vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
{
vcl_worker_t *wrk;
vcl_session_t *s;
wrk = vcl_worker_get (wrk_index);
s = vcl_session_get (wrk, vls->session_index);
if (s->session_state != STATE_LISTEN)
return;
vppcom_send_unbind_sock (wrk, s->vpp_handle);
s->session_state = STATE_LISTEN_NO_MQ;
vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
}
int
vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
{
int i, do_disconnect;
vcl_session_t *s;
s = vcl_session_get (wrk, vls->session_index);
if (s->session_state == STATE_LISTEN)
vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
for (i = 0; i < vec_len (vls->workers_subscribed); i++)
{
if (vls->workers_subscribed[i] != wrk->wrk_index)
continue;
if (s->rx_fifo)
{
svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
}
vec_del1 (vls->workers_subscribed, i);
do_disconnect = s->session_state == STATE_LISTEN;
vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
return 0;
}
/* Return, if this is not the owning worker */
if (vls->worker_index != wrk->wrk_index)
return 0;
/* Check if we can change owner or close */
if (vec_len (vls->workers_subscribed))
{
vls->worker_index = vls->workers_subscribed[0];
vec_del1 (vls->workers_subscribed, 0);
vcl_send_session_worker_update (wrk, s, vls->worker_index);
if (vec_len (vls->workers_subscribed))
clib_warning ("more workers need to be updated");
}
else
{
vcl_session_cleanup (wrk, s, vcl_session_handle (s),
1 /* do_disconnect */ );
}
return 0;
}
void
vls_share_vcl_session (vcl_worker_t * wrk, vcl_session_t * s)
{
vcl_locked_session_t *vls;
vls = vls_get (vls_si_to_vlsh (s->session_index));
if (!vls)
return;
vls_lock (vls);
vec_add1 (vls->workers_subscribed, wrk->wrk_index);
if (s->rx_fifo)
{
svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
}
else if (s->session_state == STATE_LISTEN)
{
s->session_state = STATE_LISTEN_NO_MQ;
}
vls_unlock (vls);
}
void
vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
{
vcl_worker_t *wrk = vcl_worker_get_current ();
vcl_session_t *s;
wrk->vpp_event_queues = vec_dup (parent_wrk->vpp_event_queues);
wrk->sessions = pool_dup (parent_wrk->sessions);
wrk->session_index_by_vpp_handles =
hash_dup (parent_wrk->session_index_by_vpp_handles);
vls_table_rlock ();
/* *INDENT-OFF* */
pool_foreach (s, wrk->sessions, ({
vls_share_vcl_session (wrk, s);
}));
/* *INDENT-ON* */
vls_table_runlock ();
}
static void
vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
{
vcl_worker_t *wrk = vcl_worker_get_current ();
vcl_session_t *s = 0;
int is_nonblk = 0;
if (vls)
{
s = vcl_session_get (wrk, vls->session_index);
if (PREDICT_FALSE (!s))
return;
is_nonblk = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
}
switch (op)
{
case VLS_MT_OP_READ:
if (!is_nonblk)
is_nonblk = vcl_session_read_ready (s) != 0;
if (!is_nonblk)
{
vls_mt_mq_lock ();
*locks_acq |= VLS_MT_LOCK_MQ;
}
break;
case VLS_MT_OP_WRITE:
ASSERT (s);
if (!is_nonblk)
is_nonblk = vcl_session_write_ready (s) != 0;
if (!is_nonblk)
{
vls_mt_mq_lock ();
*locks_acq |= VLS_MT_LOCK_MQ;
}
break;
case VLS_MT_OP_XPOLL:
vls_mt_mq_lock ();
*locks_acq |= VLS_MT_LOCK_MQ;
break;
case VLS_MT_OP_SPOOL:
vls_mt_spool_lock ();
*locks_acq |= VLS_MT_LOCK_SPOOL;
break;
default:
break;
}
}
static void
vls_mt_rel_locks (int locks_acq)
{
if (locks_acq & VLS_MT_LOCK_MQ)
vls_mt_mq_unlock ();
if (locks_acq & VLS_MT_LOCK_SPOOL)
vls_mt_create_unlock ();
}
#define vls_mt_guard(_vls, _op) \
int _locks_acq = 0; \
if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) \
vls_mt_add (); \
if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
vls_mt_acq_locks (_vls, _op, &_locks_acq); \
#define vls_mt_unguard() \
if (PREDICT_FALSE (_locks_acq)) \
vls_mt_rel_locks (_locks_acq)
int
vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
int
vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
int
vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
vppcom_endpt_t * ep)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
ssize_t
vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_READ);
rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
ssize_t
vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
vppcom_endpt_t * ep)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_READ);
rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
ep);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
int
vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
{
vcl_locked_session_t *vls;
int rv;
if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
vls_mt_add ();
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
vls_get_and_unlock (vlsh);
return rv;
}
int
vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
vls_get_and_unlock (vlsh);
return rv;
}
int
vls_listen (vls_handle_t vlsh, int q_len)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_XPOLL);
rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
int
vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_XPOLL);
rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
vls_mt_unguard ();
vls_get_and_unlock (vlsh);
return rv;
}
static inline void
vls_mp_checks (vcl_locked_session_t * vls, int is_add)
{
vcl_worker_t *wrk = vcl_worker_get_current ();
vcl_session_t *s;
s = vcl_session_get (wrk, vls->session_index);
switch (s->session_state)
{
case STATE_LISTEN:
if (is_add)
{
if (vls->worker_index == wrk->wrk_index)
vls_listener_wrk_set (vls, wrk->wrk_index, 1 /* is_active */ );
break;
}
vls_listener_wrk_stop_listen (vls, vls->worker_index);
break;
case STATE_LISTEN_NO_MQ:
if (!is_add)
break;
/* Register worker as listener */
vls_listener_wrk_start_listen (vls, wrk->wrk_index);
/* If owner worker did not attempt to accept/xpoll on the session,
* force a listen stop for it, since it may not be interested in
* accepting new sessions.
* This is pretty much a hack done to give app workers the illusion
* that it is fine to listen and not accept new sessions for a
* given listener. Without it, we would accumulate unhandled
* accepts on the passive worker message queue. */
if (!vls_listener_wrk_is_active (vls, vls->worker_index))
vls_listener_wrk_stop_listen (vls, vls->worker_index);
break;
default:
break;
}
}
vls_handle_t
vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
{
vls_handle_t accepted_vlsh;
vcl_locked_session_t *vls;
int sh;
if (!(vls = vls_get_w_dlock (listener_vlsh)))
return VPPCOM_EBADFD;
if (vcl_n_workers () > 1)
vls_mp_checks (vls, 1 /* is_add */ );
vls_mt_guard (vls, VLS_MT_OP_SPOOL);
sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
vls_mt_unguard ();
vls_get_and_unlock (listener_vlsh);
if (sh < 0)
return sh;
accepted_vlsh = vls_alloc (sh);
if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
vppcom_session_close (sh);
return accepted_vlsh;
}
vls_handle_t
vls_create (uint8_t proto, uint8_t is_nonblocking)
{
vcl_session_handle_t sh;
vls_handle_t vlsh;
vls_mt_guard (0, VLS_MT_OP_SPOOL);
sh = vppcom_session_create (proto, is_nonblocking);
vls_mt_unguard ();
if (sh == INVALID_SESSION_ID)
return VLS_INVALID_HANDLE;
vlsh = vls_alloc (sh);
if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
vppcom_session_close (sh);
return vlsh;
}
int
vls_close (vls_handle_t vlsh)
{
vcl_locked_session_t *vls;
int rv;
vls_table_wlock ();
vls = vls_get_and_lock (vlsh);
if (!vls)
{
vls_table_wunlock ();
return VPPCOM_EBADFD;
}
vls_mt_guard (0, VLS_MT_OP_SPOOL);
if (vls_is_shared (vls))
{
/* At least two workers share the session so vls won't be freed */
vls_unshare_session (vls, vcl_worker_get_current ());
vls_unlock (vls);
vls_mt_unguard ();
vls_table_wunlock ();
return VPPCOM_OK;
}
rv = vppcom_session_close (vls_to_sh (vls));
vls_free (vls);
vls_mt_unguard ();
vls_table_wunlock ();
return rv;
}
vls_handle_t
vls_epoll_create (void)
{
vcl_session_handle_t sh;
vls_handle_t vlsh;
if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
vls_mt_add ();
sh = vppcom_epoll_create ();
if (sh == INVALID_SESSION_ID)
return VLS_INVALID_HANDLE;
vlsh = vls_alloc (sh);
if (vlsh == VLS_INVALID_HANDLE)
vppcom_session_close (sh);
return vlsh;
}
static void
vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
{
if (vcl_n_workers () <= 1)
{
vlsl->epoll_mp_check = 1;
return;
}
if (op == EPOLL_CTL_MOD)
return;
vlsl->epoll_mp_check = 1;
vls_mp_checks (vls, op == EPOLL_CTL_ADD);
}
int
vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
struct epoll_event *event)
{
vcl_locked_session_t *ep_vls, *vls;
vcl_session_handle_t ep_sh, sh;
int rv;
vls_table_rlock ();
ep_vls = vls_get_and_lock (ep_vlsh);
vls = vls_get_and_lock (vlsh);
ep_sh = vls_to_sh (ep_vls);
sh = vls_to_sh (vls);
if (PREDICT_FALSE (!vlsl->epoll_mp_check))
vls_epoll_ctl_mp_checks (vls, op);
vls_table_runlock ();
rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
vls_table_rlock ();
ep_vls = vls_get (ep_vlsh);
vls = vls_get (vlsh);
vls_unlock (vls);
vls_unlock (ep_vls);
vls_table_runlock ();
return rv;
}
int
vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
int maxevents, double wait_for_time)
{
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (ep_vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (0, VLS_MT_OP_XPOLL);
rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
wait_for_time);
vls_mt_unguard ();
vls_get_and_unlock (ep_vlsh);
return rv;
}
static void
vls_select_mp_checks (vcl_si_set * read_map)
{
vcl_locked_session_t *vls;
vcl_worker_t *wrk;
vcl_session_t *s;
u32 si;
if (vcl_n_workers () <= 1)
{
vlsl->select_mp_check = 1;
return;
}
if (!read_map)
return;
vlsl->select_mp_check = 1;
wrk = vcl_worker_get_current ();
/* *INDENT-OFF* */
clib_bitmap_foreach (si, read_map, ({
s = vcl_session_get (wrk, si);
if (s->session_state == STATE_LISTEN)
{
vls = vls_get (vls_session_index_to_vlsh (si));
vls_mp_checks (vls, 1 /* is_add */);
}
}));
/* *INDENT-ON* */
}
int
vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
vcl_si_set * except_map, double wait_for_time)
{
int rv;
vls_mt_guard (0, VLS_MT_OP_XPOLL);
if (PREDICT_FALSE (!vlsl->select_mp_check))
vls_select_mp_checks (read_map);
rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
vls_mt_unguard ();
return rv;
}
static void
vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
{
u32 current_wrk, is_current;
vcl_locked_session_t *vls;
vcl_session_t *s;
if (pool_elts (vcm->workers) <= 1)
return;
current_wrk = vcl_get_worker_index ();
is_current = current_wrk == wrk->wrk_index;
vls_table_wlock ();
/* *INDENT-OFF* */
pool_foreach (s, wrk->sessions, ({
vls = vls_get (vls_si_to_vlsh (s->session_index));
if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
vls_unshare_session (vls, wrk);
}));
/* *INDENT-ON* */
vls_table_wunlock ();
}
static void
vls_cleanup_vcl_worker (vcl_worker_t * wrk)
{
/* Unshare sessions and also cleanup worker since child may have
* called _exit () and therefore vcl may not catch the event */
vls_unshare_vcl_worker_sessions (wrk);
vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
}
static void
vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
{
vcl_worker_t *sub_child;
int tries = 0;
if (child_wrk->forked_child != ~0)
{
sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
if (sub_child)
{
/* Wait a bit, maybe the process is going away */
while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
usleep (1e3);
if (kill (sub_child->current_pid, 0) < 0)
vls_cleanup_forked_child (child_wrk, sub_child);
}
}
vls_cleanup_vcl_worker (child_wrk);
VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
wrk->forked_child = ~0;
}
static struct sigaction old_sa;
static void
vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
{
vcl_worker_t *wrk, *child_wrk;
if (vcl_get_worker_index () == ~0)
return;
if (sigaction (SIGCHLD, &old_sa, 0))
{
VERR ("couldn't restore sigchld");
exit (-1);
}
wrk = vcl_worker_get_current ();
if (wrk->forked_child == ~0)
return;
child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
if (!child_wrk)
goto done;
if (si && si->si_pid != child_wrk->current_pid)
{
VDBG (0, "unexpected child pid %u", si->si_pid);
goto done;
}
vls_cleanup_forked_child (wrk, child_wrk);
done:
if (old_sa.sa_flags & SA_SIGINFO)
{
void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
fn (signum, si, uc);
}
else
{
void (*fn) (int) = old_sa.sa_handler;
if (fn)
fn (signum);
}
}
static void
vls_incercept_sigchld ()
{
struct sigaction sa;
clib_memset (&sa, 0, sizeof (sa));
sa.sa_sigaction = vls_intercept_sigchld_handler;
sa.sa_flags = SA_SIGINFO;
if (sigaction (SIGCHLD, &sa, &old_sa))
{
VERR ("couldn't intercept sigchld");
exit (-1);
}
}
static void
vls_app_pre_fork (void)
{
vls_incercept_sigchld ();
vcl_flush_mq_events ();
}
static void
vls_app_fork_child_handler (void)
{
vcl_worker_t *parent_wrk;
int rv, parent_wrk_index;
u8 *child_name;
parent_wrk_index = vcl_get_worker_index ();
VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
parent_wrk_index);
/*
* Allocate worker
*/
vcl_set_worker_index (~0);
if (!vcl_worker_alloc_and_init ())
VERR ("couldn't allocate new worker");
/*
* Attach to binary api
*/
child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
vcl_cleanup_bapi ();
vppcom_api_hookup ();
vcm->app_state = STATE_APP_START;
rv = vppcom_connect_to_vpp ((char *) child_name);
vec_free (child_name);
if (rv)
{
VERR ("couldn't connect to VPP!");
return;
}
/*
* Register worker with vpp and share sessions
*/
vcl_worker_register_with_vpp ();
parent_wrk = vcl_worker_get (parent_wrk_index);
vls_worker_copy_on_fork (parent_wrk);
parent_wrk->forked_child = vcl_get_worker_index ();
/* Reset number of threads and set wrk index */
vlsl->vls_mt_n_threads = 0;
vlsl->vls_wrk_index = vcl_get_worker_index ();
vlsl->select_mp_check = 0;
vlsl->epoll_mp_check = 0;
vls_mt_locks_init ();
VDBG (0, "forked child main worker initialized");
vcm->forking = 0;
}
static void
vls_app_fork_parent_handler (void)
{
vcm->forking = 1;
while (vcm->forking)
;
}
void
vls_app_exit (void)
{
/* Unshare the sessions. VCL will clean up the worker */
vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
}
int
vls_app_create (char *app_name)
{
int rv;
if ((rv = vppcom_app_create (app_name)))
return rv;
vlsm = clib_mem_alloc (sizeof (vls_main_t));
clib_memset (vlsm, 0, sizeof (*vlsm));
clib_rwlock_init (&vlsm->vls_table_lock);
pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
vls_app_fork_child_handler);
atexit (vls_app_exit);
vlsl->vls_wrk_index = vcl_get_worker_index ();
vls_mt_locks_init ();
return VPPCOM_OK;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|