vls: multi-process and multi-threaded apps improvements 71/17071/13
authorFlorin Coras <fcoras@cisco.com>
Fri, 18 Jan 2019 16:37:13 +0000 (08:37 -0800)
committerDamjan Marion <dmarion@me.com>
Tue, 29 Jan 2019 16:36:06 +0000 (16:36 +0000)
- More fine tuning for multi-process applications.
- Experimental support for multi-thread apps. This is meant for app
  whose threads are not vcl workers and the sessions are shared between
  them.

Change-Id: Ie07651da5f2cdcf39f5dead5431f50ad39cf3f74
Signed-off-by: Florin Coras <fcoras@cisco.com>
src/svm/svm_fifo.h
src/vcl/ldp.c
src/vcl/vcl_locked.c
src/vcl/vcl_locked.h
src/vcl/vcl_private.c
src/vcl/vcl_private.h
src/vcl/vppcom.c

index b0d38ed..07614b4 100644 (file)
@@ -88,7 +88,7 @@ typedef struct _svm_fifo
   u32 ooos_newest;             /**< Last segment to have been updated */
   struct _svm_fifo *next;      /**< next in freelist/active chain */
   struct _svm_fifo *prev;      /**< prev in active chain */
-  u8 n_subscribers;
+  volatile u8 n_subscribers;
   u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
 
 #if SVM_FIFO_TRACE
index 36cff2a..ab4cfbf 100644 (file)
@@ -739,10 +739,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
                              vec_len (ldpw->ex_bitmap) *
                              sizeof (clib_bitmap_t));
 
-         rv = vppcom_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
-                             writefds ? ldpw->wr_bitmap : NULL,
-                             exceptfds ? ldpw->ex_bitmap : NULL,
-                             vcl_timeout);
+         rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
+                          writefds ? ldpw->wr_bitmap : NULL,
+                          exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
          if (rv < 0)
            {
              errno = -rv;
@@ -2370,16 +2369,18 @@ ldp_constructor (void)
 void
 ldp_destructor (void)
 {
-  swrap_destructor ();
-  if (ldp->init)
-    ldp->init = 0;
+  /*
+     swrap_destructor ();
+     if (ldp->init)
+     ldp->init = 0;
+   */
 
   /* Don't use clib_warning() here because that calls writev()
    * which will call ldp_init().
    */
   if (LDP_DEBUG > 0)
-    printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
-           __func__, __LINE__, getpid ());
+    fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
+            __func__, __LINE__, getpid ());
 }
 
 
index c6c79cd..8a8d7d9 100644 (file)
@@ -60,6 +60,56 @@ 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 int vls_wrk_index = ~0;
+static volatile int vls_mt_n_threads;
+static pthread_mutex_t vls_mt_mq_mlock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t vls_mt_spool_mlock = PTHREAD_MUTEX_INITIALIZER;
+
+static void
+vls_mt_add (void)
+{
+  vls_mt_n_threads += 1;
+  vcl_set_worker_index (vls_wrk_index);
+}
+
+static inline void
+vls_mt_mq_lock (void)
+{
+  pthread_mutex_lock (&vls_mt_mq_mlock);
+}
+
+static inline void
+vls_mt_mq_unlock (void)
+{
+  pthread_mutex_unlock (&vls_mt_mq_mlock);
+}
+
+static inline void
+vls_mt_spool_lock (void)
+{
+  pthread_mutex_lock (&vls_mt_spool_mlock);
+}
+
+static inline void
+vls_mt_create_unlock (void)
+{
+  pthread_mutex_unlock (&vls_mt_spool_mlock);
+}
+
 static inline vcl_session_handle_t
 vls_to_sh (vcl_locked_session_t * vls)
 {
@@ -101,12 +151,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 *
@@ -154,27 +204,25 @@ vls_dunlock (vcl_locked_session_t * vls)
   vls_table_runlock ();
 }
 
-static void
-vls_get_and_free (vls_handle_t vlsh)
+u8
+vls_is_shared (vcl_locked_session_t * vls)
 {
-  vcl_locked_session_t *vls;
-
-  vls_table_wlock ();
-  vls = vls_get (vlsh);
-  vls_free (vls);
-  vls_table_wunlock ();
+  return vec_len (vls->workers_subscribed);
 }
 
 u8
-vls_is_shared (vcl_locked_session_t * vls)
+vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
 {
-  return vec_len (vls->workers_subscribed);
+  int i;
+  for (i = 0; i < vec_len (vls->workers_subscribed); i++)
+    if (vls->workers_subscribed[i] == wrk_index)
+      return 1;
+  return 0;
 }
 
 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 ();
   vcl_session_t *s;
   int i;
 
@@ -195,17 +243,26 @@ vls_unshare_session (vcl_locked_session_t * vls)
       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;
+
+  s = vcl_session_get (wrk, vls->session_index);
+
+  /* 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;
 }
@@ -245,6 +302,74 @@ vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
   /* *INDENT-ON* */
 }
 
+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:
+      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 (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 +378,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 +394,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 +410,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 +425,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 +441,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;
 }
@@ -347,7 +483,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,7 +498,9 @@ 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;
 }
@@ -374,7 +514,9 @@ vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
 
   if (!(vls = vls_get_w_dlock (listener_vlsh)))
     return VPPCOM_EBADFD;
+  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 +532,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 +549,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;
 }
 
@@ -482,12 +632,25 @@ 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;
 }
 
+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)
+{
+  int rv;
+  vls_mt_guard (0, VLS_MT_OP_XPOLL);
+  rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
+  vls_mt_unguard ();
+  return rv;
+}
+
 vcl_session_handle_t
 vlsh_to_sh (vls_handle_t vlsh)
 {
@@ -510,20 +673,57 @@ vlsh_to_session_index (vls_handle_t 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;
-  uword *vlshp;
 
   vls_table_rlock ();
-  vlshp = hash_get (vlsm->session_index_to_vlsh_table, session_index);
-  vlsh = vlshp ? *vlshp : VLS_INVALID_HANDLE;
+  vlsh = vls_si_to_vlsh (session_index);
   vls_table_runlock ();
 
   return vlsh;
 }
 
+static void
+vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
+{
+  u32 current_wrk, is_current;
+  vcl_locked_session_t *vls;
+  vcl_session_t *s;
+
+  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
 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
 {
@@ -542,8 +742,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 +854,10 @@ 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 */
+  vls_mt_n_threads = 0;
+  vls_wrk_index = vcl_get_worker_index ();
+
   VDBG (0, "forked child main worker initialized");
   vcm->forking = 0;
 }
@@ -666,15 +870,26 @@ 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;
+
   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);
+  vls_wrk_index = vcl_get_worker_index ();
   return VPPCOM_OK;
 }
 
index 4f40f43..2c8f211 100644 (file)
@@ -44,6 +44,8 @@ int vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
                   struct epoll_event *event);
 int vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
                    int maxevents, double wait_for_time);
+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 vlsh_to_sh (vls_handle_t vlsh);
 vcl_session_handle_t vlsh_to_session_index (vls_handle_t vlsh);
 vls_handle_t vls_session_index_to_vlsh (uint32_t session_index);
index df602b3..5f9ce27 100644 (file)
@@ -391,6 +391,70 @@ vcl_cleanup_bapi (void)
   vl_client_api_unmap ();
 }
 
+int
+vcl_session_read_ready (vcl_session_t * session)
+{
+  /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
+  if (PREDICT_FALSE (session->is_vep))
+    {
+      VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
+           session->session_index);
+      return VPPCOM_EBADFD;
+    }
+
+  if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
+    {
+      session_state_t state = session->session_state;
+      int rv;
+
+      rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
+
+      VDBG (1, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
+           session->session_index, session->vpp_handle, state,
+           vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
+      return rv;
+    }
+
+  if (session->session_state & STATE_LISTEN)
+    return clib_fifo_elts (session->accept_evts_fifo);
+
+  return svm_fifo_max_dequeue (session->rx_fifo);
+}
+
+int
+vcl_session_write_ready (vcl_session_t * session)
+{
+  /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
+  if (PREDICT_FALSE (session->is_vep))
+    {
+      VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
+           session->session_index, session->vpp_handle);
+      return VPPCOM_EBADFD;
+    }
+
+  if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
+    {
+      if (session->tx_fifo)
+       return svm_fifo_max_enqueue (session->tx_fifo);
+      else
+       return VPPCOM_EBADFD;
+    }
+
+  if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
+    {
+      session_state_t state = session->session_state;
+      int rv;
+
+      rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
+      VDBG (0, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
+           session->session_index, session->vpp_handle, state,
+           vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
+      return rv;
+    }
+
+  return svm_fifo_max_enqueue (session->tx_fifo);
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
index 08dfe42..1cde850 100644 (file)
@@ -543,6 +543,9 @@ void vcl_segment_table_add (u64 segment_handle, u32 svm_segment_index);
 u32 vcl_segment_table_lookup (u64 segment_handle);
 void vcl_segment_table_del (u64 segment_handle);
 
+int vcl_session_read_ready (vcl_session_t * session);
+int vcl_session_write_ready (vcl_session_t * session);
+
 static inline vcl_worker_t *
 vcl_worker_get (u32 wrk_index)
 {
index 317351d..fc7d194 100644 (file)
@@ -1060,7 +1060,7 @@ vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
 
       if (!do_disconnect)
        {
-         VDBG (0, "session %u [0x%llx] disconnect skipped",
+         VDBG (1, "session %u [0x%llx] disconnect skipped",
                session->session_index, vpp_handle);
          goto cleanup;
        }
@@ -1106,11 +1106,11 @@ vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
       vcl_ct_registration_unlock (wrk);
     }
 
+  VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
+
 cleanup:
   vcl_session_table_del_vpp_handle (wrk, vpp_handle);
   vcl_session_free (wrk, session);
-
-  VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
   vcl_evt (VCL_EVT_CLOSE, session, rv);
 
   return rv;
@@ -1694,37 +1694,6 @@ vppcom_session_free_segments (uint32_t session_handle,
   svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
 }
 
-static inline int
-vppcom_session_read_ready (vcl_session_t * session)
-{
-  /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
-  if (PREDICT_FALSE (session->is_vep))
-    {
-      clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
-                   "epoll session!", getpid (), session->session_index);
-      return VPPCOM_EBADFD;
-    }
-
-  if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
-    {
-      session_state_t state = session->session_state;
-      int rv;
-
-      rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
-
-      VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
-           " state 0x%x (%s), returning %d (%s)", getpid (),
-           session->vpp_handle, session->session_index, state,
-           vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
-      return rv;
-    }
-
-  if (session->session_state & STATE_LISTEN)
-    return clib_fifo_elts (session->accept_evts_fifo);
-
-  return svm_fifo_max_dequeue (session->rx_fifo);
-}
-
 int
 vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
 {
@@ -1878,41 +1847,6 @@ vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
   return 0;
 }
 
-static inline int
-vppcom_session_write_ready (vcl_session_t * session)
-{
-  /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
-  if (PREDICT_FALSE (session->is_vep))
-    {
-      VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
-           session->session_index, session->vpp_handle);
-      return VPPCOM_EBADFD;
-    }
-
-  if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
-    {
-      if (session->tx_fifo)
-       return svm_fifo_max_enqueue (session->tx_fifo);
-      else
-       return VPPCOM_EBADFD;
-    }
-
-  if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
-    {
-      session_state_t state = session->session_state;
-      int rv;
-
-      rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
-      VDBG (0, "session %u [0x%llx]: session is not open! state 0x%x (%s), "
-           "returning %d (%s)", session->session_index, session->vpp_handle,
-           state, vppcom_session_state_str (state), rv,
-           vppcom_retval_str (rv));
-      return rv;
-    }
-
-  return svm_fifo_max_enqueue (session->tx_fifo);
-}
-
 #define vcl_fifo_rx_evt_valid_or_break(_fifo)                  \
 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo)))                 \
   {                                                            \
@@ -2207,7 +2141,7 @@ check_rd:
         continue;
       }
 
-    rv = vppcom_session_read_ready (session);
+    rv = vcl_session_read_ready (session);
     if (rv)
       {
         clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
@@ -2840,12 +2774,12 @@ vppcom_session_attr (uint32_t session_handle, uint32_t op,
   switch (op)
     {
     case VPPCOM_ATTR_GET_NREAD:
-      rv = vppcom_session_read_ready (session);
+      rv = vcl_session_read_ready (session);
       VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
       break;
 
     case VPPCOM_ATTR_GET_NWRITE:
-      rv = vppcom_session_write_ready (session);
+      rv = vcl_session_write_ready (session);
       VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
            getpid (), session_handle, rv);
       break;
@@ -3484,7 +3418,7 @@ vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
 
          if (POLLIN & vp[i].events)
            {
-             rv = vppcom_session_read_ready (session);
+             rv = vcl_session_read_ready (session);
              if (rv > 0)
                {
                  vp[i].revents |= POLLIN;
@@ -3508,7 +3442,7 @@ vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
 
          if (POLLOUT & vp[i].events)
            {
-             rv = vppcom_session_write_ready (session);
+             rv = vcl_session_write_ready (session);
              if (rv > 0)
                {
                  vp[i].revents |= POLLOUT;