2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <sys/resource.h>
23 #include <netinet/tcp.h>
25 #include <vcl/ldp_socket_wrapper.h>
29 #include <vcl/vcl_locked.h>
30 #include <vppinfra/time.h>
31 #include <vppinfra/bitmap.h>
32 #include <vppinfra/lock.h>
33 #include <vppinfra/pool.h>
34 #include <vppinfra/hash.h>
36 #define HAVE_CONSTRUCTOR_ATTRIBUTE
37 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
38 #define CONSTRUCTOR_ATTRIBUTE \
39 __attribute__ ((constructor))
41 #define CONSTRUCTOR_ATTRIBUTE
42 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
44 #define HAVE_DESTRUCTOR_ATTRIBUTE
45 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
46 #define DESTRUCTOR_ATTRIBUTE \
47 __attribute__ ((destructor))
49 #define DESTRUCTOR_ATTRIBUTE
52 #define LDP_MAX_NWORKERS 32
54 typedef struct ldp_worker_ctx_
57 clib_time_t clib_time;
62 clib_bitmap_t *rd_bitmap;
63 clib_bitmap_t *wr_bitmap;
64 clib_bitmap_t *ex_bitmap;
65 clib_bitmap_t *sid_rd_bitmap;
66 clib_bitmap_t *sid_wr_bitmap;
67 clib_bitmap_t *sid_ex_bitmap;
68 clib_bitmap_t *libc_rd_bitmap;
69 clib_bitmap_t *libc_wr_bitmap;
70 clib_bitmap_t *libc_ex_bitmap;
77 struct pollfd *libc_poll;
90 ldp_worker_ctx_t *workers;
92 char app_name[LDP_APP_NAME_MAX];
97 /** vcl needs next epoll_create to go to libc_epoll */
98 u8 vcl_needs_real_epoll;
101 #define LDP_DEBUG ldp->debug
103 #define LDBG(_lvl, _fmt, _args...) \
104 if (ldp->debug > _lvl) \
105 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args)
107 static ldp_main_t ldp_main = {
108 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
109 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
110 .debug = LDP_DEBUG_INIT,
113 static ldp_main_t *ldp = &ldp_main;
115 static inline ldp_worker_ctx_t *
116 ldp_worker_get_current (void)
118 return (ldp->workers + vppcom_worker_index ());
122 * RETURN: 0 on success or -1 on error.
125 ldp_set_app_name (char *app_name)
127 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
128 "ldp-%d-%s", getpid (), app_name);
130 if (rv >= LDP_APP_NAME_MAX)
131 app_name[LDP_APP_NAME_MAX - 1] = 0;
137 if (ldp->app_name[0] == '\0')
138 ldp_set_app_name ("app");
140 return ldp->app_name;
144 ldp_vlsh_to_fd (vls_handle_t vlsh)
146 return (vlsh + ldp->vlsh_bit_val);
149 static inline vls_handle_t
150 ldp_fd_to_vlsh (int fd)
152 if (fd < ldp->vlsh_bit_val)
153 return VLS_INVALID_HANDLE;
155 return (fd - ldp->vlsh_bit_val);
161 ldp_worker_ctx_t *ldpw;
164 if (PREDICT_TRUE (ldp->init))
168 ldp->vcl_needs_real_epoll = 1;
169 rv = vls_app_create (ldp_get_app_name ());
172 ldp->vcl_needs_real_epoll = 0;
173 if (rv == VPPCOM_EEXIST)
175 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
176 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
180 ldp->vcl_needs_real_epoll = 0;
181 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
182 ldpw = ldp_worker_get_current ();
184 char *env_var_str = getenv (LDP_ENV_DEBUG);
188 if (sscanf (env_var_str, "%u", &tmp) != 1)
189 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
190 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
195 LDBG (0, "configured LDP debug level (%u) from env var "
196 LDP_ENV_DEBUG "!", ldp->debug);
200 env_var_str = getenv (LDP_ENV_APP_NAME);
203 ldp_set_app_name (env_var_str);
204 LDBG (0, "configured LDP app name (%s) from the env var "
205 LDP_ENV_APP_NAME "!", ldp->app_name);
208 env_var_str = getenv (LDP_ENV_SID_BIT);
212 if (sscanf (env_var_str, "%u", &sb) != 1)
214 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
215 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
216 "value %d (0x%x)", getpid (), env_var_str,
217 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
219 else if (sb < LDP_SID_BIT_MIN)
221 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
222 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
224 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
225 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
226 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
227 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
228 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
230 else if (sb > LDP_SID_BIT_MAX)
232 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
233 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
235 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
236 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
237 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
238 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
239 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
243 ldp->vlsh_bit_val = (1 << sb);
244 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
246 LDBG (0, "configured LDP sid bit (%u) from "
247 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
248 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
252 clib_time_init (&ldpw->clib_time);
253 LDBG (0, "LDP initialization: done!");
264 if ((errno = -ldp_init ()))
267 vlsh = ldp_fd_to_vlsh (fd);
268 if (vlsh != VLS_INVALID_HANDLE)
270 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
273 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
275 rv = libc_close (epfd);
278 u32 size = sizeof (epfd);
281 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
284 else if (PREDICT_FALSE (epfd < 0))
291 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
293 rv = vls_close (vlsh);
302 LDBG (0, "fd %d: calling libc_close", fd);
303 rv = libc_close (fd);
311 read (int fd, void *buf, size_t nbytes)
316 if ((errno = -ldp_init ()))
319 vlsh = ldp_fd_to_vlsh (fd);
320 if (vlsh != VLS_INVALID_HANDLE)
322 size = vls_read (vlsh, buf, nbytes);
331 size = libc_read (fd, buf, nbytes);
338 readv (int fd, const struct iovec * iov, int iovcnt)
340 int rv = 0, i, total = 0;
344 if ((errno = -ldp_init ()))
347 vlsh = ldp_fd_to_vlsh (fd);
348 if (vlsh != VLS_INVALID_HANDLE)
352 for (i = 0; i < iovcnt; ++i)
354 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
360 if (rv < iov[i].iov_len)
365 while ((rv >= 0) && (total == 0));
377 size = libc_readv (fd, iov, iovcnt);
384 write (int fd, const void *buf, size_t nbytes)
389 if ((errno = -ldp_init ()))
392 vlsh = ldp_fd_to_vlsh (fd);
393 if (vlsh != VLS_INVALID_HANDLE)
395 size = vls_write_msg (vlsh, (void *) buf, nbytes);
404 size = libc_write (fd, buf, nbytes);
411 writev (int fd, const struct iovec * iov, int iovcnt)
413 ssize_t size = 0, total = 0;
417 if ((errno = -ldp_init ()))
420 vlsh = ldp_fd_to_vlsh (fd);
421 if (vlsh != VLS_INVALID_HANDLE)
425 for (i = 0; i < iovcnt; ++i)
427 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
433 if (rv < iov[i].iov_len)
438 while ((rv >= 0) && (total == 0));
450 size = libc_writev (fd, iov, iovcnt);
457 fcntl (int fd, int cmd, ...)
463 if ((errno = -ldp_init ()))
468 vlsh = ldp_fd_to_vlsh (fd);
469 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
470 if (vlsh != VLS_INVALID_HANDLE)
472 int flags = va_arg (ap, int);
475 size = sizeof (flags);
480 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
484 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
489 /* TODO handle this */
490 LDBG (0, "F_SETFD ignored flags %u", flags);
505 rv = libc_vfcntl (fd, cmd, ap);
514 ioctl (int fd, unsigned long int cmd, ...)
520 if ((errno = -ldp_init ()))
525 vlsh = ldp_fd_to_vlsh (fd);
526 if (vlsh != VLS_INVALID_HANDLE)
531 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
536 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
537 u32 size = sizeof (flags);
539 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
540 * non-blocking, the flags should be read here and merged
543 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
559 rv = libc_vioctl (fd, cmd, ap);
567 ldp_pselect (int nfds, fd_set * __restrict readfds,
568 fd_set * __restrict writefds,
569 fd_set * __restrict exceptfds,
570 const struct timespec *__restrict timeout,
571 const __sigset_t * __restrict sigmask)
573 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
574 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
575 u32 minbits = clib_max (nfds, BITS (uword)), si;
588 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
589 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
591 /* select as fine grained sleep */
594 time_out += clib_time_now (&ldpw->clib_time);
595 while (clib_time_now (&ldpw->clib_time) < time_out)
609 if (nfds <= ldp->vlsh_bit_val)
611 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
616 if (PREDICT_FALSE (ldp->vlsh_bit_val > FD_SETSIZE / 2))
618 LDBG (0, "ERROR: LDP sid bit value %d > FD_SETSIZE/2 %d!",
619 ldp->vlsh_bit_val, FD_SETSIZE / 2);
624 sid_bits = libc_bits = 0;
625 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
629 clib_bitmap_validate (ldpw->sid_rd_bitmap, minbits);
630 clib_bitmap_validate (ldpw->libc_rd_bitmap, minbits);
631 clib_bitmap_validate (ldpw->rd_bitmap, minbits);
632 clib_memcpy_fast (ldpw->rd_bitmap, readfds, n_bytes);
633 memset (readfds, 0, n_bytes);
636 clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({
639 vlsh = ldp_fd_to_vlsh (fd);
640 if (vlsh == VLS_INVALID_HANDLE)
641 clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1);
643 clib_bitmap_set_no_check (ldpw->sid_rd_bitmap,
644 vlsh_to_session_index (vlsh), 1);
648 sid_bits_set = clib_bitmap_last_set (ldpw->sid_rd_bitmap) + 1;
649 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
651 libc_bits_set = clib_bitmap_last_set (ldpw->libc_rd_bitmap) + 1;
652 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
656 clib_bitmap_validate (ldpw->sid_wr_bitmap, minbits);
657 clib_bitmap_validate (ldpw->libc_wr_bitmap, minbits);
658 clib_bitmap_validate (ldpw->wr_bitmap, minbits);
659 clib_memcpy_fast (ldpw->wr_bitmap, writefds, n_bytes);
660 memset (writefds, 0, n_bytes);
663 clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({
666 vlsh = ldp_fd_to_vlsh (fd);
667 if (vlsh == VLS_INVALID_HANDLE)
668 clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1);
670 clib_bitmap_set_no_check (ldpw->sid_wr_bitmap,
671 vlsh_to_session_index (vlsh), 1);
675 sid_bits_set = clib_bitmap_last_set (ldpw->sid_wr_bitmap) + 1;
676 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
678 libc_bits_set = clib_bitmap_last_set (ldpw->libc_wr_bitmap) + 1;
679 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
684 clib_bitmap_validate (ldpw->sid_ex_bitmap, minbits);
685 clib_bitmap_validate (ldpw->libc_ex_bitmap, minbits);
686 clib_bitmap_validate (ldpw->ex_bitmap, minbits);
687 clib_memcpy_fast (ldpw->ex_bitmap, exceptfds, n_bytes);
688 memset (exceptfds, 0, n_bytes);
691 clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({
694 vlsh = ldp_fd_to_vlsh (fd);
695 if (vlsh == VLS_INVALID_HANDLE)
696 clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1);
698 clib_bitmap_set_no_check (ldpw->sid_ex_bitmap,
699 vlsh_to_session_index (vlsh), 1);
703 sid_bits_set = clib_bitmap_last_set (ldpw->sid_ex_bitmap) + 1;
704 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
706 libc_bits_set = clib_bitmap_last_set (ldpw->libc_ex_bitmap) + 1;
707 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
710 if (PREDICT_FALSE (!sid_bits && !libc_bits))
721 if (!ldpw->select_vcl)
724 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap,
725 vec_len (ldpw->rd_bitmap) *
726 sizeof (clib_bitmap_t));
728 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap,
729 vec_len (ldpw->wr_bitmap) *
730 sizeof (clib_bitmap_t));
732 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->sid_ex_bitmap,
733 vec_len (ldpw->ex_bitmap) *
734 sizeof (clib_bitmap_t));
736 rv = vppcom_select (sid_bits,
737 readfds ? (unsigned long *) ldpw->rd_bitmap
739 writefds ? (unsigned long *) ldpw->wr_bitmap
741 exceptfds ? (unsigned long *)
742 ldpw->ex_bitmap : NULL, 0);
753 clib_bitmap_foreach (si, ldpw->rd_bitmap, ({
754 vlsh = vls_session_index_to_vlsh (si);
755 fd = ldp_vlsh_to_fd (vlsh);
756 if (PREDICT_FALSE (fd < 0))
762 FD_SET (fd, readfds);
769 clib_bitmap_foreach (si, ldpw->wr_bitmap, ({
770 vlsh = vls_session_index_to_vlsh (si);
771 fd = ldp_vlsh_to_fd (vlsh);
772 if (PREDICT_FALSE (fd < 0))
778 FD_SET (fd, writefds);
785 clib_bitmap_foreach (si, ldpw->ex_bitmap, ({
786 vlsh = vls_session_index_to_vlsh (si);
787 fd = ldp_vlsh_to_fd (vlsh);
788 if (PREDICT_FALSE (fd < 0))
794 FD_SET (fd, exceptfds);
798 ldpw->select_vcl = 1;
803 ldpw->select_vcl = 0;
807 struct timespec tspec;
810 clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap,
811 vec_len (ldpw->rd_bitmap) *
812 sizeof (clib_bitmap_t));
814 clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap,
815 vec_len (ldpw->wr_bitmap) *
816 sizeof (clib_bitmap_t));
818 clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap,
819 vec_len (ldpw->ex_bitmap) *
820 sizeof (clib_bitmap_t));
821 tspec.tv_sec = tspec.tv_nsec = 0;
822 rv = libc_pselect (libc_bits,
823 readfds ? readfds : NULL,
824 writefds ? writefds : NULL,
825 exceptfds ? exceptfds : NULL, &tspec, sigmask);
830 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
834 /* TBD: set timeout to amount of time left */
835 clib_bitmap_zero (ldpw->rd_bitmap);
836 clib_bitmap_zero (ldpw->sid_rd_bitmap);
837 clib_bitmap_zero (ldpw->libc_rd_bitmap);
838 clib_bitmap_zero (ldpw->wr_bitmap);
839 clib_bitmap_zero (ldpw->sid_wr_bitmap);
840 clib_bitmap_zero (ldpw->libc_wr_bitmap);
841 clib_bitmap_zero (ldpw->ex_bitmap);
842 clib_bitmap_zero (ldpw->sid_ex_bitmap);
843 clib_bitmap_zero (ldpw->libc_ex_bitmap);
849 select (int nfds, fd_set * __restrict readfds,
850 fd_set * __restrict writefds,
851 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
853 struct timespec tspec;
857 tspec.tv_sec = timeout->tv_sec;
858 tspec.tv_nsec = timeout->tv_usec * 1000;
860 return ldp_pselect (nfds, readfds, writefds, exceptfds,
861 timeout ? &tspec : NULL, NULL);
866 pselect (int nfds, fd_set * __restrict readfds,
867 fd_set * __restrict writefds,
868 fd_set * __restrict exceptfds,
869 const struct timespec *__restrict timeout,
870 const __sigset_t * __restrict sigmask)
872 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
877 socket (int domain, int type, int protocol)
879 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
880 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
883 if ((errno = -ldp_init ()))
886 if (((domain == AF_INET) || (domain == AF_INET6)) &&
887 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
889 u8 proto = ((sock_type == SOCK_DGRAM) ?
890 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
892 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
893 proto, vppcom_proto_str (proto), is_nonblocking);
895 vlsh = vls_create (proto, is_nonblocking);
903 rv = ldp_vlsh_to_fd (vlsh);
908 LDBG (0, "calling libc_socket");
909 rv = libc_socket (domain, type, protocol);
916 * Create two new sockets, of type TYPE in domain DOMAIN and using
917 * protocol PROTOCOL, which are connected to each other, and put file
918 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
919 * one will be chosen automatically.
920 * Returns 0 on success, -1 for errors.
923 socketpair (int domain, int type, int protocol, int fds[2])
925 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
927 if ((errno = -ldp_init ()))
930 if (((domain == AF_INET) || (domain == AF_INET6)) &&
931 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
939 LDBG (1, "calling libc_socketpair");
940 rv = libc_socketpair (domain, type, protocol, fds);
947 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
952 if ((errno = -ldp_init ()))
955 vlsh = ldp_fd_to_vlsh (fd);
956 if (vlsh != VLS_INVALID_HANDLE)
960 switch (addr->sa_family)
963 if (len != sizeof (struct sockaddr_in))
965 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
971 ep.is_ip4 = VPPCOM_IS_IP4;
972 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
973 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
977 if (len != sizeof (struct sockaddr_in6))
979 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
985 ep.is_ip4 = VPPCOM_IS_IP6;
986 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
987 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
991 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
992 fd, vlsh, addr->sa_family);
993 errno = EAFNOSUPPORT;
997 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1000 rv = vls_bind (vlsh, &ep);
1001 if (rv != VPPCOM_OK)
1009 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1010 rv = libc_bind (fd, addr, len);
1014 LDBG (1, "fd %d: returning %d", fd, rv);
1020 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1021 vppcom_endpt_t * ep)
1024 int sa_len, copy_len;
1026 if ((errno = -ldp_init ()))
1029 if (addr && len && ep)
1031 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1032 switch (addr->sa_family)
1035 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1036 if (*len > sizeof (struct sockaddr_in))
1037 *len = sizeof (struct sockaddr_in);
1038 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1039 copy_len = *len - sa_len;
1041 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1046 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1047 if (*len > sizeof (struct sockaddr_in6))
1048 *len = sizeof (struct sockaddr_in6);
1049 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1050 copy_len = *len - sa_len;
1052 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1053 __in6_u.__u6_addr8, ep->ip, copy_len);
1066 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1071 if ((errno = -ldp_init ()))
1074 vlsh = ldp_fd_to_vlsh (fd);
1075 if (vlsh != VLS_INVALID_HANDLE)
1078 u8 addr_buf[sizeof (struct in6_addr)];
1079 u32 size = sizeof (ep);
1083 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1084 if (rv != VPPCOM_OK)
1091 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1092 if (rv != VPPCOM_OK)
1101 rv = libc_getsockname (fd, addr, len);
1108 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1113 if ((errno = -ldp_init ()))
1118 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1124 vlsh = ldp_fd_to_vlsh (fd);
1125 if (vlsh != VLS_INVALID_HANDLE)
1129 switch (addr->sa_family)
1132 if (len != sizeof (struct sockaddr_in))
1134 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1140 ep.is_ip4 = VPPCOM_IS_IP4;
1141 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1142 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1146 if (len != sizeof (struct sockaddr_in6))
1148 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1154 ep.is_ip4 = VPPCOM_IS_IP6;
1155 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1156 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1160 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1161 fd, vlsh, addr->sa_family);
1162 errno = EAFNOSUPPORT;
1166 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1169 rv = vls_connect (vlsh, &ep);
1170 if (rv != VPPCOM_OK)
1178 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1181 rv = libc_connect (fd, addr, len);
1185 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1190 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1195 if ((errno = -ldp_init ()))
1198 vlsh = ldp_fd_to_vlsh (fd);
1199 if (vlsh != VLS_INVALID_HANDLE)
1202 u8 addr_buf[sizeof (struct in6_addr)];
1203 u32 size = sizeof (ep);
1206 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1207 if (rv != VPPCOM_OK)
1214 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1215 if (rv != VPPCOM_OK)
1224 rv = libc_getpeername (fd, addr, len);
1231 send (int fd, const void *buf, size_t n, int flags)
1233 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1236 if ((errno = -ldp_init ()))
1239 if (vlsh != VLS_INVALID_HANDLE)
1241 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1242 if (size < VPPCOM_OK)
1250 size = libc_send (fd, buf, n, flags);
1257 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1259 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1263 if ((errno = -ldp_init ()))
1266 vlsh = ldp_fd_to_vlsh (out_fd);
1267 if (vlsh != VLS_INVALID_HANDLE)
1270 ssize_t results = 0;
1271 size_t n_bytes_left = len;
1272 size_t bytes_to_read;
1275 u32 flags, flags_len = sizeof (flags);
1277 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1278 if (PREDICT_FALSE (rv != VPPCOM_OK))
1280 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1281 out_fd, vlsh, rv, vppcom_retval_str (rv));
1283 vec_reset_length (ldpw->io_buffer);
1291 off_t off = lseek (in_fd, *offset, SEEK_SET);
1292 if (PREDICT_FALSE (off == -1))
1298 ASSERT (off == *offset);
1303 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1306 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1307 out_fd, vlsh, size, vppcom_retval_str (size));
1308 vec_reset_length (ldpw->io_buffer);
1314 bytes_to_read = size;
1315 if (bytes_to_read == 0)
1317 if (flags & O_NONBLOCK)
1326 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1327 vec_validate (ldpw->io_buffer, bytes_to_read);
1328 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1333 vec_reset_length (ldpw->io_buffer);
1340 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1343 if (size == VPPCOM_EAGAIN)
1345 if (flags & O_NONBLOCK)
1356 vec_reset_length (ldpw->io_buffer);
1365 ASSERT (n_bytes_left >= nbytes);
1366 n_bytes_left = n_bytes_left - nbytes;
1368 while (n_bytes_left > 0);
1371 vec_reset_length (ldpw->io_buffer);
1374 off_t off = lseek (in_fd, *offset, SEEK_SET);
1375 if (PREDICT_FALSE (off == -1))
1381 ASSERT (off == *offset);
1382 *offset += results + 1;
1394 size = libc_sendfile (out_fd, in_fd, offset, len);
1402 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1404 return sendfile (out_fd, in_fd, offset, len);
1408 recv (int fd, void *buf, size_t n, int flags)
1413 if ((errno = -ldp_init ()))
1416 vlsh = ldp_fd_to_vlsh (fd);
1417 if (vlsh != VLS_INVALID_HANDLE)
1419 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1425 size = libc_recv (fd, buf, n, flags);
1432 sendto (int fd, const void *buf, size_t n, int flags,
1433 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1438 if ((errno = -ldp_init ()))
1441 vlsh = ldp_fd_to_vlsh (fd);
1442 if (vlsh != INVALID_SESSION_ID)
1444 vppcom_endpt_t *ep = 0;
1450 switch (addr->sa_family)
1453 ep->is_ip4 = VPPCOM_IS_IP4;
1455 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1457 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1461 ep->is_ip4 = VPPCOM_IS_IP6;
1463 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1465 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1469 errno = EAFNOSUPPORT;
1475 size = vls_sendto (vlsh, (void *) buf, n, flags, ep);
1484 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1492 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1493 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1498 if ((errno = -ldp_init ()))
1501 sid = ldp_fd_to_vlsh (fd);
1502 if (sid != VLS_INVALID_HANDLE)
1505 u8 src_addr[sizeof (struct sockaddr_in6)];
1510 size = vls_recvfrom (sid, buf, n, flags, &ep);
1513 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1516 size = vls_recvfrom (sid, buf, n, flags, NULL);
1526 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1533 sendmsg (int fd, const struct msghdr * message, int flags)
1538 if ((errno = -ldp_init ()))
1541 vlsh = ldp_fd_to_vlsh (fd);
1542 if (vlsh != VLS_INVALID_HANDLE)
1544 LDBG (0, "LDP-TBD");
1550 size = libc_sendmsg (fd, message, flags);
1558 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1561 const char *func_str;
1562 u32 sh = ldp_fd_to_vlsh (fd);
1564 if ((errno = -ldp_init ()))
1567 if (sh != INVALID_SESSION_ID)
1569 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1575 func_str = "libc_sendmmsg";
1578 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1579 "vmessages %p, vlen %u, flags 0x%x",
1580 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1582 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1589 int errno_val = errno;
1591 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1592 "rv %d, errno = %d", getpid (), fd, fd,
1593 func_str, size, errno_val);
1597 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1598 getpid (), fd, fd, size, size);
1605 recvmsg (int fd, struct msghdr * message, int flags)
1610 if ((errno = -ldp_init ()))
1613 vlsh = ldp_fd_to_vlsh (fd);
1614 if (vlsh != VLS_INVALID_HANDLE)
1616 LDBG (0, "LDP-TBD");
1622 size = libc_recvmsg (fd, message, flags);
1630 recvmmsg (int fd, struct mmsghdr *vmessages,
1631 unsigned int vlen, int flags, struct timespec *tmo)
1634 const char *func_str;
1635 u32 sh = ldp_fd_to_vlsh (fd);
1637 if ((errno = -ldp_init ()))
1640 if (sh != INVALID_SESSION_ID)
1642 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1648 func_str = "libc_recvmmsg";
1651 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1652 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1653 getpid (), fd, fd, func_str, vmessages, vlen,
1656 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1663 int errno_val = errno;
1665 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1666 "rv %d, errno = %d", getpid (), fd, fd,
1667 func_str, size, errno_val);
1671 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1672 getpid (), fd, fd, size, size);
1679 getsockopt (int fd, int level, int optname,
1680 void *__restrict optval, socklen_t * __restrict optlen)
1685 if ((errno = -ldp_init ()))
1688 vlsh = ldp_fd_to_vlsh (fd);
1689 if (vlsh != VLS_INVALID_HANDLE)
1699 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1703 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1707 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1711 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1715 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1717 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1718 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1719 memset (optval, 0, *optlen);
1726 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1727 "optname %d unsupported!", fd, vlsh, optname);
1735 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1738 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1739 "optname %d unsupported!", fd, vlsh, optname);
1747 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1750 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1753 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1754 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1757 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1761 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1765 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1768 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1771 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1774 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1775 "optname %d unsupported!", fd, vlsh, optname);
1783 if (rv != VPPCOM_OK)
1791 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1798 setsockopt (int fd, int level, int optname,
1799 const void *optval, socklen_t optlen)
1804 if ((errno = -ldp_init ()))
1807 vlsh = ldp_fd_to_vlsh (fd);
1808 if (vlsh != VLS_INVALID_HANDLE)
1818 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1819 (void *) optval, &optlen);
1822 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1823 (void *) optval, &optlen);
1826 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1827 (void *) optval, &optlen);
1830 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
1831 (void *) optval, &optlen);
1834 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
1835 "optname %d unsupported!", fd, vlsh, optname);
1843 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
1844 (void *) optval, &optlen);
1847 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
1848 "optname %d unsupported!", fd, vlsh, optname);
1856 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
1857 (void *) optval, &optlen);
1860 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
1861 (void *) optval, &optlen);
1864 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
1865 (void *) optval, &optlen);
1868 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
1869 "optname %d unsupported!", fd, vlsh, optname);
1877 if (rv != VPPCOM_OK)
1885 rv = libc_setsockopt (fd, level, optname, optval, optlen);
1892 listen (int fd, int n)
1897 if ((errno = -ldp_init ()))
1900 vlsh = ldp_fd_to_vlsh (fd);
1901 if (vlsh != VLS_INVALID_HANDLE)
1903 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
1905 rv = vls_listen (vlsh, n);
1906 if (rv != VPPCOM_OK)
1914 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
1915 rv = libc_listen (fd, n);
1918 LDBG (1, "fd %d: returning %d", fd, rv);
1923 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
1924 socklen_t * __restrict addr_len, int flags)
1926 vls_handle_t listen_vlsh, accept_vlsh;
1929 if ((errno = -ldp_init ()))
1932 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
1933 if (listen_vlsh != VLS_INVALID_HANDLE)
1936 u8 src_addr[sizeof (struct sockaddr_in6)];
1937 memset (&ep, 0, sizeof (ep));
1940 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
1941 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
1943 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
1944 if (accept_vlsh < 0)
1946 errno = -accept_vlsh;
1951 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1952 if (rv != VPPCOM_OK)
1954 (void) vls_close (accept_vlsh);
1960 rv = ldp_vlsh_to_fd (accept_vlsh);
1966 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
1967 " flags 0x%x", listen_fd, addr, addr_len, flags);
1969 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
1972 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
1978 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
1981 return ldp_accept4 (fd, addr, addr_len, flags);
1985 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1987 return ldp_accept4 (fd, addr, addr_len, 0);
1991 shutdown (int fd, int how)
1995 u32 flags_len = sizeof (flags);
1997 if ((errno = -ldp_init ()))
2000 vlsh = ldp_fd_to_vlsh (fd);
2001 if (vlsh != VLS_INVALID_HANDLE)
2003 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2005 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
2011 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2017 if (flags == SHUT_RDWR)
2022 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2023 rv = libc_shutdown (fd, how);
2030 epoll_create1 (int flags)
2032 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2036 if ((errno = -ldp_init ()))
2039 if (ldp->vcl_needs_real_epoll)
2041 rv = libc_epoll_create1 (flags);
2042 ldp->vcl_needs_real_epoll = 0;
2043 ldpw->vcl_mq_epfd = rv;
2044 LDBG (0, "created vcl epfd %u", rv);
2048 vlsh = vls_epoll_create ();
2049 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2056 rv = ldp_vlsh_to_fd (vlsh);
2058 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2063 epoll_create (int size)
2065 return epoll_create1 (0);
2069 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2071 vls_handle_t vep_vlsh, vlsh;
2074 if ((errno = -ldp_init ()))
2077 vep_vlsh = ldp_fd_to_vlsh (epfd);
2078 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2080 /* The LDP epoll_create1 always creates VCL epfd's.
2081 * The app should never have a kernel base epoll fd unless it
2082 * was acquired outside of the LD_PRELOAD process context.
2083 * In any case, if we get one, punt it to libc_epoll_ctl.
2085 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2086 " event %p", epfd, op, fd, event);
2088 rv = libc_epoll_ctl (epfd, op, fd, event);
2092 vlsh = ldp_fd_to_vlsh (fd);
2094 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2097 if (vlsh != VLS_INVALID_HANDLE)
2099 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2100 " event %p", epfd, vep_vlsh, vlsh, event);
2102 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2103 if (rv != VPPCOM_OK)
2112 u32 size = sizeof (epfd);
2114 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2117 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2118 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2120 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2127 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2136 else if (PREDICT_FALSE (libc_epfd < 0))
2143 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2144 " event %p", epfd, libc_epfd, op, fd, event);
2146 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2154 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2155 int timeout, const sigset_t * sigmask)
2157 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2158 double time_to_wait = (double) 0, time_out, now = 0;
2159 int libc_epfd, rv = 0;
2160 vls_handle_t ep_vlsh;
2162 if ((errno = -ldp_init ()))
2165 if (PREDICT_FALSE (!events || (timeout < -1)))
2171 if (epfd == ldpw->vcl_mq_epfd)
2172 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2174 ep_vlsh = ldp_fd_to_vlsh (epfd);
2175 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2177 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2182 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2183 time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
2185 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2186 if (PREDICT_FALSE (libc_epfd < 0))
2193 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2194 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2195 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait,
2199 if (!ldpw->epoll_wait_vcl)
2201 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2204 ldpw->epoll_wait_vcl = 1;
2215 ldpw->epoll_wait_vcl = 0;
2219 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2225 now = clib_time_now (&ldpw->clib_time);
2227 while (now < time_out);
2234 epoll_pwait (int epfd, struct epoll_event *events,
2235 int maxevents, int timeout, const sigset_t * sigmask)
2237 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2241 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2243 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2247 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2249 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2250 int rv, i, n_revents = 0;
2253 double wait_for_time;
2255 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
2258 wait_for_time = (f64) timeout / 1000;
2262 for (i = 0; i < nfds; i++)
2267 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2268 if (vlsh != VLS_INVALID_HANDLE)
2270 fds[i].fd = -fds[i].fd;
2271 vec_add2 (ldpw->vcl_poll, vp, 1);
2273 vp->sh = vlsh_to_sh (vlsh);
2274 vp->events = fds[i].events;
2275 #ifdef __USE_XOPEN2K
2276 if (fds[i].events & POLLRDNORM)
2277 vp->events |= POLLIN;
2278 if (fds[i].events & POLLWRNORM)
2279 vp->events |= POLLOUT;
2281 vp->revents = fds[i].revents;
2285 vec_add1 (ldpw->libc_poll, fds[i]);
2286 vec_add1 (ldpw->libc_poll_idxs, i);
2292 if (vec_len (ldpw->vcl_poll))
2294 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2305 if (vec_len (ldpw->libc_poll))
2307 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2320 while ((wait_for_time == -1) ||
2321 (clib_time_now (&ldpw->clib_time) < wait_for_time));
2325 vec_foreach (vp, ldpw->vcl_poll)
2327 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2328 fds[vp->fds_ndx].revents = vp->revents;
2329 #ifdef __USE_XOPEN2K
2330 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2331 (fds[vp->fds_ndx].events & POLLRDNORM))
2332 fds[vp->fds_ndx].revents |= POLLRDNORM;
2333 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2334 (fds[vp->fds_ndx].events & POLLWRNORM))
2335 fds[vp->fds_ndx].revents |= POLLWRNORM;
2338 vec_reset_length (ldpw->vcl_poll);
2340 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2342 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2344 vec_reset_length (ldpw->libc_poll_idxs);
2345 vec_reset_length (ldpw->libc_poll);
2352 ppoll (struct pollfd *fds, nfds_t nfds,
2353 const struct timespec *timeout, const sigset_t * sigmask)
2355 if ((errno = -ldp_init ()))
2358 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2366 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2368 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2371 * This function is called when the library is loaded
2374 ldp_constructor (void)
2376 swrap_constructor ();
2377 if (ldp_init () != 0)
2378 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2380 else if (LDP_DEBUG > 0)
2381 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2385 * This function is called when the library is unloaded
2388 ldp_destructor (void)
2390 swrap_destructor ();
2394 /* Don't use clib_warning() here because that calls writev()
2395 * which will call ldp_init().
2398 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
2399 __func__, __LINE__, getpid ());
2404 * fd.io coding-style-patch-verification: ON
2407 * eval: (c-set-style "gnu")