ldp: ignore TCP_CORK socket option
[vpp.git] / src / vcl / ldp.c
index 088c118..6dc44ec 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Copyright (c) 2016-2019 Cisco and/or its affiliates.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
@@ -161,6 +161,14 @@ ldp_fd_to_vlsh (int fd)
   return (fd - ldp->vlsh_bit_val);
 }
 
+static void
+ldp_alloc_workers (void)
+{
+  if (ldp->workers)
+    return;
+  pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
+}
+
 static inline int
 ldp_init (void)
 {
@@ -184,7 +192,7 @@ ldp_init (void)
       return rv;
     }
   ldp->vcl_needs_real_epoll = 0;
-  pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
+  ldp_alloc_workers ();
   ldpw = ldp_worker_get_current ();
 
   char *env_var_str = getenv (LDP_ENV_DEBUG);
@@ -261,7 +269,12 @@ ldp_init (void)
        }
     }
 
-  clib_time_init (&ldpw->clib_time);
+  /* *INDENT-OFF* */
+  pool_foreach (ldpw, ldp->workers, ({
+    clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
+  }));
+  /* *INDENT-ON* */
+
   LDBG (0, "LDP initialization: done!");
 
   return 0;
@@ -641,6 +654,9 @@ ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
 {
   uword fd;
 
+  if (!libcb)
+    return;
+
   /* *INDENT-OFF* */
   clib_bitmap_foreach (fd, result, ({
     FD_SET ((int)fd, libcb);
@@ -668,6 +684,9 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       return -1;
     }
 
+  if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
+    clib_time_init (&ldpw->clib_time);
+
   if (timeout)
     {
       time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
@@ -720,7 +739,8 @@ ldp_pselect (int nfds, fd_set * __restrict readfds,
       goto done;
     }
 
-  libc_tspec = si_bits ? libc_tspec : *timeout;
+  if (!si_bits)
+    libc_tspec = timeout ? *timeout : libc_tspec;
 
   do
     {
@@ -739,10 +759,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;
@@ -1471,7 +1490,7 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags,
          __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
 {
   vls_handle_t sid;
-  ssize_t size;
+  ssize_t size, rv;
 
   if ((errno = -ldp_init ()))
     return -1;
@@ -1488,7 +1507,11 @@ recvfrom (int fd, void *__restrict buf, size_t n, int flags,
          size = vls_recvfrom (sid, buf, n, flags, &ep);
 
          if (size > 0)
-           size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
+           {
+             rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
+             if (rv < 0)
+               size = rv;
+           }
        }
       else
        size = vls_recvfrom (sid, buf, n, flags, NULL);
@@ -1814,6 +1837,7 @@ setsockopt (int fd, int level, int optname,
                             (void *) optval, &optlen);
              break;
            case TCP_CONGESTION:
+           case TCP_CORK:
              /* Ignore */
              rv = 0;
              break;
@@ -2025,6 +2049,12 @@ epoll_create1 (int flags)
 
   if (ldp->vcl_needs_real_epoll)
     {
+      /* Make sure workers have been allocated */
+      if (!ldp->workers)
+       {
+         ldp_alloc_workers ();
+         ldpw = ldp_worker_get_current ();
+       }
       rv = libc_epoll_create1 (flags);
       ldp->vcl_needs_real_epoll = 0;
       ldpw->vcl_mq_epfd = rv;
@@ -2142,7 +2172,7 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
                 int timeout, const sigset_t * sigmask)
 {
   ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
-  double time_to_wait = (double) 0, time_out, now = 0;
+  double time_to_wait = (double) 0, max_time;
   int libc_epfd, rv = 0;
   vls_handle_t ep_vlsh;
 
@@ -2166,8 +2196,10 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
       return -1;
     }
 
+  if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
+    clib_time_init (&ldpw->clib_time);
   time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
-  time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
+  max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
 
   libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
   if (PREDICT_FALSE (libc_epfd < 0))
@@ -2179,8 +2211,7 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
 
   LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
        "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
-       libc_epfd, events, maxevents, timeout, sigmask, time_to_wait,
-       time_out);
+       libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
   do
     {
       if (!ldpw->epoll_wait_vcl)
@@ -2207,11 +2238,8 @@ ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
          if (rv != 0)
            goto done;
        }
-
-      if (timeout != -1)
-       now = clib_time_now (&ldpw->clib_time);
     }
-  while (now < time_out);
+  while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
 
 done:
   return rv;
@@ -2237,14 +2265,15 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout)
   int rv, i, n_revents = 0;
   vls_handle_t vlsh;
   vcl_poll_t *vp;
-  double wait_for_time;
+  double max_time;
 
   LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
 
-  if (timeout >= 0)
-    wait_for_time = (f64) timeout / 1000;
-  else
-    wait_for_time = -1;
+  if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
+    clib_time_init (&ldpw->clib_time);
+
+  max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
+  max_time += clib_time_now (&ldpw->clib_time);
 
   for (i = 0; i < nfds; i++)
     {
@@ -2304,8 +2333,7 @@ poll (struct pollfd *fds, nfds_t nfds, int timeout)
          goto done;
        }
     }
-  while ((wait_for_time == -1) ||
-        (clib_time_now (&ldpw->clib_time) < wait_for_time));
+  while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
   rv = 0;
 
 done:
@@ -2362,8 +2390,11 @@ ldp_constructor (void)
 {
   swrap_constructor ();
   if (ldp_init () != 0)
-    fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
-            getpid ());
+    {
+      fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
+              getpid ());
+      _exit (1);
+    }
   else if (LDP_DEBUG > 0)
     clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
 }
@@ -2374,16 +2405,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 ());
 }