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 #define LDP_F_SHUT_RD (1 << 0)
55 #define LDP_F_SHUT_WR (1 << 1)
57 typedef struct ldp_fd_entry_
65 typedef struct ldp_worker_ctx_
68 clib_time_t clib_time;
73 clib_bitmap_t *rd_bitmap;
74 clib_bitmap_t *wr_bitmap;
75 clib_bitmap_t *ex_bitmap;
76 clib_bitmap_t *sid_rd_bitmap;
77 clib_bitmap_t *sid_wr_bitmap;
78 clib_bitmap_t *sid_ex_bitmap;
79 clib_bitmap_t *libc_rd_bitmap;
80 clib_bitmap_t *libc_wr_bitmap;
81 clib_bitmap_t *libc_ex_bitmap;
88 struct pollfd *libc_poll;
101 ldp_worker_ctx_t *workers;
103 char app_name[LDP_APP_NAME_MAX];
107 ldp_fd_entry_t *fd_pool;
108 clib_rwlock_t fd_table_lock;
109 uword *session_index_to_fd_table;
111 /** vcl needs next epoll_create to go to libc_epoll */
112 u8 vcl_needs_real_epoll;
115 #define LDP_DEBUG ldp->debug
117 #define LDBG(_lvl, _fmt, _args...) \
118 if (ldp->debug > _lvl) \
119 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args)
121 static ldp_main_t ldp_main = {
122 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
123 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
124 .debug = LDP_DEBUG_INIT,
127 static ldp_main_t *ldp = &ldp_main;
129 static inline ldp_worker_ctx_t *
130 ldp_worker_get_current (void)
132 return (ldp->workers + vppcom_worker_index ());
136 * RETURN: 0 on success or -1 on error.
139 ldp_set_app_name (char *app_name)
141 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
142 "ldp-%d-%s", getpid (), app_name);
144 if (rv >= LDP_APP_NAME_MAX)
145 app_name[LDP_APP_NAME_MAX - 1] = 0;
151 if (ldp->app_name[0] == '\0')
152 ldp_set_app_name ("app");
154 return ldp->app_name;
158 ldp_fd_alloc (u32 sid)
162 clib_rwlock_writer_lock (&ldp->fd_table_lock);
163 if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sid_bit_val)
165 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
168 pool_get (ldp->fd_pool, fde);
169 fde->session_index = vppcom_session_index (sid);
170 fde->fd_index = fde - ldp->fd_pool;
171 fde->fd = fde->fd_index + ldp->sid_bit_val;
172 hash_set (ldp->session_index_to_fd_table, fde->session_index, fde->fd);
173 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
177 static ldp_fd_entry_t *
178 ldp_fd_entry_get_w_lock (u32 fd_index)
180 clib_rwlock_reader_lock (&ldp->fd_table_lock);
181 if (pool_is_free_index (ldp->fd_pool, fd_index))
184 return pool_elt_at_index (ldp->fd_pool, fd_index);
188 ldp_fd_from_sid (u32 sid)
193 clib_rwlock_reader_lock (&ldp->fd_table_lock);
194 fdp = hash_get (ldp->session_index_to_fd_table, vppcom_session_index (sid));
195 fd = fdp ? *fdp : -EMFILE;
196 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
202 ldp_fd_is_sid (int fd)
204 return fd >= ldp->sid_bit_val;
208 ldp_sid_from_fd (int fd)
210 u32 fd_index, session_index;
213 if (!ldp_fd_is_sid (fd))
214 return INVALID_SESSION_ID;
216 fd_index = fd - ldp->sid_bit_val;
217 fde = ldp_fd_entry_get_w_lock (fd_index);
220 LDBG (0, "unknown fd %d", fd);
221 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
222 return INVALID_SESSION_ID;
224 session_index = fde->session_index;
225 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
227 return vppcom_session_handle (session_index);
231 ldp_fd_free_w_sid (u32 sid)
237 fd = ldp_fd_from_sid (sid);
241 fd_index = fd - ldp->sid_bit_val;
242 fde = ldp_fd_entry_get_w_lock (fd_index);
245 hash_unset (ldp->session_index_to_fd_table, fde->session_index);
246 pool_put (ldp->fd_pool, fde);
248 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
254 ldp_worker_ctx_t *ldpw;
257 if (PREDICT_TRUE (ldp->init))
261 ldp->vcl_needs_real_epoll = 1;
262 rv = vppcom_app_create (ldp_get_app_name ());
265 ldp->vcl_needs_real_epoll = 0;
266 if (rv == VPPCOM_EEXIST)
268 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
269 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
273 ldp->vcl_needs_real_epoll = 0;
274 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
275 ldpw = ldp_worker_get_current ();
277 char *env_var_str = getenv (LDP_ENV_DEBUG);
281 if (sscanf (env_var_str, "%u", &tmp) != 1)
282 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
283 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
288 LDBG (0, "configured LDP debug level (%u) from env var "
289 LDP_ENV_DEBUG "!", ldp->debug);
293 env_var_str = getenv (LDP_ENV_APP_NAME);
296 ldp_set_app_name (env_var_str);
297 LDBG (0, "configured LDP app name (%s) from the env var "
298 LDP_ENV_APP_NAME "!", ldp->app_name);
301 env_var_str = getenv (LDP_ENV_SID_BIT);
305 if (sscanf (env_var_str, "%u", &sb) != 1)
307 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
308 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
309 "value %d (0x%x)", getpid (), env_var_str,
310 ldp->sid_bit_val, ldp->sid_bit_val);
312 else if (sb < LDP_SID_BIT_MIN)
314 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
315 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
317 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
318 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
319 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
320 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
321 ldp->sid_bit_val, ldp->sid_bit_val);
323 else if (sb > LDP_SID_BIT_MAX)
325 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
326 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
328 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
329 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
330 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
331 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
332 ldp->sid_bit_val, ldp->sid_bit_val);
336 ldp->sid_bit_val = (1 << sb);
337 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
339 LDBG (0, "configured LDP sid bit (%u) from "
340 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
341 ldp->sid_bit_val, ldp->sid_bit_val);
345 clib_time_init (&ldpw->clib_time);
346 clib_rwlock_init (&ldp->fd_table_lock);
347 LDBG (0, "LDP initialization: done!");
356 u32 sid = ldp_sid_from_fd (fd);
358 if ((errno = -ldp_init ()))
361 if (sid != INVALID_SESSION_ID)
365 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
368 LDBG (0, "fd %d (0x%x): calling libc_close: epfd %u (0x%x)",
371 rv = libc_close (epfd);
374 u32 size = sizeof (epfd);
377 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
381 else if (PREDICT_FALSE (epfd < 0))
388 LDBG (0, "fd %d (0x%x): calling vppcom_session_close: sid %u (0x%x)",
391 refcnt = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REFCNT, 0, 0);
392 rv = vppcom_session_close (sid);
399 ldp_fd_free_w_sid (sid);
403 LDBG (0, "fd %d (0x%x): calling libc_close", fd, fd);
404 rv = libc_close (fd);
409 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
414 read (int fd, void *buf, size_t nbytes)
417 u32 sid = ldp_sid_from_fd (fd);
419 if ((errno = -ldp_init ()))
422 if (sid != INVALID_SESSION_ID)
424 LDBG (2, "fd %d (0x%x): calling vppcom_session_read(): sid %u (0x%x),"
425 " buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
427 size = vppcom_session_read (sid, buf, nbytes);
436 LDBG (2, "fd %d (0x%x): calling libc_read(): buf %p, nbytes %u",
437 fd, fd, buf, nbytes);
439 size = libc_read (fd, buf, nbytes);
442 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
447 readv (int fd, const struct iovec * iov, int iovcnt)
450 u32 sid = ldp_sid_from_fd (fd);
451 int rv = 0, i, total = 0;
453 if ((errno = -ldp_init ()))
456 if (sid != INVALID_SESSION_ID)
460 for (i = 0; i < iovcnt; ++i)
462 LDBG (2, "fd %d (0x%x): calling vppcom_session_read() [%d]:"
463 " sid %u (0x%x), iov %p, iovcnt %d, total %d", fd, fd, i,
464 sid, sid, iov, iovcnt, total);
466 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
472 if (rv < iov[i].iov_len)
474 LDBG (2, "fd %d (0x%x): rv (%d) < iov[%d].iov_len (%d)",
475 fd, fd, rv, i, iov[i].iov_len);
481 while ((rv >= 0) && (total == 0));
493 LDBG (2, "fd %d (0x%x): calling libc_readv(): iov %p, iovcnt %d", fd,
496 size = libc_readv (fd, iov, iovcnt);
500 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
505 write (int fd, const void *buf, size_t nbytes)
508 u32 sid = ldp_sid_from_fd (fd);
510 if ((errno = -ldp_init ()))
513 if (sid != INVALID_SESSION_ID)
515 LDBG (2, "fd %d (0x%x): calling vppcom_session_write(): sid %u (0x%x), "
516 "buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
518 size = vppcom_session_write_msg (sid, (void *) buf, nbytes);
527 LDBG (2, "fd %d (0x%x): calling libc_write(): buf %p, nbytes %u",
528 fd, fd, buf, nbytes);
530 size = libc_write (fd, buf, nbytes);
533 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
538 writev (int fd, const struct iovec * iov, int iovcnt)
540 ssize_t size = 0, total = 0;
541 u32 sid = ldp_sid_from_fd (fd);
545 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
548 if ((errno = -ldp_init ()))
551 if (sid != INVALID_SESSION_ID)
555 for (i = 0; i < iovcnt; ++i)
557 rv = vppcom_session_write_msg (sid, iov[i].iov_base,
564 if (rv < iov[i].iov_len)
569 while ((rv >= 0) && (total == 0));
581 size = libc_writev (fd, iov, iovcnt);
588 fcntl (int fd, int cmd, ...)
590 const char *func_str = __func__;
593 u32 sid = ldp_sid_from_fd (fd);
595 if ((errno = -ldp_init ()))
599 if (sid != INVALID_SESSION_ID)
601 int flags = va_arg (ap, int);
604 size = sizeof (flags);
609 func_str = "vppcom_session_attr[SET_FLAGS]";
610 LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x) "
611 "flags %d (0x%x), size %d", fd, fd, func_str, sid,
612 sid, flags, flags, size);
614 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
619 func_str = "vppcom_session_attr[GET_FLAGS]";
620 LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x), "
621 "flags %d (0x%x), size %d", fd, fd, func_str, sid,
622 sid, flags, flags, size);
624 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
628 LDBG (2, "fd %d (0x%x), cmd %d (F_GETFL): %s() "
629 "returned flags %d (0x%x)", fd, fd, cmd,
630 func_str, flags, flags);
635 /* TODO handle this */
636 LDBG (0, "F_SETFD ignored flags %u", flags);
651 func_str = "libc_vfcntl";
653 LDBG (2, "fd %d (0x%x): calling %s(): cmd %d", fd, fd, func_str, cmd);
655 rv = libc_vfcntl (fd, cmd, ap);
664 int errno_val = errno;
666 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
667 "rv %d, errno = %d", getpid (), fd, fd,
668 func_str, rv, errno_val);
672 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
673 getpid (), fd, fd, rv, rv);
679 ioctl (int fd, unsigned long int cmd, ...)
681 const char *func_str;
684 u32 sid = ldp_sid_from_fd (fd);
686 if ((errno = -ldp_init ()))
690 if (sid != INVALID_SESSION_ID)
692 func_str = "vppcom_session_attr[GET_NREAD]";
699 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
700 getpid (), fd, fd, func_str, sid, sid);
702 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
707 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
708 u32 size = sizeof (flags);
710 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
711 * non-blocking, the flags should be read here and merged
715 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
716 "sid %u (0x%x), flags %d (0x%x), size %d",
717 getpid (), fd, fd, func_str, sid, sid,
720 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
737 func_str = "libc_vioctl";
740 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
741 getpid (), fd, fd, func_str, cmd);
743 rv = libc_vioctl (fd, cmd, ap);
750 int errno_val = errno;
752 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
753 "rv %d, errno = %d", getpid (), fd, fd,
754 func_str, rv, errno_val);
758 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
759 getpid (), fd, fd, rv, rv);
766 ldp_pselect (int nfds, fd_set * __restrict readfds,
767 fd_set * __restrict writefds,
768 fd_set * __restrict exceptfds,
769 const struct timespec *__restrict timeout,
770 const __sigset_t * __restrict sigmask)
772 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
773 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
774 u32 minbits = clib_max (nfds, BITS (uword)), sid;
775 char *func_str = "##";
787 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
788 (f64) 0 : (f64) timeout->tv_sec +
789 (f64) timeout->tv_nsec / (f64) 1000000000;
791 /* select as fine grained sleep */
794 LDBG (3, "sleeping for %.02f seconds", time_out);
796 time_out += clib_time_now (&ldpw->clib_time);
797 while (clib_time_now (&ldpw->clib_time) < time_out)
811 if (nfds <= ldp->sid_bit_val)
813 func_str = "libc_pselect";
815 LDBG (3, "calling %s(): nfds %d, readfds %p, writefds %p, "
816 "exceptfds %p, timeout %p, sigmask %p", func_str, nfds,
817 readfds, writefds, exceptfds, timeout, sigmask);
819 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
824 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
826 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
827 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
828 ldp->sid_bit_val, ldp->sid_bit_val,
829 FD_SETSIZE / 2, FD_SETSIZE / 2);
834 sid_bits = libc_bits = 0;
835 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
838 clib_bitmap_validate (ldpw->sid_rd_bitmap, minbits);
839 clib_bitmap_validate (ldpw->libc_rd_bitmap, minbits);
840 clib_bitmap_validate (ldpw->rd_bitmap, minbits);
841 clib_memcpy_fast (ldpw->rd_bitmap, readfds, n_bytes);
842 memset (readfds, 0, n_bytes);
845 clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({
848 sid = ldp_sid_from_fd (fd);
849 LDBG (3, "readfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
850 if (sid == INVALID_SESSION_ID)
851 clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1);
853 clib_bitmap_set_no_check (ldpw->sid_rd_bitmap,
854 vppcom_session_index (sid), 1);
858 sid_bits_set = clib_bitmap_last_set (ldpw->sid_rd_bitmap) + 1;
859 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
861 libc_bits_set = clib_bitmap_last_set (ldpw->libc_rd_bitmap) + 1;
862 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
864 LDBG (3, "readfds: sid_bits_set %d, sid_bits %d, "
865 "libc_bits_set %d, libc_bits %d", sid_bits_set,
866 sid_bits, libc_bits_set, libc_bits);
870 clib_bitmap_validate (ldpw->sid_wr_bitmap, minbits);
871 clib_bitmap_validate (ldpw->libc_wr_bitmap, minbits);
872 clib_bitmap_validate (ldpw->wr_bitmap, minbits);
873 clib_memcpy_fast (ldpw->wr_bitmap, writefds, n_bytes);
874 memset (writefds, 0, n_bytes);
877 clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({
880 sid = ldp_sid_from_fd (fd);
881 LDBG (3, "writefds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
882 if (sid == INVALID_SESSION_ID)
883 clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1);
885 clib_bitmap_set_no_check (ldpw->sid_wr_bitmap,
886 vppcom_session_index (sid), 1);
890 sid_bits_set = clib_bitmap_last_set (ldpw->sid_wr_bitmap) + 1;
891 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
893 libc_bits_set = clib_bitmap_last_set (ldpw->libc_wr_bitmap) + 1;
894 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
896 LDBG (3, "writefds: sid_bits_set %d, sid_bits %d, "
897 "libc_bits_set %d, libc_bits %d",
898 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
902 clib_bitmap_validate (ldpw->sid_ex_bitmap, minbits);
903 clib_bitmap_validate (ldpw->libc_ex_bitmap, minbits);
904 clib_bitmap_validate (ldpw->ex_bitmap, minbits);
905 clib_memcpy_fast (ldpw->ex_bitmap, exceptfds, n_bytes);
906 memset (exceptfds, 0, n_bytes);
909 clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({
912 sid = ldp_sid_from_fd (fd);
913 LDBG (3, "exceptfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
914 if (sid == INVALID_SESSION_ID)
915 clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1);
917 clib_bitmap_set_no_check (ldpw->sid_ex_bitmap,
918 vppcom_session_index (sid), 1);
922 sid_bits_set = clib_bitmap_last_set (ldpw->sid_ex_bitmap) + 1;
923 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
925 libc_bits_set = clib_bitmap_last_set (ldpw->libc_ex_bitmap) + 1;
926 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
928 LDBG (3, "exceptfds: sid_bits_set %d, sid_bits %d, "
929 "libc_bits_set %d, libc_bits %d",
930 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
933 if (PREDICT_FALSE (!sid_bits && !libc_bits))
944 if (!ldpw->select_vcl)
946 func_str = "vppcom_select";
949 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap,
950 vec_len (ldpw->rd_bitmap) *
951 sizeof (clib_bitmap_t));
953 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap,
954 vec_len (ldpw->wr_bitmap) *
955 sizeof (clib_bitmap_t));
957 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->sid_ex_bitmap,
958 vec_len (ldpw->ex_bitmap) *
959 sizeof (clib_bitmap_t));
961 rv = vppcom_select (sid_bits,
962 readfds ? (unsigned long *) ldpw->rd_bitmap
964 writefds ? (unsigned long *) ldpw->wr_bitmap
966 exceptfds ? (unsigned long *)
967 ldpw->ex_bitmap : NULL, 0);
978 clib_bitmap_foreach (sid, ldpw->rd_bitmap,
980 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
981 if (PREDICT_FALSE (fd < 0))
987 FD_SET (fd, readfds);
994 clib_bitmap_foreach (sid, ldpw->wr_bitmap,
996 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
997 if (PREDICT_FALSE (fd < 0))
1003 FD_SET (fd, writefds);
1010 clib_bitmap_foreach (sid, ldpw->ex_bitmap,
1012 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1013 if (PREDICT_FALSE (fd < 0))
1019 FD_SET (fd, exceptfds);
1023 ldpw->select_vcl = 1;
1028 ldpw->select_vcl = 0;
1032 struct timespec tspec;
1034 func_str = "libc_pselect";
1037 clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap,
1038 vec_len (ldpw->rd_bitmap) *
1039 sizeof (clib_bitmap_t));
1041 clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap,
1042 vec_len (ldpw->wr_bitmap) *
1043 sizeof (clib_bitmap_t));
1045 clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap,
1046 vec_len (ldpw->ex_bitmap) *
1047 sizeof (clib_bitmap_t));
1048 tspec.tv_sec = tspec.tv_nsec = 0;
1049 rv = libc_pselect (libc_bits,
1050 readfds ? readfds : NULL,
1051 writefds ? writefds : NULL,
1052 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1057 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
1061 /* TBD: set timeout to amount of time left */
1062 clib_bitmap_zero (ldpw->rd_bitmap);
1063 clib_bitmap_zero (ldpw->sid_rd_bitmap);
1064 clib_bitmap_zero (ldpw->libc_rd_bitmap);
1065 clib_bitmap_zero (ldpw->wr_bitmap);
1066 clib_bitmap_zero (ldpw->sid_wr_bitmap);
1067 clib_bitmap_zero (ldpw->libc_wr_bitmap);
1068 clib_bitmap_zero (ldpw->ex_bitmap);
1069 clib_bitmap_zero (ldpw->sid_ex_bitmap);
1070 clib_bitmap_zero (ldpw->libc_ex_bitmap);
1076 int errno_val = errno;
1078 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1079 "rv %d, errno = %d", getpid (),
1080 func_str, rv, errno_val);
1084 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1090 select (int nfds, fd_set * __restrict readfds,
1091 fd_set * __restrict writefds,
1092 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1094 struct timespec tspec;
1098 tspec.tv_sec = timeout->tv_sec;
1099 tspec.tv_nsec = timeout->tv_usec * 1000;
1101 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1102 timeout ? &tspec : NULL, NULL);
1105 #ifdef __USE_XOPEN2K
1107 pselect (int nfds, fd_set * __restrict readfds,
1108 fd_set * __restrict writefds,
1109 fd_set * __restrict exceptfds,
1110 const struct timespec *__restrict timeout,
1111 const __sigset_t * __restrict sigmask)
1113 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1118 socket (int domain, int type, int protocol)
1120 const char *func_str;
1122 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1123 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1125 if ((errno = -ldp_init ()))
1128 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1129 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1132 u8 proto = ((sock_type == SOCK_DGRAM) ?
1133 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1135 func_str = "vppcom_session_create";
1137 LDBG (0, "calling %s(): proto %u (%s), is_nonblocking %u",
1138 func_str, proto, vppcom_proto_str (proto), is_nonblocking);
1140 sid = vppcom_session_create (proto, is_nonblocking);
1148 func_str = "ldp_fd_from_sid";
1149 rv = ldp_fd_alloc (sid);
1152 (void) vppcom_session_close (sid);
1160 func_str = "libc_socket";
1162 LDBG (0, "calling %s()", func_str);
1164 rv = libc_socket (domain, type, protocol);
1171 int errno_val = errno;
1173 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1174 "rv %d, errno = %d",
1175 getpid (), func_str, rv, errno_val);
1179 clib_warning ("returning fd %d (0x%x)", getpid (), rv, rv);
1185 * Create two new sockets, of type TYPE in domain DOMAIN and using
1186 * protocol PROTOCOL, which are connected to each other, and put file
1187 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1188 * one will be chosen automatically.
1189 * Returns 0 on success, -1 for errors.
1192 socketpair (int domain, int type, int protocol, int fds[2])
1194 const char *func_str;
1196 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1198 if ((errno = -ldp_init ()))
1201 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1202 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1204 func_str = __func__;
1206 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1212 func_str = "libc_socket";
1214 LDBG (1, "calling %s()", func_str);
1216 rv = libc_socketpair (domain, type, protocol, fds);
1223 int errno_val = errno;
1225 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1226 "rv %d, errno = %d",
1227 getpid (), func_str, rv, errno_val);
1231 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1237 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1240 u32 sid = ldp_sid_from_fd (fd);
1242 if ((errno = -ldp_init ()))
1245 if (sid != INVALID_SESSION_ID)
1249 switch (addr->sa_family)
1252 if (len != sizeof (struct sockaddr_in))
1255 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1256 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1261 ep.is_ip4 = VPPCOM_IS_IP4;
1262 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1263 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1267 if (len != sizeof (struct sockaddr_in6))
1270 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1271 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1276 ep.is_ip4 = VPPCOM_IS_IP6;
1277 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1278 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1282 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1283 "Unsupported address family %u!",
1284 getpid (), fd, fd, sid, sid, addr->sa_family);
1285 errno = EAFNOSUPPORT;
1289 LDBG (0, "fd %d (0x%x): calling vppcom_session_bind(): "
1290 "sid %u (0x%x), addr %p, len %u", fd, fd, sid, sid, addr, len);
1292 rv = vppcom_session_bind (sid, &ep);
1293 if (rv != VPPCOM_OK)
1301 LDBG (0, "fd %d (0x%x): calling libc_bind(): addr %p, len %u",
1304 rv = libc_bind (fd, addr, len);
1308 LDBG (1, "fd %d (0x%x): returning %d", fd, fd, rv);
1314 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1315 vppcom_endpt_t * ep)
1318 int sa_len, copy_len;
1320 if ((errno = -ldp_init ()))
1323 if (addr && len && ep)
1325 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1326 switch (addr->sa_family)
1329 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1330 if (*len > sizeof (struct sockaddr_in))
1331 *len = sizeof (struct sockaddr_in);
1332 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1333 copy_len = *len - sa_len;
1335 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1340 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1341 if (*len > sizeof (struct sockaddr_in6))
1342 *len = sizeof (struct sockaddr_in6);
1343 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1344 copy_len = *len - sa_len;
1346 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1347 __in6_u.__u6_addr8, ep->ip, copy_len);
1360 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1363 const char *func_str;
1364 u32 sid = ldp_sid_from_fd (fd);
1366 if ((errno = -ldp_init ()))
1369 if (sid != INVALID_SESSION_ID)
1372 u8 addr_buf[sizeof (struct in6_addr)];
1373 u32 size = sizeof (ep);
1376 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1379 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1381 getpid (), fd, fd, func_str, sid, sid, addr, len);
1383 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1384 if (rv != VPPCOM_OK)
1391 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1392 if (rv != VPPCOM_OK)
1401 func_str = "libc_getsockname";
1404 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1406 getpid (), fd, fd, func_str, addr, len);
1408 rv = libc_getsockname (fd, addr, len);
1415 int errno_val = errno;
1417 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1418 "rv %d, errno = %d", getpid (), fd, fd,
1419 func_str, rv, errno_val);
1423 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1424 getpid (), fd, fd, rv, rv);
1430 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1433 u32 sid = ldp_sid_from_fd (fd);
1435 if ((errno = -ldp_init ()))
1440 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1441 getpid (), fd, fd, len);
1447 if (sid != INVALID_SESSION_ID)
1451 switch (addr->sa_family)
1454 if (len != sizeof (struct sockaddr_in))
1457 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1458 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1463 ep.is_ip4 = VPPCOM_IS_IP4;
1464 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1465 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1469 if (len != sizeof (struct sockaddr_in6))
1472 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1473 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1478 ep.is_ip4 = VPPCOM_IS_IP6;
1479 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1480 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1484 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1485 "Unsupported address family %u!",
1486 getpid (), fd, fd, sid, sid, addr->sa_family);
1487 errno = EAFNOSUPPORT;
1491 LDBG (0, "fd %d (0x%x): calling vppcom_session_connect(): sid %u (0x%x)"
1492 " addr %p len %u", fd, fd, sid, sid, addr, len);
1494 rv = vppcom_session_connect (sid, &ep);
1495 if (rv != VPPCOM_OK)
1503 LDBG (0, "fd %d (0x%x): calling libc_connect(): addr %p, len %u",
1506 rv = libc_connect (fd, addr, len);
1510 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
1515 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1518 const char *func_str;
1519 u32 sid = ldp_sid_from_fd (fd);
1521 if ((errno = -ldp_init ()))
1524 if (sid != INVALID_SESSION_ID)
1527 u8 addr_buf[sizeof (struct in6_addr)];
1528 u32 size = sizeof (ep);
1531 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1534 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1536 getpid (), fd, fd, func_str, sid, sid, addr, len);
1538 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1539 if (rv != VPPCOM_OK)
1546 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1547 if (rv != VPPCOM_OK)
1556 func_str = "libc_getpeername";
1559 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1561 getpid (), fd, fd, func_str, addr, len);
1563 rv = libc_getpeername (fd, addr, len);
1570 int errno_val = errno;
1572 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1573 "rv %d, errno = %d", getpid (), fd, fd,
1574 func_str, rv, errno_val);
1578 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1579 getpid (), fd, fd, rv, rv);
1585 send (int fd, const void *buf, size_t n, int flags)
1588 const char *func_str;
1589 u32 sid = ldp_sid_from_fd (fd);
1591 if ((errno = -ldp_init ()))
1594 if (sid != INVALID_SESSION_ID)
1597 func_str = "vppcom_session_sendto";
1600 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1601 "buf %p, n %u, flags 0x%x",
1602 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1604 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1605 if (size < VPPCOM_OK)
1613 func_str = "libc_send";
1616 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1617 "buf %p, n %u, flags 0x%x",
1618 getpid (), fd, fd, func_str, buf, n, flags);
1620 size = libc_send (fd, buf, n, flags);
1627 int errno_val = errno;
1629 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1630 "rv %d, errno = %d", getpid (), fd, fd,
1631 func_str, size, errno_val);
1635 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1636 getpid (), fd, fd, size, size);
1642 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1644 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1646 const char *func_str;
1647 u32 sid = ldp_sid_from_fd (out_fd);
1649 if ((errno = -ldp_init ()))
1652 if (sid != INVALID_SESSION_ID)
1655 ssize_t results = 0;
1656 size_t n_bytes_left = len;
1657 size_t bytes_to_read;
1661 u32 flags, flags_len = sizeof (flags);
1663 func_str = "vppcom_session_attr[GET_FLAGS]";
1664 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1666 if (PREDICT_FALSE (rv != VPPCOM_OK))
1668 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1669 "sid %u (0x%x), returned %d (%s)!", getpid (),
1670 out_fd, out_fd, func_str, sid, sid, rv,
1671 vppcom_retval_str (rv));
1673 vec_reset_length (ldpw->io_buffer);
1681 off_t off = lseek (in_fd, *offset, SEEK_SET);
1682 if (PREDICT_FALSE (off == -1))
1686 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1687 "SEEK_SET failed: in_fd %d, offset %p, "
1688 "*offset %ld, rv %ld, errno %d", getpid (),
1689 out_fd, out_fd, in_fd, offset, *offset, off,
1696 ASSERT (off == *offset);
1701 func_str = "vppcom_session_attr[GET_NWRITE]";
1702 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1706 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1707 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1708 sid, sid, size, vppcom_retval_str (size));
1709 vec_reset_length (ldpw->io_buffer);
1715 bytes_to_read = size;
1718 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1719 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1720 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1723 if (bytes_to_read == 0)
1725 if (flags & O_NONBLOCK)
1730 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1732 getpid (), out_fd, out_fd, sid, sid);
1740 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1741 vec_validate (ldpw->io_buffer, bytes_to_read);
1742 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1745 func_str = "libc_read";
1747 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1748 "io_buffer %p, bytes_to_read %lu, rv %d, "
1749 "errno %d", getpid (), out_fd, out_fd, func_str,
1750 in_fd, ldpw->io_buffer, bytes_to_read, nbytes,
1756 vec_reset_length (ldpw->io_buffer);
1762 func_str = "vppcom_session_write";
1765 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1766 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1767 out_fd, out_fd, func_str, sid, sid, ldpw->io_buffer, nbytes,
1768 results, n_bytes_left);
1770 size = vppcom_session_write (sid, ldpw->io_buffer, nbytes);
1773 if (size == VPPCOM_EAGAIN)
1775 if (flags & O_NONBLOCK)
1781 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1782 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1792 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1793 "sid %u, io_buffer %p, nbytes %u "
1795 getpid (), out_fd, out_fd, func_str,
1796 sid, ldpw->io_buffer, nbytes,
1797 size, vppcom_retval_str (size));
1801 vec_reset_length (ldpw->io_buffer);
1810 ASSERT (n_bytes_left >= nbytes);
1811 n_bytes_left = n_bytes_left - nbytes;
1813 while (n_bytes_left > 0);
1816 vec_reset_length (ldpw->io_buffer);
1819 off_t off = lseek (in_fd, *offset, SEEK_SET);
1820 if (PREDICT_FALSE (off == -1))
1824 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1825 "in_fd %d, offset %p, *offset %ld, "
1826 "rv %ld, errno %d", getpid (), in_fd,
1827 offset, *offset, off, errno_val);
1833 ASSERT (off == *offset);
1834 *offset += results + 1;
1846 func_str = "libc_send";
1849 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1850 "in_fd %d, offset %p, len %u",
1851 getpid (), out_fd, out_fd, func_str,
1852 in_fd, offset, len);
1854 size = libc_sendfile (out_fd, in_fd, offset, len);
1862 int errno_val = errno;
1864 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1865 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1866 func_str, size, errno_val);
1870 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1871 getpid (), out_fd, out_fd, size, size);
1877 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1879 return sendfile (out_fd, in_fd, offset, len);
1883 recv (int fd, void *buf, size_t n, int flags)
1888 if ((errno = -ldp_init ()))
1891 sid = ldp_sid_from_fd (fd);
1892 if (sid != INVALID_SESSION_ID)
1894 LDBG (2, "fd %d (0x%x): calling vcl recvfrom: sid %u (0x%x), buf %p,"
1895 " n %u, flags 0x%x", fd, fd, sid, sid, buf, n, flags);
1897 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1903 LDBG (2, "fd %d (0x%x): calling libc_recvfrom(): buf %p, n %u, "
1904 "flags 0x%x", fd, fd, buf, n, flags);
1906 size = libc_recv (fd, buf, n, flags);
1913 sendto (int fd, const void *buf, size_t n, int flags,
1914 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1917 const char *func_str = __func__;
1918 u32 sid = ldp_sid_from_fd (fd);
1920 if ((errno = -ldp_init ()))
1923 if (sid != INVALID_SESSION_ID)
1925 vppcom_endpt_t *ep = 0;
1931 switch (addr->sa_family)
1934 ep->is_ip4 = VPPCOM_IS_IP4;
1936 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1938 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1942 ep->is_ip4 = VPPCOM_IS_IP6;
1944 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1946 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1950 errno = EAFNOSUPPORT;
1956 func_str = "vppcom_session_sendto";
1959 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1960 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
1961 getpid (), fd, fd, func_str, sid, sid, buf, n,
1964 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
1973 func_str = "libc_sendto";
1976 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1977 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
1978 getpid (), fd, fd, func_str, buf, n, flags,
1981 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1989 int errno_val = errno;
1991 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1992 "rv %d, errno = %d", getpid (), fd, fd,
1993 func_str, size, errno_val);
1997 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1998 getpid (), fd, fd, size, size);
2004 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2005 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2008 const char *func_str;
2009 u32 sid = ldp_sid_from_fd (fd);
2011 if ((errno = -ldp_init ()))
2014 if (sid != INVALID_SESSION_ID)
2017 u8 src_addr[sizeof (struct sockaddr_in6)];
2019 func_str = "vppcom_session_recvfrom";
2022 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2023 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2024 getpid (), fd, fd, func_str, sid, sid, buf, n,
2029 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2032 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2035 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2045 func_str = "libc_recvfrom";
2048 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2049 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2050 getpid (), fd, fd, func_str, buf, n, flags,
2053 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2060 int errno_val = errno;
2062 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2063 "rv %d, errno = %d", getpid (), fd, fd,
2064 func_str, size, errno_val);
2068 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2069 getpid (), fd, fd, size, size);
2075 sendmsg (int fd, const struct msghdr * message, int flags)
2078 const char *func_str;
2079 u32 sid = ldp_sid_from_fd (fd);
2081 if ((errno = -ldp_init ()))
2084 if (sid != INVALID_SESSION_ID)
2086 func_str = __func__;
2088 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2094 func_str = "libc_sendmsg";
2097 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2098 "message %p, flags 0x%x",
2099 getpid (), fd, fd, func_str, message, flags);
2101 size = libc_sendmsg (fd, message, flags);
2108 int errno_val = errno;
2110 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2111 "rv %d, errno = %d", getpid (), fd, fd,
2112 func_str, size, errno_val);
2116 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2117 getpid (), fd, fd, size, size);
2124 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2127 const char *func_str;
2128 u32 sid = ldp_sid_from_fd (fd);
2130 if ((errno = -ldp_init ()))
2133 if (sid != INVALID_SESSION_ID)
2135 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2141 func_str = "libc_sendmmsg";
2144 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2145 "vmessages %p, vlen %u, flags 0x%x",
2146 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2148 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2155 int errno_val = errno;
2157 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2158 "rv %d, errno = %d", getpid (), fd, fd,
2159 func_str, size, errno_val);
2163 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2164 getpid (), fd, fd, size, size);
2171 recvmsg (int fd, struct msghdr * message, int flags)
2174 const char *func_str;
2175 u32 sid = ldp_sid_from_fd (fd);
2177 if ((errno = -ldp_init ()))
2180 if (sid != INVALID_SESSION_ID)
2182 func_str = __func__;
2184 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2190 func_str = "libc_recvmsg";
2193 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2194 "message %p, flags 0x%x",
2195 getpid (), fd, fd, func_str, message, flags);
2197 size = libc_recvmsg (fd, message, flags);
2204 int errno_val = errno;
2206 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2207 "rv %d, errno = %d", getpid (), fd, fd,
2208 func_str, size, errno_val);
2212 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2213 getpid (), fd, fd, size, size);
2220 recvmmsg (int fd, struct mmsghdr *vmessages,
2221 unsigned int vlen, int flags, struct timespec *tmo)
2224 const char *func_str;
2225 u32 sid = ldp_sid_from_fd (fd);
2227 if ((errno = -ldp_init ()))
2230 if (sid != INVALID_SESSION_ID)
2232 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2238 func_str = "libc_recvmmsg";
2241 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2242 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2243 getpid (), fd, fd, func_str, vmessages, vlen,
2246 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2253 int errno_val = errno;
2255 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2256 "rv %d, errno = %d", getpid (), fd, fd,
2257 func_str, size, errno_val);
2261 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2262 getpid (), fd, fd, size, size);
2269 getsockopt (int fd, int level, int optname,
2270 void *__restrict optval, socklen_t * __restrict optlen)
2273 const char *func_str = __func__;
2274 u32 sid = ldp_sid_from_fd (fd);
2275 u32 buflen = optlen ? (u32) * optlen : 0;
2277 if ((errno = -ldp_init ()))
2280 if (sid != INVALID_SESSION_ID)
2290 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2292 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2294 getpid (), fd, fd, func_str, sid, sid);
2295 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2299 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2301 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2303 getpid (), fd, fd, func_str, sid, sid);
2304 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2308 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2310 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2312 getpid (), fd, fd, func_str, sid, sid);
2313 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2317 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2319 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2320 "sid %u (0x%x), SOL_TCP",
2321 getpid (), fd, fd, func_str, sid, sid);
2322 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2326 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2329 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2330 "SOL_TCP, TCP_INFO, optval %p, "
2331 "optlen %d: #LDP-NOP#",
2332 getpid (), fd, fd, sid, sid,
2334 memset (optval, 0, *optlen);
2342 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2343 "sid %u (0x%x), SOL_TCP, "
2344 "optname %d unsupported!",
2345 getpid (), fd, fd, func_str, sid, sid, optname);
2353 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2355 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2357 getpid (), fd, fd, func_str, sid, sid);
2358 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2363 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2364 "sid %u (0x%x), SOL_IPV6, "
2365 "optname %d unsupported!",
2366 getpid (), fd, fd, func_str, sid, sid, optname);
2374 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2376 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2378 getpid (), fd, fd, func_str, sid, sid);
2379 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2383 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2385 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2387 getpid (), fd, fd, func_str, sid, sid);
2388 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2392 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2394 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2396 getpid (), fd, fd, func_str, sid, sid);
2397 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2399 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2402 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2404 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2405 "sid %u (0x%x), optlen %d",
2406 getpid (), fd, fd, func_str, sid, sid, buflen);
2407 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2411 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2413 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2414 "sid %u (0x%x), optlen %d",
2415 getpid (), fd, fd, func_str, sid, sid, buflen);
2416 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2420 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2422 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2424 getpid (), fd, fd, func_str, sid, sid);
2425 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2429 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2431 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2433 getpid (), fd, fd, func_str, sid, sid);
2434 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2438 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2440 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2442 getpid (), fd, fd, func_str, sid, sid);
2443 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2448 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2449 "sid %u (0x%x), SOL_SOCKET, "
2450 "optname %d unsupported!",
2451 getpid (), fd, fd, func_str, sid, sid, optname);
2459 if (rv != VPPCOM_OK)
2467 func_str = "libc_getsockopt";
2470 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2471 "optname %d, optval %p, optlen %d",
2472 getpid (), fd, fd, func_str, level, optname,
2475 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2482 int errno_val = errno;
2484 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2485 "rv %d, errno = %d", getpid (), fd, fd,
2486 func_str, rv, errno_val);
2490 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2491 getpid (), fd, fd, rv, rv);
2497 setsockopt (int fd, int level, int optname,
2498 const void *optval, socklen_t optlen)
2501 const char *func_str = __func__;
2502 u32 sid = ldp_sid_from_fd (fd);
2504 if ((errno = -ldp_init ()))
2507 if (sid != INVALID_SESSION_ID)
2517 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2519 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2521 getpid (), fd, fd, func_str, sid, sid);
2522 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2523 (void *) optval, &optlen);
2526 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2528 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2530 getpid (), fd, fd, func_str, sid, sid);
2531 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2532 (void *) optval, &optlen);
2535 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2537 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2539 getpid (), fd, fd, func_str, sid, sid);
2540 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2541 (void *) optval, &optlen);
2544 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2546 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2547 "sid %u (0x%x), SOL_TCP",
2548 getpid (), fd, fd, func_str, sid, sid);
2549 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2550 (void *) optval, &optlen);
2554 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2555 "sid %u (0x%x), SOL_TCP, "
2556 "optname %d unsupported!",
2557 getpid (), fd, fd, func_str, sid, sid, optname);
2565 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2567 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2569 getpid (), fd, fd, func_str, sid, sid);
2570 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2571 (void *) optval, &optlen);
2575 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2576 "sid %u (0x%x), SOL_IPV6, "
2577 "optname %d unsupported!",
2578 getpid (), fd, fd, func_str, sid, sid, optname);
2586 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2588 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2590 getpid (), fd, fd, func_str, sid, sid);
2591 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2592 (void *) optval, &optlen);
2595 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2597 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2599 getpid (), fd, fd, func_str, sid, sid);
2600 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2601 (void *) optval, &optlen);
2604 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2606 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2608 getpid (), fd, fd, func_str, sid, sid);
2609 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2610 (void *) optval, &optlen);
2614 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2615 "sid %u (0x%x), SOL_SOCKET, "
2616 "optname %d unsupported!",
2617 getpid (), fd, fd, func_str, sid, sid, optname);
2625 if (rv != VPPCOM_OK)
2633 func_str = "libc_setsockopt";
2636 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2637 "optname %d, optval %p, optlen %d",
2638 getpid (), fd, fd, func_str, level, optname,
2641 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2648 int errno_val = errno;
2650 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2651 "rv %d, errno = %d", getpid (), fd, fd,
2652 func_str, rv, errno_val);
2656 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2657 getpid (), fd, fd, rv, rv);
2663 listen (int fd, int n)
2666 u32 sid = ldp_sid_from_fd (fd);
2668 if ((errno = -ldp_init ()))
2671 if (sid != INVALID_SESSION_ID)
2673 LDBG (0, "fd %d (0x%x): calling vppcom_session_listen():"
2674 " sid %u (0x%x), n %d", fd, fd, sid, sid, n);
2676 rv = vppcom_session_listen (sid, n);
2677 if (rv != VPPCOM_OK)
2685 LDBG (0, "fd %d (0x%x): calling libc_listen(): n %d", fd, fd, n);
2687 rv = libc_listen (fd, n);
2690 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
2695 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2696 socklen_t * __restrict addr_len, int flags)
2699 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2702 if ((errno = -ldp_init ()))
2705 if (listen_sid != INVALID_SESSION_ID)
2708 u8 src_addr[sizeof (struct sockaddr_in6)];
2709 memset (&ep, 0, sizeof (ep));
2712 LDBG (0, "listen fd %d (0x%x): calling vppcom_session_accept:"
2713 " listen sid %u (0x%x), ep %p, flags 0x%x", listen_fd,
2714 listen_fd, listen_sid, listen_sid, ep, flags);
2716 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2719 errno = -accept_sid;
2724 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2725 if (rv != VPPCOM_OK)
2727 (void) vppcom_session_close ((u32) accept_sid);
2733 rv = ldp_fd_alloc ((u32) accept_sid);
2736 (void) vppcom_session_close ((u32) accept_sid);
2745 LDBG (0, "listen fd %d (0x%x): calling libc_accept4(): "
2746 "addr %p, addr_len %p, flags 0x%x", listen_fd,
2747 listen_fd, addr, addr_len, flags);
2749 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2752 LDBG (1, "listen fd %d (0x%x): returning %d (0x%x)", listen_fd, listen_fd,
2759 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2762 return ldp_accept4 (fd, addr, addr_len, flags);
2766 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2768 return ldp_accept4 (fd, addr, addr_len, 0);
2772 shutdown (int fd, int how)
2776 if ((errno = -ldp_init ()))
2779 if (ldp_fd_is_sid (fd))
2781 u32 fd_index = fd - ldp->sid_bit_val;
2782 ldp_fd_entry_t *fde;
2784 fde = ldp_fd_entry_get_w_lock (fd_index);
2787 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
2793 fde->flags |= LDP_F_SHUT_RD;
2794 else if (how == SHUT_WR)
2795 fde->flags |= LDP_F_SHUT_WR;
2796 else if (how == SHUT_RDWR)
2797 fde->flags |= (LDP_F_SHUT_RD | LDP_F_SHUT_WR);
2799 if ((fde->flags & LDP_F_SHUT_RD) && (fde->flags & LDP_F_SHUT_WR))
2802 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
2803 LDBG (0, "fd %d (0x%x): calling vcl shutdown: how %d", fd, fd, how);
2807 LDBG (1, "fd %d (0x%x): calling libc_shutdown: how %d", fd, fd, how);
2808 rv = libc_shutdown (fd, how);
2815 epoll_create1 (int flags)
2817 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2818 const char *func_str;
2821 if ((errno = -ldp_init ()))
2824 if (ldp->vcl_needs_real_epoll)
2826 rv = libc_epoll_create1 (flags);
2827 ldp->vcl_needs_real_epoll = 0;
2828 ldpw->vcl_mq_epfd = rv;
2829 LDBG (0, "created vcl epfd %u", rv);
2832 func_str = "vppcom_epoll_create";
2834 LDBG (1, "calling %s()", func_str);
2836 rv = vppcom_epoll_create ();
2838 if (PREDICT_FALSE (rv < 0))
2844 rv = ldp_fd_alloc ((u32) rv);
2850 int errno_val = errno;
2852 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2853 "rv %d, errno = %d",
2854 getpid (), func_str, rv, errno_val);
2858 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2864 epoll_create (int size)
2866 return epoll_create1 (0);
2870 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2872 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
2873 const char *func_str;
2876 if ((errno = -ldp_init ()))
2879 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
2881 /* The LDP epoll_create1 always creates VCL epfd's.
2882 * The app should never have a kernel base epoll fd unless it
2883 * was acquired outside of the LD_PRELOAD process context.
2884 * In any case, if we get one, punt it to libc_epoll_ctl.
2886 func_str = "libc_epoll_ctl";
2888 LDBG (1, "epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
2889 " event %p", epfd, epfd, func_str, op, fd, fd, event);
2891 rv = libc_epoll_ctl (epfd, op, fd, event);
2895 sid = ldp_sid_from_fd (fd);
2897 LDBG (0, "epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
2898 epfd, epfd, vep_idx, vep_idx, sid, sid);
2900 if (sid != INVALID_SESSION_ID)
2902 func_str = "vppcom_epoll_ctl";
2904 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
2905 " op %d, sid %u (0x%x), event %p", epfd, epfd,
2906 func_str, vep_idx, vep_idx, sid, sid, event);
2908 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
2909 if (rv != VPPCOM_OK)
2918 u32 size = sizeof (epfd);
2920 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
2921 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
2923 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): %s() "
2924 "returned libc_epfd %d (0x%x)", epfd, epfd,
2925 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
2929 func_str = "libc_epoll_create1";
2931 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): "
2932 "calling %s(): EPOLL_CLOEXEC", epfd, epfd,
2933 vep_idx, vep_idx, func_str);
2935 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2942 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
2943 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
2944 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
2945 epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
2948 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
2957 else if (PREDICT_FALSE (libc_epfd < 0))
2964 func_str = "libc_epoll_ctl";
2966 LDBG (1, "epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
2967 "op %d, fd %d (0x%x), event %p", epfd, epfd, func_str,
2968 libc_epfd, libc_epfd, op, fd, fd, event);
2970 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2978 int errno_val = errno;
2980 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2981 "rv %d, errno = %d", getpid (), fd, fd,
2982 func_str, rv, errno_val);
2986 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2987 getpid (), fd, fd, rv, rv);
2993 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2994 int timeout, const sigset_t * sigmask)
2996 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2997 double time_to_wait = (double) 0, time_out, now = 0;
2998 u32 vep_idx = ldp_sid_from_fd (epfd);
2999 int libc_epfd, rv = 0;
3001 if ((errno = -ldp_init ()))
3004 if (PREDICT_FALSE (!events || (timeout < -1)))
3010 if (epfd == ldpw->vcl_mq_epfd)
3011 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3013 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3015 LDBG (0, "epfd %d (0x%x): bad vep_idx %d (0x%x)!", epfd, epfd, vep_idx,
3021 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
3022 time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
3024 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3025 if (PREDICT_FALSE (libc_epfd < 0))
3032 LDBG (2, "epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3033 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3034 epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3035 maxevents, timeout, sigmask, time_to_wait, time_out);
3038 if (!ldpw->epoll_wait_vcl)
3040 LDBG (3, "epfd %d (0x%x): calling vcl_epoll_wait: vep_idx %d (0x%x)"
3041 " events %p, maxevents %d", epfd, epfd, vep_idx, vep_idx,
3044 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3047 ldpw->epoll_wait_vcl = 1;
3058 ldpw->epoll_wait_vcl = 0;
3062 LDBG (3, "epfd %d (0x%x): calling libc_epoll_wait: libc_epfd %d "
3063 "(0x%x), events %p, maxevents %d, sigmask %p", epfd, epfd,
3064 libc_epfd, libc_epfd, events, maxevents, sigmask);
3066 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
3072 now = clib_time_now (&ldpw->clib_time);
3074 while (now < time_out);
3081 epoll_pwait (int epfd, struct epoll_event *events,
3082 int maxevents, int timeout, const sigset_t * sigmask)
3084 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3088 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3090 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3094 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3096 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3097 const char *func_str = __func__;
3098 int rv, i, n_revents = 0;
3101 double wait_for_time;
3103 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
3106 wait_for_time = (f64) timeout / 1000;
3110 for (i = 0; i < nfds; i++)
3115 LDBG (3, "fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3116 i, fds[i].fd, fds[i].fd, fds[i].events, fds[i].revents);
3118 sid = ldp_sid_from_fd (fds[i].fd);
3119 if (sid != INVALID_SESSION_ID)
3121 fds[i].fd = -fds[i].fd;
3122 vec_add2 (ldpw->vcl_poll, vp, 1);
3125 vp->events = fds[i].events;
3126 #ifdef __USE_XOPEN2K
3127 if (fds[i].events & POLLRDNORM)
3128 vp->events |= POLLIN;
3129 if (fds[i].events & POLLWRNORM)
3130 vp->events |= POLLOUT;
3132 vp->revents = fds[i].revents;
3136 vec_add1 (ldpw->libc_poll, fds[i]);
3137 vec_add1 (ldpw->libc_poll_idxs, i);
3143 if (vec_len (ldpw->vcl_poll))
3145 func_str = "vppcom_poll";
3147 LDBG (3, "calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3148 "n_libc_fds %u", func_str, ldpw->vcl_poll,
3149 vec_len (ldpw->vcl_poll), vec_len (ldpw->vcl_poll),
3150 vec_len (ldpw->libc_poll));
3152 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
3163 if (vec_len (ldpw->libc_poll))
3165 func_str = "libc_poll";
3167 LDBG (3, "calling %s(): fds %p, nfds %u: n_sids %u",
3168 fds, nfds, vec_len (ldpw->vcl_poll));
3170 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
3183 while ((wait_for_time == -1) ||
3184 (clib_time_now (&ldpw->clib_time) < wait_for_time));
3188 vec_foreach (vp, ldpw->vcl_poll)
3190 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3191 fds[vp->fds_ndx].revents = vp->revents;
3192 #ifdef __USE_XOPEN2K
3193 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3194 (fds[vp->fds_ndx].events & POLLRDNORM))
3195 fds[vp->fds_ndx].revents |= POLLRDNORM;
3196 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3197 (fds[vp->fds_ndx].events & POLLWRNORM))
3198 fds[vp->fds_ndx].revents |= POLLWRNORM;
3201 vec_reset_length (ldpw->vcl_poll);
3203 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
3205 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
3207 vec_reset_length (ldpw->libc_poll_idxs);
3208 vec_reset_length (ldpw->libc_poll);
3214 int errno_val = errno;
3216 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3217 "rv %d, errno = %d", getpid (),
3218 func_str, rv, errno_val);
3223 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3224 "n_libc_fds %d", getpid (), rv, rv,
3225 vec_len (ldpw->vcl_poll), vec_len (ldpw->libc_poll));
3227 for (i = 0; i < nfds; i++)
3232 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3233 ".events = 0x%x, .revents = 0x%x",
3234 getpid (), i, fds[i].fd, fds[i].fd,
3235 fds[i].events, fds[i].revents);
3246 ppoll (struct pollfd *fds, nfds_t nfds,
3247 const struct timespec *timeout, const sigset_t * sigmask)
3249 if ((errno = -ldp_init ()))
3252 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3260 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3262 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3265 * This function is called when the library is loaded
3268 ldp_constructor (void)
3270 swrap_constructor ();
3271 if (ldp_init () != 0)
3272 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3274 else if (LDP_DEBUG > 0)
3275 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3279 * This function is called when the library is unloaded
3282 ldp_destructor (void)
3284 swrap_destructor ();
3288 /* Don't use clib_warning() here because that calls writev()
3289 * which will call ldp_init().
3292 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3293 __func__, __LINE__, getpid ());
3298 * fd.io coding-style-patch-verification: ON
3301 * eval: (c-set-style "gnu")