2 * Copyright (c) 2016 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/vppcom.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_fd_entry_
61 typedef struct ldp_worker_ctx_
64 clib_time_t clib_time;
69 clib_bitmap_t *rd_bitmap;
70 clib_bitmap_t *wr_bitmap;
71 clib_bitmap_t *ex_bitmap;
72 clib_bitmap_t *sid_rd_bitmap;
73 clib_bitmap_t *sid_wr_bitmap;
74 clib_bitmap_t *sid_ex_bitmap;
75 clib_bitmap_t *libc_rd_bitmap;
76 clib_bitmap_t *libc_wr_bitmap;
77 clib_bitmap_t *libc_ex_bitmap;
84 struct pollfd *libc_poll;
97 ldp_worker_ctx_t *workers;
99 char app_name[LDP_APP_NAME_MAX];
103 ldp_fd_entry_t *fd_pool;
104 clib_rwlock_t fd_table_lock;
105 uword *session_index_to_fd_table;
107 /** vcl needs next epoll_create to go to libc_epoll */
108 u8 vcl_needs_real_epoll;
111 #define LDP_DEBUG ldp->debug
113 #define LDBG(_lvl, _fmt, _args...) \
114 if (ldp->debug > _lvl) \
115 clib_warning (_fmt, ##_args)
117 static ldp_main_t ldp_main = {
118 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
119 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
120 .debug = LDP_DEBUG_INIT,
123 static ldp_main_t *ldp = &ldp_main;
125 static inline ldp_worker_ctx_t *
126 ldp_worker_get_current (void)
128 return (ldp->workers + vppcom_worker_index ());
132 * RETURN: 0 on success or -1 on error.
135 ldp_set_app_name (char *app_name)
137 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
138 "ldp-%d-%s", getpid (), app_name);
140 if (rv >= LDP_APP_NAME_MAX)
141 app_name[LDP_APP_NAME_MAX - 1] = 0;
147 if (ldp->app_name[0] == '\0')
148 ldp_set_app_name ("app");
150 return ldp->app_name;
154 ldp_fd_alloc (u32 sid)
158 clib_rwlock_writer_lock (&ldp->fd_table_lock);
159 if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sid_bit_val)
161 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
164 pool_get (ldp->fd_pool, fde);
165 fde->session_index = vppcom_session_index (sid);
166 fde->fd_index = fde - ldp->fd_pool;
167 fde->fd = fde->fd_index + ldp->sid_bit_val;
168 hash_set (ldp->session_index_to_fd_table, fde->session_index, fde->fd);
169 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
173 static ldp_fd_entry_t *
174 ldp_fd_entry_get_w_lock (u32 fd_index)
176 clib_rwlock_reader_lock (&ldp->fd_table_lock);
177 if (pool_is_free_index (ldp->fd_pool, fd_index))
180 return pool_elt_at_index (ldp->fd_pool, fd_index);
184 ldp_fd_from_sid (u32 sid)
189 clib_rwlock_reader_lock (&ldp->fd_table_lock);
190 fdp = hash_get (ldp->session_index_to_fd_table, vppcom_session_index (sid));
191 fd = fdp ? *fdp : -EMFILE;
192 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
198 ldp_fd_is_sid (int fd)
200 return fd >= ldp->sid_bit_val;
204 ldp_sid_from_fd (int fd)
206 u32 fd_index, session_index;
209 if (!ldp_fd_is_sid (fd))
210 return INVALID_SESSION_ID;
212 fd_index = fd - ldp->sid_bit_val;
213 fde = ldp_fd_entry_get_w_lock (fd_index);
216 LDBG (0, "unknown fd %d", fd);
217 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
218 return INVALID_SESSION_ID;
220 session_index = fde->session_index;
221 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
223 return vppcom_session_handle (session_index);
227 ldp_fd_free_w_sid (u32 sid)
233 fd = ldp_fd_from_sid (sid);
237 fd_index = fd - ldp->sid_bit_val;
238 fde = ldp_fd_entry_get_w_lock (fd_index);
241 hash_unset (ldp->session_index_to_fd_table, fde->session_index);
242 pool_put (ldp->fd_pool, fde);
244 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
250 ldp_worker_ctx_t *ldpw;
253 if (PREDICT_TRUE (ldp->init))
257 ldp->vcl_needs_real_epoll = 1;
258 rv = vppcom_app_create (ldp_get_app_name ());
261 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
262 " failed! rv = %d (%s)\n",
263 getpid (), rv, vppcom_retval_str (rv));
267 ldp->vcl_needs_real_epoll = 0;
268 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
269 ldpw = ldp_worker_get_current ();
271 char *env_var_str = getenv (LDP_ENV_DEBUG);
275 if (sscanf (env_var_str, "%u", &tmp) != 1)
276 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
277 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
282 LDBG (0, "LDP<%d>: configured LDP debug level (%u) from env var "
283 LDP_ENV_DEBUG "!", getpid (), ldp->debug);
287 env_var_str = getenv (LDP_ENV_APP_NAME);
290 ldp_set_app_name (env_var_str);
291 LDBG (0, "LDP<%d>: configured LDP app name (%s) from the env var "
292 LDP_ENV_APP_NAME "!", getpid (), ldp->app_name);
295 env_var_str = getenv (LDP_ENV_SID_BIT);
299 if (sscanf (env_var_str, "%u", &sb) != 1)
301 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
302 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
303 "value %d (0x%x)", getpid (), env_var_str,
304 ldp->sid_bit_val, ldp->sid_bit_val);
306 else if (sb < LDP_SID_BIT_MIN)
308 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
309 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
311 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
312 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
313 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
314 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
315 ldp->sid_bit_val, ldp->sid_bit_val);
317 else if (sb > LDP_SID_BIT_MAX)
319 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
320 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
322 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
323 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
324 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
325 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
326 ldp->sid_bit_val, ldp->sid_bit_val);
330 ldp->sid_bit_val = (1 << sb);
331 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
333 LDBG (0, "LDP<%d>: configured LDP sid bit (%u) from "
334 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", getpid (), sb,
335 ldp->sid_bit_val, ldp->sid_bit_val);
339 clib_time_init (&ldpw->clib_time);
340 clib_rwlock_init (&ldp->fd_table_lock);
341 LDBG (0, "LDP<%d>: LDP initialization: done!", getpid ());
350 const char *func_str;
351 u32 sid = ldp_sid_from_fd (fd);
353 if ((errno = -ldp_init ()))
356 if (sid != INVALID_SESSION_ID)
360 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
361 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
364 func_str = "libc_close";
366 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
367 getpid (), fd, fd, func_str, epfd, epfd);
369 rv = libc_close (epfd);
372 u32 size = sizeof (epfd);
375 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
379 else if (PREDICT_FALSE (epfd < 0))
386 func_str = "vppcom_session_close";
388 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
389 getpid (), fd, fd, func_str, sid, sid);
391 refcnt = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REFCNT, 0, 0);
392 rv = vppcom_session_close (sid);
399 ldp_fd_free_w_sid (sid);
403 func_str = "libc_close";
405 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s()", getpid (), fd, fd,
408 rv = libc_close (fd);
416 int errno_val = errno;
418 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
419 "rv %d, errno = %d", getpid (), fd, fd,
420 func_str, rv, errno_val);
424 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
425 getpid (), fd, fd, rv, rv);
431 read (int fd, void *buf, size_t nbytes)
434 const char *func_str;
435 u32 sid = ldp_sid_from_fd (fd);
437 if ((errno = -ldp_init ()))
440 if (sid != INVALID_SESSION_ID)
442 func_str = "vppcom_session_read";
445 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
446 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
447 fd, fd, func_str, sid, sid, buf, nbytes);
449 size = vppcom_session_read (sid, buf, nbytes);
458 func_str = "libc_read";
461 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
462 "buf %p, nbytes %u", getpid (),
463 fd, fd, func_str, buf, nbytes);
465 size = libc_read (fd, buf, nbytes);
472 int errno_val = errno;
474 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
475 "rv %d, errno = %d", getpid (), fd, fd,
476 func_str, size, errno_val);
480 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
481 getpid (), fd, fd, size, size);
487 readv (int fd, const struct iovec * iov, int iovcnt)
489 const char *func_str;
491 u32 sid = ldp_sid_from_fd (fd);
492 int rv = 0, i, total = 0;
494 if ((errno = -ldp_init ()))
497 if (sid != INVALID_SESSION_ID)
499 func_str = "vppcom_session_read";
502 for (i = 0; i < iovcnt; ++i)
505 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
506 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
507 getpid (), fd, fd, func_str, i, sid, sid,
510 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
516 if (rv < iov[i].iov_len)
519 clib_warning ("LDP<%d>: fd %d (0x%x): "
520 "rv (%d) < iov[%d].iov_len (%d)",
521 getpid (), fd, fd, rv, i,
528 while ((rv >= 0) && (total == 0));
540 func_str = "libc_readv";
543 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
544 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
546 size = libc_readv (fd, iov, iovcnt);
553 int errno_val = errno;
555 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
556 "rv %d, errno = %d", getpid (), fd, fd,
557 func_str, size, errno_val);
561 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
562 getpid (), fd, fd, size, size);
568 write (int fd, const void *buf, size_t nbytes)
570 const char *func_str;
572 u32 sid = ldp_sid_from_fd (fd);
574 if ((errno = -ldp_init ()))
577 if (sid != INVALID_SESSION_ID)
579 func_str = "vppcom_session_write";
582 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
583 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
584 fd, fd, func_str, sid, sid, buf, nbytes);
586 size = vppcom_session_write (sid, (void *) buf, nbytes);
595 func_str = "libc_write";
598 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
599 "buf %p, nbytes %u", getpid (),
600 fd, fd, func_str, buf, nbytes);
602 size = libc_write (fd, buf, nbytes);
609 int errno_val = errno;
611 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
612 "rv %d, errno = %d", getpid (), fd, fd,
613 func_str, size, errno_val);
617 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
618 getpid (), fd, fd, size, size);
624 writev (int fd, const struct iovec * iov, int iovcnt)
626 const char *func_str;
627 ssize_t size = 0, total = 0;
628 u32 sid = ldp_sid_from_fd (fd);
632 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
635 if ((errno = -ldp_init ()))
638 if (sid != INVALID_SESSION_ID)
640 func_str = "vppcom_session_write";
643 for (i = 0; i < iovcnt; ++i)
646 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
647 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
648 __func__, __LINE__, getpid (), fd, fd, func_str,
649 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
651 rv = vppcom_session_write (sid, iov[i].iov_base,
658 if (rv < iov[i].iov_len)
661 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
662 "rv (%d) < iov[%d].iov_len (%ld)",
663 __func__, __LINE__, getpid (), fd, fd,
664 rv, i, iov[i].iov_len);
670 while ((rv >= 0) && (total == 0));
682 func_str = "libc_writev";
685 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
686 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
687 fd, fd, func_str, iov, iovcnt);
689 size = libc_writev (fd, iov, iovcnt);
696 int errno_val = errno;
699 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
700 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
701 fd, func_str, size, errno_val);
705 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
706 __func__, __LINE__, getpid (), fd, fd, size);
712 fcntl (int fd, int cmd, ...)
714 const char *func_str = __func__;
717 u32 sid = ldp_sid_from_fd (fd);
719 if ((errno = -ldp_init ()))
723 if (sid != INVALID_SESSION_ID)
725 int flags = va_arg (ap, int);
728 size = sizeof (flags);
733 func_str = "vppcom_session_attr[SET_FLAGS]";
734 LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
735 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
736 sid, flags, flags, size);
738 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
743 func_str = "vppcom_session_attr[GET_FLAGS]";
744 LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
745 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
746 sid, flags, flags, size);
748 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
752 LDBG (2, "LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): %s() "
753 "returned flags %d (0x%x)", getpid (), fd, fd, cmd,
754 func_str, flags, flags);
759 /* TODO handle this */
760 LDBG (0, "F_SETFD ignored flags %u", flags);
775 func_str = "libc_vfcntl";
778 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
779 getpid (), fd, fd, func_str, cmd);
781 rv = libc_vfcntl (fd, cmd, ap);
790 int errno_val = errno;
792 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
793 "rv %d, errno = %d", getpid (), fd, fd,
794 func_str, rv, errno_val);
798 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
799 getpid (), fd, fd, rv, rv);
805 ioctl (int fd, unsigned long int cmd, ...)
807 const char *func_str;
810 u32 sid = ldp_sid_from_fd (fd);
812 if ((errno = -ldp_init ()))
816 if (sid != INVALID_SESSION_ID)
818 func_str = "vppcom_session_attr[GET_NREAD]";
825 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
826 getpid (), fd, fd, func_str, sid, sid);
828 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
833 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
834 u32 size = sizeof (flags);
836 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
837 * non-blocking, the flags should be read here and merged
841 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
842 "sid %u (0x%x), flags %d (0x%x), size %d",
843 getpid (), fd, fd, func_str, sid, sid,
846 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
863 func_str = "libc_vioctl";
866 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
867 getpid (), fd, fd, func_str, cmd);
869 rv = libc_vioctl (fd, cmd, ap);
876 int errno_val = errno;
878 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
879 "rv %d, errno = %d", getpid (), fd, fd,
880 func_str, rv, errno_val);
884 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
885 getpid (), fd, fd, rv, rv);
892 ldp_pselect (int nfds, fd_set * __restrict readfds,
893 fd_set * __restrict writefds,
894 fd_set * __restrict exceptfds,
895 const struct timespec *__restrict timeout,
896 const __sigset_t * __restrict sigmask)
898 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
899 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
900 u32 minbits = clib_max (nfds, BITS (uword)), sid;
901 char *func_str = "##";
913 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
914 (f64) 0 : (f64) timeout->tv_sec +
915 (f64) timeout->tv_nsec / (f64) 1000000000;
917 /* select as fine grained sleep */
920 LDBG (3, "LDP<%d>: sleeping for %.02f seconds", getpid (),
923 time_out += clib_time_now (&ldpw->clib_time);
924 while (clib_time_now (&ldpw->clib_time) < time_out)
938 if (nfds <= ldp->sid_bit_val)
940 func_str = "libc_pselect";
942 LDBG (3, "LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
943 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
944 readfds, writefds, exceptfds, timeout, sigmask);
946 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
951 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
953 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
954 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
955 ldp->sid_bit_val, ldp->sid_bit_val,
956 FD_SETSIZE / 2, FD_SETSIZE / 2);
961 sid_bits = libc_bits = 0;
962 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
965 clib_bitmap_validate (ldpw->sid_rd_bitmap, minbits);
966 clib_bitmap_validate (ldpw->libc_rd_bitmap, minbits);
967 clib_bitmap_validate (ldpw->rd_bitmap, minbits);
968 clib_memcpy_fast (ldpw->rd_bitmap, readfds, n_bytes);
969 memset (readfds, 0, n_bytes);
972 clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({
975 sid = ldp_sid_from_fd (fd);
976 LDBG (3, "LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
977 getpid (), fd, fd, sid, sid);
978 if (sid == INVALID_SESSION_ID)
979 clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1);
981 clib_bitmap_set_no_check (ldpw->sid_rd_bitmap,
982 vppcom_session_index (sid), 1);
986 sid_bits_set = clib_bitmap_last_set (ldpw->sid_rd_bitmap) + 1;
987 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
989 libc_bits_set = clib_bitmap_last_set (ldpw->libc_rd_bitmap) + 1;
990 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
992 LDBG (3, "LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
993 "libc_bits_set %d, libc_bits %d", getpid (), sid_bits_set,
994 sid_bits, libc_bits_set, libc_bits);
998 clib_bitmap_validate (ldpw->sid_wr_bitmap, minbits);
999 clib_bitmap_validate (ldpw->libc_wr_bitmap, minbits);
1000 clib_bitmap_validate (ldpw->wr_bitmap, minbits);
1001 clib_memcpy_fast (ldpw->wr_bitmap, writefds, n_bytes);
1002 memset (writefds, 0, n_bytes);
1005 clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({
1008 sid = ldp_sid_from_fd (fd);
1009 LDBG (3, "LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
1010 getpid (), fd, fd, sid, sid);
1011 if (sid == INVALID_SESSION_ID)
1012 clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1);
1014 clib_bitmap_set_no_check (ldpw->sid_wr_bitmap,
1015 vppcom_session_index (sid), 1);
1019 sid_bits_set = clib_bitmap_last_set (ldpw->sid_wr_bitmap) + 1;
1020 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
1022 libc_bits_set = clib_bitmap_last_set (ldpw->libc_wr_bitmap) + 1;
1023 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
1025 LDBG (3, "LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
1026 "libc_bits_set %d, libc_bits %d", getpid (),
1027 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
1031 clib_bitmap_validate (ldpw->sid_ex_bitmap, minbits);
1032 clib_bitmap_validate (ldpw->libc_ex_bitmap, minbits);
1033 clib_bitmap_validate (ldpw->ex_bitmap, minbits);
1034 clib_memcpy_fast (ldpw->ex_bitmap, exceptfds, n_bytes);
1035 memset (exceptfds, 0, n_bytes);
1038 clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({
1041 sid = ldp_sid_from_fd (fd);
1042 LDBG (3, "LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
1043 getpid (), fd, fd, sid, sid);
1044 if (sid == INVALID_SESSION_ID)
1045 clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1);
1047 clib_bitmap_set_no_check (ldpw->sid_ex_bitmap,
1048 vppcom_session_index (sid), 1);
1052 sid_bits_set = clib_bitmap_last_set (ldpw->sid_ex_bitmap) + 1;
1053 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
1055 libc_bits_set = clib_bitmap_last_set (ldpw->libc_ex_bitmap) + 1;
1056 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
1058 LDBG (3, "LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
1059 "libc_bits_set %d, libc_bits %d", getpid (),
1060 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
1063 if (PREDICT_FALSE (!sid_bits && !libc_bits))
1074 if (!ldpw->select_vcl)
1076 func_str = "vppcom_select";
1079 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap,
1080 vec_len (ldpw->rd_bitmap) *
1081 sizeof (clib_bitmap_t));
1083 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap,
1084 vec_len (ldpw->wr_bitmap) *
1085 sizeof (clib_bitmap_t));
1087 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->sid_ex_bitmap,
1088 vec_len (ldpw->ex_bitmap) *
1089 sizeof (clib_bitmap_t));
1091 rv = vppcom_select (sid_bits,
1092 readfds ? ldpw->rd_bitmap : NULL,
1093 writefds ? ldpw->wr_bitmap : NULL,
1094 exceptfds ? ldpw->ex_bitmap : NULL, 0);
1105 clib_bitmap_foreach (sid, ldpw->rd_bitmap,
1107 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1108 if (PREDICT_FALSE (fd < 0))
1114 FD_SET (fd, readfds);
1121 clib_bitmap_foreach (sid, ldpw->wr_bitmap,
1123 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1124 if (PREDICT_FALSE (fd < 0))
1130 FD_SET (fd, writefds);
1137 clib_bitmap_foreach (sid, ldpw->ex_bitmap,
1139 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1140 if (PREDICT_FALSE (fd < 0))
1146 FD_SET (fd, exceptfds);
1150 ldpw->select_vcl = 1;
1155 ldpw->select_vcl = 0;
1159 struct timespec tspec;
1161 func_str = "libc_pselect";
1164 clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap,
1165 vec_len (ldpw->rd_bitmap) *
1166 sizeof (clib_bitmap_t));
1168 clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap,
1169 vec_len (ldpw->wr_bitmap) *
1170 sizeof (clib_bitmap_t));
1172 clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap,
1173 vec_len (ldpw->ex_bitmap) *
1174 sizeof (clib_bitmap_t));
1175 tspec.tv_sec = tspec.tv_nsec = 0;
1176 rv = libc_pselect (libc_bits,
1177 readfds ? readfds : NULL,
1178 writefds ? writefds : NULL,
1179 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1184 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
1188 /* TBD: set timeout to amount of time left */
1189 clib_bitmap_zero (ldpw->rd_bitmap);
1190 clib_bitmap_zero (ldpw->sid_rd_bitmap);
1191 clib_bitmap_zero (ldpw->libc_rd_bitmap);
1192 clib_bitmap_zero (ldpw->wr_bitmap);
1193 clib_bitmap_zero (ldpw->sid_wr_bitmap);
1194 clib_bitmap_zero (ldpw->libc_wr_bitmap);
1195 clib_bitmap_zero (ldpw->ex_bitmap);
1196 clib_bitmap_zero (ldpw->sid_ex_bitmap);
1197 clib_bitmap_zero (ldpw->libc_ex_bitmap);
1203 int errno_val = errno;
1205 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1206 "rv %d, errno = %d", getpid (),
1207 func_str, rv, errno_val);
1211 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1217 select (int nfds, fd_set * __restrict readfds,
1218 fd_set * __restrict writefds,
1219 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1221 struct timespec tspec;
1225 tspec.tv_sec = timeout->tv_sec;
1226 tspec.tv_nsec = timeout->tv_usec * 1000;
1228 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1229 timeout ? &tspec : NULL, NULL);
1232 #ifdef __USE_XOPEN2K
1234 pselect (int nfds, fd_set * __restrict readfds,
1235 fd_set * __restrict writefds,
1236 fd_set * __restrict exceptfds,
1237 const struct timespec *__restrict timeout,
1238 const __sigset_t * __restrict sigmask)
1240 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1245 socket (int domain, int type, int protocol)
1247 const char *func_str;
1249 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1250 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1252 if ((errno = -ldp_init ()))
1255 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1256 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1259 u8 proto = ((sock_type == SOCK_DGRAM) ?
1260 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1262 func_str = "vppcom_session_create";
1264 LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u",
1265 getpid (), func_str, proto, vppcom_proto_str (proto),
1268 sid = vppcom_session_create (proto, is_nonblocking);
1276 func_str = "ldp_fd_from_sid";
1277 rv = ldp_fd_alloc (sid);
1280 (void) vppcom_session_close (sid);
1288 func_str = "libc_socket";
1290 LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str);
1292 rv = libc_socket (domain, type, protocol);
1299 int errno_val = errno;
1301 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1302 "rv %d, errno = %d",
1303 getpid (), func_str, rv, errno_val);
1307 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1313 * Create two new sockets, of type TYPE in domain DOMAIN and using
1314 * protocol PROTOCOL, which are connected to each other, and put file
1315 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1316 * one will be chosen automatically.
1317 * Returns 0 on success, -1 for errors.
1320 socketpair (int domain, int type, int protocol, int fds[2])
1322 const char *func_str;
1324 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1326 if ((errno = -ldp_init ()))
1329 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1330 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1332 func_str = __func__;
1334 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1340 func_str = "libc_socket";
1342 LDBG (1, "LDP<%d>: : calling %s()", getpid (), func_str);
1344 rv = libc_socketpair (domain, type, protocol, fds);
1351 int errno_val = errno;
1353 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1354 "rv %d, errno = %d",
1355 getpid (), func_str, rv, errno_val);
1359 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1365 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1368 const char *func_str;
1369 u32 sid = ldp_sid_from_fd (fd);
1371 if ((errno = -ldp_init ()))
1374 if (sid != INVALID_SESSION_ID)
1378 func_str = "vppcom_session_bind";
1380 switch (addr->sa_family)
1383 if (len != sizeof (struct sockaddr_in))
1386 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1387 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1392 ep.is_ip4 = VPPCOM_IS_IP4;
1393 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1394 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1398 if (len != sizeof (struct sockaddr_in6))
1401 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1402 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1407 ep.is_ip4 = VPPCOM_IS_IP6;
1408 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1409 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1413 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1414 "Unsupported address family %u!",
1415 getpid (), fd, fd, sid, sid, addr->sa_family);
1416 errno = EAFNOSUPPORT;
1421 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1423 getpid (), fd, fd, func_str, sid, sid, addr, len);
1425 rv = vppcom_session_bind (sid, &ep);
1426 if (rv != VPPCOM_OK)
1434 func_str = "libc_bind";
1437 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1439 getpid (), fd, fd, func_str, addr, len);
1441 rv = libc_bind (fd, addr, len);
1449 int errno_val = errno;
1451 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1452 "rv %d, errno = %d", getpid (), fd, fd,
1453 func_str, rv, errno_val);
1457 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1458 getpid (), fd, fd, rv, rv);
1464 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1465 vppcom_endpt_t * ep)
1468 int sa_len, copy_len;
1470 if ((errno = -ldp_init ()))
1473 if (addr && len && ep)
1475 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1476 switch (addr->sa_family)
1479 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1480 if (*len > sizeof (struct sockaddr_in))
1481 *len = sizeof (struct sockaddr_in);
1482 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1483 copy_len = *len - sa_len;
1485 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1490 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1491 if (*len > sizeof (struct sockaddr_in6))
1492 *len = sizeof (struct sockaddr_in6);
1493 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1494 copy_len = *len - sa_len;
1496 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1497 __in6_u.__u6_addr8, ep->ip, copy_len);
1510 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1513 const char *func_str;
1514 u32 sid = ldp_sid_from_fd (fd);
1516 if ((errno = -ldp_init ()))
1519 if (sid != INVALID_SESSION_ID)
1522 u8 addr_buf[sizeof (struct in6_addr)];
1523 u32 size = sizeof (ep);
1526 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1529 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1531 getpid (), fd, fd, func_str, sid, sid, addr, len);
1533 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1534 if (rv != VPPCOM_OK)
1541 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1542 if (rv != VPPCOM_OK)
1551 func_str = "libc_getsockname";
1554 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1556 getpid (), fd, fd, func_str, addr, len);
1558 rv = libc_getsockname (fd, addr, len);
1565 int errno_val = errno;
1567 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1568 "rv %d, errno = %d", getpid (), fd, fd,
1569 func_str, rv, errno_val);
1573 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1574 getpid (), fd, fd, rv, rv);
1580 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1583 const char *func_str = __func__;
1584 u32 sid = ldp_sid_from_fd (fd);
1586 if ((errno = -ldp_init ()))
1591 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1592 getpid (), fd, fd, len);
1598 if (sid != INVALID_SESSION_ID)
1602 func_str = "vppcom_session_connect";
1604 switch (addr->sa_family)
1607 if (len != sizeof (struct sockaddr_in))
1610 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1611 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1616 ep.is_ip4 = VPPCOM_IS_IP4;
1617 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1618 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1622 if (len != sizeof (struct sockaddr_in6))
1625 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1626 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1631 ep.is_ip4 = VPPCOM_IS_IP6;
1632 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1633 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1637 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1638 "Unsupported address family %u!",
1639 getpid (), fd, fd, sid, sid, addr->sa_family);
1640 errno = EAFNOSUPPORT;
1645 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1647 getpid (), fd, fd, func_str, sid, sid, addr, len);
1649 rv = vppcom_session_connect (sid, &ep);
1650 if (rv != VPPCOM_OK)
1658 func_str = "libc_connect";
1661 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1663 getpid (), fd, fd, func_str, addr, len);
1665 rv = libc_connect (fd, addr, len);
1673 int errno_val = errno;
1675 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1676 "rv %d, errno = %d", getpid (), fd, fd,
1677 func_str, rv, errno_val);
1681 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1682 getpid (), fd, fd, rv, rv);
1688 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1691 const char *func_str;
1692 u32 sid = ldp_sid_from_fd (fd);
1694 if ((errno = -ldp_init ()))
1697 if (sid != INVALID_SESSION_ID)
1700 u8 addr_buf[sizeof (struct in6_addr)];
1701 u32 size = sizeof (ep);
1704 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1707 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1709 getpid (), fd, fd, func_str, sid, sid, addr, len);
1711 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1712 if (rv != VPPCOM_OK)
1719 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1720 if (rv != VPPCOM_OK)
1729 func_str = "libc_getpeername";
1732 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1734 getpid (), fd, fd, func_str, addr, len);
1736 rv = libc_getpeername (fd, addr, len);
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, rv, errno_val);
1751 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1752 getpid (), fd, fd, rv, rv);
1758 send (int fd, const void *buf, size_t n, int flags)
1761 const char *func_str;
1762 u32 sid = ldp_sid_from_fd (fd);
1764 if ((errno = -ldp_init ()))
1767 if (sid != INVALID_SESSION_ID)
1770 func_str = "vppcom_session_sendto";
1773 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1774 "buf %p, n %u, flags 0x%x",
1775 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1777 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1778 if (size < VPPCOM_OK)
1786 func_str = "libc_send";
1789 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1790 "buf %p, n %u, flags 0x%x",
1791 getpid (), fd, fd, func_str, buf, n, flags);
1793 size = libc_send (fd, buf, n, flags);
1800 int errno_val = errno;
1802 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1803 "rv %d, errno = %d", getpid (), fd, fd,
1804 func_str, size, errno_val);
1808 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1809 getpid (), fd, fd, size, size);
1815 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1817 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1819 const char *func_str;
1820 u32 sid = ldp_sid_from_fd (out_fd);
1822 if ((errno = -ldp_init ()))
1825 if (sid != INVALID_SESSION_ID)
1828 ssize_t results = 0;
1829 size_t n_bytes_left = len;
1830 size_t bytes_to_read;
1834 u32 flags, flags_len = sizeof (flags);
1836 func_str = "vppcom_session_attr[GET_FLAGS]";
1837 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1839 if (PREDICT_FALSE (rv != VPPCOM_OK))
1841 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1842 "sid %u (0x%x), returned %d (%s)!", getpid (),
1843 out_fd, out_fd, func_str, sid, sid, rv,
1844 vppcom_retval_str (rv));
1846 vec_reset_length (ldpw->io_buffer);
1854 off_t off = lseek (in_fd, *offset, SEEK_SET);
1855 if (PREDICT_FALSE (off == -1))
1859 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1860 "SEEK_SET failed: in_fd %d, offset %p, "
1861 "*offset %ld, rv %ld, errno %d", getpid (),
1862 out_fd, out_fd, in_fd, offset, *offset, off,
1869 ASSERT (off == *offset);
1874 func_str = "vppcom_session_attr[GET_NWRITE]";
1875 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1879 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1880 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1881 sid, sid, size, vppcom_retval_str (size));
1882 vec_reset_length (ldpw->io_buffer);
1888 bytes_to_read = size;
1891 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1892 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1893 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1896 if (bytes_to_read == 0)
1898 if (flags & O_NONBLOCK)
1903 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1905 getpid (), out_fd, out_fd, sid, sid);
1913 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1914 vec_validate (ldpw->io_buffer, bytes_to_read);
1915 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1918 func_str = "libc_read";
1920 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1921 "io_buffer %p, bytes_to_read %lu, rv %d, "
1922 "errno %d", getpid (), out_fd, out_fd, func_str,
1923 in_fd, ldpw->io_buffer, bytes_to_read, nbytes,
1929 vec_reset_length (ldpw->io_buffer);
1935 func_str = "vppcom_session_write";
1938 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1939 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1940 out_fd, out_fd, func_str, sid, sid, ldpw->io_buffer, nbytes,
1941 results, n_bytes_left);
1943 size = vppcom_session_write (sid, ldpw->io_buffer, nbytes);
1946 if (size == VPPCOM_EAGAIN)
1948 if (flags & O_NONBLOCK)
1954 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1955 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1965 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1966 "sid %u, io_buffer %p, nbytes %u "
1968 getpid (), out_fd, out_fd, func_str,
1969 sid, ldpw->io_buffer, nbytes,
1970 size, vppcom_retval_str (size));
1974 vec_reset_length (ldpw->io_buffer);
1983 ASSERT (n_bytes_left >= nbytes);
1984 n_bytes_left = n_bytes_left - nbytes;
1986 while (n_bytes_left > 0);
1989 vec_reset_length (ldpw->io_buffer);
1992 off_t off = lseek (in_fd, *offset, SEEK_SET);
1993 if (PREDICT_FALSE (off == -1))
1997 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1998 "in_fd %d, offset %p, *offset %ld, "
1999 "rv %ld, errno %d", getpid (), in_fd,
2000 offset, *offset, off, errno_val);
2006 ASSERT (off == *offset);
2007 *offset += results + 1;
2019 func_str = "libc_send";
2022 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2023 "in_fd %d, offset %p, len %u",
2024 getpid (), out_fd, out_fd, func_str,
2025 in_fd, offset, len);
2027 size = libc_sendfile (out_fd, in_fd, offset, len);
2035 int errno_val = errno;
2037 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2038 "rv %d, errno = %d", getpid (), out_fd, out_fd,
2039 func_str, size, errno_val);
2043 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2044 getpid (), out_fd, out_fd, size, size);
2050 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
2052 return sendfile (out_fd, in_fd, offset, len);
2056 recv (int fd, void *buf, size_t n, int flags)
2059 const char *func_str;
2060 u32 sid = ldp_sid_from_fd (fd);
2062 if ((errno = -ldp_init ()))
2065 if (sid != INVALID_SESSION_ID)
2067 func_str = "vppcom_session_recvfrom";
2070 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2071 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
2072 fd, fd, func_str, sid, sid, buf, n, flags);
2074 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2083 func_str = "libc_recv";
2086 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2087 "buf %p, n %u, flags 0x%x", getpid (),
2088 fd, fd, func_str, buf, n, flags);
2090 size = libc_recv (fd, buf, n, flags);
2097 int errno_val = errno;
2099 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2100 "rv %d, errno = %d", getpid (), fd, fd,
2101 func_str, size, errno_val);
2105 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2106 getpid (), fd, fd, size, size);
2112 sendto (int fd, const void *buf, size_t n, int flags,
2113 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2116 const char *func_str = __func__;
2117 u32 sid = ldp_sid_from_fd (fd);
2119 if ((errno = -ldp_init ()))
2122 if (sid != INVALID_SESSION_ID)
2124 vppcom_endpt_t *ep = 0;
2130 switch (addr->sa_family)
2133 ep->is_ip4 = VPPCOM_IS_IP4;
2135 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2137 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2141 ep->is_ip4 = VPPCOM_IS_IP6;
2143 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2145 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2149 errno = EAFNOSUPPORT;
2155 func_str = "vppcom_session_sendto";
2158 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2159 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2160 getpid (), fd, fd, func_str, sid, sid, buf, n,
2163 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2172 func_str = "libc_sendto";
2175 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2176 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2177 getpid (), fd, fd, func_str, buf, n, flags,
2180 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2188 int errno_val = errno;
2190 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2191 "rv %d, errno = %d", getpid (), fd, fd,
2192 func_str, size, errno_val);
2196 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2197 getpid (), fd, fd, size, size);
2203 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2204 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2207 const char *func_str;
2208 u32 sid = ldp_sid_from_fd (fd);
2210 if ((errno = -ldp_init ()))
2213 if (sid != INVALID_SESSION_ID)
2216 u8 src_addr[sizeof (struct sockaddr_in6)];
2218 func_str = "vppcom_session_recvfrom";
2221 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2222 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2223 getpid (), fd, fd, func_str, sid, sid, buf, n,
2228 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2231 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2234 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2244 func_str = "libc_recvfrom";
2247 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2248 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2249 getpid (), fd, fd, func_str, buf, n, flags,
2252 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2259 int errno_val = errno;
2261 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2262 "rv %d, errno = %d", getpid (), fd, fd,
2263 func_str, size, errno_val);
2267 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2268 getpid (), fd, fd, size, size);
2274 sendmsg (int fd, const struct msghdr * message, int flags)
2277 const char *func_str;
2278 u32 sid = ldp_sid_from_fd (fd);
2280 if ((errno = -ldp_init ()))
2283 if (sid != INVALID_SESSION_ID)
2285 func_str = __func__;
2287 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2293 func_str = "libc_sendmsg";
2296 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2297 "message %p, flags 0x%x",
2298 getpid (), fd, fd, func_str, message, flags);
2300 size = libc_sendmsg (fd, message, flags);
2307 int errno_val = errno;
2309 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2310 "rv %d, errno = %d", getpid (), fd, fd,
2311 func_str, size, errno_val);
2315 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2316 getpid (), fd, fd, size, size);
2323 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2326 const char *func_str;
2327 u32 sid = ldp_sid_from_fd (fd);
2329 if ((errno = -ldp_init ()))
2332 if (sid != INVALID_SESSION_ID)
2334 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2340 func_str = "libc_sendmmsg";
2343 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2344 "vmessages %p, vlen %u, flags 0x%x",
2345 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2347 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2354 int errno_val = errno;
2356 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2357 "rv %d, errno = %d", getpid (), fd, fd,
2358 func_str, size, errno_val);
2362 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2363 getpid (), fd, fd, size, size);
2370 recvmsg (int fd, struct msghdr * message, int flags)
2373 const char *func_str;
2374 u32 sid = ldp_sid_from_fd (fd);
2376 if ((errno = -ldp_init ()))
2379 if (sid != INVALID_SESSION_ID)
2381 func_str = __func__;
2383 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2389 func_str = "libc_recvmsg";
2392 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2393 "message %p, flags 0x%x",
2394 getpid (), fd, fd, func_str, message, flags);
2396 size = libc_recvmsg (fd, message, flags);
2403 int errno_val = errno;
2405 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2406 "rv %d, errno = %d", getpid (), fd, fd,
2407 func_str, size, errno_val);
2411 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2412 getpid (), fd, fd, size, size);
2419 recvmmsg (int fd, struct mmsghdr *vmessages,
2420 unsigned int vlen, int flags, struct timespec *tmo)
2423 const char *func_str;
2424 u32 sid = ldp_sid_from_fd (fd);
2426 if ((errno = -ldp_init ()))
2429 if (sid != INVALID_SESSION_ID)
2431 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2437 func_str = "libc_recvmmsg";
2440 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2441 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2442 getpid (), fd, fd, func_str, vmessages, vlen,
2445 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2452 int errno_val = errno;
2454 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2455 "rv %d, errno = %d", getpid (), fd, fd,
2456 func_str, size, errno_val);
2460 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2461 getpid (), fd, fd, size, size);
2468 getsockopt (int fd, int level, int optname,
2469 void *__restrict optval, socklen_t * __restrict optlen)
2472 const char *func_str = __func__;
2473 u32 sid = ldp_sid_from_fd (fd);
2474 u32 buflen = optlen ? (u32) * optlen : 0;
2476 if ((errno = -ldp_init ()))
2479 if (sid != INVALID_SESSION_ID)
2489 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2491 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2493 getpid (), fd, fd, func_str, sid, sid);
2494 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2498 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2500 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2502 getpid (), fd, fd, func_str, sid, sid);
2503 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2507 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2509 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2511 getpid (), fd, fd, func_str, sid, sid);
2512 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2516 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2518 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2519 "sid %u (0x%x), SOL_TCP",
2520 getpid (), fd, fd, func_str, sid, sid);
2521 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2525 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2528 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2529 "SOL_TCP, TCP_INFO, optval %p, "
2530 "optlen %d: #LDP-NOP#",
2531 getpid (), fd, fd, sid, sid,
2533 memset (optval, 0, *optlen);
2541 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2542 "sid %u (0x%x), SOL_TCP, "
2543 "optname %d unsupported!",
2544 getpid (), fd, fd, func_str, sid, sid, optname);
2552 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2554 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2556 getpid (), fd, fd, func_str, sid, sid);
2557 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2562 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2563 "sid %u (0x%x), SOL_IPV6, "
2564 "optname %d unsupported!",
2565 getpid (), fd, fd, func_str, sid, sid, optname);
2573 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2575 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2577 getpid (), fd, fd, func_str, sid, sid);
2578 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2582 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2584 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2586 getpid (), fd, fd, func_str, sid, sid);
2587 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2591 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2593 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2595 getpid (), fd, fd, func_str, sid, sid);
2596 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2598 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2601 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2603 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2604 "sid %u (0x%x), optlen %d",
2605 getpid (), fd, fd, func_str, sid, sid, buflen);
2606 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2610 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2612 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2613 "sid %u (0x%x), optlen %d",
2614 getpid (), fd, fd, func_str, sid, sid, buflen);
2615 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2619 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2621 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2623 getpid (), fd, fd, func_str, sid, sid);
2624 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2628 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2630 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2632 getpid (), fd, fd, func_str, sid, sid);
2633 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2637 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2639 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2641 getpid (), fd, fd, func_str, sid, sid);
2642 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2647 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2648 "sid %u (0x%x), SOL_SOCKET, "
2649 "optname %d unsupported!",
2650 getpid (), fd, fd, func_str, sid, sid, optname);
2658 if (rv != VPPCOM_OK)
2666 func_str = "libc_getsockopt";
2669 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2670 "optname %d, optval %p, optlen %d",
2671 getpid (), fd, fd, func_str, level, optname,
2674 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2681 int errno_val = errno;
2683 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2684 "rv %d, errno = %d", getpid (), fd, fd,
2685 func_str, rv, errno_val);
2689 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2690 getpid (), fd, fd, rv, rv);
2696 setsockopt (int fd, int level, int optname,
2697 const void *optval, socklen_t optlen)
2700 const char *func_str = __func__;
2701 u32 sid = ldp_sid_from_fd (fd);
2703 if ((errno = -ldp_init ()))
2706 if (sid != INVALID_SESSION_ID)
2716 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2718 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2720 getpid (), fd, fd, func_str, sid, sid);
2721 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2722 (void *) optval, &optlen);
2725 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2727 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2729 getpid (), fd, fd, func_str, sid, sid);
2730 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2731 (void *) optval, &optlen);
2734 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2736 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2738 getpid (), fd, fd, func_str, sid, sid);
2739 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2740 (void *) optval, &optlen);
2743 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2745 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2746 "sid %u (0x%x), SOL_TCP",
2747 getpid (), fd, fd, func_str, sid, sid);
2748 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2749 (void *) optval, &optlen);
2753 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2754 "sid %u (0x%x), SOL_TCP, "
2755 "optname %d unsupported!",
2756 getpid (), fd, fd, func_str, sid, sid, optname);
2764 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2766 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2768 getpid (), fd, fd, func_str, sid, sid);
2769 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2770 (void *) optval, &optlen);
2774 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2775 "sid %u (0x%x), SOL_IPV6, "
2776 "optname %d unsupported!",
2777 getpid (), fd, fd, func_str, sid, sid, optname);
2785 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2787 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2789 getpid (), fd, fd, func_str, sid, sid);
2790 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2791 (void *) optval, &optlen);
2794 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2796 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2798 getpid (), fd, fd, func_str, sid, sid);
2799 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2800 (void *) optval, &optlen);
2803 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2805 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2807 getpid (), fd, fd, func_str, sid, sid);
2808 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2809 (void *) optval, &optlen);
2813 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2814 "sid %u (0x%x), SOL_SOCKET, "
2815 "optname %d unsupported!",
2816 getpid (), fd, fd, func_str, sid, sid, optname);
2824 if (rv != VPPCOM_OK)
2832 func_str = "libc_setsockopt";
2835 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2836 "optname %d, optval %p, optlen %d",
2837 getpid (), fd, fd, func_str, level, optname,
2840 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2847 int errno_val = errno;
2849 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2850 "rv %d, errno = %d", getpid (), fd, fd,
2851 func_str, rv, errno_val);
2855 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2856 getpid (), fd, fd, rv, rv);
2862 listen (int fd, int n)
2865 const char *func_str;
2866 u32 sid = ldp_sid_from_fd (fd);
2868 if ((errno = -ldp_init ()))
2871 if (sid != INVALID_SESSION_ID)
2873 func_str = "vppcom_session_listen";
2875 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2876 getpid (), fd, fd, func_str, sid, sid, n);
2878 rv = vppcom_session_listen (sid, n);
2879 if (rv != VPPCOM_OK)
2887 func_str = "libc_listen";
2889 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd,
2892 rv = libc_listen (fd, n);
2899 int errno_val = errno;
2901 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2902 "rv %d, errno = %d", getpid (), fd, fd,
2903 func_str, rv, errno_val);
2907 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2908 getpid (), fd, fd, rv, rv);
2914 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2915 socklen_t * __restrict addr_len, int flags)
2918 const char *func_str;
2919 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2922 if ((errno = -ldp_init ()))
2925 if (listen_sid != INVALID_SESSION_ID)
2928 u8 src_addr[sizeof (struct sockaddr_in6)];
2929 memset (&ep, 0, sizeof (ep));
2932 func_str = "vppcom_session_accept";
2935 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2936 "listen sid %u (0x%x), ep %p, flags 0x%x",
2937 getpid (), listen_fd, listen_fd, func_str,
2938 listen_sid, listen_sid, ep, flags);
2940 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2943 errno = -accept_sid;
2948 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2949 if (rv != VPPCOM_OK)
2951 (void) vppcom_session_close ((u32) accept_sid);
2957 func_str = "ldp_fd_from_sid";
2959 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2960 "accept sid %u (0x%x), ep %p, flags 0x%x",
2961 getpid (), listen_fd, listen_fd,
2962 func_str, accept_sid, accept_sid, ep, flags);
2963 rv = ldp_fd_alloc ((u32) accept_sid);
2966 (void) vppcom_session_close ((u32) accept_sid);
2975 func_str = "libc_accept4";
2978 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2979 "addr %p, addr_len %p, flags 0x%x",
2980 getpid (), listen_fd, listen_fd, func_str,
2981 addr, addr_len, flags);
2983 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2990 int errno_val = errno;
2992 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2993 "rv %d, errno = %d", getpid (), listen_fd,
2994 listen_fd, func_str, rv, errno_val);
2998 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2999 getpid (), listen_fd, listen_fd, rv, rv);
3005 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
3008 return ldp_accept4 (fd, addr, addr_len, flags);
3012 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
3014 return ldp_accept4 (fd, addr, addr_len, 0);
3018 shutdown (int fd, int how)
3021 const char *func_str;
3022 u32 sid = ldp_sid_from_fd (fd);
3024 if ((errno = -ldp_init ()))
3027 if (sid != INVALID_SESSION_ID)
3029 func_str = "vppcom_session_close[TODO]";
3034 func_str = "libc_shutdown";
3037 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
3038 getpid (), fd, fd, func_str, how);
3040 rv = libc_shutdown (fd, how);
3047 int errno_val = errno;
3049 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3050 "rv %d, errno = %d", getpid (), fd, fd,
3051 func_str, rv, errno_val);
3055 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3056 getpid (), fd, fd, rv, rv);
3062 epoll_create1 (int flags)
3064 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3065 const char *func_str;
3068 if ((errno = -ldp_init ()))
3071 if (ldp->vcl_needs_real_epoll)
3073 rv = libc_epoll_create1 (flags);
3074 ldp->vcl_needs_real_epoll = 0;
3075 ldpw->vcl_mq_epfd = rv;
3076 LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv);
3079 func_str = "vppcom_epoll_create";
3081 LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str);
3083 rv = vppcom_epoll_create ();
3085 if (PREDICT_FALSE (rv < 0))
3091 rv = ldp_fd_alloc ((u32) rv);
3097 int errno_val = errno;
3099 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3100 "rv %d, errno = %d",
3101 getpid (), func_str, rv, errno_val);
3105 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
3111 epoll_create (int size)
3113 return epoll_create1 (0);
3117 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3119 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
3120 const char *func_str;
3123 if ((errno = -ldp_init ()))
3126 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3128 /* The LDP epoll_create1 always creates VCL epfd's.
3129 * The app should never have a kernel base epoll fd unless it
3130 * was acquired outside of the LD_PRELOAD process context.
3131 * In any case, if we get one, punt it to libc_epoll_ctl.
3133 func_str = "libc_epoll_ctl";
3135 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
3136 " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event);
3138 rv = libc_epoll_ctl (epfd, op, fd, event);
3142 sid = ldp_sid_from_fd (fd);
3144 LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
3145 getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid);
3147 if (sid != INVALID_SESSION_ID)
3149 func_str = "vppcom_epoll_ctl";
3151 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3152 " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd,
3153 func_str, vep_idx, vep_idx, sid, sid, event);
3155 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3156 if (rv != VPPCOM_OK)
3165 u32 size = sizeof (epfd);
3167 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3168 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
3170 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() "
3171 "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd,
3172 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
3176 func_str = "libc_epoll_create1";
3178 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3179 "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd,
3180 vep_idx, vep_idx, func_str);
3182 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3189 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3190 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3191 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3192 getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3195 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3204 else if (PREDICT_FALSE (libc_epfd < 0))
3211 func_str = "libc_epoll_ctl";
3213 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3214 "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str,
3215 libc_epfd, libc_epfd, op, fd, fd, event);
3217 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3225 int errno_val = errno;
3227 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3228 "rv %d, errno = %d", getpid (), fd, fd,
3229 func_str, rv, errno_val);
3233 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3234 getpid (), fd, fd, rv, rv);
3240 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3241 int timeout, const sigset_t * sigmask)
3243 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3244 double time_to_wait = (double) 0, time_out, now = 0;
3245 u32 vep_idx = ldp_sid_from_fd (epfd);
3246 int libc_epfd, rv = 0;
3247 const char *func_str;
3249 if ((errno = -ldp_init ()))
3252 if (PREDICT_FALSE (!events || (timeout < -1)))
3258 if (epfd == ldpw->vcl_mq_epfd)
3259 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3261 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3263 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3264 getpid (), epfd, epfd, vep_idx, vep_idx);
3269 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3270 time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
3272 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3273 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3274 if (PREDICT_FALSE (libc_epfd < 0))
3281 LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3282 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3283 getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3284 maxevents, timeout, sigmask, time_to_wait, time_out);
3287 if (!ldpw->epoll_wait_vcl)
3289 func_str = "vppcom_epoll_wait";
3291 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3292 " events %p, maxevents %d", getpid (), epfd, epfd, func_str,
3293 vep_idx, vep_idx, events, maxevents);
3295 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3298 ldpw->epoll_wait_vcl = 1;
3309 ldpw->epoll_wait_vcl = 0;
3313 func_str = "libc_epoll_pwait";
3315 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d "
3316 "(0x%x), events %p, maxevents %d, sigmask %p", getpid (),
3317 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3318 maxevents, sigmask);
3320 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3326 now = clib_time_now (&ldpw->clib_time);
3328 while (now < time_out);
3337 int errno_val = errno;
3339 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3340 "rv %d, errno = %d", getpid (), epfd, epfd,
3341 func_str, rv, errno_val);
3345 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3346 getpid (), epfd, epfd, rv, rv);
3352 epoll_pwait (int epfd, struct epoll_event *events,
3353 int maxevents, int timeout, const sigset_t * sigmask)
3355 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3359 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3361 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3365 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3367 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3368 const char *func_str = __func__;
3369 int rv, i, n_revents = 0;
3372 double wait_for_time;
3374 LDBG (3, "LDP<%d>: fds %p, nfds %d, timeout %d", getpid (), fds, nfds,
3378 wait_for_time = (f64) timeout / 1000;
3382 for (i = 0; i < nfds; i++)
3387 LDBG (3, "LDP<%d>: fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3388 getpid (), i, fds[i].fd, fds[i].fd, fds[i].events,
3391 sid = ldp_sid_from_fd (fds[i].fd);
3392 if (sid != INVALID_SESSION_ID)
3394 fds[i].fd = -fds[i].fd;
3395 vec_add2 (ldpw->vcl_poll, vp, 1);
3398 vp->events = fds[i].events;
3399 #ifdef __USE_XOPEN2K
3400 if (fds[i].events & POLLRDNORM)
3401 vp->events |= POLLIN;
3402 if (fds[i].events & POLLWRNORM)
3403 vp->events |= POLLOUT;
3405 vp->revents = fds[i].revents;
3409 vec_add1 (ldpw->libc_poll, fds[i]);
3410 vec_add1 (ldpw->libc_poll_idxs, i);
3416 if (vec_len (ldpw->vcl_poll))
3418 func_str = "vppcom_poll";
3420 LDBG (3, "LDP<%d>: calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3421 "n_libc_fds %u", getpid (), func_str, ldpw->vcl_poll,
3422 vec_len (ldpw->vcl_poll), vec_len (ldpw->vcl_poll),
3423 vec_len (ldpw->libc_poll));
3425 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
3436 if (vec_len (ldpw->libc_poll))
3438 func_str = "libc_poll";
3440 LDBG (3, "LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3441 getpid (), fds, nfds, vec_len (ldpw->vcl_poll));
3443 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
3456 while ((wait_for_time == -1) ||
3457 (clib_time_now (&ldpw->clib_time) < wait_for_time));
3461 vec_foreach (vp, ldpw->vcl_poll)
3463 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3464 fds[vp->fds_ndx].revents = vp->revents;
3465 #ifdef __USE_XOPEN2K
3466 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3467 (fds[vp->fds_ndx].events & POLLRDNORM))
3468 fds[vp->fds_ndx].revents |= POLLRDNORM;
3469 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3470 (fds[vp->fds_ndx].events & POLLWRNORM))
3471 fds[vp->fds_ndx].revents |= POLLWRNORM;
3474 vec_reset_length (ldpw->vcl_poll);
3476 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
3478 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
3480 vec_reset_length (ldpw->libc_poll_idxs);
3481 vec_reset_length (ldpw->libc_poll);
3487 int errno_val = errno;
3489 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3490 "rv %d, errno = %d", getpid (),
3491 func_str, rv, errno_val);
3496 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3497 "n_libc_fds %d", getpid (), rv, rv,
3498 vec_len (ldpw->vcl_poll), vec_len (ldpw->libc_poll));
3500 for (i = 0; i < nfds; i++)
3505 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3506 ".events = 0x%x, .revents = 0x%x",
3507 getpid (), i, fds[i].fd, fds[i].fd,
3508 fds[i].events, fds[i].revents);
3519 ppoll (struct pollfd *fds, nfds_t nfds,
3520 const struct timespec *timeout, const sigset_t * sigmask)
3522 if ((errno = -ldp_init ()))
3525 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3533 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3535 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3538 * This function is called when the library is loaded
3541 ldp_constructor (void)
3543 swrap_constructor ();
3544 if (ldp_init () != 0)
3545 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3547 else if (LDP_DEBUG > 0)
3548 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3552 * This function is called when the library is unloaded
3555 ldp_destructor (void)
3557 swrap_destructor ();
3561 /* Don't use clib_warning() here because that calls writev()
3562 * which will call ldp_init().
3565 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3566 __func__, __LINE__, getpid ());
3571 * fd.io coding-style-patch-verification: ON
3574 * eval: (c-set-style "gnu")