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 ("ldp<%d>: " _fmt, getpid(), ##_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 ldp->vcl_needs_real_epoll = 0;
262 if (rv == VPPCOM_EEXIST)
264 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
265 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
269 ldp->vcl_needs_real_epoll = 0;
270 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
271 ldpw = ldp_worker_get_current ();
273 char *env_var_str = getenv (LDP_ENV_DEBUG);
277 if (sscanf (env_var_str, "%u", &tmp) != 1)
278 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
279 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
284 LDBG (0, "configured LDP debug level (%u) from env var "
285 LDP_ENV_DEBUG "!", ldp->debug);
289 env_var_str = getenv (LDP_ENV_APP_NAME);
292 ldp_set_app_name (env_var_str);
293 LDBG (0, "configured LDP app name (%s) from the env var "
294 LDP_ENV_APP_NAME "!", ldp->app_name);
297 env_var_str = getenv (LDP_ENV_SID_BIT);
301 if (sscanf (env_var_str, "%u", &sb) != 1)
303 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
304 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
305 "value %d (0x%x)", getpid (), env_var_str,
306 ldp->sid_bit_val, ldp->sid_bit_val);
308 else if (sb < LDP_SID_BIT_MIN)
310 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
311 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
313 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
314 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
315 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
316 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
317 ldp->sid_bit_val, ldp->sid_bit_val);
319 else if (sb > LDP_SID_BIT_MAX)
321 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
322 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
324 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
325 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
326 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
327 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
328 ldp->sid_bit_val, ldp->sid_bit_val);
332 ldp->sid_bit_val = (1 << sb);
333 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
335 LDBG (0, "configured LDP sid bit (%u) from "
336 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
337 ldp->sid_bit_val, ldp->sid_bit_val);
341 clib_time_init (&ldpw->clib_time);
342 clib_rwlock_init (&ldp->fd_table_lock);
343 LDBG (0, "LDP initialization: done!");
352 u32 sid = ldp_sid_from_fd (fd);
354 if ((errno = -ldp_init ()))
357 if (sid != INVALID_SESSION_ID)
361 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
364 LDBG (0, "fd %d (0x%x): calling libc_close: epfd %u (0x%x)",
367 rv = libc_close (epfd);
370 u32 size = sizeof (epfd);
373 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
377 else if (PREDICT_FALSE (epfd < 0))
384 LDBG (0, "fd %d (0x%x): calling vppcom_session_close: sid %u (0x%x)",
387 refcnt = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REFCNT, 0, 0);
388 rv = vppcom_session_close (sid);
395 ldp_fd_free_w_sid (sid);
399 LDBG (0, "fd %d (0x%x): calling libc_close", fd, fd);
400 rv = libc_close (fd);
405 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
410 read (int fd, void *buf, size_t nbytes)
413 u32 sid = ldp_sid_from_fd (fd);
415 if ((errno = -ldp_init ()))
418 if (sid != INVALID_SESSION_ID)
420 LDBG (2, "fd %d (0x%x): calling vppcom_session_read(): sid %u (0x%x),"
421 " buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
423 size = vppcom_session_read (sid, buf, nbytes);
432 LDBG (2, "fd %d (0x%x): calling libc_read(): buf %p, nbytes %u",
433 fd, fd, buf, nbytes);
435 size = libc_read (fd, buf, nbytes);
438 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
443 readv (int fd, const struct iovec * iov, int iovcnt)
446 u32 sid = ldp_sid_from_fd (fd);
447 int rv = 0, i, total = 0;
449 if ((errno = -ldp_init ()))
452 if (sid != INVALID_SESSION_ID)
456 for (i = 0; i < iovcnt; ++i)
458 LDBG (2, "fd %d (0x%x): calling vppcom_session_read() [%d]:"
459 " sid %u (0x%x), iov %p, iovcnt %d, total %d", fd, fd, i,
460 sid, sid, iov, iovcnt, total);
462 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
468 if (rv < iov[i].iov_len)
470 LDBG (2, "fd %d (0x%x): rv (%d) < iov[%d].iov_len (%d)",
471 fd, fd, rv, i, iov[i].iov_len);
477 while ((rv >= 0) && (total == 0));
489 LDBG (2, "fd %d (0x%x): calling libc_readv(): iov %p, iovcnt %d", fd,
492 size = libc_readv (fd, iov, iovcnt);
496 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
501 write (int fd, const void *buf, size_t nbytes)
504 u32 sid = ldp_sid_from_fd (fd);
506 if ((errno = -ldp_init ()))
509 if (sid != INVALID_SESSION_ID)
511 LDBG (2, "fd %d (0x%x): calling vppcom_session_write(): sid %u (0x%x), "
512 "buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
514 size = vppcom_session_write (sid, (void *) buf, nbytes);
523 LDBG (2, "fd %d (0x%x): calling libc_write(): buf %p, nbytes %u",
524 fd, fd, buf, nbytes);
526 size = libc_write (fd, buf, nbytes);
529 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
534 writev (int fd, const struct iovec * iov, int iovcnt)
536 const char *func_str;
537 ssize_t size = 0, total = 0;
538 u32 sid = ldp_sid_from_fd (fd);
542 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
545 if ((errno = -ldp_init ()))
548 if (sid != INVALID_SESSION_ID)
550 func_str = "vppcom_session_write";
553 for (i = 0; i < iovcnt; ++i)
556 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
557 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
558 __func__, __LINE__, getpid (), fd, fd, func_str,
559 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
561 rv = vppcom_session_write (sid, iov[i].iov_base,
568 if (rv < iov[i].iov_len)
571 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
572 "rv (%d) < iov[%d].iov_len (%ld)",
573 __func__, __LINE__, getpid (), fd, fd,
574 rv, i, iov[i].iov_len);
580 while ((rv >= 0) && (total == 0));
592 func_str = "libc_writev";
595 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
596 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
597 fd, fd, func_str, iov, iovcnt);
599 size = libc_writev (fd, iov, iovcnt);
606 int errno_val = errno;
609 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
610 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
611 fd, func_str, size, errno_val);
615 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
616 __func__, __LINE__, getpid (), fd, fd, size);
622 fcntl (int fd, int cmd, ...)
624 const char *func_str = __func__;
627 u32 sid = ldp_sid_from_fd (fd);
629 if ((errno = -ldp_init ()))
633 if (sid != INVALID_SESSION_ID)
635 int flags = va_arg (ap, int);
638 size = sizeof (flags);
643 func_str = "vppcom_session_attr[SET_FLAGS]";
644 LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x) "
645 "flags %d (0x%x), size %d", fd, fd, func_str, sid,
646 sid, flags, flags, size);
648 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
653 func_str = "vppcom_session_attr[GET_FLAGS]";
654 LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x), "
655 "flags %d (0x%x), size %d", fd, fd, func_str, sid,
656 sid, flags, flags, size);
658 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
662 LDBG (2, "fd %d (0x%x), cmd %d (F_GETFL): %s() "
663 "returned flags %d (0x%x)", fd, fd, cmd,
664 func_str, flags, flags);
669 /* TODO handle this */
670 LDBG (0, "F_SETFD ignored flags %u", flags);
685 func_str = "libc_vfcntl";
687 LDBG (2, "fd %d (0x%x): calling %s(): cmd %d", fd, fd, func_str, cmd);
689 rv = libc_vfcntl (fd, cmd, ap);
698 int errno_val = errno;
700 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
701 "rv %d, errno = %d", getpid (), fd, fd,
702 func_str, rv, errno_val);
706 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
707 getpid (), fd, fd, rv, rv);
713 ioctl (int fd, unsigned long int cmd, ...)
715 const char *func_str;
718 u32 sid = ldp_sid_from_fd (fd);
720 if ((errno = -ldp_init ()))
724 if (sid != INVALID_SESSION_ID)
726 func_str = "vppcom_session_attr[GET_NREAD]";
733 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
734 getpid (), fd, fd, func_str, sid, sid);
736 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
741 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
742 u32 size = sizeof (flags);
744 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
745 * non-blocking, the flags should be read here and merged
749 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
750 "sid %u (0x%x), flags %d (0x%x), size %d",
751 getpid (), fd, fd, func_str, sid, sid,
754 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
771 func_str = "libc_vioctl";
774 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
775 getpid (), fd, fd, func_str, cmd);
777 rv = libc_vioctl (fd, cmd, ap);
784 int errno_val = errno;
786 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
787 "rv %d, errno = %d", getpid (), fd, fd,
788 func_str, rv, errno_val);
792 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
793 getpid (), fd, fd, rv, rv);
800 ldp_pselect (int nfds, fd_set * __restrict readfds,
801 fd_set * __restrict writefds,
802 fd_set * __restrict exceptfds,
803 const struct timespec *__restrict timeout,
804 const __sigset_t * __restrict sigmask)
806 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
807 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
808 u32 minbits = clib_max (nfds, BITS (uword)), sid;
809 char *func_str = "##";
821 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
822 (f64) 0 : (f64) timeout->tv_sec +
823 (f64) timeout->tv_nsec / (f64) 1000000000;
825 /* select as fine grained sleep */
828 LDBG (3, "sleeping for %.02f seconds", time_out);
830 time_out += clib_time_now (&ldpw->clib_time);
831 while (clib_time_now (&ldpw->clib_time) < time_out)
845 if (nfds <= ldp->sid_bit_val)
847 func_str = "libc_pselect";
849 LDBG (3, "calling %s(): nfds %d, readfds %p, writefds %p, "
850 "exceptfds %p, timeout %p, sigmask %p", func_str, nfds,
851 readfds, writefds, exceptfds, timeout, sigmask);
853 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
858 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
860 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
861 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
862 ldp->sid_bit_val, ldp->sid_bit_val,
863 FD_SETSIZE / 2, FD_SETSIZE / 2);
868 sid_bits = libc_bits = 0;
869 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
872 clib_bitmap_validate (ldpw->sid_rd_bitmap, minbits);
873 clib_bitmap_validate (ldpw->libc_rd_bitmap, minbits);
874 clib_bitmap_validate (ldpw->rd_bitmap, minbits);
875 clib_memcpy_fast (ldpw->rd_bitmap, readfds, n_bytes);
876 memset (readfds, 0, n_bytes);
879 clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({
882 sid = ldp_sid_from_fd (fd);
883 LDBG (3, "readfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
884 if (sid == INVALID_SESSION_ID)
885 clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1);
887 clib_bitmap_set_no_check (ldpw->sid_rd_bitmap,
888 vppcom_session_index (sid), 1);
892 sid_bits_set = clib_bitmap_last_set (ldpw->sid_rd_bitmap) + 1;
893 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
895 libc_bits_set = clib_bitmap_last_set (ldpw->libc_rd_bitmap) + 1;
896 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
898 LDBG (3, "readfds: sid_bits_set %d, sid_bits %d, "
899 "libc_bits_set %d, libc_bits %d", sid_bits_set,
900 sid_bits, libc_bits_set, libc_bits);
904 clib_bitmap_validate (ldpw->sid_wr_bitmap, minbits);
905 clib_bitmap_validate (ldpw->libc_wr_bitmap, minbits);
906 clib_bitmap_validate (ldpw->wr_bitmap, minbits);
907 clib_memcpy_fast (ldpw->wr_bitmap, writefds, n_bytes);
908 memset (writefds, 0, n_bytes);
911 clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({
914 sid = ldp_sid_from_fd (fd);
915 LDBG (3, "writefds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
916 if (sid == INVALID_SESSION_ID)
917 clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1);
919 clib_bitmap_set_no_check (ldpw->sid_wr_bitmap,
920 vppcom_session_index (sid), 1);
924 sid_bits_set = clib_bitmap_last_set (ldpw->sid_wr_bitmap) + 1;
925 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
927 libc_bits_set = clib_bitmap_last_set (ldpw->libc_wr_bitmap) + 1;
928 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
930 LDBG (3, "writefds: sid_bits_set %d, sid_bits %d, "
931 "libc_bits_set %d, libc_bits %d",
932 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
936 clib_bitmap_validate (ldpw->sid_ex_bitmap, minbits);
937 clib_bitmap_validate (ldpw->libc_ex_bitmap, minbits);
938 clib_bitmap_validate (ldpw->ex_bitmap, minbits);
939 clib_memcpy_fast (ldpw->ex_bitmap, exceptfds, n_bytes);
940 memset (exceptfds, 0, n_bytes);
943 clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({
946 sid = ldp_sid_from_fd (fd);
947 LDBG (3, "exceptfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
948 if (sid == INVALID_SESSION_ID)
949 clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1);
951 clib_bitmap_set_no_check (ldpw->sid_ex_bitmap,
952 vppcom_session_index (sid), 1);
956 sid_bits_set = clib_bitmap_last_set (ldpw->sid_ex_bitmap) + 1;
957 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
959 libc_bits_set = clib_bitmap_last_set (ldpw->libc_ex_bitmap) + 1;
960 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
962 LDBG (3, "exceptfds: sid_bits_set %d, sid_bits %d, "
963 "libc_bits_set %d, libc_bits %d",
964 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
967 if (PREDICT_FALSE (!sid_bits && !libc_bits))
978 if (!ldpw->select_vcl)
980 func_str = "vppcom_select";
983 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap,
984 vec_len (ldpw->rd_bitmap) *
985 sizeof (clib_bitmap_t));
987 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap,
988 vec_len (ldpw->wr_bitmap) *
989 sizeof (clib_bitmap_t));
991 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->sid_ex_bitmap,
992 vec_len (ldpw->ex_bitmap) *
993 sizeof (clib_bitmap_t));
995 rv = vppcom_select (sid_bits,
996 readfds ? ldpw->rd_bitmap : NULL,
997 writefds ? ldpw->wr_bitmap : NULL,
998 exceptfds ? ldpw->ex_bitmap : NULL, 0);
1009 clib_bitmap_foreach (sid, ldpw->rd_bitmap,
1011 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1012 if (PREDICT_FALSE (fd < 0))
1018 FD_SET (fd, readfds);
1025 clib_bitmap_foreach (sid, ldpw->wr_bitmap,
1027 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1028 if (PREDICT_FALSE (fd < 0))
1034 FD_SET (fd, writefds);
1041 clib_bitmap_foreach (sid, ldpw->ex_bitmap,
1043 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1044 if (PREDICT_FALSE (fd < 0))
1050 FD_SET (fd, exceptfds);
1054 ldpw->select_vcl = 1;
1059 ldpw->select_vcl = 0;
1063 struct timespec tspec;
1065 func_str = "libc_pselect";
1068 clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap,
1069 vec_len (ldpw->rd_bitmap) *
1070 sizeof (clib_bitmap_t));
1072 clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap,
1073 vec_len (ldpw->wr_bitmap) *
1074 sizeof (clib_bitmap_t));
1076 clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap,
1077 vec_len (ldpw->ex_bitmap) *
1078 sizeof (clib_bitmap_t));
1079 tspec.tv_sec = tspec.tv_nsec = 0;
1080 rv = libc_pselect (libc_bits,
1081 readfds ? readfds : NULL,
1082 writefds ? writefds : NULL,
1083 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1088 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
1092 /* TBD: set timeout to amount of time left */
1093 clib_bitmap_zero (ldpw->rd_bitmap);
1094 clib_bitmap_zero (ldpw->sid_rd_bitmap);
1095 clib_bitmap_zero (ldpw->libc_rd_bitmap);
1096 clib_bitmap_zero (ldpw->wr_bitmap);
1097 clib_bitmap_zero (ldpw->sid_wr_bitmap);
1098 clib_bitmap_zero (ldpw->libc_wr_bitmap);
1099 clib_bitmap_zero (ldpw->ex_bitmap);
1100 clib_bitmap_zero (ldpw->sid_ex_bitmap);
1101 clib_bitmap_zero (ldpw->libc_ex_bitmap);
1107 int errno_val = errno;
1109 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1110 "rv %d, errno = %d", getpid (),
1111 func_str, rv, errno_val);
1115 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1121 select (int nfds, fd_set * __restrict readfds,
1122 fd_set * __restrict writefds,
1123 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1125 struct timespec tspec;
1129 tspec.tv_sec = timeout->tv_sec;
1130 tspec.tv_nsec = timeout->tv_usec * 1000;
1132 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1133 timeout ? &tspec : NULL, NULL);
1136 #ifdef __USE_XOPEN2K
1138 pselect (int nfds, fd_set * __restrict readfds,
1139 fd_set * __restrict writefds,
1140 fd_set * __restrict exceptfds,
1141 const struct timespec *__restrict timeout,
1142 const __sigset_t * __restrict sigmask)
1144 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1149 socket (int domain, int type, int protocol)
1151 const char *func_str;
1153 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1154 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1156 if ((errno = -ldp_init ()))
1159 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1160 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1163 u8 proto = ((sock_type == SOCK_DGRAM) ?
1164 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1166 func_str = "vppcom_session_create";
1168 LDBG (0, "calling %s(): proto %u (%s), is_nonblocking %u",
1169 func_str, proto, vppcom_proto_str (proto), is_nonblocking);
1171 sid = vppcom_session_create (proto, is_nonblocking);
1179 func_str = "ldp_fd_from_sid";
1180 rv = ldp_fd_alloc (sid);
1183 (void) vppcom_session_close (sid);
1191 func_str = "libc_socket";
1193 LDBG (0, "calling %s()", func_str);
1195 rv = libc_socket (domain, type, protocol);
1202 int errno_val = errno;
1204 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1205 "rv %d, errno = %d",
1206 getpid (), func_str, rv, errno_val);
1210 clib_warning ("returning fd %d (0x%x)", getpid (), rv, rv);
1216 * Create two new sockets, of type TYPE in domain DOMAIN and using
1217 * protocol PROTOCOL, which are connected to each other, and put file
1218 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1219 * one will be chosen automatically.
1220 * Returns 0 on success, -1 for errors.
1223 socketpair (int domain, int type, int protocol, int fds[2])
1225 const char *func_str;
1227 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1229 if ((errno = -ldp_init ()))
1232 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1233 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1235 func_str = __func__;
1237 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1243 func_str = "libc_socket";
1245 LDBG (1, "calling %s()", func_str);
1247 rv = libc_socketpair (domain, type, protocol, fds);
1254 int errno_val = errno;
1256 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1257 "rv %d, errno = %d",
1258 getpid (), func_str, rv, errno_val);
1262 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1268 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1271 u32 sid = ldp_sid_from_fd (fd);
1273 if ((errno = -ldp_init ()))
1276 if (sid != INVALID_SESSION_ID)
1280 switch (addr->sa_family)
1283 if (len != sizeof (struct sockaddr_in))
1286 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1287 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1292 ep.is_ip4 = VPPCOM_IS_IP4;
1293 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1294 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1298 if (len != sizeof (struct sockaddr_in6))
1301 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1302 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1307 ep.is_ip4 = VPPCOM_IS_IP6;
1308 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1309 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1313 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1314 "Unsupported address family %u!",
1315 getpid (), fd, fd, sid, sid, addr->sa_family);
1316 errno = EAFNOSUPPORT;
1320 LDBG (0, "fd %d (0x%x): calling vppcom_session_bind(): "
1321 "sid %u (0x%x), addr %p, len %u", fd, fd, sid, sid, addr, len);
1323 rv = vppcom_session_bind (sid, &ep);
1324 if (rv != VPPCOM_OK)
1332 LDBG (0, "fd %d (0x%x): calling libc_bind(): addr %p, len %u",
1335 rv = libc_bind (fd, addr, len);
1339 LDBG (1, "fd %d (0x%x): returning %d", fd, fd, rv);
1345 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1346 vppcom_endpt_t * ep)
1349 int sa_len, copy_len;
1351 if ((errno = -ldp_init ()))
1354 if (addr && len && ep)
1356 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1357 switch (addr->sa_family)
1360 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1361 if (*len > sizeof (struct sockaddr_in))
1362 *len = sizeof (struct sockaddr_in);
1363 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1364 copy_len = *len - sa_len;
1366 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1371 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1372 if (*len > sizeof (struct sockaddr_in6))
1373 *len = sizeof (struct sockaddr_in6);
1374 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1375 copy_len = *len - sa_len;
1377 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1378 __in6_u.__u6_addr8, ep->ip, copy_len);
1391 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1394 const char *func_str;
1395 u32 sid = ldp_sid_from_fd (fd);
1397 if ((errno = -ldp_init ()))
1400 if (sid != INVALID_SESSION_ID)
1403 u8 addr_buf[sizeof (struct in6_addr)];
1404 u32 size = sizeof (ep);
1407 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1410 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1412 getpid (), fd, fd, func_str, sid, sid, addr, len);
1414 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1415 if (rv != VPPCOM_OK)
1422 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1423 if (rv != VPPCOM_OK)
1432 func_str = "libc_getsockname";
1435 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1437 getpid (), fd, fd, func_str, addr, len);
1439 rv = libc_getsockname (fd, addr, len);
1446 int errno_val = errno;
1448 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1449 "rv %d, errno = %d", getpid (), fd, fd,
1450 func_str, rv, errno_val);
1454 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1455 getpid (), fd, fd, rv, rv);
1461 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1464 u32 sid = ldp_sid_from_fd (fd);
1466 if ((errno = -ldp_init ()))
1471 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1472 getpid (), fd, fd, len);
1478 if (sid != INVALID_SESSION_ID)
1482 switch (addr->sa_family)
1485 if (len != sizeof (struct sockaddr_in))
1488 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1489 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1494 ep.is_ip4 = VPPCOM_IS_IP4;
1495 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1496 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1500 if (len != sizeof (struct sockaddr_in6))
1503 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1504 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1509 ep.is_ip4 = VPPCOM_IS_IP6;
1510 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1511 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1515 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1516 "Unsupported address family %u!",
1517 getpid (), fd, fd, sid, sid, addr->sa_family);
1518 errno = EAFNOSUPPORT;
1522 LDBG (0, "fd %d (0x%x): calling vppcom_session_connect(): sid %u (0x%x)"
1523 " addr %p len %u", fd, fd, sid, sid, addr, len);
1525 rv = vppcom_session_connect (sid, &ep);
1526 if (rv != VPPCOM_OK)
1534 LDBG (0, "fd %d (0x%x): calling libc_connect(): addr %p, len %u",
1537 rv = libc_connect (fd, addr, len);
1541 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
1546 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1549 const char *func_str;
1550 u32 sid = ldp_sid_from_fd (fd);
1552 if ((errno = -ldp_init ()))
1555 if (sid != INVALID_SESSION_ID)
1558 u8 addr_buf[sizeof (struct in6_addr)];
1559 u32 size = sizeof (ep);
1562 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1565 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1567 getpid (), fd, fd, func_str, sid, sid, addr, len);
1569 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1570 if (rv != VPPCOM_OK)
1577 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1578 if (rv != VPPCOM_OK)
1587 func_str = "libc_getpeername";
1590 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1592 getpid (), fd, fd, func_str, addr, len);
1594 rv = libc_getpeername (fd, addr, len);
1601 int errno_val = errno;
1603 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1604 "rv %d, errno = %d", getpid (), fd, fd,
1605 func_str, rv, errno_val);
1609 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1610 getpid (), fd, fd, rv, rv);
1616 send (int fd, const void *buf, size_t n, int flags)
1619 const char *func_str;
1620 u32 sid = ldp_sid_from_fd (fd);
1622 if ((errno = -ldp_init ()))
1625 if (sid != INVALID_SESSION_ID)
1628 func_str = "vppcom_session_sendto";
1631 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1632 "buf %p, n %u, flags 0x%x",
1633 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1635 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1636 if (size < VPPCOM_OK)
1644 func_str = "libc_send";
1647 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1648 "buf %p, n %u, flags 0x%x",
1649 getpid (), fd, fd, func_str, buf, n, flags);
1651 size = libc_send (fd, buf, n, flags);
1658 int errno_val = errno;
1660 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1661 "rv %d, errno = %d", getpid (), fd, fd,
1662 func_str, size, errno_val);
1666 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1667 getpid (), fd, fd, size, size);
1673 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1675 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1677 const char *func_str;
1678 u32 sid = ldp_sid_from_fd (out_fd);
1680 if ((errno = -ldp_init ()))
1683 if (sid != INVALID_SESSION_ID)
1686 ssize_t results = 0;
1687 size_t n_bytes_left = len;
1688 size_t bytes_to_read;
1692 u32 flags, flags_len = sizeof (flags);
1694 func_str = "vppcom_session_attr[GET_FLAGS]";
1695 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1697 if (PREDICT_FALSE (rv != VPPCOM_OK))
1699 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1700 "sid %u (0x%x), returned %d (%s)!", getpid (),
1701 out_fd, out_fd, func_str, sid, sid, rv,
1702 vppcom_retval_str (rv));
1704 vec_reset_length (ldpw->io_buffer);
1712 off_t off = lseek (in_fd, *offset, SEEK_SET);
1713 if (PREDICT_FALSE (off == -1))
1717 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1718 "SEEK_SET failed: in_fd %d, offset %p, "
1719 "*offset %ld, rv %ld, errno %d", getpid (),
1720 out_fd, out_fd, in_fd, offset, *offset, off,
1727 ASSERT (off == *offset);
1732 func_str = "vppcom_session_attr[GET_NWRITE]";
1733 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1737 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1738 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1739 sid, sid, size, vppcom_retval_str (size));
1740 vec_reset_length (ldpw->io_buffer);
1746 bytes_to_read = size;
1749 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1750 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1751 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1754 if (bytes_to_read == 0)
1756 if (flags & O_NONBLOCK)
1761 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1763 getpid (), out_fd, out_fd, sid, sid);
1771 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1772 vec_validate (ldpw->io_buffer, bytes_to_read);
1773 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1776 func_str = "libc_read";
1778 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1779 "io_buffer %p, bytes_to_read %lu, rv %d, "
1780 "errno %d", getpid (), out_fd, out_fd, func_str,
1781 in_fd, ldpw->io_buffer, bytes_to_read, nbytes,
1787 vec_reset_length (ldpw->io_buffer);
1793 func_str = "vppcom_session_write";
1796 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1797 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1798 out_fd, out_fd, func_str, sid, sid, ldpw->io_buffer, nbytes,
1799 results, n_bytes_left);
1801 size = vppcom_session_write (sid, ldpw->io_buffer, nbytes);
1804 if (size == VPPCOM_EAGAIN)
1806 if (flags & O_NONBLOCK)
1812 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1813 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1823 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1824 "sid %u, io_buffer %p, nbytes %u "
1826 getpid (), out_fd, out_fd, func_str,
1827 sid, ldpw->io_buffer, nbytes,
1828 size, vppcom_retval_str (size));
1832 vec_reset_length (ldpw->io_buffer);
1841 ASSERT (n_bytes_left >= nbytes);
1842 n_bytes_left = n_bytes_left - nbytes;
1844 while (n_bytes_left > 0);
1847 vec_reset_length (ldpw->io_buffer);
1850 off_t off = lseek (in_fd, *offset, SEEK_SET);
1851 if (PREDICT_FALSE (off == -1))
1855 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1856 "in_fd %d, offset %p, *offset %ld, "
1857 "rv %ld, errno %d", getpid (), in_fd,
1858 offset, *offset, off, errno_val);
1864 ASSERT (off == *offset);
1865 *offset += results + 1;
1877 func_str = "libc_send";
1880 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1881 "in_fd %d, offset %p, len %u",
1882 getpid (), out_fd, out_fd, func_str,
1883 in_fd, offset, len);
1885 size = libc_sendfile (out_fd, in_fd, offset, len);
1893 int errno_val = errno;
1895 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1896 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1897 func_str, size, errno_val);
1901 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1902 getpid (), out_fd, out_fd, size, size);
1908 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1910 return sendfile (out_fd, in_fd, offset, len);
1914 recv (int fd, void *buf, size_t n, int flags)
1917 const char *func_str;
1918 u32 sid = ldp_sid_from_fd (fd);
1920 if ((errno = -ldp_init ()))
1923 if (sid != INVALID_SESSION_ID)
1925 func_str = "vppcom_session_recvfrom";
1928 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1929 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1930 fd, fd, func_str, sid, sid, buf, n, flags);
1932 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1941 func_str = "libc_recv";
1944 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1945 "buf %p, n %u, flags 0x%x", getpid (),
1946 fd, fd, func_str, buf, n, flags);
1948 size = libc_recv (fd, buf, n, flags);
1955 int errno_val = errno;
1957 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1958 "rv %d, errno = %d", getpid (), fd, fd,
1959 func_str, size, errno_val);
1963 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1964 getpid (), fd, fd, size, size);
1970 sendto (int fd, const void *buf, size_t n, int flags,
1971 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1974 const char *func_str = __func__;
1975 u32 sid = ldp_sid_from_fd (fd);
1977 if ((errno = -ldp_init ()))
1980 if (sid != INVALID_SESSION_ID)
1982 vppcom_endpt_t *ep = 0;
1988 switch (addr->sa_family)
1991 ep->is_ip4 = VPPCOM_IS_IP4;
1993 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1995 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1999 ep->is_ip4 = VPPCOM_IS_IP6;
2001 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2003 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2007 errno = EAFNOSUPPORT;
2013 func_str = "vppcom_session_sendto";
2016 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2017 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2018 getpid (), fd, fd, func_str, sid, sid, buf, n,
2021 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2030 func_str = "libc_sendto";
2033 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2034 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2035 getpid (), fd, fd, func_str, buf, n, flags,
2038 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2046 int errno_val = errno;
2048 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2049 "rv %d, errno = %d", getpid (), fd, fd,
2050 func_str, size, errno_val);
2054 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2055 getpid (), fd, fd, size, size);
2061 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2062 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2065 const char *func_str;
2066 u32 sid = ldp_sid_from_fd (fd);
2068 if ((errno = -ldp_init ()))
2071 if (sid != INVALID_SESSION_ID)
2074 u8 src_addr[sizeof (struct sockaddr_in6)];
2076 func_str = "vppcom_session_recvfrom";
2079 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2080 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2081 getpid (), fd, fd, func_str, sid, sid, buf, n,
2086 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2089 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2092 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2102 func_str = "libc_recvfrom";
2105 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2106 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2107 getpid (), fd, fd, func_str, buf, n, flags,
2110 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2117 int errno_val = errno;
2119 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2120 "rv %d, errno = %d", getpid (), fd, fd,
2121 func_str, size, errno_val);
2125 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2126 getpid (), fd, fd, size, size);
2132 sendmsg (int fd, const struct msghdr * message, int flags)
2135 const char *func_str;
2136 u32 sid = ldp_sid_from_fd (fd);
2138 if ((errno = -ldp_init ()))
2141 if (sid != INVALID_SESSION_ID)
2143 func_str = __func__;
2145 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2151 func_str = "libc_sendmsg";
2154 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2155 "message %p, flags 0x%x",
2156 getpid (), fd, fd, func_str, message, flags);
2158 size = libc_sendmsg (fd, message, flags);
2165 int errno_val = errno;
2167 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2168 "rv %d, errno = %d", getpid (), fd, fd,
2169 func_str, size, errno_val);
2173 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2174 getpid (), fd, fd, size, size);
2181 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2184 const char *func_str;
2185 u32 sid = ldp_sid_from_fd (fd);
2187 if ((errno = -ldp_init ()))
2190 if (sid != INVALID_SESSION_ID)
2192 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2198 func_str = "libc_sendmmsg";
2201 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2202 "vmessages %p, vlen %u, flags 0x%x",
2203 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2205 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2212 int errno_val = errno;
2214 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2215 "rv %d, errno = %d", getpid (), fd, fd,
2216 func_str, size, errno_val);
2220 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2221 getpid (), fd, fd, size, size);
2228 recvmsg (int fd, struct msghdr * message, int flags)
2231 const char *func_str;
2232 u32 sid = ldp_sid_from_fd (fd);
2234 if ((errno = -ldp_init ()))
2237 if (sid != INVALID_SESSION_ID)
2239 func_str = __func__;
2241 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2247 func_str = "libc_recvmsg";
2250 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2251 "message %p, flags 0x%x",
2252 getpid (), fd, fd, func_str, message, flags);
2254 size = libc_recvmsg (fd, message, flags);
2261 int errno_val = errno;
2263 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2264 "rv %d, errno = %d", getpid (), fd, fd,
2265 func_str, size, errno_val);
2269 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2270 getpid (), fd, fd, size, size);
2277 recvmmsg (int fd, struct mmsghdr *vmessages,
2278 unsigned int vlen, int flags, struct timespec *tmo)
2281 const char *func_str;
2282 u32 sid = ldp_sid_from_fd (fd);
2284 if ((errno = -ldp_init ()))
2287 if (sid != INVALID_SESSION_ID)
2289 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2295 func_str = "libc_recvmmsg";
2298 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2299 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2300 getpid (), fd, fd, func_str, vmessages, vlen,
2303 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2310 int errno_val = errno;
2312 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2313 "rv %d, errno = %d", getpid (), fd, fd,
2314 func_str, size, errno_val);
2318 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2319 getpid (), fd, fd, size, size);
2326 getsockopt (int fd, int level, int optname,
2327 void *__restrict optval, socklen_t * __restrict optlen)
2330 const char *func_str = __func__;
2331 u32 sid = ldp_sid_from_fd (fd);
2332 u32 buflen = optlen ? (u32) * optlen : 0;
2334 if ((errno = -ldp_init ()))
2337 if (sid != INVALID_SESSION_ID)
2347 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2349 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2351 getpid (), fd, fd, func_str, sid, sid);
2352 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2356 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2358 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2360 getpid (), fd, fd, func_str, sid, sid);
2361 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2365 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2367 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2369 getpid (), fd, fd, func_str, sid, sid);
2370 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2374 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2376 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2377 "sid %u (0x%x), SOL_TCP",
2378 getpid (), fd, fd, func_str, sid, sid);
2379 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2383 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2386 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2387 "SOL_TCP, TCP_INFO, optval %p, "
2388 "optlen %d: #LDP-NOP#",
2389 getpid (), fd, fd, sid, sid,
2391 memset (optval, 0, *optlen);
2399 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2400 "sid %u (0x%x), SOL_TCP, "
2401 "optname %d unsupported!",
2402 getpid (), fd, fd, func_str, sid, sid, optname);
2410 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2412 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2414 getpid (), fd, fd, func_str, sid, sid);
2415 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2420 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2421 "sid %u (0x%x), SOL_IPV6, "
2422 "optname %d unsupported!",
2423 getpid (), fd, fd, func_str, sid, sid, optname);
2431 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2433 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2435 getpid (), fd, fd, func_str, sid, sid);
2436 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2440 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2442 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2444 getpid (), fd, fd, func_str, sid, sid);
2445 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2449 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2451 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2453 getpid (), fd, fd, func_str, sid, sid);
2454 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2456 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2459 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2461 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2462 "sid %u (0x%x), optlen %d",
2463 getpid (), fd, fd, func_str, sid, sid, buflen);
2464 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2468 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2470 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2471 "sid %u (0x%x), optlen %d",
2472 getpid (), fd, fd, func_str, sid, sid, buflen);
2473 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2477 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2479 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2481 getpid (), fd, fd, func_str, sid, sid);
2482 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2486 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2488 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2490 getpid (), fd, fd, func_str, sid, sid);
2491 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2495 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2497 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2499 getpid (), fd, fd, func_str, sid, sid);
2500 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2505 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2506 "sid %u (0x%x), SOL_SOCKET, "
2507 "optname %d unsupported!",
2508 getpid (), fd, fd, func_str, sid, sid, optname);
2516 if (rv != VPPCOM_OK)
2524 func_str = "libc_getsockopt";
2527 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2528 "optname %d, optval %p, optlen %d",
2529 getpid (), fd, fd, func_str, level, optname,
2532 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2539 int errno_val = errno;
2541 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2542 "rv %d, errno = %d", getpid (), fd, fd,
2543 func_str, rv, errno_val);
2547 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2548 getpid (), fd, fd, rv, rv);
2554 setsockopt (int fd, int level, int optname,
2555 const void *optval, socklen_t optlen)
2558 const char *func_str = __func__;
2559 u32 sid = ldp_sid_from_fd (fd);
2561 if ((errno = -ldp_init ()))
2564 if (sid != INVALID_SESSION_ID)
2574 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2576 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2578 getpid (), fd, fd, func_str, sid, sid);
2579 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2580 (void *) optval, &optlen);
2583 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2585 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2587 getpid (), fd, fd, func_str, sid, sid);
2588 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2589 (void *) optval, &optlen);
2592 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2594 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2596 getpid (), fd, fd, func_str, sid, sid);
2597 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2598 (void *) optval, &optlen);
2601 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2603 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2604 "sid %u (0x%x), SOL_TCP",
2605 getpid (), fd, fd, func_str, sid, sid);
2606 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2607 (void *) optval, &optlen);
2611 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2612 "sid %u (0x%x), SOL_TCP, "
2613 "optname %d unsupported!",
2614 getpid (), fd, fd, func_str, sid, sid, optname);
2622 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2624 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2626 getpid (), fd, fd, func_str, sid, sid);
2627 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2628 (void *) optval, &optlen);
2632 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2633 "sid %u (0x%x), SOL_IPV6, "
2634 "optname %d unsupported!",
2635 getpid (), fd, fd, func_str, sid, sid, optname);
2643 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2645 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2647 getpid (), fd, fd, func_str, sid, sid);
2648 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2649 (void *) optval, &optlen);
2652 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2654 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2656 getpid (), fd, fd, func_str, sid, sid);
2657 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2658 (void *) optval, &optlen);
2661 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2663 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2665 getpid (), fd, fd, func_str, sid, sid);
2666 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2667 (void *) optval, &optlen);
2671 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2672 "sid %u (0x%x), SOL_SOCKET, "
2673 "optname %d unsupported!",
2674 getpid (), fd, fd, func_str, sid, sid, optname);
2682 if (rv != VPPCOM_OK)
2690 func_str = "libc_setsockopt";
2693 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2694 "optname %d, optval %p, optlen %d",
2695 getpid (), fd, fd, func_str, level, optname,
2698 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2705 int errno_val = errno;
2707 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2708 "rv %d, errno = %d", getpid (), fd, fd,
2709 func_str, rv, errno_val);
2713 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2714 getpid (), fd, fd, rv, rv);
2720 listen (int fd, int n)
2723 u32 sid = ldp_sid_from_fd (fd);
2725 if ((errno = -ldp_init ()))
2728 if (sid != INVALID_SESSION_ID)
2730 LDBG (0, "fd %d (0x%x): calling vppcom_session_listen():"
2731 " sid %u (0x%x), n %d", fd, fd, sid, sid, n);
2733 rv = vppcom_session_listen (sid, n);
2734 if (rv != VPPCOM_OK)
2742 LDBG (0, "fd %d (0x%x): calling libc_listen(): n %d", fd, fd, n);
2744 rv = libc_listen (fd, n);
2747 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
2752 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2753 socklen_t * __restrict addr_len, int flags)
2756 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2759 if ((errno = -ldp_init ()))
2762 if (listen_sid != INVALID_SESSION_ID)
2765 u8 src_addr[sizeof (struct sockaddr_in6)];
2766 memset (&ep, 0, sizeof (ep));
2769 LDBG (0, "listen fd %d (0x%x): calling vppcom_session_accept:"
2770 " listen sid %u (0x%x), ep %p, flags 0x%x", listen_fd,
2771 listen_fd, listen_sid, listen_sid, ep, flags);
2773 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2776 errno = -accept_sid;
2781 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2782 if (rv != VPPCOM_OK)
2784 (void) vppcom_session_close ((u32) accept_sid);
2790 rv = ldp_fd_alloc ((u32) accept_sid);
2793 (void) vppcom_session_close ((u32) accept_sid);
2802 LDBG (0, "listen fd %d (0x%x): calling libc_accept4(): "
2803 "addr %p, addr_len %p, flags 0x%x", listen_fd,
2804 listen_fd, addr, addr_len, flags);
2806 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2809 LDBG (1, "listen fd %d (0x%x): returning %d (0x%x)", listen_fd, listen_fd,
2816 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2819 return ldp_accept4 (fd, addr, addr_len, flags);
2823 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2825 return ldp_accept4 (fd, addr, addr_len, 0);
2829 shutdown (int fd, int how)
2832 const char *func_str;
2833 u32 sid = ldp_sid_from_fd (fd);
2835 if ((errno = -ldp_init ()))
2838 if (sid != INVALID_SESSION_ID)
2840 func_str = "vppcom_session_close[TODO]";
2845 func_str = "libc_shutdown";
2848 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2849 getpid (), fd, fd, func_str, how);
2851 rv = libc_shutdown (fd, how);
2858 int errno_val = errno;
2860 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2861 "rv %d, errno = %d", getpid (), fd, fd,
2862 func_str, rv, errno_val);
2866 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2867 getpid (), fd, fd, rv, rv);
2873 epoll_create1 (int flags)
2875 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2876 const char *func_str;
2879 if ((errno = -ldp_init ()))
2882 if (ldp->vcl_needs_real_epoll)
2884 rv = libc_epoll_create1 (flags);
2885 ldp->vcl_needs_real_epoll = 0;
2886 ldpw->vcl_mq_epfd = rv;
2887 LDBG (0, "created vcl epfd %u", rv);
2890 func_str = "vppcom_epoll_create";
2892 LDBG (1, "calling %s()", func_str);
2894 rv = vppcom_epoll_create ();
2896 if (PREDICT_FALSE (rv < 0))
2902 rv = ldp_fd_alloc ((u32) rv);
2908 int errno_val = errno;
2910 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2911 "rv %d, errno = %d",
2912 getpid (), func_str, rv, errno_val);
2916 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2922 epoll_create (int size)
2924 return epoll_create1 (0);
2928 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2930 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
2931 const char *func_str;
2934 if ((errno = -ldp_init ()))
2937 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
2939 /* The LDP epoll_create1 always creates VCL epfd's.
2940 * The app should never have a kernel base epoll fd unless it
2941 * was acquired outside of the LD_PRELOAD process context.
2942 * In any case, if we get one, punt it to libc_epoll_ctl.
2944 func_str = "libc_epoll_ctl";
2946 LDBG (1, "epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
2947 " event %p", epfd, epfd, func_str, op, fd, fd, event);
2949 rv = libc_epoll_ctl (epfd, op, fd, event);
2953 sid = ldp_sid_from_fd (fd);
2955 LDBG (0, "epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
2956 epfd, epfd, vep_idx, vep_idx, sid, sid);
2958 if (sid != INVALID_SESSION_ID)
2960 func_str = "vppcom_epoll_ctl";
2962 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
2963 " op %d, sid %u (0x%x), event %p", epfd, epfd,
2964 func_str, vep_idx, vep_idx, sid, sid, event);
2966 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
2967 if (rv != VPPCOM_OK)
2976 u32 size = sizeof (epfd);
2978 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
2979 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
2981 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): %s() "
2982 "returned libc_epfd %d (0x%x)", epfd, epfd,
2983 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
2987 func_str = "libc_epoll_create1";
2989 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): "
2990 "calling %s(): EPOLL_CLOEXEC", epfd, epfd,
2991 vep_idx, vep_idx, func_str);
2993 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3000 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3001 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3002 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3003 epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3006 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3015 else if (PREDICT_FALSE (libc_epfd < 0))
3022 func_str = "libc_epoll_ctl";
3024 LDBG (1, "epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3025 "op %d, fd %d (0x%x), event %p", epfd, epfd, func_str,
3026 libc_epfd, libc_epfd, op, fd, fd, event);
3028 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3036 int errno_val = errno;
3038 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3039 "rv %d, errno = %d", getpid (), fd, fd,
3040 func_str, rv, errno_val);
3044 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3045 getpid (), fd, fd, rv, rv);
3051 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3052 int timeout, const sigset_t * sigmask)
3054 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3055 double time_to_wait = (double) 0, time_out, now = 0;
3056 u32 vep_idx = ldp_sid_from_fd (epfd);
3057 int libc_epfd, rv = 0;
3058 const char *func_str;
3060 if ((errno = -ldp_init ()))
3063 if (PREDICT_FALSE (!events || (timeout < -1)))
3069 if (epfd == ldpw->vcl_mq_epfd)
3070 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3072 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3074 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3075 getpid (), epfd, epfd, vep_idx, vep_idx);
3080 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3081 time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
3083 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3084 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3085 if (PREDICT_FALSE (libc_epfd < 0))
3092 LDBG (2, "epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3093 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3094 epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3095 maxevents, timeout, sigmask, time_to_wait, time_out);
3098 if (!ldpw->epoll_wait_vcl)
3100 func_str = "vppcom_epoll_wait";
3102 LDBG (3, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3103 " events %p, maxevents %d", epfd, epfd, func_str,
3104 vep_idx, vep_idx, events, maxevents);
3106 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3109 ldpw->epoll_wait_vcl = 1;
3120 ldpw->epoll_wait_vcl = 0;
3124 func_str = "libc_epoll_pwait";
3126 LDBG (3, "epfd %d (0x%x): calling %s(): libc_epfd %d "
3127 "(0x%x), events %p, maxevents %d, sigmask %p",
3128 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3129 maxevents, sigmask);
3131 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3137 now = clib_time_now (&ldpw->clib_time);
3139 while (now < time_out);
3148 int errno_val = errno;
3150 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3151 "rv %d, errno = %d", getpid (), epfd, epfd,
3152 func_str, rv, errno_val);
3156 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3157 getpid (), epfd, epfd, rv, rv);
3163 epoll_pwait (int epfd, struct epoll_event *events,
3164 int maxevents, int timeout, const sigset_t * sigmask)
3166 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3170 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3172 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3176 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3178 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3179 const char *func_str = __func__;
3180 int rv, i, n_revents = 0;
3183 double wait_for_time;
3185 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
3188 wait_for_time = (f64) timeout / 1000;
3192 for (i = 0; i < nfds; i++)
3197 LDBG (3, "fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3198 i, fds[i].fd, fds[i].fd, fds[i].events, fds[i].revents);
3200 sid = ldp_sid_from_fd (fds[i].fd);
3201 if (sid != INVALID_SESSION_ID)
3203 fds[i].fd = -fds[i].fd;
3204 vec_add2 (ldpw->vcl_poll, vp, 1);
3207 vp->events = fds[i].events;
3208 #ifdef __USE_XOPEN2K
3209 if (fds[i].events & POLLRDNORM)
3210 vp->events |= POLLIN;
3211 if (fds[i].events & POLLWRNORM)
3212 vp->events |= POLLOUT;
3214 vp->revents = fds[i].revents;
3218 vec_add1 (ldpw->libc_poll, fds[i]);
3219 vec_add1 (ldpw->libc_poll_idxs, i);
3225 if (vec_len (ldpw->vcl_poll))
3227 func_str = "vppcom_poll";
3229 LDBG (3, "calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3230 "n_libc_fds %u", func_str, ldpw->vcl_poll,
3231 vec_len (ldpw->vcl_poll), vec_len (ldpw->vcl_poll),
3232 vec_len (ldpw->libc_poll));
3234 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
3245 if (vec_len (ldpw->libc_poll))
3247 func_str = "libc_poll";
3249 LDBG (3, "calling %s(): fds %p, nfds %u: n_sids %u",
3250 fds, nfds, vec_len (ldpw->vcl_poll));
3252 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
3265 while ((wait_for_time == -1) ||
3266 (clib_time_now (&ldpw->clib_time) < wait_for_time));
3270 vec_foreach (vp, ldpw->vcl_poll)
3272 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3273 fds[vp->fds_ndx].revents = vp->revents;
3274 #ifdef __USE_XOPEN2K
3275 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3276 (fds[vp->fds_ndx].events & POLLRDNORM))
3277 fds[vp->fds_ndx].revents |= POLLRDNORM;
3278 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3279 (fds[vp->fds_ndx].events & POLLWRNORM))
3280 fds[vp->fds_ndx].revents |= POLLWRNORM;
3283 vec_reset_length (ldpw->vcl_poll);
3285 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
3287 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
3289 vec_reset_length (ldpw->libc_poll_idxs);
3290 vec_reset_length (ldpw->libc_poll);
3296 int errno_val = errno;
3298 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3299 "rv %d, errno = %d", getpid (),
3300 func_str, rv, errno_val);
3305 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3306 "n_libc_fds %d", getpid (), rv, rv,
3307 vec_len (ldpw->vcl_poll), vec_len (ldpw->libc_poll));
3309 for (i = 0; i < nfds; i++)
3314 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3315 ".events = 0x%x, .revents = 0x%x",
3316 getpid (), i, fds[i].fd, fds[i].fd,
3317 fds[i].events, fds[i].revents);
3328 ppoll (struct pollfd *fds, nfds_t nfds,
3329 const struct timespec *timeout, const sigset_t * sigmask)
3331 if ((errno = -ldp_init ()))
3334 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3342 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3344 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3347 * This function is called when the library is loaded
3350 ldp_constructor (void)
3352 swrap_constructor ();
3353 if (ldp_init () != 0)
3354 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3356 else if (LDP_DEBUG > 0)
3357 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3361 * This function is called when the library is unloaded
3364 ldp_destructor (void)
3366 swrap_destructor ();
3370 /* Don't use clib_warning() here because that calls writev()
3371 * which will call ldp_init().
3374 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3375 __func__, __LINE__, getpid ());
3380 * fd.io coding-style-patch-verification: ON
3383 * eval: (c-set-style "gnu")