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_locked_session_t;
36 typedef struct vls_worker_
38 vcl_locked_session_t *vls_pool;
39 uword *session_index_to_vlsh_table;
41 volatile int rpc_done;
44 typedef struct vls_local_
47 volatile int vls_mt_n_threads;
48 pthread_mutex_t vls_mt_mq_mlock;
49 pthread_mutex_t vls_mt_spool_mlock;
50 volatile u8 select_mp_check;
51 volatile u8 epoll_mp_check;
52 } vls_process_local_t;
54 static vls_process_local_t vls_local;
55 static vls_process_local_t *vlsl = &vls_local;
57 typedef struct vls_main_
59 vls_worker_t *workers;
60 clib_rwlock_t vls_table_lock;
61 /** Pool of data shared by sessions owned by different workers */
62 vls_shared_data_t *shared_data_pool;
63 clib_rwlock_t shared_data_lock;
68 typedef enum vls_rpc_msg_type_
70 VLS_RPC_CLONE_AND_SHARE,
73 typedef struct vls_rpc_msg_
79 typedef struct vls_clone_and_share_msg_
81 u32 vls_index; /**< vls to be shared */
82 u32 origin_vls_wrk; /**< worker that initiated the rpc */
83 u32 origin_vls_index; /**< vls session of the originator */
84 } vls_clone_and_share_msg_t;
87 vls_get_worker_index (void)
89 return vcl_get_worker_index ();
93 vls_shared_data_alloc (void)
95 vls_shared_data_t *vls_shd;
98 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
99 pool_get_zero (vlsm->shared_data_pool, vls_shd);
100 clib_spinlock_init (&vls_shd->lock);
101 shd_index = vls_shd - vlsm->shared_data_pool;
102 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
108 vls_shared_data_index (vls_shared_data_t * vls_shd)
110 return vls_shd - vlsm->shared_data_pool;
114 vls_shared_data_get (u32 shd_index)
116 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
118 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
122 vls_shared_data_free (u32 shd_index)
124 vls_shared_data_t *vls_shd;
126 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
127 vls_shd = vls_shared_data_get (shd_index);
128 clib_spinlock_free (&vls_shd->lock);
129 clib_bitmap_free (vls_shd->listeners);
130 vec_free (vls_shd->workers_subscribed);
131 pool_put (vlsm->shared_data_pool, vls_shd);
132 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
136 vls_shared_data_pool_rlock (void)
138 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
142 vls_shared_data_pool_runlock (void)
144 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
148 vls_table_rlock (void)
150 if (vlsl->vls_mt_n_threads > 1)
151 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
155 vls_table_runlock (void)
157 if (vlsl->vls_mt_n_threads > 1)
158 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
162 vls_table_wlock (void)
164 if (vlsl->vls_mt_n_threads > 1)
165 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
169 vls_table_wunlock (void)
171 if (vlsl->vls_mt_n_threads > 1)
172 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
185 VLS_MT_LOCK_MQ = 1 << 0,
186 VLS_MT_LOCK_SPOOL = 1 << 1
187 } vls_mt_lock_type_t;
192 vlsl->vls_mt_n_threads += 1;
193 vcl_set_worker_index (vlsl->vls_wrk_index);
197 vls_mt_mq_lock (void)
199 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
203 vls_mt_mq_unlock (void)
205 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
209 vls_mt_spool_lock (void)
211 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
215 vls_mt_create_unlock (void)
217 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
221 vls_mt_locks_init (void)
223 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
224 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
228 vls_is_shared (vcl_locked_session_t * vls)
230 return (vls->shared_data_index != ~0);
234 vls_lock (vcl_locked_session_t * vls)
236 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
237 clib_spinlock_lock (&vls->lock);
241 vls_unlock (vcl_locked_session_t * vls)
243 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
244 clib_spinlock_unlock (&vls->lock);
247 static inline vcl_session_handle_t
248 vls_to_sh (vcl_locked_session_t * vls)
250 return vcl_session_handle_from_index (vls->session_index);
253 static inline vcl_session_handle_t
254 vls_to_sh_tu (vcl_locked_session_t * vls)
256 vcl_session_handle_t sh;
257 sh = vls_to_sh (vls);
258 vls_table_runlock ();
262 static vls_worker_t *
263 vls_worker_get_current (void)
265 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
269 vls_worker_alloc (void)
273 pool_get_zero (vlsm->workers, wrk);
274 wrk->wrk_index = vcl_get_worker_index ();
278 vls_worker_free (vls_worker_t * wrk)
280 hash_free (wrk->session_index_to_vlsh_table);
281 pool_free (wrk->vls_pool);
282 pool_put (vlsm->workers, wrk);
285 static vls_worker_t *
286 vls_worker_get (u32 wrk_index)
288 if (pool_is_free_index (vlsm->workers, wrk_index))
290 return pool_elt_at_index (vlsm->workers, wrk_index);
294 vls_alloc (vcl_session_handle_t sh)
296 vls_worker_t *wrk = vls_worker_get_current ();
297 vcl_locked_session_t *vls;
301 pool_get_zero (wrk->vls_pool, vls);
302 vls->session_index = vppcom_session_index (sh);
303 vls->worker_index = vppcom_session_worker (sh);
304 vls->vls_index = vls - wrk->vls_pool;
305 vls->shared_data_index = ~0;
306 hash_set (wrk->session_index_to_vlsh_table, vls->session_index,
308 clib_spinlock_init (&vls->lock);
310 vls_table_wunlock ();
311 return vls->vls_index;
314 static vcl_locked_session_t *
315 vls_get (vls_handle_t vlsh)
317 vls_worker_t *wrk = vls_worker_get_current ();
318 if (pool_is_free_index (wrk->vls_pool, vlsh))
320 return pool_elt_at_index (wrk->vls_pool, vlsh);
324 vls_free (vcl_locked_session_t * vls)
326 vls_worker_t *wrk = vls_worker_get_current ();
329 hash_unset (wrk->session_index_to_vlsh_table, vls->session_index);
330 clib_spinlock_free (&vls->lock);
331 pool_put (wrk->vls_pool, vls);
334 static vcl_locked_session_t *
335 vls_get_and_lock (vls_handle_t vlsh)
337 vls_worker_t *wrk = vls_worker_get_current ();
338 vcl_locked_session_t *vls;
339 if (pool_is_free_index (wrk->vls_pool, vlsh))
341 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
346 static vcl_locked_session_t *
347 vls_get_w_dlock (vls_handle_t vlsh)
349 vcl_locked_session_t *vls;
351 vls = vls_get_and_lock (vlsh);
353 vls_table_runlock ();
358 vls_get_and_unlock (vls_handle_t vlsh)
360 vcl_locked_session_t *vls;
362 vls = vls_get (vlsh);
364 vls_table_runlock ();
368 vls_dunlock (vcl_locked_session_t * vls)
371 vls_table_runlock ();
374 static vcl_locked_session_t *
375 vls_session_get (vls_worker_t * wrk, u32 vls_index)
377 if (pool_is_free_index (wrk->vls_pool, vls_index))
379 return pool_elt_at_index (wrk->vls_pool, vls_index);
383 vlsh_to_sh (vls_handle_t vlsh)
385 vcl_locked_session_t *vls;
388 vls = vls_get_w_dlock (vlsh);
390 return INVALID_SESSION_ID;
391 rv = vls_to_sh (vls);
397 vlsh_to_session_index (vls_handle_t vlsh)
399 vcl_session_handle_t sh;
400 sh = vlsh_to_sh (vlsh);
401 return vppcom_session_index (sh);
405 vls_si_to_vlsh (u32 session_index)
407 vls_worker_t *wrk = vls_worker_get_current ();
409 vlshp = hash_get (wrk->session_index_to_vlsh_table, session_index);
410 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
414 vls_session_index_to_vlsh (uint32_t session_index)
419 vlsh = vls_si_to_vlsh (session_index);
420 vls_table_runlock ();
426 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
428 vls_shared_data_t *vls_shd;
431 if (vls->shared_data_index == ~0)
434 vls_shared_data_pool_rlock ();
436 vls_shd = vls_shared_data_get (vls->shared_data_index);
437 clib_spinlock_lock (&vls_shd->lock);
439 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
440 if (vls_shd->workers_subscribed[i] == wrk_index)
442 clib_spinlock_unlock (&vls_shd->lock);
443 vls_shared_data_pool_runlock ();
446 clib_spinlock_unlock (&vls_shd->lock);
448 vls_shared_data_pool_runlock ();
453 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
455 vls_shared_data_t *vls_shd;
457 if (vls->shared_data_index == ~0)
459 clib_warning ("not a shared session");
463 vls_shared_data_pool_rlock ();
465 vls_shd = vls_shared_data_get (vls->shared_data_index);
467 clib_spinlock_lock (&vls_shd->lock);
468 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
469 clib_spinlock_unlock (&vls_shd->lock);
471 vls_shared_data_pool_runlock ();
475 vls_shared_get_owner (vcl_locked_session_t * vls)
477 vls_shared_data_t *vls_shd;
480 vls_shared_data_pool_rlock ();
482 vls_shd = vls_shared_data_get (vls->shared_data_index);
483 owner_wrk = vls_shd->owner_wrk_index;
485 vls_shared_data_pool_runlock ();
491 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
493 vls_shared_data_t *vls_shd;
496 if (vls->shared_data_index == ~0)
498 clib_warning ("not a shared session");
502 vls_shared_data_pool_rlock ();
504 vls_shd = vls_shared_data_get (vls->shared_data_index);
506 clib_spinlock_lock (&vls_shd->lock);
507 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
508 clib_spinlock_unlock (&vls_shd->lock);
510 vls_shared_data_pool_runlock ();
512 return (is_set == 1);
516 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
518 vppcom_session_listen (vls_to_sh (vls), ~0);
519 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
523 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
528 wrk = vcl_worker_get (wrk_index);
529 s = vcl_session_get (wrk, vls->session_index);
530 if (s->session_state != STATE_LISTEN)
532 vcl_send_session_unlisten (wrk, s);
533 s->session_state = STATE_LISTEN_NO_MQ;
534 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
538 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
543 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
545 if (vls_shd->workers_subscribed[i] == wrk_index)
552 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
554 vls_shared_data_t *vls_shd;
555 int do_disconnect, pos;
559 ASSERT (vls->shared_data_index != ~0);
561 s = vcl_session_get (wrk, vls->session_index);
562 if (s->session_state == STATE_LISTEN)
563 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
565 vls_shared_data_pool_rlock ();
567 vls_shd = vls_shared_data_get (vls->shared_data_index);
568 clib_spinlock_lock (&vls_shd->lock);
570 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
573 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
579 * Unsubscribe from share data and fifos
583 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
584 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
586 vec_del1 (vls_shd->workers_subscribed, pos);
591 n_subscribers = vec_len (vls_shd->workers_subscribed);
592 do_disconnect = s->session_state == STATE_LISTEN || !n_subscribers;
593 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
596 * No subscriber left, cleanup shared data
600 u32 shd_index = vls_shared_data_index (vls_shd);
602 clib_spinlock_unlock (&vls_shd->lock);
603 vls_shared_data_pool_runlock ();
605 vls_shared_data_free (shd_index);
607 /* All locks have been dropped */
611 /* Return, if this is not the owning worker */
612 if (vls_shd->owner_wrk_index != wrk->wrk_index)
615 ASSERT (vec_len (vls_shd->workers_subscribed));
618 * Check if we can change owner or close
620 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
621 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
623 /* XXX is this still needed? */
624 if (vec_len (vls_shd->workers_subscribed) > 1)
625 clib_warning ("more workers need to be updated");
629 clib_spinlock_unlock (&vls_shd->lock);
630 vls_shared_data_pool_runlock ();
636 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
638 vls_shared_data_t *vls_shd;
640 u32 vls_shd_index = vls_shared_data_alloc ();
642 vls_shared_data_pool_rlock ();
644 vls_shd = vls_shared_data_get (vls_shd_index);
645 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
646 vls->shared_data_index = vls_shd_index;
647 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
649 vls_shared_data_pool_runlock ();
653 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
655 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
656 vls_shared_data_t *vls_shd;
659 s = vcl_session_get (vcl_wrk, vls->session_index);
662 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
663 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
667 ASSERT (vls->shared_data_index != ~0);
669 /* Reinit session lock */
670 clib_spinlock_init (&vls->lock);
672 vls_shared_data_pool_rlock ();
674 vls_shd = vls_shared_data_get (vls->shared_data_index);
676 clib_spinlock_lock (&vls_shd->lock);
677 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
678 clib_spinlock_unlock (&vls_shd->lock);
680 vls_shared_data_pool_runlock ();
684 svm_fifo_add_subscriber (s->rx_fifo, vcl_wrk->vpp_wrk_index);
685 svm_fifo_add_subscriber (s->tx_fifo, vcl_wrk->vpp_wrk_index);
687 else if (s->session_state == STATE_LISTEN)
689 s->session_state = STATE_LISTEN_NO_MQ;
694 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
696 vcl_locked_session_t *vls, *parent_vls;
699 pool_foreach (vls, vls_wrk->vls_pool, ({
700 /* Initialize sharing on parent session */
701 if (vls->shared_data_index == ~0)
703 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
704 vls_init_share_session (vls_parent_wrk, parent_vls);
705 vls->shared_data_index = parent_vls->shared_data_index;
707 vls_share_session (vls_wrk, vls);
713 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
715 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
716 vcl_worker_t *wrk = vcl_worker_get_current ();
721 wrk->vpp_event_queues = vec_dup (parent_wrk->vpp_event_queues);
722 wrk->sessions = pool_dup (parent_wrk->sessions);
723 wrk->session_index_by_vpp_handles =
724 hash_dup (parent_wrk->session_index_by_vpp_handles);
729 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
730 vls_wrk->session_index_to_vlsh_table =
731 hash_dup (vls_parent_wrk->session_index_to_vlsh_table);
732 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
734 vls_share_sessions (vls_parent_wrk, vls_wrk);
738 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
740 vcl_worker_t *wrk = vcl_worker_get_current ();
741 vcl_session_t *s = 0;
746 s = vcl_session_get (wrk, vls->session_index);
747 if (PREDICT_FALSE (!s))
749 is_nonblk = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
756 is_nonblk = vcl_session_read_ready (s) != 0;
760 *locks_acq |= VLS_MT_LOCK_MQ;
763 case VLS_MT_OP_WRITE:
766 is_nonblk = vcl_session_write_ready (s) != 0;
770 *locks_acq |= VLS_MT_LOCK_MQ;
773 case VLS_MT_OP_XPOLL:
775 *locks_acq |= VLS_MT_LOCK_MQ;
777 case VLS_MT_OP_SPOOL:
778 vls_mt_spool_lock ();
779 *locks_acq |= VLS_MT_LOCK_SPOOL;
787 vls_mt_rel_locks (int locks_acq)
789 if (locks_acq & VLS_MT_LOCK_MQ)
791 if (locks_acq & VLS_MT_LOCK_SPOOL)
792 vls_mt_create_unlock ();
795 #define vls_mt_guard(_vls, _op) \
796 int _locks_acq = 0; \
797 if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) \
799 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
800 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
802 #define vls_mt_unguard() \
803 if (PREDICT_FALSE (_locks_acq)) \
804 vls_mt_rel_locks (_locks_acq)
807 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
809 vcl_locked_session_t *vls;
812 if (!(vls = vls_get_w_dlock (vlsh)))
813 return VPPCOM_EBADFD;
815 vls_mt_guard (vls, VLS_MT_OP_WRITE);
816 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
818 vls_get_and_unlock (vlsh);
823 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
825 vcl_locked_session_t *vls;
828 if (!(vls = vls_get_w_dlock (vlsh)))
829 return VPPCOM_EBADFD;
830 vls_mt_guard (vls, VLS_MT_OP_WRITE);
831 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
833 vls_get_and_unlock (vlsh);
838 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
841 vcl_locked_session_t *vls;
844 if (!(vls = vls_get_w_dlock (vlsh)))
845 return VPPCOM_EBADFD;
846 vls_mt_guard (vls, VLS_MT_OP_WRITE);
847 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
849 vls_get_and_unlock (vlsh);
854 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
856 vcl_locked_session_t *vls;
859 if (!(vls = vls_get_w_dlock (vlsh)))
860 return VPPCOM_EBADFD;
861 vls_mt_guard (vls, VLS_MT_OP_READ);
862 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
864 vls_get_and_unlock (vlsh);
869 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
872 vcl_locked_session_t *vls;
875 if (!(vls = vls_get_w_dlock (vlsh)))
876 return VPPCOM_EBADFD;
877 vls_mt_guard (vls, VLS_MT_OP_READ);
878 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
881 vls_get_and_unlock (vlsh);
886 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
888 vcl_locked_session_t *vls;
891 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
894 if (!(vls = vls_get_w_dlock (vlsh)))
895 return VPPCOM_EBADFD;
896 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
897 vls_get_and_unlock (vlsh);
902 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
904 vcl_locked_session_t *vls;
907 if (!(vls = vls_get_w_dlock (vlsh)))
908 return VPPCOM_EBADFD;
909 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
910 vls_get_and_unlock (vlsh);
915 vls_listen (vls_handle_t vlsh, int q_len)
917 vcl_locked_session_t *vls;
920 if (!(vls = vls_get_w_dlock (vlsh)))
921 return VPPCOM_EBADFD;
922 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
923 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
925 vls_get_and_unlock (vlsh);
930 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
932 vcl_locked_session_t *vls;
935 if (!(vls = vls_get_w_dlock (vlsh)))
936 return VPPCOM_EBADFD;
937 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
938 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
940 vls_get_and_unlock (vlsh);
945 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
947 vcl_worker_t *wrk = vcl_worker_get_current ();
951 s = vcl_session_get (wrk, vls->session_index);
952 switch (s->session_state)
957 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
960 vls_listener_wrk_stop_listen (vls, vls->worker_index);
962 case STATE_LISTEN_NO_MQ:
966 /* Register worker as listener */
967 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
969 /* If owner worker did not attempt to accept/xpoll on the session,
970 * force a listen stop for it, since it may not be interested in
971 * accepting new sessions.
972 * This is pretty much a hack done to give app workers the illusion
973 * that it is fine to listen and not accept new sessions for a
974 * given listener. Without it, we would accumulate unhandled
975 * accepts on the passive worker message queue. */
976 owner_wrk = vls_shared_get_owner (vls);
977 if (!vls_listener_wrk_is_active (vls, owner_wrk))
978 vls_listener_wrk_stop_listen (vls, owner_wrk);
986 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
988 vls_handle_t accepted_vlsh;
989 vcl_locked_session_t *vls;
992 if (!(vls = vls_get_w_dlock (listener_vlsh)))
993 return VPPCOM_EBADFD;
994 if (vcl_n_workers () > 1)
995 vls_mp_checks (vls, 1 /* is_add */ );
996 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
997 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
999 vls_get_and_unlock (listener_vlsh);
1002 accepted_vlsh = vls_alloc (sh);
1003 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1004 vppcom_session_close (sh);
1005 return accepted_vlsh;
1009 vls_create (uint8_t proto, uint8_t is_nonblocking)
1011 vcl_session_handle_t sh;
1014 vls_mt_guard (0, VLS_MT_OP_SPOOL);
1015 sh = vppcom_session_create (proto, is_nonblocking);
1017 if (sh == INVALID_SESSION_ID)
1018 return VLS_INVALID_HANDLE;
1020 vlsh = vls_alloc (sh);
1021 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1022 vppcom_session_close (sh);
1028 vls_close (vls_handle_t vlsh)
1030 vcl_locked_session_t *vls;
1035 vls = vls_get_and_lock (vlsh);
1038 vls_table_wunlock ();
1039 return VPPCOM_EBADFD;
1042 vls_mt_guard (0, VLS_MT_OP_SPOOL);
1044 if (vls_is_shared (vls))
1045 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1047 rv = vppcom_session_close (vls_to_sh (vls));
1052 vls_table_wunlock ();
1058 vls_epoll_create (void)
1060 vcl_session_handle_t sh;
1063 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1066 sh = vppcom_epoll_create ();
1067 if (sh == INVALID_SESSION_ID)
1068 return VLS_INVALID_HANDLE;
1070 vlsh = vls_alloc (sh);
1071 if (vlsh == VLS_INVALID_HANDLE)
1072 vppcom_session_close (sh);
1078 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1080 if (vcl_n_workers () <= 1)
1082 vlsl->epoll_mp_check = 1;
1086 if (op == EPOLL_CTL_MOD)
1089 vlsl->epoll_mp_check = 1;
1090 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1094 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1095 struct epoll_event *event)
1097 vcl_locked_session_t *ep_vls, *vls;
1098 vcl_session_handle_t ep_sh, sh;
1102 ep_vls = vls_get_and_lock (ep_vlsh);
1103 vls = vls_get_and_lock (vlsh);
1104 ep_sh = vls_to_sh (ep_vls);
1105 sh = vls_to_sh (vls);
1107 if (PREDICT_FALSE (!vlsl->epoll_mp_check))
1108 vls_epoll_ctl_mp_checks (vls, op);
1110 vls_table_runlock ();
1112 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1115 ep_vls = vls_get (ep_vlsh);
1116 vls = vls_get (vlsh);
1118 vls_unlock (ep_vls);
1119 vls_table_runlock ();
1124 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1125 int maxevents, double wait_for_time)
1127 vcl_locked_session_t *vls;
1130 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1131 return VPPCOM_EBADFD;
1132 vls_mt_guard (0, VLS_MT_OP_XPOLL);
1133 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1136 vls_get_and_unlock (ep_vlsh);
1141 vls_select_mp_checks (vcl_si_set * read_map)
1143 vcl_locked_session_t *vls;
1148 if (vcl_n_workers () <= 1)
1150 vlsl->select_mp_check = 1;
1157 vlsl->select_mp_check = 1;
1158 wrk = vcl_worker_get_current ();
1161 clib_bitmap_foreach (si, read_map, ({
1162 s = vcl_session_get (wrk, si);
1163 if (s->session_state == STATE_LISTEN)
1165 vls = vls_get (vls_session_index_to_vlsh (si));
1166 vls_mp_checks (vls, 1 /* is_add */);
1173 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1174 vcl_si_set * except_map, double wait_for_time)
1178 vls_mt_guard (0, VLS_MT_OP_XPOLL);
1179 if (PREDICT_FALSE (!vlsl->select_mp_check))
1180 vls_select_mp_checks (read_map);
1181 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1187 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1189 u32 current_wrk, is_current;
1190 vcl_locked_session_t *vls;
1193 if (pool_elts (vcm->workers) <= 1)
1196 current_wrk = vcl_get_worker_index ();
1197 is_current = current_wrk == wrk->wrk_index;
1200 pool_foreach (s, wrk->sessions, ({
1201 vls = vls_get (vls_si_to_vlsh (s->session_index));
1202 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1203 vls_unshare_session (vls, wrk);
1209 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1211 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1213 /* Unshare sessions and also cleanup worker since child may have
1214 * called _exit () and therefore vcl may not catch the event */
1215 vls_unshare_vcl_worker_sessions (wrk);
1216 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
1218 vls_worker_free (vls_wrk);
1222 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1224 vcl_worker_t *sub_child;
1227 if (child_wrk->forked_child != ~0)
1229 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1232 /* Wait a bit, maybe the process is going away */
1233 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1235 if (kill (sub_child->current_pid, 0) < 0)
1236 vls_cleanup_forked_child (child_wrk, sub_child);
1239 vls_cleanup_vcl_worker (child_wrk);
1240 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1241 wrk->forked_child = ~0;
1244 static struct sigaction old_sa;
1247 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1249 vcl_worker_t *wrk, *child_wrk;
1251 if (vcl_get_worker_index () == ~0)
1254 if (sigaction (SIGCHLD, &old_sa, 0))
1256 VERR ("couldn't restore sigchld");
1260 wrk = vcl_worker_get_current ();
1261 if (wrk->forked_child == ~0)
1264 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1268 if (si && si->si_pid != child_wrk->current_pid)
1270 VDBG (0, "unexpected child pid %u", si->si_pid);
1273 vls_cleanup_forked_child (wrk, child_wrk);
1276 if (old_sa.sa_flags & SA_SIGINFO)
1278 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1279 fn (signum, si, uc);
1283 void (*fn) (int) = old_sa.sa_handler;
1290 vls_incercept_sigchld ()
1292 struct sigaction sa;
1293 clib_memset (&sa, 0, sizeof (sa));
1294 sa.sa_sigaction = vls_intercept_sigchld_handler;
1295 sa.sa_flags = SA_SIGINFO;
1296 if (sigaction (SIGCHLD, &sa, &old_sa))
1298 VERR ("couldn't intercept sigchld");
1304 vls_app_pre_fork (void)
1306 vls_incercept_sigchld ();
1307 vcl_flush_mq_events ();
1311 vls_app_fork_child_handler (void)
1313 vcl_worker_t *parent_wrk;
1314 int rv, parent_wrk_index;
1317 parent_wrk_index = vcl_get_worker_index ();
1318 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1322 * Allocate worker vcl
1324 vcl_set_worker_index (~0);
1325 if (!vcl_worker_alloc_and_init ())
1326 VERR ("couldn't allocate new worker");
1329 * Attach to binary api
1331 child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
1332 vcl_cleanup_bapi ();
1333 vppcom_api_hookup ();
1334 vcm->app_state = STATE_APP_START;
1335 rv = vppcom_connect_to_vpp ((char *) child_name);
1336 vec_free (child_name);
1339 VERR ("couldn't connect to VPP!");
1344 * Allocate/initialize vls worker
1346 vls_worker_alloc ();
1349 * Register worker with vpp and share sessions
1351 vcl_worker_register_with_vpp ();
1352 parent_wrk = vcl_worker_get (parent_wrk_index);
1353 vls_worker_copy_on_fork (parent_wrk);
1354 parent_wrk->forked_child = vcl_get_worker_index ();
1356 /* Reset number of threads and set wrk index */
1357 vlsl->vls_mt_n_threads = 0;
1358 vlsl->vls_wrk_index = vcl_get_worker_index ();
1359 vlsl->select_mp_check = 0;
1360 vlsl->epoll_mp_check = 0;
1361 vls_mt_locks_init ();
1363 VDBG (0, "forked child main worker initialized");
1368 vls_app_fork_parent_handler (void)
1371 while (vcm->forking)
1378 vls_worker_t *wrk = vls_worker_get_current ();
1380 /* Unshare the sessions. VCL will clean up the worker */
1381 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1382 vls_worker_free (wrk);
1386 vls_clone_and_share_rpc_handler (void *args)
1388 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1389 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1390 vcl_locked_session_t *vls, *dst_vls;
1391 vcl_worker_t *dst_vcl_wrk;
1392 vcl_session_t *s, *dst_s;
1394 vls = vls_session_get (wrk, msg->vls_index);
1395 vls_init_share_session (wrk, vls);
1397 s = vcl_session_get (vcl_worker_get_current (), vls->session_index);
1398 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1399 dst_vcl_wrk = vcl_worker_get (msg->origin_vls_wrk);
1400 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1401 dst_vls->shared_data_index = vls->shared_data_index;
1402 dst_s = vcl_session_get (dst_vcl_wrk, dst_vls->session_index);
1403 clib_memcpy (dst_s, s, sizeof (*s));
1405 dst_wrk->rpc_done = 1;
1409 vls_rpc_handler (void *args)
1411 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1414 case VLS_RPC_CLONE_AND_SHARE:
1415 vls_clone_and_share_rpc_handler (msg->data);
1423 vls_send_clone_and_share_rpc (vls_worker_t * wrk, vcl_locked_session_t * vls,
1424 u32 dst_wrk_index, u32 dst_vls_index)
1426 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1427 vls_clone_and_share_msg_t *msg;
1430 rpc = (vls_rpc_msg_t *) & data;
1431 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1432 msg = (vls_clone_and_share_msg_t *) & rpc->data;
1433 msg->origin_vls_wrk = wrk->wrk_index;
1434 msg->origin_vls_index = vls->vls_index;
1435 msg->vls_index = dst_vls_index;
1438 vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1439 while (!wrk->rpc_done)
1444 vls_app_create (char *app_name)
1448 if ((rv = vppcom_app_create (app_name)))
1451 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1452 clib_memset (vlsm, 0, sizeof (*vlsm));
1453 clib_rwlock_init (&vlsm->vls_table_lock);
1454 clib_rwlock_init (&vlsm->shared_data_lock);
1455 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1457 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1458 vls_app_fork_child_handler);
1459 atexit (vls_app_exit);
1460 vls_worker_alloc ();
1461 vlsl->vls_wrk_index = vcl_get_worker_index ();
1462 vls_mt_locks_init ();
1463 vcm->wrk_rpc_fn = vls_rpc_handler;
1468 vls_use_eventfd (void)
1470 return vcm->cfg.use_mq_eventfd;
1474 * fd.io coding-style-patch-verification: ON
1477 * eval: (c-set-style "gnu")