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;
71 #define LDP_DEBUG ldp->debug
73 static ldp_main_t ldp_main = {
74 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
75 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
76 .debug = LDP_DEBUG_INIT,
79 static ldp_main_t *ldp = &ldp_main;
82 * RETURN: 0 on success or -1 on error.
85 ldp_set_app_name (char *app_name)
87 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
88 "ldp-%d-%s", getpid (), app_name);
90 if (rv >= LDP_APP_NAME_MAX)
91 app_name[LDP_APP_NAME_MAX - 1] = 0;
97 if (ldp->app_name[0] == '\0')
98 ldp_set_app_name ("app");
100 return ldp->app_name;
104 ldp_fd_from_sid (u32 sid)
106 if (PREDICT_FALSE (sid >= ldp->sid_bit_val))
109 return (sid | ldp->sid_bit_val);
113 ldp_fd_is_sid (int fd)
115 return ((u32) fd & ldp->sid_bit_val) ? 1 : 0;
119 ldp_sid_from_fd (int fd)
121 return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) :
130 if (PREDICT_FALSE (!ldp->init))
133 rv = vppcom_app_create (ldp_get_app_name ());
136 char *env_var_str = getenv (LDP_ENV_DEBUG);
140 if (sscanf (env_var_str, "%u", &tmp) != 1)
141 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level "
142 "specified in the env var " LDP_ENV_DEBUG
143 " (%s)!", getpid (), env_var_str);
148 clib_warning ("LDP<%d>: configured LDP debug level (%u) "
149 "from the env var " LDP_ENV_DEBUG "!",
150 getpid (), ldp->debug);
154 env_var_str = getenv (LDP_ENV_APP_NAME);
157 ldp_set_app_name (env_var_str);
159 clib_warning ("LDP<%d>: configured LDP app name (%s) "
160 "from the env var " LDP_ENV_APP_NAME "!",
161 getpid (), ldp->app_name);
164 env_var_str = getenv (LDP_ENV_SID_BIT);
168 if (sscanf (env_var_str, "%u", &sb) != 1)
170 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit "
171 "specified in the env var "
172 LDP_ENV_SID_BIT " (%s)!"
173 "sid bit value %d (0x%x)",
174 getpid (), env_var_str,
175 ldp->sid_bit_val, ldp->sid_bit_val);
177 else if (sb < LDP_SID_BIT_MIN)
179 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
180 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
182 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) "
183 "specified in the env var "
184 LDP_ENV_SID_BIT " (%s) is too small. "
185 "Using LDP_SID_BIT_MIN (%d)! "
186 "sid bit value %d (0x%x)",
187 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
188 ldp->sid_bit_val, ldp->sid_bit_val);
190 else if (sb > LDP_SID_BIT_MAX)
192 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
193 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
195 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) "
196 "specified in the env var "
197 LDP_ENV_SID_BIT " (%s) is too big. "
198 "Using LDP_SID_BIT_MAX (%d)! "
199 "sid bit value %d (0x%x)",
200 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
201 ldp->sid_bit_val, ldp->sid_bit_val);
205 ldp->sid_bit_val = (1 << sb);
206 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
209 clib_warning ("LDP<%d>: configured LDP sid bit (%u) "
210 "from " LDP_ENV_SID_BIT
211 "! sid bit value %d (0x%x)", getpid (),
212 sb, ldp->sid_bit_val, ldp->sid_bit_val);
216 clib_time_init (&ldp->clib_time);
218 clib_warning ("LDP<%d>: LDP initialization: done!", getpid ());
222 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
223 " failed! rv = %d (%s)\n",
224 getpid (), rv, vppcom_retval_str (rv));
235 const char *func_str;
236 u32 sid = ldp_sid_from_fd (fd);
238 if ((errno = -ldp_init ()))
241 if (sid != INVALID_SESSION_ID)
245 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
246 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
249 func_str = "libc_close";
253 ("LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
254 getpid (), fd, fd, func_str, epfd, epfd);
256 rv = libc_close (epfd);
259 u32 size = sizeof (epfd);
262 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
266 else if (PREDICT_FALSE (epfd < 0))
273 func_str = "vppcom_session_close";
276 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
277 getpid (), fd, fd, func_str, sid, sid);
279 rv = vppcom_session_close (sid);
288 func_str = "libc_close";
291 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s()",
292 getpid (), fd, fd, func_str);
294 rv = libc_close (fd);
302 int errno_val = errno;
304 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
305 "rv %d, errno = %d", getpid (), fd, fd,
306 func_str, rv, errno_val);
310 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
311 getpid (), fd, fd, rv, rv);
317 read (int fd, void *buf, size_t nbytes)
320 const char *func_str;
321 u32 sid = ldp_sid_from_fd (fd);
323 if ((errno = -ldp_init ()))
326 if (sid != INVALID_SESSION_ID)
328 func_str = "vppcom_session_read";
331 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
332 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
333 fd, fd, func_str, sid, sid, buf, nbytes);
335 size = vppcom_session_read (sid, buf, nbytes);
344 func_str = "libc_read";
347 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
348 "buf %p, nbytes %u", getpid (),
349 fd, fd, func_str, buf, nbytes);
351 size = libc_read (fd, buf, nbytes);
358 int errno_val = errno;
360 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
361 "rv %d, errno = %d", getpid (), fd, fd,
362 func_str, size, errno_val);
366 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
367 getpid (), fd, fd, size, size);
373 readv (int fd, const struct iovec * iov, int iovcnt)
375 const char *func_str;
377 u32 sid = ldp_sid_from_fd (fd);
378 int rv = 0, i, total = 0;
380 if ((errno = -ldp_init ()))
383 if (sid != INVALID_SESSION_ID)
385 func_str = "vppcom_session_read";
388 for (i = 0; i < iovcnt; ++i)
391 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
392 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
393 getpid (), fd, fd, func_str, i, sid, sid,
396 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
402 if (rv < iov[i].iov_len)
405 clib_warning ("LDP<%d>: fd %d (0x%x): "
406 "rv (%d) < iov[%d].iov_len (%d)",
407 getpid (), fd, fd, rv, i,
414 while ((rv >= 0) && (total == 0));
426 func_str = "libc_readv";
429 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
430 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
432 size = libc_readv (fd, iov, iovcnt);
439 int errno_val = errno;
441 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
442 "rv %d, errno = %d", getpid (), fd, fd,
443 func_str, size, errno_val);
447 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
448 getpid (), fd, fd, size, size);
454 write (int fd, const void *buf, size_t nbytes)
456 const char *func_str;
458 u32 sid = ldp_sid_from_fd (fd);
460 if ((errno = -ldp_init ()))
463 if (sid != INVALID_SESSION_ID)
465 func_str = "vppcom_session_write";
468 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
469 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
470 fd, fd, func_str, sid, sid, buf, nbytes);
472 size = vppcom_session_write (sid, (void *) buf, nbytes);
481 func_str = "libc_write";
484 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
485 "buf %p, nbytes %u", getpid (),
486 fd, fd, func_str, buf, nbytes);
488 size = libc_write (fd, buf, nbytes);
495 int errno_val = errno;
497 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
498 "rv %d, errno = %d", getpid (), fd, fd,
499 func_str, size, errno_val);
503 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
504 getpid (), fd, fd, size, size);
510 writev (int fd, const struct iovec * iov, int iovcnt)
512 const char *func_str;
513 ssize_t size = 0, total = 0;
514 u32 sid = ldp_sid_from_fd (fd);
518 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
521 if ((errno = -ldp_init ()))
524 if (sid != INVALID_SESSION_ID)
526 func_str = "vppcom_session_write";
529 for (i = 0; i < iovcnt; ++i)
532 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
533 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
534 __func__, __LINE__, getpid (), fd, fd, func_str,
535 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
537 rv = vppcom_session_write (sid, iov[i].iov_base,
544 if (rv < iov[i].iov_len)
547 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
548 "rv (%d) < iov[%d].iov_len (%ld)",
549 __func__, __LINE__, getpid (), fd, fd,
550 rv, i, iov[i].iov_len);
556 while ((rv >= 0) && (total == 0));
568 func_str = "libc_writev";
571 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
572 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
573 fd, fd, func_str, iov, iovcnt);
575 size = libc_writev (fd, iov, iovcnt);
582 int errno_val = errno;
585 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
586 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
587 fd, func_str, size, errno_val);
591 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
592 __func__, __LINE__, getpid (), fd, fd, size);
598 fcntl (int fd, int cmd, ...)
600 const char *func_str = __func__;
603 u32 sid = ldp_sid_from_fd (fd);
605 if ((errno = -ldp_init ()))
609 if (sid != INVALID_SESSION_ID)
611 int flags = va_arg (ap, int);
614 size = sizeof (flags);
619 func_str = "vppcom_session_attr[SET_FLAGS]";
621 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
622 "sid %u (0x%x) flags %d (0x%x), size %d",
623 getpid (), fd, fd, func_str, sid, sid,
627 vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
631 func_str = "vppcom_session_attr[GET_FLAGS]";
634 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
635 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
636 sid, flags, flags, size);
639 vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
643 clib_warning ("LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): "
644 "%s() returned flags %d (0x%x)",
645 getpid (), fd, fd, cmd, func_str, flags, flags);
662 func_str = "libc_vfcntl";
665 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
666 getpid (), fd, fd, func_str, cmd);
668 rv = libc_vfcntl (fd, cmd, ap);
677 int errno_val = errno;
679 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
680 "rv %d, errno = %d", getpid (), fd, fd,
681 func_str, rv, errno_val);
685 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
686 getpid (), fd, fd, rv, rv);
692 ioctl (int fd, unsigned long int cmd, ...)
694 const char *func_str;
697 u32 sid = ldp_sid_from_fd (fd);
699 if ((errno = -ldp_init ()))
703 if (sid != INVALID_SESSION_ID)
705 func_str = "vppcom_session_attr[GET_NREAD]";
712 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
713 getpid (), fd, fd, func_str, sid, sid);
715 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
720 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
721 u32 size = sizeof (flags);
723 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
724 * non-blocking, the flags should be read here and merged
728 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
729 "sid %u (0x%x), flags %d (0x%x), size %d",
730 getpid (), fd, fd, func_str, sid, sid,
733 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
750 func_str = "libc_vioctl";
753 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
754 getpid (), fd, fd, func_str, cmd);
756 rv = libc_vioctl (fd, cmd, ap);
763 int errno_val = errno;
765 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
766 "rv %d, errno = %d", getpid (), fd, fd,
767 func_str, rv, errno_val);
771 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
772 getpid (), fd, fd, rv, rv);
779 ldp_pselect (int nfds, fd_set * __restrict readfds,
780 fd_set * __restrict writefds,
781 fd_set * __restrict exceptfds,
782 const struct timespec *__restrict timeout,
783 const __sigset_t * __restrict sigmask)
786 char *func_str = "##";
789 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
790 u32 minbits = clib_max (nfds, BITS (uword));
801 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
802 (f64) 0 : (f64) timeout->tv_sec +
803 (f64) timeout->tv_nsec / (f64) 1000000000;
805 /* select as fine grained sleep */
809 clib_warning ("LDP<%d>: sleeping for %.02f seconds",
810 getpid (), time_out);
812 time_out += clib_time_now (&ldp->clib_time);
813 while (clib_time_now (&ldp->clib_time) < time_out)
827 if (nfds <= ldp->sid_bit_val)
829 func_str = "libc_pselect";
833 ("LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
834 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
835 readfds, writefds, exceptfds, timeout, sigmask);
837 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
842 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
844 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
845 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
846 ldp->sid_bit_val, ldp->sid_bit_val,
847 FD_SETSIZE / 2, FD_SETSIZE / 2);
852 sid_bits = libc_bits = 0;
855 clib_bitmap_validate (ldp->sid_rd_bitmap, minbits);
856 clib_bitmap_validate (ldp->libc_rd_bitmap, minbits);
857 clib_bitmap_validate (ldp->rd_bitmap, minbits);
858 clib_memcpy (ldp->rd_bitmap, readfds,
859 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
863 clib_bitmap_foreach (fd, ldp->rd_bitmap,
865 sid = ldp_sid_from_fd (fd);
867 clib_warning ("LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
868 getpid (), fd, fd, sid, sid);
869 if (sid == INVALID_SESSION_ID)
870 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
872 clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1);
876 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
877 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
879 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
880 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
883 clib_warning ("LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
884 "libc_bits_set %d, libc_bits %d", getpid (),
885 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
889 clib_bitmap_validate (ldp->sid_wr_bitmap, minbits);
890 clib_bitmap_validate (ldp->libc_wr_bitmap, minbits);
891 clib_bitmap_validate (ldp->wr_bitmap, minbits);
892 clib_memcpy (ldp->wr_bitmap, writefds,
893 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
897 clib_bitmap_foreach (fd, ldp->wr_bitmap,
899 sid = ldp_sid_from_fd (fd);
901 clib_warning ("LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
902 getpid (), fd, fd, sid, sid);
903 if (sid == INVALID_SESSION_ID)
904 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
906 clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1);
910 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
911 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
913 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
914 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
917 clib_warning ("LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
918 "libc_bits_set %d, libc_bits %d", getpid (),
919 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
923 clib_bitmap_validate (ldp->sid_ex_bitmap, minbits);
924 clib_bitmap_validate (ldp->libc_ex_bitmap, minbits);
925 clib_bitmap_validate (ldp->ex_bitmap, minbits);
926 clib_memcpy (ldp->ex_bitmap, exceptfds,
927 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
931 clib_bitmap_foreach (fd, ldp->ex_bitmap,
933 sid = ldp_sid_from_fd (fd);
935 clib_warning ("LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
936 getpid (), fd, fd, sid, sid);
937 if (sid == INVALID_SESSION_ID)
938 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
940 clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1);
944 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
945 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
947 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
948 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
951 clib_warning ("LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
952 "libc_bits_set %d, libc_bits %d", getpid (),
953 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
956 if (PREDICT_FALSE (!sid_bits && !libc_bits))
967 if (!ldp->select_vcl)
969 func_str = "vppcom_select";
972 clib_memcpy (ldp->rd_bitmap, ldp->sid_rd_bitmap,
973 vec_len (ldp->rd_bitmap) *
974 sizeof (clib_bitmap_t));
976 clib_memcpy (ldp->wr_bitmap, ldp->sid_wr_bitmap,
977 vec_len (ldp->wr_bitmap) *
978 sizeof (clib_bitmap_t));
980 clib_memcpy (ldp->ex_bitmap, ldp->sid_ex_bitmap,
981 vec_len (ldp->ex_bitmap) *
982 sizeof (clib_bitmap_t));
984 rv = vppcom_select (sid_bits,
985 readfds ? ldp->rd_bitmap : NULL,
986 writefds ? ldp->wr_bitmap : NULL,
987 exceptfds ? ldp->ex_bitmap : NULL, 0);
998 clib_bitmap_foreach (sid, ldp->rd_bitmap,
1000 fd = ldp_fd_from_sid (sid);
1001 if (PREDICT_FALSE (fd < 0))
1007 FD_SET (fd, readfds);
1014 clib_bitmap_foreach (sid, ldp->wr_bitmap,
1016 fd = ldp_fd_from_sid (sid);
1017 if (PREDICT_FALSE (fd < 0))
1023 FD_SET (fd, writefds);
1030 clib_bitmap_foreach (sid, ldp->ex_bitmap,
1032 fd = ldp_fd_from_sid (sid);
1033 if (PREDICT_FALSE (fd < 0))
1039 FD_SET (fd, exceptfds);
1043 ldp->select_vcl = 1;
1048 ldp->select_vcl = 0;
1052 struct timespec tspec;
1054 func_str = "libc_pselect";
1057 clib_memcpy (readfds, ldp->libc_rd_bitmap,
1058 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
1060 clib_memcpy (writefds, ldp->libc_wr_bitmap,
1061 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
1063 clib_memcpy (exceptfds, ldp->libc_ex_bitmap,
1064 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
1065 tspec.tv_sec = tspec.tv_nsec = 0;
1066 rv = libc_pselect (libc_bits,
1067 readfds ? readfds : NULL,
1068 writefds ? writefds : NULL,
1069 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1074 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1078 /* TBD: set timeout to amount of time left */
1079 vec_reset_length (ldp->rd_bitmap);
1080 vec_reset_length (ldp->sid_rd_bitmap);
1081 vec_reset_length (ldp->libc_rd_bitmap);
1082 vec_reset_length (ldp->wr_bitmap);
1083 vec_reset_length (ldp->sid_wr_bitmap);
1084 vec_reset_length (ldp->libc_wr_bitmap);
1085 vec_reset_length (ldp->ex_bitmap);
1086 vec_reset_length (ldp->sid_ex_bitmap);
1087 vec_reset_length (ldp->libc_ex_bitmap);
1093 int errno_val = errno;
1095 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1096 "rv %d, errno = %d", getpid (),
1097 func_str, rv, errno_val);
1101 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1107 select (int nfds, fd_set * __restrict readfds,
1108 fd_set * __restrict writefds,
1109 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1111 struct timespec tspec;
1115 tspec.tv_sec = timeout->tv_sec;
1116 tspec.tv_nsec = timeout->tv_usec * 1000;
1118 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1119 timeout ? &tspec : NULL, NULL);
1122 #ifdef __USE_XOPEN2K
1124 pselect (int nfds, fd_set * __restrict readfds,
1125 fd_set * __restrict writefds,
1126 fd_set * __restrict exceptfds,
1127 const struct timespec *__restrict timeout,
1128 const __sigset_t * __restrict sigmask)
1130 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1135 socket (int domain, int type, int protocol)
1137 const char *func_str;
1139 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1140 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1142 if ((errno = -ldp_init ()))
1145 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1146 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1149 u8 proto = ((sock_type == SOCK_DGRAM) ?
1150 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1152 func_str = "vppcom_session_create";
1155 clib_warning ("LDP<%d>: : calling %s(): "
1156 "proto %u (%s), is_nonblocking %u",
1157 getpid (), func_str, proto,
1158 vppcom_proto_str (proto), is_nonblocking);
1160 sid = vppcom_session_create (proto, is_nonblocking);
1168 func_str = "ldp_fd_from_sid";
1169 rv = ldp_fd_from_sid (sid);
1172 (void) vppcom_session_close (sid);
1180 func_str = "libc_socket";
1183 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1185 rv = libc_socket (domain, type, protocol);
1192 int errno_val = errno;
1194 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1195 "rv %d, errno = %d",
1196 getpid (), func_str, rv, errno_val);
1200 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1206 * Create two new sockets, of type TYPE in domain DOMAIN and using
1207 * protocol PROTOCOL, which are connected to each other, and put file
1208 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1209 * one will be chosen automatically.
1210 * Returns 0 on success, -1 for errors.
1213 socketpair (int domain, int type, int protocol, int fds[2])
1215 const char *func_str;
1217 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1219 if ((errno = -ldp_init ()))
1222 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1223 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1225 func_str = __func__;
1227 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1233 func_str = "libc_socket";
1236 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1238 rv = libc_socket (domain, type, protocol);
1245 int errno_val = errno;
1247 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1248 "rv %d, errno = %d",
1249 getpid (), func_str, rv, errno_val);
1253 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1259 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1262 const char *func_str;
1263 u32 sid = ldp_sid_from_fd (fd);
1265 if ((errno = -ldp_init ()))
1268 if (sid != INVALID_SESSION_ID)
1272 func_str = "vppcom_session_bind";
1274 switch (addr->sa_family)
1277 if (len != sizeof (struct sockaddr_in))
1280 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1281 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1286 ep.is_ip4 = VPPCOM_IS_IP4;
1287 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1288 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1292 if (len != sizeof (struct sockaddr_in6))
1295 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1296 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1301 ep.is_ip4 = VPPCOM_IS_IP6;
1302 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1303 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1307 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1308 "Unsupported address family %u!",
1309 getpid (), fd, fd, sid, sid, addr->sa_family);
1310 errno = EAFNOSUPPORT;
1315 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1317 getpid (), fd, fd, func_str, sid, sid, addr, len);
1319 rv = vppcom_session_bind (sid, &ep);
1320 if (rv != VPPCOM_OK)
1328 func_str = "libc_bind";
1331 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1333 getpid (), fd, fd, func_str, addr, len);
1335 rv = libc_bind (fd, addr, len);
1343 int errno_val = errno;
1345 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1346 "rv %d, errno = %d", getpid (), fd, fd,
1347 func_str, rv, errno_val);
1351 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1352 getpid (), fd, fd, rv, rv);
1358 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1359 vppcom_endpt_t * ep)
1362 int sa_len, copy_len;
1364 if ((errno = -ldp_init ()))
1367 if (addr && len && ep)
1369 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1370 switch (addr->sa_family)
1373 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1374 if (*len > sizeof (struct sockaddr_in))
1375 *len = sizeof (struct sockaddr_in);
1376 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1377 copy_len = *len - sa_len;
1379 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1384 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1385 if (*len > sizeof (struct sockaddr_in6))
1386 *len = sizeof (struct sockaddr_in6);
1387 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1388 copy_len = *len - sa_len;
1390 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1391 __in6_u.__u6_addr8, ep->ip, copy_len);
1404 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1407 const char *func_str;
1408 u32 sid = ldp_sid_from_fd (fd);
1410 if ((errno = -ldp_init ()))
1413 if (sid != INVALID_SESSION_ID)
1416 u8 addr_buf[sizeof (struct in6_addr)];
1417 u32 size = sizeof (ep);
1420 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1423 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1425 getpid (), fd, fd, func_str, sid, sid, addr, len);
1427 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1428 if (rv != VPPCOM_OK)
1435 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1436 if (rv != VPPCOM_OK)
1445 func_str = "libc_getsockname";
1448 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1450 getpid (), fd, fd, func_str, addr, len);
1452 rv = libc_getsockname (fd, addr, len);
1459 int errno_val = errno;
1461 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1462 "rv %d, errno = %d", getpid (), fd, fd,
1463 func_str, rv, errno_val);
1467 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1468 getpid (), fd, fd, rv, rv);
1474 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1477 const char *func_str = __func__;
1478 u32 sid = ldp_sid_from_fd (fd);
1480 if ((errno = -ldp_init ()))
1485 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1486 getpid (), fd, fd, len);
1492 if (sid != INVALID_SESSION_ID)
1496 func_str = "vppcom_session_connect";
1498 switch (addr->sa_family)
1501 if (len != sizeof (struct sockaddr_in))
1504 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1505 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1510 ep.is_ip4 = VPPCOM_IS_IP4;
1511 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1512 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1516 if (len != sizeof (struct sockaddr_in6))
1519 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1520 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1525 ep.is_ip4 = VPPCOM_IS_IP6;
1526 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1527 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1531 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1532 "Unsupported address family %u!",
1533 getpid (), fd, fd, sid, sid, addr->sa_family);
1534 errno = EAFNOSUPPORT;
1539 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1541 getpid (), fd, fd, func_str, sid, sid, addr, len);
1543 rv = vppcom_session_connect (sid, &ep);
1544 if (rv != VPPCOM_OK)
1552 func_str = "libc_connect";
1555 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1557 getpid (), fd, fd, func_str, addr, len);
1559 rv = libc_connect (fd, addr, len);
1567 int errno_val = errno;
1569 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1570 "rv %d, errno = %d", getpid (), fd, fd,
1571 func_str, rv, errno_val);
1575 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1576 getpid (), fd, fd, rv, rv);
1582 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1585 const char *func_str;
1586 u32 sid = ldp_sid_from_fd (fd);
1588 if ((errno = -ldp_init ()))
1591 if (sid != INVALID_SESSION_ID)
1594 u8 addr_buf[sizeof (struct in6_addr)];
1595 u32 size = sizeof (ep);
1598 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1601 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1603 getpid (), fd, fd, func_str, sid, sid, addr, len);
1605 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1606 if (rv != VPPCOM_OK)
1613 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1614 if (rv != VPPCOM_OK)
1623 func_str = "libc_getpeername";
1626 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1628 getpid (), fd, fd, func_str, addr, len);
1630 rv = libc_getpeername (fd, addr, len);
1637 int errno_val = errno;
1639 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1640 "rv %d, errno = %d", getpid (), fd, fd,
1641 func_str, rv, errno_val);
1645 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1646 getpid (), fd, fd, rv, rv);
1652 send (int fd, const void *buf, size_t n, int flags)
1655 const char *func_str;
1656 u32 sid = ldp_sid_from_fd (fd);
1658 if ((errno = -ldp_init ()))
1661 if (sid != INVALID_SESSION_ID)
1664 func_str = "vppcom_session_sendto";
1667 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1668 "buf %p, n %u, flags 0x%x",
1669 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1671 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1672 if (size < VPPCOM_OK)
1680 func_str = "libc_send";
1683 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1684 "buf %p, n %u, flags 0x%x",
1685 getpid (), fd, fd, func_str, buf, n, flags);
1687 size = libc_send (fd, buf, n, flags);
1694 int errno_val = errno;
1696 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1697 "rv %d, errno = %d", getpid (), fd, fd,
1698 func_str, size, errno_val);
1702 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1703 getpid (), fd, fd, size, size);
1709 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1712 const char *func_str;
1713 u32 sid = ldp_sid_from_fd (out_fd);
1715 if ((errno = -ldp_init ()))
1718 if (sid != INVALID_SESSION_ID)
1721 ssize_t results = 0;
1722 size_t n_bytes_left = len;
1723 size_t bytes_to_read;
1727 u32 flags, flags_len = sizeof (flags);
1729 func_str = "vppcom_session_attr[GET_FLAGS]";
1730 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1732 if (PREDICT_FALSE (rv != VPPCOM_OK))
1734 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1735 "sid %u (0x%x), returned %d (%s)!", getpid (),
1736 out_fd, out_fd, func_str, sid, sid, rv,
1737 vppcom_retval_str (rv));
1739 vec_reset_length (ldp->io_buffer);
1747 off_t off = lseek (in_fd, *offset, SEEK_SET);
1748 if (PREDICT_FALSE (off == -1))
1752 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1753 "SEEK_SET failed: in_fd %d, offset %p, "
1754 "*offset %ld, rv %ld, errno %d", getpid (),
1755 out_fd, out_fd, in_fd, offset, *offset, off,
1762 ASSERT (off == *offset);
1767 func_str = "vppcom_session_attr[GET_NWRITE]";
1768 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1772 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1773 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1774 sid, sid, size, vppcom_retval_str (size));
1775 vec_reset_length (ldp->io_buffer);
1781 bytes_to_read = size;
1784 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1785 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1786 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1789 if (bytes_to_read == 0)
1791 if (flags & O_NONBLOCK)
1796 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1798 getpid (), out_fd, out_fd, sid, sid);
1806 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1807 vec_validate (ldp->io_buffer, bytes_to_read);
1808 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1811 func_str = "libc_read";
1813 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1814 "io_buffer %p, bytes_to_read %lu, rv %d, "
1815 "errno %d", getpid (), out_fd, out_fd, func_str,
1816 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1822 vec_reset_length (ldp->io_buffer);
1828 func_str = "vppcom_session_write";
1831 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1832 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1833 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1834 results, n_bytes_left);
1836 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1839 if (size == VPPCOM_EAGAIN)
1841 if (flags & O_NONBLOCK)
1847 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1848 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1858 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1859 "sid %u, io_buffer %p, nbytes %u "
1861 getpid (), out_fd, out_fd, func_str,
1862 sid, ldp->io_buffer, nbytes,
1863 size, vppcom_retval_str (size));
1867 vec_reset_length (ldp->io_buffer);
1876 ASSERT (n_bytes_left >= nbytes);
1877 n_bytes_left = n_bytes_left - nbytes;
1879 while (n_bytes_left > 0);
1882 vec_reset_length (ldp->io_buffer);
1885 off_t off = lseek (in_fd, *offset, SEEK_SET);
1886 if (PREDICT_FALSE (off == -1))
1890 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1891 "in_fd %d, offset %p, *offset %ld, "
1892 "rv %ld, errno %d", getpid (), in_fd,
1893 offset, *offset, off, errno_val);
1899 ASSERT (off == *offset);
1900 *offset += results + 1;
1912 func_str = "libc_send";
1915 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1916 "in_fd %d, offset %p, len %u",
1917 getpid (), out_fd, out_fd, func_str,
1918 in_fd, offset, len);
1920 size = libc_sendfile (out_fd, in_fd, offset, len);
1928 int errno_val = errno;
1930 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1931 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1932 func_str, size, errno_val);
1936 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1937 getpid (), out_fd, out_fd, size, size);
1943 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1945 return sendfile (out_fd, in_fd, offset, len);
1949 recv (int fd, void *buf, size_t n, int flags)
1952 const char *func_str;
1953 u32 sid = ldp_sid_from_fd (fd);
1955 if ((errno = -ldp_init ()))
1958 if (sid != INVALID_SESSION_ID)
1960 func_str = "vppcom_session_recvfrom";
1963 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1964 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1965 fd, fd, func_str, sid, sid, buf, n, flags);
1967 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1976 func_str = "libc_recv";
1979 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1980 "buf %p, n %u, flags 0x%x", getpid (),
1981 fd, fd, func_str, buf, n, flags);
1983 size = libc_recv (fd, buf, n, flags);
1990 int errno_val = errno;
1992 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1993 "rv %d, errno = %d", getpid (), fd, fd,
1994 func_str, size, errno_val);
1998 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1999 getpid (), fd, fd, size, size);
2005 sendto (int fd, const void *buf, size_t n, int flags,
2006 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2009 const char *func_str = __func__;
2010 u32 sid = ldp_sid_from_fd (fd);
2012 if ((errno = -ldp_init ()))
2015 if (sid != INVALID_SESSION_ID)
2017 vppcom_endpt_t *ep = 0;
2023 switch (addr->sa_family)
2026 ep->is_ip4 = VPPCOM_IS_IP4;
2028 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2030 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2034 ep->is_ip4 = VPPCOM_IS_IP6;
2036 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2038 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2042 errno = EAFNOSUPPORT;
2048 func_str = "vppcom_session_sendto";
2051 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2052 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2053 getpid (), fd, fd, func_str, sid, sid, buf, n,
2056 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2065 func_str = "libc_sendto";
2068 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2069 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2070 getpid (), fd, fd, func_str, buf, n, flags,
2073 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2081 int errno_val = errno;
2083 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2084 "rv %d, errno = %d", getpid (), fd, fd,
2085 func_str, size, errno_val);
2089 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2090 getpid (), fd, fd, size, size);
2096 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2097 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2100 const char *func_str;
2101 u32 sid = ldp_sid_from_fd (fd);
2103 if ((errno = -ldp_init ()))
2106 if (sid != INVALID_SESSION_ID)
2109 u8 src_addr[sizeof (struct sockaddr_in6)];
2111 func_str = "vppcom_session_recvfrom";
2114 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2115 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2116 getpid (), fd, fd, func_str, sid, sid, buf, n,
2121 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2124 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2127 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2137 func_str = "libc_recvfrom";
2140 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2141 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2142 getpid (), fd, fd, func_str, buf, n, flags,
2145 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2152 int errno_val = errno;
2154 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2155 "rv %d, errno = %d", getpid (), fd, fd,
2156 func_str, size, errno_val);
2160 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2161 getpid (), fd, fd, size, size);
2167 sendmsg (int fd, const struct msghdr * message, int flags)
2170 const char *func_str;
2171 u32 sid = ldp_sid_from_fd (fd);
2173 if ((errno = -ldp_init ()))
2176 if (sid != INVALID_SESSION_ID)
2178 func_str = __func__;
2180 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2186 func_str = "libc_sendmsg";
2189 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2190 "message %p, flags 0x%x",
2191 getpid (), fd, fd, func_str, message, flags);
2193 size = libc_sendmsg (fd, message, flags);
2200 int errno_val = errno;
2202 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2203 "rv %d, errno = %d", getpid (), fd, fd,
2204 func_str, size, errno_val);
2208 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2209 getpid (), fd, fd, size, size);
2216 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2219 const char *func_str;
2220 u32 sid = ldp_sid_from_fd (fd);
2222 if ((errno = -ldp_init ()))
2225 if (sid != INVALID_SESSION_ID)
2227 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2233 func_str = "libc_sendmmsg";
2236 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2237 "vmessages %p, vlen %u, flags 0x%x",
2238 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2240 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2247 int errno_val = errno;
2249 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2250 "rv %d, errno = %d", getpid (), fd, fd,
2251 func_str, size, errno_val);
2255 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2256 getpid (), fd, fd, size, size);
2263 recvmsg (int fd, struct msghdr * message, int flags)
2266 const char *func_str;
2267 u32 sid = ldp_sid_from_fd (fd);
2269 if ((errno = -ldp_init ()))
2272 if (sid != INVALID_SESSION_ID)
2274 func_str = __func__;
2276 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2282 func_str = "libc_recvmsg";
2285 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2286 "message %p, flags 0x%x",
2287 getpid (), fd, fd, func_str, message, flags);
2289 size = libc_recvmsg (fd, message, flags);
2296 int errno_val = errno;
2298 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2299 "rv %d, errno = %d", getpid (), fd, fd,
2300 func_str, size, errno_val);
2304 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2305 getpid (), fd, fd, size, size);
2312 recvmmsg (int fd, struct mmsghdr *vmessages,
2313 unsigned int vlen, int flags, struct timespec *tmo)
2316 const char *func_str;
2317 u32 sid = ldp_sid_from_fd (fd);
2319 if ((errno = -ldp_init ()))
2322 if (sid != INVALID_SESSION_ID)
2324 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2330 func_str = "libc_recvmmsg";
2333 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2334 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2335 getpid (), fd, fd, func_str, vmessages, vlen,
2338 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2345 int errno_val = errno;
2347 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2348 "rv %d, errno = %d", getpid (), fd, fd,
2349 func_str, size, errno_val);
2353 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2354 getpid (), fd, fd, size, size);
2361 getsockopt (int fd, int level, int optname,
2362 void *__restrict optval, socklen_t * __restrict optlen)
2365 const char *func_str = __func__;
2366 u32 sid = ldp_sid_from_fd (fd);
2367 u32 buflen = optlen ? (u32) * optlen : 0;
2369 if ((errno = -ldp_init ()))
2372 if (sid != INVALID_SESSION_ID)
2382 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2384 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2386 getpid (), fd, fd, func_str, sid, sid);
2387 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2391 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2393 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2395 getpid (), fd, fd, func_str, sid, sid);
2396 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2400 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2402 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2404 getpid (), fd, fd, func_str, sid, sid);
2405 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2409 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2411 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2412 "sid %u (0x%x), SOL_TCP",
2413 getpid (), fd, fd, func_str, sid, sid);
2414 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2418 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2421 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2422 "SOL_TCP, TCP_INFO, optval %p, "
2423 "optlen %d: #LDP-NOP#",
2424 getpid (), fd, fd, sid, sid,
2426 memset (optval, 0, *optlen);
2434 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2435 "sid %u (0x%x), SOL_TCP, "
2436 "optname %d unsupported!",
2437 getpid (), fd, fd, func_str, sid, sid, optname);
2445 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2447 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2449 getpid (), fd, fd, func_str, sid, sid);
2450 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2455 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2456 "sid %u (0x%x), SOL_IPV6, "
2457 "optname %d unsupported!",
2458 getpid (), fd, fd, func_str, sid, sid, optname);
2466 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2468 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2470 getpid (), fd, fd, func_str, sid, sid);
2471 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2475 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2477 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2479 getpid (), fd, fd, func_str, sid, sid);
2480 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2484 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2486 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2488 getpid (), fd, fd, func_str, sid, sid);
2489 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2491 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2494 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2496 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2497 "sid %u (0x%x), optlen %d",
2498 getpid (), fd, fd, func_str, sid, sid, buflen);
2499 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2503 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2505 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2506 "sid %u (0x%x), optlen %d",
2507 getpid (), fd, fd, func_str, sid, sid, buflen);
2508 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2512 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2514 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2516 getpid (), fd, fd, func_str, sid, sid);
2517 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2521 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2523 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2525 getpid (), fd, fd, func_str, sid, sid);
2526 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2530 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2532 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2534 getpid (), fd, fd, func_str, sid, sid);
2535 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2540 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2541 "sid %u (0x%x), SOL_SOCKET, "
2542 "optname %d unsupported!",
2543 getpid (), fd, fd, func_str, sid, sid, optname);
2551 if (rv != VPPCOM_OK)
2559 func_str = "libc_getsockopt";
2562 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2563 "optname %d, optval %p, optlen %d",
2564 getpid (), fd, fd, func_str, level, optname,
2567 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2574 int errno_val = errno;
2576 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2577 "rv %d, errno = %d", getpid (), fd, fd,
2578 func_str, rv, errno_val);
2582 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2583 getpid (), fd, fd, rv, rv);
2589 setsockopt (int fd, int level, int optname,
2590 const void *optval, socklen_t optlen)
2593 const char *func_str = __func__;
2594 u32 sid = ldp_sid_from_fd (fd);
2596 if ((errno = -ldp_init ()))
2599 if (sid != INVALID_SESSION_ID)
2609 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2611 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2613 getpid (), fd, fd, func_str, sid, sid);
2614 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2615 (void *) optval, &optlen);
2618 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2620 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2622 getpid (), fd, fd, func_str, sid, sid);
2623 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2624 (void *) optval, &optlen);
2627 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2629 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2631 getpid (), fd, fd, func_str, sid, sid);
2632 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2633 (void *) optval, &optlen);
2636 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2638 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2639 "sid %u (0x%x), SOL_TCP",
2640 getpid (), fd, fd, func_str, sid, sid);
2641 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2642 (void *) optval, &optlen);
2646 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2647 "sid %u (0x%x), SOL_TCP, "
2648 "optname %d unsupported!",
2649 getpid (), fd, fd, func_str, sid, sid, optname);
2657 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2659 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2661 getpid (), fd, fd, func_str, sid, sid);
2662 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2663 (void *) optval, &optlen);
2667 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2668 "sid %u (0x%x), SOL_IPV6, "
2669 "optname %d unsupported!",
2670 getpid (), fd, fd, func_str, sid, sid, optname);
2678 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2680 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2682 getpid (), fd, fd, func_str, sid, sid);
2683 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2684 (void *) optval, &optlen);
2687 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2689 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2691 getpid (), fd, fd, func_str, sid, sid);
2692 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2693 (void *) optval, &optlen);
2696 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2698 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2700 getpid (), fd, fd, func_str, sid, sid);
2701 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2702 (void *) optval, &optlen);
2706 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2707 "sid %u (0x%x), SOL_SOCKET, "
2708 "optname %d unsupported!",
2709 getpid (), fd, fd, func_str, sid, sid, optname);
2717 if (rv != VPPCOM_OK)
2725 func_str = "libc_setsockopt";
2728 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2729 "optname %d, optval %p, optlen %d",
2730 getpid (), fd, fd, func_str, level, optname,
2733 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2740 int errno_val = errno;
2742 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2743 "rv %d, errno = %d", getpid (), fd, fd,
2744 func_str, rv, errno_val);
2748 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2749 getpid (), fd, fd, rv, rv);
2755 listen (int fd, int n)
2758 const char *func_str;
2759 u32 sid = ldp_sid_from_fd (fd);
2761 if ((errno = -ldp_init ()))
2764 if (sid != INVALID_SESSION_ID)
2766 func_str = "vppcom_session_listen";
2770 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2771 getpid (), fd, fd, func_str, sid, sid, n);
2773 rv = vppcom_session_listen (sid, n);
2774 if (rv != VPPCOM_OK)
2782 func_str = "libc_listen";
2785 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): n %d",
2786 getpid (), fd, fd, func_str, n);
2788 rv = libc_listen (fd, n);
2795 int errno_val = errno;
2797 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2798 "rv %d, errno = %d", getpid (), fd, fd,
2799 func_str, rv, errno_val);
2803 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2804 getpid (), fd, fd, rv, rv);
2810 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2811 socklen_t * __restrict addr_len, int flags)
2814 const char *func_str;
2815 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2818 if ((errno = -ldp_init ()))
2821 if (listen_sid != INVALID_SESSION_ID)
2824 u8 src_addr[sizeof (struct sockaddr_in6)];
2825 memset (&ep, 0, sizeof (ep));
2828 func_str = "vppcom_session_accept";
2831 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2832 "listen sid %u (0x%x), ep %p, flags 0x%x",
2833 getpid (), listen_fd, listen_fd, func_str,
2834 listen_sid, listen_sid, ep, flags);
2836 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2839 errno = -accept_sid;
2844 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2845 if (rv != VPPCOM_OK)
2847 (void) vppcom_session_close ((u32) accept_sid);
2853 func_str = "ldp_fd_from_sid";
2855 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2856 "accept sid %u (0x%x), ep %p, flags 0x%x",
2857 getpid (), listen_fd, listen_fd,
2858 func_str, accept_sid, accept_sid, ep, flags);
2859 rv = ldp_fd_from_sid ((u32) accept_sid);
2862 (void) vppcom_session_close ((u32) accept_sid);
2871 func_str = "libc_accept4";
2874 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2875 "addr %p, addr_len %p, flags 0x%x",
2876 getpid (), listen_fd, listen_fd, func_str,
2877 addr, addr_len, flags);
2879 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2886 int errno_val = errno;
2888 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2889 "rv %d, errno = %d", getpid (), listen_fd,
2890 listen_fd, func_str, rv, errno_val);
2894 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2895 getpid (), listen_fd, listen_fd, rv, rv);
2901 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2904 return ldp_accept4 (fd, addr, addr_len, flags);
2908 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2910 return ldp_accept4 (fd, addr, addr_len, 0);
2914 shutdown (int fd, int how)
2917 const char *func_str;
2918 u32 sid = ldp_sid_from_fd (fd);
2920 if ((errno = -ldp_init ()))
2923 if (sid != INVALID_SESSION_ID)
2925 func_str = __func__;
2927 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2933 func_str = "libc_shutdown";
2936 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2937 getpid (), fd, fd, func_str, how);
2939 rv = libc_shutdown (fd, how);
2946 int errno_val = errno;
2948 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2949 "rv %d, errno = %d", getpid (), fd, fd,
2950 func_str, rv, errno_val);
2954 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2955 getpid (), fd, fd, rv, rv);
2961 epoll_create1 (int flags)
2963 const char *func_str;
2966 if ((errno = -ldp_init ()))
2969 func_str = "vppcom_epoll_create";
2972 clib_warning ("LDP<%d>: calling %s()", getpid (), func_str);
2974 rv = vppcom_epoll_create ();
2976 if (PREDICT_FALSE (rv < 0))
2982 rv = ldp_fd_from_sid ((u32) rv);
2988 int errno_val = errno;
2990 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2991 "rv %d, errno = %d",
2992 getpid (), func_str, rv, errno_val);
2996 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
3002 epoll_create (int size)
3004 return epoll_create1 (0);
3008 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3011 const char *func_str;
3012 u32 vep_idx = ldp_sid_from_fd (epfd);
3014 if ((errno = -ldp_init ()))
3017 if (PREDICT_TRUE (vep_idx != INVALID_SESSION_ID))
3019 u32 sid = ldp_sid_from_fd (fd);
3022 clib_warning ("LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), "
3023 "sid %d (0x%x)", getpid (), epfd, epfd,
3024 vep_idx, vep_idx, sid, sid);
3026 if (sid != INVALID_SESSION_ID)
3028 func_str = "vppcom_epoll_ctl";
3031 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3032 "vep_idx %d (0x%x), op %d, sid %u (0x%x), event %p",
3033 getpid (), epfd, epfd, func_str, vep_idx, vep_idx,
3036 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3037 if (rv != VPPCOM_OK)
3046 u32 size = sizeof (epfd);
3048 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3049 libc_epfd = vppcom_session_attr (vep_idx,
3050 VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3052 clib_warning ("LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3053 "%s() returned libc_epfd %d (0x%x)",
3054 getpid (), epfd, epfd, vep_idx, vep_idx,
3055 func_str, libc_epfd, libc_epfd);
3059 func_str = "libc_epoll_create1";
3062 clib_warning ("LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3063 "calling %s(): EPOLL_CLOEXEC",
3064 getpid (), epfd, epfd, vep_idx, vep_idx,
3067 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3074 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3076 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3077 "vep_idx %d (0x%x), VPPCOM_ATTR_SET_LIBC_EPFD, "
3078 "libc_epfd %d (0x%x), size %d",
3079 getpid (), epfd, epfd, func_str,
3080 vep_idx, vep_idx, libc_epfd, libc_epfd, size);
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";
3101 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3102 "libc_epfd %d (0x%x), op %d, "
3103 "fd %d (0x%x), event %p",
3104 getpid (), epfd, epfd, func_str,
3105 libc_epfd, libc_epfd, op, fd, fd, event);
3107 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3112 /* The LDP epoll_create1 always creates VCL epfd's.
3113 * The app should never have a kernel base epoll fd unless it
3114 * was acquired outside of the LD_PRELOAD process context.
3115 * In any case, if we get one, punt it to libc_epoll_ctl.
3117 func_str = "libc_epoll_ctl";
3120 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3121 "op %d, fd %d (0x%x), event %p",
3122 getpid (), epfd, epfd, func_str, op, fd, fd, event);
3124 rv = libc_epoll_ctl (epfd, op, fd, event);
3132 int errno_val = errno;
3134 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3135 "rv %d, errno = %d", getpid (), fd, fd,
3136 func_str, rv, errno_val);
3140 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3141 getpid (), fd, fd, rv, rv);
3147 ldp_epoll_pwait (int epfd, struct epoll_event *events,
3148 int maxevents, int timeout, const sigset_t * sigmask)
3150 const char *func_str;
3152 double time_to_wait = (double) 0;
3153 double time_out, now = 0;
3154 u32 vep_idx = ldp_sid_from_fd (epfd);
3157 if ((errno = -ldp_init ()))
3160 if (PREDICT_FALSE (!events || (timeout < -1)))
3166 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3168 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3169 getpid (), epfd, epfd, vep_idx, vep_idx);
3174 time_to_wait = ((timeout >= 0) ? (double) timeout / (double) 1000 : 0);
3175 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3177 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3178 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3179 if (PREDICT_FALSE (libc_epfd < 0))
3187 clib_warning ("LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), "
3188 "libc_epfd %d (0x%x), events %p, maxevents %d, "
3189 "timeout %d, sigmask %p: time_to_wait %.02f",
3190 getpid (), epfd, epfd, vep_idx, vep_idx,
3191 libc_epfd, libc_epfd, events, maxevents, timeout,
3192 sigmask, time_to_wait, time_out);
3195 if (!ldp->epoll_wait_vcl)
3197 func_str = "vppcom_epoll_wait";
3200 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3201 "vep_idx %d (0x%x), events %p, maxevents %d",
3202 getpid (), epfd, epfd, func_str,
3203 vep_idx, vep_idx, events, maxevents);
3205 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3208 ldp->epoll_wait_vcl = 1;
3219 ldp->epoll_wait_vcl = 0;
3223 func_str = "libc_epoll_pwait";
3226 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3227 "libc_epfd %d (0x%x), events %p, "
3228 "maxevents %d, sigmask %p",
3229 getpid (), epfd, epfd, func_str,
3230 libc_epfd, libc_epfd, events, maxevents, sigmask);
3232 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3238 now = clib_time_now (&ldp->clib_time);
3240 while (now < time_out);
3249 int errno_val = errno;
3251 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3252 "rv %d, errno = %d", getpid (), epfd, epfd,
3253 func_str, rv, errno_val);
3257 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3258 getpid (), epfd, epfd, rv, rv);
3264 epoll_pwait (int epfd, struct epoll_event *events,
3265 int maxevents, int timeout, const sigset_t * sigmask)
3267 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3271 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3273 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3277 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3279 const char *func_str = __func__;
3280 int rv, i, n_libc_fds, n_revents;
3283 double wait_for_time;
3286 clib_warning ("LDP<%d>: fds %p, nfds %d, timeout %d",
3287 getpid (), fds, nfds, timeout);
3290 wait_for_time = (f64) timeout / 1000;
3295 for (i = 0; i < nfds; i++)
3300 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), .events = 0x%x, "
3301 ".revents = 0x%x", getpid (), i, fds[i].fd,
3302 fds[i].fd, fds[i].events, fds[i].revents);
3304 sid = ldp_sid_from_fd (fds[i].fd);
3305 if (sid != INVALID_SESSION_ID)
3307 fds[i].fd = -fds[i].fd;
3308 vec_add2 (ldp->vcl_poll, vp, 1);
3311 vp->events = fds[i].events;
3312 #ifdef __USE_XOPEN2K
3313 if (fds[i].events & POLLRDNORM)
3314 vp->events |= POLLIN;
3315 if (fds[i].events & POLLWRNORM)
3316 vp->events |= POLLOUT;
3318 vp->revents = &fds[i].revents;
3328 if (vec_len (ldp->vcl_poll))
3330 func_str = "vppcom_poll";
3333 clib_warning ("LDP<%d>: calling %s(): "
3334 "vcl_poll %p, n_sids %u (0x%x): "
3336 getpid (), func_str, ldp->vcl_poll,
3337 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3340 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3353 func_str = "libc_poll";
3356 clib_warning ("LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3357 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3359 rv = libc_poll (fds, nfds, 0);
3372 while ((wait_for_time == -1) ||
3373 (clib_time_now (&ldp->clib_time) < wait_for_time));
3377 vec_foreach (vp, ldp->vcl_poll)
3379 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3380 #ifdef __USE_XOPEN2K
3381 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3382 (fds[vp->fds_ndx].events & POLLRDNORM))
3383 fds[vp->fds_ndx].revents |= POLLRDNORM;
3384 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3385 (fds[vp->fds_ndx].events & POLLWRNORM))
3386 fds[vp->fds_ndx].revents |= POLLWRNORM;
3389 vec_reset_length (ldp->vcl_poll);
3395 int errno_val = errno;
3397 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3398 "rv %d, errno = %d", getpid (),
3399 func_str, rv, errno_val);
3404 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3405 "n_libc_fds %d", getpid (), rv, rv,
3406 vec_len (ldp->vcl_poll), n_libc_fds);
3408 for (i = 0; i < nfds; i++)
3413 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3414 ".events = 0x%x, .revents = 0x%x",
3415 getpid (), i, fds[i].fd, fds[i].fd,
3416 fds[i].events, fds[i].revents);
3427 ppoll (struct pollfd *fds, nfds_t nfds,
3428 const struct timespec *timeout, const sigset_t * sigmask)
3430 if ((errno = -ldp_init ()))
3433 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3441 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3443 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3446 * This function is called when the library is loaded
3449 ldp_constructor (void)
3451 swrap_constructor ();
3452 if (ldp_init () != 0)
3453 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3455 else if (LDP_DEBUG > 0)
3456 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3460 * This function is called when the library is unloaded
3463 ldp_destructor (void)
3465 swrap_destructor ();
3468 vppcom_app_destroy ();
3472 /* Don't use clib_warning() here because that calls writev()
3473 * which will call ldp_init().
3476 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3477 __func__, __LINE__, getpid ());
3482 * fd.io coding-style-patch-verification: ON
3485 * eval: (c-set-style "gnu")