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 *vclb = clib_bitmap_set (*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;
629 clib_bitmap_validate (*resultb, *si_bits);
631 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
632 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
636 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
646 clib_bitmap_foreach (si, vclb, ({
647 vlsh = vls_session_index_to_vlsh (si);
648 ASSERT (vlsh != VLS_INVALID_HANDLE);
649 fd = ldp_vlsh_to_fd (vlsh);
650 if (PREDICT_FALSE (fd < 0))
663 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
671 clib_bitmap_foreach (fd, result, ({
672 FD_SET ((int)fd, libcb);
678 ldp_pselect (int nfds, fd_set * __restrict readfds,
679 fd_set * __restrict writefds,
680 fd_set * __restrict exceptfds,
681 const struct timespec *__restrict timeout,
682 const __sigset_t * __restrict sigmask)
684 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
685 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
686 struct timespec libc_tspec = { 0 };
687 f64 time_out, vcl_timeout = 0;
688 uword si_bits, libc_bits;
689 int rv, bits_set = 0;
697 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
698 clib_time_init (&ldpw->clib_time);
702 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
703 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
705 /* select as fine grained sleep */
708 time_out += clib_time_now (&ldpw->clib_time);
709 while (clib_time_now (&ldpw->clib_time) < time_out)
722 if (nfds <= ldp->vlsh_bit_val)
724 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
729 si_bits = libc_bits = 0;
730 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
733 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
734 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
735 &si_bits, &libc_bits);
737 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
738 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
739 minbits, n_bytes, &si_bits, &libc_bits);
741 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
742 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
743 minbits, n_bytes, &si_bits, &libc_bits);
745 if (PREDICT_FALSE (!si_bits && !libc_bits))
753 libc_tspec = timeout ? *timeout : libc_tspec;
760 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
761 vec_len (ldpw->si_rd_bitmap) *
762 sizeof (clib_bitmap_t));
764 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
765 vec_len (ldpw->si_wr_bitmap) *
766 sizeof (clib_bitmap_t));
768 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
769 vec_len (ldpw->si_ex_bitmap) *
770 sizeof (clib_bitmap_t));
772 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
773 writefds ? ldpw->wr_bitmap : NULL,
774 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
783 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
789 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
795 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
806 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
807 vec_len (ldpw->libc_rd_bitmap) *
808 sizeof (clib_bitmap_t));
810 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
811 vec_len (ldpw->libc_wr_bitmap) *
812 sizeof (clib_bitmap_t));
814 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
815 vec_len (ldpw->libc_ex_bitmap) *
816 sizeof (clib_bitmap_t));
818 rv = libc_pselect (libc_bits,
819 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
820 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
821 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
822 &libc_tspec, sigmask);
825 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
826 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
827 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
838 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
842 /* TBD: set timeout to amount of time left */
843 clib_bitmap_zero (ldpw->rd_bitmap);
844 clib_bitmap_zero (ldpw->si_rd_bitmap);
845 clib_bitmap_zero (ldpw->libc_rd_bitmap);
846 clib_bitmap_zero (ldpw->wr_bitmap);
847 clib_bitmap_zero (ldpw->si_wr_bitmap);
848 clib_bitmap_zero (ldpw->libc_wr_bitmap);
849 clib_bitmap_zero (ldpw->ex_bitmap);
850 clib_bitmap_zero (ldpw->si_ex_bitmap);
851 clib_bitmap_zero (ldpw->libc_ex_bitmap);
857 select (int nfds, fd_set * __restrict readfds,
858 fd_set * __restrict writefds,
859 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
861 struct timespec tspec;
865 tspec.tv_sec = timeout->tv_sec;
866 tspec.tv_nsec = timeout->tv_usec * 1000;
868 return ldp_pselect (nfds, readfds, writefds, exceptfds,
869 timeout ? &tspec : NULL, NULL);
874 pselect (int nfds, fd_set * __restrict readfds,
875 fd_set * __restrict writefds,
876 fd_set * __restrict exceptfds,
877 const struct timespec *__restrict timeout,
878 const __sigset_t * __restrict sigmask)
880 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
884 /* If transparent TLS mode is turned on, then ldp will load key and cert.
887 load_tls_cert (vls_handle_t vlsh)
889 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
897 fp = fopen (env_var_str, "r");
900 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
903 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
905 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
911 LDBG (0, "ERROR: failed to read LDP environment %s\n",
919 load_tls_key (vls_handle_t vlsh)
921 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
929 fp = fopen (env_var_str, "r");
932 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
935 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
937 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
943 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
950 socket (int domain, int type, int protocol)
952 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
953 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
956 if ((errno = -ldp_init ()))
959 if (((domain == AF_INET) || (domain == AF_INET6)) &&
960 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
963 if (ldp->transparent_tls)
965 proto = VPPCOM_PROTO_TLS;
968 proto = ((sock_type == SOCK_DGRAM) ?
969 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
971 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
972 proto, vppcom_proto_str (proto), is_nonblocking);
974 vlsh = vls_create (proto, is_nonblocking);
982 if (ldp->transparent_tls)
984 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
989 rv = ldp_vlsh_to_fd (vlsh);
994 LDBG (0, "calling libc_socket");
995 rv = libc_socket (domain, type, protocol);
1002 * Create two new sockets, of type TYPE in domain DOMAIN and using
1003 * protocol PROTOCOL, which are connected to each other, and put file
1004 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1005 * one will be chosen automatically.
1006 * Returns 0 on success, -1 for errors.
1009 socketpair (int domain, int type, int protocol, int fds[2])
1011 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1013 if ((errno = -ldp_init ()))
1016 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1017 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1019 LDBG (0, "LDP-TBD");
1025 LDBG (1, "calling libc_socketpair");
1026 rv = libc_socketpair (domain, type, protocol, fds);
1033 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1038 if ((errno = -ldp_init ()))
1041 vlsh = ldp_fd_to_vlsh (fd);
1042 if (vlsh != VLS_INVALID_HANDLE)
1046 switch (addr->sa_family)
1049 if (len != sizeof (struct sockaddr_in))
1051 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1057 ep.is_ip4 = VPPCOM_IS_IP4;
1058 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1059 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1063 if (len != sizeof (struct sockaddr_in6))
1065 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1071 ep.is_ip4 = VPPCOM_IS_IP6;
1072 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1073 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1077 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1078 fd, vlsh, addr->sa_family);
1079 errno = EAFNOSUPPORT;
1083 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1086 rv = vls_bind (vlsh, &ep);
1087 if (rv != VPPCOM_OK)
1095 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1096 rv = libc_bind (fd, addr, len);
1100 LDBG (1, "fd %d: returning %d", fd, rv);
1106 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1107 vppcom_endpt_t * ep)
1110 int sa_len, copy_len;
1112 if ((errno = -ldp_init ()))
1115 if (addr && len && ep)
1117 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1118 switch (addr->sa_family)
1121 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1122 if (*len > sizeof (struct sockaddr_in))
1123 *len = sizeof (struct sockaddr_in);
1124 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1125 copy_len = *len - sa_len;
1127 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1132 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1133 if (*len > sizeof (struct sockaddr_in6))
1134 *len = sizeof (struct sockaddr_in6);
1135 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1136 copy_len = *len - sa_len;
1138 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1139 __in6_u.__u6_addr8, ep->ip, copy_len);
1152 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1157 if ((errno = -ldp_init ()))
1160 vlsh = ldp_fd_to_vlsh (fd);
1161 if (vlsh != VLS_INVALID_HANDLE)
1164 u8 addr_buf[sizeof (struct in6_addr)];
1165 u32 size = sizeof (ep);
1169 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1170 if (rv != VPPCOM_OK)
1177 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1178 if (rv != VPPCOM_OK)
1187 rv = libc_getsockname (fd, addr, len);
1194 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1199 if ((errno = -ldp_init ()))
1204 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1210 vlsh = ldp_fd_to_vlsh (fd);
1211 if (vlsh != VLS_INVALID_HANDLE)
1215 switch (addr->sa_family)
1218 if (len != sizeof (struct sockaddr_in))
1220 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1226 ep.is_ip4 = VPPCOM_IS_IP4;
1227 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1228 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1232 if (len != sizeof (struct sockaddr_in6))
1234 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1240 ep.is_ip4 = VPPCOM_IS_IP6;
1241 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1242 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1246 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1247 fd, vlsh, addr->sa_family);
1248 errno = EAFNOSUPPORT;
1252 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1255 rv = vls_connect (vlsh, &ep);
1256 if (rv != VPPCOM_OK)
1264 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1267 rv = libc_connect (fd, addr, len);
1271 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1276 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1281 if ((errno = -ldp_init ()))
1284 vlsh = ldp_fd_to_vlsh (fd);
1285 if (vlsh != VLS_INVALID_HANDLE)
1288 u8 addr_buf[sizeof (struct in6_addr)];
1289 u32 size = sizeof (ep);
1292 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1293 if (rv != VPPCOM_OK)
1300 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1301 if (rv != VPPCOM_OK)
1310 rv = libc_getpeername (fd, addr, len);
1317 send (int fd, const void *buf, size_t n, int flags)
1319 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1322 if ((errno = -ldp_init ()))
1325 if (vlsh != VLS_INVALID_HANDLE)
1327 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1328 if (size < VPPCOM_OK)
1336 size = libc_send (fd, buf, n, flags);
1343 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1345 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1349 if ((errno = -ldp_init ()))
1352 vlsh = ldp_fd_to_vlsh (out_fd);
1353 if (vlsh != VLS_INVALID_HANDLE)
1356 ssize_t results = 0;
1357 size_t n_bytes_left = len;
1358 size_t bytes_to_read;
1361 u32 flags, flags_len = sizeof (flags);
1363 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1364 if (PREDICT_FALSE (rv != VPPCOM_OK))
1366 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1367 out_fd, vlsh, rv, vppcom_retval_str (rv));
1369 vec_reset_length (ldpw->io_buffer);
1377 off_t off = lseek (in_fd, *offset, SEEK_SET);
1378 if (PREDICT_FALSE (off == -1))
1384 ASSERT (off == *offset);
1389 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1392 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1393 out_fd, vlsh, size, vppcom_retval_str (size));
1394 vec_reset_length (ldpw->io_buffer);
1400 bytes_to_read = size;
1401 if (bytes_to_read == 0)
1403 if (flags & O_NONBLOCK)
1412 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1413 vec_validate (ldpw->io_buffer, bytes_to_read);
1414 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1419 vec_reset_length (ldpw->io_buffer);
1426 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1429 if (size == VPPCOM_EAGAIN)
1431 if (flags & O_NONBLOCK)
1442 vec_reset_length (ldpw->io_buffer);
1451 ASSERT (n_bytes_left >= nbytes);
1452 n_bytes_left = n_bytes_left - nbytes;
1454 while (n_bytes_left > 0);
1457 vec_reset_length (ldpw->io_buffer);
1460 off_t off = lseek (in_fd, *offset, SEEK_SET);
1461 if (PREDICT_FALSE (off == -1))
1467 ASSERT (off == *offset);
1468 *offset += results + 1;
1480 size = libc_sendfile (out_fd, in_fd, offset, len);
1488 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1490 return sendfile (out_fd, in_fd, offset, len);
1494 recv (int fd, void *buf, size_t n, int flags)
1499 if ((errno = -ldp_init ()))
1502 vlsh = ldp_fd_to_vlsh (fd);
1503 if (vlsh != VLS_INVALID_HANDLE)
1505 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1514 size = libc_recv (fd, buf, n, flags);
1521 sendto (int fd, const void *buf, size_t n, int flags,
1522 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1527 if ((errno = -ldp_init ()))
1530 vlsh = ldp_fd_to_vlsh (fd);
1531 if (vlsh != INVALID_SESSION_ID)
1533 vppcom_endpt_t *ep = 0;
1539 switch (addr->sa_family)
1542 ep->is_ip4 = VPPCOM_IS_IP4;
1544 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1546 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1550 ep->is_ip4 = VPPCOM_IS_IP6;
1552 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1554 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1558 errno = EAFNOSUPPORT;
1564 size = vls_sendto (vlsh, (void *) buf, n, flags, ep);
1573 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1581 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1582 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1587 if ((errno = -ldp_init ()))
1590 sid = ldp_fd_to_vlsh (fd);
1591 if (sid != VLS_INVALID_HANDLE)
1594 u8 src_addr[sizeof (struct sockaddr_in6)];
1599 size = vls_recvfrom (sid, buf, n, flags, &ep);
1603 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1609 size = vls_recvfrom (sid, buf, n, flags, NULL);
1619 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1626 sendmsg (int fd, const struct msghdr * message, int flags)
1631 if ((errno = -ldp_init ()))
1634 vlsh = ldp_fd_to_vlsh (fd);
1635 if (vlsh != VLS_INVALID_HANDLE)
1637 LDBG (0, "LDP-TBD");
1643 size = libc_sendmsg (fd, message, flags);
1651 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1654 const char *func_str;
1655 u32 sh = ldp_fd_to_vlsh (fd);
1657 if ((errno = -ldp_init ()))
1660 if (sh != INVALID_SESSION_ID)
1662 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1668 func_str = "libc_sendmmsg";
1671 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1672 "vmessages %p, vlen %u, flags 0x%x",
1673 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1675 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1682 int errno_val = errno;
1684 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1685 "rv %d, errno = %d", getpid (), fd, fd,
1686 func_str, size, errno_val);
1690 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1691 getpid (), fd, fd, size, size);
1698 recvmsg (int fd, struct msghdr * message, int flags)
1703 if ((errno = -ldp_init ()))
1706 vlsh = ldp_fd_to_vlsh (fd);
1707 if (vlsh != VLS_INVALID_HANDLE)
1709 LDBG (0, "LDP-TBD");
1715 size = libc_recvmsg (fd, message, flags);
1723 recvmmsg (int fd, struct mmsghdr *vmessages,
1724 unsigned int vlen, int flags, struct timespec *tmo)
1727 const char *func_str;
1728 u32 sh = ldp_fd_to_vlsh (fd);
1730 if ((errno = -ldp_init ()))
1733 if (sh != INVALID_SESSION_ID)
1735 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1741 func_str = "libc_recvmmsg";
1744 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1745 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1746 getpid (), fd, fd, func_str, vmessages, vlen,
1749 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1756 int errno_val = errno;
1758 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1759 "rv %d, errno = %d", getpid (), fd, fd,
1760 func_str, size, errno_val);
1764 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1765 getpid (), fd, fd, size, size);
1772 getsockopt (int fd, int level, int optname,
1773 void *__restrict optval, socklen_t * __restrict optlen)
1778 if ((errno = -ldp_init ()))
1781 vlsh = ldp_fd_to_vlsh (fd);
1782 if (vlsh != VLS_INVALID_HANDLE)
1792 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1796 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1800 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1804 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1808 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1810 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1811 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1812 memset (optval, 0, *optlen);
1818 case TCP_CONGESTION:
1819 *optlen = strlen ("cubic");
1820 strncpy (optval, "cubic", *optlen + 1);
1824 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1825 "optname %d unsupported!", fd, vlsh, optname);
1833 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1836 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1837 "optname %d unsupported!", fd, vlsh, optname);
1845 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1848 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1851 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1852 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1855 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1859 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1863 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1866 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1869 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1872 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1873 "optname %d unsupported!", fd, vlsh, optname);
1881 if (rv != VPPCOM_OK)
1889 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1896 setsockopt (int fd, int level, int optname,
1897 const void *optval, socklen_t optlen)
1902 if ((errno = -ldp_init ()))
1905 vlsh = ldp_fd_to_vlsh (fd);
1906 if (vlsh != VLS_INVALID_HANDLE)
1916 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1917 (void *) optval, &optlen);
1920 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1921 (void *) optval, &optlen);
1924 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1925 (void *) optval, &optlen);
1928 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
1929 (void *) optval, &optlen);
1931 case TCP_CONGESTION:
1937 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
1938 "optname %d unsupported!", fd, vlsh, optname);
1946 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
1947 (void *) optval, &optlen);
1950 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
1951 "optname %d unsupported!", fd, vlsh, optname);
1959 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
1960 (void *) optval, &optlen);
1963 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
1964 (void *) optval, &optlen);
1967 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
1968 (void *) optval, &optlen);
1971 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
1972 "optname %d unsupported!", fd, vlsh, optname);
1980 if (rv != VPPCOM_OK)
1988 rv = libc_setsockopt (fd, level, optname, optval, optlen);
1995 listen (int fd, int n)
2000 if ((errno = -ldp_init ()))
2003 vlsh = ldp_fd_to_vlsh (fd);
2004 if (vlsh != VLS_INVALID_HANDLE)
2006 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2008 rv = vls_listen (vlsh, n);
2009 if (rv != VPPCOM_OK)
2017 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2018 rv = libc_listen (fd, n);
2021 LDBG (1, "fd %d: returning %d", fd, rv);
2026 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2027 socklen_t * __restrict addr_len, int flags)
2029 vls_handle_t listen_vlsh, accept_vlsh;
2032 if ((errno = -ldp_init ()))
2035 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2036 if (listen_vlsh != VLS_INVALID_HANDLE)
2039 u8 src_addr[sizeof (struct sockaddr_in6)];
2040 memset (&ep, 0, sizeof (ep));
2043 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2044 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
2046 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2047 if (accept_vlsh < 0)
2049 errno = -accept_vlsh;
2054 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2055 if (rv != VPPCOM_OK)
2057 (void) vls_close (accept_vlsh);
2063 rv = ldp_vlsh_to_fd (accept_vlsh);
2069 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2070 " flags 0x%x", listen_fd, addr, addr_len, flags);
2072 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2075 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2081 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2084 return ldp_accept4 (fd, addr, addr_len, flags);
2088 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2090 return ldp_accept4 (fd, addr, addr_len, 0);
2094 shutdown (int fd, int how)
2098 u32 flags_len = sizeof (flags);
2100 if ((errno = -ldp_init ()))
2103 vlsh = ldp_fd_to_vlsh (fd);
2104 if (vlsh != VLS_INVALID_HANDLE)
2106 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2108 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
2114 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2120 if (flags == SHUT_RDWR)
2125 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2126 rv = libc_shutdown (fd, how);
2133 epoll_create1 (int flags)
2135 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2139 if ((errno = -ldp_init ()))
2142 if (ldp->vcl_needs_real_epoll)
2144 /* Make sure workers have been allocated */
2147 ldp_alloc_workers ();
2148 ldpw = ldp_worker_get_current ();
2150 rv = libc_epoll_create1 (flags);
2151 ldp->vcl_needs_real_epoll = 0;
2152 ldpw->vcl_mq_epfd = rv;
2153 LDBG (0, "created vcl epfd %u", rv);
2157 vlsh = vls_epoll_create ();
2158 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2165 rv = ldp_vlsh_to_fd (vlsh);
2167 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2172 epoll_create (int size)
2174 return epoll_create1 (0);
2178 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2180 vls_handle_t vep_vlsh, vlsh;
2183 if ((errno = -ldp_init ()))
2186 vep_vlsh = ldp_fd_to_vlsh (epfd);
2187 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2189 /* The LDP epoll_create1 always creates VCL epfd's.
2190 * The app should never have a kernel base epoll fd unless it
2191 * was acquired outside of the LD_PRELOAD process context.
2192 * In any case, if we get one, punt it to libc_epoll_ctl.
2194 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2195 " event %p", epfd, op, fd, event);
2197 rv = libc_epoll_ctl (epfd, op, fd, event);
2201 vlsh = ldp_fd_to_vlsh (fd);
2203 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2206 if (vlsh != VLS_INVALID_HANDLE)
2208 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2209 " event %p", epfd, vep_vlsh, vlsh, event);
2211 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2212 if (rv != VPPCOM_OK)
2221 u32 size = sizeof (epfd);
2223 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2226 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2227 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2229 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2236 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2245 else if (PREDICT_FALSE (libc_epfd < 0))
2252 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2253 " event %p", epfd, libc_epfd, op, fd, event);
2255 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2263 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2264 int timeout, const sigset_t * sigmask)
2266 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2267 double time_to_wait = (double) 0, max_time;
2268 int libc_epfd, rv = 0;
2269 vls_handle_t ep_vlsh;
2271 if ((errno = -ldp_init ()))
2274 if (PREDICT_FALSE (!events || (timeout < -1)))
2280 if (epfd == ldpw->vcl_mq_epfd)
2281 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2283 ep_vlsh = ldp_fd_to_vlsh (epfd);
2284 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2286 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2291 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2292 clib_time_init (&ldpw->clib_time);
2293 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2294 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2296 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2297 if (PREDICT_FALSE (libc_epfd < 0))
2304 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2305 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2306 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2309 if (!ldpw->epoll_wait_vcl)
2311 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2314 ldpw->epoll_wait_vcl = 1;
2325 ldpw->epoll_wait_vcl = 0;
2329 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2334 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2341 epoll_pwait (int epfd, struct epoll_event *events,
2342 int maxevents, int timeout, const sigset_t * sigmask)
2344 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2348 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2350 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2354 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2356 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2357 int rv, i, n_revents = 0;
2362 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
2364 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2365 clib_time_init (&ldpw->clib_time);
2367 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2368 max_time += clib_time_now (&ldpw->clib_time);
2370 for (i = 0; i < nfds; i++)
2375 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2376 if (vlsh != VLS_INVALID_HANDLE)
2378 fds[i].fd = -fds[i].fd;
2379 vec_add2 (ldpw->vcl_poll, vp, 1);
2381 vp->sh = vlsh_to_sh (vlsh);
2382 vp->events = fds[i].events;
2383 #ifdef __USE_XOPEN2K
2384 if (fds[i].events & POLLRDNORM)
2385 vp->events |= POLLIN;
2386 if (fds[i].events & POLLWRNORM)
2387 vp->events |= POLLOUT;
2389 vp->revents = fds[i].revents;
2393 vec_add1 (ldpw->libc_poll, fds[i]);
2394 vec_add1 (ldpw->libc_poll_idxs, i);
2400 if (vec_len (ldpw->vcl_poll))
2402 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2413 if (vec_len (ldpw->libc_poll))
2415 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2428 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2432 vec_foreach (vp, ldpw->vcl_poll)
2434 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2435 fds[vp->fds_ndx].revents = vp->revents;
2436 #ifdef __USE_XOPEN2K
2437 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2438 (fds[vp->fds_ndx].events & POLLRDNORM))
2439 fds[vp->fds_ndx].revents |= POLLRDNORM;
2440 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2441 (fds[vp->fds_ndx].events & POLLWRNORM))
2442 fds[vp->fds_ndx].revents |= POLLWRNORM;
2445 vec_reset_length (ldpw->vcl_poll);
2447 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2449 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2451 vec_reset_length (ldpw->libc_poll_idxs);
2452 vec_reset_length (ldpw->libc_poll);
2459 ppoll (struct pollfd *fds, nfds_t nfds,
2460 const struct timespec *timeout, const sigset_t * sigmask)
2462 if ((errno = -ldp_init ()))
2465 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2473 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2475 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2478 * This function is called when the library is loaded
2481 ldp_constructor (void)
2483 swrap_constructor ();
2484 if (ldp_init () != 0)
2486 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2490 else if (LDP_DEBUG > 0)
2491 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2495 * This function is called when the library is unloaded
2498 ldp_destructor (void)
2501 swrap_destructor ();
2506 /* Don't use clib_warning() here because that calls writev()
2507 * which will call ldp_init().
2510 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2511 __func__, __LINE__, getpid ());
2516 * fd.io coding-style-patch-verification: ON
2519 * eval: (c-set-style "gnu")