vcl: mt detection and cleanup
[vpp.git] / src / vcl / vcl_locked.c
index ae9c976..da4522a 100644 (file)
@@ -31,6 +31,9 @@ typedef struct vcl_locked_session_
   u32 worker_index;
   u32 vls_index;
   u32 shared_data_index;
+  /** VCL session owned by different workers because of migration */
+  u32 owner_vcl_wrk_index;
+  uword *vcl_wrk_index_to_session_index;
 } vcl_locked_session_t;
 
 typedef struct vls_worker_
@@ -64,10 +67,50 @@ typedef struct vls_main_
 
 vls_main_t *vlsm;
 
+typedef enum vls_rpc_msg_type_
+{
+  VLS_RPC_CLONE_AND_SHARE,
+  VLS_RPC_SESS_CLEANUP,
+} vls_rpc_msg_type_e;
+
+typedef struct vls_rpc_msg_
+{
+  u8 type;
+  u8 data[0];
+} vls_rpc_msg_t;
+
+typedef struct vls_clone_and_share_msg_
+{
+  u32 vls_index;               /**< vls to be shared */
+  u32 session_index;           /**< vcl session to be shared */
+  u32 origin_vls_wrk;          /**< vls worker that initiated the rpc */
+  u32 origin_vls_index;                /**< vls session of the originator */
+  u32 origin_vcl_wrk;          /**< vcl worker that initiated the rpc */
+  u32 origin_session_index;    /**< vcl session of the originator */
+} vls_clone_and_share_msg_t;
+
+typedef struct vls_sess_cleanup_msg_
+{
+  u32 session_index;           /**< vcl session to be cleaned */
+  u32 origin_vcl_wrk;          /**< worker that initiated the rpc */
+} vls_sess_cleanup_msg_t;
+
+void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
+                                  u32 dst_wrk_index, u32 dst_session_index);
+void vls_send_clone_and_share_rpc (vcl_worker_t * wrk,
+                                  vcl_locked_session_t * vls,
+                                  u32 session_index, u32 vls_wrk_index,
+                                  u32 dst_wrk_index, u32 dst_vls_index,
+                                  u32 dst_session_index);
+
+
 static inline u32
 vls_get_worker_index (void)
 {
-  return vcl_get_worker_index ();
+  if (vls_mt_wrk_supported ())
+    return vlsl->vls_wrk_index;
+  else
+    return vcl_get_worker_index ();
 }
 
 static u32
@@ -126,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);
@@ -171,7 +214,14 @@ static void
 vls_mt_add (void)
 {
   vlsl->vls_mt_n_threads += 1;
-  vcl_set_worker_index (vlsl->vls_wrk_index);
+
+  /* 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
+    vcl_set_worker_index (vlsl->vls_wrk_index);
 }
 
 static inline void
@@ -236,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;
 }
 
@@ -277,7 +327,7 @@ 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);
@@ -286,9 +336,15 @@ vls_alloc (vcl_session_handle_t sh)
   vls->shared_data_index = ~0;
   hash_set (wrk->session_index_to_vlsh_table, vls->session_index,
            vls->vls_index);
+  if (vls_mt_wrk_supported ())
+    {
+      hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
+               vls->session_index);
+      vls->owner_vcl_wrk_index = vls->worker_index;
+    }
   clib_spinlock_init (&vls->lock);
 
-  vls_table_wunlock ();
+  vls_mt_table_wunlock ();
   return vls->vls_index;
 }
 
@@ -328,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;
 }
 
@@ -339,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 *
@@ -396,9 +452,9 @@ vls_session_index_to_vlsh (uint32_t session_index)
 {
   vls_handle_t vlsh;
 
-  vls_table_rlock ();
+  vls_mt_table_rlock ();
   vlsh = vls_si_to_vlsh (session_index);
-  vls_table_runlock ();
+  vls_mt_table_runlock ();
 
   return vlsh;
 }
@@ -537,7 +593,8 @@ vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
   u32 n_subscribers;
   vcl_session_t *s;
 
-  ASSERT (vls->shared_data_index != ~0);
+  if (vls->shared_data_index == ~0)
+    return 0;
 
   s = vcl_session_get (wrk, vls->session_index);
   if (s->session_state == STATE_LISTEN)
@@ -614,51 +671,50 @@ done:
 }
 
 void
-vls_share_session (vcl_locked_session_t * vls, vls_worker_t * vls_wrk,
-                  vls_worker_t * vls_parent_wrk, vcl_worker_t * vcl_wrk)
+vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
 {
-  vcl_locked_session_t *parent_vls;
+  vls_shared_data_t *vls_shd;
+
+  u32 vls_shd_index = vls_shared_data_alloc ();
+
+  vls_shared_data_pool_rlock ();
+
+  vls_shd = vls_shared_data_get (vls_shd_index);
+  vls_shd->owner_wrk_index = vls_wrk->wrk_index;
+  vls->shared_data_index = vls_shd_index;
+  vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
+
+  vls_shared_data_pool_runlock ();
+}
+
+void
+vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
+{
+  vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
   vls_shared_data_t *vls_shd;
   vcl_session_t *s;
 
   s = vcl_session_get (vcl_wrk, vls->session_index);
   if (!s)
     {
-      clib_warning ("wrk %u parent %u session %u vls %u NOT AVAILABLE",
-                   vcl_wrk->wrk_index, vls_parent_wrk->wrk_index,
-                   vls->session_index, vls->vls_index);
+      clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
+                   vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
       return;
     }
 
+  ASSERT (vls->shared_data_index != ~0);
+
   /* Reinit session lock */
   clib_spinlock_init (&vls->lock);
 
-  if (vls->shared_data_index != ~0)
-    {
-      vls_shared_data_pool_rlock ();
-      vls_shd = vls_shared_data_get (vls->shared_data_index);
-    }
-  else
-    {
-      u32 vls_shd_index = vls_shared_data_alloc ();
-
-      vls_shared_data_pool_rlock ();
-
-      vls_shd = vls_shared_data_get (vls_shd_index);
-      vls_shd->owner_wrk_index = vls_parent_wrk->wrk_index;
-      vls->shared_data_index = vls_shd_index;
+  vls_shared_data_pool_rlock ();
 
-      /* Update parent shared data */
-      parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
-      parent_vls->shared_data_index = vls_shd_index;
-      vec_add1 (vls_shd->workers_subscribed, vls_parent_wrk->wrk_index);
-    }
+  vls_shd = vls_shared_data_get (vls->shared_data_index);
 
   clib_spinlock_lock (&vls_shd->lock);
-
   vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
-
   clib_spinlock_unlock (&vls_shd->lock);
+
   vls_shared_data_pool_runlock ();
 
   if (s->rx_fifo)
@@ -675,12 +731,18 @@ vls_share_session (vcl_locked_session_t * vls, vls_worker_t * vls_wrk,
 static void
 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
 {
-  vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
-  vcl_locked_session_t *vls;
+  vcl_locked_session_t *vls, *parent_vls;
 
   /* *INDENT-OFF* */
   pool_foreach (vls, vls_wrk->vls_pool, ({
-    vls_share_session (vls, vls_wrk, vls_parent_wrk, vcl_wrk);
+    /* Initialize sharing on parent session */
+    if (vls->shared_data_index == ~0)
+      {
+       parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
+       vls_init_share_session (vls_parent_wrk, parent_vls);
+       vls->shared_data_index = parent_vls->shared_data_index;
+      }
+    vls_share_session (vls_wrk, vls);
   }));
   /* *INDENT-ON* */
 }
@@ -768,15 +830,104 @@ vls_mt_rel_locks (int locks_acq)
     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);         \
+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_mt_session_migrate (vcl_locked_session_t * vls)
+{
+  u32 wrk_index = vcl_get_worker_index ();
+  vcl_worker_t *wrk;
+  u32 src_sid, sid;
+  vcl_session_t *session;
+  uword *p;
+
+  ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
+
+  /*
+   * 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;
+      vls->session_index = (u32) p[0];
+      return;
+    }
+
+  /*
+   * 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)))
+    {
+      VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
+      ASSERT (0);
+      return;
+    }
+
+  src_sid = (u32) p[0];
+  wrk = vcl_worker_get_current ();
+  session = vcl_session_alloc (wrk);
+  sid = session->session_index;
+  vls_send_clone_and_share_rpc (wrk, vls, sid, vls_get_worker_index (),
+                               vls->owner_vcl_wrk_index, vls->vls_index,
+                               src_sid);
+  session->session_index = sid;
+  vls->worker_index = wrk_index;
+  vls->session_index = sid;
+  hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
+  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))
+    {
+      /* TODO: rollback? */
+      VERR ("can't migrate nonempty epoll session");
+      ASSERT (0);
+      return;
+    }
+  else if (PREDICT_FALSE (!session->is_vep &&
+                         session->session_state != STATE_CLOSED))
+    {
+      /* TODO: rollback? */
+      VERR ("migrate NOT supported, session_status (%u)",
+           session->session_state);
+      ASSERT (0);
+      return;
+    }
+}
 
-#define vls_mt_unguard()                               \
-  if (PREDICT_FALSE (_locks_acq))                      \
+static inline void
+vls_mt_detect (void)
+{
+  if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
+    vls_mt_add ();
+}
+
+#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
@@ -785,6 +936,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;
 
@@ -801,6 +953,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);
@@ -817,6 +970,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);
@@ -832,6 +986,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);
@@ -848,6 +1003,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);
@@ -864,11 +1020,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;
+  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;
@@ -880,6 +1036,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);
@@ -893,6 +1050,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);
@@ -908,6 +1066,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);
@@ -924,6 +1083,9 @@ vls_mp_checks (vcl_locked_session_t * vls, int is_add)
   vcl_session_t *s;
   u32 owner_wrk;
 
+  if (vls_mt_wrk_supported ())
+    return;
+
   s = vcl_session_get (wrk, vls->session_index);
   switch (s->session_state)
     {
@@ -965,6 +1127,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)
@@ -987,6 +1150,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 ();
@@ -1000,32 +1164,56 @@ vls_create (uint8_t proto, uint8_t is_nonblocking)
   return vlsh;
 }
 
+static void
+vls_mt_session_cleanup (vcl_locked_session_t * vls)
+{
+  u32 session_index, wrk_index, current_vcl_wrk;
+  vcl_worker_t *wrk = vcl_worker_get_current ();
+
+  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 (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);
+}
+
 int
 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;
     }
 
-  vls_mt_guard (0, VLS_MT_OP_SPOOL);
+  vls_mt_guard (vls, VLS_MT_OP_SPOOL);
 
   if (vls_is_shared (vls))
     rv = vls_unshare_session (vls, vcl_worker_get_current ());
   else
     rv = vppcom_session_close (vls_to_sh (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;
 }
@@ -1036,8 +1224,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)
@@ -1074,25 +1261,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);
+
+  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;
 }
 
@@ -1103,6 +1295,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);
@@ -1151,6 +1344,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);
@@ -1358,6 +1552,122 @@ vls_app_exit (void)
   vls_worker_free (wrk);
 }
 
+static void
+vls_clone_and_share_rpc_handler (void *args)
+{
+  vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
+  vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
+  vcl_locked_session_t *vls, *dst_vls;
+  vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
+  vcl_session_t *s, *dst_s;
+
+  vls = vls_session_get (wrk, msg->vls_index);
+
+  if (!vls_mt_wrk_supported ())
+    vls_init_share_session (wrk, vls);
+
+  s = vcl_session_get (vcl_wrk, msg->session_index);
+  dst_wrk = vls_worker_get (msg->origin_vls_wrk);
+  dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
+  dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
+  dst_vls->shared_data_index = vls->shared_data_index;
+  dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
+  clib_memcpy (dst_s, s, sizeof (*s));
+
+  dst_vcl_wrk->rpc_done = 1;
+
+  VDBG (1, "proces session clone of worker (session): %u (%u) -> %u (%u)",
+       vcl_wrk->wrk_index, msg->session_index, dst_vcl_wrk->wrk_index,
+       msg->origin_session_index);
+}
+
+static void
+vls_session_cleanup_rpc_handler (void *args)
+{
+  vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
+  vcl_worker_t *wrk = vcl_worker_get_current ();
+  vcl_worker_t *dst_wrk = vcl_worker_get (msg->origin_vcl_wrk);
+
+  vppcom_session_close (vcl_session_handle_from_index (msg->session_index));
+
+  dst_wrk->rpc_done = 1;
+
+  VDBG (1, "proces session cleanup of worker (session): %u (%u) from %u ()",
+       wrk->wrk_index, msg->session_index, dst_wrk->wrk_index);
+}
+
+static void
+vls_rpc_handler (void *args)
+{
+  vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
+  switch (msg->type)
+    {
+    case VLS_RPC_CLONE_AND_SHARE:
+      vls_clone_and_share_rpc_handler (msg->data);
+      break;
+    case VLS_RPC_SESS_CLEANUP:
+      vls_session_cleanup_rpc_handler (msg->data);
+      break;
+    default:
+      break;
+    }
+}
+
+void
+vls_send_clone_and_share_rpc (vcl_worker_t * wrk,
+                             vcl_locked_session_t * vls, u32 session_index,
+                             u32 vls_wrk_index, u32 dst_wrk_index,
+                             u32 dst_vls_index, u32 dst_session_index)
+{
+  u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
+  vls_clone_and_share_msg_t *msg;
+  vls_rpc_msg_t *rpc;
+  int ret;
+
+  rpc = (vls_rpc_msg_t *) & data;
+  rpc->type = VLS_RPC_CLONE_AND_SHARE;
+  msg = (vls_clone_and_share_msg_t *) & rpc->data;
+  msg->origin_vls_wrk = vls_wrk_index;
+  msg->origin_vls_index = vls->vls_index;
+  msg->origin_vcl_wrk = wrk->wrk_index;
+  msg->origin_session_index = session_index;
+  msg->vls_index = dst_vls_index;
+  msg->session_index = dst_session_index;
+
+  wrk->rpc_done = 0;
+  ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
+
+  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)
+    ;
+}
+
+void
+vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
+                             u32 dst_wrk_index, u32 dst_session_index)
+{
+  u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
+  vls_sess_cleanup_msg_t *msg;
+  vls_rpc_msg_t *rpc;
+  int ret;
+
+  rpc = (vls_rpc_msg_t *) & data;
+  rpc->type = VLS_RPC_SESS_CLEANUP;
+  msg = (vls_sess_cleanup_msg_t *) & rpc->data;
+  msg->origin_vcl_wrk = wrk->wrk_index;
+  msg->session_index = dst_session_index;
+
+  wrk->rpc_done = 0;
+  ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
+
+  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)
+    ;
+}
+
 int
 vls_app_create (char *app_name)
 {
@@ -1378,9 +1688,41 @@ vls_app_create (char *app_name)
   vls_worker_alloc ();
   vlsl->vls_wrk_index = vcl_get_worker_index ();
   vls_mt_locks_init ();
+  vcm->wrk_rpc_fn = vls_rpc_handler;
   return VPPCOM_OK;
 }
 
+unsigned char
+vls_use_eventfd (void)
+{
+  return vcm->cfg.use_mq_eventfd;
+}
+
+unsigned char
+vls_mt_wrk_supported (void)
+{
+  return vcm->cfg.mt_wrk_supported;
+}
+
+int
+vls_use_real_epoll (void)
+{
+  if (vcl_get_worker_index () == ~0)
+    return 0;
+
+  return vcl_worker_get_current ()->vcl_needs_real_epoll;
+}
+
+void
+vls_register_vcl_worker (void)
+{
+  if (vppcom_worker_register () != VPPCOM_OK)
+    {
+      VERR ("failed to register worker");
+      return;
+    }
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *