session: add startup option to poll in main
[vpp.git] / src / vcl / vcl_locked.c
index 02da8cd..35061e7 100644 (file)
@@ -39,7 +39,7 @@ typedef struct vcl_locked_session_
 typedef struct vls_worker_
 {
   vcl_locked_session_t *vls_pool;
-  uword *session_index_to_vlsh_table;
+  uword *session_handle_to_vlsh_table;
   u32 wrk_index;
 } vls_worker_t;
 
@@ -169,28 +169,28 @@ vls_shared_data_pool_runlock (void)
 }
 
 static inline void
-vls_table_rlock (void)
+vls_mt_table_rlock (void)
 {
   if (vlsl->vls_mt_n_threads > 1)
     clib_rwlock_reader_lock (&vlsm->vls_table_lock);
 }
 
 static inline void
-vls_table_runlock (void)
+vls_mt_table_runlock (void)
 {
   if (vlsl->vls_mt_n_threads > 1)
     clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
 }
 
 static inline void
-vls_table_wlock (void)
+vls_mt_table_wlock (void)
 {
   if (vlsl->vls_mt_n_threads > 1)
     clib_rwlock_writer_lock (&vlsm->vls_table_lock);
 }
 
 static inline void
-vls_table_wunlock (void)
+vls_mt_table_wunlock (void)
 {
   if (vlsl->vls_mt_n_threads > 1)
     clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
@@ -214,6 +214,10 @@ static void
 vls_mt_add (void)
 {
   vlsl->vls_mt_n_threads += 1;
+
+  /* If multi-thread workers are supported, for each new thread register a new
+   * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
+   * update the vcl worker's thread local worker index variable */
   if (vls_mt_wrk_supported ())
     vls_register_vcl_worker ();
   else
@@ -282,7 +286,7 @@ vls_to_sh_tu (vcl_locked_session_t * vls)
 {
   vcl_session_handle_t sh;
   sh = vls_to_sh (vls);
-  vls_table_runlock ();
+  vls_mt_table_runlock ();
   return sh;
 }
 
@@ -304,7 +308,7 @@ vls_worker_alloc (void)
 static void
 vls_worker_free (vls_worker_t * wrk)
 {
-  hash_free (wrk->session_index_to_vlsh_table);
+  hash_free (wrk->session_handle_to_vlsh_table);
   pool_free (wrk->vls_pool);
   pool_put (vlsm->workers, wrk);
 }
@@ -323,15 +327,14 @@ vls_alloc (vcl_session_handle_t sh)
   vls_worker_t *wrk = vls_worker_get_current ();
   vcl_locked_session_t *vls;
 
-  vls_table_wlock ();
+  vls_mt_table_wlock ();
 
   pool_get_zero (wrk->vls_pool, vls);
   vls->session_index = vppcom_session_index (sh);
   vls->worker_index = vppcom_session_worker (sh);
   vls->vls_index = vls - wrk->vls_pool;
   vls->shared_data_index = ~0;
-  hash_set (wrk->session_index_to_vlsh_table, vls->session_index,
-           vls->vls_index);
+  hash_set (wrk->session_handle_to_vlsh_table, sh, vls->vls_index);
   if (vls_mt_wrk_supported ())
     {
       hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
@@ -340,7 +343,7 @@ vls_alloc (vcl_session_handle_t sh)
     }
   clib_spinlock_init (&vls->lock);
 
-  vls_table_wunlock ();
+  vls_mt_table_wunlock ();
   return vls->vls_index;
 }
 
@@ -359,7 +362,8 @@ vls_free (vcl_locked_session_t * vls)
   vls_worker_t *wrk = vls_worker_get_current ();
 
   ASSERT (vls != 0);
-  hash_unset (wrk->session_index_to_vlsh_table, vls->session_index);
+  hash_unset (wrk->session_handle_to_vlsh_table,
+             vcl_session_handle_from_index (vls->session_index));
   clib_spinlock_free (&vls->lock);
   pool_put (wrk->vls_pool, vls);
 }
@@ -380,10 +384,10 @@ static vcl_locked_session_t *
 vls_get_w_dlock (vls_handle_t vlsh)
 {
   vcl_locked_session_t *vls;
-  vls_table_rlock ();
+  vls_mt_table_rlock ();
   vls = vls_get_and_lock (vlsh);
   if (!vls)
-    vls_table_runlock ();
+    vls_mt_table_runlock ();
   return vls;
 }
 
@@ -391,17 +395,17 @@ static inline void
 vls_get_and_unlock (vls_handle_t vlsh)
 {
   vcl_locked_session_t *vls;
-  vls_table_rlock ();
+  vls_mt_table_rlock ();
   vls = vls_get (vlsh);
   vls_unlock (vls);
-  vls_table_runlock ();
+  vls_mt_table_runlock ();
 }
 
 static inline void
 vls_dunlock (vcl_locked_session_t * vls)
 {
   vls_unlock (vls);
-  vls_table_runlock ();
+  vls_mt_table_runlock ();
 }
 
 static vcl_locked_session_t *
@@ -435,11 +439,13 @@ vlsh_to_session_index (vls_handle_t vlsh)
 }
 
 vls_handle_t
-vls_si_to_vlsh (u32 session_index)
+vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
 {
   vls_worker_t *wrk = vls_worker_get_current ();
   uword *vlshp;
-  vlshp = hash_get (wrk->session_index_to_vlsh_table, session_index);
+  vlshp = hash_get (wrk->session_handle_to_vlsh_table,
+                   vcl_session_handle_from_wrk_session_index (session_index,
+                                                              vcl_wrk_index));
   return vlshp ? *vlshp : VLS_INVALID_HANDLE;
 }
 
@@ -448,9 +454,9 @@ 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 ();
+  vls_mt_table_rlock ();
+  vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
+  vls_mt_table_runlock ();
 
   return vlsh;
 }
@@ -560,10 +566,10 @@ vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
 
   wrk = vcl_worker_get (wrk_index);
   s = vcl_session_get (wrk, vls->session_index);
-  if (s->session_state != STATE_LISTEN)
+  if (s->session_state != VCL_STATE_LISTEN)
     return;
   vcl_send_session_unlisten (wrk, s);
-  s->session_state = STATE_LISTEN_NO_MQ;
+  s->session_state = VCL_STATE_LISTEN_NO_MQ;
   vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
 }
 
@@ -593,7 +599,7 @@ vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
     return 0;
 
   s = vcl_session_get (wrk, vls->session_index);
-  if (s->session_state == STATE_LISTEN)
+  if (s->session_state == VCL_STATE_LISTEN)
     vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
 
   vls_shared_data_pool_rlock ();
@@ -623,7 +629,7 @@ vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
    * Cleanup vcl state
    */
   n_subscribers = vec_len (vls_shd->workers_subscribed);
-  do_disconnect = s->session_state == STATE_LISTEN || !n_subscribers;
+  do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
   vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
 
   /*
@@ -718,9 +724,9 @@ vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
       svm_fifo_add_subscriber (s->rx_fifo, vcl_wrk->vpp_wrk_index);
       svm_fifo_add_subscriber (s->tx_fifo, vcl_wrk->vpp_wrk_index);
     }
-  else if (s->session_state == STATE_LISTEN)
+  else if (s->session_state == VCL_STATE_LISTEN)
     {
-      s->session_state = STATE_LISTEN_NO_MQ;
+      s->session_state = VCL_STATE_LISTEN_NO_MQ;
     }
 }
 
@@ -748,11 +754,12 @@ vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
 {
   vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
   vcl_worker_t *wrk = vcl_worker_get_current ();
+  u32 vls_index, session_index, wrk_index;
+  vcl_session_handle_t sh;
 
   /*
    * init vcl worker
    */
-  wrk->vpp_event_queues = vec_dup (parent_wrk->vpp_event_queues);
   wrk->sessions = pool_dup (parent_wrk->sessions);
   wrk->session_index_by_vpp_handles =
     hash_dup (parent_wrk->session_index_by_vpp_handles);
@@ -761,8 +768,14 @@ vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
    * init vls worker
    */
   vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
-  vls_wrk->session_index_to_vlsh_table =
-    hash_dup (vls_parent_wrk->session_index_to_vlsh_table);
+  /* *INDENT-OFF* */
+  hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
+    ({
+      vcl_session_handle_parse (sh, &wrk_index, &session_index);
+      hash_set (vls_wrk->session_handle_to_vlsh_table,
+               vcl_session_handle_from_index (session_index), vls_index);
+    }));
+  /* *INDENT-ON* */
   vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
 
   vls_share_sessions (vls_parent_wrk, vls_wrk);
@@ -780,7 +793,7 @@ vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
       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);
+      is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
     }
 
   switch (op)
@@ -826,8 +839,15 @@ vls_mt_rel_locks (int locks_acq)
     vls_mt_create_unlock ();
 }
 
+static inline u8
+vls_mt_session_should_migrate (vcl_locked_session_t * vls)
+{
+  return (vls_mt_wrk_supported ()
+         && vls->worker_index != vcl_get_worker_index ());
+}
+
 static void
-vls_session_migrate (vcl_locked_session_t * vls)
+vls_mt_session_migrate (vcl_locked_session_t * vls)
 {
   u32 wrk_index = vcl_get_worker_index ();
   vcl_worker_t *wrk;
@@ -835,11 +855,12 @@ vls_session_migrate (vcl_locked_session_t * vls)
   vcl_session_t *session;
   uword *p;
 
-  if (!vls_mt_wrk_supported ())
-    return;
+  ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
 
-  if (PREDICT_TRUE (vls->worker_index == wrk_index))
-    return;
+  /*
+   * VCL session on current vcl worker already allocated. Update current
+   * owner worker and index and return
+   */
   if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
     {
       vls->worker_index = wrk_index;
@@ -847,7 +868,11 @@ vls_session_migrate (vcl_locked_session_t * vls)
       return;
     }
 
-  /* migrate from orignal vls */
+  /*
+   * Ask vcl worker that owns the original vcl session to clone it into
+   * current vcl worker session pool
+   */
+
   if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
                      vls->owner_vcl_wrk_index)))
     {
@@ -870,15 +895,16 @@ vls_session_migrate (vcl_locked_session_t * vls)
   VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
        vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
 
-  if (PREDICT_FALSE (session->is_vep && session->vep.next_sh != ~0))
+  if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP)
+                    && session->vep.next_sh != ~0))
     {
       /* TODO: rollback? */
       VERR ("can't migrate nonempty epoll session");
       ASSERT (0);
       return;
     }
-  else if (PREDICT_FALSE (!session->is_vep &&
-                         session->session_state != STATE_CLOSED))
+  else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
+                         session->session_state != VCL_STATE_CLOSED))
     {
       /* TODO: rollback? */
       VERR ("migrate NOT supported, session_status (%u)",
@@ -888,19 +914,30 @@ vls_session_migrate (vcl_locked_session_t * vls)
     }
 }
 
-#define vls_mt_guard(_vls, _op)                                \
-  int _locks_acq = 0;                                  \
-  if (PREDICT_FALSE (vcl_get_worker_index () == ~0))   \
-    vls_mt_add ();                                     \
-  if (PREDICT_FALSE (_vls && vls_mt_wrk_supported () && \
-      ((vcl_locked_session_t *)_vls)->worker_index !=  \
-      vcl_get_worker_index ()))                                \
-    vls_session_migrate (_vls);                                \
-  if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1))      \
-    vls_mt_acq_locks (_vls, _op, &_locks_acq);         \
+static inline void
+vls_mt_detect (void)
+{
+  if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
+    vls_mt_add ();
+}
 
-#define vls_mt_unguard()                               \
-  if (PREDICT_FALSE (_locks_acq))                      \
+#define vls_mt_guard(_vls, _op)                                                \
+  int _locks_acq = 0;                                                  \
+  if (vls_mt_wrk_supported ())                                         \
+    {                                                                  \
+      if (PREDICT_FALSE (_vls                                          \
+           && ((vcl_locked_session_t *)_vls)->worker_index !=          \
+               vcl_get_worker_index ()))                               \
+         vls_mt_session_migrate (_vls);                                \
+    }                                                                  \
+  else                                                                 \
+    {                                                                  \
+      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
@@ -909,6 +946,7 @@ vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
 
@@ -925,6 +963,7 @@ vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (vls, VLS_MT_OP_WRITE);
@@ -941,6 +980,7 @@ vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (vls, VLS_MT_OP_WRITE);
@@ -956,6 +996,7 @@ vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (vls, VLS_MT_OP_READ);
@@ -972,6 +1013,7 @@ vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (vls, VLS_MT_OP_READ);
@@ -988,12 +1030,11 @@ 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 ();
-
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
-  vls_session_migrate (vls);
+  if (vls_mt_session_should_migrate (vls))
+    vls_mt_session_migrate (vls);
   rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
   vls_get_and_unlock (vlsh);
   return rv;
@@ -1005,6 +1046,7 @@ vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
@@ -1018,6 +1060,7 @@ vls_listen (vls_handle_t vlsh, int q_len)
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (vls, VLS_MT_OP_XPOLL);
@@ -1033,6 +1076,7 @@ vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (vls, VLS_MT_OP_XPOLL);
@@ -1055,7 +1099,7 @@ vls_mp_checks (vcl_locked_session_t * vls, int is_add)
   s = vcl_session_get (wrk, vls->session_index);
   switch (s->session_state)
     {
-    case STATE_LISTEN:
+    case VCL_STATE_LISTEN:
       if (is_add)
        {
          vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
@@ -1063,7 +1107,7 @@ vls_mp_checks (vcl_locked_session_t * vls, int is_add)
        }
       vls_listener_wrk_stop_listen (vls, vls->worker_index);
       break;
-    case STATE_LISTEN_NO_MQ:
+    case VCL_STATE_LISTEN_NO_MQ:
       if (!is_add)
        break;
 
@@ -1093,6 +1137,7 @@ vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
   vcl_locked_session_t *vls;
   int sh;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (listener_vlsh)))
     return VPPCOM_EBADFD;
   if (vcl_n_workers () > 1)
@@ -1115,6 +1160,7 @@ vls_create (uint8_t proto, uint8_t is_nonblocking)
   vcl_session_handle_t sh;
   vls_handle_t vlsh;
 
+  vls_mt_detect ();
   vls_mt_guard (0, VLS_MT_OP_SPOOL);
   sh = vppcom_session_create (proto, is_nonblocking);
   vls_mt_unguard ();
@@ -1129,21 +1175,20 @@ vls_create (uint8_t proto, uint8_t is_nonblocking)
 }
 
 static void
-vls_migrate_session_close (vcl_locked_session_t * vls)
+vls_mt_session_cleanup (vcl_locked_session_t * vls)
 {
-  u32 session_index, wrk_index;
+  u32 session_index, wrk_index, current_vcl_wrk;
   vcl_worker_t *wrk = vcl_worker_get_current ();
 
-  if (!vls_mt_wrk_supported ())
-    return;
+  ASSERT (vls_mt_wrk_supported ());
+
+  current_vcl_wrk = vcl_get_worker_index ();
 
   /* *INDENT-OFF* */
   hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
     ({
-      if (vcl_get_worker_index () != wrk_index)
-       {
-         vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
-       }
+      if (current_vcl_wrk != wrk_index)
+       vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
     }));
   /* *INDENT-ON* */
   hash_free (vls->vcl_wrk_index_to_session_index);
@@ -1155,12 +1200,13 @@ vls_close (vls_handle_t vlsh)
   vcl_locked_session_t *vls;
   int rv;
 
-  vls_table_wlock ();
+  vls_mt_detect ();
+  vls_mt_table_wlock ();
 
   vls = vls_get_and_lock (vlsh);
   if (!vls)
     {
-      vls_table_wunlock ();
+      vls_mt_table_wunlock ();
       return VPPCOM_EBADFD;
     }
 
@@ -1171,12 +1217,13 @@ vls_close (vls_handle_t vlsh)
   else
     rv = vppcom_session_close (vls_to_sh (vls));
 
-  vls_migrate_session_close (vls);
+  if (vls_mt_wrk_supported ())
+    vls_mt_session_cleanup (vls);
 
   vls_free (vls);
   vls_mt_unguard ();
 
-  vls_table_wunlock ();
+  vls_mt_table_wunlock ();
 
   return rv;
 }
@@ -1187,8 +1234,7 @@ vls_epoll_create (void)
   vcl_session_handle_t sh;
   vls_handle_t vlsh;
 
-  if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
-    vls_mt_add ();
+  vls_mt_detect ();
 
   sh = vppcom_epoll_create ();
   if (sh == INVALID_SESSION_ID)
@@ -1225,26 +1271,30 @@ vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
   vcl_session_handle_t ep_sh, sh;
   int rv;
 
-  vls_table_rlock ();
+  vls_mt_detect ();
+  vls_mt_table_rlock ();
   ep_vls = vls_get_and_lock (ep_vlsh);
   vls = vls_get_and_lock (vlsh);
-  vls_session_migrate (ep_vls);
+
+  if (vls_mt_session_should_migrate (ep_vls))
+    vls_mt_session_migrate (ep_vls);
+
   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 ();
+  vls_mt_table_runlock ();
 
   rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
 
-  vls_table_rlock ();
+  vls_mt_table_rlock ();
   ep_vls = vls_get (ep_vlsh);
   vls = vls_get (vlsh);
   vls_unlock (vls);
   vls_unlock (ep_vls);
-  vls_table_runlock ();
+  vls_mt_table_runlock ();
   return rv;
 }
 
@@ -1255,6 +1305,7 @@ vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
   vcl_locked_session_t *vls;
   int rv;
 
+  vls_mt_detect ();
   if (!(vls = vls_get_w_dlock (ep_vlsh)))
     return VPPCOM_EBADFD;
   vls_mt_guard (0, VLS_MT_OP_XPOLL);
@@ -1288,7 +1339,7 @@ vls_select_mp_checks (vcl_si_set * read_map)
   /* *INDENT-OFF* */
   clib_bitmap_foreach (si, read_map, ({
     s = vcl_session_get (wrk, si);
-    if (s->session_state == STATE_LISTEN)
+    if (s->session_state == VCL_STATE_LISTEN)
       {
        vls = vls_get (vls_session_index_to_vlsh (si));
        vls_mp_checks (vls, 1 /* is_add */);
@@ -1303,6 +1354,7 @@ vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
 {
   int rv;
 
+  vls_mt_detect ();
   vls_mt_guard (0, VLS_MT_OP_XPOLL);
   if (PREDICT_FALSE (!vlsl->select_mp_check))
     vls_select_mp_checks (read_map);
@@ -1326,7 +1378,7 @@ vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
 
   /* *INDENT-OFF* */
   pool_foreach (s, wrk->sessions, ({
-    vls = vls_get (vls_si_to_vlsh (s->session_index));
+    vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
     if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
       vls_unshare_session (vls, wrk);
   }));
@@ -1439,44 +1491,30 @@ static void
 vls_app_fork_child_handler (void)
 {
   vcl_worker_t *parent_wrk;
-  int rv, parent_wrk_index;
-  u8 *child_name;
+  int parent_wrk_index;
 
   parent_wrk_index = vcl_get_worker_index ();
   VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
        parent_wrk_index);
 
   /*
-   * Allocate worker vcl
+   * Clear old state
    */
   vcl_set_worker_index (~0);
-  if (!vcl_worker_alloc_and_init ())
-    VERR ("couldn't allocate new worker");
 
   /*
-   * Attach to binary api
+   * Allocate and register vcl worker with vpp
    */
-  child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
-  vcl_cleanup_bapi ();
-  vppcom_api_hookup ();
-  vcm->app_state = STATE_APP_START;
-  rv = vppcom_connect_to_vpp ((char *) child_name);
-  vec_free (child_name);
-  if (rv)
+  if (vppcom_worker_register ())
     {
-      VERR ("couldn't connect to VPP!");
+      VERR ("couldn't register new worker!");
       return;
     }
 
   /*
-   * Allocate/initialize vls worker
+   * Allocate/initialize vls worker and share sessions
    */
   vls_worker_alloc ();
-
-  /*
-   * Register worker with vpp and share sessions
-   */
-  vcl_worker_register_with_vpp ();
   parent_wrk = vcl_worker_get (parent_wrk_index);
   vls_worker_copy_on_fork (parent_wrk);
   parent_wrk->forked_child = vcl_get_worker_index ();
@@ -1595,8 +1633,7 @@ vls_send_clone_and_share_rpc (vcl_worker_t * wrk,
   wrk->rpc_done = 0;
   ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
 
-  VDBG (1,
-       "send session clone of worker (session): %u (%u) -> %u (%u), ret=%d",
+  VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
        dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
        msg->origin_session_index, ret);
   while (!ret && !wrk->rpc_done)
@@ -1621,8 +1658,7 @@ vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
   wrk->rpc_done = 0;
   ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
 
-  VDBG (1,
-       "send session cleanup of worker (session): %u (%u) from %u (), ret=%d",
+  VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
        dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
   while (!ret && !wrk->rpc_done)
     ;
@@ -1661,7 +1697,7 @@ vls_use_eventfd (void)
 unsigned char
 vls_mt_wrk_supported (void)
 {
-  return vcm->cfg.mt_supported;
+  return vcm->cfg.mt_wrk_supported;
 }
 
 int