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.
16 #ifdef HAVE_GNU_SOURCE
27 #include <sys/resource.h>
28 #include <netinet/tcp.h>
29 #include <netinet/udp.h>
31 #include <vcl/ldp_socket_wrapper.h>
35 #include <vcl/vcl_locked.h>
36 #include <vppinfra/time.h>
37 #include <vppinfra/bitmap.h>
38 #include <vppinfra/lock.h>
39 #include <vppinfra/pool.h>
40 #include <vppinfra/hash.h>
42 #define HAVE_CONSTRUCTOR_ATTRIBUTE
43 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
44 #define CONSTRUCTOR_ATTRIBUTE \
45 __attribute__ ((constructor))
47 #define CONSTRUCTOR_ATTRIBUTE
48 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
50 #define HAVE_DESTRUCTOR_ATTRIBUTE
51 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
52 #define DESTRUCTOR_ATTRIBUTE \
53 __attribute__ ((destructor))
55 #define DESTRUCTOR_ATTRIBUTE
58 #define LDP_MAX_NWORKERS 32
60 #ifdef HAVE_GNU_SOURCE
61 #define SOCKADDR_GET_SA(__addr) __addr.__sockaddr__;
63 #define SOCKADDR_GET_SA(__addr) _addr;
66 typedef struct ldp_worker_ctx_
69 clib_time_t clib_time;
74 clib_bitmap_t *rd_bitmap;
75 clib_bitmap_t *wr_bitmap;
76 clib_bitmap_t *ex_bitmap;
77 clib_bitmap_t *si_rd_bitmap;
78 clib_bitmap_t *si_wr_bitmap;
79 clib_bitmap_t *si_ex_bitmap;
80 clib_bitmap_t *libc_rd_bitmap;
81 clib_bitmap_t *libc_wr_bitmap;
82 clib_bitmap_t *libc_ex_bitmap;
88 struct pollfd *libc_poll;
100 /* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
101 * they are the same size */
102 STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
103 "ldp bitmap size mismatch");
104 STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
105 "ldp bitmap size mismatch");
109 ldp_worker_ctx_t *workers;
111 char app_name[LDP_APP_NAME_MAX];
116 /** vcl needs next epoll_create to go to libc_epoll */
117 u8 vcl_needs_real_epoll;
120 * crypto state used only for testing
126 #define LDP_DEBUG ldp->debug
128 #define LDBG(_lvl, _fmt, _args...) \
129 if (ldp->debug > _lvl) \
131 int errno_saved = errno; \
132 fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \
133 errno = errno_saved; \
136 static ldp_main_t ldp_main = {
137 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
138 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
139 .debug = LDP_DEBUG_INIT,
140 .transparent_tls = 0,
144 static ldp_main_t *ldp = &ldp_main;
146 static inline ldp_worker_ctx_t *
147 ldp_worker_get_current (void)
149 return (ldp->workers + vppcom_worker_index ());
153 * RETURN: 0 on success or -1 on error.
156 ldp_set_app_name (char *app_name)
158 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
159 "ldp-%d-%s", getpid (), app_name);
165 if (ldp->app_name[0] == '\0')
166 ldp_set_app_name ("app");
168 return ldp->app_name;
172 ldp_vlsh_to_fd (vls_handle_t vlsh)
174 return (vlsh + ldp->vlsh_bit_val);
177 static inline vls_handle_t
178 ldp_fd_to_vlsh (int fd)
180 if (fd < ldp->vlsh_bit_val)
181 return VLS_INVALID_HANDLE;
183 return (fd - ldp->vlsh_bit_val);
187 ldp_alloc_workers (void)
191 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
197 ldp_worker_ctx_t *ldpw;
203 ldp->vcl_needs_real_epoll = 1;
204 rv = vls_app_create (ldp_get_app_name ());
207 ldp->vcl_needs_real_epoll = 0;
208 if (rv == VPPCOM_EEXIST)
210 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
211 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
215 ldp->vcl_needs_real_epoll = 0;
216 ldp_alloc_workers ();
217 ldpw = ldp_worker_get_current ();
219 char *env_var_str = getenv (LDP_ENV_DEBUG);
223 if (sscanf (env_var_str, "%u", &tmp) != 1)
224 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
225 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
230 LDBG (0, "configured LDP debug level (%u) from env var "
231 LDP_ENV_DEBUG "!", ldp->debug);
235 env_var_str = getenv (LDP_ENV_APP_NAME);
238 ldp_set_app_name (env_var_str);
239 LDBG (0, "configured LDP app name (%s) from the env var "
240 LDP_ENV_APP_NAME "!", ldp->app_name);
243 env_var_str = getenv (LDP_ENV_SID_BIT);
247 if (sscanf (env_var_str, "%u", &sb) != 1)
249 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
250 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
251 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
253 else if (sb < LDP_SID_BIT_MIN)
255 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
256 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
258 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
259 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
260 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
261 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
263 else if (sb > LDP_SID_BIT_MAX)
265 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
266 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
268 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
269 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
270 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
271 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
275 ldp->vlsh_bit_val = (1 << sb);
276 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
278 LDBG (0, "configured LDP sid bit (%u) from "
279 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
280 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
283 /* Make sure there are enough bits in the fd set for vcl sessions */
284 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
286 /* Only valid for select/pselect, so just WARNING and not exit */
288 "WARNING: LDP vlsh bit value %d > FD_SETSIZE/2 %d, "
289 "select/pselect not supported now!",
290 ldp->vlsh_bit_val, FD_SETSIZE / 2);
293 env_var_str = getenv (LDP_ENV_TLS_TRANS);
296 ldp->transparent_tls = 1;
300 pool_foreach (ldpw, ldp->workers) {
301 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
305 LDBG (0, "LDP initialization: done!");
310 #define ldp_init_check() \
311 if (PREDICT_FALSE (!ldp->init)) \
313 if ((errno = -ldp_init ())) \
325 vlsh = ldp_fd_to_vlsh (fd);
326 if (vlsh != VLS_INVALID_HANDLE)
328 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
331 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
333 rv = libc_close (epfd);
336 u32 size = sizeof (epfd);
339 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
342 else if (PREDICT_FALSE (epfd < 0))
349 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
351 rv = vls_close (vlsh);
360 LDBG (0, "fd %d: calling libc_close", fd);
361 rv = libc_close (fd);
369 read (int fd, void *buf, size_t nbytes)
376 vlsh = ldp_fd_to_vlsh (fd);
377 if (vlsh != VLS_INVALID_HANDLE)
379 size = vls_read (vlsh, buf, nbytes);
388 size = libc_read (fd, buf, nbytes);
395 readv (int fd, const struct iovec * iov, int iovcnt)
397 int rv = 0, i, total = 0;
403 vlsh = ldp_fd_to_vlsh (fd);
404 if (vlsh != VLS_INVALID_HANDLE)
406 for (i = 0; i < iovcnt; ++i)
408 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
414 if (rv < iov[i].iov_len)
418 if (rv < 0 && total == 0)
428 size = libc_readv (fd, iov, iovcnt);
435 write (int fd, const void *buf, size_t nbytes)
442 vlsh = ldp_fd_to_vlsh (fd);
443 if (vlsh != VLS_INVALID_HANDLE)
445 size = vls_write_msg (vlsh, (void *) buf, nbytes);
454 size = libc_write (fd, buf, nbytes);
461 writev (int fd, const struct iovec * iov, int iovcnt)
463 ssize_t size = 0, total = 0;
469 vlsh = ldp_fd_to_vlsh (fd);
470 if (vlsh != VLS_INVALID_HANDLE)
472 for (i = 0; i < iovcnt; ++i)
474 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
480 if (rv < iov[i].iov_len)
485 if (rv < 0 && total == 0)
495 size = libc_writev (fd, iov, iovcnt);
502 fcntl_internal (int fd, int cmd, va_list ap)
507 vlsh = ldp_fd_to_vlsh (fd);
508 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
509 if (vlsh != VLS_INVALID_HANDLE)
511 int flags = va_arg (ap, int);
514 size = sizeof (flags);
519 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
523 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
528 /* TODO handle this */
529 LDBG (0, "F_SETFD ignored flags %u", flags);
545 rv = libc_vfcntl64 (fd, cmd, ap);
547 rv = libc_vfcntl (fd, cmd, ap);
555 fcntl (int fd, int cmd, ...)
563 rv = fcntl_internal (fd, cmd, ap);
570 fcntl64 (int fd, int cmd, ...)
578 rv = fcntl_internal (fd, cmd, ap);
584 ioctl (int fd, unsigned long int cmd, ...)
594 vlsh = ldp_fd_to_vlsh (fd);
595 if (vlsh != VLS_INVALID_HANDLE)
600 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
605 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
606 u32 size = sizeof (flags);
608 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
609 * non-blocking, the flags should be read here and merged
612 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
628 rv = libc_vioctl (fd, cmd, ap);
636 ldp_select_init_maps (fd_set * __restrict original,
637 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
638 clib_bitmap_t ** vclb, int nfds, u32 minbits,
639 u32 n_bytes, uword * si_bits, uword * libc_bits)
641 uword si_bits_set, libc_bits_set;
645 clib_bitmap_validate (*vclb, minbits);
646 clib_bitmap_validate (*libcb, minbits);
647 clib_bitmap_validate (*resultb, minbits);
648 clib_memcpy_fast (*resultb, original, n_bytes);
649 memset (original, 0, n_bytes);
652 clib_bitmap_foreach (fd, *resultb) {
655 vlsh = ldp_fd_to_vlsh (fd);
656 if (vlsh == VLS_INVALID_HANDLE)
657 clib_bitmap_set_no_check (*libcb, fd, 1);
659 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
663 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
664 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
665 clib_bitmap_validate (*resultb, *si_bits);
667 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
668 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
672 ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
682 clib_bitmap_foreach (si, vclb) {
683 vlsh = vls_session_index_to_vlsh (si);
684 ASSERT (vlsh != VLS_INVALID_HANDLE);
685 fd = ldp_vlsh_to_fd (vlsh);
686 if (PREDICT_FALSE (fd < 0))
699 ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
707 clib_bitmap_foreach (fd, result)
708 FD_SET ((int)fd, libcb);
713 ldp_pselect (int nfds, fd_set * __restrict readfds,
714 fd_set * __restrict writefds,
715 fd_set * __restrict exceptfds,
716 const struct timespec *__restrict timeout,
717 const __sigset_t * __restrict sigmask)
719 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
720 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
721 struct timespec libc_tspec = { 0 };
722 f64 time_out, vcl_timeout = 0;
723 uword si_bits, libc_bits;
724 int rv, bits_set = 0;
732 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
733 clib_time_init (&ldpw->clib_time);
737 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
738 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
740 time_out += clib_time_now (&ldpw->clib_time);
742 /* select as fine grained sleep */
745 while (clib_time_now (&ldpw->clib_time) < time_out)
758 if (nfds <= ldp->vlsh_bit_val)
760 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
765 si_bits = libc_bits = 0;
766 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
769 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
770 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
771 &si_bits, &libc_bits);
773 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
774 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
775 minbits, n_bytes, &si_bits, &libc_bits);
777 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
778 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
779 minbits, n_bytes, &si_bits, &libc_bits);
781 if (PREDICT_FALSE (!si_bits && !libc_bits))
789 libc_tspec = timeout ? *timeout : libc_tspec;
796 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
797 vec_len (ldpw->si_rd_bitmap) *
798 sizeof (clib_bitmap_t));
800 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
801 vec_len (ldpw->si_wr_bitmap) *
802 sizeof (clib_bitmap_t));
804 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
805 vec_len (ldpw->si_ex_bitmap) *
806 sizeof (clib_bitmap_t));
808 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
809 writefds ? ldpw->wr_bitmap : NULL,
810 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
819 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
825 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
831 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
842 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
843 vec_len (ldpw->libc_rd_bitmap) *
844 sizeof (clib_bitmap_t));
846 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
847 vec_len (ldpw->libc_wr_bitmap) *
848 sizeof (clib_bitmap_t));
850 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
851 vec_len (ldpw->libc_ex_bitmap) *
852 sizeof (clib_bitmap_t));
854 rv = libc_pselect (libc_bits,
855 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
856 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
857 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
858 &libc_tspec, sigmask);
861 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
862 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
863 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
874 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
878 /* TBD: set timeout to amount of time left */
879 clib_bitmap_zero (ldpw->rd_bitmap);
880 clib_bitmap_zero (ldpw->si_rd_bitmap);
881 clib_bitmap_zero (ldpw->libc_rd_bitmap);
882 clib_bitmap_zero (ldpw->wr_bitmap);
883 clib_bitmap_zero (ldpw->si_wr_bitmap);
884 clib_bitmap_zero (ldpw->libc_wr_bitmap);
885 clib_bitmap_zero (ldpw->ex_bitmap);
886 clib_bitmap_zero (ldpw->si_ex_bitmap);
887 clib_bitmap_zero (ldpw->libc_ex_bitmap);
893 select (int nfds, fd_set * __restrict readfds,
894 fd_set * __restrict writefds,
895 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
897 struct timespec tspec;
901 tspec.tv_sec = timeout->tv_sec;
902 tspec.tv_nsec = timeout->tv_usec * 1000;
904 return ldp_pselect (nfds, readfds, writefds, exceptfds,
905 timeout ? &tspec : NULL, NULL);
910 pselect (int nfds, fd_set * __restrict readfds,
911 fd_set * __restrict writefds,
912 fd_set * __restrict exceptfds,
913 const struct timespec *__restrict timeout,
914 const __sigset_t * __restrict sigmask)
916 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
920 /* If transparent TLS mode is turned on, then ldp will load key and cert.
923 load_cert_key_pair (void)
925 char *cert_str = getenv (LDP_ENV_TLS_CERT);
926 char *key_str = getenv (LDP_ENV_TLS_KEY);
927 char cert_buf[4096], key_buf[4096];
928 int cert_size, key_size;
929 vppcom_cert_key_pair_t crypto;
933 if (!cert_str || !key_str)
935 LDBG (0, "ERROR: failed to read LDP environment %s\n",
940 fp = fopen (cert_str, "r");
943 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
946 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
949 fp = fopen (key_str, "r");
952 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
955 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
958 crypto.cert = cert_buf;
959 crypto.key = key_buf;
960 crypto.cert_len = cert_size;
961 crypto.key_len = key_size;
962 ckp_index = vppcom_add_cert_key_pair (&crypto);
965 LDBG (0, "ERROR: failed to add cert key pair\n");
969 ldp->ckpair_index = ckp_index;
975 assign_cert_key_pair (vls_handle_t vlsh)
979 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
982 ckp_len = sizeof (ldp->ckpair_index);
983 return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &ckp_len);
987 socket (int domain, int type, int protocol)
989 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
990 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
995 if (((domain == AF_INET) || (domain == AF_INET6)) &&
996 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
999 if (ldp->transparent_tls)
1001 proto = VPPCOM_PROTO_TLS;
1004 proto = ((sock_type == SOCK_DGRAM) ?
1005 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1007 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
1008 proto, vppcom_proto_str (proto), is_nonblocking);
1010 vlsh = vls_create (proto, is_nonblocking);
1018 if (ldp->transparent_tls)
1020 if (assign_cert_key_pair (vlsh) < 0)
1023 rv = ldp_vlsh_to_fd (vlsh);
1028 LDBG (0, "calling libc_socket");
1029 rv = libc_socket (domain, type, protocol);
1036 * Create two new sockets, of type TYPE in domain DOMAIN and using
1037 * protocol PROTOCOL, which are connected to each other, and put file
1038 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1039 * one will be chosen automatically.
1040 * Returns 0 on success, -1 for errors.
1043 socketpair (int domain, int type, int protocol, int fds[2])
1045 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1049 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1050 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1052 LDBG (0, "LDP-TBD");
1058 LDBG (1, "calling libc_socketpair");
1059 rv = libc_socketpair (domain, type, protocol, fds);
1066 bind (int fd, __CONST_SOCKADDR_ARG _addr, socklen_t len)
1068 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1074 vlsh = ldp_fd_to_vlsh (fd);
1075 if (vlsh != VLS_INVALID_HANDLE)
1079 switch (addr->sa_family)
1082 if (len != sizeof (struct sockaddr_in))
1084 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1090 ep.is_ip4 = VPPCOM_IS_IP4;
1091 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1092 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1096 if (len != sizeof (struct sockaddr_in6))
1098 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1104 ep.is_ip4 = VPPCOM_IS_IP6;
1105 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1106 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1110 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1111 fd, vlsh, addr->sa_family);
1112 errno = EAFNOSUPPORT;
1116 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1119 rv = vls_bind (vlsh, &ep);
1120 if (rv != VPPCOM_OK)
1128 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
1129 rv = libc_bind (fd, addr, len);
1133 LDBG (1, "fd %d: returning %d", fd, rv);
1139 ldp_copy_ep_to_sockaddr (struct sockaddr *addr, socklen_t *__restrict len,
1142 int rv = 0, sa_len, copy_len;
1146 if (addr && len && ep)
1148 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1149 switch (addr->sa_family)
1152 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1153 if (*len > sizeof (struct sockaddr_in))
1154 *len = sizeof (struct sockaddr_in);
1155 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1156 copy_len = *len - sa_len;
1158 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1163 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1164 if (*len > sizeof (struct sockaddr_in6))
1165 *len = sizeof (struct sockaddr_in6);
1166 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1167 copy_len = *len - sa_len;
1169 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1170 __in6_u.__u6_addr8, ep->ip, copy_len);
1183 getsockname (int fd, __SOCKADDR_ARG _addr, socklen_t *__restrict len)
1185 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1191 vlsh = ldp_fd_to_vlsh (fd);
1192 if (vlsh != VLS_INVALID_HANDLE)
1195 u8 addr_buf[sizeof (struct in6_addr)];
1196 u32 size = sizeof (ep);
1200 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1201 if (rv != VPPCOM_OK)
1208 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1209 if (rv != VPPCOM_OK)
1218 rv = libc_getsockname (fd, _addr, len);
1225 connect (int fd, __CONST_SOCKADDR_ARG _addr, socklen_t len)
1227 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1235 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
1241 vlsh = ldp_fd_to_vlsh (fd);
1242 if (vlsh != VLS_INVALID_HANDLE)
1246 switch (addr->sa_family)
1249 if (len != sizeof (struct sockaddr_in))
1251 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1257 ep.is_ip4 = VPPCOM_IS_IP4;
1258 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1259 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1263 if (len != sizeof (struct sockaddr_in6))
1265 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1271 ep.is_ip4 = VPPCOM_IS_IP6;
1272 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1273 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1277 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1278 fd, vlsh, addr->sa_family);
1279 errno = EAFNOSUPPORT;
1283 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1286 rv = vls_connect (vlsh, &ep);
1287 if (rv != VPPCOM_OK)
1295 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1298 rv = libc_connect (fd, addr, len);
1302 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
1307 getpeername (int fd, __SOCKADDR_ARG _addr, socklen_t *__restrict len)
1309 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1315 vlsh = ldp_fd_to_vlsh (fd);
1316 if (vlsh != VLS_INVALID_HANDLE)
1319 u8 addr_buf[sizeof (struct in6_addr)];
1320 u32 size = sizeof (ep);
1323 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1324 if (rv != VPPCOM_OK)
1331 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1332 if (rv != VPPCOM_OK)
1341 rv = libc_getpeername (fd, addr, len);
1348 send (int fd, const void *buf, size_t n, int flags)
1350 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
1355 if (vlsh != VLS_INVALID_HANDLE)
1357 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
1358 if (size < VPPCOM_OK)
1366 size = libc_send (fd, buf, n, flags);
1373 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1375 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1381 vlsh = ldp_fd_to_vlsh (out_fd);
1382 if (vlsh != VLS_INVALID_HANDLE)
1385 ssize_t results = 0;
1386 size_t n_bytes_left = len;
1387 size_t bytes_to_read;
1390 u32 flags, flags_len = sizeof (flags);
1392 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
1393 if (PREDICT_FALSE (rv != VPPCOM_OK))
1395 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1396 out_fd, vlsh, rv, vppcom_retval_str (rv));
1398 vec_reset_length (ldpw->io_buffer);
1406 off_t off = lseek (in_fd, *offset, SEEK_SET);
1407 if (PREDICT_FALSE (off == -1))
1413 ASSERT (off == *offset);
1418 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1421 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
1422 out_fd, vlsh, size, vppcom_retval_str (size));
1423 vec_reset_length (ldpw->io_buffer);
1429 bytes_to_read = size;
1430 if (bytes_to_read == 0)
1432 if (flags & O_NONBLOCK)
1441 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1442 vec_validate (ldpw->io_buffer, bytes_to_read);
1443 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1448 vec_reset_length (ldpw->io_buffer);
1455 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
1458 if (size == VPPCOM_EAGAIN)
1460 if (flags & O_NONBLOCK)
1471 vec_reset_length (ldpw->io_buffer);
1480 ASSERT (n_bytes_left >= nbytes);
1481 n_bytes_left = n_bytes_left - nbytes;
1483 while (n_bytes_left > 0);
1486 vec_reset_length (ldpw->io_buffer);
1489 off_t off = lseek (in_fd, *offset, SEEK_SET);
1490 if (PREDICT_FALSE (off == -1))
1496 ASSERT (off == *offset);
1497 *offset += results + 1;
1509 size = libc_sendfile (out_fd, in_fd, offset, len);
1517 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1519 return sendfile (out_fd, in_fd, offset, len);
1523 recv (int fd, void *buf, size_t n, int flags)
1530 vlsh = ldp_fd_to_vlsh (fd);
1531 if (vlsh != VLS_INVALID_HANDLE)
1533 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1542 size = libc_recv (fd, buf, n, flags);
1549 __recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1554 return recv (fd, buf, n, flags);
1558 ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n,
1559 vppcom_endpt_tlv_t *app_tlvs, int flags,
1560 __CONST_SOCKADDR_ARG _addr, socklen_t addr_len)
1562 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1563 vppcom_endpt_t *ep = 0;
1566 _ep.app_tlvs = app_tlvs;
1571 switch (addr->sa_family)
1574 ep->is_ip4 = VPPCOM_IS_IP4;
1576 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1577 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1581 ep->is_ip4 = VPPCOM_IS_IP6;
1583 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1585 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1589 return EAFNOSUPPORT;
1593 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1597 ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n, int flags,
1598 __SOCKADDR_ARG _addr, socklen_t *__restrict addr_len)
1600 u8 src_addr[sizeof (struct sockaddr_in6)];
1601 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1609 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1613 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1619 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1625 sendto (int fd, const void *buf, size_t n, int flags,
1626 __CONST_SOCKADDR_ARG _addr, socklen_t addr_len)
1628 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
1634 vlsh = ldp_fd_to_vlsh (fd);
1635 if (vlsh != VLS_INVALID_HANDLE)
1637 size = ldp_vls_sendo (vlsh, buf, n, NULL, flags, addr, addr_len);
1646 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1653 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1654 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1661 vlsh = ldp_fd_to_vlsh (fd);
1662 if (vlsh != VLS_INVALID_HANDLE)
1664 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
1673 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1680 ldp_parse_cmsg (vls_handle_t vlsh, const struct msghdr *msg,
1681 vppcom_endpt_tlv_t **app_tlvs)
1683 uint8_t *ad, *at = (uint8_t *) *app_tlvs;
1684 vppcom_endpt_tlv_t *adh;
1685 struct in_pktinfo *pi;
1686 struct cmsghdr *cmsg;
1688 cmsg = CMSG_FIRSTHDR (msg);
1690 while (cmsg != NULL)
1692 switch (cmsg->cmsg_level)
1695 switch (cmsg->cmsg_type)
1698 vec_add2 (at, adh, sizeof (*adh));
1699 adh->data_type = VCL_UDP_SEGMENT;
1700 adh->data_len = sizeof (uint16_t);
1701 vec_add2 (at, ad, sizeof (uint16_t));
1702 *(uint16_t *) ad = *(uint16_t *) CMSG_DATA (cmsg);
1705 LDBG (1, "SOL_UDP cmsg_type %u not supported", cmsg->cmsg_type);
1710 switch (cmsg->cmsg_type)
1713 vec_add2 (at, adh, sizeof (*adh));
1714 adh->data_type = VCL_IP_PKTINFO;
1715 adh->data_len = sizeof (struct in_addr);
1716 vec_add2 (at, ad, sizeof (struct in_addr));
1717 pi = (void *) CMSG_DATA (cmsg);
1718 clib_memcpy_fast (ad, &pi->ipi_spec_dst,
1719 sizeof (struct in_addr));
1722 LDBG (1, "SOL_IP cmsg_type %u not supported", cmsg->cmsg_type);
1727 LDBG (1, "cmsg_level %u not supported", cmsg->cmsg_level);
1730 cmsg = CMSG_NXTHDR ((struct msghdr *) msg, cmsg);
1732 *app_tlvs = (vppcom_endpt_tlv_t *) at;
1737 ldp_make_cmsg (vls_handle_t vlsh, struct msghdr *msg)
1739 u32 optval, optlen = sizeof (optval);
1740 struct cmsghdr *cmsg;
1742 cmsg = CMSG_FIRSTHDR (msg);
1744 if (!vls_attr (vlsh, VPPCOM_ATTR_GET_IP_PKTINFO, (void *) &optval, &optlen))
1750 u8 addr_buf[sizeof (struct in_addr)];
1751 u32 size = sizeof (ep);
1755 if (!vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size))
1757 struct in_pktinfo pi = {};
1759 clib_memcpy (&pi.ipi_addr, ep.ip, sizeof (struct in_addr));
1760 cmsg->cmsg_level = SOL_IP;
1761 cmsg->cmsg_type = IP_PKTINFO;
1762 cmsg->cmsg_len = CMSG_LEN (sizeof (pi));
1763 clib_memcpy (CMSG_DATA (cmsg), &pi, sizeof (pi));
1771 sendmsg (int fd, const struct msghdr * msg, int flags)
1778 vlsh = ldp_fd_to_vlsh (fd);
1779 if (vlsh != VLS_INVALID_HANDLE)
1781 vppcom_endpt_tlv_t *app_tlvs = 0;
1782 struct iovec *iov = msg->msg_iov;
1786 ldp_parse_cmsg (vlsh, msg, &app_tlvs);
1788 for (i = 0; i < msg->msg_iovlen; ++i)
1790 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, app_tlvs,
1791 flags, msg->msg_name, msg->msg_namelen);
1797 if (rv < iov[i].iov_len)
1802 vec_free (app_tlvs);
1804 if (rv < 0 && total == 0)
1814 size = libc_sendmsg (fd, msg, flags);
1822 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1825 const char *func_str;
1826 u32 sh = ldp_fd_to_vlsh (fd);
1830 if (sh != VLS_INVALID_HANDLE)
1832 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1838 func_str = "libc_sendmmsg";
1841 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1842 "vmessages %p, vlen %u, flags 0x%x",
1843 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1845 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1852 int errno_val = errno;
1854 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1855 "rv %d, errno = %d", getpid (), fd, fd,
1856 func_str, size, errno_val);
1860 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1861 getpid (), fd, fd, size, size);
1868 recvmsg (int fd, struct msghdr * msg, int flags)
1875 vlsh = ldp_fd_to_vlsh (fd);
1876 if (vlsh != VLS_INVALID_HANDLE)
1878 struct iovec *iov = msg->msg_iov;
1879 ssize_t max_deq, total = 0;
1882 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1886 for (i = 0; i < msg->msg_iovlen; i++)
1888 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1889 (i == 0 ? msg->msg_name : NULL),
1890 (i == 0 ? &msg->msg_namelen : NULL));
1896 if (rv < iov[i].iov_len)
1899 if (total >= max_deq)
1903 if (rv < 0 && total == 0)
1910 if (msg->msg_controllen)
1911 ldp_make_cmsg (vlsh, msg);
1917 size = libc_recvmsg (fd, msg, flags);
1925 recvmmsg (int fd, struct mmsghdr *vmessages,
1926 unsigned int vlen, int flags, struct timespec *tmo)
1928 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1933 sh = ldp_fd_to_vlsh (fd);
1935 if (sh != VLS_INVALID_HANDLE)
1942 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
1943 clib_time_init (&ldpw->clib_time);
1946 time_out = (f64) tmo->tv_sec + (f64) tmo->tv_nsec / (f64) 1e9;
1947 time_out += clib_time_now (&ldpw->clib_time);
1951 time_out = (f64) ~0;
1954 while (nvecs < vlen)
1956 mh = &vmessages[nvecs];
1957 rv = recvmsg (fd, &mh->msg_hdr, flags);
1965 if (!time_out || clib_time_now (&ldpw->clib_time) >= time_out)
1971 return nvecs > 0 ? nvecs : rv;
1975 return libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1981 getsockopt (int fd, int level, int optname,
1982 void *__restrict optval, socklen_t * __restrict optlen)
1989 vlsh = ldp_fd_to_vlsh (fd);
1990 if (vlsh != VLS_INVALID_HANDLE)
2000 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
2004 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
2008 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2012 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2016 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2018 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
2019 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
2020 memset (optval, 0, *optlen);
2026 case TCP_CONGESTION:
2027 *optlen = strlen ("cubic");
2028 strncpy (optval, "cubic", *optlen + 1);
2032 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
2033 "optname %d unsupported!", fd, vlsh, optname);
2041 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
2044 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
2045 "optname %d unsupported!", fd, vlsh, optname);
2053 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
2056 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
2059 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
2060 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2063 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2067 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2071 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
2074 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
2077 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
2080 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
2083 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
2085 case SO_BINDTODEVICE:
2089 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
2090 "optname %d unsupported!", fd, vlsh, optname);
2098 if (rv != VPPCOM_OK)
2106 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2113 setsockopt (int fd, int level, int optname,
2114 const void *optval, socklen_t optlen)
2121 vlsh = ldp_fd_to_vlsh (fd);
2122 if (vlsh != VLS_INVALID_HANDLE)
2132 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2133 (void *) optval, &optlen);
2136 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2137 (void *) optval, &optlen);
2140 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2141 (void *) optval, &optlen);
2144 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2145 (void *) optval, &optlen);
2147 case TCP_CONGESTION:
2153 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2154 "optname %d unsupported!", fd, vlsh, optname);
2162 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2163 (void *) optval, &optlen);
2166 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2167 "optname %d unsupported!", fd, vlsh, optname);
2175 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2176 (void *) optval, &optlen);
2179 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2180 (void *) optval, &optlen);
2183 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2187 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2188 (void *) optval, &optlen);
2194 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2195 "optname %d unsupported!", fd, vlsh, optname);
2203 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_IP_PKTINFO, (void *) optval,
2208 "ERROR: fd %d: setsockopt SOL_IP: vlsh %u optname %d"
2218 if (rv != VPPCOM_OK)
2226 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2233 listen (int fd, int n)
2240 vlsh = ldp_fd_to_vlsh (fd);
2241 if (vlsh != VLS_INVALID_HANDLE)
2243 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
2245 rv = vls_listen (vlsh, n);
2246 if (rv != VPPCOM_OK)
2254 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
2255 rv = libc_listen (fd, n);
2258 LDBG (1, "fd %d: returning %d", fd, rv);
2263 ldp_accept4 (int listen_fd, __SOCKADDR_ARG _addr,
2264 socklen_t *__restrict addr_len, int flags)
2266 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
2267 vls_handle_t listen_vlsh, accept_vlsh;
2272 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2273 if (listen_vlsh != VLS_INVALID_HANDLE)
2276 u8 src_addr[sizeof (struct sockaddr_in6)];
2277 memset (&ep, 0, sizeof (ep));
2280 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2281 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
2283 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2284 if (accept_vlsh < 0)
2286 errno = -accept_vlsh;
2291 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2292 if (rv != VPPCOM_OK)
2294 (void) vls_close (accept_vlsh);
2300 rv = ldp_vlsh_to_fd (accept_vlsh);
2306 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2307 " flags 0x%x", listen_fd, addr, addr_len, flags);
2309 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2312 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
2318 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2321 return ldp_accept4 (fd, addr, addr_len, flags);
2325 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2327 return ldp_accept4 (fd, addr, addr_len, 0);
2331 shutdown (int fd, int how)
2338 vlsh = ldp_fd_to_vlsh (fd);
2339 if (vlsh != VLS_INVALID_HANDLE)
2341 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
2342 rv = vls_shutdown (vlsh, how);
2346 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
2347 rv = libc_shutdown (fd, how);
2354 epoll_create1 (int flags)
2356 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2362 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
2364 /* Make sure workers have been allocated */
2367 ldp_alloc_workers ();
2368 ldpw = ldp_worker_get_current ();
2370 rv = libc_epoll_create1 (flags);
2371 ldp->vcl_needs_real_epoll = 0;
2372 ldpw->vcl_mq_epfd = rv;
2373 LDBG (0, "created vcl epfd %u", rv);
2377 vlsh = vls_epoll_create ();
2378 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
2385 rv = ldp_vlsh_to_fd (vlsh);
2387 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
2392 epoll_create (int size)
2394 return epoll_create1 (0);
2398 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2400 vls_handle_t vep_vlsh, vlsh;
2405 vep_vlsh = ldp_fd_to_vlsh (epfd);
2406 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
2408 /* The LDP epoll_create1 always creates VCL epfd's.
2409 * The app should never have a kernel base epoll fd unless it
2410 * was acquired outside of the LD_PRELOAD process context.
2411 * In any case, if we get one, punt it to libc_epoll_ctl.
2413 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2414 " event %p", epfd, op, fd, event);
2416 rv = libc_epoll_ctl (epfd, op, fd, event);
2420 vlsh = ldp_fd_to_vlsh (fd);
2422 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2425 if (vlsh != VLS_INVALID_HANDLE)
2427 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2428 " event %p", epfd, vep_vlsh, op, vlsh, event);
2430 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
2431 if (rv != VPPCOM_OK)
2440 u32 size = sizeof (epfd);
2442 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2445 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2446 "EPOLL_CLOEXEC", epfd, vep_vlsh);
2448 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2455 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2464 else if (PREDICT_FALSE (libc_epfd < 0))
2471 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2472 " event %p", epfd, libc_epfd, op, fd, event);
2474 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2482 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2483 int timeout, const sigset_t * sigmask)
2485 ldp_worker_ctx_t *ldpw;
2486 double time_to_wait = (double) 0, max_time;
2487 int libc_epfd, rv = 0;
2488 vls_handle_t ep_vlsh;
2492 if (PREDICT_FALSE (!events || (timeout < -1)))
2498 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2499 vls_register_vcl_worker ();
2501 ldpw = ldp_worker_get_current ();
2502 if (epfd == ldpw->vcl_mq_epfd)
2503 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2505 ep_vlsh = ldp_fd_to_vlsh (epfd);
2506 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2508 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2513 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2514 clib_time_init (&ldpw->clib_time);
2515 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
2516 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
2518 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2519 if (PREDICT_FALSE (libc_epfd < 0))
2526 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2527 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
2528 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
2531 if (!ldpw->epoll_wait_vcl)
2533 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2536 ldpw->epoll_wait_vcl = 1;
2547 ldpw->epoll_wait_vcl = 0;
2551 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
2556 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
2563 ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2564 int maxevents, int timeout, const sigset_t * sigmask)
2566 ldp_worker_ctx_t *ldpw;
2567 int libc_epfd, rv = 0, num_ev;
2568 vls_handle_t ep_vlsh;
2572 if (PREDICT_FALSE (!events || (timeout < -1)))
2578 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2579 * one thread but it is now used on another */
2580 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2581 vls_register_vcl_worker ();
2583 ldpw = ldp_worker_get_current ();
2584 if (epfd == ldpw->vcl_mq_epfd)
2585 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2587 ep_vlsh = ldp_fd_to_vlsh (epfd);
2588 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2590 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2595 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2596 if (PREDICT_FALSE (!libc_epfd))
2598 u32 size = sizeof (epfd);
2600 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2601 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2602 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2609 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2617 if (PREDICT_FALSE (libc_epfd <= 0))
2624 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2626 struct epoll_event e = { 0 };
2628 e.data.fd = ldpw->vcl_mq_epfd;
2629 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2632 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2633 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2637 ldpw->mq_epfd_added = 1;
2640 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2641 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
2644 else if (PREDICT_FALSE (rv < 0))
2651 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2654 for (int i = 0; i < rv; i++)
2656 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2658 /* We should remove mq epoll fd from events. */
2662 events[i].events = events[rv].events;
2663 events[i].data.u64 = events[rv].data.u64;
2665 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2666 if (PREDICT_TRUE (num_ev > 0))
2677 epoll_pwait (int epfd, struct epoll_event *events,
2678 int maxevents, int timeout, const sigset_t * sigmask)
2680 if (vls_use_eventfd ())
2681 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2684 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2688 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2690 if (vls_use_eventfd ())
2691 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2693 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
2697 poll (struct pollfd *fds, nfds_t nfds, int timeout)
2699 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2700 int rv, i, n_revents = 0;
2705 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
2707 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2708 clib_time_init (&ldpw->clib_time);
2710 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2711 max_time += clib_time_now (&ldpw->clib_time);
2713 for (i = 0; i < nfds; i++)
2718 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2719 if (vlsh != VLS_INVALID_HANDLE)
2721 fds[i].fd = -fds[i].fd;
2722 vec_add2 (ldpw->vcl_poll, vp, 1);
2724 vp->sh = vlsh_to_sh (vlsh);
2725 vp->events = fds[i].events;
2726 #ifdef __USE_XOPEN2K
2727 if (fds[i].events & POLLRDNORM)
2728 vp->events |= POLLIN;
2729 if (fds[i].events & POLLWRNORM)
2730 vp->events |= POLLOUT;
2732 vp->revents = fds[i].revents;
2736 vec_add1 (ldpw->libc_poll, fds[i]);
2737 vec_add1 (ldpw->libc_poll_idxs, i);
2743 if (vec_len (ldpw->vcl_poll))
2745 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
2756 if (vec_len (ldpw->libc_poll))
2758 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
2771 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
2775 vec_foreach (vp, ldpw->vcl_poll)
2777 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
2778 fds[vp->fds_ndx].revents = vp->revents;
2779 #ifdef __USE_XOPEN2K
2780 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2781 (fds[vp->fds_ndx].events & POLLRDNORM))
2782 fds[vp->fds_ndx].revents |= POLLRDNORM;
2783 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2784 (fds[vp->fds_ndx].events & POLLWRNORM))
2785 fds[vp->fds_ndx].revents |= POLLWRNORM;
2788 vec_reset_length (ldpw->vcl_poll);
2790 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
2792 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
2794 vec_reset_length (ldpw->libc_poll_idxs);
2795 vec_reset_length (ldpw->libc_poll);
2802 ppoll (struct pollfd *fds, nfds_t nfds,
2803 const struct timespec *timeout, const sigset_t * sigmask)
2807 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2815 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
2817 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
2820 * This function is called when the library is loaded
2823 ldp_constructor (void)
2825 swrap_constructor ();
2826 if (ldp_init () != 0)
2828 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2832 else if (LDP_DEBUG > 0)
2833 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
2837 * This function is called when the library is unloaded
2840 ldp_destructor (void)
2843 swrap_destructor ();
2848 /* Don't use clib_warning() here because that calls writev()
2849 * which will call ldp_init().
2852 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2853 __func__, __LINE__, getpid ());
2858 * fd.io coding-style-patch-verification: ON
2861 * eval: (c-set-style "gnu")