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>
33 #define HAVE_CONSTRUCTOR_ATTRIBUTE
34 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
35 #define CONSTRUCTOR_ATTRIBUTE \
36 __attribute__ ((constructor))
38 #define CONSTRUCTOR_ATTRIBUTE
39 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
41 #define HAVE_DESTRUCTOR_ATTRIBUTE
42 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
43 #define DESTRUCTOR_ATTRIBUTE \
44 __attribute__ ((destructor))
46 #define DESTRUCTOR_ATTRIBUTE
52 char app_name[LDP_APP_NAME_MAX];
57 clib_time_t clib_time;
58 clib_bitmap_t *rd_bitmap;
59 clib_bitmap_t *wr_bitmap;
60 clib_bitmap_t *ex_bitmap;
61 clib_bitmap_t *sid_rd_bitmap;
62 clib_bitmap_t *sid_wr_bitmap;
63 clib_bitmap_t *sid_ex_bitmap;
64 clib_bitmap_t *libc_rd_bitmap;
65 clib_bitmap_t *libc_wr_bitmap;
66 clib_bitmap_t *libc_ex_bitmap;
68 struct pollfd *libc_poll;
72 u8 vcl_needs_real_epoll; /*< vcl needs next epoll_create to
77 #define LDP_DEBUG ldp->debug
79 #define LDBG(_lvl, _fmt, _args...) \
80 if (ldp->debug > _lvl) \
81 clib_warning (_fmt, ##_args)
83 static ldp_main_t ldp_main = {
84 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
85 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
86 .debug = LDP_DEBUG_INIT,
89 static ldp_main_t *ldp = &ldp_main;
92 * RETURN: 0 on success or -1 on error.
95 ldp_set_app_name (char *app_name)
97 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
98 "ldp-%d-%s", getpid (), app_name);
100 if (rv >= LDP_APP_NAME_MAX)
101 app_name[LDP_APP_NAME_MAX - 1] = 0;
107 if (ldp->app_name[0] == '\0')
108 ldp_set_app_name ("app");
110 return ldp->app_name;
114 ldp_fd_from_sid (u32 sid)
116 if (PREDICT_FALSE (sid >= ldp->sid_bit_val))
119 return (sid | ldp->sid_bit_val);
123 ldp_fd_is_sid (int fd)
125 return ((u32) fd & ldp->sid_bit_val) ? 1 : 0;
129 ldp_sid_from_fd (int fd)
131 return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) :
140 if (PREDICT_TRUE (ldp->init))
144 ldp->vcl_needs_real_epoll = 1;
145 rv = vppcom_app_create (ldp_get_app_name ());
148 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
149 " failed! rv = %d (%s)\n",
150 getpid (), rv, vppcom_retval_str (rv));
154 ldp->vcl_needs_real_epoll = 0;
156 char *env_var_str = getenv (LDP_ENV_DEBUG);
160 if (sscanf (env_var_str, "%u", &tmp) != 1)
161 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
162 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
167 LDBG (0, "LDP<%d>: configured LDP debug level (%u) from env var "
168 LDP_ENV_DEBUG "!", getpid (), ldp->debug);
172 env_var_str = getenv (LDP_ENV_APP_NAME);
175 ldp_set_app_name (env_var_str);
176 LDBG (0, "LDP<%d>: configured LDP app name (%s) from the env var "
177 LDP_ENV_APP_NAME "!", getpid (), ldp->app_name);
180 env_var_str = getenv (LDP_ENV_SID_BIT);
184 if (sscanf (env_var_str, "%u", &sb) != 1)
186 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
187 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
188 "value %d (0x%x)", getpid (), env_var_str,
189 ldp->sid_bit_val, ldp->sid_bit_val);
191 else if (sb < LDP_SID_BIT_MIN)
193 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
194 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
196 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
197 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
198 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
199 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
200 ldp->sid_bit_val, ldp->sid_bit_val);
202 else if (sb > LDP_SID_BIT_MAX)
204 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
205 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
207 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
208 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
209 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
210 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
211 ldp->sid_bit_val, ldp->sid_bit_val);
215 ldp->sid_bit_val = (1 << sb);
216 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
218 LDBG (0, "LDP<%d>: configured LDP sid bit (%u) from "
219 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", getpid (), sb,
220 ldp->sid_bit_val, ldp->sid_bit_val);
224 clib_time_init (&ldp->clib_time);
225 LDBG (0, "LDP<%d>: LDP initialization: done!", getpid ());
234 const char *func_str;
235 u32 sid = ldp_sid_from_fd (fd);
237 if ((errno = -ldp_init ()))
240 if (sid != INVALID_SESSION_ID)
244 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
245 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
248 func_str = "libc_close";
250 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
251 getpid (), fd, fd, func_str, epfd, epfd);
253 rv = libc_close (epfd);
256 u32 size = sizeof (epfd);
259 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
263 else if (PREDICT_FALSE (epfd < 0))
270 func_str = "vppcom_session_close";
272 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
273 getpid (), fd, fd, func_str, sid, sid);
275 rv = vppcom_session_close (sid);
284 func_str = "libc_close";
286 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s()", getpid (), fd, fd,
289 rv = libc_close (fd);
297 int errno_val = errno;
299 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
300 "rv %d, errno = %d", getpid (), fd, fd,
301 func_str, rv, errno_val);
305 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
306 getpid (), fd, fd, rv, rv);
312 read (int fd, void *buf, size_t nbytes)
315 const char *func_str;
316 u32 sid = ldp_sid_from_fd (fd);
318 if ((errno = -ldp_init ()))
321 if (sid != INVALID_SESSION_ID)
323 func_str = "vppcom_session_read";
326 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
327 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
328 fd, fd, func_str, sid, sid, buf, nbytes);
330 size = vppcom_session_read (sid, buf, nbytes);
339 func_str = "libc_read";
342 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
343 "buf %p, nbytes %u", getpid (),
344 fd, fd, func_str, buf, nbytes);
346 size = libc_read (fd, buf, nbytes);
353 int errno_val = errno;
355 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
356 "rv %d, errno = %d", getpid (), fd, fd,
357 func_str, size, errno_val);
361 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
362 getpid (), fd, fd, size, size);
368 readv (int fd, const struct iovec * iov, int iovcnt)
370 const char *func_str;
372 u32 sid = ldp_sid_from_fd (fd);
373 int rv = 0, i, total = 0;
375 if ((errno = -ldp_init ()))
378 if (sid != INVALID_SESSION_ID)
380 func_str = "vppcom_session_read";
383 for (i = 0; i < iovcnt; ++i)
386 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
387 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
388 getpid (), fd, fd, func_str, i, sid, sid,
391 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
397 if (rv < iov[i].iov_len)
400 clib_warning ("LDP<%d>: fd %d (0x%x): "
401 "rv (%d) < iov[%d].iov_len (%d)",
402 getpid (), fd, fd, rv, i,
409 while ((rv >= 0) && (total == 0));
421 func_str = "libc_readv";
424 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
425 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
427 size = libc_readv (fd, iov, iovcnt);
434 int errno_val = errno;
436 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
437 "rv %d, errno = %d", getpid (), fd, fd,
438 func_str, size, errno_val);
442 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
443 getpid (), fd, fd, size, size);
449 write (int fd, const void *buf, size_t nbytes)
451 const char *func_str;
453 u32 sid = ldp_sid_from_fd (fd);
455 if ((errno = -ldp_init ()))
458 if (sid != INVALID_SESSION_ID)
460 func_str = "vppcom_session_write";
463 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
464 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
465 fd, fd, func_str, sid, sid, buf, nbytes);
467 size = vppcom_session_write (sid, (void *) buf, nbytes);
476 func_str = "libc_write";
479 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
480 "buf %p, nbytes %u", getpid (),
481 fd, fd, func_str, buf, nbytes);
483 size = libc_write (fd, buf, nbytes);
490 int errno_val = errno;
492 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
493 "rv %d, errno = %d", getpid (), fd, fd,
494 func_str, size, errno_val);
498 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
499 getpid (), fd, fd, size, size);
505 writev (int fd, const struct iovec * iov, int iovcnt)
507 const char *func_str;
508 ssize_t size = 0, total = 0;
509 u32 sid = ldp_sid_from_fd (fd);
513 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
516 if ((errno = -ldp_init ()))
519 if (sid != INVALID_SESSION_ID)
521 func_str = "vppcom_session_write";
524 for (i = 0; i < iovcnt; ++i)
527 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
528 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
529 __func__, __LINE__, getpid (), fd, fd, func_str,
530 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
532 rv = vppcom_session_write (sid, iov[i].iov_base,
539 if (rv < iov[i].iov_len)
542 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
543 "rv (%d) < iov[%d].iov_len (%ld)",
544 __func__, __LINE__, getpid (), fd, fd,
545 rv, i, iov[i].iov_len);
551 while ((rv >= 0) && (total == 0));
563 func_str = "libc_writev";
566 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
567 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
568 fd, fd, func_str, iov, iovcnt);
570 size = libc_writev (fd, iov, iovcnt);
577 int errno_val = errno;
580 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
581 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
582 fd, func_str, size, errno_val);
586 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
587 __func__, __LINE__, getpid (), fd, fd, size);
593 fcntl (int fd, int cmd, ...)
595 const char *func_str = __func__;
598 u32 sid = ldp_sid_from_fd (fd);
600 if ((errno = -ldp_init ()))
604 if (sid != INVALID_SESSION_ID)
606 int flags = va_arg (ap, int);
609 size = sizeof (flags);
614 func_str = "vppcom_session_attr[SET_FLAGS]";
616 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
617 "sid %u (0x%x) flags %d (0x%x), size %d",
618 getpid (), fd, fd, func_str, sid, sid,
622 vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
626 func_str = "vppcom_session_attr[GET_FLAGS]";
629 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
630 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
631 sid, flags, flags, size);
634 vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
638 clib_warning ("LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): "
639 "%s() returned flags %d (0x%x)",
640 getpid (), fd, fd, cmd, func_str, flags, flags);
657 func_str = "libc_vfcntl";
660 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
661 getpid (), fd, fd, func_str, cmd);
663 rv = libc_vfcntl (fd, cmd, ap);
672 int errno_val = errno;
674 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
675 "rv %d, errno = %d", getpid (), fd, fd,
676 func_str, rv, errno_val);
680 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
681 getpid (), fd, fd, rv, rv);
687 ioctl (int fd, unsigned long int cmd, ...)
689 const char *func_str;
692 u32 sid = ldp_sid_from_fd (fd);
694 if ((errno = -ldp_init ()))
698 if (sid != INVALID_SESSION_ID)
700 func_str = "vppcom_session_attr[GET_NREAD]";
707 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
708 getpid (), fd, fd, func_str, sid, sid);
710 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
715 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
716 u32 size = sizeof (flags);
718 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
719 * non-blocking, the flags should be read here and merged
723 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
724 "sid %u (0x%x), flags %d (0x%x), size %d",
725 getpid (), fd, fd, func_str, sid, sid,
728 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
745 func_str = "libc_vioctl";
748 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
749 getpid (), fd, fd, func_str, cmd);
751 rv = libc_vioctl (fd, cmd, ap);
758 int errno_val = errno;
760 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
761 "rv %d, errno = %d", getpid (), fd, fd,
762 func_str, rv, errno_val);
766 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
767 getpid (), fd, fd, rv, rv);
774 ldp_pselect (int nfds, fd_set * __restrict readfds,
775 fd_set * __restrict writefds,
776 fd_set * __restrict exceptfds,
777 const struct timespec *__restrict timeout,
778 const __sigset_t * __restrict sigmask)
781 char *func_str = "##";
784 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
785 u32 minbits = clib_max (nfds, BITS (uword));
796 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
797 (f64) 0 : (f64) timeout->tv_sec +
798 (f64) timeout->tv_nsec / (f64) 1000000000;
800 /* select as fine grained sleep */
804 clib_warning ("LDP<%d>: sleeping for %.02f seconds",
805 getpid (), time_out);
807 time_out += clib_time_now (&ldp->clib_time);
808 while (clib_time_now (&ldp->clib_time) < time_out)
822 if (nfds <= ldp->sid_bit_val)
824 func_str = "libc_pselect";
828 ("LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
829 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
830 readfds, writefds, exceptfds, timeout, sigmask);
832 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
837 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
839 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
840 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
841 ldp->sid_bit_val, ldp->sid_bit_val,
842 FD_SETSIZE / 2, FD_SETSIZE / 2);
847 sid_bits = libc_bits = 0;
850 clib_bitmap_validate (ldp->sid_rd_bitmap, minbits);
851 clib_bitmap_validate (ldp->libc_rd_bitmap, minbits);
852 clib_bitmap_validate (ldp->rd_bitmap, minbits);
853 clib_memcpy_fast (ldp->rd_bitmap, readfds,
854 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
858 clib_bitmap_foreach (fd, ldp->rd_bitmap,
860 sid = ldp_sid_from_fd (fd);
862 clib_warning ("LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
863 getpid (), fd, fd, sid, sid);
864 if (sid == INVALID_SESSION_ID)
865 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
867 clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1);
871 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
872 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
874 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
875 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
878 clib_warning ("LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
879 "libc_bits_set %d, libc_bits %d", getpid (),
880 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
884 clib_bitmap_validate (ldp->sid_wr_bitmap, minbits);
885 clib_bitmap_validate (ldp->libc_wr_bitmap, minbits);
886 clib_bitmap_validate (ldp->wr_bitmap, minbits);
887 clib_memcpy_fast (ldp->wr_bitmap, writefds,
888 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
892 clib_bitmap_foreach (fd, ldp->wr_bitmap,
894 sid = ldp_sid_from_fd (fd);
896 clib_warning ("LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
897 getpid (), fd, fd, sid, sid);
898 if (sid == INVALID_SESSION_ID)
899 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
901 clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1);
905 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
906 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
908 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
909 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
912 clib_warning ("LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
913 "libc_bits_set %d, libc_bits %d", getpid (),
914 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
918 clib_bitmap_validate (ldp->sid_ex_bitmap, minbits);
919 clib_bitmap_validate (ldp->libc_ex_bitmap, minbits);
920 clib_bitmap_validate (ldp->ex_bitmap, minbits);
921 clib_memcpy_fast (ldp->ex_bitmap, exceptfds,
922 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
926 clib_bitmap_foreach (fd, ldp->ex_bitmap,
928 sid = ldp_sid_from_fd (fd);
930 clib_warning ("LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
931 getpid (), fd, fd, sid, sid);
932 if (sid == INVALID_SESSION_ID)
933 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
935 clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1);
939 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
940 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
942 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
943 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
946 clib_warning ("LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
947 "libc_bits_set %d, libc_bits %d", getpid (),
948 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
951 if (PREDICT_FALSE (!sid_bits && !libc_bits))
962 if (!ldp->select_vcl)
964 func_str = "vppcom_select";
967 clib_memcpy_fast (ldp->rd_bitmap, ldp->sid_rd_bitmap,
968 vec_len (ldp->rd_bitmap) *
969 sizeof (clib_bitmap_t));
971 clib_memcpy_fast (ldp->wr_bitmap, ldp->sid_wr_bitmap,
972 vec_len (ldp->wr_bitmap) *
973 sizeof (clib_bitmap_t));
975 clib_memcpy_fast (ldp->ex_bitmap, ldp->sid_ex_bitmap,
976 vec_len (ldp->ex_bitmap) *
977 sizeof (clib_bitmap_t));
979 rv = vppcom_select (sid_bits,
980 readfds ? ldp->rd_bitmap : NULL,
981 writefds ? ldp->wr_bitmap : NULL,
982 exceptfds ? ldp->ex_bitmap : NULL, 0);
993 clib_bitmap_foreach (sid, ldp->rd_bitmap,
995 fd = ldp_fd_from_sid (sid);
996 if (PREDICT_FALSE (fd < 0))
1002 FD_SET (fd, readfds);
1009 clib_bitmap_foreach (sid, ldp->wr_bitmap,
1011 fd = ldp_fd_from_sid (sid);
1012 if (PREDICT_FALSE (fd < 0))
1018 FD_SET (fd, writefds);
1025 clib_bitmap_foreach (sid, ldp->ex_bitmap,
1027 fd = ldp_fd_from_sid (sid);
1028 if (PREDICT_FALSE (fd < 0))
1034 FD_SET (fd, exceptfds);
1038 ldp->select_vcl = 1;
1043 ldp->select_vcl = 0;
1047 struct timespec tspec;
1049 func_str = "libc_pselect";
1052 clib_memcpy_fast (readfds, ldp->libc_rd_bitmap,
1053 vec_len (ldp->rd_bitmap) *
1054 sizeof (clib_bitmap_t));
1056 clib_memcpy_fast (writefds, ldp->libc_wr_bitmap,
1057 vec_len (ldp->wr_bitmap) *
1058 sizeof (clib_bitmap_t));
1060 clib_memcpy_fast (exceptfds, ldp->libc_ex_bitmap,
1061 vec_len (ldp->ex_bitmap) *
1062 sizeof (clib_bitmap_t));
1063 tspec.tv_sec = tspec.tv_nsec = 0;
1064 rv = libc_pselect (libc_bits,
1065 readfds ? readfds : NULL,
1066 writefds ? writefds : NULL,
1067 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1072 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1076 /* TBD: set timeout to amount of time left */
1077 vec_reset_length (ldp->rd_bitmap);
1078 vec_reset_length (ldp->sid_rd_bitmap);
1079 vec_reset_length (ldp->libc_rd_bitmap);
1080 vec_reset_length (ldp->wr_bitmap);
1081 vec_reset_length (ldp->sid_wr_bitmap);
1082 vec_reset_length (ldp->libc_wr_bitmap);
1083 vec_reset_length (ldp->ex_bitmap);
1084 vec_reset_length (ldp->sid_ex_bitmap);
1085 vec_reset_length (ldp->libc_ex_bitmap);
1091 int errno_val = errno;
1093 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1094 "rv %d, errno = %d", getpid (),
1095 func_str, rv, errno_val);
1099 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1105 select (int nfds, fd_set * __restrict readfds,
1106 fd_set * __restrict writefds,
1107 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1109 struct timespec tspec;
1113 tspec.tv_sec = timeout->tv_sec;
1114 tspec.tv_nsec = timeout->tv_usec * 1000;
1116 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1117 timeout ? &tspec : NULL, NULL);
1120 #ifdef __USE_XOPEN2K
1122 pselect (int nfds, fd_set * __restrict readfds,
1123 fd_set * __restrict writefds,
1124 fd_set * __restrict exceptfds,
1125 const struct timespec *__restrict timeout,
1126 const __sigset_t * __restrict sigmask)
1128 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1133 socket (int domain, int type, int protocol)
1135 const char *func_str;
1137 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1138 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1140 if ((errno = -ldp_init ()))
1143 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1144 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1147 u8 proto = ((sock_type == SOCK_DGRAM) ?
1148 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1150 func_str = "vppcom_session_create";
1152 LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u",
1153 getpid (), func_str, proto, vppcom_proto_str (proto),
1156 sid = vppcom_session_create (proto, is_nonblocking);
1164 func_str = "ldp_fd_from_sid";
1165 rv = ldp_fd_from_sid (sid);
1168 (void) vppcom_session_close (sid);
1176 func_str = "libc_socket";
1178 LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str);
1180 rv = libc_socket (domain, type, protocol);
1187 int errno_val = errno;
1189 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1190 "rv %d, errno = %d",
1191 getpid (), func_str, rv, errno_val);
1195 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1201 * Create two new sockets, of type TYPE in domain DOMAIN and using
1202 * protocol PROTOCOL, which are connected to each other, and put file
1203 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1204 * one will be chosen automatically.
1205 * Returns 0 on success, -1 for errors.
1208 socketpair (int domain, int type, int protocol, int fds[2])
1210 const char *func_str;
1212 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1214 if ((errno = -ldp_init ()))
1217 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1218 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1220 func_str = __func__;
1222 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1228 func_str = "libc_socket";
1231 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1233 rv = libc_socket (domain, type, protocol);
1240 int errno_val = errno;
1242 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1243 "rv %d, errno = %d",
1244 getpid (), func_str, rv, errno_val);
1248 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1254 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1257 const char *func_str;
1258 u32 sid = ldp_sid_from_fd (fd);
1260 if ((errno = -ldp_init ()))
1263 if (sid != INVALID_SESSION_ID)
1267 func_str = "vppcom_session_bind";
1269 switch (addr->sa_family)
1272 if (len != sizeof (struct sockaddr_in))
1275 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1276 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1281 ep.is_ip4 = VPPCOM_IS_IP4;
1282 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1283 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1287 if (len != sizeof (struct sockaddr_in6))
1290 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1291 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1296 ep.is_ip4 = VPPCOM_IS_IP6;
1297 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1298 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1302 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1303 "Unsupported address family %u!",
1304 getpid (), fd, fd, sid, sid, addr->sa_family);
1305 errno = EAFNOSUPPORT;
1310 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1312 getpid (), fd, fd, func_str, sid, sid, addr, len);
1314 rv = vppcom_session_bind (sid, &ep);
1315 if (rv != VPPCOM_OK)
1323 func_str = "libc_bind";
1326 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1328 getpid (), fd, fd, func_str, addr, len);
1330 rv = libc_bind (fd, addr, len);
1338 int errno_val = errno;
1340 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1341 "rv %d, errno = %d", getpid (), fd, fd,
1342 func_str, rv, errno_val);
1346 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1347 getpid (), fd, fd, rv, rv);
1353 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1354 vppcom_endpt_t * ep)
1357 int sa_len, copy_len;
1359 if ((errno = -ldp_init ()))
1362 if (addr && len && ep)
1364 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1365 switch (addr->sa_family)
1368 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1369 if (*len > sizeof (struct sockaddr_in))
1370 *len = sizeof (struct sockaddr_in);
1371 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1372 copy_len = *len - sa_len;
1374 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1379 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1380 if (*len > sizeof (struct sockaddr_in6))
1381 *len = sizeof (struct sockaddr_in6);
1382 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1383 copy_len = *len - sa_len;
1385 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1386 __in6_u.__u6_addr8, ep->ip, copy_len);
1399 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1402 const char *func_str;
1403 u32 sid = ldp_sid_from_fd (fd);
1405 if ((errno = -ldp_init ()))
1408 if (sid != INVALID_SESSION_ID)
1411 u8 addr_buf[sizeof (struct in6_addr)];
1412 u32 size = sizeof (ep);
1415 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1418 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1420 getpid (), fd, fd, func_str, sid, sid, addr, len);
1422 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1423 if (rv != VPPCOM_OK)
1430 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1431 if (rv != VPPCOM_OK)
1440 func_str = "libc_getsockname";
1443 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1445 getpid (), fd, fd, func_str, addr, len);
1447 rv = libc_getsockname (fd, addr, len);
1454 int errno_val = errno;
1456 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1457 "rv %d, errno = %d", getpid (), fd, fd,
1458 func_str, rv, errno_val);
1462 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1463 getpid (), fd, fd, rv, rv);
1469 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1472 const char *func_str = __func__;
1473 u32 sid = ldp_sid_from_fd (fd);
1475 if ((errno = -ldp_init ()))
1480 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1481 getpid (), fd, fd, len);
1487 if (sid != INVALID_SESSION_ID)
1491 func_str = "vppcom_session_connect";
1493 switch (addr->sa_family)
1496 if (len != sizeof (struct sockaddr_in))
1499 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1500 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1505 ep.is_ip4 = VPPCOM_IS_IP4;
1506 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1507 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1511 if (len != sizeof (struct sockaddr_in6))
1514 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1515 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1520 ep.is_ip4 = VPPCOM_IS_IP6;
1521 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1522 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1526 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1527 "Unsupported address family %u!",
1528 getpid (), fd, fd, sid, sid, addr->sa_family);
1529 errno = EAFNOSUPPORT;
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_connect (sid, &ep);
1539 if (rv != VPPCOM_OK)
1547 func_str = "libc_connect";
1550 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1552 getpid (), fd, fd, func_str, addr, len);
1554 rv = libc_connect (fd, addr, len);
1562 int errno_val = errno;
1564 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1565 "rv %d, errno = %d", getpid (), fd, fd,
1566 func_str, rv, errno_val);
1570 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1571 getpid (), fd, fd, rv, rv);
1577 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1580 const char *func_str;
1581 u32 sid = ldp_sid_from_fd (fd);
1583 if ((errno = -ldp_init ()))
1586 if (sid != INVALID_SESSION_ID)
1589 u8 addr_buf[sizeof (struct in6_addr)];
1590 u32 size = sizeof (ep);
1593 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1596 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1598 getpid (), fd, fd, func_str, sid, sid, addr, len);
1600 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1601 if (rv != VPPCOM_OK)
1608 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1609 if (rv != VPPCOM_OK)
1618 func_str = "libc_getpeername";
1621 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1623 getpid (), fd, fd, func_str, addr, len);
1625 rv = libc_getpeername (fd, addr, len);
1632 int errno_val = errno;
1634 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1635 "rv %d, errno = %d", getpid (), fd, fd,
1636 func_str, rv, errno_val);
1640 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1641 getpid (), fd, fd, rv, rv);
1647 send (int fd, const void *buf, size_t n, int flags)
1650 const char *func_str;
1651 u32 sid = ldp_sid_from_fd (fd);
1653 if ((errno = -ldp_init ()))
1656 if (sid != INVALID_SESSION_ID)
1659 func_str = "vppcom_session_sendto";
1662 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1663 "buf %p, n %u, flags 0x%x",
1664 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1666 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1667 if (size < VPPCOM_OK)
1675 func_str = "libc_send";
1678 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1679 "buf %p, n %u, flags 0x%x",
1680 getpid (), fd, fd, func_str, buf, n, flags);
1682 size = libc_send (fd, buf, n, flags);
1689 int errno_val = errno;
1691 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1692 "rv %d, errno = %d", getpid (), fd, fd,
1693 func_str, size, errno_val);
1697 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1698 getpid (), fd, fd, size, size);
1704 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1707 const char *func_str;
1708 u32 sid = ldp_sid_from_fd (out_fd);
1710 if ((errno = -ldp_init ()))
1713 if (sid != INVALID_SESSION_ID)
1716 ssize_t results = 0;
1717 size_t n_bytes_left = len;
1718 size_t bytes_to_read;
1722 u32 flags, flags_len = sizeof (flags);
1724 func_str = "vppcom_session_attr[GET_FLAGS]";
1725 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1727 if (PREDICT_FALSE (rv != VPPCOM_OK))
1729 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1730 "sid %u (0x%x), returned %d (%s)!", getpid (),
1731 out_fd, out_fd, func_str, sid, sid, rv,
1732 vppcom_retval_str (rv));
1734 vec_reset_length (ldp->io_buffer);
1742 off_t off = lseek (in_fd, *offset, SEEK_SET);
1743 if (PREDICT_FALSE (off == -1))
1747 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1748 "SEEK_SET failed: in_fd %d, offset %p, "
1749 "*offset %ld, rv %ld, errno %d", getpid (),
1750 out_fd, out_fd, in_fd, offset, *offset, off,
1757 ASSERT (off == *offset);
1762 func_str = "vppcom_session_attr[GET_NWRITE]";
1763 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1767 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1768 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1769 sid, sid, size, vppcom_retval_str (size));
1770 vec_reset_length (ldp->io_buffer);
1776 bytes_to_read = size;
1779 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1780 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1781 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1784 if (bytes_to_read == 0)
1786 if (flags & O_NONBLOCK)
1791 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1793 getpid (), out_fd, out_fd, sid, sid);
1801 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1802 vec_validate (ldp->io_buffer, bytes_to_read);
1803 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1806 func_str = "libc_read";
1808 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1809 "io_buffer %p, bytes_to_read %lu, rv %d, "
1810 "errno %d", getpid (), out_fd, out_fd, func_str,
1811 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1817 vec_reset_length (ldp->io_buffer);
1823 func_str = "vppcom_session_write";
1826 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1827 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1828 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1829 results, n_bytes_left);
1831 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1834 if (size == VPPCOM_EAGAIN)
1836 if (flags & O_NONBLOCK)
1842 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1843 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1853 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1854 "sid %u, io_buffer %p, nbytes %u "
1856 getpid (), out_fd, out_fd, func_str,
1857 sid, ldp->io_buffer, nbytes,
1858 size, vppcom_retval_str (size));
1862 vec_reset_length (ldp->io_buffer);
1871 ASSERT (n_bytes_left >= nbytes);
1872 n_bytes_left = n_bytes_left - nbytes;
1874 while (n_bytes_left > 0);
1877 vec_reset_length (ldp->io_buffer);
1880 off_t off = lseek (in_fd, *offset, SEEK_SET);
1881 if (PREDICT_FALSE (off == -1))
1885 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1886 "in_fd %d, offset %p, *offset %ld, "
1887 "rv %ld, errno %d", getpid (), in_fd,
1888 offset, *offset, off, errno_val);
1894 ASSERT (off == *offset);
1895 *offset += results + 1;
1907 func_str = "libc_send";
1910 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1911 "in_fd %d, offset %p, len %u",
1912 getpid (), out_fd, out_fd, func_str,
1913 in_fd, offset, len);
1915 size = libc_sendfile (out_fd, in_fd, offset, len);
1923 int errno_val = errno;
1925 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1926 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1927 func_str, size, errno_val);
1931 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1932 getpid (), out_fd, out_fd, size, size);
1938 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1940 return sendfile (out_fd, in_fd, offset, len);
1944 recv (int fd, void *buf, size_t n, int flags)
1947 const char *func_str;
1948 u32 sid = ldp_sid_from_fd (fd);
1950 if ((errno = -ldp_init ()))
1953 if (sid != INVALID_SESSION_ID)
1955 func_str = "vppcom_session_recvfrom";
1958 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1959 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1960 fd, fd, func_str, sid, sid, buf, n, flags);
1962 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1971 func_str = "libc_recv";
1974 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1975 "buf %p, n %u, flags 0x%x", getpid (),
1976 fd, fd, func_str, buf, n, flags);
1978 size = libc_recv (fd, buf, n, flags);
1985 int errno_val = errno;
1987 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1988 "rv %d, errno = %d", getpid (), fd, fd,
1989 func_str, size, errno_val);
1993 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1994 getpid (), fd, fd, size, size);
2000 sendto (int fd, const void *buf, size_t n, int flags,
2001 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2004 const char *func_str = __func__;
2005 u32 sid = ldp_sid_from_fd (fd);
2007 if ((errno = -ldp_init ()))
2010 if (sid != INVALID_SESSION_ID)
2012 vppcom_endpt_t *ep = 0;
2018 switch (addr->sa_family)
2021 ep->is_ip4 = VPPCOM_IS_IP4;
2023 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2025 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2029 ep->is_ip4 = VPPCOM_IS_IP6;
2031 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2033 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2037 errno = EAFNOSUPPORT;
2043 func_str = "vppcom_session_sendto";
2046 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2047 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2048 getpid (), fd, fd, func_str, sid, sid, buf, n,
2051 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2060 func_str = "libc_sendto";
2063 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2064 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2065 getpid (), fd, fd, func_str, buf, n, flags,
2068 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2076 int errno_val = errno;
2078 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2079 "rv %d, errno = %d", getpid (), fd, fd,
2080 func_str, size, errno_val);
2084 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2085 getpid (), fd, fd, size, size);
2091 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2092 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2095 const char *func_str;
2096 u32 sid = ldp_sid_from_fd (fd);
2098 if ((errno = -ldp_init ()))
2101 if (sid != INVALID_SESSION_ID)
2104 u8 src_addr[sizeof (struct sockaddr_in6)];
2106 func_str = "vppcom_session_recvfrom";
2109 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2110 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2111 getpid (), fd, fd, func_str, sid, sid, buf, n,
2116 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2119 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2122 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2132 func_str = "libc_recvfrom";
2135 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2136 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2137 getpid (), fd, fd, func_str, buf, n, flags,
2140 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2147 int errno_val = errno;
2149 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2150 "rv %d, errno = %d", getpid (), fd, fd,
2151 func_str, size, errno_val);
2155 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2156 getpid (), fd, fd, size, size);
2162 sendmsg (int fd, const struct msghdr * message, int flags)
2165 const char *func_str;
2166 u32 sid = ldp_sid_from_fd (fd);
2168 if ((errno = -ldp_init ()))
2171 if (sid != INVALID_SESSION_ID)
2173 func_str = __func__;
2175 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2181 func_str = "libc_sendmsg";
2184 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2185 "message %p, flags 0x%x",
2186 getpid (), fd, fd, func_str, message, flags);
2188 size = libc_sendmsg (fd, message, flags);
2195 int errno_val = errno;
2197 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2198 "rv %d, errno = %d", getpid (), fd, fd,
2199 func_str, size, errno_val);
2203 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2204 getpid (), fd, fd, size, size);
2211 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2214 const char *func_str;
2215 u32 sid = ldp_sid_from_fd (fd);
2217 if ((errno = -ldp_init ()))
2220 if (sid != INVALID_SESSION_ID)
2222 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2228 func_str = "libc_sendmmsg";
2231 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2232 "vmessages %p, vlen %u, flags 0x%x",
2233 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2235 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2242 int errno_val = errno;
2244 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2245 "rv %d, errno = %d", getpid (), fd, fd,
2246 func_str, size, errno_val);
2250 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2251 getpid (), fd, fd, size, size);
2258 recvmsg (int fd, struct msghdr * message, int flags)
2261 const char *func_str;
2262 u32 sid = ldp_sid_from_fd (fd);
2264 if ((errno = -ldp_init ()))
2267 if (sid != INVALID_SESSION_ID)
2269 func_str = __func__;
2271 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2277 func_str = "libc_recvmsg";
2280 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2281 "message %p, flags 0x%x",
2282 getpid (), fd, fd, func_str, message, flags);
2284 size = libc_recvmsg (fd, message, flags);
2291 int errno_val = errno;
2293 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2294 "rv %d, errno = %d", getpid (), fd, fd,
2295 func_str, size, errno_val);
2299 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2300 getpid (), fd, fd, size, size);
2307 recvmmsg (int fd, struct mmsghdr *vmessages,
2308 unsigned int vlen, int flags, struct timespec *tmo)
2311 const char *func_str;
2312 u32 sid = ldp_sid_from_fd (fd);
2314 if ((errno = -ldp_init ()))
2317 if (sid != INVALID_SESSION_ID)
2319 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2325 func_str = "libc_recvmmsg";
2328 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2329 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2330 getpid (), fd, fd, func_str, vmessages, vlen,
2333 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2340 int errno_val = errno;
2342 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2343 "rv %d, errno = %d", getpid (), fd, fd,
2344 func_str, size, errno_val);
2348 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2349 getpid (), fd, fd, size, size);
2356 getsockopt (int fd, int level, int optname,
2357 void *__restrict optval, socklen_t * __restrict optlen)
2360 const char *func_str = __func__;
2361 u32 sid = ldp_sid_from_fd (fd);
2362 u32 buflen = optlen ? (u32) * optlen : 0;
2364 if ((errno = -ldp_init ()))
2367 if (sid != INVALID_SESSION_ID)
2377 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2379 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2381 getpid (), fd, fd, func_str, sid, sid);
2382 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2386 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2388 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2390 getpid (), fd, fd, func_str, sid, sid);
2391 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2395 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2397 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2399 getpid (), fd, fd, func_str, sid, sid);
2400 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2404 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2406 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2407 "sid %u (0x%x), SOL_TCP",
2408 getpid (), fd, fd, func_str, sid, sid);
2409 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2413 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2416 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2417 "SOL_TCP, TCP_INFO, optval %p, "
2418 "optlen %d: #LDP-NOP#",
2419 getpid (), fd, fd, sid, sid,
2421 memset (optval, 0, *optlen);
2429 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2430 "sid %u (0x%x), SOL_TCP, "
2431 "optname %d unsupported!",
2432 getpid (), fd, fd, func_str, sid, sid, optname);
2440 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2442 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2444 getpid (), fd, fd, func_str, sid, sid);
2445 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2450 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2451 "sid %u (0x%x), SOL_IPV6, "
2452 "optname %d unsupported!",
2453 getpid (), fd, fd, func_str, sid, sid, optname);
2461 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2463 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2465 getpid (), fd, fd, func_str, sid, sid);
2466 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2470 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2472 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2474 getpid (), fd, fd, func_str, sid, sid);
2475 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2479 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2481 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2483 getpid (), fd, fd, func_str, sid, sid);
2484 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2486 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2489 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2491 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2492 "sid %u (0x%x), optlen %d",
2493 getpid (), fd, fd, func_str, sid, sid, buflen);
2494 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2498 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2500 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2501 "sid %u (0x%x), optlen %d",
2502 getpid (), fd, fd, func_str, sid, sid, buflen);
2503 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2507 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2509 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2511 getpid (), fd, fd, func_str, sid, sid);
2512 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2516 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2518 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2520 getpid (), fd, fd, func_str, sid, sid);
2521 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2525 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2527 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2529 getpid (), fd, fd, func_str, sid, sid);
2530 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2535 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2536 "sid %u (0x%x), SOL_SOCKET, "
2537 "optname %d unsupported!",
2538 getpid (), fd, fd, func_str, sid, sid, optname);
2546 if (rv != VPPCOM_OK)
2554 func_str = "libc_getsockopt";
2557 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2558 "optname %d, optval %p, optlen %d",
2559 getpid (), fd, fd, func_str, level, optname,
2562 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2569 int errno_val = errno;
2571 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2572 "rv %d, errno = %d", getpid (), fd, fd,
2573 func_str, rv, errno_val);
2577 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2578 getpid (), fd, fd, rv, rv);
2584 setsockopt (int fd, int level, int optname,
2585 const void *optval, socklen_t optlen)
2588 const char *func_str = __func__;
2589 u32 sid = ldp_sid_from_fd (fd);
2591 if ((errno = -ldp_init ()))
2594 if (sid != INVALID_SESSION_ID)
2604 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
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_TCP_NODELAY,
2610 (void *) optval, &optlen);
2613 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2615 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2617 getpid (), fd, fd, func_str, sid, sid);
2618 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2619 (void *) optval, &optlen);
2622 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2624 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2626 getpid (), fd, fd, func_str, sid, sid);
2627 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2628 (void *) optval, &optlen);
2631 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2633 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2634 "sid %u (0x%x), SOL_TCP",
2635 getpid (), fd, fd, func_str, sid, sid);
2636 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2637 (void *) optval, &optlen);
2641 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2642 "sid %u (0x%x), SOL_TCP, "
2643 "optname %d unsupported!",
2644 getpid (), fd, fd, func_str, sid, sid, optname);
2652 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2654 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2656 getpid (), fd, fd, func_str, sid, sid);
2657 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2658 (void *) optval, &optlen);
2662 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2663 "sid %u (0x%x), SOL_IPV6, "
2664 "optname %d unsupported!",
2665 getpid (), fd, fd, func_str, sid, sid, optname);
2673 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2675 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2677 getpid (), fd, fd, func_str, sid, sid);
2678 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2679 (void *) optval, &optlen);
2682 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2684 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2686 getpid (), fd, fd, func_str, sid, sid);
2687 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2688 (void *) optval, &optlen);
2691 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2693 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2695 getpid (), fd, fd, func_str, sid, sid);
2696 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2697 (void *) optval, &optlen);
2701 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2702 "sid %u (0x%x), SOL_SOCKET, "
2703 "optname %d unsupported!",
2704 getpid (), fd, fd, func_str, sid, sid, optname);
2712 if (rv != VPPCOM_OK)
2720 func_str = "libc_setsockopt";
2723 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2724 "optname %d, optval %p, optlen %d",
2725 getpid (), fd, fd, func_str, level, optname,
2728 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2735 int errno_val = errno;
2737 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2738 "rv %d, errno = %d", getpid (), fd, fd,
2739 func_str, rv, errno_val);
2743 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2744 getpid (), fd, fd, rv, rv);
2750 listen (int fd, int n)
2753 const char *func_str;
2754 u32 sid = ldp_sid_from_fd (fd);
2756 if ((errno = -ldp_init ()))
2759 if (sid != INVALID_SESSION_ID)
2761 func_str = "vppcom_session_listen";
2763 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2764 getpid (), fd, fd, func_str, sid, sid, n);
2766 rv = vppcom_session_listen (sid, n);
2767 if (rv != VPPCOM_OK)
2775 func_str = "libc_listen";
2777 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd,
2780 rv = libc_listen (fd, n);
2787 int errno_val = errno;
2789 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2790 "rv %d, errno = %d", getpid (), fd, fd,
2791 func_str, rv, errno_val);
2795 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2796 getpid (), fd, fd, rv, rv);
2802 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2803 socklen_t * __restrict addr_len, int flags)
2806 const char *func_str;
2807 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2810 if ((errno = -ldp_init ()))
2813 if (listen_sid != INVALID_SESSION_ID)
2816 u8 src_addr[sizeof (struct sockaddr_in6)];
2817 memset (&ep, 0, sizeof (ep));
2820 func_str = "vppcom_session_accept";
2823 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2824 "listen sid %u (0x%x), ep %p, flags 0x%x",
2825 getpid (), listen_fd, listen_fd, func_str,
2826 listen_sid, listen_sid, ep, flags);
2828 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2831 errno = -accept_sid;
2836 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2837 if (rv != VPPCOM_OK)
2839 (void) vppcom_session_close ((u32) accept_sid);
2845 func_str = "ldp_fd_from_sid";
2847 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2848 "accept sid %u (0x%x), ep %p, flags 0x%x",
2849 getpid (), listen_fd, listen_fd,
2850 func_str, accept_sid, accept_sid, ep, flags);
2851 rv = ldp_fd_from_sid ((u32) accept_sid);
2854 (void) vppcom_session_close ((u32) accept_sid);
2863 func_str = "libc_accept4";
2866 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2867 "addr %p, addr_len %p, flags 0x%x",
2868 getpid (), listen_fd, listen_fd, func_str,
2869 addr, addr_len, flags);
2871 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2878 int errno_val = errno;
2880 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2881 "rv %d, errno = %d", getpid (), listen_fd,
2882 listen_fd, func_str, rv, errno_val);
2886 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2887 getpid (), listen_fd, listen_fd, rv, rv);
2893 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2896 return ldp_accept4 (fd, addr, addr_len, flags);
2900 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2902 return ldp_accept4 (fd, addr, addr_len, 0);
2906 shutdown (int fd, int how)
2909 const char *func_str;
2910 u32 sid = ldp_sid_from_fd (fd);
2912 if ((errno = -ldp_init ()))
2915 if (sid != INVALID_SESSION_ID)
2917 func_str = "vppcom_session_close[TODO]";
2922 func_str = "libc_shutdown";
2925 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2926 getpid (), fd, fd, func_str, how);
2928 rv = libc_shutdown (fd, how);
2935 int errno_val = errno;
2937 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2938 "rv %d, errno = %d", getpid (), fd, fd,
2939 func_str, rv, errno_val);
2943 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2944 getpid (), fd, fd, rv, rv);
2950 epoll_create1 (int flags)
2952 const char *func_str;
2955 if ((errno = -ldp_init ()))
2958 if (ldp->vcl_needs_real_epoll)
2960 rv = libc_epoll_create1 (flags);
2961 ldp->vcl_needs_real_epoll = 0;
2962 ldp->vcl_mq_epfd = rv;
2963 LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv);
2966 func_str = "vppcom_epoll_create";
2968 LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str);
2970 rv = vppcom_epoll_create ();
2972 if (PREDICT_FALSE (rv < 0))
2978 rv = ldp_fd_from_sid ((u32) rv);
2984 int errno_val = errno;
2986 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2987 "rv %d, errno = %d",
2988 getpid (), func_str, rv, errno_val);
2992 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2998 epoll_create (int size)
3000 return epoll_create1 (0);
3004 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3006 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
3007 const char *func_str;
3010 if ((errno = -ldp_init ()))
3013 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3015 /* The LDP epoll_create1 always creates VCL epfd's.
3016 * The app should never have a kernel base epoll fd unless it
3017 * was acquired outside of the LD_PRELOAD process context.
3018 * In any case, if we get one, punt it to libc_epoll_ctl.
3020 func_str = "libc_epoll_ctl";
3022 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
3023 " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event);
3025 rv = libc_epoll_ctl (epfd, op, fd, event);
3029 sid = ldp_sid_from_fd (fd);
3031 LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
3032 getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid);
3034 if (sid != INVALID_SESSION_ID)
3036 func_str = "vppcom_epoll_ctl";
3038 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3039 " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd,
3040 func_str, vep_idx, vep_idx, sid, sid, event);
3042 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3043 if (rv != VPPCOM_OK)
3052 u32 size = sizeof (epfd);
3054 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3055 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
3057 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() "
3058 "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd,
3059 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
3063 func_str = "libc_epoll_create1";
3065 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3066 "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd,
3067 vep_idx, vep_idx, func_str);
3069 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3076 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3077 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3078 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3079 getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3082 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3091 else if (PREDICT_FALSE (libc_epfd < 0))
3098 func_str = "libc_epoll_ctl";
3100 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3101 "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str,
3102 libc_epfd, libc_epfd, op, fd, fd, event);
3104 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3112 int errno_val = errno;
3114 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3115 "rv %d, errno = %d", getpid (), fd, fd,
3116 func_str, rv, errno_val);
3120 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3121 getpid (), fd, fd, rv, rv);
3127 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3128 int timeout, const sigset_t * sigmask)
3130 double time_to_wait = (double) 0, time_out, now = 0;
3131 u32 vep_idx = ldp_sid_from_fd (epfd);
3132 int libc_epfd, rv = 0;
3133 const char *func_str;
3135 if ((errno = -ldp_init ()))
3138 if (PREDICT_FALSE (!events || (timeout < -1)))
3144 if (epfd == ldp->vcl_mq_epfd)
3145 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3147 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3149 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3150 getpid (), epfd, epfd, vep_idx, vep_idx);
3155 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3156 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3158 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3159 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3160 if (PREDICT_FALSE (libc_epfd < 0))
3167 LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3168 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3169 getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3170 maxevents, timeout, sigmask, time_to_wait, time_out);
3173 if (!ldp->epoll_wait_vcl)
3175 func_str = "vppcom_epoll_wait";
3177 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3178 " events %p, maxevents %d", getpid (), epfd, epfd, func_str,
3179 vep_idx, vep_idx, events, maxevents);
3181 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3184 ldp->epoll_wait_vcl = 1;
3195 ldp->epoll_wait_vcl = 0;
3199 func_str = "libc_epoll_pwait";
3201 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d "
3202 "(0x%x), events %p, maxevents %d, sigmask %p", getpid (),
3203 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3204 maxevents, sigmask);
3206 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3212 now = clib_time_now (&ldp->clib_time);
3214 while (now < time_out);
3223 int errno_val = errno;
3225 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3226 "rv %d, errno = %d", getpid (), epfd, epfd,
3227 func_str, rv, errno_val);
3231 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3232 getpid (), epfd, epfd, rv, rv);
3238 epoll_pwait (int epfd, struct epoll_event *events,
3239 int maxevents, int timeout, const sigset_t * sigmask)
3241 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3245 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3247 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3251 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3253 const char *func_str = __func__;
3254 int rv, i, n_revents = 0;
3257 double wait_for_time;
3259 LDBG (3, "LDP<%d>: fds %p, nfds %d, timeout %d", getpid (), fds, nfds,
3263 wait_for_time = (f64) timeout / 1000;
3267 for (i = 0; i < nfds; i++)
3272 LDBG (3, "LDP<%d>: fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3273 getpid (), i, fds[i].fd, fds[i].fd, fds[i].events,
3276 sid = ldp_sid_from_fd (fds[i].fd);
3277 if (sid != INVALID_SESSION_ID)
3279 fds[i].fd = -fds[i].fd;
3280 vec_add2 (ldp->vcl_poll, vp, 1);
3283 vp->events = fds[i].events;
3284 #ifdef __USE_XOPEN2K
3285 if (fds[i].events & POLLRDNORM)
3286 vp->events |= POLLIN;
3287 if (fds[i].events & POLLWRNORM)
3288 vp->events |= POLLOUT;
3290 vp->revents = fds[i].revents;
3294 vec_add1 (ldp->libc_poll, fds[i]);
3295 vec_add1 (ldp->libc_poll_idxs, i);
3301 if (vec_len (ldp->vcl_poll))
3303 func_str = "vppcom_poll";
3305 LDBG (3, "LDP<%d>: calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3306 "n_libc_fds %u", getpid (), func_str, ldp->vcl_poll,
3307 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3308 vec_len (ldp->libc_poll));
3310 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3321 if (vec_len (ldp->libc_poll))
3323 func_str = "libc_poll";
3325 LDBG (3, "LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3326 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3328 rv = libc_poll (ldp->libc_poll, vec_len (ldp->libc_poll), 0);
3341 while ((wait_for_time == -1) ||
3342 (clib_time_now (&ldp->clib_time) < wait_for_time));
3346 vec_foreach (vp, ldp->vcl_poll)
3348 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3349 fds[vp->fds_ndx].revents = vp->revents;
3350 #ifdef __USE_XOPEN2K
3351 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3352 (fds[vp->fds_ndx].events & POLLRDNORM))
3353 fds[vp->fds_ndx].revents |= POLLRDNORM;
3354 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3355 (fds[vp->fds_ndx].events & POLLWRNORM))
3356 fds[vp->fds_ndx].revents |= POLLWRNORM;
3359 vec_reset_length (ldp->vcl_poll);
3361 for (i = 0; i < vec_len (ldp->libc_poll); i++)
3363 fds[ldp->libc_poll_idxs[i]].revents = ldp->libc_poll[i].revents;
3365 vec_reset_length (ldp->libc_poll_idxs);
3366 vec_reset_length (ldp->libc_poll);
3372 int errno_val = errno;
3374 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3375 "rv %d, errno = %d", getpid (),
3376 func_str, rv, errno_val);
3381 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3382 "n_libc_fds %d", getpid (), rv, rv,
3383 vec_len (ldp->vcl_poll), vec_len (ldp->libc_poll));
3385 for (i = 0; i < nfds; i++)
3390 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3391 ".events = 0x%x, .revents = 0x%x",
3392 getpid (), i, fds[i].fd, fds[i].fd,
3393 fds[i].events, fds[i].revents);
3404 ppoll (struct pollfd *fds, nfds_t nfds,
3405 const struct timespec *timeout, const sigset_t * sigmask)
3407 if ((errno = -ldp_init ()))
3410 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3418 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3420 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3423 * This function is called when the library is loaded
3426 ldp_constructor (void)
3428 swrap_constructor ();
3429 if (ldp_init () != 0)
3430 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3432 else if (LDP_DEBUG > 0)
3433 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3437 * This function is called when the library is unloaded
3440 ldp_destructor (void)
3442 swrap_destructor ();
3445 vppcom_app_destroy ();
3449 /* Don't use clib_warning() here because that calls writev()
3450 * which will call ldp_init().
3453 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3454 __func__, __LINE__, getpid ());
3459 * fd.io coding-style-patch-verification: ON
3462 * eval: (c-set-style "gnu")