+ }
+ while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
+
+done:
+ return rv;
+}
+
+static inline int
+ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
+ int maxevents, int timeout, const sigset_t * sigmask)
+{
+ int libc_epfd, rv = 0, num_ev, libc_num_ev, vcl_wups = 0;
+ struct epoll_event *libc_evts;
+ ldp_worker_ctx_t *ldpw;
+ vls_handle_t ep_vlsh;
+
+ ldp_init_check ();
+
+ if (PREDICT_FALSE (!events || (timeout < -1)))
+ {
+ errno = EFAULT;
+ 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);
+
+ ep_vlsh = ldp_fd_to_vlsh (epfd);
+ if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
+ {
+ LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
+ errno = EBADFD;
+ return -1;
+ }
+
+ libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
+ if (PREDICT_FALSE (!libc_epfd))
+ {
+ u32 size = sizeof (epfd);
+
+ LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
+ "EPOLL_CLOEXEC", epfd, ep_vlsh);
+ libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
+ if (libc_epfd < 0)
+ {
+ rv = libc_epfd;
+ goto done;
+ }
+
+ rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
+ if (rv < 0)
+ {
+ errno = -rv;
+ rv = -1;
+ goto done;
+ }
+ }
+ if (PREDICT_FALSE (libc_epfd <= 0))
+ {
+ errno = -libc_epfd;
+ rv = -1;
+ goto done;
+ }
+
+ if (PREDICT_FALSE (!ldpw->mq_epfd_added))
+ {
+ struct epoll_event e = { 0 };
+ e.events = EPOLLIN;
+ e.data.fd = ldpw->vcl_mq_epfd;
+ if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
+ 0)
+ {
+ LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
+ epfd, ldpw->vcl_mq_epfd, libc_epfd);
+ rv = -1;
+ goto done;
+ }
+ ldpw->mq_epfd_added = 1;
+ }
+
+ /* Request to only drain unhandled to prevent libc_epoll_wait starved */
+ rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
+ if (rv > 0)
+ {
+ timeout = 0;
+ if (rv >= maxevents)
+ goto done;
+ }
+ else if (PREDICT_FALSE (rv < 0))
+ {
+ errno = -rv;
+ rv = -1;
+ goto done;
+ }
+
+epoll_again:
+
+ libc_evts = &events[rv];
+ libc_num_ev =
+ libc_epoll_pwait (libc_epfd, libc_evts, maxevents - rv, timeout, sigmask);
+ if (libc_num_ev <= 0)
+ {
+ rv = rv >= 0 ? rv : -1;
+ goto done;
+ }