vlib: improvement to automatic core pinning
[vpp.git] / src / vcl / vcl_locked.c
1 /*
2  * Copyright (c) 2021 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /**
17  * VCL Locked Sessions (VLS) is a wrapper that synchronizes access to VCL APIs
18  * which are, by construction, not thread safe. To this end, VLS uses
19  * configuration and heuristics to detect how applications use sessions in
20  * an attempt to optimize the locking strategy. The modes of operation
21  * currently supported are the following:
22  *
23  *                    1) per-process workers
24  *
25  *                  +----------+   +----------+
26  *                  |          |   |          |
27  *                  | process0 |   | process1 |
28  *                  |          |   |          |
29  *                  +-----+----+   +-----+----+
30  *                        |              |
31  *                        |              |
32  *                  +-----+----+   +-----+----+
33  *                  |          |   |          |
34  *                  | vls_wrk0 |   | vls_wrk1 |
35  *                  |          |   |          |
36  *                  +-----+----+   +-----+----+
37  *                        |              |
38  *                        |              |
39  *                  +-----+----+   +-----+----+
40  *                  |          |   |          |
41  *                  | vcl_wrk0 |   | vcl_wrk1 |
42  *                  |          |   |          |
43  *                  +----------+   +----------+
44  *
45  *     2) per-thread workers         3) single-worker multi-thread
46  *
47  *   +---------+   +---------+         +---------+   +---------+
48  *   |         |   |         |         |         |   |         |
49  *   | thread0 |   | thread1 |         | thread0 |   | thread1 |
50  *   |         |   |         |         |         |   |         |
51  *   +--------++   +-+-------+         +--------++   +-+-------+
52  *            |      |                          |      |
53  *            |      |                          |      |
54  *          +-+------+-+                      +-+------+-+
55  *          |          |                      |          |
56  *          | vls_wrk0 |                      | vls_wrk0 |
57  *          |          |                      |          |
58  *          +-+------+-+                      +----+-----+
59  *            |      |                             |
60  *            |      |                             |
61  *   +--------+-+  +-+--------+               +----+-----+
62  *   |          |  |          |               |          |
63  *   | vcl_wrk0 |  | vcl_wrk1 |               | vcl_wrk0 |
64  *   |          |  |          |               |          |
65  *   +----------+  +----------+               +----------+
66  *
67  * 1) per-process workers: intercept fork calls and assume all children
68  *    processes are new workers that must be registered with vcl. VLS
69  *    sessions are cloned and shared between workers. Only shared sessions
70  *    are locked on use and thereby only one process can interact with
71  *    them at a time (explicit sharing).
72  *
73  * 2) per-thread workers: each newly detected pthread is assumed to be a new
74  *    worker and is registered with vcl. Enabled via configuration.
75  *    When a thread tries to access a session it does not own, a clone and
76  *    share rpc request is sent to the owning thread via vcl and vpp.
77  *    Consequently, a vls session can map to multiple vcl sessions, one per
78  *    vcl worker. VLS sessions are locked on use (implicit sharing).
79  *
80  * 3) single-worker multi-thread: vls does not make any assumptions about
81  *    application threads and therefore implements an aggressive locking
82  *    strategy that limits access to underlying vcl resources based on type
83  *    of interaction and locks vls session on use (implicit sharing).
84  */
85
86 #include <vcl/vcl_locked.h>
87 #include <vcl/vcl_private.h>
88
89 typedef struct vls_shared_data_
90 {
91   clib_spinlock_t lock;     /**< shared data lock */
92   u32 owner_wrk_index;      /**< vcl wrk that owns session */
93   u32 *workers_subscribed;  /**< vec of wrks subscribed to session */
94   clib_bitmap_t *listeners; /**< bitmap of wrks actively listening */
95 } vls_shared_data_t;
96
97 typedef struct vcl_locked_session_
98 {
99   clib_spinlock_t lock;    /**< vls lock when in use */
100   u32 session_index;       /**< vcl session index */
101   u32 vcl_wrk_index;       /**< vcl worker index */
102   u32 vls_index;           /**< index in vls pool */
103   u32 shared_data_index;   /**< shared data index if any */
104   u32 owner_vcl_wrk_index; /**< vcl wrk of the vls wrk at alloc */
105   uword *vcl_wrk_index_to_session_index; /**< map vcl wrk to session */
106 } vcl_locked_session_t;
107
108 typedef struct vls_worker_
109 {
110   clib_rwlock_t sh_to_vlsh_table_lock; /**< ht rwlock with mt workers */
111   vcl_locked_session_t *vls_pool;      /**< pool of vls session */
112   uword *sh_to_vlsh_table;             /**< map from vcl sh to vls sh */
113   u32 *pending_vcl_wrk_cleanup;        /**< child vcl wrks to cleanup */
114   u32 vcl_wrk_index;                   /**< if 1:1 map vls to vcl wrk */
115 } vls_worker_t;
116
117 typedef struct vls_local_
118 {
119   int vls_wrk_index;                  /**< vls wrk index, 1 per process */
120   volatile int vls_mt_n_threads;      /**< number of threads detected */
121   clib_rwlock_t vls_pool_lock;        /**< per process/wrk vls pool locks */
122   pthread_mutex_t vls_mt_mq_mlock;    /**< vcl mq lock */
123   pthread_mutex_t vls_mt_spool_mlock; /**< vcl select or pool lock */
124   volatile u8 select_mp_check;        /**< flag set if select checks done */
125 } vls_process_local_t;
126
127 static vls_process_local_t vls_local;
128 static vls_process_local_t *vlsl = &vls_local;
129
130 typedef struct vls_main_
131 {
132   vls_worker_t *workers;               /**< pool of vls workers */
133   vls_shared_data_t *shared_data_pool; /**< inter proc pool of shared data */
134   clib_rwlock_t shared_data_lock;      /**< shared data pool lock */
135   clib_spinlock_t worker_rpc_lock;     /**< lock for inter-worker rpcs */
136 } vls_main_t;
137
138 vls_main_t *vlsm;
139
140 typedef enum
141 {
142   VLS_RPC_STATE_INIT,
143   VLS_RPC_STATE_SUCCESS,
144   VLS_RPC_STATE_SESSION_NOT_EXIST,
145 } vls_rpc_state_e;
146
147 typedef enum vls_rpc_msg_type_
148 {
149   VLS_RPC_CLONE_AND_SHARE,
150   VLS_RPC_SESS_CLEANUP,
151 } vls_rpc_msg_type_e;
152
153 typedef struct vls_rpc_msg_
154 {
155   u8 type;
156   u8 data[0];
157 } vls_rpc_msg_t;
158
159 typedef struct vls_clone_and_share_msg_
160 {
161   u32 vls_index;                /**< vls to be shared */
162   u32 session_index;            /**< vcl session to be shared */
163   u32 origin_vls_wrk;           /**< vls worker that initiated the rpc */
164   u32 origin_vls_index;         /**< vls session of the originator */
165   u32 origin_vcl_wrk;           /**< vcl worker that initiated the rpc */
166   u32 origin_session_index;     /**< vcl session of the originator */
167 } vls_clone_and_share_msg_t;
168
169 typedef struct vls_sess_cleanup_msg_
170 {
171   u32 session_index;            /**< vcl session to be cleaned */
172   u32 origin_vcl_wrk;           /**< worker that initiated the rpc */
173 } vls_sess_cleanup_msg_t;
174
175 void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
176                                    u32 dst_wrk_index, u32 dst_session_index);
177 void vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
178                                    u32 session_index, u32 vls_wrk_index,
179                                    u32 dst_wrk_index, u32 dst_vls_index,
180                                    u32 dst_session_index);
181 static void vls_cleanup_forked_child (vcl_worker_t *wrk,
182                                       vcl_worker_t *child_wrk);
183 static void vls_handle_pending_wrk_cleanup (void);
184
185 static inline u32
186 vls_get_worker_index (void)
187 {
188   return vlsl->vls_wrk_index;
189 }
190
191 static u32
192 vls_shared_data_alloc (void)
193 {
194   vls_shared_data_t *vls_shd;
195   u32 shd_index;
196
197   clib_rwlock_writer_lock (&vlsm->shared_data_lock);
198   pool_get_zero (vlsm->shared_data_pool, vls_shd);
199   clib_spinlock_init (&vls_shd->lock);
200   shd_index = vls_shd - vlsm->shared_data_pool;
201   clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
202
203   return shd_index;
204 }
205
206 static u32
207 vls_shared_data_index (vls_shared_data_t * vls_shd)
208 {
209   return vls_shd - vlsm->shared_data_pool;
210 }
211
212 vls_shared_data_t *
213 vls_shared_data_get (u32 shd_index)
214 {
215   if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
216     return 0;
217   return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
218 }
219
220 static void
221 vls_shared_data_free (u32 shd_index)
222 {
223   vls_shared_data_t *vls_shd;
224
225   clib_rwlock_writer_lock (&vlsm->shared_data_lock);
226   vls_shd = vls_shared_data_get (shd_index);
227   clib_spinlock_free (&vls_shd->lock);
228   clib_bitmap_free (vls_shd->listeners);
229   vec_free (vls_shd->workers_subscribed);
230   pool_put (vlsm->shared_data_pool, vls_shd);
231   clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
232 }
233
234 static inline void
235 vls_shared_data_pool_rlock (void)
236 {
237   clib_rwlock_reader_lock (&vlsm->shared_data_lock);
238 }
239
240 static inline void
241 vls_shared_data_pool_runlock (void)
242 {
243   clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
244 }
245
246 static inline void
247 vls_mt_pool_rlock (void)
248 {
249   if (vlsl->vls_mt_n_threads > 1)
250     clib_rwlock_reader_lock (&vlsl->vls_pool_lock);
251 }
252
253 static inline void
254 vls_mt_pool_runlock (void)
255 {
256   if (vlsl->vls_mt_n_threads > 1)
257     clib_rwlock_reader_unlock (&vlsl->vls_pool_lock);
258 }
259
260 static inline void
261 vls_mt_pool_wlock (void)
262 {
263   if (vlsl->vls_mt_n_threads > 1)
264     clib_rwlock_writer_lock (&vlsl->vls_pool_lock);
265 }
266
267 static inline void
268 vls_mt_pool_wunlock (void)
269 {
270   if (vlsl->vls_mt_n_threads > 1)
271     clib_rwlock_writer_unlock (&vlsl->vls_pool_lock);
272 }
273
274 typedef enum
275 {
276   VLS_MT_OP_READ,
277   VLS_MT_OP_WRITE,
278   VLS_MT_OP_SPOOL,
279   VLS_MT_OP_XPOLL,
280 } vls_mt_ops_t;
281
282 typedef enum
283 {
284   VLS_MT_LOCK_MQ = 1 << 0,
285   VLS_MT_LOCK_SPOOL = 1 << 1
286 } vls_mt_lock_type_t;
287
288 static void
289 vls_mt_add (void)
290 {
291   vlsl->vls_mt_n_threads += 1;
292
293   /* If multi-thread workers are supported, for each new thread register a new
294    * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
295    * update the vcl worker's thread local worker index variable */
296   if (vls_mt_wrk_supported ())
297     {
298       if (vppcom_worker_register () != VPPCOM_OK)
299         VERR ("failed to register worker");
300     }
301   else
302     vcl_set_worker_index (vlsl->vls_wrk_index);
303 }
304
305 static inline void
306 vls_mt_mq_lock (void)
307 {
308   pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
309 }
310
311 static inline void
312 vls_mt_mq_unlock (void)
313 {
314   pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
315 }
316
317 static inline void
318 vls_mt_spool_lock (void)
319 {
320   pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
321 }
322
323 static inline void
324 vls_mt_create_unlock (void)
325 {
326   pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
327 }
328
329 static void
330 vls_mt_locks_init (void)
331 {
332   pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
333   pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
334 }
335
336 u8
337 vls_is_shared (vcl_locked_session_t * vls)
338 {
339   return (vls->shared_data_index != ~0);
340 }
341
342 static inline void
343 vls_lock (vcl_locked_session_t * vls)
344 {
345   if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
346     clib_spinlock_lock (&vls->lock);
347 }
348
349 static inline void
350 vls_unlock (vcl_locked_session_t * vls)
351 {
352   if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
353     clib_spinlock_unlock (&vls->lock);
354 }
355
356 static inline vcl_session_handle_t
357 vls_to_sh (vcl_locked_session_t * vls)
358 {
359   return vcl_session_handle_from_index (vls->session_index);
360 }
361
362 static inline vcl_session_handle_t
363 vls_to_sh_tu (vcl_locked_session_t * vls)
364 {
365   vcl_session_handle_t sh;
366   sh = vls_to_sh (vls);
367   vls_mt_pool_runlock ();
368   return sh;
369 }
370
371 static vls_worker_t *
372 vls_worker_get_current (void)
373 {
374   return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
375 }
376
377 static void
378 vls_worker_alloc (void)
379 {
380   vls_worker_t *wrk;
381
382   pool_get_zero (vlsm->workers, wrk);
383   if (vls_mt_wrk_supported ())
384     clib_rwlock_init (&wrk->sh_to_vlsh_table_lock);
385   wrk->vcl_wrk_index = vcl_get_worker_index ();
386   vec_validate (wrk->pending_vcl_wrk_cleanup, 16);
387   vec_reset_length (wrk->pending_vcl_wrk_cleanup);
388 }
389
390 static void
391 vls_worker_free (vls_worker_t * wrk)
392 {
393   hash_free (wrk->sh_to_vlsh_table);
394   if (vls_mt_wrk_supported ())
395     clib_rwlock_free (&wrk->sh_to_vlsh_table_lock);
396   pool_free (wrk->vls_pool);
397   pool_put (vlsm->workers, wrk);
398 }
399
400 static vls_worker_t *
401 vls_worker_get (u32 wrk_index)
402 {
403   if (pool_is_free_index (vlsm->workers, wrk_index))
404     return 0;
405   return pool_elt_at_index (vlsm->workers, wrk_index);
406 }
407
408 static void
409 vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
410 {
411   if (vls_mt_wrk_supported ())
412     clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
413   hash_set (wrk->sh_to_vlsh_table, sh, vlsh);
414   if (vls_mt_wrk_supported ())
415     clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
416 }
417
418 static void
419 vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
420 {
421   if (vls_mt_wrk_supported ())
422     clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
423   hash_unset (wrk->sh_to_vlsh_table, sh);
424   if (vls_mt_wrk_supported ())
425     clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
426 }
427
428 static uword *
429 vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
430 {
431   if (vls_mt_wrk_supported ())
432     clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock);
433   uword *vlshp = hash_get (wrk->sh_to_vlsh_table, sh);
434   if (vls_mt_wrk_supported ())
435     clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock);
436   return vlshp;
437 }
438
439 static vls_handle_t
440 vls_alloc (vcl_session_handle_t sh)
441 {
442   vls_worker_t *wrk = vls_worker_get_current ();
443   vcl_locked_session_t *vls;
444
445   vls_mt_pool_wlock ();
446
447   pool_get_zero (wrk->vls_pool, vls);
448   vls->session_index = vppcom_session_index (sh);
449   vls->vcl_wrk_index = vppcom_session_worker (sh);
450   vls->vls_index = vls - wrk->vls_pool;
451   vls->shared_data_index = ~0;
452   vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index);
453   if (vls_mt_wrk_supported ())
454     {
455       hash_set (vls->vcl_wrk_index_to_session_index, vls->vcl_wrk_index,
456                 vls->session_index);
457       vls->owner_vcl_wrk_index = vls->vcl_wrk_index;
458     }
459   clib_spinlock_init (&vls->lock);
460
461   vls_mt_pool_wunlock ();
462   return vls->vls_index;
463 }
464
465 static vcl_locked_session_t *
466 vls_get (vls_handle_t vlsh)
467 {
468   vls_worker_t *wrk = vls_worker_get_current ();
469   if (pool_is_free_index (wrk->vls_pool, vlsh))
470     return 0;
471   return pool_elt_at_index (wrk->vls_pool, vlsh);
472 }
473
474 static void
475 vls_free (vcl_locked_session_t * vls)
476 {
477   vls_worker_t *wrk = vls_worker_get_current ();
478
479   ASSERT (vls != 0);
480   vls_sh_to_vlsh_table_del (
481     wrk, vcl_session_handle_from_index (vls->session_index));
482   clib_spinlock_free (&vls->lock);
483   pool_put (wrk->vls_pool, vls);
484 }
485
486 static vcl_locked_session_t *
487 vls_get_and_lock (vls_handle_t vlsh)
488 {
489   vls_worker_t *wrk = vls_worker_get_current ();
490   vcl_locked_session_t *vls;
491   if (pool_is_free_index (wrk->vls_pool, vlsh))
492     return 0;
493   vls = pool_elt_at_index (wrk->vls_pool, vlsh);
494   vls_lock (vls);
495   return vls;
496 }
497
498 static vcl_locked_session_t *
499 vls_get_w_dlock (vls_handle_t vlsh)
500 {
501   vcl_locked_session_t *vls;
502   vls_mt_pool_rlock ();
503   vls = vls_get_and_lock (vlsh);
504   if (!vls)
505     vls_mt_pool_runlock ();
506   return vls;
507 }
508
509 static inline void
510 vls_get_and_unlock (vls_handle_t vlsh)
511 {
512   vcl_locked_session_t *vls;
513   vls_mt_pool_rlock ();
514   vls = vls_get (vlsh);
515   vls_unlock (vls);
516   vls_mt_pool_runlock ();
517 }
518
519 static inline void
520 vls_dunlock (vcl_locked_session_t * vls)
521 {
522   vls_unlock (vls);
523   vls_mt_pool_runlock ();
524 }
525
526 static vcl_locked_session_t *
527 vls_session_get (vls_worker_t * wrk, u32 vls_index)
528 {
529   if (pool_is_free_index (wrk->vls_pool, vls_index))
530     return 0;
531   return pool_elt_at_index (wrk->vls_pool, vls_index);
532 }
533
534 vcl_session_handle_t
535 vlsh_to_sh (vls_handle_t vlsh)
536 {
537   vcl_locked_session_t *vls;
538   int rv;
539
540   vls = vls_get_w_dlock (vlsh);
541   if (!vls)
542     return INVALID_SESSION_ID;
543   rv = vls_to_sh (vls);
544   vls_dunlock (vls);
545   return rv;
546 }
547
548 vcl_session_handle_t
549 vlsh_to_session_index (vls_handle_t vlsh)
550 {
551   vcl_session_handle_t sh;
552   sh = vlsh_to_sh (vlsh);
553   return vppcom_session_index (sh);
554 }
555
556 vls_handle_t
557 vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
558 {
559   vls_worker_t *wrk = vls_worker_get_current ();
560   uword *vlshp = vls_sh_to_vlsh_table_get (
561     wrk,
562     vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
563
564   return vlshp ? *vlshp : VLS_INVALID_HANDLE;
565 }
566
567 vls_handle_t
568 vls_session_index_to_vlsh (uint32_t session_index)
569 {
570   vls_handle_t vlsh;
571
572   vls_mt_pool_rlock ();
573   vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
574   vls_mt_pool_runlock ();
575
576   return vlsh;
577 }
578
579 u8
580 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
581 {
582   vls_shared_data_t *vls_shd;
583   int i;
584
585   if (vls->shared_data_index == ~0)
586     return 0;
587
588   vls_shared_data_pool_rlock ();
589
590   vls_shd = vls_shared_data_get (vls->shared_data_index);
591   clib_spinlock_lock (&vls_shd->lock);
592
593   for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
594     if (vls_shd->workers_subscribed[i] == wrk_index)
595       {
596         clib_spinlock_unlock (&vls_shd->lock);
597         vls_shared_data_pool_runlock ();
598         return 1;
599       }
600   clib_spinlock_unlock (&vls_shd->lock);
601
602   vls_shared_data_pool_runlock ();
603   return 0;
604 }
605
606 static void
607 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
608 {
609   vls_shared_data_t *vls_shd;
610
611   if (vls->shared_data_index == ~0)
612     {
613       clib_warning ("not a shared session");
614       return;
615     }
616
617   vls_shared_data_pool_rlock ();
618
619   vls_shd = vls_shared_data_get (vls->shared_data_index);
620
621   clib_spinlock_lock (&vls_shd->lock);
622   vls_shd->listeners =
623     clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
624   clib_spinlock_unlock (&vls_shd->lock);
625
626   vls_shared_data_pool_runlock ();
627 }
628
629 static u32
630 vls_shared_get_owner (vcl_locked_session_t * vls)
631 {
632   vls_shared_data_t *vls_shd;
633   u32 owner_wrk;
634
635   vls_shared_data_pool_rlock ();
636
637   vls_shd = vls_shared_data_get (vls->shared_data_index);
638   owner_wrk = vls_shd->owner_wrk_index;
639
640   vls_shared_data_pool_runlock ();
641
642   return owner_wrk;
643 }
644
645 static u8
646 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
647 {
648   vls_shared_data_t *vls_shd;
649   u8 is_set;
650
651   if (vls->shared_data_index == ~0)
652     {
653       clib_warning ("not a shared session");
654       return 0;
655     }
656
657   vls_shared_data_pool_rlock ();
658
659   vls_shd = vls_shared_data_get (vls->shared_data_index);
660
661   clib_spinlock_lock (&vls_shd->lock);
662   is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
663   clib_spinlock_unlock (&vls_shd->lock);
664
665   vls_shared_data_pool_runlock ();
666
667   return (is_set == 1);
668 }
669
670 static void
671 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
672 {
673   vcl_worker_t *wrk;
674   vcl_session_t *ls;
675
676   wrk = vcl_worker_get (wrk_index);
677   ls = vcl_session_get (wrk, vls->session_index);
678
679   /* Listen request already sent */
680   if (ls->flags & VCL_SESSION_F_PENDING_LISTEN)
681     return;
682
683   vcl_send_session_listen (wrk, ls);
684
685   vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */);
686 }
687
688 static void
689 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
690 {
691   vcl_worker_t *wrk;
692   vcl_session_t *s;
693
694   wrk = vcl_worker_get (wrk_index);
695   s = vcl_session_get (wrk, vls->session_index);
696   if (s->session_state != VCL_STATE_LISTEN)
697     return;
698   vcl_send_session_unlisten (wrk, s);
699   s->session_state = VCL_STATE_LISTEN_NO_MQ;
700   vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
701 }
702
703 static int
704 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
705                                      u32 wrk_index)
706 {
707   int i;
708
709   for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
710     {
711       if (vls_shd->workers_subscribed[i] == wrk_index)
712         return i;
713     }
714   return -1;
715 }
716
717 int
718 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
719 {
720   vls_shared_data_t *vls_shd;
721   int do_disconnect, pos;
722   u32 n_subscribers;
723   vcl_session_t *s;
724
725   if (vls->shared_data_index == ~0)
726     return 0;
727
728   s = vcl_session_get (wrk, vls->session_index);
729   if (s->session_state == VCL_STATE_LISTEN)
730     vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
731
732   vls_shared_data_pool_rlock ();
733
734   vls_shd = vls_shared_data_get (vls->shared_data_index);
735   clib_spinlock_lock (&vls_shd->lock);
736
737   pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
738   if (pos < 0)
739     {
740       clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
741                     vls->vcl_wrk_index);
742       goto done;
743     }
744
745   /*
746    * Unsubscribe from share data and fifos
747    */
748   if (s->rx_fifo)
749     {
750       svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
751       svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
752     }
753   vec_del1 (vls_shd->workers_subscribed, pos);
754
755   /*
756    * Cleanup vcl state
757    */
758   n_subscribers = vec_len (vls_shd->workers_subscribed);
759   do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
760   vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
761
762   /*
763    * No subscriber left, cleanup shared data
764    */
765   if (!n_subscribers)
766     {
767       u32 shd_index = vls_shared_data_index (vls_shd);
768
769       clib_spinlock_unlock (&vls_shd->lock);
770       vls_shared_data_pool_runlock ();
771
772       vls_shared_data_free (shd_index);
773
774       /* All locks have been dropped */
775       return 0;
776     }
777
778   /* Return, if this is not the owning worker */
779   if (vls_shd->owner_wrk_index != wrk->wrk_index)
780     goto done;
781
782   ASSERT (vec_len (vls_shd->workers_subscribed));
783
784   /*
785    *  Check if we can change owner or close
786    */
787   vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
788   if (s->vpp_evt_q)
789     vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
790
791   /* XXX is this still needed? */
792   if (vec_len (vls_shd->workers_subscribed) > 1)
793     clib_warning ("more workers need to be updated");
794
795 done:
796
797   clib_spinlock_unlock (&vls_shd->lock);
798   vls_shared_data_pool_runlock ();
799
800   return 0;
801 }
802
803 void
804 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
805 {
806   vls_shared_data_t *vls_shd;
807
808   u32 vls_shd_index = vls_shared_data_alloc ();
809
810   vls_shared_data_pool_rlock ();
811
812   vls_shd = vls_shared_data_get (vls_shd_index);
813   vls_shd->owner_wrk_index = vls_wrk->vcl_wrk_index;
814   vls->shared_data_index = vls_shd_index;
815   vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
816
817   vls_shared_data_pool_runlock ();
818 }
819
820 void
821 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
822 {
823   vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->vcl_wrk_index);
824   vls_shared_data_t *vls_shd;
825   vcl_session_t *s;
826
827   s = vcl_session_get (vcl_wrk, vls->session_index);
828   if (!s)
829     {
830       clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
831                     vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
832       return;
833     }
834
835   ASSERT (vls->shared_data_index != ~0);
836
837   /* Reinit session lock */
838   clib_spinlock_init (&vls->lock);
839
840   vls_shared_data_pool_rlock ();
841
842   vls_shd = vls_shared_data_get (vls->shared_data_index);
843
844   clib_spinlock_lock (&vls_shd->lock);
845   vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
846   clib_spinlock_unlock (&vls_shd->lock);
847
848   vls_shared_data_pool_runlock ();
849
850   if (s->session_state == VCL_STATE_LISTEN)
851     {
852       s->session_state = VCL_STATE_LISTEN_NO_MQ;
853       s->rx_fifo = s->tx_fifo = 0;
854     }
855   else if (s->rx_fifo)
856     {
857       vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
858     }
859 }
860
861 static void
862 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
863 {
864   vcl_locked_session_t *vls, *parent_vls;
865
866   pool_foreach (vls, vls_wrk->vls_pool)  {
867     /* Initialize sharing on parent session */
868     if (vls->shared_data_index == ~0)
869       {
870         parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
871         vls_init_share_session (vls_parent_wrk, parent_vls);
872         vls->shared_data_index = parent_vls->shared_data_index;
873       }
874     vls_share_session (vls_wrk, vls);
875   }
876 }
877
878 static void
879 vls_validate_veps (vcl_worker_t *wrk)
880 {
881   vcl_session_t *s;
882   u32 session_index, wrk_index;
883
884   pool_foreach (s, wrk->sessions)
885     {
886       if (s->vep.vep_sh != ~0)
887         {
888           vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
889           s->vep.vep_sh = vcl_session_handle_from_index (session_index);
890         }
891       if (s->vep.next_sh != ~0)
892         {
893           vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
894                                     &session_index);
895           s->vep.next_sh = vcl_session_handle_from_index (session_index);
896         }
897       if (s->vep.prev_sh != ~0)
898         {
899           vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
900                                     &session_index);
901           s->vep.prev_sh = vcl_session_handle_from_index (session_index);
902         }
903     }
904 }
905
906 void
907 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
908 {
909   vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
910   vcl_worker_t *vcl_wrk = vcl_worker_get_current ();
911   u32 vls_index, session_index, wrk_index;
912   vcl_session_handle_t sh;
913   vcl_locked_session_t *vls;
914
915   /*
916    * init vcl worker
917    */
918   vcl_wrk->sessions = pool_dup (parent_wrk->sessions);
919   vcl_wrk->session_index_by_vpp_handles =
920     hash_dup (parent_wrk->session_index_by_vpp_handles);
921
922   /*
923    * init vls worker
924    */
925   vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
926
927   /* clang-format off */
928   hash_foreach (sh, vls_index, vls_parent_wrk->sh_to_vlsh_table, ({
929     vcl_session_handle_parse (sh, &wrk_index, &session_index);
930     hash_set (vls_wrk->sh_to_vlsh_table,
931               vcl_session_handle_from_index (session_index), vls_index);
932   }));
933   /* clang-format on */
934   vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
935
936   /*
937    * Detach vls from parent vcl worker and attach them to child.
938    */
939   pool_foreach (vls, vls_wrk->vls_pool)
940     {
941       vls->vcl_wrk_index = vcl_wrk->wrk_index;
942     }
943
944   /* Validate vep's handle */
945   vls_validate_veps (vcl_wrk);
946
947   vls_share_sessions (vls_parent_wrk, vls_wrk);
948 }
949
950 static void
951 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
952 {
953   vcl_worker_t *wrk = vcl_worker_get_current ();
954   vcl_session_t *s = 0;
955   int is_nonblk = 0;
956
957   if (vls)
958     {
959       s = vcl_session_get (wrk, vls->session_index);
960       if (PREDICT_FALSE (!s))
961         return;
962       is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
963     }
964
965   switch (op)
966     {
967     case VLS_MT_OP_READ:
968       if (!is_nonblk)
969         is_nonblk = vcl_session_read_ready (s) != 0;
970       if (!is_nonblk)
971         {
972           vls_mt_mq_lock ();
973           *locks_acq |= VLS_MT_LOCK_MQ;
974         }
975       break;
976     case VLS_MT_OP_WRITE:
977       ASSERT (s);
978       if (!is_nonblk)
979         is_nonblk = vcl_session_write_ready (s) != 0;
980       if (!is_nonblk)
981         {
982           vls_mt_mq_lock ();
983           *locks_acq |= VLS_MT_LOCK_MQ;
984         }
985       break;
986     case VLS_MT_OP_XPOLL:
987       vls_mt_mq_lock ();
988       *locks_acq |= VLS_MT_LOCK_MQ;
989       break;
990     case VLS_MT_OP_SPOOL:
991       vls_mt_spool_lock ();
992       *locks_acq |= VLS_MT_LOCK_SPOOL;
993       break;
994     default:
995       break;
996     }
997 }
998
999 static void
1000 vls_mt_rel_locks (int locks_acq)
1001 {
1002   if (locks_acq & VLS_MT_LOCK_MQ)
1003     vls_mt_mq_unlock ();
1004   if (locks_acq & VLS_MT_LOCK_SPOOL)
1005     vls_mt_create_unlock ();
1006 }
1007
1008 static inline u8
1009 vls_mt_session_should_migrate (vcl_locked_session_t * vls)
1010 {
1011   return (vls_mt_wrk_supported () &&
1012           vls->vcl_wrk_index != vcl_get_worker_index ());
1013 }
1014
1015 static vcl_locked_session_t *
1016 vls_mt_session_migrate (vcl_locked_session_t *vls)
1017 {
1018   u32 wrk_index = vcl_get_worker_index ();
1019   vcl_worker_t *wrk;
1020   vls_worker_t *vls_wrk = vls_worker_get_current ();
1021   u32 src_sid, sid, vls_index, own_vcl_wrk_index;
1022   vcl_session_t *session;
1023   uword *p;
1024
1025   ASSERT (vls_mt_wrk_supported () && vls->vcl_wrk_index != wrk_index);
1026
1027   /*
1028    * VCL session on current vcl worker already allocated. Update current
1029    * owner worker and index and return
1030    */
1031   if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
1032     {
1033       vls->vcl_wrk_index = wrk_index;
1034       vls->session_index = (u32) p[0];
1035       return vls;
1036     }
1037
1038   /*
1039    * Ask vcl worker that owns the original vcl session to clone it into
1040    * current vcl worker session pool
1041    */
1042
1043   if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
1044                       vls->owner_vcl_wrk_index)))
1045     {
1046       VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
1047       ASSERT (0);
1048       vls_unlock (vls);
1049       vls_mt_pool_runlock ();
1050       return 0;
1051     }
1052
1053   src_sid = (u32) p[0];
1054   wrk = vcl_worker_get_current ();
1055   session = vcl_session_alloc (wrk);
1056   sid = session->session_index;
1057   VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
1058         vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
1059
1060   /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
1061   vls_index = vls->vls_index;
1062   own_vcl_wrk_index = vls->owner_vcl_wrk_index;
1063   vls_unlock (vls);
1064   vls_mt_pool_runlock ();
1065   vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
1066                                 own_vcl_wrk_index, vls_index, src_sid);
1067
1068   if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
1069     {
1070       VWRN ("session %u not exist", src_sid);
1071       goto err;
1072     }
1073   else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
1074     {
1075       VWRN ("failed to wait rpc response");
1076       goto err;
1077     }
1078   else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
1079                           session->vep.next_sh != ~0))
1080     {
1081       VERR ("can't migrate nonempty epoll session");
1082       ASSERT (0);
1083       goto err;
1084     }
1085   else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
1086                           session->session_state != VCL_STATE_CLOSED))
1087     {
1088       VERR ("migrate NOT supported, session_status (%u)",
1089             session->session_state);
1090       ASSERT (0);
1091       goto err;
1092     }
1093
1094   vls = vls_get_w_dlock (vls_index);
1095   if (PREDICT_FALSE (!vls))
1096     {
1097       VWRN ("failed to get vls %u", vls_index);
1098       goto err;
1099     }
1100
1101   session->session_index = sid;
1102   vls->vcl_wrk_index = wrk_index;
1103   vls->session_index = sid;
1104   hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
1105   vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1106                             vls->vls_index);
1107   return vls;
1108
1109 err:
1110   vcl_session_free (wrk, session);
1111   return 0;
1112 }
1113
1114 static inline void
1115 vls_mt_detect (void)
1116 {
1117   if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1118     vls_mt_add ();
1119 }
1120
1121 #define vls_mt_guard(_vls, _op)                                               \
1122   int _locks_acq = 0;                                                         \
1123   if (vls_mt_wrk_supported ())                                                \
1124     {                                                                         \
1125       if (PREDICT_FALSE (_vls &&                                              \
1126                          ((vcl_locked_session_t *) _vls)->vcl_wrk_index !=    \
1127                            vcl_get_worker_index ()))                          \
1128         {                                                                     \
1129           _vls = vls_mt_session_migrate (_vls);                               \
1130           if (PREDICT_FALSE (!_vls))                                          \
1131             return VPPCOM_EBADFD;                                             \
1132         }                                                                     \
1133     }                                                                         \
1134   else                                                                        \
1135     {                                                                         \
1136       if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1))                         \
1137         vls_mt_acq_locks (_vls, _op, &_locks_acq);                            \
1138     }
1139
1140 #define vls_mt_unguard()                                                \
1141   if (PREDICT_FALSE (_locks_acq))                                       \
1142     vls_mt_rel_locks (_locks_acq)
1143
1144 int
1145 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1146 {
1147   vcl_locked_session_t *vls;
1148   int rv;
1149
1150   vls_mt_detect ();
1151   if (!(vls = vls_get_w_dlock (vlsh)))
1152     return VPPCOM_EBADFD;
1153
1154   vls_mt_guard (vls, VLS_MT_OP_WRITE);
1155   rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
1156   vls_mt_unguard ();
1157   vls_get_and_unlock (vlsh);
1158   return rv;
1159 }
1160
1161 int
1162 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1163 {
1164   vcl_locked_session_t *vls;
1165   int rv;
1166
1167   vls_mt_detect ();
1168   if (!(vls = vls_get_w_dlock (vlsh)))
1169     return VPPCOM_EBADFD;
1170   vls_mt_guard (vls, VLS_MT_OP_WRITE);
1171   rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
1172   vls_mt_unguard ();
1173   vls_get_and_unlock (vlsh);
1174   return rv;
1175 }
1176
1177 int
1178 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1179             vppcom_endpt_t * ep)
1180 {
1181   vcl_locked_session_t *vls;
1182   int rv;
1183
1184   vls_mt_detect ();
1185   if (!(vls = vls_get_w_dlock (vlsh)))
1186     return VPPCOM_EBADFD;
1187   vls_mt_guard (vls, VLS_MT_OP_WRITE);
1188   rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
1189   vls_mt_unguard ();
1190   vls_get_and_unlock (vlsh);
1191   return rv;
1192 }
1193
1194 ssize_t
1195 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1196 {
1197   vcl_locked_session_t *vls;
1198   int rv;
1199
1200   vls_mt_detect ();
1201   if (!(vls = vls_get_w_dlock (vlsh)))
1202     return VPPCOM_EBADFD;
1203   vls_mt_guard (vls, VLS_MT_OP_READ);
1204   rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
1205   vls_mt_unguard ();
1206   vls_get_and_unlock (vlsh);
1207   return rv;
1208 }
1209
1210 ssize_t
1211 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1212               vppcom_endpt_t * ep)
1213 {
1214   vcl_locked_session_t *vls;
1215   int rv;
1216
1217   vls_mt_detect ();
1218   if (!(vls = vls_get_w_dlock (vlsh)))
1219     return VPPCOM_EBADFD;
1220   vls_mt_guard (vls, VLS_MT_OP_READ);
1221   rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1222                                 ep);
1223   vls_mt_unguard ();
1224   vls_get_and_unlock (vlsh);
1225   return rv;
1226 }
1227
1228 int
1229 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1230 {
1231   vcl_locked_session_t *vls;
1232   int rv;
1233
1234   vls_mt_detect ();
1235   if (!(vls = vls_get_w_dlock (vlsh)))
1236     return VPPCOM_EBADFD;
1237   if (vls_mt_session_should_migrate (vls))
1238     {
1239       vls = vls_mt_session_migrate (vls);
1240       if (PREDICT_FALSE (!vls))
1241         return VPPCOM_EBADFD;
1242     }
1243   rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1244   vls_get_and_unlock (vlsh);
1245   return rv;
1246 }
1247
1248 int
1249 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1250 {
1251   vcl_locked_session_t *vls;
1252   int rv;
1253
1254   vls_mt_detect ();
1255   if (!(vls = vls_get_w_dlock (vlsh)))
1256     return VPPCOM_EBADFD;
1257   rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1258   vls_get_and_unlock (vlsh);
1259   return rv;
1260 }
1261
1262 int
1263 vls_listen (vls_handle_t vlsh, int q_len)
1264 {
1265   vcl_locked_session_t *vls;
1266   int rv;
1267
1268   vls_mt_detect ();
1269   if (!(vls = vls_get_w_dlock (vlsh)))
1270     return VPPCOM_EBADFD;
1271   vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1272   rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
1273   vls_mt_unguard ();
1274   vls_get_and_unlock (vlsh);
1275   return rv;
1276 }
1277
1278 int
1279 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1280 {
1281   vcl_locked_session_t *vls;
1282   int rv;
1283
1284   vls_mt_detect ();
1285   if (!(vls = vls_get_w_dlock (vlsh)))
1286     return VPPCOM_EBADFD;
1287   vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1288   rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
1289   vls_mt_unguard ();
1290   vls_get_and_unlock (vlsh);
1291   return rv;
1292 }
1293
1294 static inline void
1295 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1296 {
1297   vcl_worker_t *wrk = vcl_worker_get_current ();
1298   vcl_session_t *s;
1299   u32 owner_wrk;
1300
1301   if (vls_mt_wrk_supported ())
1302     return;
1303
1304   ASSERT (wrk->wrk_index == vls->vcl_wrk_index);
1305   s = vcl_session_get (wrk, vls->session_index);
1306   switch (s->session_state)
1307     {
1308     case VCL_STATE_LISTEN:
1309       if (is_add)
1310         {
1311           vls_listener_wrk_set (vls, vls->vcl_wrk_index, 1 /* is_active */);
1312           break;
1313         }
1314       /* Although removal from epoll means listener no longer accepts new
1315        * sessions, the accept queue built by vpp cannot be drained by stopping
1316        * the listener. Morover, some applications, e.g., nginx, might
1317        * constantly remove and add listeners to their epfds. Removing
1318        * listeners in such situations causes a lot of churn in vpp as segments
1319        * and segment managers need to be recreated. */
1320       /* vls_listener_wrk_stop_listen (vls, vls->vcl_wrk_index); */
1321       break;
1322     case VCL_STATE_LISTEN_NO_MQ:
1323       if (!is_add)
1324         break;
1325
1326       /* Register worker as listener */
1327       vls_listener_wrk_start_listen (vls, vls->vcl_wrk_index);
1328
1329       /* If owner worker did not attempt to accept/xpoll on the session,
1330        * force a listen stop for it, since it may not be interested in
1331        * accepting new sessions.
1332        * This is pretty much a hack done to give app workers the illusion
1333        * that it is fine to listen and not accept new sessions for a
1334        * given listener. Without it, we would accumulate unhandled
1335        * accepts on the passive worker message queue. */
1336       owner_wrk = vls_shared_get_owner (vls);
1337       if (!vls_listener_wrk_is_active (vls, owner_wrk))
1338         vls_listener_wrk_stop_listen (vls, owner_wrk);
1339       break;
1340     default:
1341       break;
1342     }
1343 }
1344
1345 vls_handle_t
1346 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1347 {
1348   vls_handle_t accepted_vlsh;
1349   vcl_locked_session_t *vls;
1350   int sh;
1351
1352   vls_mt_detect ();
1353   if (!(vls = vls_get_w_dlock (listener_vlsh)))
1354     return VPPCOM_EBADFD;
1355   if (vcl_n_workers () > 1)
1356     vls_mp_checks (vls, 1 /* is_add */ );
1357   vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1358   sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
1359   vls_mt_unguard ();
1360   vls_get_and_unlock (listener_vlsh);
1361   if (sh < 0)
1362     return sh;
1363   accepted_vlsh = vls_alloc (sh);
1364   if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1365     vppcom_session_close (sh);
1366   return accepted_vlsh;
1367 }
1368
1369 vls_handle_t
1370 vls_create (uint8_t proto, uint8_t is_nonblocking)
1371 {
1372   vcl_session_handle_t sh;
1373   vls_handle_t vlsh;
1374   vcl_locked_session_t *vls = NULL;
1375
1376   vls_mt_detect ();
1377   vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1378   sh = vppcom_session_create (proto, is_nonblocking);
1379   vls_mt_unguard ();
1380   if (sh == INVALID_SESSION_ID)
1381     return VLS_INVALID_HANDLE;
1382
1383   vlsh = vls_alloc (sh);
1384   if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1385     vppcom_session_close (sh);
1386
1387   return vlsh;
1388 }
1389
1390 static void
1391 vls_mt_session_cleanup (vcl_locked_session_t * vls)
1392 {
1393   u32 session_index, wrk_index, current_vcl_wrk;
1394   vcl_worker_t *wrk = vcl_worker_get_current ();
1395
1396   ASSERT (vls_mt_wrk_supported ());
1397
1398   current_vcl_wrk = vcl_get_worker_index ();
1399
1400   hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1401     ({
1402       if (current_vcl_wrk != wrk_index)
1403         vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
1404     }));
1405   hash_free (vls->vcl_wrk_index_to_session_index);
1406 }
1407
1408 int
1409 vls_close (vls_handle_t vlsh)
1410 {
1411   vcl_locked_session_t *vls;
1412   int rv;
1413
1414   vls_mt_detect ();
1415   vls_mt_pool_wlock ();
1416
1417   vls = vls_get_and_lock (vlsh);
1418   if (!vls)
1419     {
1420       vls_mt_pool_wunlock ();
1421       return VPPCOM_EBADFD;
1422     }
1423
1424   vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1425
1426   if (vls_is_shared (vls))
1427     rv = vls_unshare_session (vls, vcl_worker_get_current ());
1428   else
1429     rv = vppcom_session_close (vls_to_sh (vls));
1430
1431   if (vls_mt_wrk_supported ())
1432     vls_mt_session_cleanup (vls);
1433
1434   vls_free (vls);
1435   vls_mt_unguard ();
1436
1437   vls_mt_pool_wunlock ();
1438
1439   return rv;
1440 }
1441
1442 int
1443 vls_shutdown (vls_handle_t vlsh, int how)
1444 {
1445   vcl_locked_session_t *vls;
1446   int rv;
1447
1448   vls_mt_detect ();
1449   if (!(vls = vls_get_w_dlock (vlsh)))
1450     return VPPCOM_EBADFD;
1451
1452   vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1453   rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
1454   vls_mt_unguard ();
1455   vls_get_and_unlock (vlsh);
1456
1457   return rv;
1458 }
1459
1460 vls_handle_t
1461 vls_epoll_create (void)
1462 {
1463   vcl_session_handle_t sh;
1464   vls_handle_t vlsh;
1465
1466   vls_mt_detect ();
1467
1468   sh = vppcom_epoll_create ();
1469   if (sh == INVALID_SESSION_ID)
1470     return VLS_INVALID_HANDLE;
1471
1472   vlsh = vls_alloc (sh);
1473   if (vlsh == VLS_INVALID_HANDLE)
1474     vppcom_session_close (sh);
1475
1476   return vlsh;
1477 }
1478
1479 static void
1480 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1481 {
1482   if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
1483     return;
1484
1485   vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1486 }
1487
1488 int
1489 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1490                struct epoll_event *event)
1491 {
1492   vcl_locked_session_t *ep_vls, *vls;
1493   vcl_session_handle_t ep_sh, sh;
1494   int rv;
1495
1496   vls_mt_detect ();
1497   vls_mt_pool_rlock ();
1498
1499   ep_vls = vls_get_and_lock (ep_vlsh);
1500   if (PREDICT_FALSE (!ep_vls))
1501     {
1502       vls_mt_pool_runlock ();
1503       return VPPCOM_EBADFD;
1504     }
1505
1506   if (vls_mt_session_should_migrate (ep_vls))
1507     {
1508       ep_vls = vls_mt_session_migrate (ep_vls);
1509       if (PREDICT_FALSE (!ep_vls))
1510         {
1511           vls_mt_pool_runlock ();
1512           return VPPCOM_EBADFD;
1513         }
1514     }
1515
1516   vls = vls_get_and_lock (vlsh);
1517   if (PREDICT_FALSE (!vls))
1518     {
1519       vls_unlock (ep_vls);
1520       vls_mt_pool_runlock ();
1521       return VPPCOM_EBADFD;
1522     }
1523
1524   ep_sh = vls_to_sh (ep_vls);
1525   sh = vls_to_sh (vls);
1526
1527   vls_epoll_ctl_mp_checks (vls, op);
1528   vls_mt_pool_runlock ();
1529   rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1530
1531   vls_mt_pool_rlock ();
1532   ep_vls = vls_get (ep_vlsh);
1533   vls = vls_get (vlsh);
1534   vls_unlock (vls);
1535   vls_unlock (ep_vls);
1536   vls_mt_pool_runlock ();
1537   return rv;
1538 }
1539
1540 int
1541 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1542                 int maxevents, double wait_for_time)
1543 {
1544   vcl_locked_session_t *vls, *vls_tmp = NULL;
1545   int rv;
1546
1547   vls_mt_detect ();
1548   if (!(vls = vls_get_w_dlock (ep_vlsh)))
1549     return VPPCOM_EBADFD;
1550   vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
1551   rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1552                           wait_for_time);
1553   vls_mt_unguard ();
1554   vls_get_and_unlock (ep_vlsh);
1555   vls_handle_pending_wrk_cleanup ();
1556   return rv;
1557 }
1558
1559 static void
1560 vls_select_mp_checks (vcl_si_set * read_map)
1561 {
1562   vcl_locked_session_t *vls;
1563   vcl_worker_t *wrk;
1564   vcl_session_t *s;
1565   u32 si;
1566
1567   if (vcl_n_workers () <= 1)
1568     {
1569       vlsl->select_mp_check = 1;
1570       return;
1571     }
1572
1573   if (!read_map)
1574     return;
1575
1576   vlsl->select_mp_check = 1;
1577   wrk = vcl_worker_get_current ();
1578
1579   clib_bitmap_foreach (si, read_map)  {
1580     s = vcl_session_get (wrk, si);
1581     if (s->session_state == VCL_STATE_LISTEN)
1582       {
1583         vls = vls_get (vls_session_index_to_vlsh (si));
1584         vls_mp_checks (vls, 1 /* is_add */);
1585       }
1586   }
1587 }
1588
1589 int
1590 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1591             vcl_si_set * except_map, double wait_for_time)
1592 {
1593   int rv;
1594   vcl_locked_session_t *vls = NULL;
1595
1596   vls_mt_detect ();
1597   vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1598   if (PREDICT_FALSE (!vlsl->select_mp_check))
1599     vls_select_mp_checks (read_map);
1600   rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1601   vls_mt_unguard ();
1602   vls_handle_pending_wrk_cleanup ();
1603   return rv;
1604 }
1605
1606 static void
1607 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1608 {
1609   u32 current_wrk, is_current;
1610   vcl_locked_session_t *vls;
1611   vcl_session_t *s;
1612
1613   if (pool_elts (vcm->workers) <= 1)
1614     return;
1615
1616   current_wrk = vcl_get_worker_index ();
1617   is_current = current_wrk == wrk->wrk_index;
1618
1619   pool_foreach (s, wrk->sessions)  {
1620     vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
1621     if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1622       vls_unshare_session (vls, wrk);
1623   }
1624 }
1625
1626 static void
1627 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1628 {
1629   vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1630
1631   /* Unshare sessions and also cleanup worker since child may have
1632    * called _exit () and therefore vcl may not catch the event */
1633   vls_unshare_vcl_worker_sessions (wrk);
1634
1635   /* Since child may have exited and thereforce fd of vpp_app_socket_api
1636    * may have been closed, so DONOT notify VPP.
1637    */
1638   vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
1639
1640   vls_worker_free (vls_wrk);
1641 }
1642
1643 static void
1644 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1645 {
1646   vcl_worker_t *sub_child;
1647   int tries = 0;
1648
1649   if (child_wrk->forked_child != ~0)
1650     {
1651       sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1652       if (sub_child)
1653         {
1654           /* Wait a bit, maybe the process is going away */
1655           while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1656             usleep (1e3);
1657           if (kill (sub_child->current_pid, 0) < 0)
1658             vls_cleanup_forked_child (child_wrk, sub_child);
1659         }
1660     }
1661   vls_cleanup_vcl_worker (child_wrk);
1662   VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1663   wrk->forked_child = ~0;
1664 }
1665
1666 static void
1667 vls_handle_pending_wrk_cleanup (void)
1668 {
1669   u32 *wip;
1670   vcl_worker_t *child_wrk, *wrk;
1671   vls_worker_t *vls_wrk = vls_worker_get_current ();
1672
1673   if (PREDICT_TRUE (vec_len (vls_wrk->pending_vcl_wrk_cleanup) == 0))
1674     return;
1675
1676   wrk = vcl_worker_get_current ();
1677   vec_foreach (wip, vls_wrk->pending_vcl_wrk_cleanup)
1678     {
1679       child_wrk = vcl_worker_get_if_valid (*wip);
1680       if (!child_wrk)
1681         continue;
1682       vls_cleanup_forked_child (wrk, child_wrk);
1683     }
1684   vec_reset_length (vls_wrk->pending_vcl_wrk_cleanup);
1685 }
1686
1687 static struct sigaction old_sa;
1688
1689 static void
1690 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1691 {
1692   vcl_worker_t *wrk, *child_wrk;
1693   vls_worker_t *vls_wrk;
1694
1695   if (vcl_get_worker_index () == ~0)
1696     return;
1697
1698   if (sigaction (SIGCHLD, &old_sa, 0))
1699     {
1700       VERR ("couldn't restore sigchld");
1701       exit (-1);
1702     }
1703
1704   wrk = vcl_worker_get_current ();
1705   if (wrk->forked_child == ~0)
1706     return;
1707
1708   child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1709   if (!child_wrk)
1710     goto done;
1711
1712   if (si && si->si_pid != child_wrk->current_pid)
1713     {
1714       VDBG (0, "unexpected child pid %u", si->si_pid);
1715       goto done;
1716     }
1717
1718   /* Parent process may enter sighandler with a lock, such as lock in localtime
1719    * or in mspace_free, and child wrk cleanup may try to get such locks and
1720    * cause deadlock.
1721    * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1722    */
1723   vls_wrk = vls_worker_get_current ();
1724   vec_add1 (vls_wrk->pending_vcl_wrk_cleanup, child_wrk->wrk_index);
1725
1726 done:
1727   if (old_sa.sa_flags & SA_SIGINFO)
1728     {
1729       void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1730       fn (signum, si, uc);
1731     }
1732   else
1733     {
1734       void (*fn) (int) = old_sa.sa_handler;
1735       if (fn)
1736         fn (signum);
1737     }
1738 }
1739
1740 static void
1741 vls_incercept_sigchld ()
1742 {
1743   struct sigaction sa;
1744   if (old_sa.sa_sigaction)
1745     {
1746       VDBG (0, "have intercepted sigchld");
1747       return;
1748     }
1749   clib_memset (&sa, 0, sizeof (sa));
1750   sa.sa_sigaction = vls_intercept_sigchld_handler;
1751   sa.sa_flags = SA_SIGINFO;
1752   if (sigaction (SIGCHLD, &sa, &old_sa))
1753     {
1754       VERR ("couldn't intercept sigchld");
1755       exit (-1);
1756     }
1757 }
1758
1759 static void
1760 vls_app_pre_fork (void)
1761 {
1762   vls_incercept_sigchld ();
1763   vcl_flush_mq_events ();
1764 }
1765
1766 static void
1767 vls_app_fork_child_handler (void)
1768 {
1769   vcl_worker_t *parent_wrk;
1770   int parent_wrk_index;
1771
1772   parent_wrk_index = vcl_get_worker_index ();
1773   VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1774         parent_wrk_index);
1775
1776   /*
1777    * Clear old state
1778    */
1779   vcl_set_worker_index (~0);
1780
1781   /*
1782    * Allocate and register vcl worker with vpp
1783    */
1784   if (vppcom_worker_register ())
1785     {
1786       VERR ("couldn't register new worker!");
1787       return;
1788     }
1789
1790   /*
1791    * Allocate/initialize vls worker and share sessions
1792    */
1793   vls_worker_alloc ();
1794
1795   /* Reset number of threads and set wrk index */
1796   vlsl->vls_mt_n_threads = 0;
1797   vlsl->vls_wrk_index = vcl_get_worker_index ();
1798   vlsl->select_mp_check = 0;
1799   clib_rwlock_init (&vlsl->vls_pool_lock);
1800   vls_mt_locks_init ();
1801
1802   parent_wrk = vcl_worker_get (parent_wrk_index);
1803   vls_worker_copy_on_fork (parent_wrk);
1804   parent_wrk->forked_child = vcl_get_worker_index ();
1805
1806   VDBG (0, "forked child main worker initialized");
1807   vcm->forking = 0;
1808 }
1809
1810 static void
1811 vls_app_fork_parent_handler (void)
1812 {
1813   vcm->forking = 1;
1814   while (vcm->forking)
1815     ;
1816 }
1817
1818 void
1819 vls_app_exit (void)
1820 {
1821   vls_worker_t *wrk = vls_worker_get_current ();
1822
1823   /* Handle pending wrk cleanup */
1824   vls_handle_pending_wrk_cleanup ();
1825
1826   /* Unshare the sessions. VCL will clean up the worker */
1827   vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1828   vls_worker_free (wrk);
1829 }
1830
1831 static void
1832 vls_clone_and_share_rpc_handler (void *args)
1833 {
1834   vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1835   vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1836   vcl_locked_session_t *vls, *dst_vls;
1837   vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
1838   vcl_session_t *s, *dst_s;
1839
1840   VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1841         vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1842         msg->origin_session_index);
1843
1844   /* VCL locked session can't been protected, so DONT touch it.
1845    * VCL session may been free, check it.
1846    */
1847   dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1848   s = vcl_session_get (vcl_wrk, msg->session_index);
1849   if (PREDICT_FALSE (!s))
1850     {
1851       dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1852       return;
1853     }
1854
1855   if (!vls_mt_wrk_supported ())
1856     {
1857       vls = vls_session_get (wrk, msg->vls_index);
1858       vls_init_share_session (wrk, vls);
1859       dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1860       dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1861       dst_vls->shared_data_index = vls->shared_data_index;
1862     }
1863   dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
1864   clib_memcpy (dst_s, s, sizeof (*s));
1865
1866   dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
1867 }
1868
1869 static void
1870 vls_session_cleanup_rpc_handler (void *args)
1871 {
1872   vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1873   vcl_worker_t *wrk = vcl_worker_get_current ();
1874   vls_worker_t *vls_wrk = vls_worker_get_current ();
1875   vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
1876
1877   VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1878         wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
1879
1880   vppcom_session_close (sh);
1881   vls_sh_to_vlsh_table_del (vls_wrk, sh);
1882 }
1883
1884 static void
1885 vls_rpc_handler (void *args)
1886 {
1887   vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1888   switch (msg->type)
1889     {
1890     case VLS_RPC_CLONE_AND_SHARE:
1891       vls_clone_and_share_rpc_handler (msg->data);
1892       break;
1893     case VLS_RPC_SESS_CLEANUP:
1894       vls_session_cleanup_rpc_handler (msg->data);
1895       break;
1896     default:
1897       break;
1898     }
1899 }
1900
1901 void
1902 vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1903                               u32 session_index, u32 vls_wrk_index,
1904                               u32 dst_wrk_index, u32 dst_vls_index,
1905                               u32 dst_session_index)
1906 {
1907   u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1908   vls_clone_and_share_msg_t *msg;
1909   vls_rpc_msg_t *rpc;
1910   int ret;
1911   f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
1912
1913   rpc = (vls_rpc_msg_t *) & data;
1914   rpc->type = VLS_RPC_CLONE_AND_SHARE;
1915   msg = (vls_clone_and_share_msg_t *) & rpc->data;
1916   msg->origin_vls_wrk = vls_wrk_index;
1917   msg->origin_vls_index = origin_vls_index;
1918   msg->origin_vcl_wrk = wrk->wrk_index;
1919   msg->origin_session_index = session_index;
1920   msg->vls_index = dst_vls_index;
1921   msg->session_index = dst_session_index;
1922
1923   /* Try lock and handle rpcs if two threads send each other
1924    * clone requests at the same time.
1925    */
1926   wrk->rpc_done = VLS_RPC_STATE_INIT;
1927   while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1928     vcl_flush_mq_events ();
1929   ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1930
1931   VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
1932         dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1933         msg->origin_session_index, ret);
1934   while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1935          clib_time_now (&wrk->clib_time) < timeout)
1936     ;
1937   clib_spinlock_unlock (&vlsm->worker_rpc_lock);
1938 }
1939
1940 void
1941 vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1942                               u32 dst_wrk_index, u32 dst_session_index)
1943 {
1944   u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1945   vls_sess_cleanup_msg_t *msg;
1946   vls_rpc_msg_t *rpc;
1947   int ret;
1948
1949   rpc = (vls_rpc_msg_t *) & data;
1950   rpc->type = VLS_RPC_SESS_CLEANUP;
1951   msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1952   msg->origin_vcl_wrk = wrk->wrk_index;
1953   msg->session_index = dst_session_index;
1954
1955   ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1956
1957   VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
1958         dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
1959 }
1960
1961 int
1962 vls_app_create (char *app_name)
1963 {
1964   int rv;
1965
1966   if ((rv = vppcom_app_create (app_name)))
1967     return rv;
1968
1969   vlsm = clib_mem_alloc (sizeof (vls_main_t));
1970   clib_memset (vlsm, 0, sizeof (*vlsm));
1971   clib_rwlock_init (&vlsm->shared_data_lock);
1972   clib_spinlock_init (&vlsm->worker_rpc_lock);
1973   pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1974
1975   pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1976                   vls_app_fork_child_handler);
1977   atexit (vls_app_exit);
1978   vls_worker_alloc ();
1979   vlsl->vls_wrk_index = vcl_get_worker_index ();
1980   clib_rwlock_init (&vlsl->vls_pool_lock);
1981   vls_mt_locks_init ();
1982   vcm->wrk_rpc_fn = vls_rpc_handler;
1983   return VPPCOM_OK;
1984 }
1985
1986 unsigned char
1987 vls_use_eventfd (void)
1988 {
1989   return vcm->cfg.use_mq_eventfd;
1990 }
1991
1992 unsigned char
1993 vls_mt_wrk_supported (void)
1994 {
1995   return vcm->cfg.mt_wrk_supported;
1996 }
1997
1998 int
1999 vls_use_real_epoll (void)
2000 {
2001   if (vcl_get_worker_index () == ~0)
2002     return 0;
2003
2004   return vcl_worker_get_current ()->vcl_needs_real_epoll;
2005 }
2006
2007 void
2008 vls_register_vcl_worker (void)
2009 {
2010   vls_mt_add ();
2011 }
2012
2013 /*
2014  * fd.io coding-style-patch-verification: ON
2015  *
2016  * Local Variables:
2017  * eval: (c-set-style "gnu")
2018  * End:
2019  */