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 typedef struct ldp_fd_entry_
62 char app_name[LDP_APP_NAME_MAX];
67 clib_time_t clib_time;
68 clib_bitmap_t *rd_bitmap;
69 clib_bitmap_t *wr_bitmap;
70 clib_bitmap_t *ex_bitmap;
71 clib_bitmap_t *sid_rd_bitmap;
72 clib_bitmap_t *sid_wr_bitmap;
73 clib_bitmap_t *sid_ex_bitmap;
74 clib_bitmap_t *libc_rd_bitmap;
75 clib_bitmap_t *libc_wr_bitmap;
76 clib_bitmap_t *libc_ex_bitmap;
78 struct pollfd *libc_poll;
82 u8 vcl_needs_real_epoll; /*< vcl needs next epoll_create to
85 ldp_fd_entry_t *fd_pool;
86 clib_rwlock_t fd_table_lock;
87 uword *sid_to_fd_table;
89 #define LDP_DEBUG ldp->debug
91 #define LDBG(_lvl, _fmt, _args...) \
92 if (ldp->debug > _lvl) \
93 clib_warning (_fmt, ##_args)
95 static ldp_main_t ldp_main = {
96 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
97 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
98 .debug = LDP_DEBUG_INIT,
101 static ldp_main_t *ldp = &ldp_main;
104 * RETURN: 0 on success or -1 on error.
107 ldp_set_app_name (char *app_name)
109 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
110 "ldp-%d-%s", getpid (), app_name);
112 if (rv >= LDP_APP_NAME_MAX)
113 app_name[LDP_APP_NAME_MAX - 1] = 0;
119 if (ldp->app_name[0] == '\0')
120 ldp_set_app_name ("app");
122 return ldp->app_name;
126 ldp_fd_alloc (u32 sid)
130 clib_rwlock_writer_lock (&ldp->fd_table_lock);
131 if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sid_bit_val)
133 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
136 pool_get (ldp->fd_pool, fde);
138 fde->fd_index = fde - ldp->fd_pool;
139 fde->fd = fde->fd_index + ldp->sid_bit_val;
140 hash_set (ldp->sid_to_fd_table, sid, fde->fd);
141 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
145 static ldp_fd_entry_t *
146 ldp_fd_entry_get_w_lock (u32 fd_index)
148 clib_rwlock_reader_lock (&ldp->fd_table_lock);
149 if (pool_is_free_index (ldp->fd_pool, fd_index))
152 return pool_elt_at_index (ldp->fd_pool, fd_index);
156 ldp_fd_from_sid (u32 sid)
161 clib_rwlock_reader_lock (&ldp->fd_table_lock);
162 fdp = hash_get (ldp->sid_to_fd_table, sid);
163 fd = fdp ? *fdp : -EMFILE;
164 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
170 ldp_fd_is_sid (int fd)
172 return fd >= ldp->sid_bit_val;
176 ldp_sid_from_fd (int fd)
181 if (!ldp_fd_is_sid (fd))
182 return INVALID_SESSION_ID;
184 fd_index = fd - ldp->sid_bit_val;
185 fde = ldp_fd_entry_get_w_lock (fd_index);
186 sid = fde ? fde->sid : INVALID_SESSION_ID;
187 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
190 sid = vppcom_session_handle (vppcom_session_index (sid));
195 ldp_fd_free_w_sid (u32 sid)
201 fd = ldp_fd_from_sid (sid);
205 fd_index = fd - ldp->sid_bit_val;
206 fde = ldp_fd_entry_get_w_lock (fd_index);
209 hash_unset (ldp->sid_to_fd_table, fde->sid);
210 pool_put (ldp->fd_pool, fde);
212 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
220 if (PREDICT_TRUE (ldp->init))
224 ldp->vcl_needs_real_epoll = 1;
225 rv = vppcom_app_create (ldp_get_app_name ());
228 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
229 " failed! rv = %d (%s)\n",
230 getpid (), rv, vppcom_retval_str (rv));
234 ldp->vcl_needs_real_epoll = 0;
236 char *env_var_str = getenv (LDP_ENV_DEBUG);
240 if (sscanf (env_var_str, "%u", &tmp) != 1)
241 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
242 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
247 LDBG (0, "LDP<%d>: configured LDP debug level (%u) from env var "
248 LDP_ENV_DEBUG "!", getpid (), ldp->debug);
252 env_var_str = getenv (LDP_ENV_APP_NAME);
255 ldp_set_app_name (env_var_str);
256 LDBG (0, "LDP<%d>: configured LDP app name (%s) from the env var "
257 LDP_ENV_APP_NAME "!", getpid (), ldp->app_name);
260 env_var_str = getenv (LDP_ENV_SID_BIT);
264 if (sscanf (env_var_str, "%u", &sb) != 1)
266 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
267 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
268 "value %d (0x%x)", getpid (), env_var_str,
269 ldp->sid_bit_val, ldp->sid_bit_val);
271 else if (sb < LDP_SID_BIT_MIN)
273 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
274 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
276 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
277 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
278 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
279 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
280 ldp->sid_bit_val, ldp->sid_bit_val);
282 else if (sb > LDP_SID_BIT_MAX)
284 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
285 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
287 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
288 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
289 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
290 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
291 ldp->sid_bit_val, ldp->sid_bit_val);
295 ldp->sid_bit_val = (1 << sb);
296 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
298 LDBG (0, "LDP<%d>: configured LDP sid bit (%u) from "
299 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", getpid (), sb,
300 ldp->sid_bit_val, ldp->sid_bit_val);
304 clib_time_init (&ldp->clib_time);
305 clib_rwlock_init (&ldp->fd_table_lock);
306 LDBG (0, "LDP<%d>: LDP initialization: done!", getpid ());
315 const char *func_str;
316 u32 sid = ldp_sid_from_fd (fd);
318 if ((errno = -ldp_init ()))
321 if (sid != INVALID_SESSION_ID)
325 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
326 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
329 func_str = "libc_close";
331 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
332 getpid (), fd, fd, func_str, epfd, epfd);
334 rv = libc_close (epfd);
337 u32 size = sizeof (epfd);
340 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
344 else if (PREDICT_FALSE (epfd < 0))
351 func_str = "vppcom_session_close";
353 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
354 getpid (), fd, fd, func_str, sid, sid);
356 ldp_fd_free_w_sid (sid);
357 rv = vppcom_session_close (sid);
366 func_str = "libc_close";
368 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s()", getpid (), fd, fd,
371 rv = libc_close (fd);
379 int errno_val = errno;
381 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
382 "rv %d, errno = %d", getpid (), fd, fd,
383 func_str, rv, errno_val);
387 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
388 getpid (), fd, fd, rv, rv);
394 read (int fd, void *buf, size_t nbytes)
397 const char *func_str;
398 u32 sid = ldp_sid_from_fd (fd);
400 if ((errno = -ldp_init ()))
403 if (sid != INVALID_SESSION_ID)
405 func_str = "vppcom_session_read";
408 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
409 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
410 fd, fd, func_str, sid, sid, buf, nbytes);
412 size = vppcom_session_read (sid, buf, nbytes);
421 func_str = "libc_read";
424 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
425 "buf %p, nbytes %u", getpid (),
426 fd, fd, func_str, buf, nbytes);
428 size = libc_read (fd, buf, nbytes);
435 int errno_val = errno;
437 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
438 "rv %d, errno = %d", getpid (), fd, fd,
439 func_str, size, errno_val);
443 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
444 getpid (), fd, fd, size, size);
450 readv (int fd, const struct iovec * iov, int iovcnt)
452 const char *func_str;
454 u32 sid = ldp_sid_from_fd (fd);
455 int rv = 0, i, total = 0;
457 if ((errno = -ldp_init ()))
460 if (sid != INVALID_SESSION_ID)
462 func_str = "vppcom_session_read";
465 for (i = 0; i < iovcnt; ++i)
468 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
469 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
470 getpid (), fd, fd, func_str, i, sid, sid,
473 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
479 if (rv < iov[i].iov_len)
482 clib_warning ("LDP<%d>: fd %d (0x%x): "
483 "rv (%d) < iov[%d].iov_len (%d)",
484 getpid (), fd, fd, rv, i,
491 while ((rv >= 0) && (total == 0));
503 func_str = "libc_readv";
506 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
507 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
509 size = libc_readv (fd, iov, iovcnt);
516 int errno_val = errno;
518 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
519 "rv %d, errno = %d", getpid (), fd, fd,
520 func_str, size, errno_val);
524 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
525 getpid (), fd, fd, size, size);
531 write (int fd, const void *buf, size_t nbytes)
533 const char *func_str;
535 u32 sid = ldp_sid_from_fd (fd);
537 if ((errno = -ldp_init ()))
540 if (sid != INVALID_SESSION_ID)
542 func_str = "vppcom_session_write";
545 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
546 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
547 fd, fd, func_str, sid, sid, buf, nbytes);
549 size = vppcom_session_write (sid, (void *) buf, nbytes);
558 func_str = "libc_write";
561 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
562 "buf %p, nbytes %u", getpid (),
563 fd, fd, func_str, buf, nbytes);
565 size = libc_write (fd, buf, nbytes);
572 int errno_val = errno;
574 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
575 "rv %d, errno = %d", getpid (), fd, fd,
576 func_str, size, errno_val);
580 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
581 getpid (), fd, fd, size, size);
587 writev (int fd, const struct iovec * iov, int iovcnt)
589 const char *func_str;
590 ssize_t size = 0, total = 0;
591 u32 sid = ldp_sid_from_fd (fd);
595 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
598 if ((errno = -ldp_init ()))
601 if (sid != INVALID_SESSION_ID)
603 func_str = "vppcom_session_write";
606 for (i = 0; i < iovcnt; ++i)
609 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
610 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
611 __func__, __LINE__, getpid (), fd, fd, func_str,
612 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
614 rv = vppcom_session_write (sid, iov[i].iov_base,
621 if (rv < iov[i].iov_len)
624 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
625 "rv (%d) < iov[%d].iov_len (%ld)",
626 __func__, __LINE__, getpid (), fd, fd,
627 rv, i, iov[i].iov_len);
633 while ((rv >= 0) && (total == 0));
645 func_str = "libc_writev";
648 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
649 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
650 fd, fd, func_str, iov, iovcnt);
652 size = libc_writev (fd, iov, iovcnt);
659 int errno_val = errno;
662 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
663 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
664 fd, func_str, size, errno_val);
668 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
669 __func__, __LINE__, getpid (), fd, fd, size);
675 fcntl (int fd, int cmd, ...)
677 const char *func_str = __func__;
680 u32 sid = ldp_sid_from_fd (fd);
682 if ((errno = -ldp_init ()))
686 if (sid != INVALID_SESSION_ID)
688 int flags = va_arg (ap, int);
691 size = sizeof (flags);
696 func_str = "vppcom_session_attr[SET_FLAGS]";
697 LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
698 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
699 sid, flags, flags, size);
701 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
706 func_str = "vppcom_session_attr[GET_FLAGS]";
707 LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
708 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
709 sid, flags, flags, size);
711 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
715 LDBG (2, "LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): %s() "
716 "returned flags %d (0x%x)", getpid (), fd, fd, cmd,
717 func_str, flags, flags);
722 /* TODO handle this */
723 LDBG (0, "F_SETFD ignored flags %u", flags);
738 func_str = "libc_vfcntl";
741 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
742 getpid (), fd, fd, func_str, cmd);
744 rv = libc_vfcntl (fd, cmd, ap);
753 int errno_val = errno;
755 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
756 "rv %d, errno = %d", getpid (), fd, fd,
757 func_str, rv, errno_val);
761 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
762 getpid (), fd, fd, rv, rv);
768 ioctl (int fd, unsigned long int cmd, ...)
770 const char *func_str;
773 u32 sid = ldp_sid_from_fd (fd);
775 if ((errno = -ldp_init ()))
779 if (sid != INVALID_SESSION_ID)
781 func_str = "vppcom_session_attr[GET_NREAD]";
788 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
789 getpid (), fd, fd, func_str, sid, sid);
791 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
796 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
797 u32 size = sizeof (flags);
799 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
800 * non-blocking, the flags should be read here and merged
804 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
805 "sid %u (0x%x), flags %d (0x%x), size %d",
806 getpid (), fd, fd, func_str, sid, sid,
809 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
826 func_str = "libc_vioctl";
829 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
830 getpid (), fd, fd, func_str, cmd);
832 rv = libc_vioctl (fd, cmd, ap);
839 int errno_val = errno;
841 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
842 "rv %d, errno = %d", getpid (), fd, fd,
843 func_str, rv, errno_val);
847 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
848 getpid (), fd, fd, rv, rv);
855 ldp_pselect (int nfds, fd_set * __restrict readfds,
856 fd_set * __restrict writefds,
857 fd_set * __restrict exceptfds,
858 const struct timespec *__restrict timeout,
859 const __sigset_t * __restrict sigmask)
861 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
862 u32 minbits = clib_max (nfds, BITS (uword)), sid;
863 char *func_str = "##";
875 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
876 (f64) 0 : (f64) timeout->tv_sec +
877 (f64) timeout->tv_nsec / (f64) 1000000000;
879 /* select as fine grained sleep */
882 LDBG (3, "LDP<%d>: sleeping for %.02f seconds", getpid (),
885 time_out += clib_time_now (&ldp->clib_time);
886 while (clib_time_now (&ldp->clib_time) < time_out)
900 if (nfds <= ldp->sid_bit_val)
902 func_str = "libc_pselect";
904 LDBG (3, "LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
905 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
906 readfds, writefds, exceptfds, timeout, sigmask);
908 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
913 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
915 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
916 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
917 ldp->sid_bit_val, ldp->sid_bit_val,
918 FD_SETSIZE / 2, FD_SETSIZE / 2);
923 sid_bits = libc_bits = 0;
924 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
927 clib_bitmap_validate (ldp->sid_rd_bitmap, minbits);
928 clib_bitmap_validate (ldp->libc_rd_bitmap, minbits);
929 clib_bitmap_validate (ldp->rd_bitmap, minbits);
930 clib_memcpy_fast (ldp->rd_bitmap, readfds, n_bytes);
931 memset (readfds, 0, n_bytes);
934 clib_bitmap_foreach (fd, ldp->rd_bitmap, ({
937 sid = ldp_sid_from_fd (fd);
938 LDBG (3, "LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
939 getpid (), fd, fd, sid, sid);
940 if (sid == INVALID_SESSION_ID)
941 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
943 clib_bitmap_set_no_check (ldp->sid_rd_bitmap,
944 vppcom_session_index (sid), 1);
948 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
949 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
951 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
952 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
954 LDBG (3, "LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
955 "libc_bits_set %d, libc_bits %d", getpid (), sid_bits_set,
956 sid_bits, libc_bits_set, libc_bits);
960 clib_bitmap_validate (ldp->sid_wr_bitmap, minbits);
961 clib_bitmap_validate (ldp->libc_wr_bitmap, minbits);
962 clib_bitmap_validate (ldp->wr_bitmap, minbits);
963 clib_memcpy_fast (ldp->wr_bitmap, writefds, n_bytes);
964 memset (writefds, 0, n_bytes);
967 clib_bitmap_foreach (fd, ldp->wr_bitmap, ({
970 sid = ldp_sid_from_fd (fd);
971 LDBG (3, "LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
972 getpid (), fd, fd, sid, sid);
973 if (sid == INVALID_SESSION_ID)
974 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
976 clib_bitmap_set_no_check (ldp->sid_wr_bitmap,
977 vppcom_session_index (sid), 1);
981 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
982 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
984 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
985 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
987 LDBG (3, "LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
988 "libc_bits_set %d, libc_bits %d", getpid (),
989 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
993 clib_bitmap_validate (ldp->sid_ex_bitmap, minbits);
994 clib_bitmap_validate (ldp->libc_ex_bitmap, minbits);
995 clib_bitmap_validate (ldp->ex_bitmap, minbits);
996 clib_memcpy_fast (ldp->ex_bitmap, exceptfds, n_bytes);
997 memset (exceptfds, 0, n_bytes);
1000 clib_bitmap_foreach (fd, ldp->ex_bitmap, ({
1003 sid = ldp_sid_from_fd (fd);
1004 LDBG (3, "LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
1005 getpid (), fd, fd, sid, sid);
1006 if (sid == INVALID_SESSION_ID)
1007 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
1009 clib_bitmap_set_no_check (ldp->sid_ex_bitmap,
1010 vppcom_session_index (sid), 1);
1014 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
1015 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
1017 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
1018 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
1020 LDBG (3, "LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
1021 "libc_bits_set %d, libc_bits %d", getpid (),
1022 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
1025 if (PREDICT_FALSE (!sid_bits && !libc_bits))
1036 if (!ldp->select_vcl)
1038 func_str = "vppcom_select";
1041 clib_memcpy_fast (ldp->rd_bitmap, ldp->sid_rd_bitmap,
1042 vec_len (ldp->rd_bitmap) *
1043 sizeof (clib_bitmap_t));
1045 clib_memcpy_fast (ldp->wr_bitmap, ldp->sid_wr_bitmap,
1046 vec_len (ldp->wr_bitmap) *
1047 sizeof (clib_bitmap_t));
1049 clib_memcpy_fast (ldp->ex_bitmap, ldp->sid_ex_bitmap,
1050 vec_len (ldp->ex_bitmap) *
1051 sizeof (clib_bitmap_t));
1053 rv = vppcom_select (sid_bits,
1054 readfds ? ldp->rd_bitmap : NULL,
1055 writefds ? ldp->wr_bitmap : NULL,
1056 exceptfds ? ldp->ex_bitmap : NULL, 0);
1067 clib_bitmap_foreach (sid, ldp->rd_bitmap,
1069 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1070 if (PREDICT_FALSE (fd < 0))
1076 FD_SET (fd, readfds);
1083 clib_bitmap_foreach (sid, ldp->wr_bitmap,
1085 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1086 if (PREDICT_FALSE (fd < 0))
1092 FD_SET (fd, writefds);
1099 clib_bitmap_foreach (sid, ldp->ex_bitmap,
1101 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1102 if (PREDICT_FALSE (fd < 0))
1108 FD_SET (fd, exceptfds);
1112 ldp->select_vcl = 1;
1117 ldp->select_vcl = 0;
1121 struct timespec tspec;
1123 func_str = "libc_pselect";
1126 clib_memcpy_fast (readfds, ldp->libc_rd_bitmap,
1127 vec_len (ldp->rd_bitmap) *
1128 sizeof (clib_bitmap_t));
1130 clib_memcpy_fast (writefds, ldp->libc_wr_bitmap,
1131 vec_len (ldp->wr_bitmap) *
1132 sizeof (clib_bitmap_t));
1134 clib_memcpy_fast (exceptfds, ldp->libc_ex_bitmap,
1135 vec_len (ldp->ex_bitmap) *
1136 sizeof (clib_bitmap_t));
1137 tspec.tv_sec = tspec.tv_nsec = 0;
1138 rv = libc_pselect (libc_bits,
1139 readfds ? readfds : NULL,
1140 writefds ? writefds : NULL,
1141 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1146 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1150 /* TBD: set timeout to amount of time left */
1151 clib_bitmap_zero (ldp->rd_bitmap);
1152 clib_bitmap_zero (ldp->sid_rd_bitmap);
1153 clib_bitmap_zero (ldp->libc_rd_bitmap);
1154 clib_bitmap_zero (ldp->wr_bitmap);
1155 clib_bitmap_zero (ldp->sid_wr_bitmap);
1156 clib_bitmap_zero (ldp->libc_wr_bitmap);
1157 clib_bitmap_zero (ldp->ex_bitmap);
1158 clib_bitmap_zero (ldp->sid_ex_bitmap);
1159 clib_bitmap_zero (ldp->libc_ex_bitmap);
1165 int errno_val = errno;
1167 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1168 "rv %d, errno = %d", getpid (),
1169 func_str, rv, errno_val);
1173 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1179 select (int nfds, fd_set * __restrict readfds,
1180 fd_set * __restrict writefds,
1181 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1183 struct timespec tspec;
1187 tspec.tv_sec = timeout->tv_sec;
1188 tspec.tv_nsec = timeout->tv_usec * 1000;
1190 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1191 timeout ? &tspec : NULL, NULL);
1194 #ifdef __USE_XOPEN2K
1196 pselect (int nfds, fd_set * __restrict readfds,
1197 fd_set * __restrict writefds,
1198 fd_set * __restrict exceptfds,
1199 const struct timespec *__restrict timeout,
1200 const __sigset_t * __restrict sigmask)
1202 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1207 socket (int domain, int type, int protocol)
1209 const char *func_str;
1211 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1212 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1214 if ((errno = -ldp_init ()))
1217 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1218 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1221 u8 proto = ((sock_type == SOCK_DGRAM) ?
1222 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1224 func_str = "vppcom_session_create";
1226 LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u",
1227 getpid (), func_str, proto, vppcom_proto_str (proto),
1230 sid = vppcom_session_create (proto, is_nonblocking);
1238 func_str = "ldp_fd_from_sid";
1239 rv = ldp_fd_alloc (sid);
1242 (void) vppcom_session_close (sid);
1250 func_str = "libc_socket";
1252 LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str);
1254 rv = libc_socket (domain, type, protocol);
1261 int errno_val = errno;
1263 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1264 "rv %d, errno = %d",
1265 getpid (), func_str, rv, errno_val);
1269 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1275 * Create two new sockets, of type TYPE in domain DOMAIN and using
1276 * protocol PROTOCOL, which are connected to each other, and put file
1277 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1278 * one will be chosen automatically.
1279 * Returns 0 on success, -1 for errors.
1282 socketpair (int domain, int type, int protocol, int fds[2])
1284 const char *func_str;
1286 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1288 if ((errno = -ldp_init ()))
1291 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1292 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1294 func_str = __func__;
1296 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1302 func_str = "libc_socket";
1304 LDBG (1, "LDP<%d>: : calling %s()", getpid (), func_str);
1306 rv = libc_socketpair (domain, type, protocol, fds);
1313 int errno_val = errno;
1315 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1316 "rv %d, errno = %d",
1317 getpid (), func_str, rv, errno_val);
1321 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1327 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1330 const char *func_str;
1331 u32 sid = ldp_sid_from_fd (fd);
1333 if ((errno = -ldp_init ()))
1336 if (sid != INVALID_SESSION_ID)
1340 func_str = "vppcom_session_bind";
1342 switch (addr->sa_family)
1345 if (len != sizeof (struct sockaddr_in))
1348 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1349 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1354 ep.is_ip4 = VPPCOM_IS_IP4;
1355 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1356 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1360 if (len != sizeof (struct sockaddr_in6))
1363 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1364 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1369 ep.is_ip4 = VPPCOM_IS_IP6;
1370 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1371 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1375 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1376 "Unsupported address family %u!",
1377 getpid (), fd, fd, sid, sid, addr->sa_family);
1378 errno = EAFNOSUPPORT;
1383 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1385 getpid (), fd, fd, func_str, sid, sid, addr, len);
1387 rv = vppcom_session_bind (sid, &ep);
1388 if (rv != VPPCOM_OK)
1396 func_str = "libc_bind";
1399 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1401 getpid (), fd, fd, func_str, addr, len);
1403 rv = libc_bind (fd, addr, len);
1411 int errno_val = errno;
1413 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1414 "rv %d, errno = %d", getpid (), fd, fd,
1415 func_str, rv, errno_val);
1419 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1420 getpid (), fd, fd, rv, rv);
1426 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1427 vppcom_endpt_t * ep)
1430 int sa_len, copy_len;
1432 if ((errno = -ldp_init ()))
1435 if (addr && len && ep)
1437 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1438 switch (addr->sa_family)
1441 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1442 if (*len > sizeof (struct sockaddr_in))
1443 *len = sizeof (struct sockaddr_in);
1444 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1445 copy_len = *len - sa_len;
1447 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1452 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1453 if (*len > sizeof (struct sockaddr_in6))
1454 *len = sizeof (struct sockaddr_in6);
1455 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1456 copy_len = *len - sa_len;
1458 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1459 __in6_u.__u6_addr8, ep->ip, copy_len);
1472 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1475 const char *func_str;
1476 u32 sid = ldp_sid_from_fd (fd);
1478 if ((errno = -ldp_init ()))
1481 if (sid != INVALID_SESSION_ID)
1484 u8 addr_buf[sizeof (struct in6_addr)];
1485 u32 size = sizeof (ep);
1488 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1491 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1493 getpid (), fd, fd, func_str, sid, sid, addr, len);
1495 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1496 if (rv != VPPCOM_OK)
1503 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1504 if (rv != VPPCOM_OK)
1513 func_str = "libc_getsockname";
1516 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1518 getpid (), fd, fd, func_str, addr, len);
1520 rv = libc_getsockname (fd, addr, len);
1527 int errno_val = errno;
1529 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1530 "rv %d, errno = %d", getpid (), fd, fd,
1531 func_str, rv, errno_val);
1535 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1536 getpid (), fd, fd, rv, rv);
1542 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1545 const char *func_str = __func__;
1546 u32 sid = ldp_sid_from_fd (fd);
1548 if ((errno = -ldp_init ()))
1553 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1554 getpid (), fd, fd, len);
1560 if (sid != INVALID_SESSION_ID)
1564 func_str = "vppcom_session_connect";
1566 switch (addr->sa_family)
1569 if (len != sizeof (struct sockaddr_in))
1572 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1573 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1578 ep.is_ip4 = VPPCOM_IS_IP4;
1579 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1580 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1584 if (len != sizeof (struct sockaddr_in6))
1587 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1588 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1593 ep.is_ip4 = VPPCOM_IS_IP6;
1594 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1595 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1599 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1600 "Unsupported address family %u!",
1601 getpid (), fd, fd, sid, sid, addr->sa_family);
1602 errno = EAFNOSUPPORT;
1607 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1609 getpid (), fd, fd, func_str, sid, sid, addr, len);
1611 rv = vppcom_session_connect (sid, &ep);
1612 if (rv != VPPCOM_OK)
1620 func_str = "libc_connect";
1623 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1625 getpid (), fd, fd, func_str, addr, len);
1627 rv = libc_connect (fd, addr, len);
1635 int errno_val = errno;
1637 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1638 "rv %d, errno = %d", getpid (), fd, fd,
1639 func_str, rv, errno_val);
1643 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1644 getpid (), fd, fd, rv, rv);
1650 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1653 const char *func_str;
1654 u32 sid = ldp_sid_from_fd (fd);
1656 if ((errno = -ldp_init ()))
1659 if (sid != INVALID_SESSION_ID)
1662 u8 addr_buf[sizeof (struct in6_addr)];
1663 u32 size = sizeof (ep);
1666 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1669 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1671 getpid (), fd, fd, func_str, sid, sid, addr, len);
1673 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1674 if (rv != VPPCOM_OK)
1681 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1682 if (rv != VPPCOM_OK)
1691 func_str = "libc_getpeername";
1694 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1696 getpid (), fd, fd, func_str, addr, len);
1698 rv = libc_getpeername (fd, addr, len);
1705 int errno_val = errno;
1707 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1708 "rv %d, errno = %d", getpid (), fd, fd,
1709 func_str, rv, errno_val);
1713 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1714 getpid (), fd, fd, rv, rv);
1720 send (int fd, const void *buf, size_t n, int flags)
1723 const char *func_str;
1724 u32 sid = ldp_sid_from_fd (fd);
1726 if ((errno = -ldp_init ()))
1729 if (sid != INVALID_SESSION_ID)
1732 func_str = "vppcom_session_sendto";
1735 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1736 "buf %p, n %u, flags 0x%x",
1737 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1739 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1740 if (size < VPPCOM_OK)
1748 func_str = "libc_send";
1751 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1752 "buf %p, n %u, flags 0x%x",
1753 getpid (), fd, fd, func_str, buf, n, flags);
1755 size = libc_send (fd, buf, n, flags);
1762 int errno_val = errno;
1764 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1765 "rv %d, errno = %d", getpid (), fd, fd,
1766 func_str, size, errno_val);
1770 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1771 getpid (), fd, fd, size, size);
1777 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1780 const char *func_str;
1781 u32 sid = ldp_sid_from_fd (out_fd);
1783 if ((errno = -ldp_init ()))
1786 if (sid != INVALID_SESSION_ID)
1789 ssize_t results = 0;
1790 size_t n_bytes_left = len;
1791 size_t bytes_to_read;
1795 u32 flags, flags_len = sizeof (flags);
1797 func_str = "vppcom_session_attr[GET_FLAGS]";
1798 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1800 if (PREDICT_FALSE (rv != VPPCOM_OK))
1802 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1803 "sid %u (0x%x), returned %d (%s)!", getpid (),
1804 out_fd, out_fd, func_str, sid, sid, rv,
1805 vppcom_retval_str (rv));
1807 vec_reset_length (ldp->io_buffer);
1815 off_t off = lseek (in_fd, *offset, SEEK_SET);
1816 if (PREDICT_FALSE (off == -1))
1820 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1821 "SEEK_SET failed: in_fd %d, offset %p, "
1822 "*offset %ld, rv %ld, errno %d", getpid (),
1823 out_fd, out_fd, in_fd, offset, *offset, off,
1830 ASSERT (off == *offset);
1835 func_str = "vppcom_session_attr[GET_NWRITE]";
1836 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1840 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1841 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1842 sid, sid, size, vppcom_retval_str (size));
1843 vec_reset_length (ldp->io_buffer);
1849 bytes_to_read = size;
1852 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1853 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1854 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1857 if (bytes_to_read == 0)
1859 if (flags & O_NONBLOCK)
1864 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1866 getpid (), out_fd, out_fd, sid, sid);
1874 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1875 vec_validate (ldp->io_buffer, bytes_to_read);
1876 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1879 func_str = "libc_read";
1881 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1882 "io_buffer %p, bytes_to_read %lu, rv %d, "
1883 "errno %d", getpid (), out_fd, out_fd, func_str,
1884 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1890 vec_reset_length (ldp->io_buffer);
1896 func_str = "vppcom_session_write";
1899 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1900 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1901 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1902 results, n_bytes_left);
1904 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1907 if (size == VPPCOM_EAGAIN)
1909 if (flags & O_NONBLOCK)
1915 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1916 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1926 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1927 "sid %u, io_buffer %p, nbytes %u "
1929 getpid (), out_fd, out_fd, func_str,
1930 sid, ldp->io_buffer, nbytes,
1931 size, vppcom_retval_str (size));
1935 vec_reset_length (ldp->io_buffer);
1944 ASSERT (n_bytes_left >= nbytes);
1945 n_bytes_left = n_bytes_left - nbytes;
1947 while (n_bytes_left > 0);
1950 vec_reset_length (ldp->io_buffer);
1953 off_t off = lseek (in_fd, *offset, SEEK_SET);
1954 if (PREDICT_FALSE (off == -1))
1958 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1959 "in_fd %d, offset %p, *offset %ld, "
1960 "rv %ld, errno %d", getpid (), in_fd,
1961 offset, *offset, off, errno_val);
1967 ASSERT (off == *offset);
1968 *offset += results + 1;
1980 func_str = "libc_send";
1983 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1984 "in_fd %d, offset %p, len %u",
1985 getpid (), out_fd, out_fd, func_str,
1986 in_fd, offset, len);
1988 size = libc_sendfile (out_fd, in_fd, offset, len);
1996 int errno_val = errno;
1998 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1999 "rv %d, errno = %d", getpid (), out_fd, out_fd,
2000 func_str, size, errno_val);
2004 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2005 getpid (), out_fd, out_fd, size, size);
2011 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
2013 return sendfile (out_fd, in_fd, offset, len);
2017 recv (int fd, void *buf, size_t n, int flags)
2020 const char *func_str;
2021 u32 sid = ldp_sid_from_fd (fd);
2023 if ((errno = -ldp_init ()))
2026 if (sid != INVALID_SESSION_ID)
2028 func_str = "vppcom_session_recvfrom";
2031 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2032 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
2033 fd, fd, func_str, sid, sid, buf, n, flags);
2035 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2044 func_str = "libc_recv";
2047 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2048 "buf %p, n %u, flags 0x%x", getpid (),
2049 fd, fd, func_str, buf, n, flags);
2051 size = libc_recv (fd, buf, n, flags);
2058 int errno_val = errno;
2060 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2061 "rv %d, errno = %d", getpid (), fd, fd,
2062 func_str, size, errno_val);
2066 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2067 getpid (), fd, fd, size, size);
2073 sendto (int fd, const void *buf, size_t n, int flags,
2074 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2077 const char *func_str = __func__;
2078 u32 sid = ldp_sid_from_fd (fd);
2080 if ((errno = -ldp_init ()))
2083 if (sid != INVALID_SESSION_ID)
2085 vppcom_endpt_t *ep = 0;
2091 switch (addr->sa_family)
2094 ep->is_ip4 = VPPCOM_IS_IP4;
2096 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2098 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2102 ep->is_ip4 = VPPCOM_IS_IP6;
2104 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2106 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2110 errno = EAFNOSUPPORT;
2116 func_str = "vppcom_session_sendto";
2119 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2120 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2121 getpid (), fd, fd, func_str, sid, sid, buf, n,
2124 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2133 func_str = "libc_sendto";
2136 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2137 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2138 getpid (), fd, fd, func_str, buf, n, flags,
2141 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2149 int errno_val = errno;
2151 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2152 "rv %d, errno = %d", getpid (), fd, fd,
2153 func_str, size, errno_val);
2157 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2158 getpid (), fd, fd, size, size);
2164 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2165 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2168 const char *func_str;
2169 u32 sid = ldp_sid_from_fd (fd);
2171 if ((errno = -ldp_init ()))
2174 if (sid != INVALID_SESSION_ID)
2177 u8 src_addr[sizeof (struct sockaddr_in6)];
2179 func_str = "vppcom_session_recvfrom";
2182 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2183 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2184 getpid (), fd, fd, func_str, sid, sid, buf, n,
2189 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2192 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2195 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2205 func_str = "libc_recvfrom";
2208 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2209 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2210 getpid (), fd, fd, func_str, buf, n, flags,
2213 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2220 int errno_val = errno;
2222 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2223 "rv %d, errno = %d", getpid (), fd, fd,
2224 func_str, size, errno_val);
2228 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2229 getpid (), fd, fd, size, size);
2235 sendmsg (int fd, const struct msghdr * message, int flags)
2238 const char *func_str;
2239 u32 sid = ldp_sid_from_fd (fd);
2241 if ((errno = -ldp_init ()))
2244 if (sid != INVALID_SESSION_ID)
2246 func_str = __func__;
2248 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2254 func_str = "libc_sendmsg";
2257 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2258 "message %p, flags 0x%x",
2259 getpid (), fd, fd, func_str, message, flags);
2261 size = libc_sendmsg (fd, message, flags);
2268 int errno_val = errno;
2270 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2271 "rv %d, errno = %d", getpid (), fd, fd,
2272 func_str, size, errno_val);
2276 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2277 getpid (), fd, fd, size, size);
2284 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2287 const char *func_str;
2288 u32 sid = ldp_sid_from_fd (fd);
2290 if ((errno = -ldp_init ()))
2293 if (sid != INVALID_SESSION_ID)
2295 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2301 func_str = "libc_sendmmsg";
2304 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2305 "vmessages %p, vlen %u, flags 0x%x",
2306 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2308 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2315 int errno_val = errno;
2317 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2318 "rv %d, errno = %d", getpid (), fd, fd,
2319 func_str, size, errno_val);
2323 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2324 getpid (), fd, fd, size, size);
2331 recvmsg (int fd, struct msghdr * message, int flags)
2334 const char *func_str;
2335 u32 sid = ldp_sid_from_fd (fd);
2337 if ((errno = -ldp_init ()))
2340 if (sid != INVALID_SESSION_ID)
2342 func_str = __func__;
2344 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2350 func_str = "libc_recvmsg";
2353 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2354 "message %p, flags 0x%x",
2355 getpid (), fd, fd, func_str, message, flags);
2357 size = libc_recvmsg (fd, message, flags);
2364 int errno_val = errno;
2366 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2367 "rv %d, errno = %d", getpid (), fd, fd,
2368 func_str, size, errno_val);
2372 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2373 getpid (), fd, fd, size, size);
2380 recvmmsg (int fd, struct mmsghdr *vmessages,
2381 unsigned int vlen, int flags, struct timespec *tmo)
2384 const char *func_str;
2385 u32 sid = ldp_sid_from_fd (fd);
2387 if ((errno = -ldp_init ()))
2390 if (sid != INVALID_SESSION_ID)
2392 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2398 func_str = "libc_recvmmsg";
2401 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2402 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2403 getpid (), fd, fd, func_str, vmessages, vlen,
2406 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2413 int errno_val = errno;
2415 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2416 "rv %d, errno = %d", getpid (), fd, fd,
2417 func_str, size, errno_val);
2421 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2422 getpid (), fd, fd, size, size);
2429 getsockopt (int fd, int level, int optname,
2430 void *__restrict optval, socklen_t * __restrict optlen)
2433 const char *func_str = __func__;
2434 u32 sid = ldp_sid_from_fd (fd);
2435 u32 buflen = optlen ? (u32) * optlen : 0;
2437 if ((errno = -ldp_init ()))
2440 if (sid != INVALID_SESSION_ID)
2450 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2452 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2454 getpid (), fd, fd, func_str, sid, sid);
2455 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2459 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2461 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2463 getpid (), fd, fd, func_str, sid, sid);
2464 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2468 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2470 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2472 getpid (), fd, fd, func_str, sid, sid);
2473 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2477 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2479 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2480 "sid %u (0x%x), SOL_TCP",
2481 getpid (), fd, fd, func_str, sid, sid);
2482 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2486 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2489 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2490 "SOL_TCP, TCP_INFO, optval %p, "
2491 "optlen %d: #LDP-NOP#",
2492 getpid (), fd, fd, sid, sid,
2494 memset (optval, 0, *optlen);
2502 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2503 "sid %u (0x%x), SOL_TCP, "
2504 "optname %d unsupported!",
2505 getpid (), fd, fd, func_str, sid, sid, optname);
2513 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2515 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2517 getpid (), fd, fd, func_str, sid, sid);
2518 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2523 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2524 "sid %u (0x%x), SOL_IPV6, "
2525 "optname %d unsupported!",
2526 getpid (), fd, fd, func_str, sid, sid, optname);
2534 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2536 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2538 getpid (), fd, fd, func_str, sid, sid);
2539 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2543 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2545 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2547 getpid (), fd, fd, func_str, sid, sid);
2548 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2552 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
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_PROTOCOL,
2559 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2562 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2564 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2565 "sid %u (0x%x), optlen %d",
2566 getpid (), fd, fd, func_str, sid, sid, buflen);
2567 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2571 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2573 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2574 "sid %u (0x%x), optlen %d",
2575 getpid (), fd, fd, func_str, sid, sid, buflen);
2576 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2580 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2582 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2584 getpid (), fd, fd, func_str, sid, sid);
2585 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2589 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2591 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2593 getpid (), fd, fd, func_str, sid, sid);
2594 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2598 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2600 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2602 getpid (), fd, fd, func_str, sid, sid);
2603 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2608 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2609 "sid %u (0x%x), SOL_SOCKET, "
2610 "optname %d unsupported!",
2611 getpid (), fd, fd, func_str, sid, sid, optname);
2619 if (rv != VPPCOM_OK)
2627 func_str = "libc_getsockopt";
2630 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2631 "optname %d, optval %p, optlen %d",
2632 getpid (), fd, fd, func_str, level, optname,
2635 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2642 int errno_val = errno;
2644 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2645 "rv %d, errno = %d", getpid (), fd, fd,
2646 func_str, rv, errno_val);
2650 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2651 getpid (), fd, fd, rv, rv);
2657 setsockopt (int fd, int level, int optname,
2658 const void *optval, socklen_t optlen)
2661 const char *func_str = __func__;
2662 u32 sid = ldp_sid_from_fd (fd);
2664 if ((errno = -ldp_init ()))
2667 if (sid != INVALID_SESSION_ID)
2677 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2679 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2681 getpid (), fd, fd, func_str, sid, sid);
2682 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2683 (void *) optval, &optlen);
2686 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2688 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2690 getpid (), fd, fd, func_str, sid, sid);
2691 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2692 (void *) optval, &optlen);
2695 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2697 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2699 getpid (), fd, fd, func_str, sid, sid);
2700 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2701 (void *) optval, &optlen);
2704 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2706 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2707 "sid %u (0x%x), SOL_TCP",
2708 getpid (), fd, fd, func_str, sid, sid);
2709 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2710 (void *) optval, &optlen);
2714 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2715 "sid %u (0x%x), SOL_TCP, "
2716 "optname %d unsupported!",
2717 getpid (), fd, fd, func_str, sid, sid, optname);
2725 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
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_V6ONLY,
2731 (void *) optval, &optlen);
2735 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2736 "sid %u (0x%x), SOL_IPV6, "
2737 "optname %d unsupported!",
2738 getpid (), fd, fd, func_str, sid, sid, optname);
2746 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2748 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2750 getpid (), fd, fd, func_str, sid, sid);
2751 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2752 (void *) optval, &optlen);
2755 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2757 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2759 getpid (), fd, fd, func_str, sid, sid);
2760 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2761 (void *) optval, &optlen);
2764 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
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_BROADCAST,
2770 (void *) optval, &optlen);
2774 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2775 "sid %u (0x%x), SOL_SOCKET, "
2776 "optname %d unsupported!",
2777 getpid (), fd, fd, func_str, sid, sid, optname);
2785 if (rv != VPPCOM_OK)
2793 func_str = "libc_setsockopt";
2796 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2797 "optname %d, optval %p, optlen %d",
2798 getpid (), fd, fd, func_str, level, optname,
2801 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2808 int errno_val = errno;
2810 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2811 "rv %d, errno = %d", getpid (), fd, fd,
2812 func_str, rv, errno_val);
2816 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2817 getpid (), fd, fd, rv, rv);
2823 listen (int fd, int n)
2826 const char *func_str;
2827 u32 sid = ldp_sid_from_fd (fd);
2829 if ((errno = -ldp_init ()))
2832 if (sid != INVALID_SESSION_ID)
2834 func_str = "vppcom_session_listen";
2836 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2837 getpid (), fd, fd, func_str, sid, sid, n);
2839 rv = vppcom_session_listen (sid, n);
2840 if (rv != VPPCOM_OK)
2848 func_str = "libc_listen";
2850 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd,
2853 rv = libc_listen (fd, n);
2860 int errno_val = errno;
2862 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2863 "rv %d, errno = %d", getpid (), fd, fd,
2864 func_str, rv, errno_val);
2868 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2869 getpid (), fd, fd, rv, rv);
2875 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2876 socklen_t * __restrict addr_len, int flags)
2879 const char *func_str;
2880 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2883 if ((errno = -ldp_init ()))
2886 if (listen_sid != INVALID_SESSION_ID)
2889 u8 src_addr[sizeof (struct sockaddr_in6)];
2890 memset (&ep, 0, sizeof (ep));
2893 func_str = "vppcom_session_accept";
2896 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2897 "listen sid %u (0x%x), ep %p, flags 0x%x",
2898 getpid (), listen_fd, listen_fd, func_str,
2899 listen_sid, listen_sid, ep, flags);
2901 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2904 errno = -accept_sid;
2909 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2910 if (rv != VPPCOM_OK)
2912 (void) vppcom_session_close ((u32) accept_sid);
2918 func_str = "ldp_fd_from_sid";
2920 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2921 "accept sid %u (0x%x), ep %p, flags 0x%x",
2922 getpid (), listen_fd, listen_fd,
2923 func_str, accept_sid, accept_sid, ep, flags);
2924 rv = ldp_fd_alloc ((u32) accept_sid);
2927 (void) vppcom_session_close ((u32) accept_sid);
2936 func_str = "libc_accept4";
2939 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2940 "addr %p, addr_len %p, flags 0x%x",
2941 getpid (), listen_fd, listen_fd, func_str,
2942 addr, addr_len, flags);
2944 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2951 int errno_val = errno;
2953 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2954 "rv %d, errno = %d", getpid (), listen_fd,
2955 listen_fd, func_str, rv, errno_val);
2959 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2960 getpid (), listen_fd, listen_fd, rv, rv);
2966 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2969 return ldp_accept4 (fd, addr, addr_len, flags);
2973 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2975 return ldp_accept4 (fd, addr, addr_len, 0);
2979 shutdown (int fd, int how)
2982 const char *func_str;
2983 u32 sid = ldp_sid_from_fd (fd);
2985 if ((errno = -ldp_init ()))
2988 if (sid != INVALID_SESSION_ID)
2990 func_str = "vppcom_session_close[TODO]";
2995 func_str = "libc_shutdown";
2998 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2999 getpid (), fd, fd, func_str, how);
3001 rv = libc_shutdown (fd, how);
3008 int errno_val = errno;
3010 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3011 "rv %d, errno = %d", getpid (), fd, fd,
3012 func_str, rv, errno_val);
3016 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3017 getpid (), fd, fd, rv, rv);
3023 epoll_create1 (int flags)
3025 const char *func_str;
3028 if ((errno = -ldp_init ()))
3031 if (ldp->vcl_needs_real_epoll)
3033 rv = libc_epoll_create1 (flags);
3034 ldp->vcl_needs_real_epoll = 0;
3035 ldp->vcl_mq_epfd = rv;
3036 LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv);
3039 func_str = "vppcom_epoll_create";
3041 LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str);
3043 rv = vppcom_epoll_create ();
3045 if (PREDICT_FALSE (rv < 0))
3051 rv = ldp_fd_alloc ((u32) rv);
3057 int errno_val = errno;
3059 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3060 "rv %d, errno = %d",
3061 getpid (), func_str, rv, errno_val);
3065 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
3071 epoll_create (int size)
3073 return epoll_create1 (0);
3077 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3079 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
3080 const char *func_str;
3083 if ((errno = -ldp_init ()))
3086 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3088 /* The LDP epoll_create1 always creates VCL epfd's.
3089 * The app should never have a kernel base epoll fd unless it
3090 * was acquired outside of the LD_PRELOAD process context.
3091 * In any case, if we get one, punt it to libc_epoll_ctl.
3093 func_str = "libc_epoll_ctl";
3095 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
3096 " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event);
3098 rv = libc_epoll_ctl (epfd, op, fd, event);
3102 sid = ldp_sid_from_fd (fd);
3104 LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
3105 getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid);
3107 if (sid != INVALID_SESSION_ID)
3109 func_str = "vppcom_epoll_ctl";
3111 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3112 " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd,
3113 func_str, vep_idx, vep_idx, sid, sid, event);
3115 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3116 if (rv != VPPCOM_OK)
3125 u32 size = sizeof (epfd);
3127 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3128 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
3130 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() "
3131 "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd,
3132 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
3136 func_str = "libc_epoll_create1";
3138 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3139 "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd,
3140 vep_idx, vep_idx, func_str);
3142 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3149 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3150 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3151 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3152 getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3155 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3164 else if (PREDICT_FALSE (libc_epfd < 0))
3171 func_str = "libc_epoll_ctl";
3173 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3174 "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str,
3175 libc_epfd, libc_epfd, op, fd, fd, event);
3177 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3185 int errno_val = errno;
3187 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3188 "rv %d, errno = %d", getpid (), fd, fd,
3189 func_str, rv, errno_val);
3193 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3194 getpid (), fd, fd, rv, rv);
3200 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3201 int timeout, const sigset_t * sigmask)
3203 double time_to_wait = (double) 0, time_out, now = 0;
3204 u32 vep_idx = ldp_sid_from_fd (epfd);
3205 int libc_epfd, rv = 0;
3206 const char *func_str;
3208 if ((errno = -ldp_init ()))
3211 if (PREDICT_FALSE (!events || (timeout < -1)))
3217 if (epfd == ldp->vcl_mq_epfd)
3218 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3220 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3222 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3223 getpid (), epfd, epfd, vep_idx, vep_idx);
3228 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3229 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3231 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3232 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3233 if (PREDICT_FALSE (libc_epfd < 0))
3240 LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3241 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3242 getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3243 maxevents, timeout, sigmask, time_to_wait, time_out);
3246 if (!ldp->epoll_wait_vcl)
3248 func_str = "vppcom_epoll_wait";
3250 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3251 " events %p, maxevents %d", getpid (), epfd, epfd, func_str,
3252 vep_idx, vep_idx, events, maxevents);
3254 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3257 ldp->epoll_wait_vcl = 1;
3268 ldp->epoll_wait_vcl = 0;
3272 func_str = "libc_epoll_pwait";
3274 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d "
3275 "(0x%x), events %p, maxevents %d, sigmask %p", getpid (),
3276 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3277 maxevents, sigmask);
3279 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3285 now = clib_time_now (&ldp->clib_time);
3287 while (now < time_out);
3296 int errno_val = errno;
3298 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3299 "rv %d, errno = %d", getpid (), epfd, epfd,
3300 func_str, rv, errno_val);
3304 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3305 getpid (), epfd, epfd, rv, rv);
3311 epoll_pwait (int epfd, struct epoll_event *events,
3312 int maxevents, int timeout, const sigset_t * sigmask)
3314 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3318 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3320 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3324 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3326 const char *func_str = __func__;
3327 int rv, i, n_revents = 0;
3330 double wait_for_time;
3332 LDBG (3, "LDP<%d>: fds %p, nfds %d, timeout %d", getpid (), fds, nfds,
3336 wait_for_time = (f64) timeout / 1000;
3340 for (i = 0; i < nfds; i++)
3345 LDBG (3, "LDP<%d>: fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3346 getpid (), i, fds[i].fd, fds[i].fd, fds[i].events,
3349 sid = ldp_sid_from_fd (fds[i].fd);
3350 if (sid != INVALID_SESSION_ID)
3352 fds[i].fd = -fds[i].fd;
3353 vec_add2 (ldp->vcl_poll, vp, 1);
3356 vp->events = fds[i].events;
3357 #ifdef __USE_XOPEN2K
3358 if (fds[i].events & POLLRDNORM)
3359 vp->events |= POLLIN;
3360 if (fds[i].events & POLLWRNORM)
3361 vp->events |= POLLOUT;
3363 vp->revents = fds[i].revents;
3367 vec_add1 (ldp->libc_poll, fds[i]);
3368 vec_add1 (ldp->libc_poll_idxs, i);
3374 if (vec_len (ldp->vcl_poll))
3376 func_str = "vppcom_poll";
3378 LDBG (3, "LDP<%d>: calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3379 "n_libc_fds %u", getpid (), func_str, ldp->vcl_poll,
3380 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3381 vec_len (ldp->libc_poll));
3383 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3394 if (vec_len (ldp->libc_poll))
3396 func_str = "libc_poll";
3398 LDBG (3, "LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3399 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3401 rv = libc_poll (ldp->libc_poll, vec_len (ldp->libc_poll), 0);
3414 while ((wait_for_time == -1) ||
3415 (clib_time_now (&ldp->clib_time) < wait_for_time));
3419 vec_foreach (vp, ldp->vcl_poll)
3421 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3422 fds[vp->fds_ndx].revents = vp->revents;
3423 #ifdef __USE_XOPEN2K
3424 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3425 (fds[vp->fds_ndx].events & POLLRDNORM))
3426 fds[vp->fds_ndx].revents |= POLLRDNORM;
3427 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3428 (fds[vp->fds_ndx].events & POLLWRNORM))
3429 fds[vp->fds_ndx].revents |= POLLWRNORM;
3432 vec_reset_length (ldp->vcl_poll);
3434 for (i = 0; i < vec_len (ldp->libc_poll); i++)
3436 fds[ldp->libc_poll_idxs[i]].revents = ldp->libc_poll[i].revents;
3438 vec_reset_length (ldp->libc_poll_idxs);
3439 vec_reset_length (ldp->libc_poll);
3445 int errno_val = errno;
3447 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3448 "rv %d, errno = %d", getpid (),
3449 func_str, rv, errno_val);
3454 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3455 "n_libc_fds %d", getpid (), rv, rv,
3456 vec_len (ldp->vcl_poll), vec_len (ldp->libc_poll));
3458 for (i = 0; i < nfds; i++)
3463 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3464 ".events = 0x%x, .revents = 0x%x",
3465 getpid (), i, fds[i].fd, fds[i].fd,
3466 fds[i].events, fds[i].revents);
3477 ppoll (struct pollfd *fds, nfds_t nfds,
3478 const struct timespec *timeout, const sigset_t * sigmask)
3480 if ((errno = -ldp_init ()))
3483 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3491 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3493 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3496 * This function is called when the library is loaded
3499 ldp_constructor (void)
3501 swrap_constructor ();
3502 if (ldp_init () != 0)
3503 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3505 else if (LDP_DEBUG > 0)
3506 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3510 * This function is called when the library is unloaded
3513 ldp_destructor (void)
3515 swrap_destructor ();
3518 vppcom_app_destroy ();
3522 /* Don't use clib_warning() here because that calls writev()
3523 * which will call ldp_init().
3526 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3527 __func__, __LINE__, getpid ());
3532 * fd.io coding-style-patch-verification: ON
3535 * eval: (c-set-style "gnu")