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 ? ldpw->rd_bitmap : NULL,
963 writefds ? ldpw->wr_bitmap : NULL,
964 exceptfds ? ldpw->ex_bitmap : NULL, 0);
975 clib_bitmap_foreach (sid, ldpw->rd_bitmap,
977 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
978 if (PREDICT_FALSE (fd < 0))
984 FD_SET (fd, readfds);
991 clib_bitmap_foreach (sid, ldpw->wr_bitmap,
993 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
994 if (PREDICT_FALSE (fd < 0))
1000 FD_SET (fd, writefds);
1007 clib_bitmap_foreach (sid, ldpw->ex_bitmap,
1009 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
1010 if (PREDICT_FALSE (fd < 0))
1016 FD_SET (fd, exceptfds);
1020 ldpw->select_vcl = 1;
1025 ldpw->select_vcl = 0;
1029 struct timespec tspec;
1031 func_str = "libc_pselect";
1034 clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap,
1035 vec_len (ldpw->rd_bitmap) *
1036 sizeof (clib_bitmap_t));
1038 clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap,
1039 vec_len (ldpw->wr_bitmap) *
1040 sizeof (clib_bitmap_t));
1042 clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap,
1043 vec_len (ldpw->ex_bitmap) *
1044 sizeof (clib_bitmap_t));
1045 tspec.tv_sec = tspec.tv_nsec = 0;
1046 rv = libc_pselect (libc_bits,
1047 readfds ? readfds : NULL,
1048 writefds ? writefds : NULL,
1049 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1054 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
1058 /* TBD: set timeout to amount of time left */
1059 clib_bitmap_zero (ldpw->rd_bitmap);
1060 clib_bitmap_zero (ldpw->sid_rd_bitmap);
1061 clib_bitmap_zero (ldpw->libc_rd_bitmap);
1062 clib_bitmap_zero (ldpw->wr_bitmap);
1063 clib_bitmap_zero (ldpw->sid_wr_bitmap);
1064 clib_bitmap_zero (ldpw->libc_wr_bitmap);
1065 clib_bitmap_zero (ldpw->ex_bitmap);
1066 clib_bitmap_zero (ldpw->sid_ex_bitmap);
1067 clib_bitmap_zero (ldpw->libc_ex_bitmap);
1073 int errno_val = errno;
1075 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1076 "rv %d, errno = %d", getpid (),
1077 func_str, rv, errno_val);
1081 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1087 select (int nfds, fd_set * __restrict readfds,
1088 fd_set * __restrict writefds,
1089 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1091 struct timespec tspec;
1095 tspec.tv_sec = timeout->tv_sec;
1096 tspec.tv_nsec = timeout->tv_usec * 1000;
1098 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1099 timeout ? &tspec : NULL, NULL);
1102 #ifdef __USE_XOPEN2K
1104 pselect (int nfds, fd_set * __restrict readfds,
1105 fd_set * __restrict writefds,
1106 fd_set * __restrict exceptfds,
1107 const struct timespec *__restrict timeout,
1108 const __sigset_t * __restrict sigmask)
1110 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1115 socket (int domain, int type, int protocol)
1117 const char *func_str;
1119 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1120 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1122 if ((errno = -ldp_init ()))
1125 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1126 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1129 u8 proto = ((sock_type == SOCK_DGRAM) ?
1130 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1132 func_str = "vppcom_session_create";
1134 LDBG (0, "calling %s(): proto %u (%s), is_nonblocking %u",
1135 func_str, proto, vppcom_proto_str (proto), is_nonblocking);
1137 sid = vppcom_session_create (proto, is_nonblocking);
1145 func_str = "ldp_fd_from_sid";
1146 rv = ldp_fd_alloc (sid);
1149 (void) vppcom_session_close (sid);
1157 func_str = "libc_socket";
1159 LDBG (0, "calling %s()", func_str);
1161 rv = libc_socket (domain, type, protocol);
1168 int errno_val = errno;
1170 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1171 "rv %d, errno = %d",
1172 getpid (), func_str, rv, errno_val);
1176 clib_warning ("returning fd %d (0x%x)", getpid (), rv, rv);
1182 * Create two new sockets, of type TYPE in domain DOMAIN and using
1183 * protocol PROTOCOL, which are connected to each other, and put file
1184 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1185 * one will be chosen automatically.
1186 * Returns 0 on success, -1 for errors.
1189 socketpair (int domain, int type, int protocol, int fds[2])
1191 const char *func_str;
1193 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1195 if ((errno = -ldp_init ()))
1198 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1199 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1201 func_str = __func__;
1203 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1209 func_str = "libc_socket";
1211 LDBG (1, "calling %s()", func_str);
1213 rv = libc_socketpair (domain, type, protocol, fds);
1220 int errno_val = errno;
1222 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1223 "rv %d, errno = %d",
1224 getpid (), func_str, rv, errno_val);
1228 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1234 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1237 u32 sid = ldp_sid_from_fd (fd);
1239 if ((errno = -ldp_init ()))
1242 if (sid != INVALID_SESSION_ID)
1246 switch (addr->sa_family)
1249 if (len != sizeof (struct sockaddr_in))
1252 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1253 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1258 ep.is_ip4 = VPPCOM_IS_IP4;
1259 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1260 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1264 if (len != sizeof (struct sockaddr_in6))
1267 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1268 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1273 ep.is_ip4 = VPPCOM_IS_IP6;
1274 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1275 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1279 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1280 "Unsupported address family %u!",
1281 getpid (), fd, fd, sid, sid, addr->sa_family);
1282 errno = EAFNOSUPPORT;
1286 LDBG (0, "fd %d (0x%x): calling vppcom_session_bind(): "
1287 "sid %u (0x%x), addr %p, len %u", fd, fd, sid, sid, addr, len);
1289 rv = vppcom_session_bind (sid, &ep);
1290 if (rv != VPPCOM_OK)
1298 LDBG (0, "fd %d (0x%x): calling libc_bind(): addr %p, len %u",
1301 rv = libc_bind (fd, addr, len);
1305 LDBG (1, "fd %d (0x%x): returning %d", fd, fd, rv);
1311 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1312 vppcom_endpt_t * ep)
1315 int sa_len, copy_len;
1317 if ((errno = -ldp_init ()))
1320 if (addr && len && ep)
1322 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1323 switch (addr->sa_family)
1326 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1327 if (*len > sizeof (struct sockaddr_in))
1328 *len = sizeof (struct sockaddr_in);
1329 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1330 copy_len = *len - sa_len;
1332 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1337 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1338 if (*len > sizeof (struct sockaddr_in6))
1339 *len = sizeof (struct sockaddr_in6);
1340 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1341 copy_len = *len - sa_len;
1343 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1344 __in6_u.__u6_addr8, ep->ip, copy_len);
1357 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1360 const char *func_str;
1361 u32 sid = ldp_sid_from_fd (fd);
1363 if ((errno = -ldp_init ()))
1366 if (sid != INVALID_SESSION_ID)
1369 u8 addr_buf[sizeof (struct in6_addr)];
1370 u32 size = sizeof (ep);
1373 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1376 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1378 getpid (), fd, fd, func_str, sid, sid, addr, len);
1380 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1381 if (rv != VPPCOM_OK)
1388 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1389 if (rv != VPPCOM_OK)
1398 func_str = "libc_getsockname";
1401 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1403 getpid (), fd, fd, func_str, addr, len);
1405 rv = libc_getsockname (fd, addr, len);
1412 int errno_val = errno;
1414 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1415 "rv %d, errno = %d", getpid (), fd, fd,
1416 func_str, rv, errno_val);
1420 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1421 getpid (), fd, fd, rv, rv);
1427 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1430 u32 sid = ldp_sid_from_fd (fd);
1432 if ((errno = -ldp_init ()))
1437 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1438 getpid (), fd, fd, len);
1444 if (sid != INVALID_SESSION_ID)
1448 switch (addr->sa_family)
1451 if (len != sizeof (struct sockaddr_in))
1454 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1455 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1460 ep.is_ip4 = VPPCOM_IS_IP4;
1461 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1462 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1466 if (len != sizeof (struct sockaddr_in6))
1469 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1470 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1475 ep.is_ip4 = VPPCOM_IS_IP6;
1476 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1477 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1481 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1482 "Unsupported address family %u!",
1483 getpid (), fd, fd, sid, sid, addr->sa_family);
1484 errno = EAFNOSUPPORT;
1488 LDBG (0, "fd %d (0x%x): calling vppcom_session_connect(): sid %u (0x%x)"
1489 " addr %p len %u", fd, fd, sid, sid, addr, len);
1491 rv = vppcom_session_connect (sid, &ep);
1492 if (rv != VPPCOM_OK)
1500 LDBG (0, "fd %d (0x%x): calling libc_connect(): addr %p, len %u",
1503 rv = libc_connect (fd, addr, len);
1507 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
1512 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1515 const char *func_str;
1516 u32 sid = ldp_sid_from_fd (fd);
1518 if ((errno = -ldp_init ()))
1521 if (sid != INVALID_SESSION_ID)
1524 u8 addr_buf[sizeof (struct in6_addr)];
1525 u32 size = sizeof (ep);
1528 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1531 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1533 getpid (), fd, fd, func_str, sid, sid, addr, len);
1535 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1536 if (rv != VPPCOM_OK)
1543 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1544 if (rv != VPPCOM_OK)
1553 func_str = "libc_getpeername";
1556 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1558 getpid (), fd, fd, func_str, addr, len);
1560 rv = libc_getpeername (fd, addr, len);
1567 int errno_val = errno;
1569 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1570 "rv %d, errno = %d", getpid (), fd, fd,
1571 func_str, rv, errno_val);
1575 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1576 getpid (), fd, fd, rv, rv);
1582 send (int fd, const void *buf, size_t n, int flags)
1585 const char *func_str;
1586 u32 sid = ldp_sid_from_fd (fd);
1588 if ((errno = -ldp_init ()))
1591 if (sid != INVALID_SESSION_ID)
1594 func_str = "vppcom_session_sendto";
1597 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1598 "buf %p, n %u, flags 0x%x",
1599 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1601 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1602 if (size < VPPCOM_OK)
1610 func_str = "libc_send";
1613 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1614 "buf %p, n %u, flags 0x%x",
1615 getpid (), fd, fd, func_str, buf, n, flags);
1617 size = libc_send (fd, buf, n, flags);
1624 int errno_val = errno;
1626 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1627 "rv %d, errno = %d", getpid (), fd, fd,
1628 func_str, size, errno_val);
1632 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1633 getpid (), fd, fd, size, size);
1639 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1641 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1643 const char *func_str;
1644 u32 sid = ldp_sid_from_fd (out_fd);
1646 if ((errno = -ldp_init ()))
1649 if (sid != INVALID_SESSION_ID)
1652 ssize_t results = 0;
1653 size_t n_bytes_left = len;
1654 size_t bytes_to_read;
1658 u32 flags, flags_len = sizeof (flags);
1660 func_str = "vppcom_session_attr[GET_FLAGS]";
1661 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1663 if (PREDICT_FALSE (rv != VPPCOM_OK))
1665 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1666 "sid %u (0x%x), returned %d (%s)!", getpid (),
1667 out_fd, out_fd, func_str, sid, sid, rv,
1668 vppcom_retval_str (rv));
1670 vec_reset_length (ldpw->io_buffer);
1678 off_t off = lseek (in_fd, *offset, SEEK_SET);
1679 if (PREDICT_FALSE (off == -1))
1683 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1684 "SEEK_SET failed: in_fd %d, offset %p, "
1685 "*offset %ld, rv %ld, errno %d", getpid (),
1686 out_fd, out_fd, in_fd, offset, *offset, off,
1693 ASSERT (off == *offset);
1698 func_str = "vppcom_session_attr[GET_NWRITE]";
1699 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1703 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1704 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1705 sid, sid, size, vppcom_retval_str (size));
1706 vec_reset_length (ldpw->io_buffer);
1712 bytes_to_read = size;
1715 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1716 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1717 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1720 if (bytes_to_read == 0)
1722 if (flags & O_NONBLOCK)
1727 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1729 getpid (), out_fd, out_fd, sid, sid);
1737 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1738 vec_validate (ldpw->io_buffer, bytes_to_read);
1739 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
1742 func_str = "libc_read";
1744 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1745 "io_buffer %p, bytes_to_read %lu, rv %d, "
1746 "errno %d", getpid (), out_fd, out_fd, func_str,
1747 in_fd, ldpw->io_buffer, bytes_to_read, nbytes,
1753 vec_reset_length (ldpw->io_buffer);
1759 func_str = "vppcom_session_write";
1762 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1763 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1764 out_fd, out_fd, func_str, sid, sid, ldpw->io_buffer, nbytes,
1765 results, n_bytes_left);
1767 size = vppcom_session_write (sid, ldpw->io_buffer, nbytes);
1770 if (size == VPPCOM_EAGAIN)
1772 if (flags & O_NONBLOCK)
1778 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1779 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1789 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1790 "sid %u, io_buffer %p, nbytes %u "
1792 getpid (), out_fd, out_fd, func_str,
1793 sid, ldpw->io_buffer, nbytes,
1794 size, vppcom_retval_str (size));
1798 vec_reset_length (ldpw->io_buffer);
1807 ASSERT (n_bytes_left >= nbytes);
1808 n_bytes_left = n_bytes_left - nbytes;
1810 while (n_bytes_left > 0);
1813 vec_reset_length (ldpw->io_buffer);
1816 off_t off = lseek (in_fd, *offset, SEEK_SET);
1817 if (PREDICT_FALSE (off == -1))
1821 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1822 "in_fd %d, offset %p, *offset %ld, "
1823 "rv %ld, errno %d", getpid (), in_fd,
1824 offset, *offset, off, errno_val);
1830 ASSERT (off == *offset);
1831 *offset += results + 1;
1843 func_str = "libc_send";
1846 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1847 "in_fd %d, offset %p, len %u",
1848 getpid (), out_fd, out_fd, func_str,
1849 in_fd, offset, len);
1851 size = libc_sendfile (out_fd, in_fd, offset, len);
1859 int errno_val = errno;
1861 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1862 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1863 func_str, size, errno_val);
1867 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1868 getpid (), out_fd, out_fd, size, size);
1874 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1876 return sendfile (out_fd, in_fd, offset, len);
1880 recv (int fd, void *buf, size_t n, int flags)
1885 if ((errno = -ldp_init ()))
1888 sid = ldp_sid_from_fd (fd);
1889 if (sid != INVALID_SESSION_ID)
1891 LDBG (2, "fd %d (0x%x): calling vcl recvfrom: sid %u (0x%x), buf %p,"
1892 " n %u, flags 0x%x", fd, fd, sid, sid, buf, n, flags);
1894 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1900 LDBG (2, "fd %d (0x%x): calling libc_recvfrom(): buf %p, n %u, "
1901 "flags 0x%x", fd, fd, buf, n, flags);
1903 size = libc_recv (fd, buf, n, flags);
1910 sendto (int fd, const void *buf, size_t n, int flags,
1911 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1914 const char *func_str = __func__;
1915 u32 sid = ldp_sid_from_fd (fd);
1917 if ((errno = -ldp_init ()))
1920 if (sid != INVALID_SESSION_ID)
1922 vppcom_endpt_t *ep = 0;
1928 switch (addr->sa_family)
1931 ep->is_ip4 = VPPCOM_IS_IP4;
1933 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1935 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1939 ep->is_ip4 = VPPCOM_IS_IP6;
1941 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1943 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1947 errno = EAFNOSUPPORT;
1953 func_str = "vppcom_session_sendto";
1956 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1957 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
1958 getpid (), fd, fd, func_str, sid, sid, buf, n,
1961 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
1970 func_str = "libc_sendto";
1973 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1974 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
1975 getpid (), fd, fd, func_str, buf, n, flags,
1978 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1986 int errno_val = errno;
1988 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1989 "rv %d, errno = %d", getpid (), fd, fd,
1990 func_str, size, errno_val);
1994 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1995 getpid (), fd, fd, size, size);
2001 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2002 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2005 const char *func_str;
2006 u32 sid = ldp_sid_from_fd (fd);
2008 if ((errno = -ldp_init ()))
2011 if (sid != INVALID_SESSION_ID)
2014 u8 src_addr[sizeof (struct sockaddr_in6)];
2016 func_str = "vppcom_session_recvfrom";
2019 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2020 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2021 getpid (), fd, fd, func_str, sid, sid, buf, n,
2026 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2029 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2032 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2042 func_str = "libc_recvfrom";
2045 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2046 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2047 getpid (), fd, fd, func_str, buf, n, flags,
2050 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2057 int errno_val = errno;
2059 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2060 "rv %d, errno = %d", getpid (), fd, fd,
2061 func_str, size, errno_val);
2065 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2066 getpid (), fd, fd, size, size);
2072 sendmsg (int fd, const struct msghdr * message, int flags)
2075 const char *func_str;
2076 u32 sid = ldp_sid_from_fd (fd);
2078 if ((errno = -ldp_init ()))
2081 if (sid != INVALID_SESSION_ID)
2083 func_str = __func__;
2085 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2091 func_str = "libc_sendmsg";
2094 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2095 "message %p, flags 0x%x",
2096 getpid (), fd, fd, func_str, message, flags);
2098 size = libc_sendmsg (fd, message, flags);
2105 int errno_val = errno;
2107 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2108 "rv %d, errno = %d", getpid (), fd, fd,
2109 func_str, size, errno_val);
2113 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2114 getpid (), fd, fd, size, size);
2121 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2124 const char *func_str;
2125 u32 sid = ldp_sid_from_fd (fd);
2127 if ((errno = -ldp_init ()))
2130 if (sid != INVALID_SESSION_ID)
2132 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2138 func_str = "libc_sendmmsg";
2141 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2142 "vmessages %p, vlen %u, flags 0x%x",
2143 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2145 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2152 int errno_val = errno;
2154 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2155 "rv %d, errno = %d", getpid (), fd, fd,
2156 func_str, size, errno_val);
2160 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2161 getpid (), fd, fd, size, size);
2168 recvmsg (int fd, struct msghdr * message, int flags)
2171 const char *func_str;
2172 u32 sid = ldp_sid_from_fd (fd);
2174 if ((errno = -ldp_init ()))
2177 if (sid != INVALID_SESSION_ID)
2179 func_str = __func__;
2181 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2187 func_str = "libc_recvmsg";
2190 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2191 "message %p, flags 0x%x",
2192 getpid (), fd, fd, func_str, message, flags);
2194 size = libc_recvmsg (fd, message, flags);
2201 int errno_val = errno;
2203 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2204 "rv %d, errno = %d", getpid (), fd, fd,
2205 func_str, size, errno_val);
2209 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2210 getpid (), fd, fd, size, size);
2217 recvmmsg (int fd, struct mmsghdr *vmessages,
2218 unsigned int vlen, int flags, struct timespec *tmo)
2221 const char *func_str;
2222 u32 sid = ldp_sid_from_fd (fd);
2224 if ((errno = -ldp_init ()))
2227 if (sid != INVALID_SESSION_ID)
2229 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2235 func_str = "libc_recvmmsg";
2238 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2239 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2240 getpid (), fd, fd, func_str, vmessages, vlen,
2243 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2250 int errno_val = errno;
2252 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2253 "rv %d, errno = %d", getpid (), fd, fd,
2254 func_str, size, errno_val);
2258 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2259 getpid (), fd, fd, size, size);
2266 getsockopt (int fd, int level, int optname,
2267 void *__restrict optval, socklen_t * __restrict optlen)
2270 const char *func_str = __func__;
2271 u32 sid = ldp_sid_from_fd (fd);
2272 u32 buflen = optlen ? (u32) * optlen : 0;
2274 if ((errno = -ldp_init ()))
2277 if (sid != INVALID_SESSION_ID)
2287 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2289 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2291 getpid (), fd, fd, func_str, sid, sid);
2292 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2296 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2298 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2300 getpid (), fd, fd, func_str, sid, sid);
2301 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2305 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2307 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2309 getpid (), fd, fd, func_str, sid, sid);
2310 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2314 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2316 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2317 "sid %u (0x%x), SOL_TCP",
2318 getpid (), fd, fd, func_str, sid, sid);
2319 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2323 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2326 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2327 "SOL_TCP, TCP_INFO, optval %p, "
2328 "optlen %d: #LDP-NOP#",
2329 getpid (), fd, fd, sid, sid,
2331 memset (optval, 0, *optlen);
2339 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2340 "sid %u (0x%x), SOL_TCP, "
2341 "optname %d unsupported!",
2342 getpid (), fd, fd, func_str, sid, sid, optname);
2350 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2352 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2354 getpid (), fd, fd, func_str, sid, sid);
2355 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2360 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2361 "sid %u (0x%x), SOL_IPV6, "
2362 "optname %d unsupported!",
2363 getpid (), fd, fd, func_str, sid, sid, optname);
2371 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2373 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2375 getpid (), fd, fd, func_str, sid, sid);
2376 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2380 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2382 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2384 getpid (), fd, fd, func_str, sid, sid);
2385 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2389 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2391 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2393 getpid (), fd, fd, func_str, sid, sid);
2394 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2396 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2399 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2401 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2402 "sid %u (0x%x), optlen %d",
2403 getpid (), fd, fd, func_str, sid, sid, buflen);
2404 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2408 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2410 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2411 "sid %u (0x%x), optlen %d",
2412 getpid (), fd, fd, func_str, sid, sid, buflen);
2413 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2417 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2419 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2421 getpid (), fd, fd, func_str, sid, sid);
2422 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2426 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2428 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2430 getpid (), fd, fd, func_str, sid, sid);
2431 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2435 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2437 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2439 getpid (), fd, fd, func_str, sid, sid);
2440 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2445 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2446 "sid %u (0x%x), SOL_SOCKET, "
2447 "optname %d unsupported!",
2448 getpid (), fd, fd, func_str, sid, sid, optname);
2456 if (rv != VPPCOM_OK)
2464 func_str = "libc_getsockopt";
2467 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2468 "optname %d, optval %p, optlen %d",
2469 getpid (), fd, fd, func_str, level, optname,
2472 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2479 int errno_val = errno;
2481 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2482 "rv %d, errno = %d", getpid (), fd, fd,
2483 func_str, rv, errno_val);
2487 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2488 getpid (), fd, fd, rv, rv);
2494 setsockopt (int fd, int level, int optname,
2495 const void *optval, socklen_t optlen)
2498 const char *func_str = __func__;
2499 u32 sid = ldp_sid_from_fd (fd);
2501 if ((errno = -ldp_init ()))
2504 if (sid != INVALID_SESSION_ID)
2514 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2516 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2518 getpid (), fd, fd, func_str, sid, sid);
2519 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2520 (void *) optval, &optlen);
2523 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2525 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2527 getpid (), fd, fd, func_str, sid, sid);
2528 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2529 (void *) optval, &optlen);
2532 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2534 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2536 getpid (), fd, fd, func_str, sid, sid);
2537 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2538 (void *) optval, &optlen);
2541 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2543 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2544 "sid %u (0x%x), SOL_TCP",
2545 getpid (), fd, fd, func_str, sid, sid);
2546 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2547 (void *) optval, &optlen);
2551 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2552 "sid %u (0x%x), SOL_TCP, "
2553 "optname %d unsupported!",
2554 getpid (), fd, fd, func_str, sid, sid, optname);
2562 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2564 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2566 getpid (), fd, fd, func_str, sid, sid);
2567 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2568 (void *) optval, &optlen);
2572 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2573 "sid %u (0x%x), SOL_IPV6, "
2574 "optname %d unsupported!",
2575 getpid (), fd, fd, func_str, sid, sid, optname);
2583 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
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_KEEPALIVE,
2589 (void *) optval, &optlen);
2592 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
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_REUSEADDR,
2598 (void *) optval, &optlen);
2601 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2603 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2605 getpid (), fd, fd, func_str, sid, sid);
2606 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2607 (void *) optval, &optlen);
2611 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2612 "sid %u (0x%x), SOL_SOCKET, "
2613 "optname %d unsupported!",
2614 getpid (), fd, fd, func_str, sid, sid, optname);
2622 if (rv != VPPCOM_OK)
2630 func_str = "libc_setsockopt";
2633 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2634 "optname %d, optval %p, optlen %d",
2635 getpid (), fd, fd, func_str, level, optname,
2638 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2645 int errno_val = errno;
2647 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2648 "rv %d, errno = %d", getpid (), fd, fd,
2649 func_str, rv, errno_val);
2653 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2654 getpid (), fd, fd, rv, rv);
2660 listen (int fd, int n)
2663 u32 sid = ldp_sid_from_fd (fd);
2665 if ((errno = -ldp_init ()))
2668 if (sid != INVALID_SESSION_ID)
2670 LDBG (0, "fd %d (0x%x): calling vppcom_session_listen():"
2671 " sid %u (0x%x), n %d", fd, fd, sid, sid, n);
2673 rv = vppcom_session_listen (sid, n);
2674 if (rv != VPPCOM_OK)
2682 LDBG (0, "fd %d (0x%x): calling libc_listen(): n %d", fd, fd, n);
2684 rv = libc_listen (fd, n);
2687 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
2692 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2693 socklen_t * __restrict addr_len, int flags)
2696 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2699 if ((errno = -ldp_init ()))
2702 if (listen_sid != INVALID_SESSION_ID)
2705 u8 src_addr[sizeof (struct sockaddr_in6)];
2706 memset (&ep, 0, sizeof (ep));
2709 LDBG (0, "listen fd %d (0x%x): calling vppcom_session_accept:"
2710 " listen sid %u (0x%x), ep %p, flags 0x%x", listen_fd,
2711 listen_fd, listen_sid, listen_sid, ep, flags);
2713 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2716 errno = -accept_sid;
2721 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2722 if (rv != VPPCOM_OK)
2724 (void) vppcom_session_close ((u32) accept_sid);
2730 rv = ldp_fd_alloc ((u32) accept_sid);
2733 (void) vppcom_session_close ((u32) accept_sid);
2742 LDBG (0, "listen fd %d (0x%x): calling libc_accept4(): "
2743 "addr %p, addr_len %p, flags 0x%x", listen_fd,
2744 listen_fd, addr, addr_len, flags);
2746 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2749 LDBG (1, "listen fd %d (0x%x): returning %d (0x%x)", listen_fd, listen_fd,
2756 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2759 return ldp_accept4 (fd, addr, addr_len, flags);
2763 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2765 return ldp_accept4 (fd, addr, addr_len, 0);
2769 shutdown (int fd, int how)
2773 if ((errno = -ldp_init ()))
2776 if (ldp_fd_is_sid (fd))
2778 u32 fd_index = fd - ldp->sid_bit_val;
2779 ldp_fd_entry_t *fde;
2781 fde = ldp_fd_entry_get_w_lock (fd_index);
2784 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
2790 fde->flags |= LDP_F_SHUT_RD;
2791 else if (how == SHUT_WR)
2792 fde->flags |= LDP_F_SHUT_WR;
2793 else if (how == SHUT_RDWR)
2794 fde->flags |= (LDP_F_SHUT_RD | LDP_F_SHUT_WR);
2796 if ((fde->flags & LDP_F_SHUT_RD) && (fde->flags & LDP_F_SHUT_WR))
2799 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
2800 LDBG (0, "fd %d (0x%x): calling vcl shutdown: how %d", fd, fd, how);
2804 LDBG (1, "fd %d (0x%x): calling libc_shutdown: how %d", fd, fd, how);
2805 rv = libc_shutdown (fd, how);
2812 epoll_create1 (int flags)
2814 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2815 const char *func_str;
2818 if ((errno = -ldp_init ()))
2821 if (ldp->vcl_needs_real_epoll)
2823 rv = libc_epoll_create1 (flags);
2824 ldp->vcl_needs_real_epoll = 0;
2825 ldpw->vcl_mq_epfd = rv;
2826 LDBG (0, "created vcl epfd %u", rv);
2829 func_str = "vppcom_epoll_create";
2831 LDBG (1, "calling %s()", func_str);
2833 rv = vppcom_epoll_create ();
2835 if (PREDICT_FALSE (rv < 0))
2841 rv = ldp_fd_alloc ((u32) rv);
2847 int errno_val = errno;
2849 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2850 "rv %d, errno = %d",
2851 getpid (), func_str, rv, errno_val);
2855 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2861 epoll_create (int size)
2863 return epoll_create1 (0);
2867 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2869 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
2870 const char *func_str;
2873 if ((errno = -ldp_init ()))
2876 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
2878 /* The LDP epoll_create1 always creates VCL epfd's.
2879 * The app should never have a kernel base epoll fd unless it
2880 * was acquired outside of the LD_PRELOAD process context.
2881 * In any case, if we get one, punt it to libc_epoll_ctl.
2883 func_str = "libc_epoll_ctl";
2885 LDBG (1, "epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
2886 " event %p", epfd, epfd, func_str, op, fd, fd, event);
2888 rv = libc_epoll_ctl (epfd, op, fd, event);
2892 sid = ldp_sid_from_fd (fd);
2894 LDBG (0, "epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
2895 epfd, epfd, vep_idx, vep_idx, sid, sid);
2897 if (sid != INVALID_SESSION_ID)
2899 func_str = "vppcom_epoll_ctl";
2901 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
2902 " op %d, sid %u (0x%x), event %p", epfd, epfd,
2903 func_str, vep_idx, vep_idx, sid, sid, event);
2905 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
2906 if (rv != VPPCOM_OK)
2915 u32 size = sizeof (epfd);
2917 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
2918 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
2920 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): %s() "
2921 "returned libc_epfd %d (0x%x)", epfd, epfd,
2922 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
2926 func_str = "libc_epoll_create1";
2928 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): "
2929 "calling %s(): EPOLL_CLOEXEC", epfd, epfd,
2930 vep_idx, vep_idx, func_str);
2932 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2939 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
2940 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
2941 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
2942 epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
2945 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
2954 else if (PREDICT_FALSE (libc_epfd < 0))
2961 func_str = "libc_epoll_ctl";
2963 LDBG (1, "epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
2964 "op %d, fd %d (0x%x), event %p", epfd, epfd, func_str,
2965 libc_epfd, libc_epfd, op, fd, fd, event);
2967 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
2975 int errno_val = errno;
2977 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2978 "rv %d, errno = %d", getpid (), fd, fd,
2979 func_str, rv, errno_val);
2983 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2984 getpid (), fd, fd, rv, rv);
2990 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2991 int timeout, const sigset_t * sigmask)
2993 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
2994 double time_to_wait = (double) 0, time_out, now = 0;
2995 u32 vep_idx = ldp_sid_from_fd (epfd);
2996 int libc_epfd, rv = 0;
2998 if ((errno = -ldp_init ()))
3001 if (PREDICT_FALSE (!events || (timeout < -1)))
3007 if (epfd == ldpw->vcl_mq_epfd)
3008 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3010 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3012 LDBG (0, "epfd %d (0x%x): bad vep_idx %d (0x%x)!", epfd, epfd, vep_idx,
3018 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
3019 time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
3021 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3022 if (PREDICT_FALSE (libc_epfd < 0))
3029 LDBG (2, "epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3030 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3031 epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3032 maxevents, timeout, sigmask, time_to_wait, time_out);
3035 if (!ldpw->epoll_wait_vcl)
3037 LDBG (3, "epfd %d (0x%x): calling vcl_epoll_wait: vep_idx %d (0x%x)"
3038 " events %p, maxevents %d", epfd, epfd, vep_idx, vep_idx,
3041 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3044 ldpw->epoll_wait_vcl = 1;
3055 ldpw->epoll_wait_vcl = 0;
3059 LDBG (3, "epfd %d (0x%x): calling libc_epoll_wait: libc_epfd %d "
3060 "(0x%x), events %p, maxevents %d, sigmask %p", epfd, epfd,
3061 libc_epfd, libc_epfd, events, maxevents, sigmask);
3063 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
3069 now = clib_time_now (&ldpw->clib_time);
3071 while (now < time_out);
3078 epoll_pwait (int epfd, struct epoll_event *events,
3079 int maxevents, int timeout, const sigset_t * sigmask)
3081 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3085 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3087 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3091 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3093 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
3094 const char *func_str = __func__;
3095 int rv, i, n_revents = 0;
3098 double wait_for_time;
3100 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
3103 wait_for_time = (f64) timeout / 1000;
3107 for (i = 0; i < nfds; i++)
3112 LDBG (3, "fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3113 i, fds[i].fd, fds[i].fd, fds[i].events, fds[i].revents);
3115 sid = ldp_sid_from_fd (fds[i].fd);
3116 if (sid != INVALID_SESSION_ID)
3118 fds[i].fd = -fds[i].fd;
3119 vec_add2 (ldpw->vcl_poll, vp, 1);
3122 vp->events = fds[i].events;
3123 #ifdef __USE_XOPEN2K
3124 if (fds[i].events & POLLRDNORM)
3125 vp->events |= POLLIN;
3126 if (fds[i].events & POLLWRNORM)
3127 vp->events |= POLLOUT;
3129 vp->revents = fds[i].revents;
3133 vec_add1 (ldpw->libc_poll, fds[i]);
3134 vec_add1 (ldpw->libc_poll_idxs, i);
3140 if (vec_len (ldpw->vcl_poll))
3142 func_str = "vppcom_poll";
3144 LDBG (3, "calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3145 "n_libc_fds %u", func_str, ldpw->vcl_poll,
3146 vec_len (ldpw->vcl_poll), vec_len (ldpw->vcl_poll),
3147 vec_len (ldpw->libc_poll));
3149 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
3160 if (vec_len (ldpw->libc_poll))
3162 func_str = "libc_poll";
3164 LDBG (3, "calling %s(): fds %p, nfds %u: n_sids %u",
3165 fds, nfds, vec_len (ldpw->vcl_poll));
3167 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
3180 while ((wait_for_time == -1) ||
3181 (clib_time_now (&ldpw->clib_time) < wait_for_time));
3185 vec_foreach (vp, ldpw->vcl_poll)
3187 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3188 fds[vp->fds_ndx].revents = vp->revents;
3189 #ifdef __USE_XOPEN2K
3190 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3191 (fds[vp->fds_ndx].events & POLLRDNORM))
3192 fds[vp->fds_ndx].revents |= POLLRDNORM;
3193 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3194 (fds[vp->fds_ndx].events & POLLWRNORM))
3195 fds[vp->fds_ndx].revents |= POLLWRNORM;
3198 vec_reset_length (ldpw->vcl_poll);
3200 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
3202 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
3204 vec_reset_length (ldpw->libc_poll_idxs);
3205 vec_reset_length (ldpw->libc_poll);
3211 int errno_val = errno;
3213 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3214 "rv %d, errno = %d", getpid (),
3215 func_str, rv, errno_val);
3220 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3221 "n_libc_fds %d", getpid (), rv, rv,
3222 vec_len (ldpw->vcl_poll), vec_len (ldpw->libc_poll));
3224 for (i = 0; i < nfds; i++)
3229 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3230 ".events = 0x%x, .revents = 0x%x",
3231 getpid (), i, fds[i].fd, fds[i].fd,
3232 fds[i].events, fds[i].revents);
3243 ppoll (struct pollfd *fds, nfds_t nfds,
3244 const struct timespec *timeout, const sigset_t * sigmask)
3246 if ((errno = -ldp_init ()))
3249 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3257 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3259 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3262 * This function is called when the library is loaded
3265 ldp_constructor (void)
3267 swrap_constructor ();
3268 if (ldp_init () != 0)
3269 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3271 else if (LDP_DEBUG > 0)
3272 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3276 * This function is called when the library is unloaded
3279 ldp_destructor (void)
3281 swrap_destructor ();
3285 /* Don't use clib_warning() here because that calls writev()
3286 * which will call ldp_init().
3289 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3290 __func__, __LINE__, getpid ());
3295 * fd.io coding-style-patch-verification: ON
3298 * eval: (c-set-style "gnu")