vcl: fix epoll wait with indefinite timeout 62/32162/4
authorFlorin Coras <fcoras@cisco.com>
Wed, 28 Apr 2021 20:01:06 +0000 (13:01 -0700)
committerFlorin Coras <fcoras@cisco.com>
Thu, 29 Apr 2021 01:10:10 +0000 (18:10 -0700)
Also avoid syscalls if timeout is 0.

Type: fix

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I9193d731c51a1432a52c89d5e54e6406e5100c96

src/plugins/hs_apps/vcl/vcl_test_protos.c
src/vcl/vppcom.c

index 45fff0f..3c44093 100644 (file)
@@ -51,7 +51,7 @@ vt_tcp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
 {
   int rv;
 
-  ts->fd = vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */);
+  ts->fd = vppcom_session_create (VPPCOM_PROTO_TCP, 1 /* is_nonblocking */);
   if (ts->fd < 0)
     {
       vterr ("vppcom_session_create()", ts->fd);
@@ -138,7 +138,7 @@ vt_udp_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
 {
   int rv;
 
-  ts->fd = vppcom_session_create (VPPCOM_PROTO_UDP, 0 /* is_nonblocking */);
+  ts->fd = vppcom_session_create (VPPCOM_PROTO_UDP, 1 /* is_nonblocking */);
   if (ts->fd < 0)
     {
       vterr ("vppcom_session_create()", ts->fd);
@@ -318,7 +318,7 @@ vt_tls_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
   uint32_t ckp_len;
   int rv;
 
-  ts->fd = vppcom_session_create (VPPCOM_PROTO_TLS, 0 /* is_nonblocking */);
+  ts->fd = vppcom_session_create (VPPCOM_PROTO_TLS, 1 /* is_nonblocking */);
   if (ts->fd < 0)
     {
       vterr ("vppcom_session_create()", ts->fd);
@@ -423,7 +423,7 @@ vt_dtls_listen (vcl_test_session_t *ts, vppcom_endpt_t *endpt)
   uint32_t ckp_len;
   int rv;
 
-  ts->fd = vppcom_session_create (VPPCOM_PROTO_DTLS, 0 /* is_nonblocking */);
+  ts->fd = vppcom_session_create (VPPCOM_PROTO_DTLS, 1 /* is_nonblocking */);
   if (ts->fd < 0)
     {
       vterr ("vppcom_session_create()", ts->fd);
index a0f4338..5e37dcf 100644 (file)
@@ -3001,56 +3001,50 @@ handle_dequeued:
 }
 
 static int
-vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
-                          int maxevents, u32 n_evts, double wait_for_time)
+vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
+                          int maxevents, u32 n_evts, double timeout_ms)
 {
-  double wait = 0, start = 0, now;
+  double end = -1;
 
   if (!n_evts)
     {
-      wait = wait_for_time;
-      start = clib_time_now (&wrk->clib_time);
+      if (timeout_ms > 0)
+       end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
     }
 
   do
     {
       vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
-                               wait, &n_evts);
-      if (n_evts)
+                               timeout_ms, &n_evts);
+      if (n_evts || !timeout_ms)
        return n_evts;
-      if (wait == -1)
-       continue;
-
-      now = clib_time_now (&wrk->clib_time);
-      wait -= (now - start) * 1e3;
-      start = now;
     }
-  while (wait > 0);
+  while (end == -1 || clib_time_now (&wrk->clib_time) < end);
 
   return 0;
 }
 
 static int
-vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
-                          int maxevents, u32 n_evts, double wait_for_time)
+vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
+                          int maxevents, u32 n_evts, double timeout_ms)
 {
-  double wait = 0, start = 0, now;
   int __clib_unused n_read;
   vcl_mq_evt_conn_t *mqc;
   int n_mq_evts, i;
+  double end = -1;
   u64 buf;
 
   vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
   if (!n_evts)
     {
-      wait = wait_for_time;
-      start = clib_time_now (&wrk->clib_time);
+      if (timeout_ms > 0)
+       end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
     }
 
   do
     {
       n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
-                             vec_len (wrk->mq_events), wait);
+                             vec_len (wrk->mq_events), timeout_ms);
       if (n_mq_evts < 0)
        {
          VDBG (0, "epoll_wait error %u", errno);
@@ -3065,16 +3059,10 @@ vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
                                    &n_evts);
        }
 
-      if (n_evts)
+      if (n_evts || !timeout_ms)
        return n_evts;
-      if (wait == -1)
-       continue;
-
-      now = clib_time_now (&wrk->clib_time);
-      wait -= (now - start) * 1e3;
-      start = now;
     }
-  while (wait > 0);
+  while (end == -1 || clib_time_now (&wrk->clib_time) < end);
 
   return 0;
 }