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 return vlsl->vls_wrk_index;
126 vls_shared_data_alloc (void)
128 vls_shared_data_t *vls_shd;
131 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
132 pool_get_zero (vlsm->shared_data_pool, vls_shd);
133 clib_spinlock_init (&vls_shd->lock);
134 shd_index = vls_shd - vlsm->shared_data_pool;
135 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
141 vls_shared_data_index (vls_shared_data_t * vls_shd)
143 return vls_shd - vlsm->shared_data_pool;
147 vls_shared_data_get (u32 shd_index)
149 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
151 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
155 vls_shared_data_free (u32 shd_index)
157 vls_shared_data_t *vls_shd;
159 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
160 vls_shd = vls_shared_data_get (shd_index);
161 clib_spinlock_free (&vls_shd->lock);
162 clib_bitmap_free (vls_shd->listeners);
163 vec_free (vls_shd->workers_subscribed);
164 pool_put (vlsm->shared_data_pool, vls_shd);
165 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
169 vls_shared_data_pool_rlock (void)
171 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
175 vls_shared_data_pool_runlock (void)
177 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
181 vls_mt_table_rlock (void)
183 if (vlsl->vls_mt_n_threads > 1)
184 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
188 vls_mt_table_runlock (void)
190 if (vlsl->vls_mt_n_threads > 1)
191 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
195 vls_mt_table_wlock (void)
197 if (vlsl->vls_mt_n_threads > 1)
198 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
202 vls_mt_table_wunlock (void)
204 if (vlsl->vls_mt_n_threads > 1)
205 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
218 VLS_MT_LOCK_MQ = 1 << 0,
219 VLS_MT_LOCK_SPOOL = 1 << 1
220 } vls_mt_lock_type_t;
225 vlsl->vls_mt_n_threads += 1;
227 /* If multi-thread workers are supported, for each new thread register a new
228 * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
229 * update the vcl worker's thread local worker index variable */
230 if (vls_mt_wrk_supported ())
232 if (vppcom_worker_register () != VPPCOM_OK)
233 VERR ("failed to register worker");
236 vcl_set_worker_index (vlsl->vls_wrk_index);
240 vls_mt_mq_lock (void)
242 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
246 vls_mt_mq_unlock (void)
248 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
252 vls_mt_spool_lock (void)
254 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
258 vls_mt_create_unlock (void)
260 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
264 vls_mt_locks_init (void)
266 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
267 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
271 vls_is_shared (vcl_locked_session_t * vls)
273 return (vls->shared_data_index != ~0);
277 vls_lock (vcl_locked_session_t * vls)
279 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
280 clib_spinlock_lock (&vls->lock);
284 vls_unlock (vcl_locked_session_t * vls)
286 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
287 clib_spinlock_unlock (&vls->lock);
290 static inline vcl_session_handle_t
291 vls_to_sh (vcl_locked_session_t * vls)
293 return vcl_session_handle_from_index (vls->session_index);
296 static inline vcl_session_handle_t
297 vls_to_sh_tu (vcl_locked_session_t * vls)
299 vcl_session_handle_t sh;
300 sh = vls_to_sh (vls);
301 vls_mt_table_runlock ();
305 static vls_worker_t *
306 vls_worker_get_current (void)
308 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
312 vls_worker_alloc (void)
316 pool_get_zero (vlsm->workers, wrk);
317 if (vls_mt_wrk_supported ())
318 clib_rwlock_init (&wrk->sh_to_vlsh_table_lock);
319 wrk->wrk_index = vcl_get_worker_index ();
320 vec_validate (wrk->pending_wrk_cleanup, 16);
321 vec_reset_length (wrk->pending_wrk_cleanup);
325 vls_worker_free (vls_worker_t * wrk)
327 hash_free (wrk->session_handle_to_vlsh_table);
328 if (vls_mt_wrk_supported ())
329 clib_rwlock_free (&wrk->sh_to_vlsh_table_lock);
330 pool_free (wrk->vls_pool);
331 pool_put (vlsm->workers, wrk);
334 static vls_worker_t *
335 vls_worker_get (u32 wrk_index)
337 if (pool_is_free_index (vlsm->workers, wrk_index))
339 return pool_elt_at_index (vlsm->workers, wrk_index);
343 vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
345 if (vls_mt_wrk_supported ())
346 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
347 hash_set (wrk->session_handle_to_vlsh_table, sh, vlsh);
348 if (vls_mt_wrk_supported ())
349 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
353 vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
355 if (vls_mt_wrk_supported ())
356 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
357 hash_unset (wrk->session_handle_to_vlsh_table, sh);
358 if (vls_mt_wrk_supported ())
359 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
363 vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
365 if (vls_mt_wrk_supported ())
366 clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock);
367 uword *vlshp = hash_get (wrk->session_handle_to_vlsh_table, sh);
368 if (vls_mt_wrk_supported ())
369 clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock);
374 vls_alloc (vcl_session_handle_t sh)
376 vls_worker_t *wrk = vls_worker_get_current ();
377 vcl_locked_session_t *vls;
379 vls_mt_table_wlock ();
381 pool_get_zero (wrk->vls_pool, vls);
382 vls->session_index = vppcom_session_index (sh);
383 vls->worker_index = vppcom_session_worker (sh);
384 vls->vls_index = vls - wrk->vls_pool;
385 vls->shared_data_index = ~0;
386 vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index);
387 if (vls_mt_wrk_supported ())
389 hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
391 vls->owner_vcl_wrk_index = vls->worker_index;
393 clib_spinlock_init (&vls->lock);
395 vls_mt_table_wunlock ();
396 return vls->vls_index;
399 static vcl_locked_session_t *
400 vls_get (vls_handle_t vlsh)
402 vls_worker_t *wrk = vls_worker_get_current ();
403 if (pool_is_free_index (wrk->vls_pool, vlsh))
405 return pool_elt_at_index (wrk->vls_pool, vlsh);
409 vls_free (vcl_locked_session_t * vls)
411 vls_worker_t *wrk = vls_worker_get_current ();
414 vls_sh_to_vlsh_table_del (
415 wrk, vcl_session_handle_from_index (vls->session_index));
416 clib_spinlock_free (&vls->lock);
417 pool_put (wrk->vls_pool, vls);
420 static vcl_locked_session_t *
421 vls_get_and_lock (vls_handle_t vlsh)
423 vls_worker_t *wrk = vls_worker_get_current ();
424 vcl_locked_session_t *vls;
425 if (pool_is_free_index (wrk->vls_pool, vlsh))
427 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
432 static vcl_locked_session_t *
433 vls_get_w_dlock (vls_handle_t vlsh)
435 vcl_locked_session_t *vls;
436 vls_mt_table_rlock ();
437 vls = vls_get_and_lock (vlsh);
439 vls_mt_table_runlock ();
444 vls_get_and_unlock (vls_handle_t vlsh)
446 vcl_locked_session_t *vls;
447 vls_mt_table_rlock ();
448 vls = vls_get (vlsh);
450 vls_mt_table_runlock ();
454 vls_dunlock (vcl_locked_session_t * vls)
457 vls_mt_table_runlock ();
460 static vcl_locked_session_t *
461 vls_session_get (vls_worker_t * wrk, u32 vls_index)
463 if (pool_is_free_index (wrk->vls_pool, vls_index))
465 return pool_elt_at_index (wrk->vls_pool, vls_index);
469 vlsh_to_sh (vls_handle_t vlsh)
471 vcl_locked_session_t *vls;
474 vls = vls_get_w_dlock (vlsh);
476 return INVALID_SESSION_ID;
477 rv = vls_to_sh (vls);
483 vlsh_to_session_index (vls_handle_t vlsh)
485 vcl_session_handle_t sh;
486 sh = vlsh_to_sh (vlsh);
487 return vppcom_session_index (sh);
491 vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
493 vls_worker_t *wrk = vls_worker_get_current ();
494 uword *vlshp = vls_sh_to_vlsh_table_get (
496 vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
498 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
502 vls_session_index_to_vlsh (uint32_t session_index)
506 vls_mt_table_rlock ();
507 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
508 vls_mt_table_runlock ();
514 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
516 vls_shared_data_t *vls_shd;
519 if (vls->shared_data_index == ~0)
522 vls_shared_data_pool_rlock ();
524 vls_shd = vls_shared_data_get (vls->shared_data_index);
525 clib_spinlock_lock (&vls_shd->lock);
527 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
528 if (vls_shd->workers_subscribed[i] == wrk_index)
530 clib_spinlock_unlock (&vls_shd->lock);
531 vls_shared_data_pool_runlock ();
534 clib_spinlock_unlock (&vls_shd->lock);
536 vls_shared_data_pool_runlock ();
541 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
543 vls_shared_data_t *vls_shd;
545 if (vls->shared_data_index == ~0)
547 clib_warning ("not a shared session");
551 vls_shared_data_pool_rlock ();
553 vls_shd = vls_shared_data_get (vls->shared_data_index);
555 clib_spinlock_lock (&vls_shd->lock);
556 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
557 clib_spinlock_unlock (&vls_shd->lock);
559 vls_shared_data_pool_runlock ();
563 vls_shared_get_owner (vcl_locked_session_t * vls)
565 vls_shared_data_t *vls_shd;
568 vls_shared_data_pool_rlock ();
570 vls_shd = vls_shared_data_get (vls->shared_data_index);
571 owner_wrk = vls_shd->owner_wrk_index;
573 vls_shared_data_pool_runlock ();
579 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
581 vls_shared_data_t *vls_shd;
584 if (vls->shared_data_index == ~0)
586 clib_warning ("not a shared session");
590 vls_shared_data_pool_rlock ();
592 vls_shd = vls_shared_data_get (vls->shared_data_index);
594 clib_spinlock_lock (&vls_shd->lock);
595 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
596 clib_spinlock_unlock (&vls_shd->lock);
598 vls_shared_data_pool_runlock ();
600 return (is_set == 1);
604 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
606 vppcom_session_listen (vls_to_sh (vls), ~0);
607 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
611 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
616 wrk = vcl_worker_get (wrk_index);
617 s = vcl_session_get (wrk, vls->session_index);
618 if (s->session_state != VCL_STATE_LISTEN)
620 vcl_send_session_unlisten (wrk, s);
621 s->session_state = VCL_STATE_LISTEN_NO_MQ;
622 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
626 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
631 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
633 if (vls_shd->workers_subscribed[i] == wrk_index)
640 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
642 vls_shared_data_t *vls_shd;
643 int do_disconnect, pos;
647 if (vls->shared_data_index == ~0)
650 s = vcl_session_get (wrk, vls->session_index);
651 if (s->session_state == VCL_STATE_LISTEN)
652 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
654 vls_shared_data_pool_rlock ();
656 vls_shd = vls_shared_data_get (vls->shared_data_index);
657 clib_spinlock_lock (&vls_shd->lock);
659 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
662 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
668 * Unsubscribe from share data and fifos
672 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
673 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
675 vec_del1 (vls_shd->workers_subscribed, pos);
680 n_subscribers = vec_len (vls_shd->workers_subscribed);
681 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
682 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
685 * No subscriber left, cleanup shared data
689 u32 shd_index = vls_shared_data_index (vls_shd);
691 clib_spinlock_unlock (&vls_shd->lock);
692 vls_shared_data_pool_runlock ();
694 vls_shared_data_free (shd_index);
696 /* All locks have been dropped */
700 /* Return, if this is not the owning worker */
701 if (vls_shd->owner_wrk_index != wrk->wrk_index)
704 ASSERT (vec_len (vls_shd->workers_subscribed));
707 * Check if we can change owner or close
709 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
711 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
713 /* XXX is this still needed? */
714 if (vec_len (vls_shd->workers_subscribed) > 1)
715 clib_warning ("more workers need to be updated");
719 clib_spinlock_unlock (&vls_shd->lock);
720 vls_shared_data_pool_runlock ();
726 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
728 vls_shared_data_t *vls_shd;
730 u32 vls_shd_index = vls_shared_data_alloc ();
732 vls_shared_data_pool_rlock ();
734 vls_shd = vls_shared_data_get (vls_shd_index);
735 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
736 vls->shared_data_index = vls_shd_index;
737 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
739 vls_shared_data_pool_runlock ();
743 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
745 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
746 vls_shared_data_t *vls_shd;
749 s = vcl_session_get (vcl_wrk, vls->session_index);
752 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
753 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
757 ASSERT (vls->shared_data_index != ~0);
759 /* Reinit session lock */
760 clib_spinlock_init (&vls->lock);
762 vls_shared_data_pool_rlock ();
764 vls_shd = vls_shared_data_get (vls->shared_data_index);
766 clib_spinlock_lock (&vls_shd->lock);
767 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
768 clib_spinlock_unlock (&vls_shd->lock);
770 vls_shared_data_pool_runlock ();
774 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
776 else if (s->session_state == VCL_STATE_LISTEN)
778 s->session_state = VCL_STATE_LISTEN_NO_MQ;
783 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
785 vcl_locked_session_t *vls, *parent_vls;
788 pool_foreach (vls, vls_wrk->vls_pool) {
789 /* Initialize sharing on parent session */
790 if (vls->shared_data_index == ~0)
792 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
793 vls_init_share_session (vls_parent_wrk, parent_vls);
794 vls->shared_data_index = parent_vls->shared_data_index;
796 vls_share_session (vls_wrk, vls);
802 vls_validate_veps (vcl_worker_t *wrk)
805 u32 session_index, wrk_index;
807 pool_foreach (s, wrk->sessions)
809 if (s->vep.vep_sh != ~0)
811 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
812 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
814 if (s->vep.next_sh != ~0)
816 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
818 s->vep.next_sh = vcl_session_handle_from_index (session_index);
820 if (s->vep.prev_sh != ~0)
822 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
824 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
830 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
832 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
833 vcl_worker_t *wrk = vcl_worker_get_current ();
834 u32 vls_index, session_index, wrk_index;
835 vcl_session_handle_t sh;
836 vcl_locked_session_t *vls;
841 wrk->sessions = pool_dup (parent_wrk->sessions);
842 wrk->session_index_by_vpp_handles =
843 hash_dup (parent_wrk->session_index_by_vpp_handles);
848 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
850 hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
852 vcl_session_handle_parse (sh, &wrk_index, &session_index);
853 hash_set (vls_wrk->session_handle_to_vlsh_table,
854 vcl_session_handle_from_index (session_index), vls_index);
857 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
860 * Detach vls from parent vcl worker and attach them to child.
862 pool_foreach (vls, vls_wrk->vls_pool)
864 vls->worker_index = wrk->wrk_index;
867 /* Validate vep's handle */
868 vls_validate_veps (wrk);
870 vls_share_sessions (vls_parent_wrk, vls_wrk);
874 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
876 vcl_worker_t *wrk = vcl_worker_get_current ();
877 vcl_session_t *s = 0;
882 s = vcl_session_get (wrk, vls->session_index);
883 if (PREDICT_FALSE (!s))
885 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
892 is_nonblk = vcl_session_read_ready (s) != 0;
896 *locks_acq |= VLS_MT_LOCK_MQ;
899 case VLS_MT_OP_WRITE:
902 is_nonblk = vcl_session_write_ready (s) != 0;
906 *locks_acq |= VLS_MT_LOCK_MQ;
909 case VLS_MT_OP_XPOLL:
911 *locks_acq |= VLS_MT_LOCK_MQ;
913 case VLS_MT_OP_SPOOL:
914 vls_mt_spool_lock ();
915 *locks_acq |= VLS_MT_LOCK_SPOOL;
923 vls_mt_rel_locks (int locks_acq)
925 if (locks_acq & VLS_MT_LOCK_MQ)
927 if (locks_acq & VLS_MT_LOCK_SPOOL)
928 vls_mt_create_unlock ();
932 vls_mt_session_should_migrate (vcl_locked_session_t * vls)
934 return (vls_mt_wrk_supported ()
935 && vls->worker_index != vcl_get_worker_index ());
938 static vcl_locked_session_t *
939 vls_mt_session_migrate (vcl_locked_session_t *vls)
941 u32 wrk_index = vcl_get_worker_index ();
943 vls_worker_t *vls_wrk = vls_worker_get_current ();
944 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
945 vcl_session_t *session;
948 ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
951 * VCL session on current vcl worker already allocated. Update current
952 * owner worker and index and return
954 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
956 vls->worker_index = wrk_index;
957 vls->session_index = (u32) p[0];
962 * Ask vcl worker that owns the original vcl session to clone it into
963 * current vcl worker session pool
966 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
967 vls->owner_vcl_wrk_index)))
969 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
972 vls_mt_table_runlock ();
976 src_sid = (u32) p[0];
977 wrk = vcl_worker_get_current ();
978 session = vcl_session_alloc (wrk);
979 sid = session->session_index;
980 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
981 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
983 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
984 vls_index = vls->vls_index;
985 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
987 vls_mt_table_runlock ();
988 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
989 own_vcl_wrk_index, vls_index, src_sid);
991 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
993 VWRN ("session %u not exist", src_sid);
996 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
998 VWRN ("failed to wait rpc response");
1001 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
1002 session->vep.next_sh != ~0))
1004 VERR ("can't migrate nonempty epoll session");
1008 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
1009 session->session_state != VCL_STATE_CLOSED))
1011 VERR ("migrate NOT supported, session_status (%u)",
1012 session->session_state);
1017 vls = vls_get_w_dlock (vls_index);
1018 if (PREDICT_FALSE (!vls))
1020 VWRN ("failed to get vls %u", vls_index);
1024 session->session_index = sid;
1025 vls->worker_index = wrk_index;
1026 vls->session_index = sid;
1027 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
1028 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1033 vcl_session_free (wrk, session);
1038 vls_mt_detect (void)
1040 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1044 #define vls_mt_guard(_vls, _op) \
1045 int _locks_acq = 0; \
1046 if (vls_mt_wrk_supported ()) \
1048 if (PREDICT_FALSE (_vls && \
1049 ((vcl_locked_session_t *) _vls)->worker_index != \
1050 vcl_get_worker_index ())) \
1052 _vls = vls_mt_session_migrate (_vls); \
1053 if (PREDICT_FALSE (!_vls)) \
1054 return VPPCOM_EBADFD; \
1059 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1060 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1063 #define vls_mt_unguard() \
1064 if (PREDICT_FALSE (_locks_acq)) \
1065 vls_mt_rel_locks (_locks_acq)
1068 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1070 vcl_locked_session_t *vls;
1074 if (!(vls = vls_get_w_dlock (vlsh)))
1075 return VPPCOM_EBADFD;
1077 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1078 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
1080 vls_get_and_unlock (vlsh);
1085 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1087 vcl_locked_session_t *vls;
1091 if (!(vls = vls_get_w_dlock (vlsh)))
1092 return VPPCOM_EBADFD;
1093 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1094 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
1096 vls_get_and_unlock (vlsh);
1101 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1102 vppcom_endpt_t * ep)
1104 vcl_locked_session_t *vls;
1108 if (!(vls = vls_get_w_dlock (vlsh)))
1109 return VPPCOM_EBADFD;
1110 vls_mt_guard (vls, VLS_MT_OP_WRITE);
1111 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
1113 vls_get_and_unlock (vlsh);
1118 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1120 vcl_locked_session_t *vls;
1124 if (!(vls = vls_get_w_dlock (vlsh)))
1125 return VPPCOM_EBADFD;
1126 vls_mt_guard (vls, VLS_MT_OP_READ);
1127 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
1129 vls_get_and_unlock (vlsh);
1134 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1135 vppcom_endpt_t * ep)
1137 vcl_locked_session_t *vls;
1141 if (!(vls = vls_get_w_dlock (vlsh)))
1142 return VPPCOM_EBADFD;
1143 vls_mt_guard (vls, VLS_MT_OP_READ);
1144 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1147 vls_get_and_unlock (vlsh);
1152 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1154 vcl_locked_session_t *vls;
1158 if (!(vls = vls_get_w_dlock (vlsh)))
1159 return VPPCOM_EBADFD;
1160 if (vls_mt_session_should_migrate (vls))
1162 vls = vls_mt_session_migrate (vls);
1163 if (PREDICT_FALSE (!vls))
1164 return VPPCOM_EBADFD;
1166 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1167 vls_get_and_unlock (vlsh);
1172 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1174 vcl_locked_session_t *vls;
1178 if (!(vls = vls_get_w_dlock (vlsh)))
1179 return VPPCOM_EBADFD;
1180 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1181 vls_get_and_unlock (vlsh);
1186 vls_listen (vls_handle_t vlsh, int q_len)
1188 vcl_locked_session_t *vls;
1192 if (!(vls = vls_get_w_dlock (vlsh)))
1193 return VPPCOM_EBADFD;
1194 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1195 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
1197 vls_get_and_unlock (vlsh);
1202 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1204 vcl_locked_session_t *vls;
1208 if (!(vls = vls_get_w_dlock (vlsh)))
1209 return VPPCOM_EBADFD;
1210 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1211 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
1213 vls_get_and_unlock (vlsh);
1218 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1220 vcl_worker_t *wrk = vcl_worker_get_current ();
1224 if (vls_mt_wrk_supported ())
1227 ASSERT (wrk->wrk_index == vls->worker_index);
1228 s = vcl_session_get (wrk, vls->session_index);
1229 switch (s->session_state)
1231 case VCL_STATE_LISTEN:
1234 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
1237 vls_listener_wrk_stop_listen (vls, vls->worker_index);
1239 case VCL_STATE_LISTEN_NO_MQ:
1243 /* Register worker as listener */
1244 vls_listener_wrk_start_listen (vls, vls->worker_index);
1246 /* If owner worker did not attempt to accept/xpoll on the session,
1247 * force a listen stop for it, since it may not be interested in
1248 * accepting new sessions.
1249 * This is pretty much a hack done to give app workers the illusion
1250 * that it is fine to listen and not accept new sessions for a
1251 * given listener. Without it, we would accumulate unhandled
1252 * accepts on the passive worker message queue. */
1253 owner_wrk = vls_shared_get_owner (vls);
1254 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1255 vls_listener_wrk_stop_listen (vls, owner_wrk);
1263 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1265 vls_handle_t accepted_vlsh;
1266 vcl_locked_session_t *vls;
1270 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1271 return VPPCOM_EBADFD;
1272 if (vcl_n_workers () > 1)
1273 vls_mp_checks (vls, 1 /* is_add */ );
1274 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1275 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
1277 vls_get_and_unlock (listener_vlsh);
1280 accepted_vlsh = vls_alloc (sh);
1281 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1282 vppcom_session_close (sh);
1283 return accepted_vlsh;
1287 vls_create (uint8_t proto, uint8_t is_nonblocking)
1289 vcl_session_handle_t sh;
1291 vcl_locked_session_t *vls = NULL;
1294 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1295 sh = vppcom_session_create (proto, is_nonblocking);
1297 if (sh == INVALID_SESSION_ID)
1298 return VLS_INVALID_HANDLE;
1300 vlsh = vls_alloc (sh);
1301 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1302 vppcom_session_close (sh);
1308 vls_mt_session_cleanup (vcl_locked_session_t * vls)
1310 u32 session_index, wrk_index, current_vcl_wrk;
1311 vcl_worker_t *wrk = vcl_worker_get_current ();
1313 ASSERT (vls_mt_wrk_supported ());
1315 current_vcl_wrk = vcl_get_worker_index ();
1318 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1320 if (current_vcl_wrk != wrk_index)
1321 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
1324 hash_free (vls->vcl_wrk_index_to_session_index);
1328 vls_close (vls_handle_t vlsh)
1330 vcl_locked_session_t *vls;
1334 vls_mt_table_wlock ();
1336 vls = vls_get_and_lock (vlsh);
1339 vls_mt_table_wunlock ();
1340 return VPPCOM_EBADFD;
1343 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1345 if (vls_is_shared (vls))
1346 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1348 rv = vppcom_session_close (vls_to_sh (vls));
1350 if (vls_mt_wrk_supported ())
1351 vls_mt_session_cleanup (vls);
1356 vls_mt_table_wunlock ();
1362 vls_shutdown (vls_handle_t vlsh, int how)
1364 vcl_locked_session_t *vls;
1368 if (!(vls = vls_get_w_dlock (vlsh)))
1369 return VPPCOM_EBADFD;
1371 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1372 rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
1374 vls_get_and_unlock (vlsh);
1380 vls_epoll_create (void)
1382 vcl_session_handle_t sh;
1387 sh = vppcom_epoll_create ();
1388 if (sh == INVALID_SESSION_ID)
1389 return VLS_INVALID_HANDLE;
1391 vlsh = vls_alloc (sh);
1392 if (vlsh == VLS_INVALID_HANDLE)
1393 vppcom_session_close (sh);
1399 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1401 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
1404 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1408 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1409 struct epoll_event *event)
1411 vcl_locked_session_t *ep_vls, *vls;
1412 vcl_session_handle_t ep_sh, sh;
1416 vls_mt_table_rlock ();
1417 ep_vls = vls_get_and_lock (ep_vlsh);
1419 if (vls_mt_session_should_migrate (ep_vls))
1421 ep_vls = vls_mt_session_migrate (ep_vls);
1422 if (PREDICT_FALSE (!ep_vls))
1423 return VPPCOM_EBADFD;
1426 ep_sh = vls_to_sh (ep_vls);
1427 vls = vls_get_and_lock (vlsh);
1428 sh = vls_to_sh (vls);
1430 vls_epoll_ctl_mp_checks (vls, op);
1431 vls_mt_table_runlock ();
1432 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1434 vls_mt_table_rlock ();
1435 ep_vls = vls_get (ep_vlsh);
1436 vls = vls_get (vlsh);
1438 vls_unlock (ep_vls);
1439 vls_mt_table_runlock ();
1444 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1445 int maxevents, double wait_for_time)
1447 vcl_locked_session_t *vls, *vls_tmp = NULL;
1451 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1452 return VPPCOM_EBADFD;
1453 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
1454 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1457 vls_get_and_unlock (ep_vlsh);
1458 vls_handle_pending_wrk_cleanup ();
1463 vls_select_mp_checks (vcl_si_set * read_map)
1465 vcl_locked_session_t *vls;
1470 if (vcl_n_workers () <= 1)
1472 vlsl->select_mp_check = 1;
1479 vlsl->select_mp_check = 1;
1480 wrk = vcl_worker_get_current ();
1483 clib_bitmap_foreach (si, read_map) {
1484 s = vcl_session_get (wrk, si);
1485 if (s->session_state == VCL_STATE_LISTEN)
1487 vls = vls_get (vls_session_index_to_vlsh (si));
1488 vls_mp_checks (vls, 1 /* is_add */);
1495 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1496 vcl_si_set * except_map, double wait_for_time)
1499 vcl_locked_session_t *vls = NULL;
1502 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1503 if (PREDICT_FALSE (!vlsl->select_mp_check))
1504 vls_select_mp_checks (read_map);
1505 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1507 vls_handle_pending_wrk_cleanup ();
1512 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1514 u32 current_wrk, is_current;
1515 vcl_locked_session_t *vls;
1518 if (pool_elts (vcm->workers) <= 1)
1521 current_wrk = vcl_get_worker_index ();
1522 is_current = current_wrk == wrk->wrk_index;
1525 pool_foreach (s, wrk->sessions) {
1526 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
1527 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1528 vls_unshare_session (vls, wrk);
1534 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1536 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1538 /* Unshare sessions and also cleanup worker since child may have
1539 * called _exit () and therefore vcl may not catch the event */
1540 vls_unshare_vcl_worker_sessions (wrk);
1542 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1543 * may have been closed, so DONOT notify VPP.
1545 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
1547 vls_worker_free (vls_wrk);
1551 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1553 vcl_worker_t *sub_child;
1556 if (child_wrk->forked_child != ~0)
1558 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1561 /* Wait a bit, maybe the process is going away */
1562 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1564 if (kill (sub_child->current_pid, 0) < 0)
1565 vls_cleanup_forked_child (child_wrk, sub_child);
1568 vls_cleanup_vcl_worker (child_wrk);
1569 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1570 wrk->forked_child = ~0;
1574 vls_handle_pending_wrk_cleanup (void)
1577 vcl_worker_t *child_wrk, *wrk;
1578 vls_worker_t *vls_wrk = vls_worker_get_current ();
1580 if (PREDICT_TRUE (vec_len (vls_wrk->pending_wrk_cleanup) == 0))
1583 wrk = vcl_worker_get_current ();
1584 vec_foreach (wip, vls_wrk->pending_wrk_cleanup)
1586 child_wrk = vcl_worker_get_if_valid (*wip);
1589 vls_cleanup_forked_child (wrk, child_wrk);
1591 vec_reset_length (vls_wrk->pending_wrk_cleanup);
1594 static struct sigaction old_sa;
1597 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1599 vcl_worker_t *wrk, *child_wrk;
1600 vls_worker_t *vls_wrk;
1602 if (vcl_get_worker_index () == ~0)
1605 if (sigaction (SIGCHLD, &old_sa, 0))
1607 VERR ("couldn't restore sigchld");
1611 wrk = vcl_worker_get_current ();
1612 if (wrk->forked_child == ~0)
1615 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1619 if (si && si->si_pid != child_wrk->current_pid)
1621 VDBG (0, "unexpected child pid %u", si->si_pid);
1625 /* Parent process may enter sighandler with a lock, such as lock in localtime
1626 * or in mspace_free, and child wrk cleanup may try to get such locks and
1628 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1630 vls_wrk = vls_worker_get_current ();
1631 vec_add1 (vls_wrk->pending_wrk_cleanup, child_wrk->wrk_index);
1634 if (old_sa.sa_flags & SA_SIGINFO)
1636 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1637 fn (signum, si, uc);
1641 void (*fn) (int) = old_sa.sa_handler;
1648 vls_incercept_sigchld ()
1650 struct sigaction sa;
1651 if (old_sa.sa_sigaction)
1653 VDBG (0, "have intercepted sigchld");
1656 clib_memset (&sa, 0, sizeof (sa));
1657 sa.sa_sigaction = vls_intercept_sigchld_handler;
1658 sa.sa_flags = SA_SIGINFO;
1659 if (sigaction (SIGCHLD, &sa, &old_sa))
1661 VERR ("couldn't intercept sigchld");
1667 vls_app_pre_fork (void)
1669 vls_incercept_sigchld ();
1670 vcl_flush_mq_events ();
1674 vls_app_fork_child_handler (void)
1676 vcl_worker_t *parent_wrk;
1677 int parent_wrk_index;
1679 parent_wrk_index = vcl_get_worker_index ();
1680 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1686 vcl_set_worker_index (~0);
1689 * Allocate and register vcl worker with vpp
1691 if (vppcom_worker_register ())
1693 VERR ("couldn't register new worker!");
1698 * Allocate/initialize vls worker and share sessions
1700 vls_worker_alloc ();
1702 /* Reset number of threads and set wrk index */
1703 vlsl->vls_mt_n_threads = 0;
1704 vlsl->vls_wrk_index = vcl_get_worker_index ();
1705 vlsl->select_mp_check = 0;
1706 vls_mt_locks_init ();
1708 parent_wrk = vcl_worker_get (parent_wrk_index);
1709 vls_worker_copy_on_fork (parent_wrk);
1710 parent_wrk->forked_child = vcl_get_worker_index ();
1712 VDBG (0, "forked child main worker initialized");
1717 vls_app_fork_parent_handler (void)
1720 while (vcm->forking)
1727 vls_worker_t *wrk = vls_worker_get_current ();
1729 /* Handle pending wrk cleanup */
1730 vls_handle_pending_wrk_cleanup ();
1732 /* Unshare the sessions. VCL will clean up the worker */
1733 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1734 vls_worker_free (wrk);
1738 vls_clone_and_share_rpc_handler (void *args)
1740 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1741 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1742 vcl_locked_session_t *vls, *dst_vls;
1743 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
1744 vcl_session_t *s, *dst_s;
1746 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1747 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1748 msg->origin_session_index);
1750 /* VCL locked session can't been protected, so DONT touch it.
1751 * VCL session may been free, check it.
1753 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1754 s = vcl_session_get (vcl_wrk, msg->session_index);
1755 if (PREDICT_FALSE (!s))
1757 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1761 if (!vls_mt_wrk_supported ())
1763 vls = vls_session_get (wrk, msg->vls_index);
1764 vls_init_share_session (wrk, vls);
1765 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1766 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1767 dst_vls->shared_data_index = vls->shared_data_index;
1769 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
1770 clib_memcpy (dst_s, s, sizeof (*s));
1772 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
1776 vls_session_cleanup_rpc_handler (void *args)
1778 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1779 vcl_worker_t *wrk = vcl_worker_get_current ();
1780 vls_worker_t *vls_wrk = vls_worker_get_current ();
1781 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
1783 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1784 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
1786 vppcom_session_close (sh);
1787 vls_sh_to_vlsh_table_del (vls_wrk, sh);
1791 vls_rpc_handler (void *args)
1793 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1796 case VLS_RPC_CLONE_AND_SHARE:
1797 vls_clone_and_share_rpc_handler (msg->data);
1799 case VLS_RPC_SESS_CLEANUP:
1800 vls_session_cleanup_rpc_handler (msg->data);
1808 vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1809 u32 session_index, u32 vls_wrk_index,
1810 u32 dst_wrk_index, u32 dst_vls_index,
1811 u32 dst_session_index)
1813 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1814 vls_clone_and_share_msg_t *msg;
1817 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
1819 rpc = (vls_rpc_msg_t *) & data;
1820 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1821 msg = (vls_clone_and_share_msg_t *) & rpc->data;
1822 msg->origin_vls_wrk = vls_wrk_index;
1823 msg->origin_vls_index = origin_vls_index;
1824 msg->origin_vcl_wrk = wrk->wrk_index;
1825 msg->origin_session_index = session_index;
1826 msg->vls_index = dst_vls_index;
1827 msg->session_index = dst_session_index;
1829 /* Try lock and handle rpcs if two threads send each other
1830 * clone requests at the same time.
1832 wrk->rpc_done = VLS_RPC_STATE_INIT;
1833 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1834 vcl_flush_mq_events ();
1835 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1837 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
1838 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1839 msg->origin_session_index, ret);
1840 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1841 clib_time_now (&wrk->clib_time) < timeout)
1843 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
1847 vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1848 u32 dst_wrk_index, u32 dst_session_index)
1850 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1851 vls_sess_cleanup_msg_t *msg;
1855 rpc = (vls_rpc_msg_t *) & data;
1856 rpc->type = VLS_RPC_SESS_CLEANUP;
1857 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1858 msg->origin_vcl_wrk = wrk->wrk_index;
1859 msg->session_index = dst_session_index;
1861 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1863 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
1864 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
1868 vls_app_create (char *app_name)
1872 if ((rv = vppcom_app_create (app_name)))
1875 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1876 clib_memset (vlsm, 0, sizeof (*vlsm));
1877 clib_rwlock_init (&vlsm->vls_table_lock);
1878 clib_rwlock_init (&vlsm->shared_data_lock);
1879 clib_spinlock_init (&vlsm->worker_rpc_lock);
1880 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1882 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1883 vls_app_fork_child_handler);
1884 atexit (vls_app_exit);
1885 vls_worker_alloc ();
1886 vlsl->vls_wrk_index = vcl_get_worker_index ();
1887 vls_mt_locks_init ();
1888 vcm->wrk_rpc_fn = vls_rpc_handler;
1893 vls_use_eventfd (void)
1895 return vcm->cfg.use_mq_eventfd;
1899 vls_mt_wrk_supported (void)
1901 return vcm->cfg.mt_wrk_supported;
1905 vls_use_real_epoll (void)
1907 if (vcl_get_worker_index () == ~0)
1910 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1914 vls_register_vcl_worker (void)
1920 * fd.io coding-style-patch-verification: ON
1923 * eval: (c-set-style "gnu")