2 * Copyright (c) 2021 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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:
23 * 1) per-process workers
25 * +----------+ +----------+
27 * | process0 | | process1 |
29 * +-----+----+ +-----+----+
32 * +-----+----+ +-----+----+
34 * | vls_wrk0 | | vls_wrk1 |
36 * +-----+----+ +-----+----+
39 * +-----+----+ +-----+----+
41 * | vcl_wrk0 | | vcl_wrk1 |
43 * +----------+ +----------+
45 * 2) per-thread workers 3) single-worker multi-thread
47 * +---------+ +---------+ +---------+ +---------+
49 * | thread0 | | thread1 | | thread0 | | thread1 |
51 * +--------++ +-+-------+ +--------++ +-+-------+
54 * +-+------+-+ +-+------+-+
56 * | vls_wrk0 | | vls_wrk0 |
58 * +-+------+-+ +----+-----+
61 * +--------+-+ +-+--------+ +----+-----+
63 * | vcl_wrk0 | | vcl_wrk1 | | vcl_wrk0 |
65 * +----------+ +----------+ +----------+
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).
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).
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).
86 #include <vcl/vcl_locked.h>
87 #include <vcl/vcl_private.h>
89 typedef struct vls_shared_data_
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 */
97 typedef struct vcl_locked_session_
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;
108 typedef struct vls_worker_
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 */
117 typedef struct vls_local_
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;
127 static vls_process_local_t vls_local;
128 static vls_process_local_t *vlsl = &vls_local;
130 typedef struct vls_main_
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 */
143 VLS_RPC_STATE_SUCCESS,
144 VLS_RPC_STATE_SESSION_NOT_EXIST,
147 typedef enum vls_rpc_msg_type_
149 VLS_RPC_CLONE_AND_SHARE,
150 VLS_RPC_SESS_CLEANUP,
151 } vls_rpc_msg_type_e;
153 typedef struct vls_rpc_msg_
159 typedef struct vls_clone_and_share_msg_
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;
169 typedef struct vls_sess_cleanup_msg_
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;
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);
186 vls_get_worker_index (void)
188 return vlsl->vls_wrk_index;
192 vls_shared_data_alloc (void)
194 vls_shared_data_t *vls_shd;
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);
207 vls_shared_data_index (vls_shared_data_t * vls_shd)
209 return vls_shd - vlsm->shared_data_pool;
213 vls_shared_data_get (u32 shd_index)
215 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
217 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
221 vls_shared_data_free (u32 shd_index)
223 vls_shared_data_t *vls_shd;
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);
235 vls_shared_data_pool_rlock (void)
237 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
241 vls_shared_data_pool_runlock (void)
243 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
247 vls_mt_pool_rlock (void)
249 if (vlsl->vls_mt_n_threads > 1)
250 clib_rwlock_reader_lock (&vlsl->vls_pool_lock);
254 vls_mt_pool_runlock (void)
256 if (vlsl->vls_mt_n_threads > 1)
257 clib_rwlock_reader_unlock (&vlsl->vls_pool_lock);
261 vls_mt_pool_wlock (void)
263 if (vlsl->vls_mt_n_threads > 1)
264 clib_rwlock_writer_lock (&vlsl->vls_pool_lock);
268 vls_mt_pool_wunlock (void)
270 if (vlsl->vls_mt_n_threads > 1)
271 clib_rwlock_writer_unlock (&vlsl->vls_pool_lock);
284 VLS_MT_LOCK_MQ = 1 << 0,
285 VLS_MT_LOCK_SPOOL = 1 << 1
286 } vls_mt_lock_type_t;
291 vlsl->vls_mt_n_threads += 1;
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 ())
298 if (vppcom_worker_register () != VPPCOM_OK)
299 VERR ("failed to register worker");
302 vcl_set_worker_index (vlsl->vls_wrk_index);
306 vls_mt_mq_lock (void)
308 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
312 vls_mt_mq_unlock (void)
314 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
318 vls_mt_spool_lock (void)
320 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
324 vls_mt_create_unlock (void)
326 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
330 vls_mt_locks_init (void)
332 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
333 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
337 vls_is_shared (vcl_locked_session_t * vls)
339 return (vls->shared_data_index != ~0);
343 vls_lock (vcl_locked_session_t * vls)
345 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
346 clib_spinlock_lock (&vls->lock);
350 vls_unlock (vcl_locked_session_t * vls)
352 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
353 clib_spinlock_unlock (&vls->lock);
356 static inline vcl_session_handle_t
357 vls_to_sh (vcl_locked_session_t * vls)
359 return vcl_session_handle_from_index (vls->session_index);
362 static inline vcl_session_handle_t
363 vls_to_sh_tu (vcl_locked_session_t * vls)
365 vcl_session_handle_t sh;
366 sh = vls_to_sh (vls);
367 vls_mt_pool_runlock ();
371 static vls_worker_t *
372 vls_worker_get_current (void)
374 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
378 vls_worker_alloc (void)
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);
391 vls_worker_free (vls_worker_t * wrk)
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);
400 static vls_worker_t *
401 vls_worker_get (u32 wrk_index)
403 if (pool_is_free_index (vlsm->workers, wrk_index))
405 return pool_elt_at_index (vlsm->workers, wrk_index);
409 vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
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);
419 vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
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);
429 vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
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);
440 vls_alloc (vcl_session_handle_t sh)
442 vls_worker_t *wrk = vls_worker_get_current ();
443 vcl_locked_session_t *vls;
445 vls_mt_pool_wlock ();
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 ())
455 hash_set (vls->vcl_wrk_index_to_session_index, vls->vcl_wrk_index,
457 vls->owner_vcl_wrk_index = vls->vcl_wrk_index;
459 clib_spinlock_init (&vls->lock);
461 vls_mt_pool_wunlock ();
462 return vls->vls_index;
465 static vcl_locked_session_t *
466 vls_get (vls_handle_t vlsh)
468 vls_worker_t *wrk = vls_worker_get_current ();
469 if (pool_is_free_index (wrk->vls_pool, vlsh))
471 return pool_elt_at_index (wrk->vls_pool, vlsh);
475 vls_free (vcl_locked_session_t * vls)
477 vls_worker_t *wrk = vls_worker_get_current ();
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);
486 static vcl_locked_session_t *
487 vls_get_and_lock (vls_handle_t vlsh)
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))
493 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
498 static vcl_locked_session_t *
499 vls_get_w_dlock (vls_handle_t vlsh)
501 vcl_locked_session_t *vls;
502 vls_mt_pool_rlock ();
503 vls = vls_get_and_lock (vlsh);
505 vls_mt_pool_runlock ();
510 vls_get_and_unlock (vls_handle_t vlsh)
512 vcl_locked_session_t *vls;
513 vls_mt_pool_rlock ();
514 vls = vls_get (vlsh);
516 vls_mt_pool_runlock ();
520 vls_dunlock (vcl_locked_session_t * vls)
523 vls_mt_pool_runlock ();
526 static vcl_locked_session_t *
527 vls_session_get (vls_worker_t * wrk, u32 vls_index)
529 if (pool_is_free_index (wrk->vls_pool, vls_index))
531 return pool_elt_at_index (wrk->vls_pool, vls_index);
535 vlsh_to_sh (vls_handle_t vlsh)
537 vcl_locked_session_t *vls;
540 vls = vls_get_w_dlock (vlsh);
542 return INVALID_SESSION_ID;
543 rv = vls_to_sh (vls);
549 vlsh_to_session_index (vls_handle_t vlsh)
551 vcl_session_handle_t sh;
552 sh = vlsh_to_sh (vlsh);
553 return vppcom_session_index (sh);
557 vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
559 vls_worker_t *wrk = vls_worker_get_current ();
560 uword *vlshp = vls_sh_to_vlsh_table_get (
562 vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
564 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
568 vls_session_index_to_vlsh (uint32_t session_index)
572 vls_mt_pool_rlock ();
573 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
574 vls_mt_pool_runlock ();
580 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
582 vls_shared_data_t *vls_shd;
585 if (vls->shared_data_index == ~0)
588 vls_shared_data_pool_rlock ();
590 vls_shd = vls_shared_data_get (vls->shared_data_index);
591 clib_spinlock_lock (&vls_shd->lock);
593 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
594 if (vls_shd->workers_subscribed[i] == wrk_index)
596 clib_spinlock_unlock (&vls_shd->lock);
597 vls_shared_data_pool_runlock ();
600 clib_spinlock_unlock (&vls_shd->lock);
602 vls_shared_data_pool_runlock ();
607 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
609 vls_shared_data_t *vls_shd;
611 if (vls->shared_data_index == ~0)
613 clib_warning ("not a shared session");
617 vls_shared_data_pool_rlock ();
619 vls_shd = vls_shared_data_get (vls->shared_data_index);
621 clib_spinlock_lock (&vls_shd->lock);
623 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
624 clib_spinlock_unlock (&vls_shd->lock);
626 vls_shared_data_pool_runlock ();
630 vls_shared_get_owner (vcl_locked_session_t * vls)
632 vls_shared_data_t *vls_shd;
635 vls_shared_data_pool_rlock ();
637 vls_shd = vls_shared_data_get (vls->shared_data_index);
638 owner_wrk = vls_shd->owner_wrk_index;
640 vls_shared_data_pool_runlock ();
646 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
648 vls_shared_data_t *vls_shd;
651 if (vls->shared_data_index == ~0)
653 clib_warning ("not a shared session");
657 vls_shared_data_pool_rlock ();
659 vls_shd = vls_shared_data_get (vls->shared_data_index);
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);
665 vls_shared_data_pool_runlock ();
667 return (is_set == 1);
671 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
676 wrk = vcl_worker_get (wrk_index);
677 ls = vcl_session_get (wrk, vls->session_index);
679 /* Listen request already sent */
680 if (ls->flags & VCL_SESSION_F_PENDING_LISTEN)
683 vcl_send_session_listen (wrk, ls);
685 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */);
689 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
694 wrk = vcl_worker_get (wrk_index);
695 s = vcl_session_get (wrk, vls->session_index);
696 if (s->session_state != VCL_STATE_LISTEN)
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 */ );
704 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
709 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
711 if (vls_shd->workers_subscribed[i] == wrk_index)
718 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
720 vls_shared_data_t *vls_shd;
721 int do_disconnect, pos;
725 if (vls->shared_data_index == ~0)
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 */ );
732 vls_shared_data_pool_rlock ();
734 vls_shd = vls_shared_data_get (vls->shared_data_index);
735 clib_spinlock_lock (&vls_shd->lock);
737 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
740 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
746 * Unsubscribe from share data and fifos
750 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
751 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
753 vec_del1 (vls_shd->workers_subscribed, pos);
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);
763 * No subscriber left, cleanup shared data
767 u32 shd_index = vls_shared_data_index (vls_shd);
769 clib_spinlock_unlock (&vls_shd->lock);
770 vls_shared_data_pool_runlock ();
772 vls_shared_data_free (shd_index);
774 /* All locks have been dropped */
778 /* Return, if this is not the owning worker */
779 if (vls_shd->owner_wrk_index != wrk->wrk_index)
782 ASSERT (vec_len (vls_shd->workers_subscribed));
785 * Check if we can change owner or close
787 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
789 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
791 /* XXX is this still needed? */
792 if (vec_len (vls_shd->workers_subscribed) > 1)
793 clib_warning ("more workers need to be updated");
797 clib_spinlock_unlock (&vls_shd->lock);
798 vls_shared_data_pool_runlock ();
804 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
806 vls_shared_data_t *vls_shd;
808 u32 vls_shd_index = vls_shared_data_alloc ();
810 vls_shared_data_pool_rlock ();
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);
817 vls_shared_data_pool_runlock ();
821 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
823 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->vcl_wrk_index);
824 vls_shared_data_t *vls_shd;
827 s = vcl_session_get (vcl_wrk, vls->session_index);
830 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
831 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
835 ASSERT (vls->shared_data_index != ~0);
837 /* Reinit session lock */
838 clib_spinlock_init (&vls->lock);
840 vls_shared_data_pool_rlock ();
842 vls_shd = vls_shared_data_get (vls->shared_data_index);
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);
848 vls_shared_data_pool_runlock ();
850 if (s->session_state == VCL_STATE_LISTEN)
852 s->session_state = VCL_STATE_LISTEN_NO_MQ;
853 s->rx_fifo = s->tx_fifo = 0;
857 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
862 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
864 vcl_locked_session_t *vls, *parent_vls;
867 pool_foreach (vls, vls_wrk->vls_pool) {
868 /* Initialize sharing on parent session */
869 if (vls->shared_data_index == ~0)
871 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
872 vls_init_share_session (vls_parent_wrk, parent_vls);
873 vls->shared_data_index = parent_vls->shared_data_index;
875 vls_share_session (vls_wrk, vls);
881 vls_validate_veps (vcl_worker_t *wrk)
884 u32 session_index, wrk_index;
886 pool_foreach (s, wrk->sessions)
888 if (s->vep.vep_sh != ~0)
890 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
891 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
893 if (s->vep.next_sh != ~0)
895 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
897 s->vep.next_sh = vcl_session_handle_from_index (session_index);
899 if (s->vep.prev_sh != ~0)
901 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
903 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
909 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
911 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
912 vcl_worker_t *vcl_wrk = vcl_worker_get_current ();
913 u32 vls_index, session_index, wrk_index;
914 vcl_session_handle_t sh;
915 vcl_locked_session_t *vls;
920 vcl_wrk->sessions = pool_dup (parent_wrk->sessions);
921 vcl_wrk->session_index_by_vpp_handles =
922 hash_dup (parent_wrk->session_index_by_vpp_handles);
927 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
929 /* clang-format off */
930 hash_foreach (sh, vls_index, vls_parent_wrk->sh_to_vlsh_table, ({
931 vcl_session_handle_parse (sh, &wrk_index, &session_index);
932 hash_set (vls_wrk->sh_to_vlsh_table,
933 vcl_session_handle_from_index (session_index), vls_index);
935 /* clang-format on */
936 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
939 * Detach vls from parent vcl worker and attach them to child.
941 pool_foreach (vls, vls_wrk->vls_pool)
943 vls->vcl_wrk_index = vcl_wrk->wrk_index;
946 /* Validate vep's handle */
947 vls_validate_veps (vcl_wrk);
949 vls_share_sessions (vls_parent_wrk, vls_wrk);
953 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
955 vcl_worker_t *wrk = vcl_worker_get_current ();
956 vcl_session_t *s = 0;
961 s = vcl_session_get (wrk, vls->session_index);
962 if (PREDICT_FALSE (!s))
964 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
971 is_nonblk = vcl_session_read_ready (s) != 0;
975 *locks_acq |= VLS_MT_LOCK_MQ;
978 case VLS_MT_OP_WRITE:
981 is_nonblk = vcl_session_write_ready (s) != 0;
985 *locks_acq |= VLS_MT_LOCK_MQ;
988 case VLS_MT_OP_XPOLL:
990 *locks_acq |= VLS_MT_LOCK_MQ;
992 case VLS_MT_OP_SPOOL:
993 vls_mt_spool_lock ();
994 *locks_acq |= VLS_MT_LOCK_SPOOL;
1002 vls_mt_rel_locks (int locks_acq)
1004 if (locks_acq & VLS_MT_LOCK_MQ)
1005 vls_mt_mq_unlock ();
1006 if (locks_acq & VLS_MT_LOCK_SPOOL)
1007 vls_mt_create_unlock ();
1011 vls_mt_session_should_migrate (vcl_locked_session_t * vls)
1013 return (vls_mt_wrk_supported () &&
1014 vls->vcl_wrk_index != vcl_get_worker_index ());
1017 static vcl_locked_session_t *
1018 vls_mt_session_migrate (vcl_locked_session_t *vls)
1020 u32 wrk_index = vcl_get_worker_index ();
1022 vls_worker_t *vls_wrk = vls_worker_get_current ();
1023 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
1024 vcl_session_t *session;
1027 ASSERT (vls_mt_wrk_supported () && vls->vcl_wrk_index != wrk_index);
1030 * VCL session on current vcl worker already allocated. Update current
1031 * owner worker and index and return
1033 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
1035 vls->vcl_wrk_index = wrk_index;
1036 vls->session_index = (u32) p[0];
1041 * Ask vcl worker that owns the original vcl session to clone it into
1042 * current vcl worker session pool
1045 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
1046 vls->owner_vcl_wrk_index)))
1048 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
1051 vls_mt_pool_runlock ();
1055 src_sid = (u32) p[0];
1056 wrk = vcl_worker_get_current ();
1057 session = vcl_session_alloc (wrk);
1058 sid = session->session_index;
1059 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
1060 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
1062 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
1063 vls_index = vls->vls_index;
1064 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
1066 vls_mt_pool_runlock ();
1067 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
1068 own_vcl_wrk_index, vls_index, src_sid);
1070 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
1072 VWRN ("session %u not exist", src_sid);
1075 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
1077 VWRN ("failed to wait rpc response");
1080 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
1081 session->vep.next_sh != ~0))
1083 VERR ("can't migrate nonempty epoll session");
1087 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
1088 session->session_state != VCL_STATE_CLOSED))
1090 VERR ("migrate NOT supported, session_status (%u)",
1091 session->session_state);
1096 vls = vls_get_w_dlock (vls_index);
1097 if (PREDICT_FALSE (!vls))
1099 VWRN ("failed to get vls %u", vls_index);
1103 session->session_index = sid;
1104 vls->vcl_wrk_index = wrk_index;
1105 vls->session_index = sid;
1106 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
1107 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1112 vcl_session_free (wrk, session);
1117 vls_mt_detect (void)
1119 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1123 #define vls_mt_guard(_vls, _op) \
1124 int _locks_acq = 0; \
1125 if (vls_mt_wrk_supported ()) \
1127 if (PREDICT_FALSE (_vls && \
1128 ((vcl_locked_session_t *) _vls)->vcl_wrk_index != \
1129 vcl_get_worker_index ())) \
1131 _vls = vls_mt_session_migrate (_vls); \
1132 if (PREDICT_FALSE (!_vls)) \
1133 return VPPCOM_EBADFD; \
1138 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1139 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1142 #define vls_mt_unguard() \
1143 if (PREDICT_FALSE (_locks_acq)) \
1144 vls_mt_rel_locks (_locks_acq)
1147 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1149 vcl_locked_session_t *vls;
1153 if (!(vls = vls_get_w_dlock (vlsh)))
1154 return VPPCOM_EBADFD;
1156 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1157 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
1159 vls_get_and_unlock (vlsh);
1164 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1166 vcl_locked_session_t *vls;
1170 if (!(vls = vls_get_w_dlock (vlsh)))
1171 return VPPCOM_EBADFD;
1172 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1173 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
1175 vls_get_and_unlock (vlsh);
1180 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1181 vppcom_endpt_t * ep)
1183 vcl_locked_session_t *vls;
1187 if (!(vls = vls_get_w_dlock (vlsh)))
1188 return VPPCOM_EBADFD;
1189 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1190 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
1192 vls_get_and_unlock (vlsh);
1197 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1199 vcl_locked_session_t *vls;
1203 if (!(vls = vls_get_w_dlock (vlsh)))
1204 return VPPCOM_EBADFD;
1205 vls_mt_guard (vls, VLS_MT_OP_READ);
1206 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
1208 vls_get_and_unlock (vlsh);
1213 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1214 vppcom_endpt_t * ep)
1216 vcl_locked_session_t *vls;
1220 if (!(vls = vls_get_w_dlock (vlsh)))
1221 return VPPCOM_EBADFD;
1222 vls_mt_guard (vls, VLS_MT_OP_READ);
1223 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1226 vls_get_and_unlock (vlsh);
1231 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1233 vcl_locked_session_t *vls;
1237 if (!(vls = vls_get_w_dlock (vlsh)))
1238 return VPPCOM_EBADFD;
1239 if (vls_mt_session_should_migrate (vls))
1241 vls = vls_mt_session_migrate (vls);
1242 if (PREDICT_FALSE (!vls))
1243 return VPPCOM_EBADFD;
1245 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1246 vls_get_and_unlock (vlsh);
1251 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1253 vcl_locked_session_t *vls;
1257 if (!(vls = vls_get_w_dlock (vlsh)))
1258 return VPPCOM_EBADFD;
1259 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1260 vls_get_and_unlock (vlsh);
1265 vls_listen (vls_handle_t vlsh, int q_len)
1267 vcl_locked_session_t *vls;
1271 if (!(vls = vls_get_w_dlock (vlsh)))
1272 return VPPCOM_EBADFD;
1273 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1274 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
1276 vls_get_and_unlock (vlsh);
1281 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1283 vcl_locked_session_t *vls;
1287 if (!(vls = vls_get_w_dlock (vlsh)))
1288 return VPPCOM_EBADFD;
1289 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1290 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
1292 vls_get_and_unlock (vlsh);
1297 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1299 vcl_worker_t *wrk = vcl_worker_get_current ();
1303 if (vls_mt_wrk_supported ())
1306 ASSERT (wrk->wrk_index == vls->vcl_wrk_index);
1307 s = vcl_session_get (wrk, vls->session_index);
1308 switch (s->session_state)
1310 case VCL_STATE_LISTEN:
1313 vls_listener_wrk_set (vls, vls->vcl_wrk_index, 1 /* is_active */);
1316 /* Although removal from epoll means listener no longer accepts new
1317 * sessions, the accept queue built by vpp cannot be drained by stopping
1318 * the listener. Morover, some applications, e.g., nginx, might
1319 * constantly remove and add listeners to their epfds. Removing
1320 * listeners in such situations causes a lot of churn in vpp as segments
1321 * and segment managers need to be recreated. */
1322 /* vls_listener_wrk_stop_listen (vls, vls->vcl_wrk_index); */
1324 case VCL_STATE_LISTEN_NO_MQ:
1328 /* Register worker as listener */
1329 vls_listener_wrk_start_listen (vls, vls->vcl_wrk_index);
1331 /* If owner worker did not attempt to accept/xpoll on the session,
1332 * force a listen stop for it, since it may not be interested in
1333 * accepting new sessions.
1334 * This is pretty much a hack done to give app workers the illusion
1335 * that it is fine to listen and not accept new sessions for a
1336 * given listener. Without it, we would accumulate unhandled
1337 * accepts on the passive worker message queue. */
1338 owner_wrk = vls_shared_get_owner (vls);
1339 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1340 vls_listener_wrk_stop_listen (vls, owner_wrk);
1348 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1350 vls_handle_t accepted_vlsh;
1351 vcl_locked_session_t *vls;
1355 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1356 return VPPCOM_EBADFD;
1357 if (vcl_n_workers () > 1)
1358 vls_mp_checks (vls, 1 /* is_add */ );
1359 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1360 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
1362 vls_get_and_unlock (listener_vlsh);
1365 accepted_vlsh = vls_alloc (sh);
1366 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1367 vppcom_session_close (sh);
1368 return accepted_vlsh;
1372 vls_create (uint8_t proto, uint8_t is_nonblocking)
1374 vcl_session_handle_t sh;
1376 vcl_locked_session_t *vls = NULL;
1379 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1380 sh = vppcom_session_create (proto, is_nonblocking);
1382 if (sh == INVALID_SESSION_ID)
1383 return VLS_INVALID_HANDLE;
1385 vlsh = vls_alloc (sh);
1386 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1387 vppcom_session_close (sh);
1393 vls_mt_session_cleanup (vcl_locked_session_t * vls)
1395 u32 session_index, wrk_index, current_vcl_wrk;
1396 vcl_worker_t *wrk = vcl_worker_get_current ();
1398 ASSERT (vls_mt_wrk_supported ());
1400 current_vcl_wrk = vcl_get_worker_index ();
1403 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1405 if (current_vcl_wrk != wrk_index)
1406 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
1409 hash_free (vls->vcl_wrk_index_to_session_index);
1413 vls_close (vls_handle_t vlsh)
1415 vcl_locked_session_t *vls;
1419 vls_mt_pool_wlock ();
1421 vls = vls_get_and_lock (vlsh);
1424 vls_mt_pool_wunlock ();
1425 return VPPCOM_EBADFD;
1428 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1430 if (vls_is_shared (vls))
1431 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1433 rv = vppcom_session_close (vls_to_sh (vls));
1435 if (vls_mt_wrk_supported ())
1436 vls_mt_session_cleanup (vls);
1441 vls_mt_pool_wunlock ();
1447 vls_shutdown (vls_handle_t vlsh, int how)
1449 vcl_locked_session_t *vls;
1453 if (!(vls = vls_get_w_dlock (vlsh)))
1454 return VPPCOM_EBADFD;
1456 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1457 rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
1459 vls_get_and_unlock (vlsh);
1465 vls_epoll_create (void)
1467 vcl_session_handle_t sh;
1472 sh = vppcom_epoll_create ();
1473 if (sh == INVALID_SESSION_ID)
1474 return VLS_INVALID_HANDLE;
1476 vlsh = vls_alloc (sh);
1477 if (vlsh == VLS_INVALID_HANDLE)
1478 vppcom_session_close (sh);
1484 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1486 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
1489 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1493 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1494 struct epoll_event *event)
1496 vcl_locked_session_t *ep_vls, *vls;
1497 vcl_session_handle_t ep_sh, sh;
1501 vls_mt_pool_rlock ();
1503 ep_vls = vls_get_and_lock (ep_vlsh);
1504 if (PREDICT_FALSE (!ep_vls))
1506 vls_mt_pool_runlock ();
1507 return VPPCOM_EBADFD;
1510 if (vls_mt_session_should_migrate (ep_vls))
1512 ep_vls = vls_mt_session_migrate (ep_vls);
1513 if (PREDICT_FALSE (!ep_vls))
1515 vls_mt_pool_runlock ();
1516 return VPPCOM_EBADFD;
1520 vls = vls_get_and_lock (vlsh);
1521 if (PREDICT_FALSE (!vls))
1523 vls_unlock (ep_vls);
1524 vls_mt_pool_runlock ();
1525 return VPPCOM_EBADFD;
1528 ep_sh = vls_to_sh (ep_vls);
1529 sh = vls_to_sh (vls);
1531 vls_epoll_ctl_mp_checks (vls, op);
1532 vls_mt_pool_runlock ();
1533 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1535 vls_mt_pool_rlock ();
1536 ep_vls = vls_get (ep_vlsh);
1537 vls = vls_get (vlsh);
1539 vls_unlock (ep_vls);
1540 vls_mt_pool_runlock ();
1545 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1546 int maxevents, double wait_for_time)
1548 vcl_locked_session_t *vls, *vls_tmp = NULL;
1552 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1553 return VPPCOM_EBADFD;
1554 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
1555 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1558 vls_get_and_unlock (ep_vlsh);
1559 vls_handle_pending_wrk_cleanup ();
1564 vls_select_mp_checks (vcl_si_set * read_map)
1566 vcl_locked_session_t *vls;
1571 if (vcl_n_workers () <= 1)
1573 vlsl->select_mp_check = 1;
1580 vlsl->select_mp_check = 1;
1581 wrk = vcl_worker_get_current ();
1584 clib_bitmap_foreach (si, read_map) {
1585 s = vcl_session_get (wrk, si);
1586 if (s->session_state == VCL_STATE_LISTEN)
1588 vls = vls_get (vls_session_index_to_vlsh (si));
1589 vls_mp_checks (vls, 1 /* is_add */);
1596 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1597 vcl_si_set * except_map, double wait_for_time)
1600 vcl_locked_session_t *vls = NULL;
1603 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1604 if (PREDICT_FALSE (!vlsl->select_mp_check))
1605 vls_select_mp_checks (read_map);
1606 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1608 vls_handle_pending_wrk_cleanup ();
1613 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1615 u32 current_wrk, is_current;
1616 vcl_locked_session_t *vls;
1619 if (pool_elts (vcm->workers) <= 1)
1622 current_wrk = vcl_get_worker_index ();
1623 is_current = current_wrk == wrk->wrk_index;
1626 pool_foreach (s, wrk->sessions) {
1627 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
1628 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1629 vls_unshare_session (vls, wrk);
1635 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1637 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1639 /* Unshare sessions and also cleanup worker since child may have
1640 * called _exit () and therefore vcl may not catch the event */
1641 vls_unshare_vcl_worker_sessions (wrk);
1643 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1644 * may have been closed, so DONOT notify VPP.
1646 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
1648 vls_worker_free (vls_wrk);
1652 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1654 vcl_worker_t *sub_child;
1657 if (child_wrk->forked_child != ~0)
1659 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1662 /* Wait a bit, maybe the process is going away */
1663 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1665 if (kill (sub_child->current_pid, 0) < 0)
1666 vls_cleanup_forked_child (child_wrk, sub_child);
1669 vls_cleanup_vcl_worker (child_wrk);
1670 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1671 wrk->forked_child = ~0;
1675 vls_handle_pending_wrk_cleanup (void)
1678 vcl_worker_t *child_wrk, *wrk;
1679 vls_worker_t *vls_wrk = vls_worker_get_current ();
1681 if (PREDICT_TRUE (vec_len (vls_wrk->pending_vcl_wrk_cleanup) == 0))
1684 wrk = vcl_worker_get_current ();
1685 vec_foreach (wip, vls_wrk->pending_vcl_wrk_cleanup)
1687 child_wrk = vcl_worker_get_if_valid (*wip);
1690 vls_cleanup_forked_child (wrk, child_wrk);
1692 vec_reset_length (vls_wrk->pending_vcl_wrk_cleanup);
1695 static struct sigaction old_sa;
1698 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1700 vcl_worker_t *wrk, *child_wrk;
1701 vls_worker_t *vls_wrk;
1703 if (vcl_get_worker_index () == ~0)
1706 if (sigaction (SIGCHLD, &old_sa, 0))
1708 VERR ("couldn't restore sigchld");
1712 wrk = vcl_worker_get_current ();
1713 if (wrk->forked_child == ~0)
1716 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1720 if (si && si->si_pid != child_wrk->current_pid)
1722 VDBG (0, "unexpected child pid %u", si->si_pid);
1726 /* Parent process may enter sighandler with a lock, such as lock in localtime
1727 * or in mspace_free, and child wrk cleanup may try to get such locks and
1729 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1731 vls_wrk = vls_worker_get_current ();
1732 vec_add1 (vls_wrk->pending_vcl_wrk_cleanup, child_wrk->wrk_index);
1735 if (old_sa.sa_flags & SA_SIGINFO)
1737 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1738 fn (signum, si, uc);
1742 void (*fn) (int) = old_sa.sa_handler;
1749 vls_incercept_sigchld ()
1751 struct sigaction sa;
1752 if (old_sa.sa_sigaction)
1754 VDBG (0, "have intercepted sigchld");
1757 clib_memset (&sa, 0, sizeof (sa));
1758 sa.sa_sigaction = vls_intercept_sigchld_handler;
1759 sa.sa_flags = SA_SIGINFO;
1760 if (sigaction (SIGCHLD, &sa, &old_sa))
1762 VERR ("couldn't intercept sigchld");
1768 vls_app_pre_fork (void)
1770 vls_incercept_sigchld ();
1771 vcl_flush_mq_events ();
1775 vls_app_fork_child_handler (void)
1777 vcl_worker_t *parent_wrk;
1778 int parent_wrk_index;
1780 parent_wrk_index = vcl_get_worker_index ();
1781 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1787 vcl_set_worker_index (~0);
1790 * Allocate and register vcl worker with vpp
1792 if (vppcom_worker_register ())
1794 VERR ("couldn't register new worker!");
1799 * Allocate/initialize vls worker and share sessions
1801 vls_worker_alloc ();
1803 /* Reset number of threads and set wrk index */
1804 vlsl->vls_mt_n_threads = 0;
1805 vlsl->vls_wrk_index = vcl_get_worker_index ();
1806 vlsl->select_mp_check = 0;
1807 clib_rwlock_init (&vlsl->vls_pool_lock);
1808 vls_mt_locks_init ();
1810 parent_wrk = vcl_worker_get (parent_wrk_index);
1811 vls_worker_copy_on_fork (parent_wrk);
1812 parent_wrk->forked_child = vcl_get_worker_index ();
1814 VDBG (0, "forked child main worker initialized");
1819 vls_app_fork_parent_handler (void)
1822 while (vcm->forking)
1829 vls_worker_t *wrk = vls_worker_get_current ();
1831 /* Handle pending wrk cleanup */
1832 vls_handle_pending_wrk_cleanup ();
1834 /* Unshare the sessions. VCL will clean up the worker */
1835 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1836 vls_worker_free (wrk);
1840 vls_clone_and_share_rpc_handler (void *args)
1842 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1843 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1844 vcl_locked_session_t *vls, *dst_vls;
1845 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
1846 vcl_session_t *s, *dst_s;
1848 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1849 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1850 msg->origin_session_index);
1852 /* VCL locked session can't been protected, so DONT touch it.
1853 * VCL session may been free, check it.
1855 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1856 s = vcl_session_get (vcl_wrk, msg->session_index);
1857 if (PREDICT_FALSE (!s))
1859 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1863 if (!vls_mt_wrk_supported ())
1865 vls = vls_session_get (wrk, msg->vls_index);
1866 vls_init_share_session (wrk, vls);
1867 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1868 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1869 dst_vls->shared_data_index = vls->shared_data_index;
1871 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
1872 clib_memcpy (dst_s, s, sizeof (*s));
1874 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
1878 vls_session_cleanup_rpc_handler (void *args)
1880 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1881 vcl_worker_t *wrk = vcl_worker_get_current ();
1882 vls_worker_t *vls_wrk = vls_worker_get_current ();
1883 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
1885 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1886 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
1888 vppcom_session_close (sh);
1889 vls_sh_to_vlsh_table_del (vls_wrk, sh);
1893 vls_rpc_handler (void *args)
1895 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1898 case VLS_RPC_CLONE_AND_SHARE:
1899 vls_clone_and_share_rpc_handler (msg->data);
1901 case VLS_RPC_SESS_CLEANUP:
1902 vls_session_cleanup_rpc_handler (msg->data);
1910 vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1911 u32 session_index, u32 vls_wrk_index,
1912 u32 dst_wrk_index, u32 dst_vls_index,
1913 u32 dst_session_index)
1915 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1916 vls_clone_and_share_msg_t *msg;
1919 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
1921 rpc = (vls_rpc_msg_t *) & data;
1922 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1923 msg = (vls_clone_and_share_msg_t *) & rpc->data;
1924 msg->origin_vls_wrk = vls_wrk_index;
1925 msg->origin_vls_index = origin_vls_index;
1926 msg->origin_vcl_wrk = wrk->wrk_index;
1927 msg->origin_session_index = session_index;
1928 msg->vls_index = dst_vls_index;
1929 msg->session_index = dst_session_index;
1931 /* Try lock and handle rpcs if two threads send each other
1932 * clone requests at the same time.
1934 wrk->rpc_done = VLS_RPC_STATE_INIT;
1935 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1936 vcl_flush_mq_events ();
1937 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1939 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
1940 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1941 msg->origin_session_index, ret);
1942 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1943 clib_time_now (&wrk->clib_time) < timeout)
1945 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
1949 vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1950 u32 dst_wrk_index, u32 dst_session_index)
1952 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1953 vls_sess_cleanup_msg_t *msg;
1957 rpc = (vls_rpc_msg_t *) & data;
1958 rpc->type = VLS_RPC_SESS_CLEANUP;
1959 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1960 msg->origin_vcl_wrk = wrk->wrk_index;
1961 msg->session_index = dst_session_index;
1963 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1965 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
1966 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
1970 vls_app_create (char *app_name)
1974 if ((rv = vppcom_app_create (app_name)))
1977 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1978 clib_memset (vlsm, 0, sizeof (*vlsm));
1979 clib_rwlock_init (&vlsm->shared_data_lock);
1980 clib_spinlock_init (&vlsm->worker_rpc_lock);
1981 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1983 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1984 vls_app_fork_child_handler);
1985 atexit (vls_app_exit);
1986 vls_worker_alloc ();
1987 vlsl->vls_wrk_index = vcl_get_worker_index ();
1988 clib_rwlock_init (&vlsl->vls_pool_lock);
1989 vls_mt_locks_init ();
1990 vcm->wrk_rpc_fn = vls_rpc_handler;
1995 vls_use_eventfd (void)
1997 return vcm->cfg.use_mq_eventfd;
2001 vls_mt_wrk_supported (void)
2003 return vcm->cfg.mt_wrk_supported;
2007 vls_use_real_epoll (void)
2009 if (vcl_get_worker_index () == ~0)
2012 return vcl_worker_get_current ()->vcl_needs_real_epoll;
2016 vls_register_vcl_worker (void)
2022 * fd.io coding-style-patch-verification: ON
2025 * eval: (c-set-style "gnu")