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 vcl_locked_session_
25 u32 *workers_subscribed;
26 clib_bitmap_t *listeners;
27 } vcl_locked_session_t;
29 typedef struct vls_local_
32 volatile int vls_mt_n_threads;
33 pthread_mutex_t vls_mt_mq_mlock;
34 pthread_mutex_t vls_mt_spool_mlock;
35 volatile u8 select_mp_check;
36 volatile u8 epoll_mp_check;
37 } vls_process_local_t;
39 static vls_process_local_t vls_local;
40 static vls_process_local_t *vlsl = &vls_local;
42 typedef struct vls_main_
44 vcl_locked_session_t *vls_pool;
45 clib_rwlock_t vls_table_lock;
46 uword *session_index_to_vlsh_table;
52 vls_table_rlock (void)
54 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
58 vls_table_runlock (void)
60 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
64 vls_table_wlock (void)
66 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
70 vls_table_wunlock (void)
72 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
85 VLS_MT_LOCK_MQ = 1 << 0,
86 VLS_MT_LOCK_SPOOL = 1 << 1
92 vlsl->vls_mt_n_threads += 1;
93 vcl_set_worker_index (vlsl->vls_wrk_index);
99 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
103 vls_mt_mq_unlock (void)
105 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
109 vls_mt_spool_lock (void)
111 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
115 vls_mt_create_unlock (void)
117 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
121 vls_mt_locks_init (void)
123 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
124 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
127 static inline vcl_session_handle_t
128 vls_to_sh (vcl_locked_session_t * vls)
130 return vcl_session_handle_from_index (vls->session_index);
133 static inline vcl_session_handle_t
134 vls_to_sh_tu (vcl_locked_session_t * vls)
136 vcl_session_handle_t sh;
137 sh = vls_to_sh (vls);
138 vls_table_runlock ();
143 vls_alloc (vcl_session_handle_t sh)
145 vcl_locked_session_t *vls;
148 pool_get (vlsm->vls_pool, vls);
149 vls->session_index = vppcom_session_index (sh);
150 vls->worker_index = vppcom_session_worker (sh);
151 vls->vls_index = vls - vlsm->vls_pool;
152 hash_set (vlsm->session_index_to_vlsh_table, vls->session_index,
154 clib_spinlock_init (&vls->lock);
155 vls_table_wunlock ();
156 return vls->vls_index;
159 static vcl_locked_session_t *
160 vls_get (vls_handle_t vlsh)
162 if (pool_is_free_index (vlsm->vls_pool, vlsh))
164 return pool_elt_at_index (vlsm->vls_pool, vlsh);
168 vls_free (vcl_locked_session_t * vls)
171 hash_unset (vlsm->session_index_to_vlsh_table, vls->session_index);
172 clib_spinlock_free (&vls->lock);
173 pool_put (vlsm->vls_pool, vls);
176 static vcl_locked_session_t *
177 vls_get_and_lock (vls_handle_t vlsh)
179 vcl_locked_session_t *vls;
180 if (pool_is_free_index (vlsm->vls_pool, vlsh))
182 vls = pool_elt_at_index (vlsm->vls_pool, vlsh);
183 clib_spinlock_lock (&vls->lock);
187 static vcl_locked_session_t *
188 vls_get_w_dlock (vls_handle_t vlsh)
190 vcl_locked_session_t *vls;
192 vls = vls_get_and_lock (vlsh);
194 vls_table_runlock ();
199 vls_lock (vcl_locked_session_t * vls)
201 clib_spinlock_lock (&vls->lock);
205 vls_unlock (vcl_locked_session_t * vls)
207 clib_spinlock_unlock (&vls->lock);
211 vls_get_and_unlock (vls_handle_t vlsh)
213 vcl_locked_session_t *vls;
215 vls = vls_get (vlsh);
217 vls_table_runlock ();
221 vls_dunlock (vcl_locked_session_t * vls)
224 vls_table_runlock ();
228 vlsh_to_sh (vls_handle_t vlsh)
230 vcl_locked_session_t *vls;
233 vls = vls_get_w_dlock (vlsh);
235 return INVALID_SESSION_ID;
236 rv = vls_to_sh (vls);
242 vlsh_to_session_index (vls_handle_t vlsh)
244 vcl_session_handle_t sh;
245 sh = vlsh_to_sh (vlsh);
246 return vppcom_session_index (sh);
250 vls_si_to_vlsh (u32 session_index)
253 vlshp = hash_get (vlsm->session_index_to_vlsh_table, session_index);
254 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
258 vls_session_index_to_vlsh (uint32_t session_index)
263 vlsh = vls_si_to_vlsh (session_index);
264 vls_table_runlock ();
270 vls_is_shared (vcl_locked_session_t * vls)
272 return vec_len (vls->workers_subscribed);
276 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
279 for (i = 0; i < vec_len (vls->workers_subscribed); i++)
280 if (vls->workers_subscribed[i] == wrk_index)
286 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
288 clib_bitmap_set (vls->listeners, wrk_index, is_active);
292 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
294 return (clib_bitmap_get (vls->listeners, wrk_index) == 1);
298 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
300 vppcom_session_listen (vls_to_sh (vls), ~0);
301 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
305 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
310 wrk = vcl_worker_get (wrk_index);
311 s = vcl_session_get (wrk, vls->session_index);
312 if (s->session_state != STATE_LISTEN)
314 vcl_send_session_unlisten (wrk, s);
315 s->session_state = STATE_LISTEN_NO_MQ;
316 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
320 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
322 int i, do_disconnect;
325 s = vcl_session_get (wrk, vls->session_index);
326 if (s->session_state == STATE_LISTEN)
327 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
329 for (i = 0; i < vec_len (vls->workers_subscribed); i++)
331 if (vls->workers_subscribed[i] != wrk->wrk_index)
336 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
337 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
339 vec_del1 (vls->workers_subscribed, i);
340 do_disconnect = s->session_state == STATE_LISTEN;
341 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
345 /* Return, if this is not the owning worker */
346 if (vls->worker_index != wrk->wrk_index)
349 /* Check if we can change owner or close */
350 if (vec_len (vls->workers_subscribed))
352 vls->worker_index = vls->workers_subscribed[0];
353 vec_del1 (vls->workers_subscribed, 0);
354 vcl_send_session_worker_update (wrk, s, vls->worker_index);
355 if (vec_len (vls->workers_subscribed))
356 clib_warning ("more workers need to be updated");
360 vcl_session_cleanup (wrk, s, vcl_session_handle (s),
361 1 /* do_disconnect */ );
368 vls_share_vcl_session (vcl_worker_t * wrk, vcl_session_t * s)
370 vcl_locked_session_t *vls;
372 vls = vls_get (vls_si_to_vlsh (s->session_index));
376 vec_add1 (vls->workers_subscribed, wrk->wrk_index);
379 svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
380 svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
382 else if (s->session_state == STATE_LISTEN)
384 s->session_state = STATE_LISTEN_NO_MQ;
391 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
393 vcl_worker_t *wrk = vcl_worker_get_current ();
396 wrk->vpp_event_queues = vec_dup (parent_wrk->vpp_event_queues);
397 wrk->sessions = pool_dup (parent_wrk->sessions);
398 wrk->session_index_by_vpp_handles =
399 hash_dup (parent_wrk->session_index_by_vpp_handles);
403 pool_foreach (s, wrk->sessions, ({
404 vls_share_vcl_session (wrk, s);
408 vls_table_runlock ();
412 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
414 vcl_worker_t *wrk = vcl_worker_get_current ();
415 vcl_session_t *s = 0;
420 s = vcl_session_get (wrk, vls->session_index);
421 if (PREDICT_FALSE (!s))
423 is_nonblk = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
430 is_nonblk = vcl_session_read_ready (s) != 0;
434 *locks_acq |= VLS_MT_LOCK_MQ;
437 case VLS_MT_OP_WRITE:
440 is_nonblk = vcl_session_write_ready (s) != 0;
444 *locks_acq |= VLS_MT_LOCK_MQ;
447 case VLS_MT_OP_XPOLL:
449 *locks_acq |= VLS_MT_LOCK_MQ;
451 case VLS_MT_OP_SPOOL:
452 vls_mt_spool_lock ();
453 *locks_acq |= VLS_MT_LOCK_SPOOL;
461 vls_mt_rel_locks (int locks_acq)
463 if (locks_acq & VLS_MT_LOCK_MQ)
465 if (locks_acq & VLS_MT_LOCK_SPOOL)
466 vls_mt_create_unlock ();
469 #define vls_mt_guard(_vls, _op) \
470 int _locks_acq = 0; \
471 if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) \
473 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
474 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
476 #define vls_mt_unguard() \
477 if (PREDICT_FALSE (_locks_acq)) \
478 vls_mt_rel_locks (_locks_acq)
481 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
483 vcl_locked_session_t *vls;
486 if (!(vls = vls_get_w_dlock (vlsh)))
487 return VPPCOM_EBADFD;
489 vls_mt_guard (vls, VLS_MT_OP_WRITE);
490 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
492 vls_get_and_unlock (vlsh);
497 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
499 vcl_locked_session_t *vls;
502 if (!(vls = vls_get_w_dlock (vlsh)))
503 return VPPCOM_EBADFD;
504 vls_mt_guard (vls, VLS_MT_OP_WRITE);
505 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
507 vls_get_and_unlock (vlsh);
512 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
515 vcl_locked_session_t *vls;
518 if (!(vls = vls_get_w_dlock (vlsh)))
519 return VPPCOM_EBADFD;
520 vls_mt_guard (vls, VLS_MT_OP_WRITE);
521 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
523 vls_get_and_unlock (vlsh);
528 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
530 vcl_locked_session_t *vls;
533 if (!(vls = vls_get_w_dlock (vlsh)))
534 return VPPCOM_EBADFD;
535 vls_mt_guard (vls, VLS_MT_OP_READ);
536 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
538 vls_get_and_unlock (vlsh);
543 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
546 vcl_locked_session_t *vls;
549 if (!(vls = vls_get_w_dlock (vlsh)))
550 return VPPCOM_EBADFD;
551 vls_mt_guard (vls, VLS_MT_OP_READ);
552 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
555 vls_get_and_unlock (vlsh);
560 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
562 vcl_locked_session_t *vls;
565 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
568 if (!(vls = vls_get_w_dlock (vlsh)))
569 return VPPCOM_EBADFD;
570 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
571 vls_get_and_unlock (vlsh);
576 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
578 vcl_locked_session_t *vls;
581 if (!(vls = vls_get_w_dlock (vlsh)))
582 return VPPCOM_EBADFD;
583 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
584 vls_get_and_unlock (vlsh);
589 vls_listen (vls_handle_t vlsh, int q_len)
591 vcl_locked_session_t *vls;
594 if (!(vls = vls_get_w_dlock (vlsh)))
595 return VPPCOM_EBADFD;
596 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
597 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
599 vls_get_and_unlock (vlsh);
604 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
606 vcl_locked_session_t *vls;
609 if (!(vls = vls_get_w_dlock (vlsh)))
610 return VPPCOM_EBADFD;
611 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
612 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
614 vls_get_and_unlock (vlsh);
619 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
621 vcl_worker_t *wrk = vcl_worker_get_current ();
624 s = vcl_session_get (wrk, vls->session_index);
625 switch (s->session_state)
630 if (vls->worker_index == wrk->wrk_index)
631 vls_listener_wrk_set (vls, wrk->wrk_index, 1 /* is_active */ );
634 vls_listener_wrk_stop_listen (vls, vls->worker_index);
636 case STATE_LISTEN_NO_MQ:
640 /* Register worker as listener */
641 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
643 /* If owner worker did not attempt to accept/xpoll on the session,
644 * force a listen stop for it, since it may not be interested in
645 * accepting new sessions.
646 * This is pretty much a hack done to give app workers the illusion
647 * that it is fine to listen and not accept new sessions for a
648 * given listener. Without it, we would accumulate unhandled
649 * accepts on the passive worker message queue. */
650 if (!vls_listener_wrk_is_active (vls, vls->worker_index))
651 vls_listener_wrk_stop_listen (vls, vls->worker_index);
659 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
661 vls_handle_t accepted_vlsh;
662 vcl_locked_session_t *vls;
665 if (!(vls = vls_get_w_dlock (listener_vlsh)))
666 return VPPCOM_EBADFD;
667 if (vcl_n_workers () > 1)
668 vls_mp_checks (vls, 1 /* is_add */ );
669 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
670 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
672 vls_get_and_unlock (listener_vlsh);
675 accepted_vlsh = vls_alloc (sh);
676 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
677 vppcom_session_close (sh);
678 return accepted_vlsh;
682 vls_create (uint8_t proto, uint8_t is_nonblocking)
684 vcl_session_handle_t sh;
687 vls_mt_guard (0, VLS_MT_OP_SPOOL);
688 sh = vppcom_session_create (proto, is_nonblocking);
690 if (sh == INVALID_SESSION_ID)
691 return VLS_INVALID_HANDLE;
693 vlsh = vls_alloc (sh);
694 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
695 vppcom_session_close (sh);
701 vls_close (vls_handle_t vlsh)
703 vcl_locked_session_t *vls;
708 vls = vls_get_and_lock (vlsh);
711 vls_table_wunlock ();
712 return VPPCOM_EBADFD;
715 vls_mt_guard (0, VLS_MT_OP_SPOOL);
716 if (vls_is_shared (vls))
718 /* At least two workers share the session so vls won't be freed */
719 vls_unshare_session (vls, vcl_worker_get_current ());
722 vls_table_wunlock ();
726 rv = vppcom_session_close (vls_to_sh (vls));
730 vls_table_wunlock ();
736 vls_epoll_create (void)
738 vcl_session_handle_t sh;
741 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
744 sh = vppcom_epoll_create ();
745 if (sh == INVALID_SESSION_ID)
746 return VLS_INVALID_HANDLE;
748 vlsh = vls_alloc (sh);
749 if (vlsh == VLS_INVALID_HANDLE)
750 vppcom_session_close (sh);
756 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
758 if (vcl_n_workers () <= 1)
760 vlsl->epoll_mp_check = 1;
764 if (op == EPOLL_CTL_MOD)
767 vlsl->epoll_mp_check = 1;
768 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
772 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
773 struct epoll_event *event)
775 vcl_locked_session_t *ep_vls, *vls;
776 vcl_session_handle_t ep_sh, sh;
780 ep_vls = vls_get_and_lock (ep_vlsh);
781 vls = vls_get_and_lock (vlsh);
782 ep_sh = vls_to_sh (ep_vls);
783 sh = vls_to_sh (vls);
785 if (PREDICT_FALSE (!vlsl->epoll_mp_check))
786 vls_epoll_ctl_mp_checks (vls, op);
788 vls_table_runlock ();
790 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
793 ep_vls = vls_get (ep_vlsh);
794 vls = vls_get (vlsh);
797 vls_table_runlock ();
802 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
803 int maxevents, double wait_for_time)
805 vcl_locked_session_t *vls;
808 if (!(vls = vls_get_w_dlock (ep_vlsh)))
809 return VPPCOM_EBADFD;
810 vls_mt_guard (0, VLS_MT_OP_XPOLL);
811 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
814 vls_get_and_unlock (ep_vlsh);
819 vls_select_mp_checks (vcl_si_set * read_map)
821 vcl_locked_session_t *vls;
826 if (vcl_n_workers () <= 1)
828 vlsl->select_mp_check = 1;
835 vlsl->select_mp_check = 1;
836 wrk = vcl_worker_get_current ();
839 clib_bitmap_foreach (si, read_map, ({
840 s = vcl_session_get (wrk, si);
841 if (s->session_state == STATE_LISTEN)
843 vls = vls_get (vls_session_index_to_vlsh (si));
844 vls_mp_checks (vls, 1 /* is_add */);
851 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
852 vcl_si_set * except_map, double wait_for_time)
856 vls_mt_guard (0, VLS_MT_OP_XPOLL);
857 if (PREDICT_FALSE (!vlsl->select_mp_check))
858 vls_select_mp_checks (read_map);
859 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
865 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
867 u32 current_wrk, is_current;
868 vcl_locked_session_t *vls;
871 if (pool_elts (vcm->workers) <= 1)
874 current_wrk = vcl_get_worker_index ();
875 is_current = current_wrk == wrk->wrk_index;
879 pool_foreach (s, wrk->sessions, ({
880 vls = vls_get (vls_si_to_vlsh (s->session_index));
881 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
882 vls_unshare_session (vls, wrk);
886 vls_table_wunlock ();
890 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
892 /* Unshare sessions and also cleanup worker since child may have
893 * called _exit () and therefore vcl may not catch the event */
894 vls_unshare_vcl_worker_sessions (wrk);
895 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
899 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
901 vcl_worker_t *sub_child;
904 if (child_wrk->forked_child != ~0)
906 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
909 /* Wait a bit, maybe the process is going away */
910 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
912 if (kill (sub_child->current_pid, 0) < 0)
913 vls_cleanup_forked_child (child_wrk, sub_child);
916 vls_cleanup_vcl_worker (child_wrk);
917 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
918 wrk->forked_child = ~0;
921 static struct sigaction old_sa;
924 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
926 vcl_worker_t *wrk, *child_wrk;
928 if (vcl_get_worker_index () == ~0)
931 if (sigaction (SIGCHLD, &old_sa, 0))
933 VERR ("couldn't restore sigchld");
937 wrk = vcl_worker_get_current ();
938 if (wrk->forked_child == ~0)
941 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
945 if (si && si->si_pid != child_wrk->current_pid)
947 VDBG (0, "unexpected child pid %u", si->si_pid);
950 vls_cleanup_forked_child (wrk, child_wrk);
953 if (old_sa.sa_flags & SA_SIGINFO)
955 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
960 void (*fn) (int) = old_sa.sa_handler;
967 vls_incercept_sigchld ()
970 clib_memset (&sa, 0, sizeof (sa));
971 sa.sa_sigaction = vls_intercept_sigchld_handler;
972 sa.sa_flags = SA_SIGINFO;
973 if (sigaction (SIGCHLD, &sa, &old_sa))
975 VERR ("couldn't intercept sigchld");
981 vls_app_pre_fork (void)
983 vls_incercept_sigchld ();
984 vcl_flush_mq_events ();
988 vls_app_fork_child_handler (void)
990 vcl_worker_t *parent_wrk;
991 int rv, parent_wrk_index;
994 parent_wrk_index = vcl_get_worker_index ();
995 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1001 vcl_set_worker_index (~0);
1002 if (!vcl_worker_alloc_and_init ())
1003 VERR ("couldn't allocate new worker");
1006 * Attach to binary api
1008 child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
1009 vcl_cleanup_bapi ();
1010 vppcom_api_hookup ();
1011 vcm->app_state = STATE_APP_START;
1012 rv = vppcom_connect_to_vpp ((char *) child_name);
1013 vec_free (child_name);
1016 VERR ("couldn't connect to VPP!");
1021 * Register worker with vpp and share sessions
1023 vcl_worker_register_with_vpp ();
1024 parent_wrk = vcl_worker_get (parent_wrk_index);
1025 vls_worker_copy_on_fork (parent_wrk);
1026 parent_wrk->forked_child = vcl_get_worker_index ();
1028 /* Reset number of threads and set wrk index */
1029 vlsl->vls_mt_n_threads = 0;
1030 vlsl->vls_wrk_index = vcl_get_worker_index ();
1031 vlsl->select_mp_check = 0;
1032 vlsl->epoll_mp_check = 0;
1033 vls_mt_locks_init ();
1035 VDBG (0, "forked child main worker initialized");
1040 vls_app_fork_parent_handler (void)
1043 while (vcm->forking)
1050 /* Unshare the sessions. VCL will clean up the worker */
1051 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1055 vls_app_create (char *app_name)
1059 if ((rv = vppcom_app_create (app_name)))
1061 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1062 clib_memset (vlsm, 0, sizeof (*vlsm));
1063 clib_rwlock_init (&vlsm->vls_table_lock);
1064 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1065 vls_app_fork_child_handler);
1066 atexit (vls_app_exit);
1067 vlsl->vls_wrk_index = vcl_get_worker_index ();
1068 vls_mt_locks_init ();
1073 * fd.io coding-style-patch-verification: ON
1076 * eval: (c-set-style "gnu")