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];
104 /** vcl needs next epoll_create to go to libc_epoll */
105 u8 vcl_needs_real_epoll;
108 * crypto state used only for testing
114 #define LDP_DEBUG ldp->debug
116 #define LDBG(_lvl, _fmt, _args...) \
117 if (ldp->debug > _lvl) \
119 int errno_saved = errno; \
120 fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \
121 errno = errno_saved; \
124 static ldp_main_t ldp_main = {
125 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
126 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
127 .debug = LDP_DEBUG_INIT,
128 .transparent_tls = 0,
132 static ldp_main_t *ldp = &ldp_main;
134 static inline ldp_worker_ctx_t *
135 ldp_worker_get_current (void)
137 return (ldp->workers + vppcom_worker_index ());
141 * RETURN: 0 on success or -1 on error.
144 ldp_set_app_name (char *app_name)
146 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
147 "ldp-%d-%s", getpid (), app_name);
153 if (ldp->app_name[0] == '\0')
154 ldp_set_app_name ("app");
156 return ldp->app_name;
160 ldp_vlsh_to_fd (vls_handle_t vlsh)
162 return (vlsh + ldp->vlsh_bit_val);
165 static inline vls_handle_t
166 ldp_fd_to_vlsh (int fd)
168 if (fd < ldp->vlsh_bit_val)
169 return VLS_INVALID_HANDLE;
171 return (fd - ldp->vlsh_bit_val);
175 ldp_alloc_workers (void)
179 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
185 ldp_worker_ctx_t *ldpw;
188 if (PREDICT_TRUE (ldp->init))
192 ldp->vcl_needs_real_epoll = 1;
193 rv = vls_app_create (ldp_get_app_name ());
196 ldp->vcl_needs_real_epoll = 0;
197 if (rv == VPPCOM_EEXIST)
199 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
200 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
204 ldp->vcl_needs_real_epoll = 0;
205 ldp_alloc_workers ();
206 ldpw = ldp_worker_get_current ();
208 char *env_var_str = getenv (LDP_ENV_DEBUG);
212 if (sscanf (env_var_str, "%u", &tmp) != 1)
213 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
214 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
219 LDBG (0, "configured LDP debug level (%u) from env var "
220 LDP_ENV_DEBUG "!", ldp->debug);
224 env_var_str = getenv (LDP_ENV_APP_NAME);
227 ldp_set_app_name (env_var_str);
228 LDBG (0, "configured LDP app name (%s) from the env var "
229 LDP_ENV_APP_NAME "!", ldp->app_name);
232 env_var_str = getenv (LDP_ENV_SID_BIT);
236 if (sscanf (env_var_str, "%u", &sb) != 1)
238 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
239 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
240 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
242 else if (sb < LDP_SID_BIT_MIN)
244 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
245 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
247 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
248 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
249 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
250 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
252 else if (sb > LDP_SID_BIT_MAX)
254 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
255 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
257 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
258 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
259 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
260 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
264 ldp->vlsh_bit_val = (1 << sb);
265 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
267 LDBG (0, "configured LDP sid bit (%u) from "
268 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
269 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
272 /* Make sure there are enough bits in the fd set for vcl sessions */
273 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
275 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
276 ldp->vlsh_bit_val, FD_SETSIZE / 2);
281 env_var_str = getenv (LDP_ENV_TLS_TRANS);
284 ldp->transparent_tls = 1;
288 pool_foreach (ldpw, ldp->workers) {
289 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
293 LDBG (0, "LDP initialization: done!");
304 if ((errno = -ldp_init ()))
307 vlsh = ldp_fd_to_vlsh (fd);
308 if (vlsh != VLS_INVALID_HANDLE)
310 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
313 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
315 rv = libc_close (epfd);
318 u32 size = sizeof (epfd);
321 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
324 else if (PREDICT_FALSE (epfd < 0))
331 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
333 rv = vls_close (vlsh);
342 LDBG (0, "fd %d: calling libc_close", fd);
343 rv = libc_close (fd);
351 read (int fd, void *buf, size_t nbytes)
356 if ((errno = -ldp_init ()))
359 vlsh = ldp_fd_to_vlsh (fd);
360 if (vlsh != VLS_INVALID_HANDLE)
362 size = vls_read (vlsh, buf, nbytes);
371 size = libc_read (fd, buf, nbytes);
378 readv (int fd, const struct iovec * iov, int iovcnt)
380 int rv = 0, i, total = 0;
384 if ((errno = -ldp_init ()))
387 vlsh = ldp_fd_to_vlsh (fd);
388 if (vlsh != VLS_INVALID_HANDLE)
390 for (i = 0; i < iovcnt; ++i)
392 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
398 if (rv < iov[i].iov_len)
402 if (rv < 0 && total == 0)
412 size = libc_readv (fd, iov, iovcnt);
419 write (int fd, const void *buf, size_t nbytes)
424 if ((errno = -ldp_init ()))
427 vlsh = ldp_fd_to_vlsh (fd);
428 if (vlsh != VLS_INVALID_HANDLE)
430 size = vls_write_msg (vlsh, (void *) buf, nbytes);
439 size = libc_write (fd, buf, nbytes);
446 writev (int fd, const struct iovec * iov, int iovcnt)
448 ssize_t size = 0, total = 0;
452 if ((errno = -ldp_init ()))
455 vlsh = ldp_fd_to_vlsh (fd);
456 if (vlsh != VLS_INVALID_HANDLE)
458 for (i = 0; i < iovcnt; ++i)
460 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
466 if (rv < iov[i].iov_len)
471 if (rv < 0 && total == 0)
481 size = libc_writev (fd, iov, iovcnt);
488 fcntl_internal (int fd, int cmd, va_list ap)
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);
541 fcntl (int fd, int cmd, ...)
546 if ((errno = -ldp_init ()))
550 rv = fcntl_internal (fd, cmd, ap);
557 fcntl64 (int fd, int cmd, ...)
562 if ((errno = -ldp_init ()))
566 rv = fcntl_internal (fd, cmd, ap);
572 ioctl (int fd, unsigned long int cmd, ...)
578 if ((errno = -ldp_init ()))
583 vlsh = ldp_fd_to_vlsh (fd);
584 if (vlsh != VLS_INVALID_HANDLE)
589 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
594 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
595 u32 size = sizeof (flags);
597 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
598 * non-blocking, the flags should be read here and merged
601 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
617 rv = libc_vioctl (fd, cmd, ap);
625 ldp_select_init_maps (fd_set * __restrict original,
626 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
627 clib_bitmap_t ** vclb, int nfds, u32 minbits,
628 u32 n_bytes, uword * si_bits, uword * libc_bits)
630 uword si_bits_set, libc_bits_set;
634 clib_bitmap_validate (*vclb, minbits);
635 clib_bitmap_validate (*libcb, minbits);
636 clib_bitmap_validate (*resultb, minbits);
637 clib_memcpy_fast (*resultb, original, n_bytes);
638 memset (original, 0, n_bytes);
641 clib_bitmap_foreach (fd, *resultb) {
644 vlsh = ldp_fd_to_vlsh (fd);
645 if (vlsh == VLS_INVALID_HANDLE)
646 clib_bitmap_set_no_check (*libcb, fd, 1);
648 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
652 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
653 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
654 clib_bitmap_validate (*resultb, *si_bits);
656 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
657 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
661 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
671 clib_bitmap_foreach (si, vclb) {
672 vlsh = vls_session_index_to_vlsh (si);
673 ASSERT (vlsh != VLS_INVALID_HANDLE);
674 fd = ldp_vlsh_to_fd (vlsh);
675 if (PREDICT_FALSE (fd < 0))
688 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
696 clib_bitmap_foreach (fd, result)
697 FD_SET ((int)fd, libcb);
702 ldp_pselect (int nfds, fd_set * __restrict readfds,
703 fd_set * __restrict writefds,
704 fd_set * __restrict exceptfds,
705 const struct timespec *__restrict timeout,
706 const __sigset_t * __restrict sigmask)
708 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
709 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
710 struct timespec libc_tspec = { 0 };
711 f64 time_out, vcl_timeout = 0;
712 uword si_bits, libc_bits;
713 int rv, bits_set = 0;
721 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
722 clib_time_init (&ldpw->clib_time);
726 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
727 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
729 /* select as fine grained sleep */
732 time_out += clib_time_now (&ldpw->clib_time);
733 while (clib_time_now (&ldpw->clib_time) < time_out)
746 if (nfds <= ldp->vlsh_bit_val)
748 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
753 si_bits = libc_bits = 0;
754 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
757 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
758 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
759 &si_bits, &libc_bits);
761 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
762 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
763 minbits, n_bytes, &si_bits, &libc_bits);
765 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
766 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
767 minbits, n_bytes, &si_bits, &libc_bits);
769 if (PREDICT_FALSE (!si_bits && !libc_bits))
777 libc_tspec = timeout ? *timeout : libc_tspec;
784 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
785 vec_len (ldpw->si_rd_bitmap) *
786 sizeof (clib_bitmap_t));
788 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
789 vec_len (ldpw->si_wr_bitmap) *
790 sizeof (clib_bitmap_t));
792 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
793 vec_len (ldpw->si_ex_bitmap) *
794 sizeof (clib_bitmap_t));
796 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
797 writefds ? ldpw->wr_bitmap : NULL,
798 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
807 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
813 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
819 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
830 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
831 vec_len (ldpw->libc_rd_bitmap) *
832 sizeof (clib_bitmap_t));
834 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
835 vec_len (ldpw->libc_wr_bitmap) *
836 sizeof (clib_bitmap_t));
838 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
839 vec_len (ldpw->libc_ex_bitmap) *
840 sizeof (clib_bitmap_t));
842 rv = libc_pselect (libc_bits,
843 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
844 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
845 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
846 &libc_tspec, sigmask);
849 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
850 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
851 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
862 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
866 /* TBD: set timeout to amount of time left */
867 clib_bitmap_zero (ldpw->rd_bitmap);
868 clib_bitmap_zero (ldpw->si_rd_bitmap);
869 clib_bitmap_zero (ldpw->libc_rd_bitmap);
870 clib_bitmap_zero (ldpw->wr_bitmap);
871 clib_bitmap_zero (ldpw->si_wr_bitmap);
872 clib_bitmap_zero (ldpw->libc_wr_bitmap);
873 clib_bitmap_zero (ldpw->ex_bitmap);
874 clib_bitmap_zero (ldpw->si_ex_bitmap);
875 clib_bitmap_zero (ldpw->libc_ex_bitmap);
881 select (int nfds, fd_set * __restrict readfds,
882 fd_set * __restrict writefds,
883 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
885 struct timespec tspec;
889 tspec.tv_sec = timeout->tv_sec;
890 tspec.tv_nsec = timeout->tv_usec * 1000;
892 return ldp_pselect (nfds, readfds, writefds, exceptfds,
893 timeout ? &tspec : NULL, NULL);
898 pselect (int nfds, fd_set * __restrict readfds,
899 fd_set * __restrict writefds,
900 fd_set * __restrict exceptfds,
901 const struct timespec *__restrict timeout,
902 const __sigset_t * __restrict sigmask)
904 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
908 /* If transparent TLS mode is turned on, then ldp will load key and cert.
911 load_cert_key_pair (void)
913 char *cert_str = getenv (LDP_ENV_TLS_CERT);
914 char *key_str = getenv (LDP_ENV_TLS_KEY);
915 char cert_buf[4096], key_buf[4096];
916 int cert_size, key_size;
917 vppcom_cert_key_pair_t crypto;
921 if (!cert_str || !key_str)
923 LDBG (0, "ERROR: failed to read LDP environment %s\n",
928 fp = fopen (cert_str, "r");
931 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
934 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
937 fp = fopen (key_str, "r");
940 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
943 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
946 crypto.cert = cert_buf;
947 crypto.key = key_buf;
948 crypto.cert_len = cert_size;
949 crypto.key_len = key_size;
950 ckp_index = vppcom_add_cert_key_pair (&crypto);
953 LDBG (0, "ERROR: failed to add cert key pair\n");
957 ldp->ckpair_index = ckp_index;
963 assign_cert_key_pair (vls_handle_t vlsh)
967 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
970 ckp_len = sizeof (ldp->ckpair_index);
971 return vppcom_session_attr (vlsh_to_session_index (vlsh),
972 VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index,
977 socket (int domain, int type, int protocol)
979 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
980 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
983 if ((errno = -ldp_init ()))
986 if (((domain == AF_INET) || (domain == AF_INET6)) &&
987 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
990 if (ldp->transparent_tls)
992 proto = VPPCOM_PROTO_TLS;
995 proto = ((sock_type == SOCK_DGRAM) ?
996 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
998 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
999 proto, vppcom_proto_str (proto), is_nonblocking);
1001 vlsh = vls_create (proto, is_nonblocking);
1009 if (ldp->transparent_tls)
1011 if (assign_cert_key_pair (vlsh) < 0)
1014 rv = ldp_vlsh_to_fd (vlsh);
1019 LDBG (0, "calling libc_socket");
1020 rv = libc_socket (domain, type, protocol);
1027 * Create two new sockets, of type TYPE in domain DOMAIN and using
1028 * protocol PROTOCOL, which are connected to each other, and put file
1029 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1030 * one will be chosen automatically.
1031 * Returns 0 on success, -1 for errors.
1034 socketpair (int domain, int type, int protocol, int fds[2])
1036 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1038 if ((errno = -ldp_init ()))
1041 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1042 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1044 LDBG (0, "LDP-TBD");
1050 LDBG (1, "calling libc_socketpair");
1051 rv = libc_socketpair (domain, type, protocol, fds);
1058 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1063 if ((errno = -ldp_init ()))
1066 vlsh = ldp_fd_to_vlsh (fd);
1067 if (vlsh != VLS_INVALID_HANDLE)
1071 switch (addr->sa_family)
1074 if (len != sizeof (struct sockaddr_in))
1076 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1082 ep.is_ip4 = VPPCOM_IS_IP4;
1083 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1084 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1088 if (len != sizeof (struct sockaddr_in6))
1090 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1096 ep.is_ip4 = VPPCOM_IS_IP6;
1097 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1098 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1102 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1103 fd, vlsh, addr->sa_family);
1104 errno = EAFNOSUPPORT;
1108 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1111 rv = vls_bind (vlsh, &ep);
1112 if (rv != VPPCOM_OK)
1120 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1121 rv = libc_bind (fd, addr, len);
1125 LDBG (1, "fd %d: returning %d", fd, rv);
1131 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1132 vppcom_endpt_t * ep)
1135 int sa_len, copy_len;
1137 if ((errno = -ldp_init ()))
1140 if (addr && len && ep)
1142 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1143 switch (addr->sa_family)
1146 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1147 if (*len > sizeof (struct sockaddr_in))
1148 *len = sizeof (struct sockaddr_in);
1149 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1150 copy_len = *len - sa_len;
1152 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1157 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1158 if (*len > sizeof (struct sockaddr_in6))
1159 *len = sizeof (struct sockaddr_in6);
1160 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1161 copy_len = *len - sa_len;
1163 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1164 __in6_u.__u6_addr8, ep->ip, copy_len);
1177 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1182 if ((errno = -ldp_init ()))
1185 vlsh = ldp_fd_to_vlsh (fd);
1186 if (vlsh != VLS_INVALID_HANDLE)
1189 u8 addr_buf[sizeof (struct in6_addr)];
1190 u32 size = sizeof (ep);
1194 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1195 if (rv != VPPCOM_OK)
1202 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1203 if (rv != VPPCOM_OK)
1212 rv = libc_getsockname (fd, addr, len);
1219 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1224 if ((errno = -ldp_init ()))
1229 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1235 vlsh = ldp_fd_to_vlsh (fd);
1236 if (vlsh != VLS_INVALID_HANDLE)
1240 switch (addr->sa_family)
1243 if (len != sizeof (struct sockaddr_in))
1245 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1251 ep.is_ip4 = VPPCOM_IS_IP4;
1252 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1253 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1257 if (len != sizeof (struct sockaddr_in6))
1259 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1265 ep.is_ip4 = VPPCOM_IS_IP6;
1266 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1267 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1271 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1272 fd, vlsh, addr->sa_family);
1273 errno = EAFNOSUPPORT;
1277 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1280 rv = vls_connect (vlsh, &ep);
1281 if (rv != VPPCOM_OK)
1289 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1292 rv = libc_connect (fd, addr, len);
1296 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1301 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1306 if ((errno = -ldp_init ()))
1309 vlsh = ldp_fd_to_vlsh (fd);
1310 if (vlsh != VLS_INVALID_HANDLE)
1313 u8 addr_buf[sizeof (struct in6_addr)];
1314 u32 size = sizeof (ep);
1317 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1318 if (rv != VPPCOM_OK)
1325 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1326 if (rv != VPPCOM_OK)
1335 rv = libc_getpeername (fd, addr, len);
1342 send (int fd, const void *buf, size_t n, int flags)
1344 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1347 if ((errno = -ldp_init ()))
1350 if (vlsh != VLS_INVALID_HANDLE)
1352 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1353 if (size < VPPCOM_OK)
1361 size = libc_send (fd, buf, n, flags);
1368 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1370 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1374 if ((errno = -ldp_init ()))
1377 vlsh = ldp_fd_to_vlsh (out_fd);
1378 if (vlsh != VLS_INVALID_HANDLE)
1381 ssize_t results = 0;
1382 size_t n_bytes_left = len;
1383 size_t bytes_to_read;
1386 u32 flags, flags_len = sizeof (flags);
1388 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1389 if (PREDICT_FALSE (rv != VPPCOM_OK))
1391 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1392 out_fd, vlsh, rv, vppcom_retval_str (rv));
1394 vec_reset_length (ldpw->io_buffer);
1402 off_t off = lseek (in_fd, *offset, SEEK_SET);
1403 if (PREDICT_FALSE (off == -1))
1409 ASSERT (off == *offset);
1414 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1417 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
1418 out_fd, vlsh, size, vppcom_retval_str (size));
1419 vec_reset_length (ldpw->io_buffer);
1425 bytes_to_read = size;
1426 if (bytes_to_read == 0)
1428 if (flags & O_NONBLOCK)
1437 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1438 vec_validate (ldpw->io_buffer, bytes_to_read);
1439 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1444 vec_reset_length (ldpw->io_buffer);
1451 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1454 if (size == VPPCOM_EAGAIN)
1456 if (flags & O_NONBLOCK)
1467 vec_reset_length (ldpw->io_buffer);
1476 ASSERT (n_bytes_left >= nbytes);
1477 n_bytes_left = n_bytes_left - nbytes;
1479 while (n_bytes_left > 0);
1482 vec_reset_length (ldpw->io_buffer);
1485 off_t off = lseek (in_fd, *offset, SEEK_SET);
1486 if (PREDICT_FALSE (off == -1))
1492 ASSERT (off == *offset);
1493 *offset += results + 1;
1505 size = libc_sendfile (out_fd, in_fd, offset, len);
1513 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1515 return sendfile (out_fd, in_fd, offset, len);
1519 recv (int fd, void *buf, size_t n, int flags)
1524 if ((errno = -ldp_init ()))
1527 vlsh = ldp_fd_to_vlsh (fd);
1528 if (vlsh != VLS_INVALID_HANDLE)
1530 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1539 size = libc_recv (fd, buf, n, flags);
1546 ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1547 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1549 vppcom_endpt_t *ep = 0;
1555 switch (addr->sa_family)
1558 ep->is_ip4 = VPPCOM_IS_IP4;
1560 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1561 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1565 ep->is_ip4 = VPPCOM_IS_IP6;
1567 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1569 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1573 return EAFNOSUPPORT;
1577 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1581 ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1582 int flags, __SOCKADDR_ARG addr,
1583 socklen_t * __restrict addr_len)
1585 u8 src_addr[sizeof (struct sockaddr_in6)];
1593 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1597 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1603 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1609 sendto (int fd, const void *buf, size_t n, int flags,
1610 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1615 if ((errno = -ldp_init ()))
1618 vlsh = ldp_fd_to_vlsh (fd);
1619 if (vlsh != INVALID_SESSION_ID)
1621 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
1630 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1637 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1638 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1643 if ((errno = -ldp_init ()))
1646 vlsh = ldp_fd_to_vlsh (fd);
1647 if (vlsh != VLS_INVALID_HANDLE)
1649 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
1658 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1665 sendmsg (int fd, const struct msghdr * msg, int flags)
1670 if ((errno = -ldp_init ()))
1673 vlsh = ldp_fd_to_vlsh (fd);
1674 if (vlsh != VLS_INVALID_HANDLE)
1676 struct iovec *iov = msg->msg_iov;
1680 for (i = 0; i < msg->msg_iovlen; ++i)
1682 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1683 msg->msg_name, msg->msg_namelen);
1689 if (rv < iov[i].iov_len)
1694 if (rv < 0 && total == 0)
1704 size = libc_sendmsg (fd, msg, flags);
1712 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1715 const char *func_str;
1716 u32 sh = ldp_fd_to_vlsh (fd);
1718 if ((errno = -ldp_init ()))
1721 if (sh != INVALID_SESSION_ID)
1723 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1729 func_str = "libc_sendmmsg";
1732 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1733 "vmessages %p, vlen %u, flags 0x%x",
1734 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1736 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1743 int errno_val = errno;
1745 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1746 "rv %d, errno = %d", getpid (), fd, fd,
1747 func_str, size, errno_val);
1751 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1752 getpid (), fd, fd, size, size);
1759 recvmsg (int fd, struct msghdr * msg, int flags)
1764 if ((errno = -ldp_init ()))
1767 vlsh = ldp_fd_to_vlsh (fd);
1768 if (vlsh != VLS_INVALID_HANDLE)
1770 struct iovec *iov = msg->msg_iov;
1771 ssize_t max_deq, total = 0;
1774 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1778 for (i = 0; i < msg->msg_iovlen; i++)
1780 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1781 (i == 0 ? msg->msg_name : NULL),
1782 (i == 0 ? &msg->msg_namelen : NULL));
1788 if (rv < iov[i].iov_len)
1791 if (total >= max_deq)
1795 if (rv < 0 && total == 0)
1805 size = libc_recvmsg (fd, msg, flags);
1813 recvmmsg (int fd, struct mmsghdr *vmessages,
1814 unsigned int vlen, int flags, struct timespec *tmo)
1817 const char *func_str;
1818 u32 sh = ldp_fd_to_vlsh (fd);
1820 if ((errno = -ldp_init ()))
1823 if (sh != INVALID_SESSION_ID)
1825 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1831 func_str = "libc_recvmmsg";
1834 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1835 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1836 getpid (), fd, fd, func_str, vmessages, vlen,
1839 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1846 int errno_val = errno;
1848 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1849 "rv %d, errno = %d", getpid (), fd, fd,
1850 func_str, size, errno_val);
1854 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1855 getpid (), fd, fd, size, size);
1862 getsockopt (int fd, int level, int optname,
1863 void *__restrict optval, socklen_t * __restrict optlen)
1868 if ((errno = -ldp_init ()))
1871 vlsh = ldp_fd_to_vlsh (fd);
1872 if (vlsh != VLS_INVALID_HANDLE)
1882 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1886 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1890 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1894 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1898 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1900 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1901 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1902 memset (optval, 0, *optlen);
1908 case TCP_CONGESTION:
1909 *optlen = strlen ("cubic");
1910 strncpy (optval, "cubic", *optlen + 1);
1914 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1915 "optname %d unsupported!", fd, vlsh, optname);
1923 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1926 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1927 "optname %d unsupported!", fd, vlsh, optname);
1935 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1938 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1941 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1942 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1945 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1949 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1953 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1956 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1959 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1962 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1965 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1968 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1969 "optname %d unsupported!", fd, vlsh, optname);
1977 if (rv != VPPCOM_OK)
1985 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1992 setsockopt (int fd, int level, int optname,
1993 const void *optval, socklen_t optlen)
1998 if ((errno = -ldp_init ()))
2001 vlsh = ldp_fd_to_vlsh (fd);
2002 if (vlsh != VLS_INVALID_HANDLE)
2012 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2013 (void *) optval, &optlen);
2016 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2017 (void *) optval, &optlen);
2020 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2021 (void *) optval, &optlen);
2024 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2025 (void *) optval, &optlen);
2027 case TCP_CONGESTION:
2033 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2034 "optname %d unsupported!", fd, vlsh, optname);
2042 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2043 (void *) optval, &optlen);
2046 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2047 "optname %d unsupported!", fd, vlsh, optname);
2055 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2056 (void *) optval, &optlen);
2059 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2060 (void *) optval, &optlen);
2063 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2067 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2068 (void *) optval, &optlen);
2071 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2072 "optname %d unsupported!", fd, vlsh, optname);
2080 if (rv != VPPCOM_OK)
2088 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2095 listen (int fd, int n)
2100 if ((errno = -ldp_init ()))
2103 vlsh = ldp_fd_to_vlsh (fd);
2104 if (vlsh != VLS_INVALID_HANDLE)
2106 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2108 rv = vls_listen (vlsh, n);
2109 if (rv != VPPCOM_OK)
2117 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2118 rv = libc_listen (fd, n);
2121 LDBG (1, "fd %d: returning %d", fd, rv);
2126 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2127 socklen_t * __restrict addr_len, int flags)
2129 vls_handle_t listen_vlsh, accept_vlsh;
2132 if ((errno = -ldp_init ()))
2135 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2136 if (listen_vlsh != VLS_INVALID_HANDLE)
2139 u8 src_addr[sizeof (struct sockaddr_in6)];
2140 memset (&ep, 0, sizeof (ep));
2143 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2144 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
2146 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2147 if (accept_vlsh < 0)
2149 errno = -accept_vlsh;
2154 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2155 if (rv != VPPCOM_OK)
2157 (void) vls_close (accept_vlsh);
2163 rv = ldp_vlsh_to_fd (accept_vlsh);
2169 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2170 " flags 0x%x", listen_fd, addr, addr_len, flags);
2172 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2175 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2181 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2184 return ldp_accept4 (fd, addr, addr_len, flags);
2188 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2190 return ldp_accept4 (fd, addr, addr_len, 0);
2194 shutdown (int fd, int how)
2198 u32 flags_len = sizeof (flags);
2200 if ((errno = -ldp_init ()))
2203 vlsh = ldp_fd_to_vlsh (fd);
2204 if (vlsh != VLS_INVALID_HANDLE)
2206 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2208 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
2214 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2220 if (flags == SHUT_RDWR)
2225 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2226 rv = libc_shutdown (fd, how);
2233 epoll_create1 (int flags)
2235 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2239 if ((errno = -ldp_init ()))
2242 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
2244 /* Make sure workers have been allocated */
2247 ldp_alloc_workers ();
2248 ldpw = ldp_worker_get_current ();
2250 rv = libc_epoll_create1 (flags);
2251 ldp->vcl_needs_real_epoll = 0;
2252 ldpw->vcl_mq_epfd = rv;
2253 LDBG (0, "created vcl epfd %u", rv);
2257 vlsh = vls_epoll_create ();
2258 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2265 rv = ldp_vlsh_to_fd (vlsh);
2267 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2272 epoll_create (int size)
2274 return epoll_create1 (0);
2278 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2280 vls_handle_t vep_vlsh, vlsh;
2283 if ((errno = -ldp_init ()))
2286 vep_vlsh = ldp_fd_to_vlsh (epfd);
2287 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2289 /* The LDP epoll_create1 always creates VCL epfd's.
2290 * The app should never have a kernel base epoll fd unless it
2291 * was acquired outside of the LD_PRELOAD process context.
2292 * In any case, if we get one, punt it to libc_epoll_ctl.
2294 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2295 " event %p", epfd, op, fd, event);
2297 rv = libc_epoll_ctl (epfd, op, fd, event);
2301 vlsh = ldp_fd_to_vlsh (fd);
2303 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2306 if (vlsh != VLS_INVALID_HANDLE)
2308 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2309 " event %p", epfd, vep_vlsh, op, vlsh, event);
2311 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2312 if (rv != VPPCOM_OK)
2321 u32 size = sizeof (epfd);
2323 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2326 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2327 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2329 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2336 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2345 else if (PREDICT_FALSE (libc_epfd < 0))
2352 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2353 " event %p", epfd, libc_epfd, op, fd, event);
2355 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2363 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2364 int timeout, const sigset_t * sigmask)
2366 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2367 double time_to_wait = (double) 0, max_time;
2368 int libc_epfd, rv = 0;
2369 vls_handle_t ep_vlsh;
2371 if ((errno = -ldp_init ()))
2374 if (PREDICT_FALSE (!events || (timeout < -1)))
2380 if (epfd == ldpw->vcl_mq_epfd)
2381 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2383 ep_vlsh = ldp_fd_to_vlsh (epfd);
2384 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2386 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2391 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2392 clib_time_init (&ldpw->clib_time);
2393 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2394 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2396 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2397 if (PREDICT_FALSE (libc_epfd < 0))
2404 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2405 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2406 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2409 if (!ldpw->epoll_wait_vcl)
2411 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2414 ldpw->epoll_wait_vcl = 1;
2425 ldpw->epoll_wait_vcl = 0;
2429 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2434 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2441 ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2442 int maxevents, int timeout, const sigset_t * sigmask)
2444 ldp_worker_ctx_t *ldpw;
2445 int libc_epfd, rv = 0, num_ev;
2446 vls_handle_t ep_vlsh;
2448 if ((errno = -ldp_init ()))
2451 if (PREDICT_FALSE (!events || (timeout < -1)))
2457 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2458 * one thread but it is now used on another */
2459 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2460 vls_register_vcl_worker ();
2462 ldpw = ldp_worker_get_current ();
2463 if (epfd == ldpw->vcl_mq_epfd)
2464 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2466 ep_vlsh = ldp_fd_to_vlsh (epfd);
2467 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2469 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2474 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2475 if (PREDICT_FALSE (!libc_epfd))
2477 u32 size = sizeof (epfd);
2479 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2480 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2481 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2488 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2496 if (PREDICT_FALSE (libc_epfd <= 0))
2503 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2505 struct epoll_event e = { 0 };
2507 e.data.fd = ldpw->vcl_mq_epfd;
2508 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2511 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2512 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2516 ldpw->mq_epfd_added = 1;
2519 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2520 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
2523 else if (PREDICT_FALSE (rv < 0))
2530 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2533 for (int i = 0; i < rv; i++)
2535 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2537 /* We should remove mq epoll fd from events. */
2541 events[i].events = events[rv].events;
2542 events[i].data.u64 = events[rv].data.u64;
2544 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2545 if (PREDICT_TRUE (num_ev > 0))
2556 epoll_pwait (int epfd, struct epoll_event *events,
2557 int maxevents, int timeout, const sigset_t * sigmask)
2559 if (vls_use_eventfd ())
2560 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2563 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2567 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2569 if (vls_use_eventfd ())
2570 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2572 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2576 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2578 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2579 int rv, i, n_revents = 0;
2584 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
2586 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2587 clib_time_init (&ldpw->clib_time);
2589 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2590 max_time += clib_time_now (&ldpw->clib_time);
2592 for (i = 0; i < nfds; i++)
2597 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2598 if (vlsh != VLS_INVALID_HANDLE)
2600 fds[i].fd = -fds[i].fd;
2601 vec_add2 (ldpw->vcl_poll, vp, 1);
2603 vp->sh = vlsh_to_sh (vlsh);
2604 vp->events = fds[i].events;
2605 #ifdef __USE_XOPEN2K
2606 if (fds[i].events & POLLRDNORM)
2607 vp->events |= POLLIN;
2608 if (fds[i].events & POLLWRNORM)
2609 vp->events |= POLLOUT;
2611 vp->revents = fds[i].revents;
2615 vec_add1 (ldpw->libc_poll, fds[i]);
2616 vec_add1 (ldpw->libc_poll_idxs, i);
2622 if (vec_len (ldpw->vcl_poll))
2624 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2635 if (vec_len (ldpw->libc_poll))
2637 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2650 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2654 vec_foreach (vp, ldpw->vcl_poll)
2656 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2657 fds[vp->fds_ndx].revents = vp->revents;
2658 #ifdef __USE_XOPEN2K
2659 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2660 (fds[vp->fds_ndx].events & POLLRDNORM))
2661 fds[vp->fds_ndx].revents |= POLLRDNORM;
2662 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2663 (fds[vp->fds_ndx].events & POLLWRNORM))
2664 fds[vp->fds_ndx].revents |= POLLWRNORM;
2667 vec_reset_length (ldpw->vcl_poll);
2669 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2671 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2673 vec_reset_length (ldpw->libc_poll_idxs);
2674 vec_reset_length (ldpw->libc_poll);
2681 ppoll (struct pollfd *fds, nfds_t nfds,
2682 const struct timespec *timeout, const sigset_t * sigmask)
2684 if ((errno = -ldp_init ()))
2687 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2695 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2697 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2700 * This function is called when the library is loaded
2703 ldp_constructor (void)
2705 swrap_constructor ();
2706 if (ldp_init () != 0)
2708 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2712 else if (LDP_DEBUG > 0)
2713 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2717 * This function is called when the library is unloaded
2720 ldp_destructor (void)
2723 swrap_destructor ();
2728 /* Don't use clib_warning() here because that calls writev()
2729 * which will call ldp_init().
2732 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2733 __func__, __LINE__, getpid ());
2738 * fd.io coding-style-patch-verification: ON
2741 * eval: (c-set-style "gnu")