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 (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 (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 (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 (ldp->rd_bitmap, ldp->sid_rd_bitmap,
968 vec_len (ldp->rd_bitmap) *
969 sizeof (clib_bitmap_t));
971 clib_memcpy (ldp->wr_bitmap, ldp->sid_wr_bitmap,
972 vec_len (ldp->wr_bitmap) *
973 sizeof (clib_bitmap_t));
975 clib_memcpy (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 (readfds, ldp->libc_rd_bitmap,
1053 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
1055 clib_memcpy (writefds, ldp->libc_wr_bitmap,
1056 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
1058 clib_memcpy (exceptfds, ldp->libc_ex_bitmap,
1059 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
1060 tspec.tv_sec = tspec.tv_nsec = 0;
1061 rv = libc_pselect (libc_bits,
1062 readfds ? readfds : NULL,
1063 writefds ? writefds : NULL,
1064 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1069 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1073 /* TBD: set timeout to amount of time left */
1074 vec_reset_length (ldp->rd_bitmap);
1075 vec_reset_length (ldp->sid_rd_bitmap);
1076 vec_reset_length (ldp->libc_rd_bitmap);
1077 vec_reset_length (ldp->wr_bitmap);
1078 vec_reset_length (ldp->sid_wr_bitmap);
1079 vec_reset_length (ldp->libc_wr_bitmap);
1080 vec_reset_length (ldp->ex_bitmap);
1081 vec_reset_length (ldp->sid_ex_bitmap);
1082 vec_reset_length (ldp->libc_ex_bitmap);
1088 int errno_val = errno;
1090 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1091 "rv %d, errno = %d", getpid (),
1092 func_str, rv, errno_val);
1096 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1102 select (int nfds, fd_set * __restrict readfds,
1103 fd_set * __restrict writefds,
1104 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1106 struct timespec tspec;
1110 tspec.tv_sec = timeout->tv_sec;
1111 tspec.tv_nsec = timeout->tv_usec * 1000;
1113 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1114 timeout ? &tspec : NULL, NULL);
1117 #ifdef __USE_XOPEN2K
1119 pselect (int nfds, fd_set * __restrict readfds,
1120 fd_set * __restrict writefds,
1121 fd_set * __restrict exceptfds,
1122 const struct timespec *__restrict timeout,
1123 const __sigset_t * __restrict sigmask)
1125 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1130 socket (int domain, int type, int protocol)
1132 const char *func_str;
1134 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1135 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1137 if ((errno = -ldp_init ()))
1140 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1141 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1144 u8 proto = ((sock_type == SOCK_DGRAM) ?
1145 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1147 func_str = "vppcom_session_create";
1149 LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u",
1150 getpid (), func_str, proto, vppcom_proto_str (proto),
1153 sid = vppcom_session_create (proto, is_nonblocking);
1161 func_str = "ldp_fd_from_sid";
1162 rv = ldp_fd_from_sid (sid);
1165 (void) vppcom_session_close (sid);
1173 func_str = "libc_socket";
1175 LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str);
1177 rv = libc_socket (domain, type, protocol);
1184 int errno_val = errno;
1186 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1187 "rv %d, errno = %d",
1188 getpid (), func_str, rv, errno_val);
1192 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1198 * Create two new sockets, of type TYPE in domain DOMAIN and using
1199 * protocol PROTOCOL, which are connected to each other, and put file
1200 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1201 * one will be chosen automatically.
1202 * Returns 0 on success, -1 for errors.
1205 socketpair (int domain, int type, int protocol, int fds[2])
1207 const char *func_str;
1209 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1211 if ((errno = -ldp_init ()))
1214 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1215 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1217 func_str = __func__;
1219 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1225 func_str = "libc_socket";
1228 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1230 rv = libc_socket (domain, type, protocol);
1237 int errno_val = errno;
1239 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1240 "rv %d, errno = %d",
1241 getpid (), func_str, rv, errno_val);
1245 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1251 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1254 const char *func_str;
1255 u32 sid = ldp_sid_from_fd (fd);
1257 if ((errno = -ldp_init ()))
1260 if (sid != INVALID_SESSION_ID)
1264 func_str = "vppcom_session_bind";
1266 switch (addr->sa_family)
1269 if (len != sizeof (struct sockaddr_in))
1272 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1273 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1278 ep.is_ip4 = VPPCOM_IS_IP4;
1279 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1280 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1284 if (len != sizeof (struct sockaddr_in6))
1287 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1288 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1293 ep.is_ip4 = VPPCOM_IS_IP6;
1294 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1295 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1299 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1300 "Unsupported address family %u!",
1301 getpid (), fd, fd, sid, sid, addr->sa_family);
1302 errno = EAFNOSUPPORT;
1307 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1309 getpid (), fd, fd, func_str, sid, sid, addr, len);
1311 rv = vppcom_session_bind (sid, &ep);
1312 if (rv != VPPCOM_OK)
1320 func_str = "libc_bind";
1323 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1325 getpid (), fd, fd, func_str, addr, len);
1327 rv = libc_bind (fd, addr, len);
1335 int errno_val = errno;
1337 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1338 "rv %d, errno = %d", getpid (), fd, fd,
1339 func_str, rv, errno_val);
1343 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1344 getpid (), fd, fd, rv, rv);
1350 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1351 vppcom_endpt_t * ep)
1354 int sa_len, copy_len;
1356 if ((errno = -ldp_init ()))
1359 if (addr && len && ep)
1361 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1362 switch (addr->sa_family)
1365 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1366 if (*len > sizeof (struct sockaddr_in))
1367 *len = sizeof (struct sockaddr_in);
1368 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1369 copy_len = *len - sa_len;
1371 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1376 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1377 if (*len > sizeof (struct sockaddr_in6))
1378 *len = sizeof (struct sockaddr_in6);
1379 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1380 copy_len = *len - sa_len;
1382 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1383 __in6_u.__u6_addr8, ep->ip, copy_len);
1396 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1399 const char *func_str;
1400 u32 sid = ldp_sid_from_fd (fd);
1402 if ((errno = -ldp_init ()))
1405 if (sid != INVALID_SESSION_ID)
1408 u8 addr_buf[sizeof (struct in6_addr)];
1409 u32 size = sizeof (ep);
1412 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1415 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1417 getpid (), fd, fd, func_str, sid, sid, addr, len);
1419 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1420 if (rv != VPPCOM_OK)
1427 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1428 if (rv != VPPCOM_OK)
1437 func_str = "libc_getsockname";
1440 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1442 getpid (), fd, fd, func_str, addr, len);
1444 rv = libc_getsockname (fd, addr, len);
1451 int errno_val = errno;
1453 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1454 "rv %d, errno = %d", getpid (), fd, fd,
1455 func_str, rv, errno_val);
1459 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1460 getpid (), fd, fd, rv, rv);
1466 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1469 const char *func_str = __func__;
1470 u32 sid = ldp_sid_from_fd (fd);
1472 if ((errno = -ldp_init ()))
1477 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1478 getpid (), fd, fd, len);
1484 if (sid != INVALID_SESSION_ID)
1488 func_str = "vppcom_session_connect";
1490 switch (addr->sa_family)
1493 if (len != sizeof (struct sockaddr_in))
1496 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1497 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1502 ep.is_ip4 = VPPCOM_IS_IP4;
1503 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1504 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1508 if (len != sizeof (struct sockaddr_in6))
1511 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1512 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1517 ep.is_ip4 = VPPCOM_IS_IP6;
1518 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1519 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1523 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1524 "Unsupported address family %u!",
1525 getpid (), fd, fd, sid, sid, addr->sa_family);
1526 errno = EAFNOSUPPORT;
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_connect (sid, &ep);
1536 if (rv != VPPCOM_OK)
1544 func_str = "libc_connect";
1547 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1549 getpid (), fd, fd, func_str, addr, len);
1551 rv = libc_connect (fd, addr, len);
1559 int errno_val = errno;
1561 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1562 "rv %d, errno = %d", getpid (), fd, fd,
1563 func_str, rv, errno_val);
1567 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1568 getpid (), fd, fd, rv, rv);
1574 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1577 const char *func_str;
1578 u32 sid = ldp_sid_from_fd (fd);
1580 if ((errno = -ldp_init ()))
1583 if (sid != INVALID_SESSION_ID)
1586 u8 addr_buf[sizeof (struct in6_addr)];
1587 u32 size = sizeof (ep);
1590 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1593 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1595 getpid (), fd, fd, func_str, sid, sid, addr, len);
1597 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1598 if (rv != VPPCOM_OK)
1605 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1606 if (rv != VPPCOM_OK)
1615 func_str = "libc_getpeername";
1618 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1620 getpid (), fd, fd, func_str, addr, len);
1622 rv = libc_getpeername (fd, addr, len);
1629 int errno_val = errno;
1631 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1632 "rv %d, errno = %d", getpid (), fd, fd,
1633 func_str, rv, errno_val);
1637 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1638 getpid (), fd, fd, rv, rv);
1644 send (int fd, const void *buf, size_t n, int flags)
1647 const char *func_str;
1648 u32 sid = ldp_sid_from_fd (fd);
1650 if ((errno = -ldp_init ()))
1653 if (sid != INVALID_SESSION_ID)
1656 func_str = "vppcom_session_sendto";
1659 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1660 "buf %p, n %u, flags 0x%x",
1661 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1663 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1664 if (size < VPPCOM_OK)
1672 func_str = "libc_send";
1675 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1676 "buf %p, n %u, flags 0x%x",
1677 getpid (), fd, fd, func_str, buf, n, flags);
1679 size = libc_send (fd, buf, n, flags);
1686 int errno_val = errno;
1688 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1689 "rv %d, errno = %d", getpid (), fd, fd,
1690 func_str, size, errno_val);
1694 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1695 getpid (), fd, fd, size, size);
1701 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1704 const char *func_str;
1705 u32 sid = ldp_sid_from_fd (out_fd);
1707 if ((errno = -ldp_init ()))
1710 if (sid != INVALID_SESSION_ID)
1713 ssize_t results = 0;
1714 size_t n_bytes_left = len;
1715 size_t bytes_to_read;
1719 u32 flags, flags_len = sizeof (flags);
1721 func_str = "vppcom_session_attr[GET_FLAGS]";
1722 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1724 if (PREDICT_FALSE (rv != VPPCOM_OK))
1726 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1727 "sid %u (0x%x), returned %d (%s)!", getpid (),
1728 out_fd, out_fd, func_str, sid, sid, rv,
1729 vppcom_retval_str (rv));
1731 vec_reset_length (ldp->io_buffer);
1739 off_t off = lseek (in_fd, *offset, SEEK_SET);
1740 if (PREDICT_FALSE (off == -1))
1744 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1745 "SEEK_SET failed: in_fd %d, offset %p, "
1746 "*offset %ld, rv %ld, errno %d", getpid (),
1747 out_fd, out_fd, in_fd, offset, *offset, off,
1754 ASSERT (off == *offset);
1759 func_str = "vppcom_session_attr[GET_NWRITE]";
1760 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1764 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1765 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1766 sid, sid, size, vppcom_retval_str (size));
1767 vec_reset_length (ldp->io_buffer);
1773 bytes_to_read = size;
1776 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1777 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1778 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1781 if (bytes_to_read == 0)
1783 if (flags & O_NONBLOCK)
1788 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1790 getpid (), out_fd, out_fd, sid, sid);
1798 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1799 vec_validate (ldp->io_buffer, bytes_to_read);
1800 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1803 func_str = "libc_read";
1805 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1806 "io_buffer %p, bytes_to_read %lu, rv %d, "
1807 "errno %d", getpid (), out_fd, out_fd, func_str,
1808 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1814 vec_reset_length (ldp->io_buffer);
1820 func_str = "vppcom_session_write";
1823 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1824 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1825 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1826 results, n_bytes_left);
1828 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1831 if (size == VPPCOM_EAGAIN)
1833 if (flags & O_NONBLOCK)
1839 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1840 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1850 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1851 "sid %u, io_buffer %p, nbytes %u "
1853 getpid (), out_fd, out_fd, func_str,
1854 sid, ldp->io_buffer, nbytes,
1855 size, vppcom_retval_str (size));
1859 vec_reset_length (ldp->io_buffer);
1868 ASSERT (n_bytes_left >= nbytes);
1869 n_bytes_left = n_bytes_left - nbytes;
1871 while (n_bytes_left > 0);
1874 vec_reset_length (ldp->io_buffer);
1877 off_t off = lseek (in_fd, *offset, SEEK_SET);
1878 if (PREDICT_FALSE (off == -1))
1882 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1883 "in_fd %d, offset %p, *offset %ld, "
1884 "rv %ld, errno %d", getpid (), in_fd,
1885 offset, *offset, off, errno_val);
1891 ASSERT (off == *offset);
1892 *offset += results + 1;
1904 func_str = "libc_send";
1907 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1908 "in_fd %d, offset %p, len %u",
1909 getpid (), out_fd, out_fd, func_str,
1910 in_fd, offset, len);
1912 size = libc_sendfile (out_fd, in_fd, offset, len);
1920 int errno_val = errno;
1922 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1923 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1924 func_str, size, errno_val);
1928 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1929 getpid (), out_fd, out_fd, size, size);
1935 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1937 return sendfile (out_fd, in_fd, offset, len);
1941 recv (int fd, void *buf, size_t n, int flags)
1944 const char *func_str;
1945 u32 sid = ldp_sid_from_fd (fd);
1947 if ((errno = -ldp_init ()))
1950 if (sid != INVALID_SESSION_ID)
1952 func_str = "vppcom_session_recvfrom";
1955 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1956 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1957 fd, fd, func_str, sid, sid, buf, n, flags);
1959 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1968 func_str = "libc_recv";
1971 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1972 "buf %p, n %u, flags 0x%x", getpid (),
1973 fd, fd, func_str, buf, n, flags);
1975 size = libc_recv (fd, buf, n, flags);
1982 int errno_val = errno;
1984 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1985 "rv %d, errno = %d", getpid (), fd, fd,
1986 func_str, size, errno_val);
1990 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1991 getpid (), fd, fd, size, size);
1997 sendto (int fd, const void *buf, size_t n, int flags,
1998 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2001 const char *func_str = __func__;
2002 u32 sid = ldp_sid_from_fd (fd);
2004 if ((errno = -ldp_init ()))
2007 if (sid != INVALID_SESSION_ID)
2009 vppcom_endpt_t *ep = 0;
2015 switch (addr->sa_family)
2018 ep->is_ip4 = VPPCOM_IS_IP4;
2020 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2022 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2026 ep->is_ip4 = VPPCOM_IS_IP6;
2028 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2030 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2034 errno = EAFNOSUPPORT;
2040 func_str = "vppcom_session_sendto";
2043 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2044 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2045 getpid (), fd, fd, func_str, sid, sid, buf, n,
2048 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2057 func_str = "libc_sendto";
2060 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2061 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2062 getpid (), fd, fd, func_str, buf, n, flags,
2065 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2073 int errno_val = errno;
2075 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2076 "rv %d, errno = %d", getpid (), fd, fd,
2077 func_str, size, errno_val);
2081 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2082 getpid (), fd, fd, size, size);
2088 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2089 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2092 const char *func_str;
2093 u32 sid = ldp_sid_from_fd (fd);
2095 if ((errno = -ldp_init ()))
2098 if (sid != INVALID_SESSION_ID)
2101 u8 src_addr[sizeof (struct sockaddr_in6)];
2103 func_str = "vppcom_session_recvfrom";
2106 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2107 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2108 getpid (), fd, fd, func_str, sid, sid, buf, n,
2113 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2116 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2119 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2129 func_str = "libc_recvfrom";
2132 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2133 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2134 getpid (), fd, fd, func_str, buf, n, flags,
2137 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2144 int errno_val = errno;
2146 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2147 "rv %d, errno = %d", getpid (), fd, fd,
2148 func_str, size, errno_val);
2152 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2153 getpid (), fd, fd, size, size);
2159 sendmsg (int fd, const struct msghdr * message, int flags)
2162 const char *func_str;
2163 u32 sid = ldp_sid_from_fd (fd);
2165 if ((errno = -ldp_init ()))
2168 if (sid != INVALID_SESSION_ID)
2170 func_str = __func__;
2172 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2178 func_str = "libc_sendmsg";
2181 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2182 "message %p, flags 0x%x",
2183 getpid (), fd, fd, func_str, message, flags);
2185 size = libc_sendmsg (fd, message, flags);
2192 int errno_val = errno;
2194 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2195 "rv %d, errno = %d", getpid (), fd, fd,
2196 func_str, size, errno_val);
2200 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2201 getpid (), fd, fd, size, size);
2208 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2211 const char *func_str;
2212 u32 sid = ldp_sid_from_fd (fd);
2214 if ((errno = -ldp_init ()))
2217 if (sid != INVALID_SESSION_ID)
2219 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2225 func_str = "libc_sendmmsg";
2228 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2229 "vmessages %p, vlen %u, flags 0x%x",
2230 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2232 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2239 int errno_val = errno;
2241 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2242 "rv %d, errno = %d", getpid (), fd, fd,
2243 func_str, size, errno_val);
2247 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2248 getpid (), fd, fd, size, size);
2255 recvmsg (int fd, struct msghdr * message, int flags)
2258 const char *func_str;
2259 u32 sid = ldp_sid_from_fd (fd);
2261 if ((errno = -ldp_init ()))
2264 if (sid != INVALID_SESSION_ID)
2266 func_str = __func__;
2268 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2274 func_str = "libc_recvmsg";
2277 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2278 "message %p, flags 0x%x",
2279 getpid (), fd, fd, func_str, message, flags);
2281 size = libc_recvmsg (fd, message, flags);
2288 int errno_val = errno;
2290 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2291 "rv %d, errno = %d", getpid (), fd, fd,
2292 func_str, size, errno_val);
2296 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2297 getpid (), fd, fd, size, size);
2304 recvmmsg (int fd, struct mmsghdr *vmessages,
2305 unsigned int vlen, int flags, struct timespec *tmo)
2308 const char *func_str;
2309 u32 sid = ldp_sid_from_fd (fd);
2311 if ((errno = -ldp_init ()))
2314 if (sid != INVALID_SESSION_ID)
2316 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2322 func_str = "libc_recvmmsg";
2325 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2326 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2327 getpid (), fd, fd, func_str, vmessages, vlen,
2330 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2337 int errno_val = errno;
2339 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2340 "rv %d, errno = %d", getpid (), fd, fd,
2341 func_str, size, errno_val);
2345 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2346 getpid (), fd, fd, size, size);
2353 getsockopt (int fd, int level, int optname,
2354 void *__restrict optval, socklen_t * __restrict optlen)
2357 const char *func_str = __func__;
2358 u32 sid = ldp_sid_from_fd (fd);
2359 u32 buflen = optlen ? (u32) * optlen : 0;
2361 if ((errno = -ldp_init ()))
2364 if (sid != INVALID_SESSION_ID)
2374 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2376 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2378 getpid (), fd, fd, func_str, sid, sid);
2379 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2383 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2385 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2387 getpid (), fd, fd, func_str, sid, sid);
2388 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2392 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2394 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2396 getpid (), fd, fd, func_str, sid, sid);
2397 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2401 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2403 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2404 "sid %u (0x%x), SOL_TCP",
2405 getpid (), fd, fd, func_str, sid, sid);
2406 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2410 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2413 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2414 "SOL_TCP, TCP_INFO, optval %p, "
2415 "optlen %d: #LDP-NOP#",
2416 getpid (), fd, fd, sid, sid,
2418 memset (optval, 0, *optlen);
2426 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2427 "sid %u (0x%x), SOL_TCP, "
2428 "optname %d unsupported!",
2429 getpid (), fd, fd, func_str, sid, sid, optname);
2437 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2439 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2441 getpid (), fd, fd, func_str, sid, sid);
2442 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2447 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2448 "sid %u (0x%x), SOL_IPV6, "
2449 "optname %d unsupported!",
2450 getpid (), fd, fd, func_str, sid, sid, optname);
2458 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2460 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2462 getpid (), fd, fd, func_str, sid, sid);
2463 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2467 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2469 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2471 getpid (), fd, fd, func_str, sid, sid);
2472 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2476 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2478 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2480 getpid (), fd, fd, func_str, sid, sid);
2481 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2483 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2486 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2488 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2489 "sid %u (0x%x), optlen %d",
2490 getpid (), fd, fd, func_str, sid, sid, buflen);
2491 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2495 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2497 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2498 "sid %u (0x%x), optlen %d",
2499 getpid (), fd, fd, func_str, sid, sid, buflen);
2500 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2504 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2506 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2508 getpid (), fd, fd, func_str, sid, sid);
2509 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2513 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2515 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2517 getpid (), fd, fd, func_str, sid, sid);
2518 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2522 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2524 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2526 getpid (), fd, fd, func_str, sid, sid);
2527 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2532 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2533 "sid %u (0x%x), SOL_SOCKET, "
2534 "optname %d unsupported!",
2535 getpid (), fd, fd, func_str, sid, sid, optname);
2543 if (rv != VPPCOM_OK)
2551 func_str = "libc_getsockopt";
2554 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2555 "optname %d, optval %p, optlen %d",
2556 getpid (), fd, fd, func_str, level, optname,
2559 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2566 int errno_val = errno;
2568 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2569 "rv %d, errno = %d", getpid (), fd, fd,
2570 func_str, rv, errno_val);
2574 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2575 getpid (), fd, fd, rv, rv);
2581 setsockopt (int fd, int level, int optname,
2582 const void *optval, socklen_t optlen)
2585 const char *func_str = __func__;
2586 u32 sid = ldp_sid_from_fd (fd);
2588 if ((errno = -ldp_init ()))
2591 if (sid != INVALID_SESSION_ID)
2601 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
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_TCP_NODELAY,
2607 (void *) optval, &optlen);
2610 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2612 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2614 getpid (), fd, fd, func_str, sid, sid);
2615 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2616 (void *) optval, &optlen);
2619 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2621 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2623 getpid (), fd, fd, func_str, sid, sid);
2624 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2625 (void *) optval, &optlen);
2628 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2630 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2631 "sid %u (0x%x), SOL_TCP",
2632 getpid (), fd, fd, func_str, sid, sid);
2633 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2634 (void *) optval, &optlen);
2638 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2639 "sid %u (0x%x), SOL_TCP, "
2640 "optname %d unsupported!",
2641 getpid (), fd, fd, func_str, sid, sid, optname);
2649 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2651 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2653 getpid (), fd, fd, func_str, sid, sid);
2654 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2655 (void *) optval, &optlen);
2659 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2660 "sid %u (0x%x), SOL_IPV6, "
2661 "optname %d unsupported!",
2662 getpid (), fd, fd, func_str, sid, sid, optname);
2670 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2672 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2674 getpid (), fd, fd, func_str, sid, sid);
2675 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2676 (void *) optval, &optlen);
2679 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2681 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2683 getpid (), fd, fd, func_str, sid, sid);
2684 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2685 (void *) optval, &optlen);
2688 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2690 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2692 getpid (), fd, fd, func_str, sid, sid);
2693 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2694 (void *) optval, &optlen);
2698 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2699 "sid %u (0x%x), SOL_SOCKET, "
2700 "optname %d unsupported!",
2701 getpid (), fd, fd, func_str, sid, sid, optname);
2709 if (rv != VPPCOM_OK)
2717 func_str = "libc_setsockopt";
2720 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2721 "optname %d, optval %p, optlen %d",
2722 getpid (), fd, fd, func_str, level, optname,
2725 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2732 int errno_val = errno;
2734 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2735 "rv %d, errno = %d", getpid (), fd, fd,
2736 func_str, rv, errno_val);
2740 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2741 getpid (), fd, fd, rv, rv);
2747 listen (int fd, int n)
2750 const char *func_str;
2751 u32 sid = ldp_sid_from_fd (fd);
2753 if ((errno = -ldp_init ()))
2756 if (sid != INVALID_SESSION_ID)
2758 func_str = "vppcom_session_listen";
2760 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2761 getpid (), fd, fd, func_str, sid, sid, n);
2763 rv = vppcom_session_listen (sid, n);
2764 if (rv != VPPCOM_OK)
2772 func_str = "libc_listen";
2774 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd,
2777 rv = libc_listen (fd, n);
2784 int errno_val = errno;
2786 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2787 "rv %d, errno = %d", getpid (), fd, fd,
2788 func_str, rv, errno_val);
2792 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2793 getpid (), fd, fd, rv, rv);
2799 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2800 socklen_t * __restrict addr_len, int flags)
2803 const char *func_str;
2804 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2807 if ((errno = -ldp_init ()))
2810 if (listen_sid != INVALID_SESSION_ID)
2813 u8 src_addr[sizeof (struct sockaddr_in6)];
2814 memset (&ep, 0, sizeof (ep));
2817 func_str = "vppcom_session_accept";
2820 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2821 "listen sid %u (0x%x), ep %p, flags 0x%x",
2822 getpid (), listen_fd, listen_fd, func_str,
2823 listen_sid, listen_sid, ep, flags);
2825 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2828 errno = -accept_sid;
2833 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2834 if (rv != VPPCOM_OK)
2836 (void) vppcom_session_close ((u32) accept_sid);
2842 func_str = "ldp_fd_from_sid";
2844 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2845 "accept sid %u (0x%x), ep %p, flags 0x%x",
2846 getpid (), listen_fd, listen_fd,
2847 func_str, accept_sid, accept_sid, ep, flags);
2848 rv = ldp_fd_from_sid ((u32) accept_sid);
2851 (void) vppcom_session_close ((u32) accept_sid);
2860 func_str = "libc_accept4";
2863 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2864 "addr %p, addr_len %p, flags 0x%x",
2865 getpid (), listen_fd, listen_fd, func_str,
2866 addr, addr_len, flags);
2868 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2875 int errno_val = errno;
2877 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2878 "rv %d, errno = %d", getpid (), listen_fd,
2879 listen_fd, func_str, rv, errno_val);
2883 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2884 getpid (), listen_fd, listen_fd, rv, rv);
2890 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2893 return ldp_accept4 (fd, addr, addr_len, flags);
2897 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2899 return ldp_accept4 (fd, addr, addr_len, 0);
2903 shutdown (int fd, int how)
2906 const char *func_str;
2907 u32 sid = ldp_sid_from_fd (fd);
2909 if ((errno = -ldp_init ()))
2912 if (sid != INVALID_SESSION_ID)
2914 func_str = "vppcom_session_close[TODO]";
2919 func_str = "libc_shutdown";
2922 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2923 getpid (), fd, fd, func_str, how);
2925 rv = libc_shutdown (fd, how);
2932 int errno_val = errno;
2934 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2935 "rv %d, errno = %d", getpid (), fd, fd,
2936 func_str, rv, errno_val);
2940 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2941 getpid (), fd, fd, rv, rv);
2947 epoll_create1 (int flags)
2949 const char *func_str;
2952 if ((errno = -ldp_init ()))
2955 if (ldp->vcl_needs_real_epoll)
2957 rv = libc_epoll_create1 (flags);
2958 ldp->vcl_needs_real_epoll = 0;
2959 ldp->vcl_mq_epfd = rv;
2960 LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv);
2963 func_str = "vppcom_epoll_create";
2965 LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str);
2967 rv = vppcom_epoll_create ();
2969 if (PREDICT_FALSE (rv < 0))
2975 rv = ldp_fd_from_sid ((u32) rv);
2981 int errno_val = errno;
2983 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2984 "rv %d, errno = %d",
2985 getpid (), func_str, rv, errno_val);
2989 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2995 epoll_create (int size)
2997 return epoll_create1 (0);
3001 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3003 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
3004 const char *func_str;
3007 if ((errno = -ldp_init ()))
3010 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3012 /* The LDP epoll_create1 always creates VCL epfd's.
3013 * The app should never have a kernel base epoll fd unless it
3014 * was acquired outside of the LD_PRELOAD process context.
3015 * In any case, if we get one, punt it to libc_epoll_ctl.
3017 func_str = "libc_epoll_ctl";
3019 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
3020 " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event);
3022 rv = libc_epoll_ctl (epfd, op, fd, event);
3026 sid = ldp_sid_from_fd (fd);
3028 LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
3029 getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid);
3031 if (sid != INVALID_SESSION_ID)
3033 func_str = "vppcom_epoll_ctl";
3035 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3036 " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd,
3037 func_str, vep_idx, vep_idx, sid, sid, event);
3039 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3040 if (rv != VPPCOM_OK)
3049 u32 size = sizeof (epfd);
3051 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3052 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
3054 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() "
3055 "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd,
3056 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
3060 func_str = "libc_epoll_create1";
3062 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3063 "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd,
3064 vep_idx, vep_idx, func_str);
3066 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3073 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3074 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3075 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3076 getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3079 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3088 else if (PREDICT_FALSE (libc_epfd < 0))
3095 func_str = "libc_epoll_ctl";
3097 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3098 "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str,
3099 libc_epfd, libc_epfd, op, fd, fd, event);
3101 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3109 int errno_val = errno;
3111 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3112 "rv %d, errno = %d", getpid (), fd, fd,
3113 func_str, rv, errno_val);
3117 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3118 getpid (), fd, fd, rv, rv);
3124 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3125 int timeout, const sigset_t * sigmask)
3127 double time_to_wait = (double) 0, time_out, now = 0;
3128 u32 vep_idx = ldp_sid_from_fd (epfd);
3129 int libc_epfd, rv = 0;
3130 const char *func_str;
3132 if ((errno = -ldp_init ()))
3135 if (PREDICT_FALSE (!events || (timeout < -1)))
3141 if (epfd == ldp->vcl_mq_epfd)
3142 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3144 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3146 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3147 getpid (), epfd, epfd, vep_idx, vep_idx);
3152 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3153 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3155 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3156 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3157 if (PREDICT_FALSE (libc_epfd < 0))
3164 LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3165 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3166 getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3167 maxevents, timeout, sigmask, time_to_wait, time_out);
3170 if (!ldp->epoll_wait_vcl)
3172 func_str = "vppcom_epoll_wait";
3174 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3175 " events %p, maxevents %d", getpid (), epfd, epfd, func_str,
3176 vep_idx, vep_idx, events, maxevents);
3178 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3181 ldp->epoll_wait_vcl = 1;
3192 ldp->epoll_wait_vcl = 0;
3196 func_str = "libc_epoll_pwait";
3198 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d "
3199 "(0x%x), events %p, maxevents %d, sigmask %p", getpid (),
3200 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3201 maxevents, sigmask);
3203 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3209 now = clib_time_now (&ldp->clib_time);
3211 while (now < time_out);
3220 int errno_val = errno;
3222 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3223 "rv %d, errno = %d", getpid (), epfd, epfd,
3224 func_str, rv, errno_val);
3228 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3229 getpid (), epfd, epfd, rv, rv);
3235 epoll_pwait (int epfd, struct epoll_event *events,
3236 int maxevents, int timeout, const sigset_t * sigmask)
3238 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3242 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3244 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3248 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3250 const char *func_str = __func__;
3251 int rv, i, n_revents = 0;
3254 double wait_for_time;
3256 LDBG (3, "LDP<%d>: fds %p, nfds %d, timeout %d", getpid (), fds, nfds,
3260 wait_for_time = (f64) timeout / 1000;
3264 for (i = 0; i < nfds; i++)
3269 LDBG (3, "LDP<%d>: fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3270 getpid (), i, fds[i].fd, fds[i].fd, fds[i].events,
3273 sid = ldp_sid_from_fd (fds[i].fd);
3274 if (sid != INVALID_SESSION_ID)
3276 fds[i].fd = -fds[i].fd;
3277 vec_add2 (ldp->vcl_poll, vp, 1);
3280 vp->events = fds[i].events;
3281 #ifdef __USE_XOPEN2K
3282 if (fds[i].events & POLLRDNORM)
3283 vp->events |= POLLIN;
3284 if (fds[i].events & POLLWRNORM)
3285 vp->events |= POLLOUT;
3287 vp->revents = fds[i].revents;
3291 vec_add1 (ldp->libc_poll, fds[i]);
3292 vec_add1 (ldp->libc_poll_idxs, i);
3298 if (vec_len (ldp->vcl_poll))
3300 func_str = "vppcom_poll";
3302 LDBG (3, "LDP<%d>: calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3303 "n_libc_fds %u", getpid (), func_str, ldp->vcl_poll,
3304 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3305 vec_len (ldp->libc_poll));
3307 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3318 if (vec_len (ldp->libc_poll))
3320 func_str = "libc_poll";
3322 LDBG (3, "LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3323 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3325 rv = libc_poll (ldp->libc_poll, vec_len (ldp->libc_poll), 0);
3338 while ((wait_for_time == -1) ||
3339 (clib_time_now (&ldp->clib_time) < wait_for_time));
3343 vec_foreach (vp, ldp->vcl_poll)
3345 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3346 fds[vp->fds_ndx].revents = vp->revents;
3347 #ifdef __USE_XOPEN2K
3348 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3349 (fds[vp->fds_ndx].events & POLLRDNORM))
3350 fds[vp->fds_ndx].revents |= POLLRDNORM;
3351 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3352 (fds[vp->fds_ndx].events & POLLWRNORM))
3353 fds[vp->fds_ndx].revents |= POLLWRNORM;
3356 vec_reset_length (ldp->vcl_poll);
3358 for (i = 0; i < vec_len (ldp->libc_poll); i++)
3360 fds[ldp->libc_poll_idxs[i]].revents = ldp->libc_poll[i].revents;
3362 vec_reset_length (ldp->libc_poll_idxs);
3363 vec_reset_length (ldp->libc_poll);
3369 int errno_val = errno;
3371 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3372 "rv %d, errno = %d", getpid (),
3373 func_str, rv, errno_val);
3378 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3379 "n_libc_fds %d", getpid (), rv, rv,
3380 vec_len (ldp->vcl_poll), vec_len (ldp->libc_poll));
3382 for (i = 0; i < nfds; i++)
3387 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3388 ".events = 0x%x, .revents = 0x%x",
3389 getpid (), i, fds[i].fd, fds[i].fd,
3390 fds[i].events, fds[i].revents);
3401 ppoll (struct pollfd *fds, nfds_t nfds,
3402 const struct timespec *timeout, const sigset_t * sigmask)
3404 if ((errno = -ldp_init ()))
3407 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3415 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3417 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3420 * This function is called when the library is loaded
3423 ldp_constructor (void)
3425 swrap_constructor ();
3426 if (ldp_init () != 0)
3427 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3429 else if (LDP_DEBUG > 0)
3430 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3434 * This function is called when the library is unloaded
3437 ldp_destructor (void)
3439 swrap_destructor ();
3442 vppcom_app_destroy ();
3446 /* Don't use clib_warning() here because that calls writev()
3447 * which will call ldp_init().
3450 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3451 __func__, __LINE__, getpid ());
3456 * fd.io coding-style-patch-verification: ON
3459 * eval: (c-set-style "gnu")