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;
88 /* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
89 * they are the same size */
90 STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
91 "ldp bitmap size mismatch");
92 STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
93 "ldp bitmap size mismatch");
97 ldp_worker_ctx_t *workers;
99 char app_name[LDP_APP_NAME_MAX];
105 /** vcl needs next epoll_create to go to libc_epoll */
106 u8 vcl_needs_real_epoll;
109 #define LDP_DEBUG ldp->debug
111 #define LDBG(_lvl, _fmt, _args...) \
112 if (ldp->debug > _lvl) \
114 int errno_saved = errno; \
115 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args); \
116 errno = errno_saved; \
119 static ldp_main_t ldp_main = {
120 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
121 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
122 .debug = LDP_DEBUG_INIT,
123 .transparent_tls = 0,
126 static ldp_main_t *ldp = &ldp_main;
128 static inline ldp_worker_ctx_t *
129 ldp_worker_get_current (void)
131 return (ldp->workers + vppcom_worker_index ());
135 * RETURN: 0 on success or -1 on error.
138 ldp_set_app_name (char *app_name)
140 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
141 "ldp-%d-%s", getpid (), app_name);
147 if (ldp->app_name[0] == '\0')
148 ldp_set_app_name ("app");
150 return ldp->app_name;
154 ldp_vlsh_to_fd (vls_handle_t vlsh)
156 return (vlsh + ldp->vlsh_bit_val);
159 static inline vls_handle_t
160 ldp_fd_to_vlsh (int fd)
162 if (fd < ldp->vlsh_bit_val)
163 return VLS_INVALID_HANDLE;
165 return (fd - ldp->vlsh_bit_val);
169 ldp_alloc_workers (void)
173 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
179 ldp_worker_ctx_t *ldpw;
182 if (PREDICT_TRUE (ldp->init))
186 ldp->vcl_needs_real_epoll = 1;
187 rv = vls_app_create (ldp_get_app_name ());
190 ldp->vcl_needs_real_epoll = 0;
191 if (rv == VPPCOM_EEXIST)
193 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
194 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
198 ldp->vcl_needs_real_epoll = 0;
199 ldp_alloc_workers ();
200 ldpw = ldp_worker_get_current ();
202 char *env_var_str = getenv (LDP_ENV_DEBUG);
206 if (sscanf (env_var_str, "%u", &tmp) != 1)
207 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
208 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
213 LDBG (0, "configured LDP debug level (%u) from env var "
214 LDP_ENV_DEBUG "!", ldp->debug);
218 env_var_str = getenv (LDP_ENV_APP_NAME);
221 ldp_set_app_name (env_var_str);
222 LDBG (0, "configured LDP app name (%s) from the env var "
223 LDP_ENV_APP_NAME "!", ldp->app_name);
226 env_var_str = getenv (LDP_ENV_SID_BIT);
230 if (sscanf (env_var_str, "%u", &sb) != 1)
232 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
233 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
234 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
236 else if (sb < LDP_SID_BIT_MIN)
238 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
239 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
241 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
242 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
243 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
244 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
246 else if (sb > LDP_SID_BIT_MAX)
248 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
249 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
251 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
252 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
253 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
254 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
258 ldp->vlsh_bit_val = (1 << sb);
259 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
261 LDBG (0, "configured LDP sid bit (%u) from "
262 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
263 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
266 /* Make sure there are enough bits in the fd set for vcl sessions */
267 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
269 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
270 ldp->vlsh_bit_val, FD_SETSIZE / 2);
275 env_var_str = getenv (LDP_ENV_TLS_TRANS);
278 ldp->transparent_tls = 1;
282 pool_foreach (ldpw, ldp->workers, ({
283 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
287 LDBG (0, "LDP initialization: done!");
298 if ((errno = -ldp_init ()))
301 vlsh = ldp_fd_to_vlsh (fd);
302 if (vlsh != VLS_INVALID_HANDLE)
304 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
307 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
309 rv = libc_close (epfd);
312 u32 size = sizeof (epfd);
315 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
318 else if (PREDICT_FALSE (epfd < 0))
325 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
327 rv = vls_close (vlsh);
336 LDBG (0, "fd %d: calling libc_close", fd);
337 rv = libc_close (fd);
345 read (int fd, void *buf, size_t nbytes)
350 if ((errno = -ldp_init ()))
353 vlsh = ldp_fd_to_vlsh (fd);
354 if (vlsh != VLS_INVALID_HANDLE)
356 size = vls_read (vlsh, buf, nbytes);
365 size = libc_read (fd, buf, nbytes);
372 readv (int fd, const struct iovec * iov, int iovcnt)
374 int rv = 0, i, total = 0;
378 if ((errno = -ldp_init ()))
381 vlsh = ldp_fd_to_vlsh (fd);
382 if (vlsh != VLS_INVALID_HANDLE)
384 for (i = 0; i < iovcnt; ++i)
386 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
392 if (rv < iov[i].iov_len)
396 if (rv < 0 && total == 0)
406 size = libc_readv (fd, iov, iovcnt);
413 write (int fd, const void *buf, size_t nbytes)
418 if ((errno = -ldp_init ()))
421 vlsh = ldp_fd_to_vlsh (fd);
422 if (vlsh != VLS_INVALID_HANDLE)
424 size = vls_write_msg (vlsh, (void *) buf, nbytes);
433 size = libc_write (fd, buf, nbytes);
440 writev (int fd, const struct iovec * iov, int iovcnt)
442 ssize_t size = 0, total = 0;
446 if ((errno = -ldp_init ()))
449 vlsh = ldp_fd_to_vlsh (fd);
450 if (vlsh != VLS_INVALID_HANDLE)
452 for (i = 0; i < iovcnt; ++i)
454 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
460 if (rv < iov[i].iov_len)
465 if (rv < 0 && total == 0)
475 size = libc_writev (fd, iov, iovcnt);
482 fcntl (int fd, int cmd, ...)
488 if ((errno = -ldp_init ()))
493 vlsh = ldp_fd_to_vlsh (fd);
494 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
495 if (vlsh != VLS_INVALID_HANDLE)
497 int flags = va_arg (ap, int);
500 size = sizeof (flags);
505 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
509 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
514 /* TODO handle this */
515 LDBG (0, "F_SETFD ignored flags %u", flags);
531 rv = libc_vfcntl64 (fd, cmd, ap);
533 rv = libc_vfcntl (fd, cmd, ap);
543 fcntl64 (int fd, int cmd, ...)
549 rv = fcntl (fd, cmd, ap);
555 ioctl (int fd, unsigned long int cmd, ...)
561 if ((errno = -ldp_init ()))
566 vlsh = ldp_fd_to_vlsh (fd);
567 if (vlsh != VLS_INVALID_HANDLE)
572 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
577 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
578 u32 size = sizeof (flags);
580 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
581 * non-blocking, the flags should be read here and merged
584 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
600 rv = libc_vioctl (fd, cmd, ap);
608 ldp_select_init_maps (fd_set * __restrict original,
609 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
610 clib_bitmap_t ** vclb, int nfds, u32 minbits,
611 u32 n_bytes, uword * si_bits, uword * libc_bits)
613 uword si_bits_set, libc_bits_set;
617 clib_bitmap_validate (*vclb, minbits);
618 clib_bitmap_validate (*libcb, minbits);
619 clib_bitmap_validate (*resultb, minbits);
620 clib_memcpy_fast (*resultb, original, n_bytes);
621 memset (original, 0, n_bytes);
624 clib_bitmap_foreach (fd, *resultb, ({
627 vlsh = ldp_fd_to_vlsh (fd);
628 if (vlsh == VLS_INVALID_HANDLE)
629 clib_bitmap_set_no_check (*libcb, fd, 1);
631 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
635 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
636 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
637 clib_bitmap_validate (*resultb, *si_bits);
639 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
640 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
644 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
654 clib_bitmap_foreach (si, vclb, ({
655 vlsh = vls_session_index_to_vlsh (si);
656 ASSERT (vlsh != VLS_INVALID_HANDLE);
657 fd = ldp_vlsh_to_fd (vlsh);
658 if (PREDICT_FALSE (fd < 0))
671 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
679 clib_bitmap_foreach (fd, result, ({
680 FD_SET ((int)fd, libcb);
686 ldp_pselect (int nfds, fd_set * __restrict readfds,
687 fd_set * __restrict writefds,
688 fd_set * __restrict exceptfds,
689 const struct timespec *__restrict timeout,
690 const __sigset_t * __restrict sigmask)
692 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
693 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
694 struct timespec libc_tspec = { 0 };
695 f64 time_out, vcl_timeout = 0;
696 uword si_bits, libc_bits;
697 int rv, bits_set = 0;
705 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
706 clib_time_init (&ldpw->clib_time);
710 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
711 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
713 /* select as fine grained sleep */
716 time_out += clib_time_now (&ldpw->clib_time);
717 while (clib_time_now (&ldpw->clib_time) < time_out)
730 if (nfds <= ldp->vlsh_bit_val)
732 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
737 si_bits = libc_bits = 0;
738 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
741 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
742 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
743 &si_bits, &libc_bits);
745 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
746 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
747 minbits, n_bytes, &si_bits, &libc_bits);
749 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
750 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
751 minbits, n_bytes, &si_bits, &libc_bits);
753 if (PREDICT_FALSE (!si_bits && !libc_bits))
761 libc_tspec = timeout ? *timeout : libc_tspec;
768 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
769 vec_len (ldpw->si_rd_bitmap) *
770 sizeof (clib_bitmap_t));
772 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
773 vec_len (ldpw->si_wr_bitmap) *
774 sizeof (clib_bitmap_t));
776 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
777 vec_len (ldpw->si_ex_bitmap) *
778 sizeof (clib_bitmap_t));
780 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
781 writefds ? ldpw->wr_bitmap : NULL,
782 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
791 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
797 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
803 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
814 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
815 vec_len (ldpw->libc_rd_bitmap) *
816 sizeof (clib_bitmap_t));
818 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
819 vec_len (ldpw->libc_wr_bitmap) *
820 sizeof (clib_bitmap_t));
822 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
823 vec_len (ldpw->libc_ex_bitmap) *
824 sizeof (clib_bitmap_t));
826 rv = libc_pselect (libc_bits,
827 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
828 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
829 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
830 &libc_tspec, sigmask);
833 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
834 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
835 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
846 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
850 /* TBD: set timeout to amount of time left */
851 clib_bitmap_zero (ldpw->rd_bitmap);
852 clib_bitmap_zero (ldpw->si_rd_bitmap);
853 clib_bitmap_zero (ldpw->libc_rd_bitmap);
854 clib_bitmap_zero (ldpw->wr_bitmap);
855 clib_bitmap_zero (ldpw->si_wr_bitmap);
856 clib_bitmap_zero (ldpw->libc_wr_bitmap);
857 clib_bitmap_zero (ldpw->ex_bitmap);
858 clib_bitmap_zero (ldpw->si_ex_bitmap);
859 clib_bitmap_zero (ldpw->libc_ex_bitmap);
865 select (int nfds, fd_set * __restrict readfds,
866 fd_set * __restrict writefds,
867 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
869 struct timespec tspec;
873 tspec.tv_sec = timeout->tv_sec;
874 tspec.tv_nsec = timeout->tv_usec * 1000;
876 return ldp_pselect (nfds, readfds, writefds, exceptfds,
877 timeout ? &tspec : NULL, NULL);
882 pselect (int nfds, fd_set * __restrict readfds,
883 fd_set * __restrict writefds,
884 fd_set * __restrict exceptfds,
885 const struct timespec *__restrict timeout,
886 const __sigset_t * __restrict sigmask)
888 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
892 /* If transparent TLS mode is turned on, then ldp will load key and cert.
895 load_tls_cert (vls_handle_t vlsh)
897 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
905 fp = fopen (env_var_str, "r");
908 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
911 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
913 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
919 LDBG (0, "ERROR: failed to read LDP environment %s\n",
927 load_tls_key (vls_handle_t vlsh)
929 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
937 fp = fopen (env_var_str, "r");
940 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
943 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
945 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
951 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
958 socket (int domain, int type, int protocol)
960 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
961 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
964 if ((errno = -ldp_init ()))
967 if (((domain == AF_INET) || (domain == AF_INET6)) &&
968 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
971 if (ldp->transparent_tls)
973 proto = VPPCOM_PROTO_TLS;
976 proto = ((sock_type == SOCK_DGRAM) ?
977 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
979 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
980 proto, vppcom_proto_str (proto), is_nonblocking);
982 vlsh = vls_create (proto, is_nonblocking);
990 if (ldp->transparent_tls)
992 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
997 rv = ldp_vlsh_to_fd (vlsh);
1002 LDBG (0, "calling libc_socket");
1003 rv = libc_socket (domain, type, protocol);
1010 * Create two new sockets, of type TYPE in domain DOMAIN and using
1011 * protocol PROTOCOL, which are connected to each other, and put file
1012 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1013 * one will be chosen automatically.
1014 * Returns 0 on success, -1 for errors.
1017 socketpair (int domain, int type, int protocol, int fds[2])
1019 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1021 if ((errno = -ldp_init ()))
1024 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1025 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1027 LDBG (0, "LDP-TBD");
1033 LDBG (1, "calling libc_socketpair");
1034 rv = libc_socketpair (domain, type, protocol, fds);
1041 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1046 if ((errno = -ldp_init ()))
1049 vlsh = ldp_fd_to_vlsh (fd);
1050 if (vlsh != VLS_INVALID_HANDLE)
1054 switch (addr->sa_family)
1057 if (len != sizeof (struct sockaddr_in))
1059 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1065 ep.is_ip4 = VPPCOM_IS_IP4;
1066 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1067 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1071 if (len != sizeof (struct sockaddr_in6))
1073 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1079 ep.is_ip4 = VPPCOM_IS_IP6;
1080 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1081 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1085 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1086 fd, vlsh, addr->sa_family);
1087 errno = EAFNOSUPPORT;
1091 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1094 rv = vls_bind (vlsh, &ep);
1095 if (rv != VPPCOM_OK)
1103 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1104 rv = libc_bind (fd, addr, len);
1108 LDBG (1, "fd %d: returning %d", fd, rv);
1114 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1115 vppcom_endpt_t * ep)
1118 int sa_len, copy_len;
1120 if ((errno = -ldp_init ()))
1123 if (addr && len && ep)
1125 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1126 switch (addr->sa_family)
1129 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1130 if (*len > sizeof (struct sockaddr_in))
1131 *len = sizeof (struct sockaddr_in);
1132 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1133 copy_len = *len - sa_len;
1135 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1140 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1141 if (*len > sizeof (struct sockaddr_in6))
1142 *len = sizeof (struct sockaddr_in6);
1143 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1144 copy_len = *len - sa_len;
1146 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1147 __in6_u.__u6_addr8, ep->ip, copy_len);
1160 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1165 if ((errno = -ldp_init ()))
1168 vlsh = ldp_fd_to_vlsh (fd);
1169 if (vlsh != VLS_INVALID_HANDLE)
1172 u8 addr_buf[sizeof (struct in6_addr)];
1173 u32 size = sizeof (ep);
1177 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1178 if (rv != VPPCOM_OK)
1185 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1186 if (rv != VPPCOM_OK)
1195 rv = libc_getsockname (fd, addr, len);
1202 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1207 if ((errno = -ldp_init ()))
1212 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1218 vlsh = ldp_fd_to_vlsh (fd);
1219 if (vlsh != VLS_INVALID_HANDLE)
1223 switch (addr->sa_family)
1226 if (len != sizeof (struct sockaddr_in))
1228 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1234 ep.is_ip4 = VPPCOM_IS_IP4;
1235 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1236 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1240 if (len != sizeof (struct sockaddr_in6))
1242 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1248 ep.is_ip4 = VPPCOM_IS_IP6;
1249 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1250 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1254 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1255 fd, vlsh, addr->sa_family);
1256 errno = EAFNOSUPPORT;
1260 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1263 rv = vls_connect (vlsh, &ep);
1264 if (rv != VPPCOM_OK)
1272 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1275 rv = libc_connect (fd, addr, len);
1279 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1284 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1289 if ((errno = -ldp_init ()))
1292 vlsh = ldp_fd_to_vlsh (fd);
1293 if (vlsh != VLS_INVALID_HANDLE)
1296 u8 addr_buf[sizeof (struct in6_addr)];
1297 u32 size = sizeof (ep);
1300 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1301 if (rv != VPPCOM_OK)
1308 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1309 if (rv != VPPCOM_OK)
1318 rv = libc_getpeername (fd, addr, len);
1325 send (int fd, const void *buf, size_t n, int flags)
1327 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1330 if ((errno = -ldp_init ()))
1333 if (vlsh != VLS_INVALID_HANDLE)
1335 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1336 if (size < VPPCOM_OK)
1344 size = libc_send (fd, buf, n, flags);
1351 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1353 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1357 if ((errno = -ldp_init ()))
1360 vlsh = ldp_fd_to_vlsh (out_fd);
1361 if (vlsh != VLS_INVALID_HANDLE)
1364 ssize_t results = 0;
1365 size_t n_bytes_left = len;
1366 size_t bytes_to_read;
1369 u32 flags, flags_len = sizeof (flags);
1371 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1372 if (PREDICT_FALSE (rv != VPPCOM_OK))
1374 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1375 out_fd, vlsh, rv, vppcom_retval_str (rv));
1377 vec_reset_length (ldpw->io_buffer);
1385 off_t off = lseek (in_fd, *offset, SEEK_SET);
1386 if (PREDICT_FALSE (off == -1))
1392 ASSERT (off == *offset);
1397 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1400 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1401 out_fd, vlsh, size, vppcom_retval_str (size));
1402 vec_reset_length (ldpw->io_buffer);
1408 bytes_to_read = size;
1409 if (bytes_to_read == 0)
1411 if (flags & O_NONBLOCK)
1420 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1421 vec_validate (ldpw->io_buffer, bytes_to_read);
1422 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1427 vec_reset_length (ldpw->io_buffer);
1434 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1437 if (size == VPPCOM_EAGAIN)
1439 if (flags & O_NONBLOCK)
1450 vec_reset_length (ldpw->io_buffer);
1459 ASSERT (n_bytes_left >= nbytes);
1460 n_bytes_left = n_bytes_left - nbytes;
1462 while (n_bytes_left > 0);
1465 vec_reset_length (ldpw->io_buffer);
1468 off_t off = lseek (in_fd, *offset, SEEK_SET);
1469 if (PREDICT_FALSE (off == -1))
1475 ASSERT (off == *offset);
1476 *offset += results + 1;
1488 size = libc_sendfile (out_fd, in_fd, offset, len);
1496 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1498 return sendfile (out_fd, in_fd, offset, len);
1502 recv (int fd, void *buf, size_t n, int flags)
1507 if ((errno = -ldp_init ()))
1510 vlsh = ldp_fd_to_vlsh (fd);
1511 if (vlsh != VLS_INVALID_HANDLE)
1513 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1522 size = libc_recv (fd, buf, n, flags);
1529 ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1530 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1532 vppcom_endpt_t *ep = 0;
1538 switch (addr->sa_family)
1541 ep->is_ip4 = VPPCOM_IS_IP4;
1543 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1544 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1548 ep->is_ip4 = VPPCOM_IS_IP6;
1550 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1552 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1556 return EAFNOSUPPORT;
1560 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1564 ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1565 int flags, __SOCKADDR_ARG addr,
1566 socklen_t * __restrict addr_len)
1568 u8 src_addr[sizeof (struct sockaddr_in6)];
1576 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1580 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1586 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1592 sendto (int fd, const void *buf, size_t n, int flags,
1593 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1598 if ((errno = -ldp_init ()))
1601 vlsh = ldp_fd_to_vlsh (fd);
1602 if (vlsh != INVALID_SESSION_ID)
1604 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
1613 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1620 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1621 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1626 if ((errno = -ldp_init ()))
1629 vlsh = ldp_fd_to_vlsh (fd);
1630 if (vlsh != VLS_INVALID_HANDLE)
1632 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
1641 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1648 sendmsg (int fd, const struct msghdr * msg, int flags)
1653 if ((errno = -ldp_init ()))
1656 vlsh = ldp_fd_to_vlsh (fd);
1657 if (vlsh != VLS_INVALID_HANDLE)
1659 struct iovec *iov = msg->msg_iov;
1663 for (i = 0; i < msg->msg_iovlen; ++i)
1665 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1666 msg->msg_name, msg->msg_namelen);
1672 if (rv < iov[i].iov_len)
1677 if (rv < 0 && total == 0)
1687 size = libc_sendmsg (fd, msg, flags);
1695 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1698 const char *func_str;
1699 u32 sh = ldp_fd_to_vlsh (fd);
1701 if ((errno = -ldp_init ()))
1704 if (sh != INVALID_SESSION_ID)
1706 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1712 func_str = "libc_sendmmsg";
1715 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1716 "vmessages %p, vlen %u, flags 0x%x",
1717 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1719 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1726 int errno_val = errno;
1728 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1729 "rv %d, errno = %d", getpid (), fd, fd,
1730 func_str, size, errno_val);
1734 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1735 getpid (), fd, fd, size, size);
1742 recvmsg (int fd, struct msghdr * msg, int flags)
1747 if ((errno = -ldp_init ()))
1750 vlsh = ldp_fd_to_vlsh (fd);
1751 if (vlsh != VLS_INVALID_HANDLE)
1753 struct iovec *iov = msg->msg_iov;
1754 ssize_t max_deq, total = 0;
1757 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1761 for (i = 0; i < msg->msg_iovlen; i++)
1763 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1764 (i == 0 ? msg->msg_name : NULL),
1765 (i == 0 ? &msg->msg_namelen : NULL));
1771 if (rv < iov[i].iov_len)
1774 if (total >= max_deq)
1778 if (rv < 0 && total == 0)
1788 size = libc_recvmsg (fd, msg, flags);
1796 recvmmsg (int fd, struct mmsghdr *vmessages,
1797 unsigned int vlen, int flags, struct timespec *tmo)
1800 const char *func_str;
1801 u32 sh = ldp_fd_to_vlsh (fd);
1803 if ((errno = -ldp_init ()))
1806 if (sh != INVALID_SESSION_ID)
1808 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1814 func_str = "libc_recvmmsg";
1817 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1818 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1819 getpid (), fd, fd, func_str, vmessages, vlen,
1822 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1829 int errno_val = errno;
1831 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1832 "rv %d, errno = %d", getpid (), fd, fd,
1833 func_str, size, errno_val);
1837 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1838 getpid (), fd, fd, size, size);
1845 getsockopt (int fd, int level, int optname,
1846 void *__restrict optval, socklen_t * __restrict optlen)
1851 if ((errno = -ldp_init ()))
1854 vlsh = ldp_fd_to_vlsh (fd);
1855 if (vlsh != VLS_INVALID_HANDLE)
1865 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1869 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1873 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1877 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1881 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1883 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1884 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1885 memset (optval, 0, *optlen);
1891 case TCP_CONGESTION:
1892 *optlen = strlen ("cubic");
1893 strncpy (optval, "cubic", *optlen + 1);
1897 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1898 "optname %d unsupported!", fd, vlsh, optname);
1906 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1909 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1910 "optname %d unsupported!", fd, vlsh, optname);
1918 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1921 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1924 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1925 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1928 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1932 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1936 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1939 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1942 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1945 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1946 "optname %d unsupported!", fd, vlsh, optname);
1954 if (rv != VPPCOM_OK)
1962 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1969 setsockopt (int fd, int level, int optname,
1970 const void *optval, socklen_t optlen)
1975 if ((errno = -ldp_init ()))
1978 vlsh = ldp_fd_to_vlsh (fd);
1979 if (vlsh != VLS_INVALID_HANDLE)
1989 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1990 (void *) optval, &optlen);
1993 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1994 (void *) optval, &optlen);
1997 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1998 (void *) optval, &optlen);
2001 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2002 (void *) optval, &optlen);
2004 case TCP_CONGESTION:
2010 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2011 "optname %d unsupported!", fd, vlsh, optname);
2019 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2020 (void *) optval, &optlen);
2023 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2024 "optname %d unsupported!", fd, vlsh, optname);
2032 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2033 (void *) optval, &optlen);
2036 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2037 (void *) optval, &optlen);
2040 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2041 (void *) optval, &optlen);
2044 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2045 "optname %d unsupported!", fd, vlsh, optname);
2053 if (rv != VPPCOM_OK)
2061 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2068 listen (int fd, int n)
2073 if ((errno = -ldp_init ()))
2076 vlsh = ldp_fd_to_vlsh (fd);
2077 if (vlsh != VLS_INVALID_HANDLE)
2079 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2081 rv = vls_listen (vlsh, n);
2082 if (rv != VPPCOM_OK)
2090 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2091 rv = libc_listen (fd, n);
2094 LDBG (1, "fd %d: returning %d", fd, rv);
2099 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2100 socklen_t * __restrict addr_len, int flags)
2102 vls_handle_t listen_vlsh, accept_vlsh;
2105 if ((errno = -ldp_init ()))
2108 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2109 if (listen_vlsh != VLS_INVALID_HANDLE)
2112 u8 src_addr[sizeof (struct sockaddr_in6)];
2113 memset (&ep, 0, sizeof (ep));
2116 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2117 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
2119 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2120 if (accept_vlsh < 0)
2122 errno = -accept_vlsh;
2127 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2128 if (rv != VPPCOM_OK)
2130 (void) vls_close (accept_vlsh);
2136 rv = ldp_vlsh_to_fd (accept_vlsh);
2142 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2143 " flags 0x%x", listen_fd, addr, addr_len, flags);
2145 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2148 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2154 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2157 return ldp_accept4 (fd, addr, addr_len, flags);
2161 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2163 return ldp_accept4 (fd, addr, addr_len, 0);
2167 shutdown (int fd, int how)
2171 u32 flags_len = sizeof (flags);
2173 if ((errno = -ldp_init ()))
2176 vlsh = ldp_fd_to_vlsh (fd);
2177 if (vlsh != VLS_INVALID_HANDLE)
2179 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2181 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
2187 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2193 if (flags == SHUT_RDWR)
2198 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2199 rv = libc_shutdown (fd, how);
2206 epoll_create1 (int flags)
2208 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2212 if ((errno = -ldp_init ()))
2215 if (ldp->vcl_needs_real_epoll)
2217 /* Make sure workers have been allocated */
2220 ldp_alloc_workers ();
2221 ldpw = ldp_worker_get_current ();
2223 rv = libc_epoll_create1 (flags);
2224 ldp->vcl_needs_real_epoll = 0;
2225 ldpw->vcl_mq_epfd = rv;
2226 LDBG (0, "created vcl epfd %u", rv);
2230 vlsh = vls_epoll_create ();
2231 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2238 rv = ldp_vlsh_to_fd (vlsh);
2240 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2245 epoll_create (int size)
2247 return epoll_create1 (0);
2251 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2253 vls_handle_t vep_vlsh, vlsh;
2256 if ((errno = -ldp_init ()))
2259 vep_vlsh = ldp_fd_to_vlsh (epfd);
2260 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2262 /* The LDP epoll_create1 always creates VCL epfd's.
2263 * The app should never have a kernel base epoll fd unless it
2264 * was acquired outside of the LD_PRELOAD process context.
2265 * In any case, if we get one, punt it to libc_epoll_ctl.
2267 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2268 " event %p", epfd, op, fd, event);
2270 rv = libc_epoll_ctl (epfd, op, fd, event);
2274 vlsh = ldp_fd_to_vlsh (fd);
2276 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2279 if (vlsh != VLS_INVALID_HANDLE)
2281 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2282 " event %p", epfd, vep_vlsh, vlsh, event);
2284 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2285 if (rv != VPPCOM_OK)
2294 u32 size = sizeof (epfd);
2296 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2299 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2300 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2302 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2309 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2318 else if (PREDICT_FALSE (libc_epfd < 0))
2325 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2326 " event %p", epfd, libc_epfd, op, fd, event);
2328 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2336 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2337 int timeout, const sigset_t * sigmask)
2339 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2340 double time_to_wait = (double) 0, max_time;
2341 int libc_epfd, rv = 0;
2342 vls_handle_t ep_vlsh;
2344 if ((errno = -ldp_init ()))
2347 if (PREDICT_FALSE (!events || (timeout < -1)))
2353 if (epfd == ldpw->vcl_mq_epfd)
2354 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2356 ep_vlsh = ldp_fd_to_vlsh (epfd);
2357 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2359 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2364 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2365 clib_time_init (&ldpw->clib_time);
2366 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2367 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2369 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2370 if (PREDICT_FALSE (libc_epfd < 0))
2377 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2378 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2379 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2382 if (!ldpw->epoll_wait_vcl)
2384 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2387 ldpw->epoll_wait_vcl = 1;
2398 ldpw->epoll_wait_vcl = 0;
2402 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2407 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2414 ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2415 int maxevents, int timeout, const sigset_t * sigmask)
2417 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2418 int libc_epfd, rv = 0, num_ev;
2419 vls_handle_t ep_vlsh;
2421 if ((errno = -ldp_init ()))
2424 if (PREDICT_FALSE (!events || (timeout < -1)))
2430 if (epfd == ldpw->vcl_mq_epfd)
2431 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2433 ep_vlsh = ldp_fd_to_vlsh (epfd);
2434 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2436 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2441 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2442 if (PREDICT_FALSE (!libc_epfd))
2444 u32 size = sizeof (epfd);
2446 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2447 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2448 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2455 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2463 if (PREDICT_FALSE (libc_epfd <= 0))
2470 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2472 struct epoll_event e = { 0 };
2474 e.data.fd = ldpw->vcl_mq_epfd;
2475 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2478 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2479 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2483 ldpw->mq_epfd_added = 1;
2486 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2496 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2499 for (int i = 0; i < rv; i++)
2501 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2503 /* We should remove mq epoll fd from events. */
2507 events[i].events = events[rv].events;
2508 events[i].data.u64 = events[rv].data.u64;
2510 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2511 if (PREDICT_TRUE (num_ev > 0))
2522 epoll_pwait (int epfd, struct epoll_event *events,
2523 int maxevents, int timeout, const sigset_t * sigmask)
2525 if (vls_use_eventfd ())
2526 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2529 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2533 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2535 if (vls_use_eventfd ())
2536 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2538 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2542 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2544 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2545 int rv, i, n_revents = 0;
2550 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
2552 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2553 clib_time_init (&ldpw->clib_time);
2555 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2556 max_time += clib_time_now (&ldpw->clib_time);
2558 for (i = 0; i < nfds; i++)
2563 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2564 if (vlsh != VLS_INVALID_HANDLE)
2566 fds[i].fd = -fds[i].fd;
2567 vec_add2 (ldpw->vcl_poll, vp, 1);
2569 vp->sh = vlsh_to_sh (vlsh);
2570 vp->events = fds[i].events;
2571 #ifdef __USE_XOPEN2K
2572 if (fds[i].events & POLLRDNORM)
2573 vp->events |= POLLIN;
2574 if (fds[i].events & POLLWRNORM)
2575 vp->events |= POLLOUT;
2577 vp->revents = fds[i].revents;
2581 vec_add1 (ldpw->libc_poll, fds[i]);
2582 vec_add1 (ldpw->libc_poll_idxs, i);
2588 if (vec_len (ldpw->vcl_poll))
2590 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2601 if (vec_len (ldpw->libc_poll))
2603 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2616 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2620 vec_foreach (vp, ldpw->vcl_poll)
2622 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2623 fds[vp->fds_ndx].revents = vp->revents;
2624 #ifdef __USE_XOPEN2K
2625 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2626 (fds[vp->fds_ndx].events & POLLRDNORM))
2627 fds[vp->fds_ndx].revents |= POLLRDNORM;
2628 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2629 (fds[vp->fds_ndx].events & POLLWRNORM))
2630 fds[vp->fds_ndx].revents |= POLLWRNORM;
2633 vec_reset_length (ldpw->vcl_poll);
2635 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2637 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2639 vec_reset_length (ldpw->libc_poll_idxs);
2640 vec_reset_length (ldpw->libc_poll);
2647 ppoll (struct pollfd *fds, nfds_t nfds,
2648 const struct timespec *timeout, const sigset_t * sigmask)
2650 if ((errno = -ldp_init ()))
2653 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2661 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2663 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2666 * This function is called when the library is loaded
2669 ldp_constructor (void)
2671 swrap_constructor ();
2672 if (ldp_init () != 0)
2674 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2678 else if (LDP_DEBUG > 0)
2679 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2683 * This function is called when the library is unloaded
2686 ldp_destructor (void)
2689 swrap_destructor ();
2694 /* Don't use clib_warning() here because that calls writev()
2695 * which will call ldp_init().
2698 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2699 __func__, __LINE__, getpid ());
2704 * fd.io coding-style-patch-verification: ON
2707 * eval: (c-set-style "gnu")