2 * Copyright (c) 2016-2019 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 *si_rd_bitmap;
66 clib_bitmap_t *si_wr_bitmap;
67 clib_bitmap_t *si_ex_bitmap;
68 clib_bitmap_t *libc_rd_bitmap;
69 clib_bitmap_t *libc_wr_bitmap;
70 clib_bitmap_t *libc_ex_bitmap;
76 struct pollfd *libc_poll;
87 /* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
88 * they are the same size */
89 STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
90 "ldp bitmap size mismatch");
91 STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
92 "ldp bitmap size mismatch");
96 ldp_worker_ctx_t *workers;
98 char app_name[LDP_APP_NAME_MAX];
104 /** vcl needs next epoll_create to go to libc_epoll */
105 u8 vcl_needs_real_epoll;
108 #define LDP_DEBUG ldp->debug
110 #define LDBG(_lvl, _fmt, _args...) \
111 if (ldp->debug > _lvl) \
113 int errno_saved = errno; \
114 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args); \
115 errno = errno_saved; \
118 static ldp_main_t ldp_main = {
119 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
120 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
121 .debug = LDP_DEBUG_INIT,
122 .transparent_tls = 0,
125 static ldp_main_t *ldp = &ldp_main;
127 static inline ldp_worker_ctx_t *
128 ldp_worker_get_current (void)
130 return (ldp->workers + vppcom_worker_index ());
134 * RETURN: 0 on success or -1 on error.
137 ldp_set_app_name (char *app_name)
139 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
140 "ldp-%d-%s", getpid (), app_name);
146 if (ldp->app_name[0] == '\0')
147 ldp_set_app_name ("app");
149 return ldp->app_name;
153 ldp_vlsh_to_fd (vls_handle_t vlsh)
155 return (vlsh + ldp->vlsh_bit_val);
158 static inline vls_handle_t
159 ldp_fd_to_vlsh (int fd)
161 if (fd < ldp->vlsh_bit_val)
162 return VLS_INVALID_HANDLE;
164 return (fd - ldp->vlsh_bit_val);
168 ldp_alloc_workers (void)
172 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
178 ldp_worker_ctx_t *ldpw;
181 if (PREDICT_TRUE (ldp->init))
185 ldp->vcl_needs_real_epoll = 1;
186 rv = vls_app_create (ldp_get_app_name ());
189 ldp->vcl_needs_real_epoll = 0;
190 if (rv == VPPCOM_EEXIST)
192 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
193 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
197 ldp->vcl_needs_real_epoll = 0;
198 ldp_alloc_workers ();
199 ldpw = ldp_worker_get_current ();
201 char *env_var_str = getenv (LDP_ENV_DEBUG);
205 if (sscanf (env_var_str, "%u", &tmp) != 1)
206 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
207 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
212 LDBG (0, "configured LDP debug level (%u) from env var "
213 LDP_ENV_DEBUG "!", ldp->debug);
217 env_var_str = getenv (LDP_ENV_APP_NAME);
220 ldp_set_app_name (env_var_str);
221 LDBG (0, "configured LDP app name (%s) from the env var "
222 LDP_ENV_APP_NAME "!", ldp->app_name);
225 env_var_str = getenv (LDP_ENV_SID_BIT);
229 if (sscanf (env_var_str, "%u", &sb) != 1)
231 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
232 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
233 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
235 else if (sb < LDP_SID_BIT_MIN)
237 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
238 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
240 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
241 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
242 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
243 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
245 else if (sb > LDP_SID_BIT_MAX)
247 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
248 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
250 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
251 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
252 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
253 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
257 ldp->vlsh_bit_val = (1 << sb);
258 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
260 LDBG (0, "configured LDP sid bit (%u) from "
261 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
262 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
265 /* Make sure there are enough bits in the fd set for vcl sessions */
266 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
268 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
269 ldp->vlsh_bit_val, FD_SETSIZE / 2);
274 env_var_str = getenv (LDP_ENV_TLS_TRANS);
277 ldp->transparent_tls = 1;
281 pool_foreach (ldpw, ldp->workers, ({
282 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
286 LDBG (0, "LDP initialization: done!");
297 if ((errno = -ldp_init ()))
300 vlsh = ldp_fd_to_vlsh (fd);
301 if (vlsh != VLS_INVALID_HANDLE)
303 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
306 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
308 rv = libc_close (epfd);
311 u32 size = sizeof (epfd);
314 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
317 else if (PREDICT_FALSE (epfd < 0))
324 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
326 rv = vls_close (vlsh);
335 LDBG (0, "fd %d: calling libc_close", fd);
336 rv = libc_close (fd);
344 read (int fd, void *buf, size_t nbytes)
349 if ((errno = -ldp_init ()))
352 vlsh = ldp_fd_to_vlsh (fd);
353 if (vlsh != VLS_INVALID_HANDLE)
355 size = vls_read (vlsh, buf, nbytes);
364 size = libc_read (fd, buf, nbytes);
371 readv (int fd, const struct iovec * iov, int iovcnt)
373 int rv = 0, i, total = 0;
377 if ((errno = -ldp_init ()))
380 vlsh = ldp_fd_to_vlsh (fd);
381 if (vlsh != VLS_INVALID_HANDLE)
383 for (i = 0; i < iovcnt; ++i)
385 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
391 if (rv < iov[i].iov_len)
395 if (rv < 0 && total == 0)
405 size = libc_readv (fd, iov, iovcnt);
412 write (int fd, const void *buf, size_t nbytes)
417 if ((errno = -ldp_init ()))
420 vlsh = ldp_fd_to_vlsh (fd);
421 if (vlsh != VLS_INVALID_HANDLE)
423 size = vls_write_msg (vlsh, (void *) buf, nbytes);
432 size = libc_write (fd, buf, nbytes);
439 writev (int fd, const struct iovec * iov, int iovcnt)
441 ssize_t size = 0, total = 0;
445 if ((errno = -ldp_init ()))
448 vlsh = ldp_fd_to_vlsh (fd);
449 if (vlsh != VLS_INVALID_HANDLE)
451 for (i = 0; i < iovcnt; ++i)
453 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
459 if (rv < iov[i].iov_len)
464 if (rv < 0 && total == 0)
474 size = libc_writev (fd, iov, iovcnt);
482 fcntl64 (int fd, int cmd, ...)
485 fcntl (int fd, int cmd, ...)
492 if ((errno = -ldp_init ()))
497 vlsh = ldp_fd_to_vlsh (fd);
498 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
499 if (vlsh != VLS_INVALID_HANDLE)
501 int flags = va_arg (ap, int);
504 size = sizeof (flags);
509 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
513 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
518 /* TODO handle this */
519 LDBG (0, "F_SETFD ignored flags %u", flags);
535 rv = libc_vfcntl64 (fd, cmd, ap);
537 rv = libc_vfcntl (fd, cmd, ap);
547 ioctl (int fd, unsigned long int cmd, ...)
553 if ((errno = -ldp_init ()))
558 vlsh = ldp_fd_to_vlsh (fd);
559 if (vlsh != VLS_INVALID_HANDLE)
564 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
569 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
570 u32 size = sizeof (flags);
572 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
573 * non-blocking, the flags should be read here and merged
576 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
592 rv = libc_vioctl (fd, cmd, ap);
600 ldp_select_init_maps (fd_set * __restrict original,
601 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
602 clib_bitmap_t ** vclb, int nfds, u32 minbits,
603 u32 n_bytes, uword * si_bits, uword * libc_bits)
605 uword si_bits_set, libc_bits_set;
609 clib_bitmap_validate (*vclb, minbits);
610 clib_bitmap_validate (*libcb, minbits);
611 clib_bitmap_validate (*resultb, minbits);
612 clib_memcpy_fast (*resultb, original, n_bytes);
613 memset (original, 0, n_bytes);
616 clib_bitmap_foreach (fd, *resultb, ({
619 vlsh = ldp_fd_to_vlsh (fd);
620 if (vlsh == VLS_INVALID_HANDLE)
621 clib_bitmap_set_no_check (*libcb, fd, 1);
623 clib_bitmap_set_no_check (*vclb, vlsh_to_session_index (vlsh), 1);
627 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
628 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
630 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
631 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
635 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
645 clib_bitmap_foreach (si, vclb, ({
646 vlsh = vls_session_index_to_vlsh (si);
647 fd = ldp_vlsh_to_fd (vlsh);
648 if (PREDICT_FALSE (fd < 0))
661 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
669 clib_bitmap_foreach (fd, result, ({
670 FD_SET ((int)fd, libcb);
676 ldp_pselect (int nfds, fd_set * __restrict readfds,
677 fd_set * __restrict writefds,
678 fd_set * __restrict exceptfds,
679 const struct timespec *__restrict timeout,
680 const __sigset_t * __restrict sigmask)
682 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
683 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
684 struct timespec libc_tspec = { 0 };
685 f64 time_out, vcl_timeout = 0;
686 uword si_bits, libc_bits;
687 int rv, bits_set = 0;
695 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
696 clib_time_init (&ldpw->clib_time);
700 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
701 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
703 /* select as fine grained sleep */
706 time_out += clib_time_now (&ldpw->clib_time);
707 while (clib_time_now (&ldpw->clib_time) < time_out)
720 if (nfds <= ldp->vlsh_bit_val)
722 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
727 si_bits = libc_bits = 0;
728 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
731 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
732 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
733 &si_bits, &libc_bits);
735 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
736 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
737 minbits, n_bytes, &si_bits, &libc_bits);
739 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
740 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
741 minbits, n_bytes, &si_bits, &libc_bits);
743 if (PREDICT_FALSE (!si_bits && !libc_bits))
751 libc_tspec = timeout ? *timeout : libc_tspec;
758 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
759 vec_len (ldpw->rd_bitmap) *
760 sizeof (clib_bitmap_t));
762 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
763 vec_len (ldpw->wr_bitmap) *
764 sizeof (clib_bitmap_t));
766 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
767 vec_len (ldpw->ex_bitmap) *
768 sizeof (clib_bitmap_t));
770 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
771 writefds ? ldpw->wr_bitmap : NULL,
772 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
780 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
786 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
792 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
803 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
804 vec_len (ldpw->libc_rd_bitmap) *
805 sizeof (clib_bitmap_t));
807 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
808 vec_len (ldpw->libc_wr_bitmap) *
809 sizeof (clib_bitmap_t));
811 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
812 vec_len (ldpw->libc_ex_bitmap) *
813 sizeof (clib_bitmap_t));
815 rv = libc_pselect (libc_bits,
816 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
817 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
818 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
819 &libc_tspec, sigmask);
822 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
823 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
824 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
835 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
839 /* TBD: set timeout to amount of time left */
840 clib_bitmap_zero (ldpw->rd_bitmap);
841 clib_bitmap_zero (ldpw->si_rd_bitmap);
842 clib_bitmap_zero (ldpw->libc_rd_bitmap);
843 clib_bitmap_zero (ldpw->wr_bitmap);
844 clib_bitmap_zero (ldpw->si_wr_bitmap);
845 clib_bitmap_zero (ldpw->libc_wr_bitmap);
846 clib_bitmap_zero (ldpw->ex_bitmap);
847 clib_bitmap_zero (ldpw->si_ex_bitmap);
848 clib_bitmap_zero (ldpw->libc_ex_bitmap);
854 select (int nfds, fd_set * __restrict readfds,
855 fd_set * __restrict writefds,
856 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
858 struct timespec tspec;
862 tspec.tv_sec = timeout->tv_sec;
863 tspec.tv_nsec = timeout->tv_usec * 1000;
865 return ldp_pselect (nfds, readfds, writefds, exceptfds,
866 timeout ? &tspec : NULL, NULL);
871 pselect (int nfds, fd_set * __restrict readfds,
872 fd_set * __restrict writefds,
873 fd_set * __restrict exceptfds,
874 const struct timespec *__restrict timeout,
875 const __sigset_t * __restrict sigmask)
877 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
881 /* If transparent TLS mode is turned on, then ldp will load key and cert.
884 load_tls_cert (vls_handle_t vlsh)
886 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
894 fp = fopen (env_var_str, "r");
897 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
900 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
902 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
908 LDBG (0, "ERROR: failed to read LDP environment %s\n",
916 load_tls_key (vls_handle_t vlsh)
918 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
926 fp = fopen (env_var_str, "r");
929 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
932 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
934 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
940 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
947 socket (int domain, int type, int protocol)
949 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
950 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
953 if ((errno = -ldp_init ()))
956 if (((domain == AF_INET) || (domain == AF_INET6)) &&
957 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
960 if (ldp->transparent_tls)
962 proto = VPPCOM_PROTO_TLS;
965 proto = ((sock_type == SOCK_DGRAM) ?
966 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
968 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
969 proto, vppcom_proto_str (proto), is_nonblocking);
971 vlsh = vls_create (proto, is_nonblocking);
979 if (ldp->transparent_tls)
981 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
986 rv = ldp_vlsh_to_fd (vlsh);
991 LDBG (0, "calling libc_socket");
992 rv = libc_socket (domain, type, protocol);
999 * Create two new sockets, of type TYPE in domain DOMAIN and using
1000 * protocol PROTOCOL, which are connected to each other, and put file
1001 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1002 * one will be chosen automatically.
1003 * Returns 0 on success, -1 for errors.
1006 socketpair (int domain, int type, int protocol, int fds[2])
1008 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1010 if ((errno = -ldp_init ()))
1013 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1014 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1016 LDBG (0, "LDP-TBD");
1022 LDBG (1, "calling libc_socketpair");
1023 rv = libc_socketpair (domain, type, protocol, fds);
1030 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1035 if ((errno = -ldp_init ()))
1038 vlsh = ldp_fd_to_vlsh (fd);
1039 if (vlsh != VLS_INVALID_HANDLE)
1043 switch (addr->sa_family)
1046 if (len != sizeof (struct sockaddr_in))
1048 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1054 ep.is_ip4 = VPPCOM_IS_IP4;
1055 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1056 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1060 if (len != sizeof (struct sockaddr_in6))
1062 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1068 ep.is_ip4 = VPPCOM_IS_IP6;
1069 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1070 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1074 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1075 fd, vlsh, addr->sa_family);
1076 errno = EAFNOSUPPORT;
1080 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1083 rv = vls_bind (vlsh, &ep);
1084 if (rv != VPPCOM_OK)
1092 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1093 rv = libc_bind (fd, addr, len);
1097 LDBG (1, "fd %d: returning %d", fd, rv);
1103 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1104 vppcom_endpt_t * ep)
1107 int sa_len, copy_len;
1109 if ((errno = -ldp_init ()))
1112 if (addr && len && ep)
1114 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1115 switch (addr->sa_family)
1118 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1119 if (*len > sizeof (struct sockaddr_in))
1120 *len = sizeof (struct sockaddr_in);
1121 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1122 copy_len = *len - sa_len;
1124 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1129 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1130 if (*len > sizeof (struct sockaddr_in6))
1131 *len = sizeof (struct sockaddr_in6);
1132 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1133 copy_len = *len - sa_len;
1135 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1136 __in6_u.__u6_addr8, ep->ip, copy_len);
1149 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1154 if ((errno = -ldp_init ()))
1157 vlsh = ldp_fd_to_vlsh (fd);
1158 if (vlsh != VLS_INVALID_HANDLE)
1161 u8 addr_buf[sizeof (struct in6_addr)];
1162 u32 size = sizeof (ep);
1166 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1167 if (rv != VPPCOM_OK)
1174 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1175 if (rv != VPPCOM_OK)
1184 rv = libc_getsockname (fd, addr, len);
1191 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1196 if ((errno = -ldp_init ()))
1201 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1207 vlsh = ldp_fd_to_vlsh (fd);
1208 if (vlsh != VLS_INVALID_HANDLE)
1212 switch (addr->sa_family)
1215 if (len != sizeof (struct sockaddr_in))
1217 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1223 ep.is_ip4 = VPPCOM_IS_IP4;
1224 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1225 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1229 if (len != sizeof (struct sockaddr_in6))
1231 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1237 ep.is_ip4 = VPPCOM_IS_IP6;
1238 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1239 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1243 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1244 fd, vlsh, addr->sa_family);
1245 errno = EAFNOSUPPORT;
1249 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1252 rv = vls_connect (vlsh, &ep);
1253 if (rv != VPPCOM_OK)
1261 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1264 rv = libc_connect (fd, addr, len);
1268 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1273 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1278 if ((errno = -ldp_init ()))
1281 vlsh = ldp_fd_to_vlsh (fd);
1282 if (vlsh != VLS_INVALID_HANDLE)
1285 u8 addr_buf[sizeof (struct in6_addr)];
1286 u32 size = sizeof (ep);
1289 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1290 if (rv != VPPCOM_OK)
1297 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1298 if (rv != VPPCOM_OK)
1307 rv = libc_getpeername (fd, addr, len);
1314 send (int fd, const void *buf, size_t n, int flags)
1316 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1319 if ((errno = -ldp_init ()))
1322 if (vlsh != VLS_INVALID_HANDLE)
1324 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1325 if (size < VPPCOM_OK)
1333 size = libc_send (fd, buf, n, flags);
1340 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1342 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1346 if ((errno = -ldp_init ()))
1349 vlsh = ldp_fd_to_vlsh (out_fd);
1350 if (vlsh != VLS_INVALID_HANDLE)
1353 ssize_t results = 0;
1354 size_t n_bytes_left = len;
1355 size_t bytes_to_read;
1358 u32 flags, flags_len = sizeof (flags);
1360 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1361 if (PREDICT_FALSE (rv != VPPCOM_OK))
1363 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1364 out_fd, vlsh, rv, vppcom_retval_str (rv));
1366 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);
1386 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1389 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1390 out_fd, vlsh, size, vppcom_retval_str (size));
1391 vec_reset_length (ldpw->io_buffer);
1397 bytes_to_read = size;
1398 if (bytes_to_read == 0)
1400 if (flags & O_NONBLOCK)
1409 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1410 vec_validate (ldpw->io_buffer, bytes_to_read);
1411 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1416 vec_reset_length (ldpw->io_buffer);
1423 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1426 if (size == VPPCOM_EAGAIN)
1428 if (flags & O_NONBLOCK)
1439 vec_reset_length (ldpw->io_buffer);
1448 ASSERT (n_bytes_left >= nbytes);
1449 n_bytes_left = n_bytes_left - nbytes;
1451 while (n_bytes_left > 0);
1454 vec_reset_length (ldpw->io_buffer);
1457 off_t off = lseek (in_fd, *offset, SEEK_SET);
1458 if (PREDICT_FALSE (off == -1))
1464 ASSERT (off == *offset);
1465 *offset += results + 1;
1477 size = libc_sendfile (out_fd, in_fd, offset, len);
1485 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1487 return sendfile (out_fd, in_fd, offset, len);
1491 recv (int fd, void *buf, size_t n, int flags)
1496 if ((errno = -ldp_init ()))
1499 vlsh = ldp_fd_to_vlsh (fd);
1500 if (vlsh != VLS_INVALID_HANDLE)
1502 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1511 size = libc_recv (fd, buf, n, flags);
1518 sendto (int fd, const void *buf, size_t n, int flags,
1519 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1524 if ((errno = -ldp_init ()))
1527 vlsh = ldp_fd_to_vlsh (fd);
1528 if (vlsh != INVALID_SESSION_ID)
1530 vppcom_endpt_t *ep = 0;
1536 switch (addr->sa_family)
1539 ep->is_ip4 = VPPCOM_IS_IP4;
1541 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1543 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1547 ep->is_ip4 = VPPCOM_IS_IP6;
1549 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1551 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1555 errno = EAFNOSUPPORT;
1561 size = vls_sendto (vlsh, (void *) buf, n, flags, ep);
1570 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1578 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1579 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1584 if ((errno = -ldp_init ()))
1587 sid = ldp_fd_to_vlsh (fd);
1588 if (sid != VLS_INVALID_HANDLE)
1591 u8 src_addr[sizeof (struct sockaddr_in6)];
1596 size = vls_recvfrom (sid, buf, n, flags, &ep);
1600 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1606 size = vls_recvfrom (sid, buf, n, flags, NULL);
1616 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1623 sendmsg (int fd, const struct msghdr * message, int flags)
1628 if ((errno = -ldp_init ()))
1631 vlsh = ldp_fd_to_vlsh (fd);
1632 if (vlsh != VLS_INVALID_HANDLE)
1634 LDBG (0, "LDP-TBD");
1640 size = libc_sendmsg (fd, message, flags);
1648 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1651 const char *func_str;
1652 u32 sh = ldp_fd_to_vlsh (fd);
1654 if ((errno = -ldp_init ()))
1657 if (sh != INVALID_SESSION_ID)
1659 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1665 func_str = "libc_sendmmsg";
1668 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1669 "vmessages %p, vlen %u, flags 0x%x",
1670 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1672 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1679 int errno_val = errno;
1681 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1682 "rv %d, errno = %d", getpid (), fd, fd,
1683 func_str, size, errno_val);
1687 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1688 getpid (), fd, fd, size, size);
1695 recvmsg (int fd, struct msghdr * message, int flags)
1700 if ((errno = -ldp_init ()))
1703 vlsh = ldp_fd_to_vlsh (fd);
1704 if (vlsh != VLS_INVALID_HANDLE)
1706 LDBG (0, "LDP-TBD");
1712 size = libc_recvmsg (fd, message, flags);
1720 recvmmsg (int fd, struct mmsghdr *vmessages,
1721 unsigned int vlen, int flags, struct timespec *tmo)
1724 const char *func_str;
1725 u32 sh = ldp_fd_to_vlsh (fd);
1727 if ((errno = -ldp_init ()))
1730 if (sh != INVALID_SESSION_ID)
1732 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1738 func_str = "libc_recvmmsg";
1741 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1742 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1743 getpid (), fd, fd, func_str, vmessages, vlen,
1746 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1753 int errno_val = errno;
1755 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1756 "rv %d, errno = %d", getpid (), fd, fd,
1757 func_str, size, errno_val);
1761 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1762 getpid (), fd, fd, size, size);
1769 getsockopt (int fd, int level, int optname,
1770 void *__restrict optval, socklen_t * __restrict optlen)
1775 if ((errno = -ldp_init ()))
1778 vlsh = ldp_fd_to_vlsh (fd);
1779 if (vlsh != VLS_INVALID_HANDLE)
1789 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1793 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1797 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1801 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1805 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1807 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1808 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1809 memset (optval, 0, *optlen);
1815 case TCP_CONGESTION:
1816 *optlen = strlen ("cubic");
1817 strncpy (optval, "cubic", *optlen + 1);
1821 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1822 "optname %d unsupported!", fd, vlsh, optname);
1830 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1833 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1834 "optname %d unsupported!", fd, vlsh, optname);
1842 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1845 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1848 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1849 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1852 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1856 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1860 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1863 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1866 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1869 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1870 "optname %d unsupported!", fd, vlsh, optname);
1878 if (rv != VPPCOM_OK)
1886 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1893 setsockopt (int fd, int level, int optname,
1894 const void *optval, socklen_t optlen)
1899 if ((errno = -ldp_init ()))
1902 vlsh = ldp_fd_to_vlsh (fd);
1903 if (vlsh != VLS_INVALID_HANDLE)
1913 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1914 (void *) optval, &optlen);
1917 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1918 (void *) optval, &optlen);
1921 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1922 (void *) optval, &optlen);
1925 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
1926 (void *) optval, &optlen);
1928 case TCP_CONGESTION:
1934 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
1935 "optname %d unsupported!", fd, vlsh, optname);
1943 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
1944 (void *) optval, &optlen);
1947 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
1948 "optname %d unsupported!", fd, vlsh, optname);
1956 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
1957 (void *) optval, &optlen);
1960 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
1961 (void *) optval, &optlen);
1964 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
1965 (void *) optval, &optlen);
1968 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
1969 "optname %d unsupported!", fd, vlsh, optname);
1977 if (rv != VPPCOM_OK)
1985 rv = libc_setsockopt (fd, level, optname, optval, optlen);
1992 listen (int fd, int n)
1997 if ((errno = -ldp_init ()))
2000 vlsh = ldp_fd_to_vlsh (fd);
2001 if (vlsh != VLS_INVALID_HANDLE)
2003 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2005 rv = vls_listen (vlsh, n);
2006 if (rv != VPPCOM_OK)
2014 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2015 rv = libc_listen (fd, n);
2018 LDBG (1, "fd %d: returning %d", fd, rv);
2023 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2024 socklen_t * __restrict addr_len, int flags)
2026 vls_handle_t listen_vlsh, accept_vlsh;
2029 if ((errno = -ldp_init ()))
2032 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2033 if (listen_vlsh != VLS_INVALID_HANDLE)
2036 u8 src_addr[sizeof (struct sockaddr_in6)];
2037 memset (&ep, 0, sizeof (ep));
2040 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2041 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
2043 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2044 if (accept_vlsh < 0)
2046 errno = -accept_vlsh;
2051 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2052 if (rv != VPPCOM_OK)
2054 (void) vls_close (accept_vlsh);
2060 rv = ldp_vlsh_to_fd (accept_vlsh);
2066 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2067 " flags 0x%x", listen_fd, addr, addr_len, flags);
2069 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2072 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2078 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2081 return ldp_accept4 (fd, addr, addr_len, flags);
2085 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2087 return ldp_accept4 (fd, addr, addr_len, 0);
2091 shutdown (int fd, int how)
2095 u32 flags_len = sizeof (flags);
2097 if ((errno = -ldp_init ()))
2100 vlsh = ldp_fd_to_vlsh (fd);
2101 if (vlsh != VLS_INVALID_HANDLE)
2103 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2105 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
2111 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2117 if (flags == SHUT_RDWR)
2122 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2123 rv = libc_shutdown (fd, how);
2130 epoll_create1 (int flags)
2132 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2136 if ((errno = -ldp_init ()))
2139 if (ldp->vcl_needs_real_epoll)
2141 /* Make sure workers have been allocated */
2144 ldp_alloc_workers ();
2145 ldpw = ldp_worker_get_current ();
2147 rv = libc_epoll_create1 (flags);
2148 ldp->vcl_needs_real_epoll = 0;
2149 ldpw->vcl_mq_epfd = rv;
2150 LDBG (0, "created vcl epfd %u", rv);
2154 vlsh = vls_epoll_create ();
2155 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2162 rv = ldp_vlsh_to_fd (vlsh);
2164 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2169 epoll_create (int size)
2171 return epoll_create1 (0);
2175 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2177 vls_handle_t vep_vlsh, vlsh;
2180 if ((errno = -ldp_init ()))
2183 vep_vlsh = ldp_fd_to_vlsh (epfd);
2184 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2186 /* The LDP epoll_create1 always creates VCL epfd's.
2187 * The app should never have a kernel base epoll fd unless it
2188 * was acquired outside of the LD_PRELOAD process context.
2189 * In any case, if we get one, punt it to libc_epoll_ctl.
2191 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2192 " event %p", epfd, op, fd, event);
2194 rv = libc_epoll_ctl (epfd, op, fd, event);
2198 vlsh = ldp_fd_to_vlsh (fd);
2200 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2203 if (vlsh != VLS_INVALID_HANDLE)
2205 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2206 " event %p", epfd, vep_vlsh, vlsh, event);
2208 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2209 if (rv != VPPCOM_OK)
2218 u32 size = sizeof (epfd);
2220 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2223 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2224 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2226 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2233 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2242 else if (PREDICT_FALSE (libc_epfd < 0))
2249 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2250 " event %p", epfd, libc_epfd, op, fd, event);
2252 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2260 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2261 int timeout, const sigset_t * sigmask)
2263 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2264 double time_to_wait = (double) 0, max_time;
2265 int libc_epfd, rv = 0;
2266 vls_handle_t ep_vlsh;
2268 if ((errno = -ldp_init ()))
2271 if (PREDICT_FALSE (!events || (timeout < -1)))
2277 if (epfd == ldpw->vcl_mq_epfd)
2278 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2280 ep_vlsh = ldp_fd_to_vlsh (epfd);
2281 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2283 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2288 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2289 clib_time_init (&ldpw->clib_time);
2290 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2291 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2293 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2294 if (PREDICT_FALSE (libc_epfd < 0))
2301 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2302 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2303 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2306 if (!ldpw->epoll_wait_vcl)
2308 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2311 ldpw->epoll_wait_vcl = 1;
2322 ldpw->epoll_wait_vcl = 0;
2326 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2331 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2338 epoll_pwait (int epfd, struct epoll_event *events,
2339 int maxevents, int timeout, const sigset_t * sigmask)
2341 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2345 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2347 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2351 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2353 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2354 int rv, i, n_revents = 0;
2359 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
2361 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2362 clib_time_init (&ldpw->clib_time);
2364 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2365 max_time += clib_time_now (&ldpw->clib_time);
2367 for (i = 0; i < nfds; i++)
2372 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2373 if (vlsh != VLS_INVALID_HANDLE)
2375 fds[i].fd = -fds[i].fd;
2376 vec_add2 (ldpw->vcl_poll, vp, 1);
2378 vp->sh = vlsh_to_sh (vlsh);
2379 vp->events = fds[i].events;
2380 #ifdef __USE_XOPEN2K
2381 if (fds[i].events & POLLRDNORM)
2382 vp->events |= POLLIN;
2383 if (fds[i].events & POLLWRNORM)
2384 vp->events |= POLLOUT;
2386 vp->revents = fds[i].revents;
2390 vec_add1 (ldpw->libc_poll, fds[i]);
2391 vec_add1 (ldpw->libc_poll_idxs, i);
2397 if (vec_len (ldpw->vcl_poll))
2399 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2410 if (vec_len (ldpw->libc_poll))
2412 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2425 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2429 vec_foreach (vp, ldpw->vcl_poll)
2431 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2432 fds[vp->fds_ndx].revents = vp->revents;
2433 #ifdef __USE_XOPEN2K
2434 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2435 (fds[vp->fds_ndx].events & POLLRDNORM))
2436 fds[vp->fds_ndx].revents |= POLLRDNORM;
2437 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2438 (fds[vp->fds_ndx].events & POLLWRNORM))
2439 fds[vp->fds_ndx].revents |= POLLWRNORM;
2442 vec_reset_length (ldpw->vcl_poll);
2444 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2446 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2448 vec_reset_length (ldpw->libc_poll_idxs);
2449 vec_reset_length (ldpw->libc_poll);
2456 ppoll (struct pollfd *fds, nfds_t nfds,
2457 const struct timespec *timeout, const sigset_t * sigmask)
2459 if ((errno = -ldp_init ()))
2462 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2470 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2472 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2475 * This function is called when the library is loaded
2478 ldp_constructor (void)
2480 swrap_constructor ();
2481 if (ldp_init () != 0)
2483 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2487 else if (LDP_DEBUG > 0)
2488 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2492 * This function is called when the library is unloaded
2495 ldp_destructor (void)
2498 swrap_destructor ();
2503 /* Don't use clib_warning() here because that calls writev()
2504 * which will call ldp_init().
2507 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2508 __func__, __LINE__, getpid ());
2513 * fd.io coding-style-patch-verification: ON
2516 * eval: (c-set-style "gnu")