X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvcl%2Fvcl_locked.c;h=d69d391d626bb5f7a6d188ae90b403f65d994065;hb=b020806806c0e6c54886cdb4347a5fd1f19504b0;hp=c6c79cdd470eb8baf7f43492ea781922a4253be1;hpb=f9240dc9207b677038183ca29888d4be89bb88d5;p=vpp.git diff --git a/src/vcl/vcl_locked.c b/src/vcl/vcl_locked.c index c6c79cdd470..d69d391d626 100644 --- a/src/vcl/vcl_locked.c +++ b/src/vcl/vcl_locked.c @@ -22,19 +22,31 @@ typedef struct vcl_locked_session_ u32 session_index; u32 worker_index; u32 vls_index; - u32 flags; u32 *workers_subscribed; + clib_bitmap_t *listeners; } vcl_locked_session_t; -typedef struct vcl_main_ +typedef struct vls_local_ +{ + int vls_wrk_index; + volatile int vls_mt_n_threads; + pthread_mutex_t vls_mt_mq_mlock; + pthread_mutex_t vls_mt_spool_mlock; + volatile u8 select_mp_check; + volatile u8 epoll_mp_check; +} vls_process_local_t; + +static vls_process_local_t vls_local; +static vls_process_local_t *vlsl = &vls_local; + +typedef struct vls_main_ { vcl_locked_session_t *vls_pool; clib_rwlock_t vls_table_lock; uword *session_index_to_vlsh_table; } vls_main_t; -vls_main_t vls_main; -vls_main_t *vlsm = &vls_main; +vls_main_t *vlsm; static inline void vls_table_rlock (void) @@ -60,6 +72,58 @@ vls_table_wunlock (void) clib_rwlock_writer_unlock (&vlsm->vls_table_lock); } +typedef enum +{ + VLS_MT_OP_READ, + VLS_MT_OP_WRITE, + VLS_MT_OP_SPOOL, + VLS_MT_OP_XPOLL, +} vls_mt_ops_t; + +typedef enum +{ + VLS_MT_LOCK_MQ = 1 << 0, + VLS_MT_LOCK_SPOOL = 1 << 1 +} vls_mt_lock_type_t; + +static void +vls_mt_add (void) +{ + vlsl->vls_mt_n_threads += 1; + vcl_set_worker_index (vlsl->vls_wrk_index); +} + +static inline void +vls_mt_mq_lock (void) +{ + pthread_mutex_lock (&vlsl->vls_mt_mq_mlock); +} + +static inline void +vls_mt_mq_unlock (void) +{ + pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock); +} + +static inline void +vls_mt_spool_lock (void) +{ + pthread_mutex_lock (&vlsl->vls_mt_spool_mlock); +} + +static inline void +vls_mt_create_unlock (void) +{ + pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock); +} + +static void +vls_mt_locks_init (void) +{ + pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL); + pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL); +} + static inline vcl_session_handle_t vls_to_sh (vcl_locked_session_t * vls) { @@ -101,12 +165,12 @@ vls_get (vls_handle_t vlsh) } static void -vls_free (vcl_locked_session_t * fde) +vls_free (vcl_locked_session_t * vls) { - ASSERT (fde != 0); - hash_unset (vlsm->session_index_to_vlsh_table, fde->session_index); - clib_spinlock_free (&fde->lock); - pool_put (vlsm->vls_pool, fde); + ASSERT (vls != 0); + hash_unset (vlsm->session_index_to_vlsh_table, vls->session_index); + clib_spinlock_free (&vls->lock); + pool_put (vlsm->vls_pool, vls); } static vcl_locked_session_t * @@ -131,6 +195,12 @@ vls_get_w_dlock (vls_handle_t vlsh) return vls; } +static inline void +vls_lock (vcl_locked_session_t * vls) +{ + clib_spinlock_lock (&vls->lock); +} + static inline void vls_unlock (vcl_locked_session_t * vls) { @@ -154,15 +224,46 @@ vls_dunlock (vcl_locked_session_t * vls) vls_table_runlock (); } -static void -vls_get_and_free (vls_handle_t vlsh) +vcl_session_handle_t +vlsh_to_sh (vls_handle_t vlsh) { vcl_locked_session_t *vls; + int rv; - vls_table_wlock (); - vls = vls_get (vlsh); - vls_free (vls); - vls_table_wunlock (); + vls = vls_get_w_dlock (vlsh); + if (!vls) + return INVALID_SESSION_ID; + rv = vls_to_sh (vls); + vls_dunlock (vls); + return rv; +} + +vcl_session_handle_t +vlsh_to_session_index (vls_handle_t vlsh) +{ + vcl_session_handle_t sh; + sh = vlsh_to_sh (vlsh); + return vppcom_session_index (sh); +} + +vls_handle_t +vls_si_to_vlsh (u32 session_index) +{ + uword *vlshp; + vlshp = hash_get (vlsm->session_index_to_vlsh_table, session_index); + return vlshp ? *vlshp : VLS_INVALID_HANDLE; +} + +vls_handle_t +vls_session_index_to_vlsh (uint32_t session_index) +{ + vls_handle_t vlsh; + + vls_table_rlock (); + vlsh = vls_si_to_vlsh (session_index); + vls_table_runlock (); + + return vlsh; } u8 @@ -171,41 +272,94 @@ vls_is_shared (vcl_locked_session_t * vls) return vec_len (vls->workers_subscribed); } +u8 +vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index) +{ + int i; + for (i = 0; i < vec_len (vls->workers_subscribed); i++) + if (vls->workers_subscribed[i] == wrk_index) + return 1; + return 0; +} + +static void +vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active) +{ + clib_bitmap_set (vls->listeners, wrk_index, is_active); +} + +static u8 +vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index) +{ + return (clib_bitmap_get (vls->listeners, wrk_index) == 1); +} + +static void +vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index) +{ + vppcom_session_listen (vls_to_sh (vls), ~0); + vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ ); +} + +static void +vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index) +{ + vcl_worker_t *wrk; + vcl_session_t *s; + + wrk = vcl_worker_get (wrk_index); + s = vcl_session_get (wrk, vls->session_index); + if (s->session_state != STATE_LISTEN) + return; + vcl_send_session_unlisten (wrk, s); + s->session_state = STATE_LISTEN_NO_MQ; + vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ ); +} + int -vls_unshare_session (vcl_locked_session_t * vls) +vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk) { - vcl_worker_t *wrk = vcl_worker_get_current (); + int i, do_disconnect; vcl_session_t *s; - int i; + + s = vcl_session_get (wrk, vls->session_index); + if (s->session_state == STATE_LISTEN) + vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ ); for (i = 0; i < vec_len (vls->workers_subscribed); i++) { if (vls->workers_subscribed[i] != wrk->wrk_index) continue; - s = vcl_session_get (wrk, vls->session_index); if (s->rx_fifo) { svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index); svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index); } vec_del1 (vls->workers_subscribed, i); - vcl_session_cleanup (wrk, s, vcl_session_handle (s), - 0 /* do_disconnect */ ); + do_disconnect = s->session_state == STATE_LISTEN; + vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect); return 0; } - /* Assumption is that unshare is only called if session is shared. - * So shared_workers must be non-empty if the worker is the owner */ - if (vls->worker_index == wrk->wrk_index) + /* Return, if this is not the owning worker */ + if (vls->worker_index != wrk->wrk_index) + return 0; + + /* Check if we can change owner or close */ + if (vec_len (vls->workers_subscribed)) { - s = vcl_session_get (wrk, vls->session_index); vls->worker_index = vls->workers_subscribed[0]; vec_del1 (vls->workers_subscribed, 0); vcl_send_session_worker_update (wrk, s, vls->worker_index); if (vec_len (vls->workers_subscribed)) clib_warning ("more workers need to be updated"); } + else + { + vcl_session_cleanup (wrk, s, vcl_session_handle (s), + 1 /* do_disconnect */ ); + } return 0; } @@ -215,16 +369,22 @@ vls_share_vcl_session (vcl_worker_t * wrk, vcl_session_t * s) { vcl_locked_session_t *vls; - vls = vls_get_w_dlock (vls_session_index_to_vlsh (s->session_index)); + vls = vls_get (vls_si_to_vlsh (s->session_index)); if (!vls) return; + vls_lock (vls); vec_add1 (vls->workers_subscribed, wrk->wrk_index); if (s->rx_fifo) { svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index); svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index); } - vls_dunlock (vls); + else if (s->session_state == STATE_LISTEN) + { + s->session_state = STATE_LISTEN_NO_MQ; + } + + vls_unlock (vls); } void @@ -237,14 +397,86 @@ vls_worker_copy_on_fork (vcl_worker_t * parent_wrk) wrk->sessions = pool_dup (parent_wrk->sessions); wrk->session_index_by_vpp_handles = hash_dup (parent_wrk->session_index_by_vpp_handles); + vls_table_rlock (); /* *INDENT-OFF* */ pool_foreach (s, wrk->sessions, ({ vls_share_vcl_session (wrk, s); })); /* *INDENT-ON* */ + + vls_table_runlock (); } +static void +vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq) +{ + vcl_worker_t *wrk = vcl_worker_get_current (); + vcl_session_t *s = 0; + int is_nonblk = 0; + + if (vls) + { + s = vcl_session_get (wrk, vls->session_index); + if (PREDICT_FALSE (!s)) + return; + is_nonblk = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK); + } + + switch (op) + { + case VLS_MT_OP_READ: + if (!is_nonblk) + is_nonblk = vcl_session_read_ready (s) != 0; + if (!is_nonblk) + { + vls_mt_mq_lock (); + *locks_acq |= VLS_MT_LOCK_MQ; + } + break; + case VLS_MT_OP_WRITE: + ASSERT (s); + if (!is_nonblk) + is_nonblk = vcl_session_write_ready (s) != 0; + if (!is_nonblk) + { + vls_mt_mq_lock (); + *locks_acq |= VLS_MT_LOCK_MQ; + } + break; + case VLS_MT_OP_XPOLL: + vls_mt_mq_lock (); + *locks_acq |= VLS_MT_LOCK_MQ; + break; + case VLS_MT_OP_SPOOL: + vls_mt_spool_lock (); + *locks_acq |= VLS_MT_LOCK_SPOOL; + break; + default: + break; + } +} + +static void +vls_mt_rel_locks (int locks_acq) +{ + if (locks_acq & VLS_MT_LOCK_MQ) + vls_mt_mq_unlock (); + if (locks_acq & VLS_MT_LOCK_SPOOL) + vls_mt_create_unlock (); +} + +#define vls_mt_guard(_vls, _op) \ + int _locks_acq = 0; \ + if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) \ + vls_mt_add (); \ + if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \ + vls_mt_acq_locks (_vls, _op, &_locks_acq); \ + +#define vls_mt_unguard() \ + if (PREDICT_FALSE (_locks_acq)) \ + vls_mt_rel_locks (_locks_acq) + int vls_write (vls_handle_t vlsh, void *buf, size_t nbytes) { @@ -253,7 +485,10 @@ vls_write (vls_handle_t vlsh, void *buf, size_t nbytes) if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + + vls_mt_guard (vls, VLS_MT_OP_WRITE); rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } @@ -266,7 +501,9 @@ vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes) if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (vls, VLS_MT_OP_WRITE); rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } @@ -280,7 +517,9 @@ vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags, if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (vls, VLS_MT_OP_WRITE); rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } @@ -293,7 +532,9 @@ vls_read (vls_handle_t vlsh, void *buf, size_t nbytes) if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (vls, VLS_MT_OP_READ); rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } @@ -307,8 +548,10 @@ vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags, if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (vls, VLS_MT_OP_READ); rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags, ep); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } @@ -319,6 +562,9 @@ vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen) vcl_locked_session_t *vls; int rv; + if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) + vls_mt_add (); + if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen); @@ -347,7 +593,9 @@ vls_listen (vls_handle_t vlsh, int q_len) if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (vls, VLS_MT_OP_XPOLL); rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } @@ -360,11 +608,53 @@ vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep) if (!(vls = vls_get_w_dlock (vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (vls, VLS_MT_OP_XPOLL); rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep); + vls_mt_unguard (); vls_get_and_unlock (vlsh); return rv; } +static inline void +vls_mp_checks (vcl_locked_session_t * vls, int is_add) +{ + vcl_worker_t *wrk = vcl_worker_get_current (); + vcl_session_t *s; + + s = vcl_session_get (wrk, vls->session_index); + switch (s->session_state) + { + case STATE_LISTEN: + if (is_add) + { + if (vls->worker_index == wrk->wrk_index) + vls_listener_wrk_set (vls, wrk->wrk_index, 1 /* is_active */ ); + break; + } + vls_listener_wrk_stop_listen (vls, vls->worker_index); + break; + case STATE_LISTEN_NO_MQ: + if (!is_add) + break; + + /* Register worker as listener */ + vls_listener_wrk_start_listen (vls, wrk->wrk_index); + + /* If owner worker did not attempt to accept/xpoll on the session, + * force a listen stop for it, since it may not be interested in + * accepting new sessions. + * This is pretty much a hack done to give app workers the illusion + * that it is fine to listen and not accept new sessions for a + * given listener. Without it, we would accumulate unhandled + * accepts on the passive worker message queue. */ + if (!vls_listener_wrk_is_active (vls, vls->worker_index)) + vls_listener_wrk_stop_listen (vls, vls->worker_index); + break; + default: + break; + } +} + vls_handle_t vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags) { @@ -374,7 +664,11 @@ vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags) if (!(vls = vls_get_w_dlock (listener_vlsh))) return VPPCOM_EBADFD; + if (vcl_n_workers () > 1) + vls_mp_checks (vls, 1 /* is_add */ ); + vls_mt_guard (vls, VLS_MT_OP_SPOOL); sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags); + vls_mt_unguard (); vls_get_and_unlock (listener_vlsh); if (sh < 0) return sh; @@ -390,7 +684,9 @@ vls_create (uint8_t proto, uint8_t is_nonblocking) vcl_session_handle_t sh; vls_handle_t vlsh; + vls_mt_guard (0, VLS_MT_OP_SPOOL); sh = vppcom_session_create (proto, is_nonblocking); + vls_mt_unguard (); if (sh == INVALID_SESSION_ID) return VLS_INVALID_HANDLE; @@ -405,28 +701,34 @@ int vls_close (vls_handle_t vlsh) { vcl_locked_session_t *vls; - vcl_session_handle_t sh; int rv; - if (!(vls = vls_get_w_dlock (vlsh))) - return VPPCOM_EBADFD; + vls_table_wlock (); - if (vls_is_shared (vls)) + vls = vls_get_and_lock (vlsh); + if (!vls) { - vls_unshare_session (vls); - vls_dunlock (vls); - return VPPCOM_OK; + vls_table_wunlock (); + return VPPCOM_EBADFD; } - sh = vls_to_sh (vls); - if ((rv = vppcom_session_close (sh))) + vls_mt_guard (0, VLS_MT_OP_SPOOL); + if (vls_is_shared (vls)) { - vls_dunlock (vls); - return rv; + /* At least two workers share the session so vls won't be freed */ + vls_unshare_session (vls, vcl_worker_get_current ()); + vls_unlock (vls); + vls_mt_unguard (); + vls_table_wunlock (); + return VPPCOM_OK; } - vls_dunlock (vls); - vls_get_and_free (vlsh); + rv = vppcom_session_close (vls_to_sh (vls)); + vls_free (vls); + vls_mt_unguard (); + + vls_table_wunlock (); + return rv; } @@ -436,6 +738,9 @@ vls_epoll_create (void) vcl_session_handle_t sh; vls_handle_t vlsh; + if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) + vls_mt_add (); + sh = vppcom_epoll_create (); if (sh == INVALID_SESSION_ID) return VLS_INVALID_HANDLE; @@ -447,6 +752,22 @@ vls_epoll_create (void) return vlsh; } +static void +vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op) +{ + if (vcl_n_workers () <= 1) + { + vlsl->epoll_mp_check = 1; + return; + } + + if (op == EPOLL_CTL_MOD) + return; + + vlsl->epoll_mp_check = 1; + vls_mp_checks (vls, op == EPOLL_CTL_ADD); +} + int vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh, struct epoll_event *event) @@ -460,6 +781,10 @@ vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh, vls = vls_get_and_lock (vlsh); ep_sh = vls_to_sh (ep_vls); sh = vls_to_sh (vls); + + if (PREDICT_FALSE (!vlsl->epoll_mp_check)) + vls_epoll_ctl_mp_checks (vls, op); + vls_table_runlock (); rv = vppcom_epoll_ctl (ep_sh, op, sh, event); @@ -482,46 +807,92 @@ vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events, if (!(vls = vls_get_w_dlock (ep_vlsh))) return VPPCOM_EBADFD; + vls_mt_guard (0, VLS_MT_OP_XPOLL); rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents, wait_for_time); + vls_mt_unguard (); vls_get_and_unlock (ep_vlsh); return rv; } -vcl_session_handle_t -vlsh_to_sh (vls_handle_t vlsh) +static void +vls_select_mp_checks (vcl_si_set * read_map) { vcl_locked_session_t *vls; - int rv; + vcl_worker_t *wrk; + vcl_session_t *s; + u32 si; - vls = vls_get_w_dlock (vlsh); - if (!vls) - return INVALID_SESSION_ID; - rv = vls_to_sh (vls); - vls_dunlock (vls); - return rv; + if (vcl_n_workers () <= 1) + { + vlsl->select_mp_check = 1; + return; + } + + if (!read_map) + return; + + vlsl->select_mp_check = 1; + wrk = vcl_worker_get_current (); + + /* *INDENT-OFF* */ + clib_bitmap_foreach (si, read_map, ({ + s = vcl_session_get (wrk, si); + if (s->session_state == STATE_LISTEN) + { + vls = vls_get (vls_session_index_to_vlsh (si)); + vls_mp_checks (vls, 1 /* is_add */); + } + })); + /* *INDENT-ON* */ } -vcl_session_handle_t -vlsh_to_session_index (vls_handle_t vlsh) +int +vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map, + vcl_si_set * except_map, double wait_for_time) { - vcl_session_handle_t sh; - sh = vlsh_to_sh (vlsh); - return vppcom_session_index (sh); + int rv; + + vls_mt_guard (0, VLS_MT_OP_XPOLL); + if (PREDICT_FALSE (!vlsl->select_mp_check)) + vls_select_mp_checks (read_map); + rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time); + vls_mt_unguard (); + return rv; } -vls_handle_t -vls_session_index_to_vlsh (uint32_t session_index) +static void +vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk) { - vls_handle_t vlsh; - uword *vlshp; + u32 current_wrk, is_current; + vcl_locked_session_t *vls; + vcl_session_t *s; - vls_table_rlock (); - vlshp = hash_get (vlsm->session_index_to_vlsh_table, session_index); - vlsh = vlshp ? *vlshp : VLS_INVALID_HANDLE; - vls_table_runlock (); + if (pool_elts (vcm->workers) <= 1) + return; - return vlsh; + current_wrk = vcl_get_worker_index (); + is_current = current_wrk == wrk->wrk_index; + vls_table_wlock (); + + /* *INDENT-OFF* */ + pool_foreach (s, wrk->sessions, ({ + vls = vls_get (vls_si_to_vlsh (s->session_index)); + if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk))) + vls_unshare_session (vls, wrk); + })); + /* *INDENT-ON* */ + + vls_table_wunlock (); +} + +static void +vls_cleanup_vcl_worker (vcl_worker_t * wrk) +{ + /* Unshare sessions and also cleanup worker since child may have + * called _exit () and therefore vcl may not catch the event */ + vls_unshare_vcl_worker_sessions (wrk); + vcl_worker_cleanup (wrk, 1 /* notify vpp */ ); } static void @@ -542,8 +913,8 @@ vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk) vls_cleanup_forked_child (child_wrk, sub_child); } } - vcl_worker_cleanup (child_wrk, 1 /* notify vpp */ ); - VDBG (0, "Cleaned up wrk %u", child_wrk->wrk_index); + vls_cleanup_vcl_worker (child_wrk); + VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index); wrk->forked_child = ~0; } @@ -654,6 +1025,13 @@ vls_app_fork_child_handler (void) vls_worker_copy_on_fork (parent_wrk); parent_wrk->forked_child = vcl_get_worker_index (); + /* Reset number of threads and set wrk index */ + vlsl->vls_mt_n_threads = 0; + vlsl->vls_wrk_index = vcl_get_worker_index (); + vlsl->select_mp_check = 0; + vlsl->epoll_mp_check = 0; + vls_mt_locks_init (); + VDBG (0, "forked child main worker initialized"); vcm->forking = 0; } @@ -666,15 +1044,28 @@ vls_app_fork_parent_handler (void) ; } +void +vls_app_exit (void) +{ + /* Unshare the sessions. VCL will clean up the worker */ + vls_unshare_vcl_worker_sessions (vcl_worker_get_current ()); +} + int vls_app_create (char *app_name) { int rv; + if ((rv = vppcom_app_create (app_name))) return rv; + vlsm = clib_mem_alloc (sizeof (vls_main_t)); + clib_memset (vlsm, 0, sizeof (*vlsm)); clib_rwlock_init (&vlsm->vls_table_lock); pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler, vls_app_fork_child_handler); + atexit (vls_app_exit); + vlsl->vls_wrk_index = vcl_get_worker_index (); + vls_mt_locks_init (); return VPPCOM_OK; }