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;
191 ldp->vcl_needs_real_epoll = 1;
192 rv = vls_app_create (ldp_get_app_name ());
195 ldp->vcl_needs_real_epoll = 0;
196 if (rv == VPPCOM_EEXIST)
198 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
199 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
203 ldp->vcl_needs_real_epoll = 0;
204 ldp_alloc_workers ();
205 ldpw = ldp_worker_get_current ();
207 char *env_var_str = getenv (LDP_ENV_DEBUG);
211 if (sscanf (env_var_str, "%u", &tmp) != 1)
212 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
213 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
218 LDBG (0, "configured LDP debug level (%u) from env var "
219 LDP_ENV_DEBUG "!", ldp->debug);
223 env_var_str = getenv (LDP_ENV_APP_NAME);
226 ldp_set_app_name (env_var_str);
227 LDBG (0, "configured LDP app name (%s) from the env var "
228 LDP_ENV_APP_NAME "!", ldp->app_name);
231 env_var_str = getenv (LDP_ENV_SID_BIT);
235 if (sscanf (env_var_str, "%u", &sb) != 1)
237 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
238 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
239 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
241 else if (sb < LDP_SID_BIT_MIN)
243 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
244 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
246 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
247 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
248 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
249 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
251 else if (sb > LDP_SID_BIT_MAX)
253 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
254 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
256 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
257 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
258 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
259 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
263 ldp->vlsh_bit_val = (1 << sb);
264 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
266 LDBG (0, "configured LDP sid bit (%u) from "
267 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
268 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
271 /* Make sure there are enough bits in the fd set for vcl sessions */
272 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
274 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
275 ldp->vlsh_bit_val, FD_SETSIZE / 2);
280 env_var_str = getenv (LDP_ENV_TLS_TRANS);
283 ldp->transparent_tls = 1;
287 pool_foreach (ldpw, ldp->workers) {
288 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
292 LDBG (0, "LDP initialization: done!");
297 #define ldp_init_check() \
298 if (PREDICT_FALSE (!ldp->init)) \
300 if ((errno = -ldp_init ())) \
312 vlsh = ldp_fd_to_vlsh (fd);
313 if (vlsh != VLS_INVALID_HANDLE)
315 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
318 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
320 rv = libc_close (epfd);
323 u32 size = sizeof (epfd);
326 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
329 else if (PREDICT_FALSE (epfd < 0))
336 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
338 rv = vls_close (vlsh);
347 LDBG (0, "fd %d: calling libc_close", fd);
348 rv = libc_close (fd);
356 read (int fd, void *buf, size_t nbytes)
363 vlsh = ldp_fd_to_vlsh (fd);
364 if (vlsh != VLS_INVALID_HANDLE)
366 size = vls_read (vlsh, buf, nbytes);
375 size = libc_read (fd, buf, nbytes);
382 readv (int fd, const struct iovec * iov, int iovcnt)
384 int rv = 0, i, total = 0;
390 vlsh = ldp_fd_to_vlsh (fd);
391 if (vlsh != VLS_INVALID_HANDLE)
393 for (i = 0; i < iovcnt; ++i)
395 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
401 if (rv < iov[i].iov_len)
405 if (rv < 0 && total == 0)
415 size = libc_readv (fd, iov, iovcnt);
422 write (int fd, const void *buf, size_t nbytes)
429 vlsh = ldp_fd_to_vlsh (fd);
430 if (vlsh != VLS_INVALID_HANDLE)
432 size = vls_write_msg (vlsh, (void *) buf, nbytes);
441 size = libc_write (fd, buf, nbytes);
448 writev (int fd, const struct iovec * iov, int iovcnt)
450 ssize_t size = 0, total = 0;
456 vlsh = ldp_fd_to_vlsh (fd);
457 if (vlsh != VLS_INVALID_HANDLE)
459 for (i = 0; i < iovcnt; ++i)
461 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
467 if (rv < iov[i].iov_len)
472 if (rv < 0 && total == 0)
482 size = libc_writev (fd, iov, iovcnt);
489 fcntl_internal (int fd, int cmd, va_list ap)
494 vlsh = ldp_fd_to_vlsh (fd);
495 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
496 if (vlsh != VLS_INVALID_HANDLE)
498 int flags = va_arg (ap, int);
501 size = sizeof (flags);
506 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
510 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
515 /* TODO handle this */
516 LDBG (0, "F_SETFD ignored flags %u", flags);
532 rv = libc_vfcntl64 (fd, cmd, ap);
534 rv = libc_vfcntl (fd, cmd, ap);
542 fcntl (int fd, int cmd, ...)
550 rv = fcntl_internal (fd, cmd, ap);
557 fcntl64 (int fd, int cmd, ...)
565 rv = fcntl_internal (fd, cmd, ap);
571 ioctl (int fd, unsigned long int cmd, ...)
581 vlsh = ldp_fd_to_vlsh (fd);
582 if (vlsh != VLS_INVALID_HANDLE)
587 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
592 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
593 u32 size = sizeof (flags);
595 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
596 * non-blocking, the flags should be read here and merged
599 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
615 rv = libc_vioctl (fd, cmd, ap);
623 ldp_select_init_maps (fd_set * __restrict original,
624 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
625 clib_bitmap_t ** vclb, int nfds, u32 minbits,
626 u32 n_bytes, uword * si_bits, uword * libc_bits)
628 uword si_bits_set, libc_bits_set;
632 clib_bitmap_validate (*vclb, minbits);
633 clib_bitmap_validate (*libcb, minbits);
634 clib_bitmap_validate (*resultb, minbits);
635 clib_memcpy_fast (*resultb, original, n_bytes);
636 memset (original, 0, n_bytes);
639 clib_bitmap_foreach (fd, *resultb) {
642 vlsh = ldp_fd_to_vlsh (fd);
643 if (vlsh == VLS_INVALID_HANDLE)
644 clib_bitmap_set_no_check (*libcb, fd, 1);
646 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
650 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
651 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
652 clib_bitmap_validate (*resultb, *si_bits);
654 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
655 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
659 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
669 clib_bitmap_foreach (si, vclb) {
670 vlsh = vls_session_index_to_vlsh (si);
671 ASSERT (vlsh != VLS_INVALID_HANDLE);
672 fd = ldp_vlsh_to_fd (vlsh);
673 if (PREDICT_FALSE (fd < 0))
686 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
694 clib_bitmap_foreach (fd, result)
695 FD_SET ((int)fd, libcb);
700 ldp_pselect (int nfds, fd_set * __restrict readfds,
701 fd_set * __restrict writefds,
702 fd_set * __restrict exceptfds,
703 const struct timespec *__restrict timeout,
704 const __sigset_t * __restrict sigmask)
706 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
707 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
708 struct timespec libc_tspec = { 0 };
709 f64 time_out, vcl_timeout = 0;
710 uword si_bits, libc_bits;
711 int rv, bits_set = 0;
719 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
720 clib_time_init (&ldpw->clib_time);
724 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
725 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
727 time_out += clib_time_now (&ldpw->clib_time);
729 /* select as fine grained sleep */
732 while (clib_time_now (&ldpw->clib_time) < time_out)
745 if (nfds <= ldp->vlsh_bit_val)
747 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
752 si_bits = libc_bits = 0;
753 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
756 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
757 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
758 &si_bits, &libc_bits);
760 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
761 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
762 minbits, n_bytes, &si_bits, &libc_bits);
764 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
765 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
766 minbits, n_bytes, &si_bits, &libc_bits);
768 if (PREDICT_FALSE (!si_bits && !libc_bits))
776 libc_tspec = timeout ? *timeout : libc_tspec;
783 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
784 vec_len (ldpw->si_rd_bitmap) *
785 sizeof (clib_bitmap_t));
787 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
788 vec_len (ldpw->si_wr_bitmap) *
789 sizeof (clib_bitmap_t));
791 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
792 vec_len (ldpw->si_ex_bitmap) *
793 sizeof (clib_bitmap_t));
795 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
796 writefds ? ldpw->wr_bitmap : NULL,
797 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
806 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
812 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
818 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
829 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
830 vec_len (ldpw->libc_rd_bitmap) *
831 sizeof (clib_bitmap_t));
833 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
834 vec_len (ldpw->libc_wr_bitmap) *
835 sizeof (clib_bitmap_t));
837 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
838 vec_len (ldpw->libc_ex_bitmap) *
839 sizeof (clib_bitmap_t));
841 rv = libc_pselect (libc_bits,
842 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
843 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
844 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
845 &libc_tspec, sigmask);
848 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
849 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
850 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
861 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
865 /* TBD: set timeout to amount of time left */
866 clib_bitmap_zero (ldpw->rd_bitmap);
867 clib_bitmap_zero (ldpw->si_rd_bitmap);
868 clib_bitmap_zero (ldpw->libc_rd_bitmap);
869 clib_bitmap_zero (ldpw->wr_bitmap);
870 clib_bitmap_zero (ldpw->si_wr_bitmap);
871 clib_bitmap_zero (ldpw->libc_wr_bitmap);
872 clib_bitmap_zero (ldpw->ex_bitmap);
873 clib_bitmap_zero (ldpw->si_ex_bitmap);
874 clib_bitmap_zero (ldpw->libc_ex_bitmap);
880 select (int nfds, fd_set * __restrict readfds,
881 fd_set * __restrict writefds,
882 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
884 struct timespec tspec;
888 tspec.tv_sec = timeout->tv_sec;
889 tspec.tv_nsec = timeout->tv_usec * 1000;
891 return ldp_pselect (nfds, readfds, writefds, exceptfds,
892 timeout ? &tspec : NULL, NULL);
897 pselect (int nfds, fd_set * __restrict readfds,
898 fd_set * __restrict writefds,
899 fd_set * __restrict exceptfds,
900 const struct timespec *__restrict timeout,
901 const __sigset_t * __restrict sigmask)
903 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
907 /* If transparent TLS mode is turned on, then ldp will load key and cert.
910 load_cert_key_pair (void)
912 char *cert_str = getenv (LDP_ENV_TLS_CERT);
913 char *key_str = getenv (LDP_ENV_TLS_KEY);
914 char cert_buf[4096], key_buf[4096];
915 int cert_size, key_size;
916 vppcom_cert_key_pair_t crypto;
920 if (!cert_str || !key_str)
922 LDBG (0, "ERROR: failed to read LDP environment %s\n",
927 fp = fopen (cert_str, "r");
930 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
933 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
936 fp = fopen (key_str, "r");
939 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
942 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
945 crypto.cert = cert_buf;
946 crypto.key = key_buf;
947 crypto.cert_len = cert_size;
948 crypto.key_len = key_size;
949 ckp_index = vppcom_add_cert_key_pair (&crypto);
952 LDBG (0, "ERROR: failed to add cert key pair\n");
956 ldp->ckpair_index = ckp_index;
962 assign_cert_key_pair (vls_handle_t vlsh)
966 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
969 ckp_len = sizeof (ldp->ckpair_index);
970 return vppcom_session_attr (vlsh_to_session_index (vlsh),
971 VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index,
976 socket (int domain, int type, int protocol)
978 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
979 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
984 if (((domain == AF_INET) || (domain == AF_INET6)) &&
985 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
988 if (ldp->transparent_tls)
990 proto = VPPCOM_PROTO_TLS;
993 proto = ((sock_type == SOCK_DGRAM) ?
994 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
996 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
997 proto, vppcom_proto_str (proto), is_nonblocking);
999 vlsh = vls_create (proto, is_nonblocking);
1007 if (ldp->transparent_tls)
1009 if (assign_cert_key_pair (vlsh) < 0)
1012 rv = ldp_vlsh_to_fd (vlsh);
1017 LDBG (0, "calling libc_socket");
1018 rv = libc_socket (domain, type, protocol);
1025 * Create two new sockets, of type TYPE in domain DOMAIN and using
1026 * protocol PROTOCOL, which are connected to each other, and put file
1027 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1028 * one will be chosen automatically.
1029 * Returns 0 on success, -1 for errors.
1032 socketpair (int domain, int type, int protocol, int fds[2])
1034 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1038 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1039 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1041 LDBG (0, "LDP-TBD");
1047 LDBG (1, "calling libc_socketpair");
1048 rv = libc_socketpair (domain, type, protocol, fds);
1055 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1062 vlsh = ldp_fd_to_vlsh (fd);
1063 if (vlsh != VLS_INVALID_HANDLE)
1067 switch (addr->sa_family)
1070 if (len != sizeof (struct sockaddr_in))
1072 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1078 ep.is_ip4 = VPPCOM_IS_IP4;
1079 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1080 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1084 if (len != sizeof (struct sockaddr_in6))
1086 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1092 ep.is_ip4 = VPPCOM_IS_IP6;
1093 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1094 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1098 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1099 fd, vlsh, addr->sa_family);
1100 errno = EAFNOSUPPORT;
1104 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1107 rv = vls_bind (vlsh, &ep);
1108 if (rv != VPPCOM_OK)
1116 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1117 rv = libc_bind (fd, addr, len);
1121 LDBG (1, "fd %d: returning %d", fd, rv);
1127 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1128 vppcom_endpt_t * ep)
1131 int sa_len, copy_len;
1135 if (addr && len && ep)
1137 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1138 switch (addr->sa_family)
1141 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1142 if (*len > sizeof (struct sockaddr_in))
1143 *len = sizeof (struct sockaddr_in);
1144 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1145 copy_len = *len - sa_len;
1147 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1152 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1153 if (*len > sizeof (struct sockaddr_in6))
1154 *len = sizeof (struct sockaddr_in6);
1155 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1156 copy_len = *len - sa_len;
1158 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1159 __in6_u.__u6_addr8, ep->ip, copy_len);
1172 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1179 vlsh = ldp_fd_to_vlsh (fd);
1180 if (vlsh != VLS_INVALID_HANDLE)
1183 u8 addr_buf[sizeof (struct in6_addr)];
1184 u32 size = sizeof (ep);
1188 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1189 if (rv != VPPCOM_OK)
1196 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1197 if (rv != VPPCOM_OK)
1206 rv = libc_getsockname (fd, addr, len);
1213 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1222 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1228 vlsh = ldp_fd_to_vlsh (fd);
1229 if (vlsh != VLS_INVALID_HANDLE)
1233 switch (addr->sa_family)
1236 if (len != sizeof (struct sockaddr_in))
1238 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1244 ep.is_ip4 = VPPCOM_IS_IP4;
1245 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1246 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1250 if (len != sizeof (struct sockaddr_in6))
1252 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1258 ep.is_ip4 = VPPCOM_IS_IP6;
1259 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1260 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1264 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1265 fd, vlsh, addr->sa_family);
1266 errno = EAFNOSUPPORT;
1270 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1273 rv = vls_connect (vlsh, &ep);
1274 if (rv != VPPCOM_OK)
1282 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1285 rv = libc_connect (fd, addr, len);
1289 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1294 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1301 vlsh = ldp_fd_to_vlsh (fd);
1302 if (vlsh != VLS_INVALID_HANDLE)
1305 u8 addr_buf[sizeof (struct in6_addr)];
1306 u32 size = sizeof (ep);
1309 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1310 if (rv != VPPCOM_OK)
1317 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1318 if (rv != VPPCOM_OK)
1327 rv = libc_getpeername (fd, addr, len);
1334 send (int fd, const void *buf, size_t n, int flags)
1336 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1341 if (vlsh != VLS_INVALID_HANDLE)
1343 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1344 if (size < VPPCOM_OK)
1352 size = libc_send (fd, buf, n, flags);
1359 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1361 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1367 vlsh = ldp_fd_to_vlsh (out_fd);
1368 if (vlsh != VLS_INVALID_HANDLE)
1371 ssize_t results = 0;
1372 size_t n_bytes_left = len;
1373 size_t bytes_to_read;
1376 u32 flags, flags_len = sizeof (flags);
1378 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1379 if (PREDICT_FALSE (rv != VPPCOM_OK))
1381 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1382 out_fd, vlsh, rv, vppcom_retval_str (rv));
1384 vec_reset_length (ldpw->io_buffer);
1392 off_t off = lseek (in_fd, *offset, SEEK_SET);
1393 if (PREDICT_FALSE (off == -1))
1399 ASSERT (off == *offset);
1404 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1407 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
1408 out_fd, vlsh, size, vppcom_retval_str (size));
1409 vec_reset_length (ldpw->io_buffer);
1415 bytes_to_read = size;
1416 if (bytes_to_read == 0)
1418 if (flags & O_NONBLOCK)
1427 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1428 vec_validate (ldpw->io_buffer, bytes_to_read);
1429 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1434 vec_reset_length (ldpw->io_buffer);
1441 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1444 if (size == VPPCOM_EAGAIN)
1446 if (flags & O_NONBLOCK)
1457 vec_reset_length (ldpw->io_buffer);
1466 ASSERT (n_bytes_left >= nbytes);
1467 n_bytes_left = n_bytes_left - nbytes;
1469 while (n_bytes_left > 0);
1472 vec_reset_length (ldpw->io_buffer);
1475 off_t off = lseek (in_fd, *offset, SEEK_SET);
1476 if (PREDICT_FALSE (off == -1))
1482 ASSERT (off == *offset);
1483 *offset += results + 1;
1495 size = libc_sendfile (out_fd, in_fd, offset, len);
1503 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1505 return sendfile (out_fd, in_fd, offset, len);
1509 recv (int fd, void *buf, size_t n, int flags)
1516 vlsh = ldp_fd_to_vlsh (fd);
1517 if (vlsh != VLS_INVALID_HANDLE)
1519 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1528 size = libc_recv (fd, buf, n, flags);
1535 __recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1540 return recv (fd, buf, n, flags);
1544 ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1545 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1547 vppcom_endpt_t *ep = 0;
1553 switch (addr->sa_family)
1556 ep->is_ip4 = VPPCOM_IS_IP4;
1558 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1559 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1563 ep->is_ip4 = VPPCOM_IS_IP6;
1565 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1567 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1571 return EAFNOSUPPORT;
1575 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1579 ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1580 int flags, __SOCKADDR_ARG addr,
1581 socklen_t * __restrict addr_len)
1583 u8 src_addr[sizeof (struct sockaddr_in6)];
1591 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1595 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1601 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1607 sendto (int fd, const void *buf, size_t n, int flags,
1608 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1615 vlsh = ldp_fd_to_vlsh (fd);
1616 if (vlsh != VLS_INVALID_HANDLE)
1618 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
1627 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1634 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1635 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1642 vlsh = ldp_fd_to_vlsh (fd);
1643 if (vlsh != VLS_INVALID_HANDLE)
1645 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
1654 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1661 sendmsg (int fd, const struct msghdr * msg, int flags)
1668 vlsh = ldp_fd_to_vlsh (fd);
1669 if (vlsh != VLS_INVALID_HANDLE)
1671 struct iovec *iov = msg->msg_iov;
1675 for (i = 0; i < msg->msg_iovlen; ++i)
1677 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1678 msg->msg_name, msg->msg_namelen);
1684 if (rv < iov[i].iov_len)
1689 if (rv < 0 && total == 0)
1699 size = libc_sendmsg (fd, msg, flags);
1707 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1710 const char *func_str;
1711 u32 sh = ldp_fd_to_vlsh (fd);
1715 if (sh != VLS_INVALID_HANDLE)
1717 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1723 func_str = "libc_sendmmsg";
1726 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1727 "vmessages %p, vlen %u, flags 0x%x",
1728 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1730 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1737 int errno_val = errno;
1739 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1740 "rv %d, errno = %d", getpid (), fd, fd,
1741 func_str, size, errno_val);
1745 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1746 getpid (), fd, fd, size, size);
1753 recvmsg (int fd, struct msghdr * msg, int flags)
1760 vlsh = ldp_fd_to_vlsh (fd);
1761 if (vlsh != VLS_INVALID_HANDLE)
1763 struct iovec *iov = msg->msg_iov;
1764 ssize_t max_deq, total = 0;
1767 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1771 for (i = 0; i < msg->msg_iovlen; i++)
1773 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1774 (i == 0 ? msg->msg_name : NULL),
1775 (i == 0 ? &msg->msg_namelen : NULL));
1781 if (rv < iov[i].iov_len)
1784 if (total >= max_deq)
1788 if (rv < 0 && total == 0)
1798 size = libc_recvmsg (fd, msg, flags);
1806 recvmmsg (int fd, struct mmsghdr *vmessages,
1807 unsigned int vlen, int flags, struct timespec *tmo)
1810 const char *func_str;
1811 u32 sh = ldp_fd_to_vlsh (fd);
1815 if (sh != VLS_INVALID_HANDLE)
1817 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1823 func_str = "libc_recvmmsg";
1826 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1827 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1828 getpid (), fd, fd, func_str, vmessages, vlen,
1831 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1838 int errno_val = errno;
1840 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1841 "rv %d, errno = %d", getpid (), fd, fd,
1842 func_str, size, errno_val);
1846 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1847 getpid (), fd, fd, size, size);
1854 getsockopt (int fd, int level, int optname,
1855 void *__restrict optval, socklen_t * __restrict optlen)
1862 vlsh = ldp_fd_to_vlsh (fd);
1863 if (vlsh != VLS_INVALID_HANDLE)
1873 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1877 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1881 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1885 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1889 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1891 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1892 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
1893 memset (optval, 0, *optlen);
1899 case TCP_CONGESTION:
1900 *optlen = strlen ("cubic");
1901 strncpy (optval, "cubic", *optlen + 1);
1905 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1906 "optname %d unsupported!", fd, vlsh, optname);
1914 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
1917 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1918 "optname %d unsupported!", fd, vlsh, optname);
1926 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
1929 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
1932 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
1933 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1936 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1940 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1944 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
1947 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1950 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
1953 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1956 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
1958 case SO_BINDTODEVICE:
1962 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1963 "optname %d unsupported!", fd, vlsh, optname);
1971 if (rv != VPPCOM_OK)
1979 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1986 setsockopt (int fd, int level, int optname,
1987 const void *optval, socklen_t optlen)
1994 vlsh = ldp_fd_to_vlsh (fd);
1995 if (vlsh != VLS_INVALID_HANDLE)
2005 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2006 (void *) optval, &optlen);
2009 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2010 (void *) optval, &optlen);
2013 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2014 (void *) optval, &optlen);
2017 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2018 (void *) optval, &optlen);
2020 case TCP_CONGESTION:
2026 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2027 "optname %d unsupported!", fd, vlsh, optname);
2035 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2036 (void *) optval, &optlen);
2039 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2040 "optname %d unsupported!", fd, vlsh, optname);
2048 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2049 (void *) optval, &optlen);
2052 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2053 (void *) optval, &optlen);
2056 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2060 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2061 (void *) optval, &optlen);
2067 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2068 "optname %d unsupported!", fd, vlsh, optname);
2076 if (rv != VPPCOM_OK)
2084 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2091 listen (int fd, int n)
2098 vlsh = ldp_fd_to_vlsh (fd);
2099 if (vlsh != VLS_INVALID_HANDLE)
2101 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2103 rv = vls_listen (vlsh, n);
2104 if (rv != VPPCOM_OK)
2112 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2113 rv = libc_listen (fd, n);
2116 LDBG (1, "fd %d: returning %d", fd, rv);
2121 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2122 socklen_t * __restrict addr_len, int flags)
2124 vls_handle_t listen_vlsh, accept_vlsh;
2129 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2130 if (listen_vlsh != VLS_INVALID_HANDLE)
2133 u8 src_addr[sizeof (struct sockaddr_in6)];
2134 memset (&ep, 0, sizeof (ep));
2137 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2138 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
2140 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2141 if (accept_vlsh < 0)
2143 errno = -accept_vlsh;
2148 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2149 if (rv != VPPCOM_OK)
2151 (void) vls_close (accept_vlsh);
2157 rv = ldp_vlsh_to_fd (accept_vlsh);
2163 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2164 " flags 0x%x", listen_fd, addr, addr_len, flags);
2166 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2169 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2175 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2178 return ldp_accept4 (fd, addr, addr_len, flags);
2182 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2184 return ldp_accept4 (fd, addr, addr_len, 0);
2188 shutdown (int fd, int how)
2195 vlsh = ldp_fd_to_vlsh (fd);
2196 if (vlsh != VLS_INVALID_HANDLE)
2198 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2199 rv = vls_shutdown (vlsh, how);
2203 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2204 rv = libc_shutdown (fd, how);
2211 epoll_create1 (int flags)
2213 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2219 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
2221 /* Make sure workers have been allocated */
2224 ldp_alloc_workers ();
2225 ldpw = ldp_worker_get_current ();
2227 rv = libc_epoll_create1 (flags);
2228 ldp->vcl_needs_real_epoll = 0;
2229 ldpw->vcl_mq_epfd = rv;
2230 LDBG (0, "created vcl epfd %u", rv);
2234 vlsh = vls_epoll_create ();
2235 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2242 rv = ldp_vlsh_to_fd (vlsh);
2244 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2249 epoll_create (int size)
2251 return epoll_create1 (0);
2255 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2257 vls_handle_t vep_vlsh, vlsh;
2262 vep_vlsh = ldp_fd_to_vlsh (epfd);
2263 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2265 /* The LDP epoll_create1 always creates VCL epfd's.
2266 * The app should never have a kernel base epoll fd unless it
2267 * was acquired outside of the LD_PRELOAD process context.
2268 * In any case, if we get one, punt it to libc_epoll_ctl.
2270 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2271 " event %p", epfd, op, fd, event);
2273 rv = libc_epoll_ctl (epfd, op, fd, event);
2277 vlsh = ldp_fd_to_vlsh (fd);
2279 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2282 if (vlsh != VLS_INVALID_HANDLE)
2284 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2285 " event %p", epfd, vep_vlsh, op, vlsh, event);
2287 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2288 if (rv != VPPCOM_OK)
2297 u32 size = sizeof (epfd);
2299 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2302 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2303 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2305 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2312 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2321 else if (PREDICT_FALSE (libc_epfd < 0))
2328 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2329 " event %p", epfd, libc_epfd, op, fd, event);
2331 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2339 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2340 int timeout, const sigset_t * sigmask)
2342 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2343 double time_to_wait = (double) 0, max_time;
2344 int libc_epfd, rv = 0;
2345 vls_handle_t ep_vlsh;
2349 if (PREDICT_FALSE (!events || (timeout < -1)))
2355 if (epfd == ldpw->vcl_mq_epfd)
2356 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2358 ep_vlsh = ldp_fd_to_vlsh (epfd);
2359 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2361 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2366 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2367 clib_time_init (&ldpw->clib_time);
2368 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2369 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2371 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2372 if (PREDICT_FALSE (libc_epfd < 0))
2379 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2380 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2381 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2384 if (!ldpw->epoll_wait_vcl)
2386 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2389 ldpw->epoll_wait_vcl = 1;
2400 ldpw->epoll_wait_vcl = 0;
2404 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2409 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2416 ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2417 int maxevents, int timeout, const sigset_t * sigmask)
2419 ldp_worker_ctx_t *ldpw;
2420 int libc_epfd, rv = 0, num_ev;
2421 vls_handle_t ep_vlsh;
2425 if (PREDICT_FALSE (!events || (timeout < -1)))
2431 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2432 * one thread but it is now used on another */
2433 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2434 vls_register_vcl_worker ();
2436 ldpw = ldp_worker_get_current ();
2437 if (epfd == ldpw->vcl_mq_epfd)
2438 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2440 ep_vlsh = ldp_fd_to_vlsh (epfd);
2441 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2443 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2448 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2449 if (PREDICT_FALSE (!libc_epfd))
2451 u32 size = sizeof (epfd);
2453 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2454 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2455 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2462 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2470 if (PREDICT_FALSE (libc_epfd <= 0))
2477 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2479 struct epoll_event e = { 0 };
2481 e.data.fd = ldpw->vcl_mq_epfd;
2482 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2485 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2486 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2490 ldpw->mq_epfd_added = 1;
2493 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2494 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
2497 else if (PREDICT_FALSE (rv < 0))
2504 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2507 for (int i = 0; i < rv; i++)
2509 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2511 /* We should remove mq epoll fd from events. */
2515 events[i].events = events[rv].events;
2516 events[i].data.u64 = events[rv].data.u64;
2518 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2519 if (PREDICT_TRUE (num_ev > 0))
2530 epoll_pwait (int epfd, struct epoll_event *events,
2531 int maxevents, int timeout, const sigset_t * sigmask)
2533 if (vls_use_eventfd ())
2534 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2537 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2541 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2543 if (vls_use_eventfd ())
2544 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2546 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2550 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2552 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2553 int rv, i, n_revents = 0;
2558 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
2560 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2561 clib_time_init (&ldpw->clib_time);
2563 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2564 max_time += clib_time_now (&ldpw->clib_time);
2566 for (i = 0; i < nfds; i++)
2571 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2572 if (vlsh != VLS_INVALID_HANDLE)
2574 fds[i].fd = -fds[i].fd;
2575 vec_add2 (ldpw->vcl_poll, vp, 1);
2577 vp->sh = vlsh_to_sh (vlsh);
2578 vp->events = fds[i].events;
2579 #ifdef __USE_XOPEN2K
2580 if (fds[i].events & POLLRDNORM)
2581 vp->events |= POLLIN;
2582 if (fds[i].events & POLLWRNORM)
2583 vp->events |= POLLOUT;
2585 vp->revents = fds[i].revents;
2589 vec_add1 (ldpw->libc_poll, fds[i]);
2590 vec_add1 (ldpw->libc_poll_idxs, i);
2596 if (vec_len (ldpw->vcl_poll))
2598 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2609 if (vec_len (ldpw->libc_poll))
2611 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2624 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2628 vec_foreach (vp, ldpw->vcl_poll)
2630 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2631 fds[vp->fds_ndx].revents = vp->revents;
2632 #ifdef __USE_XOPEN2K
2633 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2634 (fds[vp->fds_ndx].events & POLLRDNORM))
2635 fds[vp->fds_ndx].revents |= POLLRDNORM;
2636 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2637 (fds[vp->fds_ndx].events & POLLWRNORM))
2638 fds[vp->fds_ndx].revents |= POLLWRNORM;
2641 vec_reset_length (ldpw->vcl_poll);
2643 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2645 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2647 vec_reset_length (ldpw->libc_poll_idxs);
2648 vec_reset_length (ldpw->libc_poll);
2655 ppoll (struct pollfd *fds, nfds_t nfds,
2656 const struct timespec *timeout, const sigset_t * sigmask)
2660 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2668 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2670 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2673 * This function is called when the library is loaded
2676 ldp_constructor (void)
2678 swrap_constructor ();
2679 if (ldp_init () != 0)
2681 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2685 else if (LDP_DEBUG > 0)
2686 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2690 * This function is called when the library is unloaded
2693 ldp_destructor (void)
2696 swrap_destructor ();
2701 /* Don't use clib_warning() here because that calls writev()
2702 * which will call ldp_init().
2705 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2706 __func__, __LINE__, getpid ());
2711 * fd.io coding-style-patch-verification: ON
2714 * eval: (c-set-style "gnu")