2 * Copyright (c) 2019 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.
16 #include <vcl/vcl_locked.h>
17 #include <vcl/vcl_private.h>
19 typedef struct vls_shared_data_
23 u32 *workers_subscribed;
24 clib_bitmap_t *listeners;
27 typedef struct vcl_locked_session_
33 u32 shared_data_index;
34 /** VCL session owned by different workers because of migration */
35 u32 owner_vcl_wrk_index;
36 uword *vcl_wrk_index_to_session_index;
37 } vcl_locked_session_t;
39 typedef struct vls_worker_
41 clib_rwlock_t sh_to_vlsh_table_lock; /** valid for multithread workers */
42 vcl_locked_session_t *vls_pool;
43 uword *session_handle_to_vlsh_table;
45 /** Vector of child wrk to cleanup */
46 u32 *pending_wrk_cleanup;
49 typedef struct vls_local_
52 volatile int vls_mt_n_threads;
53 pthread_mutex_t vls_mt_mq_mlock;
54 pthread_mutex_t vls_mt_spool_mlock;
55 volatile u8 select_mp_check;
56 } vls_process_local_t;
58 static vls_process_local_t vls_local;
59 static vls_process_local_t *vlsl = &vls_local;
61 typedef struct vls_main_
63 vls_worker_t *workers;
64 clib_rwlock_t vls_table_lock;
65 /** Pool of data shared by sessions owned by different workers */
66 vls_shared_data_t *shared_data_pool;
67 clib_rwlock_t shared_data_lock;
68 /** Lock to protect rpc among workers */
69 clib_spinlock_t worker_rpc_lock;
77 VLS_RPC_STATE_SUCCESS,
78 VLS_RPC_STATE_SESSION_NOT_EXIST,
81 typedef enum vls_rpc_msg_type_
83 VLS_RPC_CLONE_AND_SHARE,
87 typedef struct vls_rpc_msg_
93 typedef struct vls_clone_and_share_msg_
95 u32 vls_index; /**< vls to be shared */
96 u32 session_index; /**< vcl session to be shared */
97 u32 origin_vls_wrk; /**< vls worker that initiated the rpc */
98 u32 origin_vls_index; /**< vls session of the originator */
99 u32 origin_vcl_wrk; /**< vcl worker that initiated the rpc */
100 u32 origin_session_index; /**< vcl session of the originator */
101 } vls_clone_and_share_msg_t;
103 typedef struct vls_sess_cleanup_msg_
105 u32 session_index; /**< vcl session to be cleaned */
106 u32 origin_vcl_wrk; /**< worker that initiated the rpc */
107 } vls_sess_cleanup_msg_t;
109 void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
110 u32 dst_wrk_index, u32 dst_session_index);
111 void vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
112 u32 session_index, u32 vls_wrk_index,
113 u32 dst_wrk_index, u32 dst_vls_index,
114 u32 dst_session_index);
115 static void vls_cleanup_forked_child (vcl_worker_t *wrk,
116 vcl_worker_t *child_wrk);
117 static void vls_handle_pending_wrk_cleanup (void);
120 vls_get_worker_index (void)
122 if (vls_mt_wrk_supported ())
123 return vlsl->vls_wrk_index;
125 return vcl_get_worker_index ();
129 vls_shared_data_alloc (void)
131 vls_shared_data_t *vls_shd;
134 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
135 pool_get_zero (vlsm->shared_data_pool, vls_shd);
136 clib_spinlock_init (&vls_shd->lock);
137 shd_index = vls_shd - vlsm->shared_data_pool;
138 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
144 vls_shared_data_index (vls_shared_data_t * vls_shd)
146 return vls_shd - vlsm->shared_data_pool;
150 vls_shared_data_get (u32 shd_index)
152 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
154 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
158 vls_shared_data_free (u32 shd_index)
160 vls_shared_data_t *vls_shd;
162 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
163 vls_shd = vls_shared_data_get (shd_index);
164 clib_spinlock_free (&vls_shd->lock);
165 clib_bitmap_free (vls_shd->listeners);
166 vec_free (vls_shd->workers_subscribed);
167 pool_put (vlsm->shared_data_pool, vls_shd);
168 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
172 vls_shared_data_pool_rlock (void)
174 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
178 vls_shared_data_pool_runlock (void)
180 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
184 vls_mt_table_rlock (void)
186 if (vlsl->vls_mt_n_threads > 1)
187 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
191 vls_mt_table_runlock (void)
193 if (vlsl->vls_mt_n_threads > 1)
194 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
198 vls_mt_table_wlock (void)
200 if (vlsl->vls_mt_n_threads > 1)
201 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
205 vls_mt_table_wunlock (void)
207 if (vlsl->vls_mt_n_threads > 1)
208 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
221 VLS_MT_LOCK_MQ = 1 << 0,
222 VLS_MT_LOCK_SPOOL = 1 << 1
223 } vls_mt_lock_type_t;
228 vlsl->vls_mt_n_threads += 1;
230 /* If multi-thread workers are supported, for each new thread register a new
231 * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
232 * update the vcl worker's thread local worker index variable */
233 if (vls_mt_wrk_supported ())
235 if (vppcom_worker_register () != VPPCOM_OK)
236 VERR ("failed to register worker");
239 vcl_set_worker_index (vlsl->vls_wrk_index);
243 vls_mt_mq_lock (void)
245 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
249 vls_mt_mq_unlock (void)
251 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
255 vls_mt_spool_lock (void)
257 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
261 vls_mt_create_unlock (void)
263 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
267 vls_mt_locks_init (void)
269 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
270 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
274 vls_is_shared (vcl_locked_session_t * vls)
276 return (vls->shared_data_index != ~0);
280 vls_lock (vcl_locked_session_t * vls)
282 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
283 clib_spinlock_lock (&vls->lock);
287 vls_unlock (vcl_locked_session_t * vls)
289 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
290 clib_spinlock_unlock (&vls->lock);
293 static inline vcl_session_handle_t
294 vls_to_sh (vcl_locked_session_t * vls)
296 return vcl_session_handle_from_index (vls->session_index);
299 static inline vcl_session_handle_t
300 vls_to_sh_tu (vcl_locked_session_t * vls)
302 vcl_session_handle_t sh;
303 sh = vls_to_sh (vls);
304 vls_mt_table_runlock ();
308 static vls_worker_t *
309 vls_worker_get_current (void)
311 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
315 vls_worker_alloc (void)
319 pool_get_zero (vlsm->workers, wrk);
320 if (vls_mt_wrk_supported ())
321 clib_rwlock_init (&wrk->sh_to_vlsh_table_lock);
322 wrk->wrk_index = vcl_get_worker_index ();
323 vec_validate (wrk->pending_wrk_cleanup, 16);
324 vec_reset_length (wrk->pending_wrk_cleanup);
328 vls_worker_free (vls_worker_t * wrk)
330 hash_free (wrk->session_handle_to_vlsh_table);
331 if (vls_mt_wrk_supported ())
332 clib_rwlock_free (&wrk->sh_to_vlsh_table_lock);
333 pool_free (wrk->vls_pool);
334 pool_put (vlsm->workers, wrk);
337 static vls_worker_t *
338 vls_worker_get (u32 wrk_index)
340 if (pool_is_free_index (vlsm->workers, wrk_index))
342 return pool_elt_at_index (vlsm->workers, wrk_index);
346 vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
348 if (vls_mt_wrk_supported ())
349 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
350 hash_set (wrk->session_handle_to_vlsh_table, sh, vlsh);
351 if (vls_mt_wrk_supported ())
352 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
356 vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
358 if (vls_mt_wrk_supported ())
359 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
360 hash_unset (wrk->session_handle_to_vlsh_table, sh);
361 if (vls_mt_wrk_supported ())
362 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
366 vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
368 if (vls_mt_wrk_supported ())
369 clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock);
370 uword *vlshp = hash_get (wrk->session_handle_to_vlsh_table, sh);
371 if (vls_mt_wrk_supported ())
372 clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock);
377 vls_alloc (vcl_session_handle_t sh)
379 vls_worker_t *wrk = vls_worker_get_current ();
380 vcl_locked_session_t *vls;
382 vls_mt_table_wlock ();
384 pool_get_zero (wrk->vls_pool, vls);
385 vls->session_index = vppcom_session_index (sh);
386 vls->worker_index = vppcom_session_worker (sh);
387 vls->vls_index = vls - wrk->vls_pool;
388 vls->shared_data_index = ~0;
389 vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index);
390 if (vls_mt_wrk_supported ())
392 hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
394 vls->owner_vcl_wrk_index = vls->worker_index;
396 clib_spinlock_init (&vls->lock);
398 vls_mt_table_wunlock ();
399 return vls->vls_index;
402 static vcl_locked_session_t *
403 vls_get (vls_handle_t vlsh)
405 vls_worker_t *wrk = vls_worker_get_current ();
406 if (pool_is_free_index (wrk->vls_pool, vlsh))
408 return pool_elt_at_index (wrk->vls_pool, vlsh);
412 vls_free (vcl_locked_session_t * vls)
414 vls_worker_t *wrk = vls_worker_get_current ();
417 vls_sh_to_vlsh_table_del (
418 wrk, vcl_session_handle_from_index (vls->session_index));
419 clib_spinlock_free (&vls->lock);
420 pool_put (wrk->vls_pool, vls);
423 static vcl_locked_session_t *
424 vls_get_and_lock (vls_handle_t vlsh)
426 vls_worker_t *wrk = vls_worker_get_current ();
427 vcl_locked_session_t *vls;
428 if (pool_is_free_index (wrk->vls_pool, vlsh))
430 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
435 static vcl_locked_session_t *
436 vls_get_w_dlock (vls_handle_t vlsh)
438 vcl_locked_session_t *vls;
439 vls_mt_table_rlock ();
440 vls = vls_get_and_lock (vlsh);
442 vls_mt_table_runlock ();
447 vls_get_and_unlock (vls_handle_t vlsh)
449 vcl_locked_session_t *vls;
450 vls_mt_table_rlock ();
451 vls = vls_get (vlsh);
453 vls_mt_table_runlock ();
457 vls_dunlock (vcl_locked_session_t * vls)
460 vls_mt_table_runlock ();
463 static vcl_locked_session_t *
464 vls_session_get (vls_worker_t * wrk, u32 vls_index)
466 if (pool_is_free_index (wrk->vls_pool, vls_index))
468 return pool_elt_at_index (wrk->vls_pool, vls_index);
472 vlsh_to_sh (vls_handle_t vlsh)
474 vcl_locked_session_t *vls;
477 vls = vls_get_w_dlock (vlsh);
479 return INVALID_SESSION_ID;
480 rv = vls_to_sh (vls);
486 vlsh_to_session_index (vls_handle_t vlsh)
488 vcl_session_handle_t sh;
489 sh = vlsh_to_sh (vlsh);
490 return vppcom_session_index (sh);
494 vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
496 vls_worker_t *wrk = vls_worker_get_current ();
497 uword *vlshp = vls_sh_to_vlsh_table_get (
499 vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
501 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
505 vls_session_index_to_vlsh (uint32_t session_index)
509 vls_mt_table_rlock ();
510 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
511 vls_mt_table_runlock ();
517 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
519 vls_shared_data_t *vls_shd;
522 if (vls->shared_data_index == ~0)
525 vls_shared_data_pool_rlock ();
527 vls_shd = vls_shared_data_get (vls->shared_data_index);
528 clib_spinlock_lock (&vls_shd->lock);
530 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
531 if (vls_shd->workers_subscribed[i] == wrk_index)
533 clib_spinlock_unlock (&vls_shd->lock);
534 vls_shared_data_pool_runlock ();
537 clib_spinlock_unlock (&vls_shd->lock);
539 vls_shared_data_pool_runlock ();
544 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
546 vls_shared_data_t *vls_shd;
548 if (vls->shared_data_index == ~0)
550 clib_warning ("not a shared session");
554 vls_shared_data_pool_rlock ();
556 vls_shd = vls_shared_data_get (vls->shared_data_index);
558 clib_spinlock_lock (&vls_shd->lock);
559 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
560 clib_spinlock_unlock (&vls_shd->lock);
562 vls_shared_data_pool_runlock ();
566 vls_shared_get_owner (vcl_locked_session_t * vls)
568 vls_shared_data_t *vls_shd;
571 vls_shared_data_pool_rlock ();
573 vls_shd = vls_shared_data_get (vls->shared_data_index);
574 owner_wrk = vls_shd->owner_wrk_index;
576 vls_shared_data_pool_runlock ();
582 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
584 vls_shared_data_t *vls_shd;
587 if (vls->shared_data_index == ~0)
589 clib_warning ("not a shared session");
593 vls_shared_data_pool_rlock ();
595 vls_shd = vls_shared_data_get (vls->shared_data_index);
597 clib_spinlock_lock (&vls_shd->lock);
598 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
599 clib_spinlock_unlock (&vls_shd->lock);
601 vls_shared_data_pool_runlock ();
603 return (is_set == 1);
607 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
609 vppcom_session_listen (vls_to_sh (vls), ~0);
610 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
614 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
619 wrk = vcl_worker_get (wrk_index);
620 s = vcl_session_get (wrk, vls->session_index);
621 if (s->session_state != VCL_STATE_LISTEN)
623 vcl_send_session_unlisten (wrk, s);
624 s->session_state = VCL_STATE_LISTEN_NO_MQ;
625 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
629 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
634 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
636 if (vls_shd->workers_subscribed[i] == wrk_index)
643 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
645 vls_shared_data_t *vls_shd;
646 int do_disconnect, pos;
650 if (vls->shared_data_index == ~0)
653 s = vcl_session_get (wrk, vls->session_index);
654 if (s->session_state == VCL_STATE_LISTEN)
655 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
657 vls_shared_data_pool_rlock ();
659 vls_shd = vls_shared_data_get (vls->shared_data_index);
660 clib_spinlock_lock (&vls_shd->lock);
662 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
665 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
671 * Unsubscribe from share data and fifos
675 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
676 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
678 vec_del1 (vls_shd->workers_subscribed, pos);
683 n_subscribers = vec_len (vls_shd->workers_subscribed);
684 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
685 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
688 * No subscriber left, cleanup shared data
692 u32 shd_index = vls_shared_data_index (vls_shd);
694 clib_spinlock_unlock (&vls_shd->lock);
695 vls_shared_data_pool_runlock ();
697 vls_shared_data_free (shd_index);
699 /* All locks have been dropped */
703 /* Return, if this is not the owning worker */
704 if (vls_shd->owner_wrk_index != wrk->wrk_index)
707 ASSERT (vec_len (vls_shd->workers_subscribed));
710 * Check if we can change owner or close
712 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
713 if (s->session_state != VCL_STATE_LISTEN_NO_MQ)
714 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
716 /* XXX is this still needed? */
717 if (vec_len (vls_shd->workers_subscribed) > 1)
718 clib_warning ("more workers need to be updated");
722 clib_spinlock_unlock (&vls_shd->lock);
723 vls_shared_data_pool_runlock ();
729 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
731 vls_shared_data_t *vls_shd;
733 u32 vls_shd_index = vls_shared_data_alloc ();
735 vls_shared_data_pool_rlock ();
737 vls_shd = vls_shared_data_get (vls_shd_index);
738 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
739 vls->shared_data_index = vls_shd_index;
740 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
742 vls_shared_data_pool_runlock ();
746 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
748 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
749 vls_shared_data_t *vls_shd;
752 s = vcl_session_get (vcl_wrk, vls->session_index);
755 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
756 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
760 ASSERT (vls->shared_data_index != ~0);
762 /* Reinit session lock */
763 clib_spinlock_init (&vls->lock);
765 vls_shared_data_pool_rlock ();
767 vls_shd = vls_shared_data_get (vls->shared_data_index);
769 clib_spinlock_lock (&vls_shd->lock);
770 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
771 clib_spinlock_unlock (&vls_shd->lock);
773 vls_shared_data_pool_runlock ();
777 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
779 else if (s->session_state == VCL_STATE_LISTEN)
781 s->session_state = VCL_STATE_LISTEN_NO_MQ;
786 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
788 vcl_locked_session_t *vls, *parent_vls;
791 pool_foreach (vls, vls_wrk->vls_pool) {
792 /* Initialize sharing on parent session */
793 if (vls->shared_data_index == ~0)
795 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
796 vls_init_share_session (vls_parent_wrk, parent_vls);
797 vls->shared_data_index = parent_vls->shared_data_index;
799 vls_share_session (vls_wrk, vls);
805 vls_validate_veps (vcl_worker_t *wrk)
808 u32 session_index, wrk_index;
810 pool_foreach (s, wrk->sessions)
812 if (s->vep.vep_sh != ~0)
814 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
815 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
817 if (s->vep.next_sh != ~0)
819 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
821 s->vep.next_sh = vcl_session_handle_from_index (session_index);
823 if (s->vep.prev_sh != ~0)
825 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
827 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
833 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
835 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
836 vcl_worker_t *wrk = vcl_worker_get_current ();
837 u32 vls_index, session_index, wrk_index;
838 vcl_session_handle_t sh;
843 wrk->sessions = pool_dup (parent_wrk->sessions);
844 wrk->session_index_by_vpp_handles =
845 hash_dup (parent_wrk->session_index_by_vpp_handles);
850 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
852 hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
854 vcl_session_handle_parse (sh, &wrk_index, &session_index);
855 hash_set (vls_wrk->session_handle_to_vlsh_table,
856 vcl_session_handle_from_index (session_index), vls_index);
859 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
861 /* Validate vep's handle */
862 vls_validate_veps (wrk);
864 vls_share_sessions (vls_parent_wrk, vls_wrk);
868 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
870 vcl_worker_t *wrk = vcl_worker_get_current ();
871 vcl_session_t *s = 0;
876 s = vcl_session_get (wrk, vls->session_index);
877 if (PREDICT_FALSE (!s))
879 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
886 is_nonblk = vcl_session_read_ready (s) != 0;
890 *locks_acq |= VLS_MT_LOCK_MQ;
893 case VLS_MT_OP_WRITE:
896 is_nonblk = vcl_session_write_ready (s) != 0;
900 *locks_acq |= VLS_MT_LOCK_MQ;
903 case VLS_MT_OP_XPOLL:
905 *locks_acq |= VLS_MT_LOCK_MQ;
907 case VLS_MT_OP_SPOOL:
908 vls_mt_spool_lock ();
909 *locks_acq |= VLS_MT_LOCK_SPOOL;
917 vls_mt_rel_locks (int locks_acq)
919 if (locks_acq & VLS_MT_LOCK_MQ)
921 if (locks_acq & VLS_MT_LOCK_SPOOL)
922 vls_mt_create_unlock ();
926 vls_mt_session_should_migrate (vcl_locked_session_t * vls)
928 return (vls_mt_wrk_supported ()
929 && vls->worker_index != vcl_get_worker_index ());
932 static vcl_locked_session_t *
933 vls_mt_session_migrate (vcl_locked_session_t *vls)
935 u32 wrk_index = vcl_get_worker_index ();
937 vls_worker_t *vls_wrk = vls_worker_get_current ();
938 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
939 vcl_session_t *session;
942 ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
945 * VCL session on current vcl worker already allocated. Update current
946 * owner worker and index and return
948 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
950 vls->worker_index = wrk_index;
951 vls->session_index = (u32) p[0];
956 * Ask vcl worker that owns the original vcl session to clone it into
957 * current vcl worker session pool
960 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
961 vls->owner_vcl_wrk_index)))
963 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
966 vls_mt_table_runlock ();
970 src_sid = (u32) p[0];
971 wrk = vcl_worker_get_current ();
972 session = vcl_session_alloc (wrk);
973 sid = session->session_index;
974 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
975 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
977 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
978 vls_index = vls->vls_index;
979 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
981 vls_mt_table_runlock ();
982 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
983 own_vcl_wrk_index, vls_index, src_sid);
985 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
987 VWRN ("session %u not exist", src_sid);
990 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
992 VWRN ("failed to wait rpc response");
995 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
996 session->vep.next_sh != ~0))
998 VERR ("can't migrate nonempty epoll session");
1002 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
1003 session->session_state != VCL_STATE_CLOSED))
1005 VERR ("migrate NOT supported, session_status (%u)",
1006 session->session_state);
1011 vls = vls_get_w_dlock (vls_index);
1012 if (PREDICT_FALSE (!vls))
1014 VWRN ("failed to get vls %u", vls_index);
1018 session->session_index = sid;
1019 vls->worker_index = wrk_index;
1020 vls->session_index = sid;
1021 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
1022 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1027 vcl_session_free (wrk, session);
1032 vls_mt_detect (void)
1034 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1038 #define vls_mt_guard(_vls, _op) \
1039 int _locks_acq = 0; \
1040 if (vls_mt_wrk_supported ()) \
1042 if (PREDICT_FALSE (_vls && \
1043 ((vcl_locked_session_t *) _vls)->worker_index != \
1044 vcl_get_worker_index ())) \
1046 _vls = vls_mt_session_migrate (_vls); \
1047 if (PREDICT_FALSE (!_vls)) \
1048 return VPPCOM_EBADFD; \
1053 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1054 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1057 #define vls_mt_unguard() \
1058 if (PREDICT_FALSE (_locks_acq)) \
1059 vls_mt_rel_locks (_locks_acq)
1062 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1064 vcl_locked_session_t *vls;
1068 if (!(vls = vls_get_w_dlock (vlsh)))
1069 return VPPCOM_EBADFD;
1071 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1072 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
1074 vls_get_and_unlock (vlsh);
1079 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1081 vcl_locked_session_t *vls;
1085 if (!(vls = vls_get_w_dlock (vlsh)))
1086 return VPPCOM_EBADFD;
1087 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1088 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
1090 vls_get_and_unlock (vlsh);
1095 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1096 vppcom_endpt_t * ep)
1098 vcl_locked_session_t *vls;
1102 if (!(vls = vls_get_w_dlock (vlsh)))
1103 return VPPCOM_EBADFD;
1104 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1105 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
1107 vls_get_and_unlock (vlsh);
1112 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1114 vcl_locked_session_t *vls;
1118 if (!(vls = vls_get_w_dlock (vlsh)))
1119 return VPPCOM_EBADFD;
1120 vls_mt_guard (vls, VLS_MT_OP_READ);
1121 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
1123 vls_get_and_unlock (vlsh);
1128 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1129 vppcom_endpt_t * ep)
1131 vcl_locked_session_t *vls;
1135 if (!(vls = vls_get_w_dlock (vlsh)))
1136 return VPPCOM_EBADFD;
1137 vls_mt_guard (vls, VLS_MT_OP_READ);
1138 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1141 vls_get_and_unlock (vlsh);
1146 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1148 vcl_locked_session_t *vls;
1152 if (!(vls = vls_get_w_dlock (vlsh)))
1153 return VPPCOM_EBADFD;
1154 if (vls_mt_session_should_migrate (vls))
1156 vls = vls_mt_session_migrate (vls);
1157 if (PREDICT_FALSE (!vls))
1158 return VPPCOM_EBADFD;
1160 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1161 vls_get_and_unlock (vlsh);
1166 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1168 vcl_locked_session_t *vls;
1172 if (!(vls = vls_get_w_dlock (vlsh)))
1173 return VPPCOM_EBADFD;
1174 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1175 vls_get_and_unlock (vlsh);
1180 vls_listen (vls_handle_t vlsh, int q_len)
1182 vcl_locked_session_t *vls;
1186 if (!(vls = vls_get_w_dlock (vlsh)))
1187 return VPPCOM_EBADFD;
1188 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1189 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
1191 vls_get_and_unlock (vlsh);
1196 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1198 vcl_locked_session_t *vls;
1202 if (!(vls = vls_get_w_dlock (vlsh)))
1203 return VPPCOM_EBADFD;
1204 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1205 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
1207 vls_get_and_unlock (vlsh);
1212 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1214 vcl_worker_t *wrk = vcl_worker_get_current ();
1218 if (vls_mt_wrk_supported ())
1221 s = vcl_session_get (wrk, vls->session_index);
1222 switch (s->session_state)
1224 case VCL_STATE_LISTEN:
1227 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
1230 vls_listener_wrk_stop_listen (vls, vls->worker_index);
1232 case VCL_STATE_LISTEN_NO_MQ:
1236 /* Register worker as listener */
1237 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
1239 /* If owner worker did not attempt to accept/xpoll on the session,
1240 * force a listen stop for it, since it may not be interested in
1241 * accepting new sessions.
1242 * This is pretty much a hack done to give app workers the illusion
1243 * that it is fine to listen and not accept new sessions for a
1244 * given listener. Without it, we would accumulate unhandled
1245 * accepts on the passive worker message queue. */
1246 owner_wrk = vls_shared_get_owner (vls);
1247 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1248 vls_listener_wrk_stop_listen (vls, owner_wrk);
1256 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1258 vls_handle_t accepted_vlsh;
1259 vcl_locked_session_t *vls;
1263 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1264 return VPPCOM_EBADFD;
1265 if (vcl_n_workers () > 1)
1266 vls_mp_checks (vls, 1 /* is_add */ );
1267 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1268 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
1270 vls_get_and_unlock (listener_vlsh);
1273 accepted_vlsh = vls_alloc (sh);
1274 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1275 vppcom_session_close (sh);
1276 return accepted_vlsh;
1280 vls_create (uint8_t proto, uint8_t is_nonblocking)
1282 vcl_session_handle_t sh;
1284 vcl_locked_session_t *vls = NULL;
1287 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1288 sh = vppcom_session_create (proto, is_nonblocking);
1290 if (sh == INVALID_SESSION_ID)
1291 return VLS_INVALID_HANDLE;
1293 vlsh = vls_alloc (sh);
1294 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1295 vppcom_session_close (sh);
1301 vls_mt_session_cleanup (vcl_locked_session_t * vls)
1303 u32 session_index, wrk_index, current_vcl_wrk;
1304 vcl_worker_t *wrk = vcl_worker_get_current ();
1306 ASSERT (vls_mt_wrk_supported ());
1308 current_vcl_wrk = vcl_get_worker_index ();
1311 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1313 if (current_vcl_wrk != wrk_index)
1314 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
1317 hash_free (vls->vcl_wrk_index_to_session_index);
1321 vls_close (vls_handle_t vlsh)
1323 vcl_locked_session_t *vls;
1327 vls_mt_table_wlock ();
1329 vls = vls_get_and_lock (vlsh);
1332 vls_mt_table_wunlock ();
1333 return VPPCOM_EBADFD;
1336 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1338 if (vls_is_shared (vls))
1339 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1341 rv = vppcom_session_close (vls_to_sh (vls));
1343 if (vls_mt_wrk_supported ())
1344 vls_mt_session_cleanup (vls);
1349 vls_mt_table_wunlock ();
1355 vls_shutdown (vls_handle_t vlsh, int how)
1357 vcl_locked_session_t *vls;
1361 if (!(vls = vls_get_w_dlock (vlsh)))
1362 return VPPCOM_EBADFD;
1364 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1365 rv = vppcom_session_shutdown (vls_to_sh (vls), how);
1367 vls_get_and_unlock (vlsh);
1373 vls_epoll_create (void)
1375 vcl_session_handle_t sh;
1380 sh = vppcom_epoll_create ();
1381 if (sh == INVALID_SESSION_ID)
1382 return VLS_INVALID_HANDLE;
1384 vlsh = vls_alloc (sh);
1385 if (vlsh == VLS_INVALID_HANDLE)
1386 vppcom_session_close (sh);
1392 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1394 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
1397 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1401 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1402 struct epoll_event *event)
1404 vcl_locked_session_t *ep_vls, *vls;
1405 vcl_session_handle_t ep_sh, sh;
1409 vls_mt_table_rlock ();
1410 ep_vls = vls_get_and_lock (ep_vlsh);
1412 if (vls_mt_session_should_migrate (ep_vls))
1414 ep_vls = vls_mt_session_migrate (ep_vls);
1415 if (PREDICT_FALSE (!ep_vls))
1416 return VPPCOM_EBADFD;
1419 ep_sh = vls_to_sh (ep_vls);
1420 vls = vls_get_and_lock (vlsh);
1421 sh = vls_to_sh (vls);
1423 vls_epoll_ctl_mp_checks (vls, op);
1424 vls_mt_table_runlock ();
1425 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1427 vls_mt_table_rlock ();
1428 ep_vls = vls_get (ep_vlsh);
1429 vls = vls_get (vlsh);
1431 vls_unlock (ep_vls);
1432 vls_mt_table_runlock ();
1437 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1438 int maxevents, double wait_for_time)
1440 vcl_locked_session_t *vls, *vls_tmp = NULL;
1444 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1445 return VPPCOM_EBADFD;
1446 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
1447 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1450 vls_get_and_unlock (ep_vlsh);
1451 vls_handle_pending_wrk_cleanup ();
1456 vls_select_mp_checks (vcl_si_set * read_map)
1458 vcl_locked_session_t *vls;
1463 if (vcl_n_workers () <= 1)
1465 vlsl->select_mp_check = 1;
1472 vlsl->select_mp_check = 1;
1473 wrk = vcl_worker_get_current ();
1476 clib_bitmap_foreach (si, read_map) {
1477 s = vcl_session_get (wrk, si);
1478 if (s->session_state == VCL_STATE_LISTEN)
1480 vls = vls_get (vls_session_index_to_vlsh (si));
1481 vls_mp_checks (vls, 1 /* is_add */);
1488 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1489 vcl_si_set * except_map, double wait_for_time)
1492 vcl_locked_session_t *vls = NULL;
1495 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1496 if (PREDICT_FALSE (!vlsl->select_mp_check))
1497 vls_select_mp_checks (read_map);
1498 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1500 vls_handle_pending_wrk_cleanup ();
1505 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1507 u32 current_wrk, is_current;
1508 vcl_locked_session_t *vls;
1511 if (pool_elts (vcm->workers) <= 1)
1514 current_wrk = vcl_get_worker_index ();
1515 is_current = current_wrk == wrk->wrk_index;
1518 pool_foreach (s, wrk->sessions) {
1519 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
1520 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1521 vls_unshare_session (vls, wrk);
1527 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1529 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1531 /* Unshare sessions and also cleanup worker since child may have
1532 * called _exit () and therefore vcl may not catch the event */
1533 vls_unshare_vcl_worker_sessions (wrk);
1535 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1536 * may have been closed, so DONOT notify VPP.
1538 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
1540 vls_worker_free (vls_wrk);
1544 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1546 vcl_worker_t *sub_child;
1549 if (child_wrk->forked_child != ~0)
1551 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1554 /* Wait a bit, maybe the process is going away */
1555 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1557 if (kill (sub_child->current_pid, 0) < 0)
1558 vls_cleanup_forked_child (child_wrk, sub_child);
1561 vls_cleanup_vcl_worker (child_wrk);
1562 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1563 wrk->forked_child = ~0;
1567 vls_handle_pending_wrk_cleanup (void)
1570 vcl_worker_t *child_wrk, *wrk;
1571 vls_worker_t *vls_wrk = vls_worker_get_current ();
1573 if (PREDICT_TRUE (vec_len (vls_wrk->pending_wrk_cleanup) == 0))
1576 wrk = vcl_worker_get_current ();
1577 vec_foreach (wip, vls_wrk->pending_wrk_cleanup)
1579 child_wrk = vcl_worker_get_if_valid (*wip);
1582 vls_cleanup_forked_child (wrk, child_wrk);
1584 vec_reset_length (vls_wrk->pending_wrk_cleanup);
1587 static struct sigaction old_sa;
1590 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1592 vcl_worker_t *wrk, *child_wrk;
1593 vls_worker_t *vls_wrk;
1595 if (vcl_get_worker_index () == ~0)
1598 if (sigaction (SIGCHLD, &old_sa, 0))
1600 VERR ("couldn't restore sigchld");
1604 wrk = vcl_worker_get_current ();
1605 if (wrk->forked_child == ~0)
1608 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1612 if (si && si->si_pid != child_wrk->current_pid)
1614 VDBG (0, "unexpected child pid %u", si->si_pid);
1618 /* Parent process may enter sighandler with a lock, such as lock in localtime
1619 * or in mspace_free, and child wrk cleanup may try to get such locks and
1621 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1623 vls_wrk = vls_worker_get_current ();
1624 vec_add1 (vls_wrk->pending_wrk_cleanup, child_wrk->wrk_index);
1627 if (old_sa.sa_flags & SA_SIGINFO)
1629 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1630 fn (signum, si, uc);
1634 void (*fn) (int) = old_sa.sa_handler;
1641 vls_incercept_sigchld ()
1643 struct sigaction sa;
1644 if (old_sa.sa_sigaction)
1646 VDBG (0, "have intercepted sigchld");
1649 clib_memset (&sa, 0, sizeof (sa));
1650 sa.sa_sigaction = vls_intercept_sigchld_handler;
1651 sa.sa_flags = SA_SIGINFO;
1652 if (sigaction (SIGCHLD, &sa, &old_sa))
1654 VERR ("couldn't intercept sigchld");
1660 vls_app_pre_fork (void)
1662 vls_incercept_sigchld ();
1663 vcl_flush_mq_events ();
1667 vls_app_fork_child_handler (void)
1669 vcl_worker_t *parent_wrk;
1670 int parent_wrk_index;
1672 parent_wrk_index = vcl_get_worker_index ();
1673 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1679 vcl_set_worker_index (~0);
1682 * Allocate and register vcl worker with vpp
1684 if (vppcom_worker_register ())
1686 VERR ("couldn't register new worker!");
1691 * Allocate/initialize vls worker and share sessions
1693 vls_worker_alloc ();
1694 parent_wrk = vcl_worker_get (parent_wrk_index);
1695 vls_worker_copy_on_fork (parent_wrk);
1696 parent_wrk->forked_child = vcl_get_worker_index ();
1698 /* Reset number of threads and set wrk index */
1699 vlsl->vls_mt_n_threads = 0;
1700 vlsl->vls_wrk_index = vcl_get_worker_index ();
1701 vlsl->select_mp_check = 0;
1702 vls_mt_locks_init ();
1704 VDBG (0, "forked child main worker initialized");
1709 vls_app_fork_parent_handler (void)
1712 while (vcm->forking)
1719 vls_worker_t *wrk = vls_worker_get_current ();
1721 /* Handle pending wrk cleanup */
1722 vls_handle_pending_wrk_cleanup ();
1724 /* Unshare the sessions. VCL will clean up the worker */
1725 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1726 vls_worker_free (wrk);
1730 vls_clone_and_share_rpc_handler (void *args)
1732 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1733 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1734 vcl_locked_session_t *vls, *dst_vls;
1735 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
1736 vcl_session_t *s, *dst_s;
1738 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1739 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1740 msg->origin_session_index);
1742 /* VCL locked session can't been protected, so DONT touch it.
1743 * VCL session may been free, check it.
1745 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1746 s = vcl_session_get (vcl_wrk, msg->session_index);
1747 if (PREDICT_FALSE (!s))
1749 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1753 if (!vls_mt_wrk_supported ())
1755 vls = vls_session_get (wrk, msg->vls_index);
1756 vls_init_share_session (wrk, vls);
1757 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1758 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1759 dst_vls->shared_data_index = vls->shared_data_index;
1761 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
1762 clib_memcpy (dst_s, s, sizeof (*s));
1764 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
1768 vls_session_cleanup_rpc_handler (void *args)
1770 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1771 vcl_worker_t *wrk = vcl_worker_get_current ();
1772 vls_worker_t *vls_wrk = vls_worker_get_current ();
1773 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
1775 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1776 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
1778 vppcom_session_close (sh);
1779 vls_sh_to_vlsh_table_del (vls_wrk, sh);
1783 vls_rpc_handler (void *args)
1785 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1788 case VLS_RPC_CLONE_AND_SHARE:
1789 vls_clone_and_share_rpc_handler (msg->data);
1791 case VLS_RPC_SESS_CLEANUP:
1792 vls_session_cleanup_rpc_handler (msg->data);
1800 vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1801 u32 session_index, u32 vls_wrk_index,
1802 u32 dst_wrk_index, u32 dst_vls_index,
1803 u32 dst_session_index)
1805 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1806 vls_clone_and_share_msg_t *msg;
1809 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
1811 rpc = (vls_rpc_msg_t *) & data;
1812 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1813 msg = (vls_clone_and_share_msg_t *) & rpc->data;
1814 msg->origin_vls_wrk = vls_wrk_index;
1815 msg->origin_vls_index = origin_vls_index;
1816 msg->origin_vcl_wrk = wrk->wrk_index;
1817 msg->origin_session_index = session_index;
1818 msg->vls_index = dst_vls_index;
1819 msg->session_index = dst_session_index;
1821 /* Try lock and handle rpcs if two threads send each other
1822 * clone requests at the same time.
1824 wrk->rpc_done = VLS_RPC_STATE_INIT;
1825 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1826 vcl_flush_mq_events ();
1827 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1829 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
1830 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1831 msg->origin_session_index, ret);
1832 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1833 clib_time_now (&wrk->clib_time) < timeout)
1835 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
1839 vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1840 u32 dst_wrk_index, u32 dst_session_index)
1842 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1843 vls_sess_cleanup_msg_t *msg;
1847 rpc = (vls_rpc_msg_t *) & data;
1848 rpc->type = VLS_RPC_SESS_CLEANUP;
1849 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1850 msg->origin_vcl_wrk = wrk->wrk_index;
1851 msg->session_index = dst_session_index;
1853 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1855 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
1856 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
1860 vls_app_create (char *app_name)
1864 if ((rv = vppcom_app_create (app_name)))
1867 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1868 clib_memset (vlsm, 0, sizeof (*vlsm));
1869 clib_rwlock_init (&vlsm->vls_table_lock);
1870 clib_rwlock_init (&vlsm->shared_data_lock);
1871 clib_spinlock_init (&vlsm->worker_rpc_lock);
1872 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1874 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1875 vls_app_fork_child_handler);
1876 atexit (vls_app_exit);
1877 vls_worker_alloc ();
1878 vlsl->vls_wrk_index = vcl_get_worker_index ();
1879 vls_mt_locks_init ();
1880 vcm->wrk_rpc_fn = vls_rpc_handler;
1885 vls_use_eventfd (void)
1887 return vcm->cfg.use_mq_eventfd;
1891 vls_mt_wrk_supported (void)
1893 return vcm->cfg.mt_wrk_supported;
1897 vls_use_real_epoll (void)
1899 if (vcl_get_worker_index () == ~0)
1902 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1906 vls_register_vcl_worker (void)
1912 * fd.io coding-style-patch-verification: ON
1915 * eval: (c-set-style "gnu")