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);
481 fcntl (int fd, int cmd, ...)
487 if ((errno = -ldp_init ()))
492 vlsh = ldp_fd_to_vlsh (fd);
493 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
494 if (vlsh != VLS_INVALID_HANDLE)
496 int flags = va_arg (ap, int);
499 size = sizeof (flags);
504 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
508 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
513 /* TODO handle this */
514 LDBG (0, "F_SETFD ignored flags %u", flags);
530 rv = libc_vfcntl64 (fd, cmd, ap);
532 rv = libc_vfcntl (fd, cmd, ap);
542 fcntl64 (int fd, int cmd, ...)
548 rv = fcntl (fd, cmd, ap);
554 ioctl (int fd, unsigned long int cmd, ...)
560 if ((errno = -ldp_init ()))
565 vlsh = ldp_fd_to_vlsh (fd);
566 if (vlsh != VLS_INVALID_HANDLE)
571 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
576 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
577 u32 size = sizeof (flags);
579 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
580 * non-blocking, the flags should be read here and merged
583 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
599 rv = libc_vioctl (fd, cmd, ap);
607 ldp_select_init_maps (fd_set * __restrict original,
608 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
609 clib_bitmap_t ** vclb, int nfds, u32 minbits,
610 u32 n_bytes, uword * si_bits, uword * libc_bits)
612 uword si_bits_set, libc_bits_set;
616 clib_bitmap_validate (*vclb, minbits);
617 clib_bitmap_validate (*libcb, minbits);
618 clib_bitmap_validate (*resultb, minbits);
619 clib_memcpy_fast (*resultb, original, n_bytes);
620 memset (original, 0, n_bytes);
623 clib_bitmap_foreach (fd, *resultb, ({
626 vlsh = ldp_fd_to_vlsh (fd);
627 if (vlsh == VLS_INVALID_HANDLE)
628 clib_bitmap_set_no_check (*libcb, fd, 1);
630 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
634 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
635 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
636 clib_bitmap_validate (*resultb, *si_bits);
638 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
639 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
643 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
653 clib_bitmap_foreach (si, vclb, ({
654 vlsh = vls_session_index_to_vlsh (si);
655 fd = ldp_vlsh_to_fd (vlsh);
656 if (PREDICT_FALSE (fd < 0))
669 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
677 clib_bitmap_foreach (fd, result, ({
678 FD_SET ((int)fd, libcb);
684 ldp_pselect (int nfds, fd_set * __restrict readfds,
685 fd_set * __restrict writefds,
686 fd_set * __restrict exceptfds,
687 const struct timespec *__restrict timeout,
688 const __sigset_t * __restrict sigmask)
690 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
691 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
692 struct timespec libc_tspec = { 0 };
693 f64 time_out, vcl_timeout = 0;
694 uword si_bits, libc_bits;
695 int rv, bits_set = 0;
703 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
704 clib_time_init (&ldpw->clib_time);
708 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
709 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
711 /* select as fine grained sleep */
714 time_out += clib_time_now (&ldpw->clib_time);
715 while (clib_time_now (&ldpw->clib_time) < time_out)
728 if (nfds <= ldp->vlsh_bit_val)
730 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
735 si_bits = libc_bits = 0;
736 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
739 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
740 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
741 &si_bits, &libc_bits);
743 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
744 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
745 minbits, n_bytes, &si_bits, &libc_bits);
747 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
748 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
749 minbits, n_bytes, &si_bits, &libc_bits);
751 if (PREDICT_FALSE (!si_bits && !libc_bits))
759 libc_tspec = timeout ? *timeout : libc_tspec;
766 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
767 vec_len (ldpw->si_rd_bitmap) *
768 sizeof (clib_bitmap_t));
770 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
771 vec_len (ldpw->si_wr_bitmap) *
772 sizeof (clib_bitmap_t));
774 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
775 vec_len (ldpw->si_ex_bitmap) *
776 sizeof (clib_bitmap_t));
778 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
779 writefds ? ldpw->wr_bitmap : NULL,
780 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
789 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
795 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
801 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
812 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
813 vec_len (ldpw->libc_rd_bitmap) *
814 sizeof (clib_bitmap_t));
816 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
817 vec_len (ldpw->libc_wr_bitmap) *
818 sizeof (clib_bitmap_t));
820 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
821 vec_len (ldpw->libc_ex_bitmap) *
822 sizeof (clib_bitmap_t));
824 rv = libc_pselect (libc_bits,
825 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
826 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
827 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
828 &libc_tspec, sigmask);
831 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
832 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
833 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
844 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
848 /* TBD: set timeout to amount of time left */
849 clib_bitmap_zero (ldpw->rd_bitmap);
850 clib_bitmap_zero (ldpw->si_rd_bitmap);
851 clib_bitmap_zero (ldpw->libc_rd_bitmap);
852 clib_bitmap_zero (ldpw->wr_bitmap);
853 clib_bitmap_zero (ldpw->si_wr_bitmap);
854 clib_bitmap_zero (ldpw->libc_wr_bitmap);
855 clib_bitmap_zero (ldpw->ex_bitmap);
856 clib_bitmap_zero (ldpw->si_ex_bitmap);
857 clib_bitmap_zero (ldpw->libc_ex_bitmap);
863 select (int nfds, fd_set * __restrict readfds,
864 fd_set * __restrict writefds,
865 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
867 struct timespec tspec;
871 tspec.tv_sec = timeout->tv_sec;
872 tspec.tv_nsec = timeout->tv_usec * 1000;
874 return ldp_pselect (nfds, readfds, writefds, exceptfds,
875 timeout ? &tspec : NULL, NULL);
880 pselect (int nfds, fd_set * __restrict readfds,
881 fd_set * __restrict writefds,
882 fd_set * __restrict exceptfds,
883 const struct timespec *__restrict timeout,
884 const __sigset_t * __restrict sigmask)
886 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
890 /* If transparent TLS mode is turned on, then ldp will load key and cert.
893 load_tls_cert (vls_handle_t vlsh)
895 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
903 fp = fopen (env_var_str, "r");
906 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
909 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
911 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
917 LDBG (0, "ERROR: failed to read LDP environment %s\n",
925 load_tls_key (vls_handle_t vlsh)
927 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
935 fp = fopen (env_var_str, "r");
938 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
941 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
943 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
949 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
956 socket (int domain, int type, int protocol)
958 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
959 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
962 if ((errno = -ldp_init ()))
965 if (((domain == AF_INET) || (domain == AF_INET6)) &&
966 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
969 if (ldp->transparent_tls)
971 proto = VPPCOM_PROTO_TLS;
974 proto = ((sock_type == SOCK_DGRAM) ?
975 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
977 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
978 proto, vppcom_proto_str (proto), is_nonblocking);
980 vlsh = vls_create (proto, is_nonblocking);
988 if (ldp->transparent_tls)
990 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
995 rv = ldp_vlsh_to_fd (vlsh);
1000 LDBG (0, "calling libc_socket");
1001 rv = libc_socket (domain, type, protocol);
1008 * Create two new sockets, of type TYPE in domain DOMAIN and using
1009 * protocol PROTOCOL, which are connected to each other, and put file
1010 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1011 * one will be chosen automatically.
1012 * Returns 0 on success, -1 for errors.
1015 socketpair (int domain, int type, int protocol, int fds[2])
1017 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1019 if ((errno = -ldp_init ()))
1022 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1023 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1025 LDBG (0, "LDP-TBD");
1031 LDBG (1, "calling libc_socketpair");
1032 rv = libc_socketpair (domain, type, protocol, fds);
1039 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1044 if ((errno = -ldp_init ()))
1047 vlsh = ldp_fd_to_vlsh (fd);
1048 if (vlsh != VLS_INVALID_HANDLE)
1052 switch (addr->sa_family)
1055 if (len != sizeof (struct sockaddr_in))
1057 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1063 ep.is_ip4 = VPPCOM_IS_IP4;
1064 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1065 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1069 if (len != sizeof (struct sockaddr_in6))
1071 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1077 ep.is_ip4 = VPPCOM_IS_IP6;
1078 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1079 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1083 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1084 fd, vlsh, addr->sa_family);
1085 errno = EAFNOSUPPORT;
1089 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1092 rv = vls_bind (vlsh, &ep);
1093 if (rv != VPPCOM_OK)
1101 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1102 rv = libc_bind (fd, addr, len);
1106 LDBG (1, "fd %d: returning %d", fd, rv);
1112 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1113 vppcom_endpt_t * ep)
1116 int sa_len, copy_len;
1118 if ((errno = -ldp_init ()))
1121 if (addr && len && ep)
1123 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1124 switch (addr->sa_family)
1127 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1128 if (*len > sizeof (struct sockaddr_in))
1129 *len = sizeof (struct sockaddr_in);
1130 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1131 copy_len = *len - sa_len;
1133 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1138 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1139 if (*len > sizeof (struct sockaddr_in6))
1140 *len = sizeof (struct sockaddr_in6);
1141 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1142 copy_len = *len - sa_len;
1144 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1145 __in6_u.__u6_addr8, ep->ip, copy_len);
1158 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1163 if ((errno = -ldp_init ()))
1166 vlsh = ldp_fd_to_vlsh (fd);
1167 if (vlsh != VLS_INVALID_HANDLE)
1170 u8 addr_buf[sizeof (struct in6_addr)];
1171 u32 size = sizeof (ep);
1175 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1176 if (rv != VPPCOM_OK)
1183 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1184 if (rv != VPPCOM_OK)
1193 rv = libc_getsockname (fd, addr, len);
1200 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1205 if ((errno = -ldp_init ()))
1210 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1216 vlsh = ldp_fd_to_vlsh (fd);
1217 if (vlsh != VLS_INVALID_HANDLE)
1221 switch (addr->sa_family)
1224 if (len != sizeof (struct sockaddr_in))
1226 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1232 ep.is_ip4 = VPPCOM_IS_IP4;
1233 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1234 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1238 if (len != sizeof (struct sockaddr_in6))
1240 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1246 ep.is_ip4 = VPPCOM_IS_IP6;
1247 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1248 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1252 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1253 fd, vlsh, addr->sa_family);
1254 errno = EAFNOSUPPORT;
1258 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1261 rv = vls_connect (vlsh, &ep);
1262 if (rv != VPPCOM_OK)
1270 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1273 rv = libc_connect (fd, addr, len);
1277 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1282 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1287 if ((errno = -ldp_init ()))
1290 vlsh = ldp_fd_to_vlsh (fd);
1291 if (vlsh != VLS_INVALID_HANDLE)
1294 u8 addr_buf[sizeof (struct in6_addr)];
1295 u32 size = sizeof (ep);
1298 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1299 if (rv != VPPCOM_OK)
1306 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1307 if (rv != VPPCOM_OK)
1316 rv = libc_getpeername (fd, addr, len);
1323 send (int fd, const void *buf, size_t n, int flags)
1325 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1328 if ((errno = -ldp_init ()))
1331 if (vlsh != VLS_INVALID_HANDLE)
1333 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1334 if (size < VPPCOM_OK)
1342 size = libc_send (fd, buf, n, flags);
1349 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1351 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1355 if ((errno = -ldp_init ()))
1358 vlsh = ldp_fd_to_vlsh (out_fd);
1359 if (vlsh != VLS_INVALID_HANDLE)
1362 ssize_t results = 0;
1363 size_t n_bytes_left = len;
1364 size_t bytes_to_read;
1367 u32 flags, flags_len = sizeof (flags);
1369 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1370 if (PREDICT_FALSE (rv != VPPCOM_OK))
1372 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1373 out_fd, vlsh, rv, vppcom_retval_str (rv));
1375 vec_reset_length (ldpw->io_buffer);
1383 off_t off = lseek (in_fd, *offset, SEEK_SET);
1384 if (PREDICT_FALSE (off == -1))
1390 ASSERT (off == *offset);
1395 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1398 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1399 out_fd, vlsh, size, vppcom_retval_str (size));
1400 vec_reset_length (ldpw->io_buffer);
1406 bytes_to_read = size;
1407 if (bytes_to_read == 0)
1409 if (flags & O_NONBLOCK)
1418 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1419 vec_validate (ldpw->io_buffer, bytes_to_read);
1420 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1425 vec_reset_length (ldpw->io_buffer);
1432 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1435 if (size == VPPCOM_EAGAIN)
1437 if (flags & O_NONBLOCK)
1448 vec_reset_length (ldpw->io_buffer);
1457 ASSERT (n_bytes_left >= nbytes);
1458 n_bytes_left = n_bytes_left - nbytes;
1460 while (n_bytes_left > 0);
1463 vec_reset_length (ldpw->io_buffer);
1466 off_t off = lseek (in_fd, *offset, SEEK_SET);
1467 if (PREDICT_FALSE (off == -1))
1473 ASSERT (off == *offset);
1474 *offset += results + 1;
1486 size = libc_sendfile (out_fd, in_fd, offset, len);
1494 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1496 return sendfile (out_fd, in_fd, offset, len);
1500 recv (int fd, void *buf, size_t n, int flags)
1505 if ((errno = -ldp_init ()))
1508 vlsh = ldp_fd_to_vlsh (fd);
1509 if (vlsh != VLS_INVALID_HANDLE)
1511 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1520 size = libc_recv (fd, buf, n, flags);
1527 sendto (int fd, const void *buf, size_t n, int flags,
1528 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1533 if ((errno = -ldp_init ()))
1536 vlsh = ldp_fd_to_vlsh (fd);
1537 if (vlsh != INVALID_SESSION_ID)
1539 vppcom_endpt_t *ep = 0;
1545 switch (addr->sa_family)
1548 ep->is_ip4 = VPPCOM_IS_IP4;
1550 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1552 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1556 ep->is_ip4 = VPPCOM_IS_IP6;
1558 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1560 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1564 errno = EAFNOSUPPORT;
1570 size = vls_sendto (vlsh, (void *) buf, n, flags, ep);
1579 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1587 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1588 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1593 if ((errno = -ldp_init ()))
1596 sid = ldp_fd_to_vlsh (fd);
1597 if (sid != VLS_INVALID_HANDLE)
1600 u8 src_addr[sizeof (struct sockaddr_in6)];
1605 size = vls_recvfrom (sid, buf, n, flags, &ep);
1609 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1615 size = vls_recvfrom (sid, buf, n, flags, NULL);
1625 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1632 sendmsg (int fd, const struct msghdr * message, int flags)
1637 if ((errno = -ldp_init ()))
1640 vlsh = ldp_fd_to_vlsh (fd);
1641 if (vlsh != VLS_INVALID_HANDLE)
1643 LDBG (0, "LDP-TBD");
1649 size = libc_sendmsg (fd, message, flags);
1657 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1660 const char *func_str;
1661 u32 sh = ldp_fd_to_vlsh (fd);
1663 if ((errno = -ldp_init ()))
1666 if (sh != INVALID_SESSION_ID)
1668 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1674 func_str = "libc_sendmmsg";
1677 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1678 "vmessages %p, vlen %u, flags 0x%x",
1679 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1681 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1688 int errno_val = errno;
1690 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1691 "rv %d, errno = %d", getpid (), fd, fd,
1692 func_str, size, errno_val);
1696 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1697 getpid (), fd, fd, size, size);
1704 recvmsg (int fd, struct msghdr * message, int flags)
1709 if ((errno = -ldp_init ()))
1712 vlsh = ldp_fd_to_vlsh (fd);
1713 if (vlsh != VLS_INVALID_HANDLE)
1715 LDBG (0, "LDP-TBD");
1721 size = libc_recvmsg (fd, message, flags);
1729 recvmmsg (int fd, struct mmsghdr *vmessages,
1730 unsigned int vlen, int flags, struct timespec *tmo)
1733 const char *func_str;
1734 u32 sh = ldp_fd_to_vlsh (fd);
1736 if ((errno = -ldp_init ()))
1739 if (sh != INVALID_SESSION_ID)
1741 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1747 func_str = "libc_recvmmsg";
1750 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1751 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1752 getpid (), fd, fd, func_str, vmessages, vlen,
1755 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1762 int errno_val = errno;
1764 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1765 "rv %d, errno = %d", getpid (), fd, fd,
1766 func_str, size, errno_val);
1770 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1771 getpid (), fd, fd, size, size);
1778 getsockopt (int fd, int level, int optname,
1779 void *__restrict optval, socklen_t * __restrict optlen)
1784 if ((errno = -ldp_init ()))
1787 vlsh = ldp_fd_to_vlsh (fd);
1788 if (vlsh != VLS_INVALID_HANDLE)
1798 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1802 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1806 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1810 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1814 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1816 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1817 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1818 memset (optval, 0, *optlen);
1824 case TCP_CONGESTION:
1825 *optlen = strlen ("cubic");
1826 strncpy (optval, "cubic", *optlen + 1);
1830 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1831 "optname %d unsupported!", fd, vlsh, optname);
1839 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1842 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1843 "optname %d unsupported!", fd, vlsh, optname);
1851 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1854 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1857 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1858 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1861 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1865 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1869 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1872 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1875 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1878 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1879 "optname %d unsupported!", fd, vlsh, optname);
1887 if (rv != VPPCOM_OK)
1895 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1902 setsockopt (int fd, int level, int optname,
1903 const void *optval, socklen_t optlen)
1908 if ((errno = -ldp_init ()))
1911 vlsh = ldp_fd_to_vlsh (fd);
1912 if (vlsh != VLS_INVALID_HANDLE)
1922 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1923 (void *) optval, &optlen);
1926 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1927 (void *) optval, &optlen);
1930 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1931 (void *) optval, &optlen);
1934 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
1935 (void *) optval, &optlen);
1937 case TCP_CONGESTION:
1943 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
1944 "optname %d unsupported!", fd, vlsh, optname);
1952 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
1953 (void *) optval, &optlen);
1956 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
1957 "optname %d unsupported!", fd, vlsh, optname);
1965 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
1966 (void *) optval, &optlen);
1969 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
1970 (void *) optval, &optlen);
1973 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
1974 (void *) optval, &optlen);
1977 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
1978 "optname %d unsupported!", fd, vlsh, optname);
1986 if (rv != VPPCOM_OK)
1994 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2001 listen (int fd, int n)
2006 if ((errno = -ldp_init ()))
2009 vlsh = ldp_fd_to_vlsh (fd);
2010 if (vlsh != VLS_INVALID_HANDLE)
2012 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2014 rv = vls_listen (vlsh, n);
2015 if (rv != VPPCOM_OK)
2023 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2024 rv = libc_listen (fd, n);
2027 LDBG (1, "fd %d: returning %d", fd, rv);
2032 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2033 socklen_t * __restrict addr_len, int flags)
2035 vls_handle_t listen_vlsh, accept_vlsh;
2038 if ((errno = -ldp_init ()))
2041 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2042 if (listen_vlsh != VLS_INVALID_HANDLE)
2045 u8 src_addr[sizeof (struct sockaddr_in6)];
2046 memset (&ep, 0, sizeof (ep));
2049 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2050 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
2052 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2053 if (accept_vlsh < 0)
2055 errno = -accept_vlsh;
2060 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2061 if (rv != VPPCOM_OK)
2063 (void) vls_close (accept_vlsh);
2069 rv = ldp_vlsh_to_fd (accept_vlsh);
2075 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2076 " flags 0x%x", listen_fd, addr, addr_len, flags);
2078 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2081 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2087 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2090 return ldp_accept4 (fd, addr, addr_len, flags);
2094 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2096 return ldp_accept4 (fd, addr, addr_len, 0);
2100 shutdown (int fd, int how)
2104 u32 flags_len = sizeof (flags);
2106 if ((errno = -ldp_init ()))
2109 vlsh = ldp_fd_to_vlsh (fd);
2110 if (vlsh != VLS_INVALID_HANDLE)
2112 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2114 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
2120 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2126 if (flags == SHUT_RDWR)
2131 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2132 rv = libc_shutdown (fd, how);
2139 epoll_create1 (int flags)
2141 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2145 if ((errno = -ldp_init ()))
2148 if (ldp->vcl_needs_real_epoll)
2150 /* Make sure workers have been allocated */
2153 ldp_alloc_workers ();
2154 ldpw = ldp_worker_get_current ();
2156 rv = libc_epoll_create1 (flags);
2157 ldp->vcl_needs_real_epoll = 0;
2158 ldpw->vcl_mq_epfd = rv;
2159 LDBG (0, "created vcl epfd %u", rv);
2163 vlsh = vls_epoll_create ();
2164 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2171 rv = ldp_vlsh_to_fd (vlsh);
2173 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2178 epoll_create (int size)
2180 return epoll_create1 (0);
2184 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2186 vls_handle_t vep_vlsh, vlsh;
2189 if ((errno = -ldp_init ()))
2192 vep_vlsh = ldp_fd_to_vlsh (epfd);
2193 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2195 /* The LDP epoll_create1 always creates VCL epfd's.
2196 * The app should never have a kernel base epoll fd unless it
2197 * was acquired outside of the LD_PRELOAD process context.
2198 * In any case, if we get one, punt it to libc_epoll_ctl.
2200 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2201 " event %p", epfd, op, fd, event);
2203 rv = libc_epoll_ctl (epfd, op, fd, event);
2207 vlsh = ldp_fd_to_vlsh (fd);
2209 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2212 if (vlsh != VLS_INVALID_HANDLE)
2214 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2215 " event %p", epfd, vep_vlsh, vlsh, event);
2217 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2218 if (rv != VPPCOM_OK)
2227 u32 size = sizeof (epfd);
2229 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2232 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2233 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2235 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2242 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2251 else if (PREDICT_FALSE (libc_epfd < 0))
2258 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2259 " event %p", epfd, libc_epfd, op, fd, event);
2261 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2269 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2270 int timeout, const sigset_t * sigmask)
2272 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2273 double time_to_wait = (double) 0, max_time;
2274 int libc_epfd, rv = 0;
2275 vls_handle_t ep_vlsh;
2277 if ((errno = -ldp_init ()))
2280 if (PREDICT_FALSE (!events || (timeout < -1)))
2286 if (epfd == ldpw->vcl_mq_epfd)
2287 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2289 ep_vlsh = ldp_fd_to_vlsh (epfd);
2290 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2292 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2297 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2298 clib_time_init (&ldpw->clib_time);
2299 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2300 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2302 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2303 if (PREDICT_FALSE (libc_epfd < 0))
2310 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2311 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2312 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2315 if (!ldpw->epoll_wait_vcl)
2317 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2320 ldpw->epoll_wait_vcl = 1;
2331 ldpw->epoll_wait_vcl = 0;
2335 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2340 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2347 epoll_pwait (int epfd, struct epoll_event *events,
2348 int maxevents, int timeout, const sigset_t * sigmask)
2350 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2354 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2356 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2360 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2362 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2363 int rv, i, n_revents = 0;
2368 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
2370 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2371 clib_time_init (&ldpw->clib_time);
2373 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2374 max_time += clib_time_now (&ldpw->clib_time);
2376 for (i = 0; i < nfds; i++)
2381 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2382 if (vlsh != VLS_INVALID_HANDLE)
2384 fds[i].fd = -fds[i].fd;
2385 vec_add2 (ldpw->vcl_poll, vp, 1);
2387 vp->sh = vlsh_to_sh (vlsh);
2388 vp->events = fds[i].events;
2389 #ifdef __USE_XOPEN2K
2390 if (fds[i].events & POLLRDNORM)
2391 vp->events |= POLLIN;
2392 if (fds[i].events & POLLWRNORM)
2393 vp->events |= POLLOUT;
2395 vp->revents = fds[i].revents;
2399 vec_add1 (ldpw->libc_poll, fds[i]);
2400 vec_add1 (ldpw->libc_poll_idxs, i);
2406 if (vec_len (ldpw->vcl_poll))
2408 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2419 if (vec_len (ldpw->libc_poll))
2421 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2434 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2438 vec_foreach (vp, ldpw->vcl_poll)
2440 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2441 fds[vp->fds_ndx].revents = vp->revents;
2442 #ifdef __USE_XOPEN2K
2443 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2444 (fds[vp->fds_ndx].events & POLLRDNORM))
2445 fds[vp->fds_ndx].revents |= POLLRDNORM;
2446 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2447 (fds[vp->fds_ndx].events & POLLWRNORM))
2448 fds[vp->fds_ndx].revents |= POLLWRNORM;
2451 vec_reset_length (ldpw->vcl_poll);
2453 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2455 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2457 vec_reset_length (ldpw->libc_poll_idxs);
2458 vec_reset_length (ldpw->libc_poll);
2465 ppoll (struct pollfd *fds, nfds_t nfds,
2466 const struct timespec *timeout, const sigset_t * sigmask)
2468 if ((errno = -ldp_init ()))
2471 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2479 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2481 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2484 * This function is called when the library is loaded
2487 ldp_constructor (void)
2489 swrap_constructor ();
2490 if (ldp_init () != 0)
2492 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2496 else if (LDP_DEBUG > 0)
2497 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2501 * This function is called when the library is unloaded
2504 ldp_destructor (void)
2507 swrap_destructor ();
2512 /* Don't use clib_warning() here because that calls writev()
2513 * which will call ldp_init().
2516 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2517 __func__, __LINE__, getpid ());
2522 * fd.io coding-style-patch-verification: ON
2525 * eval: (c-set-style "gnu")