vcl/session: add api for changing session app worker
[vpp.git] / src / vcl / ldp.c
index d538770..a61acb9 100644 (file)
 
 #define LDP_MAX_NWORKERS 32
 
+#define LDP_F_SHUT_RD  (1 << 0)
+#define LDP_F_SHUT_WR  (1 << 1)
+
 typedef struct ldp_fd_entry_
 {
   u32 session_index;
+  u32 worker_index;
   u32 fd;
   u32 fd_index;
+  u32 flags;
+  clib_spinlock_t lock;
 } ldp_fd_entry_t;
 
 typedef struct ldp_worker_ctx_
@@ -97,7 +103,7 @@ typedef struct
   ldp_worker_ctx_t *workers;
   int init;
   char app_name[LDP_APP_NAME_MAX];
-  u32 sid_bit_val;
+  u32 sh_bit_val;
   u32 sid_bit_mask;
   u32 debug;
   ldp_fd_entry_t *fd_pool;
@@ -115,7 +121,7 @@ typedef struct
     clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args)
 
 static ldp_main_t ldp_main = {
-  .sid_bit_val = (1 << LDP_SID_BIT_MIN),
+  .sh_bit_val = (1 << LDP_SID_BIT_MIN),
   .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
   .debug = LDP_DEBUG_INIT,
 };
@@ -150,44 +156,73 @@ ldp_get_app_name ()
   return ldp->app_name;
 }
 
+static inline vcl_session_handle_t
+ldp_fd_entry_sh (ldp_fd_entry_t * fde)
+{
+  return vppcom_session_handle (fde->session_index);
+}
+
 static int
-ldp_fd_alloc (u32 sid)
+ldp_fd_alloc (vcl_session_handle_t sh)
 {
   ldp_fd_entry_t *fde;
 
   clib_rwlock_writer_lock (&ldp->fd_table_lock);
-  if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sid_bit_val)
+  if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sh_bit_val)
     {
       clib_rwlock_writer_unlock (&ldp->fd_table_lock);
       return -1;
     }
   pool_get (ldp->fd_pool, fde);
-  fde->session_index = vppcom_session_index (sid);
+  fde->session_index = vppcom_session_index (sh);
+  fde->worker_index = vppcom_session_worker (sh);
   fde->fd_index = fde - ldp->fd_pool;
-  fde->fd = fde->fd_index + ldp->sid_bit_val;
+  fde->fd = fde->fd_index + ldp->sh_bit_val;
   hash_set (ldp->session_index_to_fd_table, fde->session_index, fde->fd);
+  clib_spinlock_init (&fde->lock);
   clib_rwlock_writer_unlock (&ldp->fd_table_lock);
   return fde->fd;
 }
 
 static ldp_fd_entry_t *
-ldp_fd_entry_get_w_lock (u32 fd_index)
+ldp_fd_entry_get (u32 fd_index)
 {
-  clib_rwlock_reader_lock (&ldp->fd_table_lock);
   if (pool_is_free_index (ldp->fd_pool, fd_index))
     return 0;
-
   return pool_elt_at_index (ldp->fd_pool, fd_index);
 }
 
+static ldp_fd_entry_t *
+ldp_fd_entry_lock (u32 fd_index)
+{
+  ldp_fd_entry_t *fe;
+  clib_rwlock_reader_lock (&ldp->fd_table_lock);
+  if (pool_is_free_index (ldp->fd_pool, fd_index))
+    {
+      clib_rwlock_reader_unlock (&ldp->fd_table_lock);
+      return 0;
+    }
+
+  fe = pool_elt_at_index (ldp->fd_pool, fd_index);
+  clib_spinlock_lock (&fe->lock);
+  return fe;
+}
+
+static void
+ldp_fd_entry_unlock (ldp_fd_entry_t * fde)
+{
+  clib_spinlock_unlock (&fde->lock);
+  clib_rwlock_reader_unlock (&ldp->fd_table_lock);
+}
+
 static inline int
-ldp_fd_from_sid (u32 sid)
+ldp_fd_from_sh (vcl_session_handle_t sh)
 {
   uword *fdp;
   int fd;
 
   clib_rwlock_reader_lock (&ldp->fd_table_lock);
-  fdp = hash_get (ldp->session_index_to_fd_table, vppcom_session_index (sid));
+  fdp = hash_get (ldp->session_index_to_fd_table, vppcom_session_index (sh));
   fd = fdp ? *fdp : -EMFILE;
   clib_rwlock_reader_unlock (&ldp->fd_table_lock);
 
@@ -195,52 +230,63 @@ ldp_fd_from_sid (u32 sid)
 }
 
 static inline int
-ldp_fd_is_sid (int fd)
+ldp_fd_is_sh (int fd)
 {
-  return fd >= ldp->sid_bit_val;
+  return fd >= ldp->sh_bit_val;
 }
 
 static inline u32
-ldp_sid_from_fd (int fd)
+ldp_sh_from_fd (int fd)
 {
   u32 fd_index, session_index;
   ldp_fd_entry_t *fde;
 
-  if (!ldp_fd_is_sid (fd))
+  if (!ldp_fd_is_sh (fd))
     return INVALID_SESSION_ID;
 
-  fd_index = fd - ldp->sid_bit_val;
-  fde = ldp_fd_entry_get_w_lock (fd_index);
+  fd_index = fd - ldp->sh_bit_val;
+  fde = ldp_fd_entry_lock (fd_index);
   if (!fde)
     {
       LDBG (0, "unknown fd %d", fd);
-      clib_rwlock_reader_unlock (&ldp->fd_table_lock);
       return INVALID_SESSION_ID;
     }
   session_index = fde->session_index;
-  clib_rwlock_reader_unlock (&ldp->fd_table_lock);
+  ldp_fd_entry_unlock (fde);
 
   return vppcom_session_handle (session_index);
 }
 
+static ldp_fd_entry_t *
+ldp_fd_entry_lock_w_fd (int fd)
+{
+  u32 fd_index;
+
+  if (!ldp_fd_is_sh (fd))
+    return 0;
+
+  fd_index = fd - ldp->sh_bit_val;
+  return ldp_fd_entry_lock (fd_index);
+}
+
 static void
-ldp_fd_free_w_sid (u32 sid)
+ldp_fd_free_w_sh (vcl_session_handle_t sh)
 {
   ldp_fd_entry_t *fde;
   u32 fd_index;
   int fd;
 
-  fd = ldp_fd_from_sid (sid);
+  fd = ldp_fd_from_sh (sh);
   if (!fd)
     return;
 
-  fd_index = fd - ldp->sid_bit_val;
-  fde = ldp_fd_entry_get_w_lock (fd_index);
-  if (fde)
-    {
-      hash_unset (ldp->session_index_to_fd_table, fde->session_index);
-      pool_put (ldp->fd_pool, fde);
-    }
+  fd_index = fd - ldp->sh_bit_val;
+  clib_rwlock_writer_lock (&ldp->fd_table_lock);
+  fde = ldp_fd_entry_get (fd_index);
+  ASSERT (fde != 0);
+  hash_unset (ldp->session_index_to_fd_table, fde->session_index);
+  clib_spinlock_free (&fde->lock);
+  pool_put (ldp->fd_pool, fde);
   clib_rwlock_writer_unlock (&ldp->fd_table_lock);
 }
 
@@ -303,38 +349,38 @@ ldp_init (void)
          clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
                        " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
                        "value %d (0x%x)", getpid (), env_var_str,
-                       ldp->sid_bit_val, ldp->sid_bit_val);
+                       ldp->sh_bit_val, ldp->sh_bit_val);
        }
       else if (sb < LDP_SID_BIT_MIN)
        {
-         ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
-         ldp->sid_bit_mask = ldp->sid_bit_val - 1;
+         ldp->sh_bit_val = (1 << LDP_SID_BIT_MIN);
+         ldp->sid_bit_mask = ldp->sh_bit_val - 1;
 
          clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
                        " env var " LDP_ENV_SID_BIT " (%s) is too small. "
                        "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
                        getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
-                       ldp->sid_bit_val, ldp->sid_bit_val);
+                       ldp->sh_bit_val, ldp->sh_bit_val);
        }
       else if (sb > LDP_SID_BIT_MAX)
        {
-         ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
-         ldp->sid_bit_mask = ldp->sid_bit_val - 1;
+         ldp->sh_bit_val = (1 << LDP_SID_BIT_MAX);
+         ldp->sid_bit_mask = ldp->sh_bit_val - 1;
 
          clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
                        " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
                        " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
                        getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
-                       ldp->sid_bit_val, ldp->sid_bit_val);
+                       ldp->sh_bit_val, ldp->sh_bit_val);
        }
       else
        {
-         ldp->sid_bit_val = (1 << sb);
-         ldp->sid_bit_mask = ldp->sid_bit_val - 1;
+         ldp->sh_bit_val = (1 << sb);
+         ldp->sid_bit_mask = ldp->sh_bit_val - 1;
 
          LDBG (0, "configured LDP sid bit (%u) from "
                LDP_ENV_SID_BIT "!  sid bit value %d (0x%x)", sb,
-               ldp->sid_bit_val, ldp->sid_bit_val);
+               ldp->sh_bit_val, ldp->sh_bit_val);
        }
     }
 
@@ -348,17 +394,18 @@ ldp_init (void)
 int
 close (int fd)
 {
-  int rv, refcnt;
-  u32 sid = ldp_sid_from_fd (fd);
+  int rv, refcnt, epfd;
+  ldp_fd_entry_t *fde;
+  u32 sh;
 
   if ((errno = -ldp_init ()))
     return -1;
 
-  if (sid != INVALID_SESSION_ID)
+  fde = ldp_fd_entry_lock_w_fd (fd);
+  if (fde)
     {
-      int epfd;
-
-      epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
+      sh = ldp_fd_entry_sh (fde);
+      epfd = vppcom_session_attr (sh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
       if (epfd > 0)
        {
          LDBG (0, "fd %d (0x%x): calling libc_close: epfd %u (0x%x)",
@@ -370,7 +417,7 @@ close (int fd)
              u32 size = sizeof (epfd);
              epfd = 0;
 
-             (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
+             (void) vppcom_session_attr (sh, VPPCOM_ATTR_SET_LIBC_EPFD,
                                          &epfd, &size);
            }
        }
@@ -378,21 +425,24 @@ close (int fd)
        {
          errno = -epfd;
          rv = -1;
+         ldp_fd_entry_unlock (fde);
          goto done;
        }
 
       LDBG (0, "fd %d (0x%x): calling vppcom_session_close: sid %u (0x%x)",
-           fd, fd, sid, sid);
+           fd, fd, sh, sh);
 
-      refcnt = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REFCNT, 0, 0);
-      rv = vppcom_session_close (sid);
+      refcnt = vppcom_session_attr (sh, VPPCOM_ATTR_GET_REFCNT, 0, 0);
+      rv = vppcom_session_close (sh);
       if (rv != VPPCOM_OK)
        {
          errno = -rv;
          rv = -1;
        }
-      if (refcnt == 1)
-       ldp_fd_free_w_sid (sid);
+
+      ldp_fd_entry_unlock (fde);
+      if (refcnt <= 1)
+       ldp_fd_free_w_sh (sh);
     }
   else
     {
@@ -409,23 +459,27 @@ done:
 ssize_t
 read (int fd, void *buf, size_t nbytes)
 {
+  vcl_session_handle_t sh;
+  ldp_fd_entry_t *fde;
   ssize_t size;
-  u32 sid = ldp_sid_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
 
-  if (sid != INVALID_SESSION_ID)
+  fde = ldp_fd_entry_lock_w_fd (fd);
+  if (fde)
     {
+      sh = ldp_fd_entry_sh (fde);
       LDBG (2, "fd %d (0x%x): calling vppcom_session_read(): sid %u (0x%x),"
-           " buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
+           " buf %p, nbytes %u", fd, fd, sh, sh, buf, nbytes);
 
-      size = vppcom_session_read (sid, buf, nbytes);
+      size = vppcom_session_read (sh, buf, nbytes);
       if (size < 0)
        {
          errno = -size;
          size = -1;
        }
+      ldp_fd_entry_unlock (fde);
     }
   else
     {
@@ -443,7 +497,7 @@ ssize_t
 readv (int fd, const struct iovec * iov, int iovcnt)
 {
   ssize_t size = 0;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
   int rv = 0, i, total = 0;
 
   if ((errno = -ldp_init ()))
@@ -500,23 +554,27 @@ readv (int fd, const struct iovec * iov, int iovcnt)
 ssize_t
 write (int fd, const void *buf, size_t nbytes)
 {
+  vcl_session_handle_t sh;
+  ldp_fd_entry_t *fde;
   ssize_t size = 0;
-  u32 sid = ldp_sid_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
 
-  if (sid != INVALID_SESSION_ID)
+  fde = ldp_fd_entry_lock_w_fd (fd);
+  if (fde)
     {
+      sh = ldp_fd_entry_sh (fde);
       LDBG (2, "fd %d (0x%x): calling vppcom_session_write(): sid %u (0x%x), "
-           "buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
+           "buf %p, nbytes %u", fd, fd, sh, sh, buf, nbytes);
 
-      size = vppcom_session_write (sid, (void *) buf, nbytes);
+      size = vppcom_session_write_msg (sh, (void *) buf, nbytes);
       if (size < 0)
        {
          errno = -size;
          size = -1;
        }
+      ldp_fd_entry_unlock (fde);
     }
   else
     {
@@ -533,9 +591,8 @@ write (int fd, const void *buf, size_t nbytes)
 ssize_t
 writev (int fd, const struct iovec * iov, int iovcnt)
 {
-  const char *func_str;
   ssize_t size = 0, total = 0;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
   int i, rv = 0;
 
   /*
@@ -547,33 +604,19 @@ writev (int fd, const struct iovec * iov, int iovcnt)
 
   if (sid != INVALID_SESSION_ID)
     {
-      func_str = "vppcom_session_write";
       do
        {
          for (i = 0; i < iovcnt; ++i)
            {
-             if (LDP_DEBUG > 4)
-               printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
-                       "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
-                       __func__, __LINE__, getpid (), fd, fd, func_str,
-                       i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
-
-             rv = vppcom_session_write (sid, iov[i].iov_base,
-                                        iov[i].iov_len);
+             rv = vppcom_session_write_msg (sid, iov[i].iov_base,
+                                            iov[i].iov_len);
              if (rv < 0)
                break;
              else
                {
                  total += rv;
                  if (rv < iov[i].iov_len)
-                   {
-                     if (LDP_DEBUG > 4)
-                       printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
-                               "rv (%d) < iov[%d].iov_len (%ld)",
-                               __func__, __LINE__, getpid (), fd, fd,
-                               rv, i, iov[i].iov_len);
-                     break;
-                   }
+                   break;
                }
            }
        }
@@ -589,32 +632,9 @@ writev (int fd, const struct iovec * iov, int iovcnt)
     }
   else
     {
-      func_str = "libc_writev";
-
-      if (LDP_DEBUG > 4)
-       printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
-               "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
-               fd, fd, func_str, iov, iovcnt);
-
       size = libc_writev (fd, iov, iovcnt);
     }
 
-  if (LDP_DEBUG > 4)
-    {
-      if (size < 0)
-       {
-         int errno_val = errno;
-         perror (func_str);
-         fprintf (stderr,
-                  "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
-                  "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
-                  fd, func_str, size, errno_val);
-         errno = errno_val;
-       }
-      else
-       printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
-               __func__, __LINE__, getpid (), fd, fd, size);
-    }
   return size;
 }
 
@@ -624,7 +644,7 @@ fcntl (int fd, int cmd, ...)
   const char *func_str = __func__;
   int rv = 0;
   va_list ap;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -715,7 +735,7 @@ ioctl (int fd, unsigned long int cmd, ...)
   const char *func_str;
   int rv;
   va_list ap;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -842,7 +862,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
     time_out = -1;
 
 
-  if (nfds <= ldp->sid_bit_val)
+  if (nfds <= ldp->sh_bit_val)
     {
       func_str = "libc_pselect";
 
@@ -855,11 +875,11 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       goto done;
     }
 
-  if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
+  if (PREDICT_FALSE (ldp->sh_bit_val > FD_SETSIZE / 2))
     {
       clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
                    "FD_SETSIZE/2 %d (0x%x)!", getpid (),
-                   ldp->sid_bit_val, ldp->sid_bit_val,
+                   ldp->sh_bit_val, ldp->sh_bit_val,
                    FD_SETSIZE / 2, FD_SETSIZE / 2);
       errno = EOVERFLOW;
       return -1;
@@ -879,7 +899,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({
        if (fd > nfds)
          break;
-        sid = ldp_sid_from_fd (fd);
+        sid = ldp_sh_from_fd (fd);
         LDBG (3, "readfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
         if (sid == INVALID_SESSION_ID)
           clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1);
@@ -911,7 +931,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({
        if (fd > nfds)
          break;
-        sid = ldp_sid_from_fd (fd);
+        sid = ldp_sh_from_fd (fd);
         LDBG (3, "writefds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
         if (sid == INVALID_SESSION_ID)
           clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1);
@@ -943,7 +963,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({
        if (fd > nfds)
          break;
-        sid = ldp_sid_from_fd (fd);
+        sid = ldp_sh_from_fd (fd);
         LDBG (3, "exceptfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
         if (sid == INVALID_SESSION_ID)
           clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1);
@@ -993,9 +1013,12 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
                                  sizeof (clib_bitmap_t));
 
              rv = vppcom_select (sid_bits,
-                                 readfds ? ldpw->rd_bitmap : NULL,
-                                 writefds ? ldpw->wr_bitmap : NULL,
-                                 exceptfds ? ldpw->ex_bitmap : NULL, 0);
+                                 readfds ? (unsigned long *) ldpw->rd_bitmap
+                                 : NULL,
+                                 writefds ? (unsigned long *) ldpw->wr_bitmap
+                                 : NULL,
+                                 exceptfds ? (unsigned long *)
+                                 ldpw->ex_bitmap : NULL, 0);
              if (rv < 0)
                {
                  errno = -rv;
@@ -1008,7 +1031,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
                       /* *INDENT-OFF* */
                       clib_bitmap_foreach (sid, ldpw->rd_bitmap,
                         ({
-                          fd = ldp_fd_from_sid (vppcom_session_handle (sid));
+                          fd = ldp_fd_from_sh (vppcom_session_handle (sid));
                           if (PREDICT_FALSE (fd < 0))
                             {
                               errno = EBADFD;
@@ -1024,7 +1047,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
                       /* *INDENT-OFF* */
                       clib_bitmap_foreach (sid, ldpw->wr_bitmap,
                         ({
-                          fd = ldp_fd_from_sid (vppcom_session_handle (sid));
+                          fd = ldp_fd_from_sh (vppcom_session_handle (sid));
                           if (PREDICT_FALSE (fd < 0))
                             {
                               errno = EBADFD;
@@ -1040,7 +1063,7 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
                       /* *INDENT-OFF* */
                       clib_bitmap_foreach (sid, ldpw->ex_bitmap,
                         ({
-                          fd = ldp_fd_from_sid (vppcom_session_handle (sid));
+                          fd = ldp_fd_from_sh (vppcom_session_handle (sid));
                           if (PREDICT_FALSE (fd < 0))
                             {
                               errno = EBADFD;
@@ -1268,7 +1291,7 @@ int
 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
 {
   int rv;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1392,7 +1415,7 @@ getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
 {
   int rv;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1461,7 +1484,7 @@ int
 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
 {
   int rv;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1547,7 +1570,7 @@ getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
 {
   int rv;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1617,7 +1640,7 @@ send (int fd, const void *buf, size_t n, int flags)
 {
   ssize_t size;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1675,7 +1698,7 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
   ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
   ssize_t size = 0;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (out_fd);
+  u32 sid = ldp_sh_from_fd (out_fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1914,55 +1937,29 @@ ssize_t
 recv (int fd, void *buf, size_t n, int flags)
 {
   ssize_t size;
-  const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid;
 
   if ((errno = -ldp_init ()))
     return -1;
 
+  sid = ldp_sh_from_fd (fd);
   if (sid != INVALID_SESSION_ID)
     {
-      func_str = "vppcom_session_recvfrom";
-
-      if (LDP_DEBUG > 2)
-       clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
-                     "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
-                     fd, fd, func_str, sid, sid, buf, n, flags);
+      LDBG (2, "fd %d (0x%x): calling vcl recvfrom: sid %u (0x%x), buf %p,"
+           " n %u, flags 0x%x", fd, fd, sid, sid, buf, n, flags);
 
       size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
       if (size < 0)
-       {
-         errno = -size;
-         size = -1;
-       }
+       errno = -size;
     }
   else
     {
-      func_str = "libc_recv";
-
-      if (LDP_DEBUG > 2)
-       clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
-                     "buf %p, n %u, flags 0x%x", getpid (),
-                     fd, fd, func_str, buf, n, flags);
+      LDBG (2, "fd %d (0x%x): calling libc_recvfrom(): buf %p, n %u, "
+           "flags 0x%x", fd, fd, buf, n, flags);
 
       size = libc_recv (fd, buf, n, flags);
     }
 
-  if (LDP_DEBUG > 2)
-    {
-      if (size < 0)
-       {
-         int errno_val = errno;
-         perror (func_str);
-         clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
-                       "rv %d, errno = %d", getpid (), fd, fd,
-                       func_str, size, errno_val);
-         errno = errno_val;
-       }
-      else
-       clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
-                     getpid (), fd, fd, size, size);
-    }
   return size;
 }
 
@@ -1972,7 +1969,7 @@ sendto (int fd, const void *buf, size_t n, int flags,
 {
   ssize_t size;
   const char *func_str = __func__;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2063,7 +2060,7 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags,
 {
   ssize_t size;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2133,7 +2130,7 @@ sendmsg (int fd, const struct msghdr * message, int flags)
 {
   ssize_t size;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2182,7 +2179,7 @@ sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
 {
   ssize_t size;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2229,7 +2226,7 @@ recvmsg (int fd, struct msghdr * message, int flags)
 {
   ssize_t size;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2279,7 +2276,7 @@ recvmmsg (int fd, struct mmsghdr *vmessages,
 {
   ssize_t size;
   const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2328,7 +2325,7 @@ getsockopt (int fd, int level, int optname,
 {
   int rv;
   const char *func_str = __func__;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
   u32 buflen = optlen ? (u32) * optlen : 0;
 
   if ((errno = -ldp_init ()))
@@ -2556,7 +2553,7 @@ setsockopt (int fd, int level, int optname,
 {
   int rv;
   const char *func_str = __func__;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2720,7 +2717,7 @@ int
 listen (int fd, int n)
 {
   int rv;
-  u32 sid = ldp_sid_from_fd (fd);
+  u32 sid = ldp_sh_from_fd (fd);
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -2753,13 +2750,14 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
             socklen_t * __restrict addr_len, int flags)
 {
   int rv;
-  u32 listen_sid = ldp_sid_from_fd (listen_fd);
-  int accept_sid;
+  u32 listen_sh;
+  int accept_sh;
 
   if ((errno = -ldp_init ()))
     return -1;
 
-  if (listen_sid != INVALID_SESSION_ID)
+  listen_sh = ldp_sh_from_fd (listen_fd);
+  if (listen_sh != INVALID_SESSION_ID)
     {
       vppcom_endpt_t ep;
       u8 src_addr[sizeof (struct sockaddr_in6)];
@@ -2768,12 +2766,12 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
 
       LDBG (0, "listen fd %d (0x%x): calling vppcom_session_accept:"
            " listen sid %u (0x%x), ep %p, flags 0x%x", listen_fd,
-           listen_fd, listen_sid, listen_sid, ep, flags);
+           listen_fd, listen_sh, listen_sh, ep, flags);
 
-      accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
-      if (accept_sid < 0)
+      accept_sh = vppcom_session_accept (listen_sh, &ep, flags);
+      if (accept_sh < 0)
        {
-         errno = -accept_sid;
+         errno = -accept_sh;
          rv = -1;
        }
       else
@@ -2781,16 +2779,16 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
          rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
          if (rv != VPPCOM_OK)
            {
-             (void) vppcom_session_close ((u32) accept_sid);
+             (void) vppcom_session_close ((u32) accept_sh);
              errno = -rv;
              rv = -1;
            }
          else
            {
-             rv = ldp_fd_alloc ((u32) accept_sid);
+             rv = ldp_fd_alloc ((u32) accept_sh);
              if (rv < 0)
                {
-                 (void) vppcom_session_close ((u32) accept_sid);
+                 (void) vppcom_session_close ((u32) accept_sh);
                  errno = -rv;
                  rv = -1;
                }
@@ -2828,44 +2826,42 @@ accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
 int
 shutdown (int fd, int how)
 {
-  int rv;
-  const char *func_str;
-  u32 sid = ldp_sid_from_fd (fd);
+  int rv = 0;
 
   if ((errno = -ldp_init ()))
     return -1;
 
-  if (sid != INVALID_SESSION_ID)
+  if (ldp_fd_is_sh (fd))
     {
-      func_str = "vppcom_session_close[TODO]";
-      rv = close (fd);
+      u32 fd_index = fd - ldp->sh_bit_val;
+      ldp_fd_entry_t *fde;
+
+      fde = ldp_fd_entry_lock (fd_index);
+      if (!fde)
+       {
+         errno = ENOTCONN;
+         return -1;
+       }
+
+      if (how == SHUT_RD)
+       fde->flags |= LDP_F_SHUT_RD;
+      else if (how == SHUT_WR)
+       fde->flags |= LDP_F_SHUT_WR;
+      else if (how == SHUT_RDWR)
+       fde->flags |= (LDP_F_SHUT_RD | LDP_F_SHUT_WR);
+
+      if ((fde->flags & LDP_F_SHUT_RD) && (fde->flags & LDP_F_SHUT_WR))
+       rv = close (fd);
+
+      ldp_fd_entry_unlock (fde);
+      LDBG (0, "fd %d (0x%x): calling vcl shutdown: how %d", fd, fd, how);
     }
   else
     {
-      func_str = "libc_shutdown";
-
-      if (LDP_DEBUG > 1)
-       clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
-                     getpid (), fd, fd, func_str, how);
-
+      LDBG (1, "fd %d (0x%x): calling libc_shutdown: how %d", fd, fd, how);
       rv = libc_shutdown (fd, how);
     }
 
-  if (LDP_DEBUG > 1)
-    {
-      if (rv < 0)
-       {
-         int errno_val = errno;
-         perror (func_str);
-         clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
-                       "rv %d, errno = %d", getpid (), fd, fd,
-                       func_str, rv, errno_val);
-         errno = errno_val;
-       }
-      else
-       clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
-                     getpid (), fd, fd, rv, rv);
-    }
   return rv;
 }
 
@@ -2927,7 +2923,7 @@ epoll_create (int size)
 int
 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
 {
-  u32 vep_idx = ldp_sid_from_fd (epfd), sid;
+  u32 vep_idx = ldp_sh_from_fd (epfd), sid;
   const char *func_str;
   int rv;
 
@@ -2950,7 +2946,7 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
       goto done;
     }
 
-  sid = ldp_sid_from_fd (fd);
+  sid = ldp_sh_from_fd (fd);
 
   LDBG (0, "epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
        epfd, epfd, vep_idx, vep_idx, sid, sid);
@@ -3053,9 +3049,8 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
 {
   ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
   double time_to_wait = (double) 0, time_out, now = 0;
-  u32 vep_idx = ldp_sid_from_fd (epfd);
+  u32 vep_idx = ldp_sh_from_fd (epfd);
   int libc_epfd, rv = 0;
-  const char *func_str;
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -3071,16 +3066,15 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
 
   if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
     {
-      clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
-                   getpid (), epfd, epfd, vep_idx, vep_idx);
+      LDBG (0, "epfd %d (0x%x): bad vep_idx %d (0x%x)!", epfd, epfd, vep_idx,
+           vep_idx);
       errno = EBADFD;
       return -1;
     }
 
-  time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
+  time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
   time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
 
-  func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
   libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
   if (PREDICT_FALSE (libc_epfd < 0))
     {
@@ -3097,11 +3091,9 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
     {
       if (!ldpw->epoll_wait_vcl)
        {
-         func_str = "vppcom_epoll_wait";
-
-         LDBG (3, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
-               " events %p, maxevents %d", epfd, epfd, func_str,
-               vep_idx, vep_idx, events, maxevents);
+         LDBG (3, "epfd %d (0x%x): calling vcl_epoll_wait: vep_idx %d (0x%x)"
+               " events %p, maxevents %d", epfd, epfd, vep_idx, vep_idx,
+               events, maxevents);
 
          rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
          if (rv > 0)
@@ -3121,14 +3113,11 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
 
       if (libc_epfd > 0)
        {
-         func_str = "libc_epoll_pwait";
-
-         LDBG (3, "epfd %d (0x%x): calling %s(): libc_epfd %d "
-               "(0x%x), events %p, maxevents %d, sigmask %p",
-               epfd, epfd, func_str, libc_epfd, libc_epfd, events,
-               maxevents, sigmask);
+         LDBG (3, "epfd %d (0x%x): calling libc_epoll_wait: libc_epfd %d "
+               "(0x%x), events %p, maxevents %d, sigmask %p", epfd, epfd,
+               libc_epfd, libc_epfd, events, maxevents, sigmask);
 
-         rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
+         rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
          if (rv != 0)
            goto done;
        }
@@ -3139,23 +3128,6 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
   while (now < time_out);
 
 done:
-  if (LDP_DEBUG > 3)
-    {
-      if (libc_epfd > 0)
-       epfd = libc_epfd;
-      if (rv < 0)
-       {
-         int errno_val = errno;
-         perror (func_str);
-         clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
-                       "rv %d, errno = %d", getpid (), epfd, epfd,
-                       func_str, rv, errno_val);
-         errno = errno_val;
-       }
-      else
-       clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
-                     getpid (), epfd, epfd, rv, rv);
-    }
   return rv;
 }
 
@@ -3197,7 +3169,7 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout)
       LDBG (3, "fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
            i, fds[i].fd, fds[i].fd, fds[i].events, fds[i].revents);
 
-      sid = ldp_sid_from_fd (fds[i].fd);
+      sid = ldp_sh_from_fd (fds[i].fd);
       if (sid != INVALID_SESSION_ID)
        {
          fds[i].fd = -fds[i].fd;