ldp/vcl: epoll and shutdown cleanup 56/16656/7
authorFlorin Coras <fcoras@cisco.com>
Mon, 31 Dec 2018 01:11:31 +0000 (17:11 -0800)
committerDave Barach <openvpp@barachs.net>
Wed, 2 Jan 2019 12:13:55 +0000 (12:13 +0000)
Change-Id: I882b4c495426551fad63372dc106496c5e8c141e
Signed-off-by: Florin Coras <fcoras@cisco.com>
src/vcl/ldp.c
src/vcl/vppcom.c

index 0d4fe43..5041556 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 fd;
   u32 fd_index;
+  u32 flags;
 } ldp_fd_entry_t;
 
 typedef struct ldp_worker_ctx_
@@ -1876,55 +1880,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_sid_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;
 }
 
@@ -2790,44 +2768,43 @@ 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_sid (fd))
     {
-      func_str = "vppcom_session_close[TODO]";
-      rv = close (fd);
+      u32 fd_index = fd - ldp->sid_bit_val;
+      ldp_fd_entry_t *fde;
+
+      fde = ldp_fd_entry_get_w_lock (fd_index);
+      if (!fde)
+       {
+         clib_rwlock_reader_unlock (&ldp->fd_table_lock);
+         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);
+
+      clib_rwlock_reader_unlock (&ldp->fd_table_lock);
+      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;
 }
 
@@ -3017,7 +2994,6 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
   double time_to_wait = (double) 0, time_out, now = 0;
   u32 vep_idx = ldp_sid_from_fd (epfd);
   int libc_epfd, rv = 0;
-  const char *func_str;
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -3033,8 +3009,8 @@ 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;
     }
@@ -3042,7 +3018,6 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
   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))
     {
@@ -3059,11 +3034,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)
@@ -3083,12 +3056,9 @@ 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, 0, sigmask);
          if (rv != 0)
@@ -3101,23 +3071,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;
 }
 
index a94df84..f1c58c4 100644 (file)
@@ -1571,8 +1571,8 @@ vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
                              SVM_Q_WAIT);
     }
 
-  VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
-       getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
+  VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
+       s->vpp_handle, session_handle, n_read, rx_fifo);
 
   return n_read;
 }
@@ -2330,9 +2330,8 @@ vppcom_epoll_create (void)
   vep_session->wait_cont_idx = ~0;
   vep_session->vpp_handle = ~0;
 
-  vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
-  VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
-       getpid (), vep_session->session_index, vep_session->session_index);
+  vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
+  VDBG (0, "Created vep_idx %u", vep_session->session_index);
 
   return vcl_session_handle (vep_session);
 }
@@ -2348,21 +2347,19 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
 
   if (vep_handle == session_handle)
     {
-      clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
-                   getpid (), vep_handle);
+      VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
       return VPPCOM_EINVAL;
     }
 
   vep_session = vcl_session_get_w_handle (wrk, vep_handle);
   if (PREDICT_FALSE (!vep_session))
     {
-      clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
+      VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
       return VPPCOM_EBADFD;
     }
   if (PREDICT_FALSE (!vep_session->is_vep))
     {
-      clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
-                   getpid (), vep_handle);
+      VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
       return VPPCOM_EINVAL;
     }
 
@@ -2372,13 +2369,12 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
   session = vcl_session_get_w_handle (wrk, session_handle);
   if (PREDICT_FALSE (!session))
     {
-      VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
-           getpid (), session_handle);
+      VDBG (0, "Invalid session_handle (%u)!", session_handle);
       return VPPCOM_EBADFD;
     }
   if (PREDICT_FALSE (session->is_vep))
     {
-      clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
+      VDBG (0, "session_handle (%u) is a vep!", vep_handle);
       return VPPCOM_EINVAL;
     }
 
@@ -2387,8 +2383,7 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
     case EPOLL_CTL_ADD:
       if (PREDICT_FALSE (!event))
        {
-         clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
-                       "epoll_event structure!", getpid ());
+         VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
          return VPPCOM_EINVAL;
        }
       if (vep_session->vep.next_sh != ~0)
@@ -2398,9 +2393,8 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
                                                   vep_session->vep.next_sh);
          if (PREDICT_FALSE (!next_session))
            {
-             clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
-                           "vep.next_sid (%u) on vep_idx (%u)!",
-                           getpid (), vep_session->vep.next_sh, vep_handle);
+             VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
+                   "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
              return VPPCOM_EBADFD;
            }
          ASSERT (next_session->vep.prev_sh == vep_handle);
@@ -2415,57 +2409,49 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
       session->is_vep_session = 1;
       vep_session->vep.next_sh = session_handle;
 
-      VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
-           "data 0x%llx!", getpid (), vep_handle, session_handle,
-           event->events, event->data.u64);
+      VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
+           vep_handle, session_handle, event->events, event->data.u64);
       vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
       break;
 
     case EPOLL_CTL_MOD:
       if (PREDICT_FALSE (!event))
        {
-         clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
-                       "epoll_event structure!", getpid ());
+         VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
          rv = VPPCOM_EINVAL;
          goto done;
        }
       else if (PREDICT_FALSE (!session->is_vep_session))
        {
-         clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
-                       "not a vep session!", getpid (), session_handle);
+         VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
+               session_handle);
          rv = VPPCOM_EINVAL;
          goto done;
        }
       else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
        {
-         clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
-                       "vep_idx (%u) != vep_idx (%u)!",
-                       getpid (), session_handle,
-                       session->vep.vep_sh, vep_handle);
+         VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
+               session_handle, session->vep.vep_sh, vep_handle);
          rv = VPPCOM_EINVAL;
          goto done;
        }
       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
       session->vep.ev = *event;
-      VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
-           " data 0x%llx!", getpid (), vep_handle, session_handle,
-           event->events, event->data.u64);
+      VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
+           vep_handle, session_handle, event->events, event->data.u64);
       break;
 
     case EPOLL_CTL_DEL:
       if (PREDICT_FALSE (!session->is_vep_session))
        {
-         clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
-                       "not a vep session!", getpid (), session_handle);
+         VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
          rv = VPPCOM_EINVAL;
          goto done;
        }
       else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
        {
-         clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
-                       "vep_idx (%u) != vep_idx (%u)!",
-                       getpid (), session_handle,
-                       session->vep.vep_sh, vep_handle);
+         VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
+               session_handle, session->vep.vep_sh, vep_handle);
          rv = VPPCOM_EINVAL;
          goto done;
        }
@@ -2482,9 +2468,8 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
          prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
          if (PREDICT_FALSE (!prev_session))
            {
-             clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
-                           "vep.prev_sid (%u) on sid (%u)!",
-                           getpid (), session->vep.prev_sh, session_handle);
+             VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
+                   session->vep.prev_sh, session_handle);
              return VPPCOM_EBADFD;
            }
          ASSERT (prev_session->vep.next_sh == session_handle);
@@ -2496,9 +2481,8 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
          next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
          if (PREDICT_FALSE (!next_session))
            {
-             clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
-                           "vep.next_sid (%u) on sid (%u)!",
-                           getpid (), session->vep.next_sh, session_handle);
+             VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
+                   session->vep.next_sh, session_handle);
              return VPPCOM_EBADFD;
            }
          ASSERT (next_session->vep.prev_sh == session_handle);
@@ -2510,13 +2494,13 @@ vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
       session->vep.prev_sh = ~0;
       session->vep.vep_sh = ~0;
       session->is_vep_session = 0;
-      VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
-           getpid (), vep_handle, session_handle);
+      VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
+           session_handle);
       vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
       break;
 
     default:
-      clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
+      VDBG (0, "Invalid operation (%d)!", op);
       rv = VPPCOM_EINVAL;
     }
 
@@ -3381,8 +3365,7 @@ vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
     rv = vppcom_session_peek (session_handle, buffer, buflen);
   else
     {
-      clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
-                   getpid (), flags);
+      VDBG (0, "Unsupport flags for recvfrom %d", flags);
       return VPPCOM_EAFNOSUPPORT;
     }