misc: refactor clib_bitmap_foreach macro
[vpp.git] / src / vcl / ldp.c
index 19f3827..98ed4a9 100644 (file)
@@ -112,7 +112,7 @@ typedef struct
   if (ldp->debug > _lvl)                                               \
     {                                                                  \
       int errno_saved = errno;                                         \
-      clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args);              \
+      fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args);      \
       errno = errno_saved;                                             \
     }
 
@@ -279,9 +279,9 @@ ldp_init (void)
     }
 
   /* *INDENT-OFF* */
-  pool_foreach (ldpw, ldp->workers, ({
+  pool_foreach (ldpw, ldp->workers)  {
     clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
-  }));
+  }
   /* *INDENT-ON* */
 
   LDBG (0, "LDP initialization: done!");
@@ -478,17 +478,11 @@ writev (int fd, const struct iovec * iov, int iovcnt)
   return size;
 }
 
-int
-fcntl (int fd, int cmd, ...)
+static int
+fcntl_internal (int fd, int cmd, va_list ap)
 {
   vls_handle_t vlsh;
   int rv = 0;
-  va_list ap;
-
-  if ((errno = -ldp_init ()))
-    return -1;
-
-  va_start (ap, cmd);
 
   vlsh = ldp_fd_to_vlsh (fd);
   LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
@@ -534,6 +528,20 @@ fcntl (int fd, int cmd, ...)
 #endif
     }
 
+  return rv;
+}
+
+int
+fcntl (int fd, int cmd, ...)
+{
+  va_list ap;
+  int rv;
+
+  if ((errno = -ldp_init ()))
+    return -1;
+
+  va_start (ap, cmd);
+  rv = fcntl_internal (fd, cmd, ap);
   va_end (ap);
 
   return rv;
@@ -545,8 +553,11 @@ fcntl64 (int fd, int cmd, ...)
   va_list ap;
   int rv;
 
+  if ((errno = -ldp_init ()))
+    return -1;
+
   va_start (ap, cmd);
-  rv = fcntl (fd, cmd, ap);
+  rv = fcntl_internal (fd, cmd, ap);
   va_end (ap);
   return rv;
 }
@@ -621,7 +632,7 @@ ldp_select_init_maps (fd_set * __restrict original,
   memset (original, 0, n_bytes);
 
   /* *INDENT-OFF* */
-  clib_bitmap_foreach (fd, *resultb, ({
+  clib_bitmap_foreach (fd, *resultb)  {
     if (fd > nfds)
       break;
     vlsh = ldp_fd_to_vlsh (fd);
@@ -629,7 +640,7 @@ ldp_select_init_maps (fd_set * __restrict original,
       clib_bitmap_set_no_check (*libcb, fd, 1);
     else
       *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
-  }));
+  }
   /* *INDENT-ON* */
 
   si_bits_set = clib_bitmap_last_set (*vclb) + 1;
@@ -651,7 +662,7 @@ ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
     return 0;
 
   /* *INDENT-OFF* */
-  clib_bitmap_foreach (si, vclb, ({
+  clib_bitmap_foreach (si, vclb)  {
     vlsh = vls_session_index_to_vlsh (si);
     ASSERT (vlsh != VLS_INVALID_HANDLE);
     fd = ldp_vlsh_to_fd (vlsh);
@@ -661,7 +672,7 @@ ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
         return -1;
       }
     FD_SET (fd, libcb);
-  }));
+  }
   /* *INDENT-ON* */
 
   return 0;
@@ -676,9 +687,8 @@ ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
     return;
 
   /* *INDENT-OFF* */
-  clib_bitmap_foreach (fd, result, ({
+  clib_bitmap_foreach (fd, result)
     FD_SET ((int)fd, libcb);
-  }));
   /* *INDENT-ON* */
 }
 
@@ -1397,7 +1407,7 @@ sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
          size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
          if (size < 0)
            {
-             LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
+             LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
                    out_fd, vlsh, size, vppcom_retval_str (size));
              vec_reset_length (ldpw->io_buffer);
              errno = -size;
@@ -2114,7 +2124,7 @@ ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
       ep.ip = src_addr;
 
       LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
-           " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
+           " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
 
       accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
       if (accept_vlsh < 0)
@@ -2212,7 +2222,7 @@ epoll_create1 (int flags)
   if ((errno = -ldp_init ()))
     return -1;
 
-  if (ldp->vcl_needs_real_epoll)
+  if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
     {
       /* Make sure workers have been allocated */
       if (!ldp->workers)
@@ -2279,7 +2289,7 @@ epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
   if (vlsh != VLS_INVALID_HANDLE)
     {
       LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
-           " event %p", epfd, vep_vlsh, vlsh, event);
+           " event %p", epfd, vep_vlsh, op, vlsh, event);
 
       rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
       if (rv != VPPCOM_OK)
@@ -2414,7 +2424,7 @@ static inline int
 ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
                         int maxevents, int timeout, const sigset_t * sigmask)
 {
-  ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
+  ldp_worker_ctx_t *ldpw;
   int libc_epfd, rv = 0, num_ev;
   vls_handle_t ep_vlsh;
 
@@ -2427,6 +2437,12 @@ ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
       return -1;
     }
 
+  /* Make sure the vcl worker is valid. Could be that epoll fd was created on
+   * one thread but it is now used on another */
+  if (PREDICT_FALSE (vppcom_worker_index () == ~0))
+    vls_register_vcl_worker ();
+
+  ldpw = ldp_worker_get_current ();
   if (epfd == ldpw->vcl_mq_epfd)
     return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
 
@@ -2486,7 +2502,7 @@ ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
   rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
   if (rv > 0)
     goto done;
-  else if (rv < 0)
+  else if (PREDICT_FALSE (rv < 0))
     {
       errno = -rv;
       rv = -1;
@@ -2547,7 +2563,7 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout)
   vcl_poll_t *vp;
   double max_time;
 
-  LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
+  LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
 
   if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
     clib_time_init (&ldpw->clib_time);