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;
70 u8 vcl_needs_real_epoll; /*< vcl needs next epoll_create to
75 #define LDP_DEBUG ldp->debug
77 #define LDBG(_lvl, _fmt, _args...) \
78 if (ldp->debug > _lvl) \
79 clib_warning (_fmt, ##_args)
81 static ldp_main_t ldp_main = {
82 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
83 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
84 .debug = LDP_DEBUG_INIT,
87 static ldp_main_t *ldp = &ldp_main;
90 * RETURN: 0 on success or -1 on error.
93 ldp_set_app_name (char *app_name)
95 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
96 "ldp-%d-%s", getpid (), app_name);
98 if (rv >= LDP_APP_NAME_MAX)
99 app_name[LDP_APP_NAME_MAX - 1] = 0;
105 if (ldp->app_name[0] == '\0')
106 ldp_set_app_name ("app");
108 return ldp->app_name;
112 ldp_fd_from_sid (u32 sid)
114 if (PREDICT_FALSE (sid >= ldp->sid_bit_val))
117 return (sid | ldp->sid_bit_val);
121 ldp_fd_is_sid (int fd)
123 return ((u32) fd & ldp->sid_bit_val) ? 1 : 0;
127 ldp_sid_from_fd (int fd)
129 return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) :
138 if (PREDICT_TRUE (ldp->init))
142 ldp->vcl_needs_real_epoll = 1;
143 rv = vppcom_app_create (ldp_get_app_name ());
146 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
147 " failed! rv = %d (%s)\n",
148 getpid (), rv, vppcom_retval_str (rv));
152 ldp->vcl_needs_real_epoll = 0;
154 char *env_var_str = getenv (LDP_ENV_DEBUG);
158 if (sscanf (env_var_str, "%u", &tmp) != 1)
159 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
160 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
165 LDBG (0, "LDP<%d>: configured LDP debug level (%u) from env var "
166 LDP_ENV_DEBUG "!", getpid (), ldp->debug);
170 env_var_str = getenv (LDP_ENV_APP_NAME);
173 ldp_set_app_name (env_var_str);
174 LDBG (0, "LDP<%d>: configured LDP app name (%s) from the env var "
175 LDP_ENV_APP_NAME "!", getpid (), ldp->app_name);
178 env_var_str = getenv (LDP_ENV_SID_BIT);
182 if (sscanf (env_var_str, "%u", &sb) != 1)
184 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
185 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
186 "value %d (0x%x)", getpid (), env_var_str,
187 ldp->sid_bit_val, ldp->sid_bit_val);
189 else if (sb < LDP_SID_BIT_MIN)
191 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
192 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
194 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
195 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
196 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
197 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
198 ldp->sid_bit_val, ldp->sid_bit_val);
200 else if (sb > LDP_SID_BIT_MAX)
202 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
203 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
205 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
206 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
207 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
208 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
209 ldp->sid_bit_val, ldp->sid_bit_val);
213 ldp->sid_bit_val = (1 << sb);
214 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
216 LDBG (0, "LDP<%d>: configured LDP sid bit (%u) from "
217 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", getpid (), sb,
218 ldp->sid_bit_val, ldp->sid_bit_val);
222 clib_time_init (&ldp->clib_time);
223 LDBG (0, "LDP<%d>: LDP initialization: done!", getpid ());
232 const char *func_str;
233 u32 sid = ldp_sid_from_fd (fd);
235 if ((errno = -ldp_init ()))
238 if (sid != INVALID_SESSION_ID)
242 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
243 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
246 func_str = "libc_close";
250 ("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";
273 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
274 getpid (), fd, fd, func_str, sid, sid);
276 rv = vppcom_session_close (sid);
285 func_str = "libc_close";
288 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s()",
289 getpid (), fd, fd, func_str);
291 rv = libc_close (fd);
299 int errno_val = errno;
301 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
302 "rv %d, errno = %d", getpid (), fd, fd,
303 func_str, rv, errno_val);
307 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
308 getpid (), fd, fd, rv, rv);
314 read (int fd, void *buf, size_t nbytes)
317 const char *func_str;
318 u32 sid = ldp_sid_from_fd (fd);
320 if ((errno = -ldp_init ()))
323 if (sid != INVALID_SESSION_ID)
325 func_str = "vppcom_session_read";
328 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
329 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
330 fd, fd, func_str, sid, sid, buf, nbytes);
332 size = vppcom_session_read (sid, buf, nbytes);
341 func_str = "libc_read";
344 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
345 "buf %p, nbytes %u", getpid (),
346 fd, fd, func_str, buf, nbytes);
348 size = libc_read (fd, buf, nbytes);
355 int errno_val = errno;
357 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
358 "rv %d, errno = %d", getpid (), fd, fd,
359 func_str, size, errno_val);
363 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
364 getpid (), fd, fd, size, size);
370 readv (int fd, const struct iovec * iov, int iovcnt)
372 const char *func_str;
374 u32 sid = ldp_sid_from_fd (fd);
375 int rv = 0, i, total = 0;
377 if ((errno = -ldp_init ()))
380 if (sid != INVALID_SESSION_ID)
382 func_str = "vppcom_session_read";
385 for (i = 0; i < iovcnt; ++i)
388 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
389 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
390 getpid (), fd, fd, func_str, i, sid, sid,
393 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
399 if (rv < iov[i].iov_len)
402 clib_warning ("LDP<%d>: fd %d (0x%x): "
403 "rv (%d) < iov[%d].iov_len (%d)",
404 getpid (), fd, fd, rv, i,
411 while ((rv >= 0) && (total == 0));
423 func_str = "libc_readv";
426 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
427 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
429 size = libc_readv (fd, iov, iovcnt);
436 int errno_val = errno;
438 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
439 "rv %d, errno = %d", getpid (), fd, fd,
440 func_str, size, errno_val);
444 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
445 getpid (), fd, fd, size, size);
451 write (int fd, const void *buf, size_t nbytes)
453 const char *func_str;
455 u32 sid = ldp_sid_from_fd (fd);
457 if ((errno = -ldp_init ()))
460 if (sid != INVALID_SESSION_ID)
462 func_str = "vppcom_session_write";
465 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
466 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
467 fd, fd, func_str, sid, sid, buf, nbytes);
469 size = vppcom_session_write (sid, (void *) buf, nbytes);
478 func_str = "libc_write";
481 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
482 "buf %p, nbytes %u", getpid (),
483 fd, fd, func_str, buf, nbytes);
485 size = libc_write (fd, buf, nbytes);
492 int errno_val = errno;
494 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
495 "rv %d, errno = %d", getpid (), fd, fd,
496 func_str, size, errno_val);
500 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
501 getpid (), fd, fd, size, size);
507 writev (int fd, const struct iovec * iov, int iovcnt)
509 const char *func_str;
510 ssize_t size = 0, total = 0;
511 u32 sid = ldp_sid_from_fd (fd);
515 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
518 if ((errno = -ldp_init ()))
521 if (sid != INVALID_SESSION_ID)
523 func_str = "vppcom_session_write";
526 for (i = 0; i < iovcnt; ++i)
529 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
530 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
531 __func__, __LINE__, getpid (), fd, fd, func_str,
532 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
534 rv = vppcom_session_write (sid, iov[i].iov_base,
541 if (rv < iov[i].iov_len)
544 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
545 "rv (%d) < iov[%d].iov_len (%ld)",
546 __func__, __LINE__, getpid (), fd, fd,
547 rv, i, iov[i].iov_len);
553 while ((rv >= 0) && (total == 0));
565 func_str = "libc_writev";
568 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
569 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
570 fd, fd, func_str, iov, iovcnt);
572 size = libc_writev (fd, iov, iovcnt);
579 int errno_val = errno;
582 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
583 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
584 fd, func_str, size, errno_val);
588 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
589 __func__, __LINE__, getpid (), fd, fd, size);
595 fcntl (int fd, int cmd, ...)
597 const char *func_str = __func__;
600 u32 sid = ldp_sid_from_fd (fd);
602 if ((errno = -ldp_init ()))
606 if (sid != INVALID_SESSION_ID)
608 int flags = va_arg (ap, int);
611 size = sizeof (flags);
616 func_str = "vppcom_session_attr[SET_FLAGS]";
618 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
619 "sid %u (0x%x) flags %d (0x%x), size %d",
620 getpid (), fd, fd, func_str, sid, sid,
624 vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
628 func_str = "vppcom_session_attr[GET_FLAGS]";
631 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
632 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
633 sid, flags, flags, size);
636 vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
640 clib_warning ("LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): "
641 "%s() returned flags %d (0x%x)",
642 getpid (), fd, fd, cmd, func_str, flags, flags);
659 func_str = "libc_vfcntl";
662 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
663 getpid (), fd, fd, func_str, cmd);
665 rv = libc_vfcntl (fd, cmd, ap);
674 int errno_val = errno;
676 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
677 "rv %d, errno = %d", getpid (), fd, fd,
678 func_str, rv, errno_val);
682 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
683 getpid (), fd, fd, rv, rv);
689 ioctl (int fd, unsigned long int cmd, ...)
691 const char *func_str;
694 u32 sid = ldp_sid_from_fd (fd);
696 if ((errno = -ldp_init ()))
700 if (sid != INVALID_SESSION_ID)
702 func_str = "vppcom_session_attr[GET_NREAD]";
709 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
710 getpid (), fd, fd, func_str, sid, sid);
712 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
717 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
718 u32 size = sizeof (flags);
720 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
721 * non-blocking, the flags should be read here and merged
725 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
726 "sid %u (0x%x), flags %d (0x%x), size %d",
727 getpid (), fd, fd, func_str, sid, sid,
730 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
747 func_str = "libc_vioctl";
750 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
751 getpid (), fd, fd, func_str, cmd);
753 rv = libc_vioctl (fd, cmd, ap);
760 int errno_val = errno;
762 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
763 "rv %d, errno = %d", getpid (), fd, fd,
764 func_str, rv, errno_val);
768 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
769 getpid (), fd, fd, rv, rv);
776 ldp_pselect (int nfds, fd_set * __restrict readfds,
777 fd_set * __restrict writefds,
778 fd_set * __restrict exceptfds,
779 const struct timespec *__restrict timeout,
780 const __sigset_t * __restrict sigmask)
783 char *func_str = "##";
786 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
787 u32 minbits = clib_max (nfds, BITS (uword));
798 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
799 (f64) 0 : (f64) timeout->tv_sec +
800 (f64) timeout->tv_nsec / (f64) 1000000000;
802 /* select as fine grained sleep */
806 clib_warning ("LDP<%d>: sleeping for %.02f seconds",
807 getpid (), time_out);
809 time_out += clib_time_now (&ldp->clib_time);
810 while (clib_time_now (&ldp->clib_time) < time_out)
824 if (nfds <= ldp->sid_bit_val)
826 func_str = "libc_pselect";
830 ("LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
831 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
832 readfds, writefds, exceptfds, timeout, sigmask);
834 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
839 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
841 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
842 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
843 ldp->sid_bit_val, ldp->sid_bit_val,
844 FD_SETSIZE / 2, FD_SETSIZE / 2);
849 sid_bits = libc_bits = 0;
852 clib_bitmap_validate (ldp->sid_rd_bitmap, minbits);
853 clib_bitmap_validate (ldp->libc_rd_bitmap, minbits);
854 clib_bitmap_validate (ldp->rd_bitmap, minbits);
855 clib_memcpy (ldp->rd_bitmap, readfds,
856 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
860 clib_bitmap_foreach (fd, ldp->rd_bitmap,
862 sid = ldp_sid_from_fd (fd);
864 clib_warning ("LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
865 getpid (), fd, fd, sid, sid);
866 if (sid == INVALID_SESSION_ID)
867 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
869 clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1);
873 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
874 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
876 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
877 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
880 clib_warning ("LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
881 "libc_bits_set %d, libc_bits %d", getpid (),
882 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
886 clib_bitmap_validate (ldp->sid_wr_bitmap, minbits);
887 clib_bitmap_validate (ldp->libc_wr_bitmap, minbits);
888 clib_bitmap_validate (ldp->wr_bitmap, minbits);
889 clib_memcpy (ldp->wr_bitmap, writefds,
890 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
894 clib_bitmap_foreach (fd, ldp->wr_bitmap,
896 sid = ldp_sid_from_fd (fd);
898 clib_warning ("LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
899 getpid (), fd, fd, sid, sid);
900 if (sid == INVALID_SESSION_ID)
901 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
903 clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1);
907 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
908 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
910 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
911 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
914 clib_warning ("LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
915 "libc_bits_set %d, libc_bits %d", getpid (),
916 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
920 clib_bitmap_validate (ldp->sid_ex_bitmap, minbits);
921 clib_bitmap_validate (ldp->libc_ex_bitmap, minbits);
922 clib_bitmap_validate (ldp->ex_bitmap, minbits);
923 clib_memcpy (ldp->ex_bitmap, exceptfds,
924 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
928 clib_bitmap_foreach (fd, ldp->ex_bitmap,
930 sid = ldp_sid_from_fd (fd);
932 clib_warning ("LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
933 getpid (), fd, fd, sid, sid);
934 if (sid == INVALID_SESSION_ID)
935 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
937 clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1);
941 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
942 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
944 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
945 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
948 clib_warning ("LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
949 "libc_bits_set %d, libc_bits %d", getpid (),
950 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
953 if (PREDICT_FALSE (!sid_bits && !libc_bits))
964 if (!ldp->select_vcl)
966 func_str = "vppcom_select";
969 clib_memcpy (ldp->rd_bitmap, ldp->sid_rd_bitmap,
970 vec_len (ldp->rd_bitmap) *
971 sizeof (clib_bitmap_t));
973 clib_memcpy (ldp->wr_bitmap, ldp->sid_wr_bitmap,
974 vec_len (ldp->wr_bitmap) *
975 sizeof (clib_bitmap_t));
977 clib_memcpy (ldp->ex_bitmap, ldp->sid_ex_bitmap,
978 vec_len (ldp->ex_bitmap) *
979 sizeof (clib_bitmap_t));
981 rv = vppcom_select (sid_bits,
982 readfds ? ldp->rd_bitmap : NULL,
983 writefds ? ldp->wr_bitmap : NULL,
984 exceptfds ? ldp->ex_bitmap : NULL, 0);
995 clib_bitmap_foreach (sid, ldp->rd_bitmap,
997 fd = ldp_fd_from_sid (sid);
998 if (PREDICT_FALSE (fd < 0))
1004 FD_SET (fd, readfds);
1011 clib_bitmap_foreach (sid, ldp->wr_bitmap,
1013 fd = ldp_fd_from_sid (sid);
1014 if (PREDICT_FALSE (fd < 0))
1020 FD_SET (fd, writefds);
1027 clib_bitmap_foreach (sid, ldp->ex_bitmap,
1029 fd = ldp_fd_from_sid (sid);
1030 if (PREDICT_FALSE (fd < 0))
1036 FD_SET (fd, exceptfds);
1040 ldp->select_vcl = 1;
1045 ldp->select_vcl = 0;
1049 struct timespec tspec;
1051 func_str = "libc_pselect";
1054 clib_memcpy (readfds, ldp->libc_rd_bitmap,
1055 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
1057 clib_memcpy (writefds, ldp->libc_wr_bitmap,
1058 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
1060 clib_memcpy (exceptfds, ldp->libc_ex_bitmap,
1061 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
1062 tspec.tv_sec = tspec.tv_nsec = 0;
1063 rv = libc_pselect (libc_bits,
1064 readfds ? readfds : NULL,
1065 writefds ? writefds : NULL,
1066 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1071 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1075 /* TBD: set timeout to amount of time left */
1076 vec_reset_length (ldp->rd_bitmap);
1077 vec_reset_length (ldp->sid_rd_bitmap);
1078 vec_reset_length (ldp->libc_rd_bitmap);
1079 vec_reset_length (ldp->wr_bitmap);
1080 vec_reset_length (ldp->sid_wr_bitmap);
1081 vec_reset_length (ldp->libc_wr_bitmap);
1082 vec_reset_length (ldp->ex_bitmap);
1083 vec_reset_length (ldp->sid_ex_bitmap);
1084 vec_reset_length (ldp->libc_ex_bitmap);
1090 int errno_val = errno;
1092 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1093 "rv %d, errno = %d", getpid (),
1094 func_str, rv, errno_val);
1098 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1104 select (int nfds, fd_set * __restrict readfds,
1105 fd_set * __restrict writefds,
1106 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1108 struct timespec tspec;
1112 tspec.tv_sec = timeout->tv_sec;
1113 tspec.tv_nsec = timeout->tv_usec * 1000;
1115 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1116 timeout ? &tspec : NULL, NULL);
1119 #ifdef __USE_XOPEN2K
1121 pselect (int nfds, fd_set * __restrict readfds,
1122 fd_set * __restrict writefds,
1123 fd_set * __restrict exceptfds,
1124 const struct timespec *__restrict timeout,
1125 const __sigset_t * __restrict sigmask)
1127 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1132 socket (int domain, int type, int protocol)
1134 const char *func_str;
1136 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1137 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1139 if ((errno = -ldp_init ()))
1142 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1143 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1146 u8 proto = ((sock_type == SOCK_DGRAM) ?
1147 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1149 func_str = "vppcom_session_create";
1151 LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u",
1152 getpid (), func_str, proto, vppcom_proto_str (proto),
1155 sid = vppcom_session_create (proto, is_nonblocking);
1163 func_str = "ldp_fd_from_sid";
1164 rv = ldp_fd_from_sid (sid);
1167 (void) vppcom_session_close (sid);
1175 func_str = "libc_socket";
1177 LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str);
1179 rv = libc_socket (domain, type, protocol);
1186 int errno_val = errno;
1188 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1189 "rv %d, errno = %d",
1190 getpid (), func_str, rv, errno_val);
1194 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1200 * Create two new sockets, of type TYPE in domain DOMAIN and using
1201 * protocol PROTOCOL, which are connected to each other, and put file
1202 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1203 * one will be chosen automatically.
1204 * Returns 0 on success, -1 for errors.
1207 socketpair (int domain, int type, int protocol, int fds[2])
1209 const char *func_str;
1211 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1213 if ((errno = -ldp_init ()))
1216 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1217 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1219 func_str = __func__;
1221 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1227 func_str = "libc_socket";
1230 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1232 rv = libc_socket (domain, type, protocol);
1239 int errno_val = errno;
1241 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1242 "rv %d, errno = %d",
1243 getpid (), func_str, rv, errno_val);
1247 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1253 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1256 const char *func_str;
1257 u32 sid = ldp_sid_from_fd (fd);
1259 if ((errno = -ldp_init ()))
1262 if (sid != INVALID_SESSION_ID)
1266 func_str = "vppcom_session_bind";
1268 switch (addr->sa_family)
1271 if (len != sizeof (struct sockaddr_in))
1274 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1275 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1280 ep.is_ip4 = VPPCOM_IS_IP4;
1281 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1282 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1286 if (len != sizeof (struct sockaddr_in6))
1289 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1290 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1295 ep.is_ip4 = VPPCOM_IS_IP6;
1296 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1297 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1301 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1302 "Unsupported address family %u!",
1303 getpid (), fd, fd, sid, sid, addr->sa_family);
1304 errno = EAFNOSUPPORT;
1309 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1311 getpid (), fd, fd, func_str, sid, sid, addr, len);
1313 rv = vppcom_session_bind (sid, &ep);
1314 if (rv != VPPCOM_OK)
1322 func_str = "libc_bind";
1325 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1327 getpid (), fd, fd, func_str, addr, len);
1329 rv = libc_bind (fd, addr, len);
1337 int errno_val = errno;
1339 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1340 "rv %d, errno = %d", getpid (), fd, fd,
1341 func_str, rv, errno_val);
1345 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1346 getpid (), fd, fd, rv, rv);
1352 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1353 vppcom_endpt_t * ep)
1356 int sa_len, copy_len;
1358 if ((errno = -ldp_init ()))
1361 if (addr && len && ep)
1363 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1364 switch (addr->sa_family)
1367 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1368 if (*len > sizeof (struct sockaddr_in))
1369 *len = sizeof (struct sockaddr_in);
1370 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1371 copy_len = *len - sa_len;
1373 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1378 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1379 if (*len > sizeof (struct sockaddr_in6))
1380 *len = sizeof (struct sockaddr_in6);
1381 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1382 copy_len = *len - sa_len;
1384 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1385 __in6_u.__u6_addr8, ep->ip, copy_len);
1398 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1401 const char *func_str;
1402 u32 sid = ldp_sid_from_fd (fd);
1404 if ((errno = -ldp_init ()))
1407 if (sid != INVALID_SESSION_ID)
1410 u8 addr_buf[sizeof (struct in6_addr)];
1411 u32 size = sizeof (ep);
1414 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1417 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1419 getpid (), fd, fd, func_str, sid, sid, addr, len);
1421 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1422 if (rv != VPPCOM_OK)
1429 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1430 if (rv != VPPCOM_OK)
1439 func_str = "libc_getsockname";
1442 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1444 getpid (), fd, fd, func_str, addr, len);
1446 rv = libc_getsockname (fd, addr, len);
1453 int errno_val = errno;
1455 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1456 "rv %d, errno = %d", getpid (), fd, fd,
1457 func_str, rv, errno_val);
1461 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1462 getpid (), fd, fd, rv, rv);
1468 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1471 const char *func_str = __func__;
1472 u32 sid = ldp_sid_from_fd (fd);
1474 if ((errno = -ldp_init ()))
1479 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1480 getpid (), fd, fd, len);
1486 if (sid != INVALID_SESSION_ID)
1490 func_str = "vppcom_session_connect";
1492 switch (addr->sa_family)
1495 if (len != sizeof (struct sockaddr_in))
1498 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1499 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1504 ep.is_ip4 = VPPCOM_IS_IP4;
1505 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1506 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1510 if (len != sizeof (struct sockaddr_in6))
1513 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1514 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1519 ep.is_ip4 = VPPCOM_IS_IP6;
1520 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1521 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1525 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1526 "Unsupported address family %u!",
1527 getpid (), fd, fd, sid, sid, addr->sa_family);
1528 errno = EAFNOSUPPORT;
1533 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1535 getpid (), fd, fd, func_str, sid, sid, addr, len);
1537 rv = vppcom_session_connect (sid, &ep);
1538 if (rv != VPPCOM_OK)
1546 func_str = "libc_connect";
1549 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1551 getpid (), fd, fd, func_str, addr, len);
1553 rv = libc_connect (fd, addr, len);
1561 int errno_val = errno;
1563 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1564 "rv %d, errno = %d", getpid (), fd, fd,
1565 func_str, rv, errno_val);
1569 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1570 getpid (), fd, fd, rv, rv);
1576 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1579 const char *func_str;
1580 u32 sid = ldp_sid_from_fd (fd);
1582 if ((errno = -ldp_init ()))
1585 if (sid != INVALID_SESSION_ID)
1588 u8 addr_buf[sizeof (struct in6_addr)];
1589 u32 size = sizeof (ep);
1592 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1595 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1597 getpid (), fd, fd, func_str, sid, sid, addr, len);
1599 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1600 if (rv != VPPCOM_OK)
1607 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1608 if (rv != VPPCOM_OK)
1617 func_str = "libc_getpeername";
1620 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1622 getpid (), fd, fd, func_str, addr, len);
1624 rv = libc_getpeername (fd, addr, len);
1631 int errno_val = errno;
1633 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1634 "rv %d, errno = %d", getpid (), fd, fd,
1635 func_str, rv, errno_val);
1639 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1640 getpid (), fd, fd, rv, rv);
1646 send (int fd, const void *buf, size_t n, int flags)
1649 const char *func_str;
1650 u32 sid = ldp_sid_from_fd (fd);
1652 if ((errno = -ldp_init ()))
1655 if (sid != INVALID_SESSION_ID)
1658 func_str = "vppcom_session_sendto";
1661 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1662 "buf %p, n %u, flags 0x%x",
1663 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1665 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1666 if (size < VPPCOM_OK)
1674 func_str = "libc_send";
1677 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1678 "buf %p, n %u, flags 0x%x",
1679 getpid (), fd, fd, func_str, buf, n, flags);
1681 size = libc_send (fd, buf, n, flags);
1688 int errno_val = errno;
1690 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1691 "rv %d, errno = %d", getpid (), fd, fd,
1692 func_str, size, errno_val);
1696 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1697 getpid (), fd, fd, size, size);
1703 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1706 const char *func_str;
1707 u32 sid = ldp_sid_from_fd (out_fd);
1709 if ((errno = -ldp_init ()))
1712 if (sid != INVALID_SESSION_ID)
1715 ssize_t results = 0;
1716 size_t n_bytes_left = len;
1717 size_t bytes_to_read;
1721 u32 flags, flags_len = sizeof (flags);
1723 func_str = "vppcom_session_attr[GET_FLAGS]";
1724 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1726 if (PREDICT_FALSE (rv != VPPCOM_OK))
1728 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1729 "sid %u (0x%x), returned %d (%s)!", getpid (),
1730 out_fd, out_fd, func_str, sid, sid, rv,
1731 vppcom_retval_str (rv));
1733 vec_reset_length (ldp->io_buffer);
1741 off_t off = lseek (in_fd, *offset, SEEK_SET);
1742 if (PREDICT_FALSE (off == -1))
1746 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1747 "SEEK_SET failed: in_fd %d, offset %p, "
1748 "*offset %ld, rv %ld, errno %d", getpid (),
1749 out_fd, out_fd, in_fd, offset, *offset, off,
1756 ASSERT (off == *offset);
1761 func_str = "vppcom_session_attr[GET_NWRITE]";
1762 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1766 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1767 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1768 sid, sid, size, vppcom_retval_str (size));
1769 vec_reset_length (ldp->io_buffer);
1775 bytes_to_read = size;
1778 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1779 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1780 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1783 if (bytes_to_read == 0)
1785 if (flags & O_NONBLOCK)
1790 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1792 getpid (), out_fd, out_fd, sid, sid);
1800 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1801 vec_validate (ldp->io_buffer, bytes_to_read);
1802 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1805 func_str = "libc_read";
1807 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1808 "io_buffer %p, bytes_to_read %lu, rv %d, "
1809 "errno %d", getpid (), out_fd, out_fd, func_str,
1810 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1816 vec_reset_length (ldp->io_buffer);
1822 func_str = "vppcom_session_write";
1825 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1826 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1827 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1828 results, n_bytes_left);
1830 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1833 if (size == VPPCOM_EAGAIN)
1835 if (flags & O_NONBLOCK)
1841 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1842 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1852 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1853 "sid %u, io_buffer %p, nbytes %u "
1855 getpid (), out_fd, out_fd, func_str,
1856 sid, ldp->io_buffer, nbytes,
1857 size, vppcom_retval_str (size));
1861 vec_reset_length (ldp->io_buffer);
1870 ASSERT (n_bytes_left >= nbytes);
1871 n_bytes_left = n_bytes_left - nbytes;
1873 while (n_bytes_left > 0);
1876 vec_reset_length (ldp->io_buffer);
1879 off_t off = lseek (in_fd, *offset, SEEK_SET);
1880 if (PREDICT_FALSE (off == -1))
1884 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1885 "in_fd %d, offset %p, *offset %ld, "
1886 "rv %ld, errno %d", getpid (), in_fd,
1887 offset, *offset, off, errno_val);
1893 ASSERT (off == *offset);
1894 *offset += results + 1;
1906 func_str = "libc_send";
1909 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1910 "in_fd %d, offset %p, len %u",
1911 getpid (), out_fd, out_fd, func_str,
1912 in_fd, offset, len);
1914 size = libc_sendfile (out_fd, in_fd, offset, len);
1922 int errno_val = errno;
1924 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1925 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1926 func_str, size, errno_val);
1930 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1931 getpid (), out_fd, out_fd, size, size);
1937 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1939 return sendfile (out_fd, in_fd, offset, len);
1943 recv (int fd, void *buf, size_t n, int flags)
1946 const char *func_str;
1947 u32 sid = ldp_sid_from_fd (fd);
1949 if ((errno = -ldp_init ()))
1952 if (sid != INVALID_SESSION_ID)
1954 func_str = "vppcom_session_recvfrom";
1957 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1958 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1959 fd, fd, func_str, sid, sid, buf, n, flags);
1961 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1970 func_str = "libc_recv";
1973 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1974 "buf %p, n %u, flags 0x%x", getpid (),
1975 fd, fd, func_str, buf, n, flags);
1977 size = libc_recv (fd, buf, n, flags);
1984 int errno_val = errno;
1986 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1987 "rv %d, errno = %d", getpid (), fd, fd,
1988 func_str, size, errno_val);
1992 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1993 getpid (), fd, fd, size, size);
1999 sendto (int fd, const void *buf, size_t n, int flags,
2000 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2003 const char *func_str = __func__;
2004 u32 sid = ldp_sid_from_fd (fd);
2006 if ((errno = -ldp_init ()))
2009 if (sid != INVALID_SESSION_ID)
2011 vppcom_endpt_t *ep = 0;
2017 switch (addr->sa_family)
2020 ep->is_ip4 = VPPCOM_IS_IP4;
2022 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2024 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2028 ep->is_ip4 = VPPCOM_IS_IP6;
2030 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2032 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2036 errno = EAFNOSUPPORT;
2042 func_str = "vppcom_session_sendto";
2045 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2046 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2047 getpid (), fd, fd, func_str, sid, sid, buf, n,
2050 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2059 func_str = "libc_sendto";
2062 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2063 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2064 getpid (), fd, fd, func_str, buf, n, flags,
2067 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2075 int errno_val = errno;
2077 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2078 "rv %d, errno = %d", getpid (), fd, fd,
2079 func_str, size, errno_val);
2083 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2084 getpid (), fd, fd, size, size);
2090 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2091 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2094 const char *func_str;
2095 u32 sid = ldp_sid_from_fd (fd);
2097 if ((errno = -ldp_init ()))
2100 if (sid != INVALID_SESSION_ID)
2103 u8 src_addr[sizeof (struct sockaddr_in6)];
2105 func_str = "vppcom_session_recvfrom";
2108 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2109 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2110 getpid (), fd, fd, func_str, sid, sid, buf, n,
2115 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2118 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2121 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2131 func_str = "libc_recvfrom";
2134 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2135 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2136 getpid (), fd, fd, func_str, buf, n, flags,
2139 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2146 int errno_val = errno;
2148 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2149 "rv %d, errno = %d", getpid (), fd, fd,
2150 func_str, size, errno_val);
2154 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2155 getpid (), fd, fd, size, size);
2161 sendmsg (int fd, const struct msghdr * message, int flags)
2164 const char *func_str;
2165 u32 sid = ldp_sid_from_fd (fd);
2167 if ((errno = -ldp_init ()))
2170 if (sid != INVALID_SESSION_ID)
2172 func_str = __func__;
2174 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2180 func_str = "libc_sendmsg";
2183 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2184 "message %p, flags 0x%x",
2185 getpid (), fd, fd, func_str, message, flags);
2187 size = libc_sendmsg (fd, message, flags);
2194 int errno_val = errno;
2196 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2197 "rv %d, errno = %d", getpid (), fd, fd,
2198 func_str, size, errno_val);
2202 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2203 getpid (), fd, fd, size, size);
2210 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2213 const char *func_str;
2214 u32 sid = ldp_sid_from_fd (fd);
2216 if ((errno = -ldp_init ()))
2219 if (sid != INVALID_SESSION_ID)
2221 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2227 func_str = "libc_sendmmsg";
2230 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2231 "vmessages %p, vlen %u, flags 0x%x",
2232 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2234 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2241 int errno_val = errno;
2243 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2244 "rv %d, errno = %d", getpid (), fd, fd,
2245 func_str, size, errno_val);
2249 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2250 getpid (), fd, fd, size, size);
2257 recvmsg (int fd, struct msghdr * message, int flags)
2260 const char *func_str;
2261 u32 sid = ldp_sid_from_fd (fd);
2263 if ((errno = -ldp_init ()))
2266 if (sid != INVALID_SESSION_ID)
2268 func_str = __func__;
2270 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2276 func_str = "libc_recvmsg";
2279 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2280 "message %p, flags 0x%x",
2281 getpid (), fd, fd, func_str, message, flags);
2283 size = libc_recvmsg (fd, message, flags);
2290 int errno_val = errno;
2292 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2293 "rv %d, errno = %d", getpid (), fd, fd,
2294 func_str, size, errno_val);
2298 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2299 getpid (), fd, fd, size, size);
2306 recvmmsg (int fd, struct mmsghdr *vmessages,
2307 unsigned int vlen, int flags, struct timespec *tmo)
2310 const char *func_str;
2311 u32 sid = ldp_sid_from_fd (fd);
2313 if ((errno = -ldp_init ()))
2316 if (sid != INVALID_SESSION_ID)
2318 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2324 func_str = "libc_recvmmsg";
2327 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2328 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2329 getpid (), fd, fd, func_str, vmessages, vlen,
2332 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2339 int errno_val = errno;
2341 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2342 "rv %d, errno = %d", getpid (), fd, fd,
2343 func_str, size, errno_val);
2347 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2348 getpid (), fd, fd, size, size);
2355 getsockopt (int fd, int level, int optname,
2356 void *__restrict optval, socklen_t * __restrict optlen)
2359 const char *func_str = __func__;
2360 u32 sid = ldp_sid_from_fd (fd);
2361 u32 buflen = optlen ? (u32) * optlen : 0;
2363 if ((errno = -ldp_init ()))
2366 if (sid != INVALID_SESSION_ID)
2376 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2378 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2380 getpid (), fd, fd, func_str, sid, sid);
2381 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2385 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2387 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2389 getpid (), fd, fd, func_str, sid, sid);
2390 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2394 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2396 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2398 getpid (), fd, fd, func_str, sid, sid);
2399 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2403 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2405 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2406 "sid %u (0x%x), SOL_TCP",
2407 getpid (), fd, fd, func_str, sid, sid);
2408 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2412 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2415 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2416 "SOL_TCP, TCP_INFO, optval %p, "
2417 "optlen %d: #LDP-NOP#",
2418 getpid (), fd, fd, sid, sid,
2420 memset (optval, 0, *optlen);
2428 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2429 "sid %u (0x%x), SOL_TCP, "
2430 "optname %d unsupported!",
2431 getpid (), fd, fd, func_str, sid, sid, optname);
2439 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2441 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2443 getpid (), fd, fd, func_str, sid, sid);
2444 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2449 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2450 "sid %u (0x%x), SOL_IPV6, "
2451 "optname %d unsupported!",
2452 getpid (), fd, fd, func_str, sid, sid, optname);
2460 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2462 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2464 getpid (), fd, fd, func_str, sid, sid);
2465 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2469 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2471 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2473 getpid (), fd, fd, func_str, sid, sid);
2474 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2478 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2480 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2482 getpid (), fd, fd, func_str, sid, sid);
2483 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2485 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2488 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2490 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2491 "sid %u (0x%x), optlen %d",
2492 getpid (), fd, fd, func_str, sid, sid, buflen);
2493 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2497 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2499 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2500 "sid %u (0x%x), optlen %d",
2501 getpid (), fd, fd, func_str, sid, sid, buflen);
2502 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2506 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2508 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2510 getpid (), fd, fd, func_str, sid, sid);
2511 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2515 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2517 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2519 getpid (), fd, fd, func_str, sid, sid);
2520 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2524 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2526 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2528 getpid (), fd, fd, func_str, sid, sid);
2529 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2534 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2535 "sid %u (0x%x), SOL_SOCKET, "
2536 "optname %d unsupported!",
2537 getpid (), fd, fd, func_str, sid, sid, optname);
2545 if (rv != VPPCOM_OK)
2553 func_str = "libc_getsockopt";
2556 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2557 "optname %d, optval %p, optlen %d",
2558 getpid (), fd, fd, func_str, level, optname,
2561 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2568 int errno_val = errno;
2570 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2571 "rv %d, errno = %d", getpid (), fd, fd,
2572 func_str, rv, errno_val);
2576 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2577 getpid (), fd, fd, rv, rv);
2583 setsockopt (int fd, int level, int optname,
2584 const void *optval, socklen_t optlen)
2587 const char *func_str = __func__;
2588 u32 sid = ldp_sid_from_fd (fd);
2590 if ((errno = -ldp_init ()))
2593 if (sid != INVALID_SESSION_ID)
2603 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2605 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2607 getpid (), fd, fd, func_str, sid, sid);
2608 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2609 (void *) optval, &optlen);
2612 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2614 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2616 getpid (), fd, fd, func_str, sid, sid);
2617 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2618 (void *) optval, &optlen);
2621 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2623 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2625 getpid (), fd, fd, func_str, sid, sid);
2626 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2627 (void *) optval, &optlen);
2630 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2632 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2633 "sid %u (0x%x), SOL_TCP",
2634 getpid (), fd, fd, func_str, sid, sid);
2635 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2636 (void *) optval, &optlen);
2640 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2641 "sid %u (0x%x), SOL_TCP, "
2642 "optname %d unsupported!",
2643 getpid (), fd, fd, func_str, sid, sid, optname);
2651 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2653 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2655 getpid (), fd, fd, func_str, sid, sid);
2656 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2657 (void *) optval, &optlen);
2661 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2662 "sid %u (0x%x), SOL_IPV6, "
2663 "optname %d unsupported!",
2664 getpid (), fd, fd, func_str, sid, sid, optname);
2672 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2674 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2676 getpid (), fd, fd, func_str, sid, sid);
2677 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2678 (void *) optval, &optlen);
2681 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2683 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2685 getpid (), fd, fd, func_str, sid, sid);
2686 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2687 (void *) optval, &optlen);
2690 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2692 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2694 getpid (), fd, fd, func_str, sid, sid);
2695 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2696 (void *) optval, &optlen);
2700 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2701 "sid %u (0x%x), SOL_SOCKET, "
2702 "optname %d unsupported!",
2703 getpid (), fd, fd, func_str, sid, sid, optname);
2711 if (rv != VPPCOM_OK)
2719 func_str = "libc_setsockopt";
2722 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2723 "optname %d, optval %p, optlen %d",
2724 getpid (), fd, fd, func_str, level, optname,
2727 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2734 int errno_val = errno;
2736 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2737 "rv %d, errno = %d", getpid (), fd, fd,
2738 func_str, rv, errno_val);
2742 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2743 getpid (), fd, fd, rv, rv);
2749 listen (int fd, int n)
2752 const char *func_str;
2753 u32 sid = ldp_sid_from_fd (fd);
2755 if ((errno = -ldp_init ()))
2758 if (sid != INVALID_SESSION_ID)
2760 func_str = "vppcom_session_listen";
2762 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2763 getpid (), fd, fd, func_str, sid, sid, n);
2765 rv = vppcom_session_listen (sid, n);
2766 if (rv != VPPCOM_OK)
2774 func_str = "libc_listen";
2776 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd,
2779 rv = libc_listen (fd, n);
2786 int errno_val = errno;
2788 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2789 "rv %d, errno = %d", getpid (), fd, fd,
2790 func_str, rv, errno_val);
2794 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2795 getpid (), fd, fd, rv, rv);
2801 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2802 socklen_t * __restrict addr_len, int flags)
2805 const char *func_str;
2806 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2809 if ((errno = -ldp_init ()))
2812 if (listen_sid != INVALID_SESSION_ID)
2815 u8 src_addr[sizeof (struct sockaddr_in6)];
2816 memset (&ep, 0, sizeof (ep));
2819 func_str = "vppcom_session_accept";
2822 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2823 "listen sid %u (0x%x), ep %p, flags 0x%x",
2824 getpid (), listen_fd, listen_fd, func_str,
2825 listen_sid, listen_sid, ep, flags);
2827 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2830 errno = -accept_sid;
2835 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2836 if (rv != VPPCOM_OK)
2838 (void) vppcom_session_close ((u32) accept_sid);
2844 func_str = "ldp_fd_from_sid";
2846 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2847 "accept sid %u (0x%x), ep %p, flags 0x%x",
2848 getpid (), listen_fd, listen_fd,
2849 func_str, accept_sid, accept_sid, ep, flags);
2850 rv = ldp_fd_from_sid ((u32) accept_sid);
2853 (void) vppcom_session_close ((u32) accept_sid);
2862 func_str = "libc_accept4";
2865 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2866 "addr %p, addr_len %p, flags 0x%x",
2867 getpid (), listen_fd, listen_fd, func_str,
2868 addr, addr_len, flags);
2870 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2877 int errno_val = errno;
2879 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2880 "rv %d, errno = %d", getpid (), listen_fd,
2881 listen_fd, func_str, rv, errno_val);
2885 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2886 getpid (), listen_fd, listen_fd, rv, rv);
2892 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2895 return ldp_accept4 (fd, addr, addr_len, flags);
2899 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2901 return ldp_accept4 (fd, addr, addr_len, 0);
2905 shutdown (int fd, int how)
2908 const char *func_str;
2909 u32 sid = ldp_sid_from_fd (fd);
2911 if ((errno = -ldp_init ()))
2914 if (sid != INVALID_SESSION_ID)
2916 func_str = __func__;
2918 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2924 func_str = "libc_shutdown";
2927 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2928 getpid (), fd, fd, func_str, how);
2930 rv = libc_shutdown (fd, how);
2937 int errno_val = errno;
2939 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2940 "rv %d, errno = %d", getpid (), fd, fd,
2941 func_str, rv, errno_val);
2945 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2946 getpid (), fd, fd, rv, rv);
2952 epoll_create1 (int flags)
2954 const char *func_str;
2957 if ((errno = -ldp_init ()))
2960 if (ldp->vcl_needs_real_epoll)
2962 rv = libc_epoll_create1 (flags);
2963 ldp->vcl_needs_real_epoll = 0;
2964 ldp->vcl_mq_epfd = rv;
2965 LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv);
2968 func_str = "vppcom_epoll_create";
2970 LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str);
2972 rv = vppcom_epoll_create ();
2974 if (PREDICT_FALSE (rv < 0))
2980 rv = ldp_fd_from_sid ((u32) rv);
2986 int errno_val = errno;
2988 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2989 "rv %d, errno = %d",
2990 getpid (), func_str, rv, errno_val);
2994 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
3000 epoll_create (int size)
3002 return epoll_create1 (0);
3006 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3008 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
3009 const char *func_str;
3012 if ((errno = -ldp_init ()))
3015 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3017 /* The LDP epoll_create1 always creates VCL epfd's.
3018 * The app should never have a kernel base epoll fd unless it
3019 * was acquired outside of the LD_PRELOAD process context.
3020 * In any case, if we get one, punt it to libc_epoll_ctl.
3022 func_str = "libc_epoll_ctl";
3024 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
3025 " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event);
3027 rv = libc_epoll_ctl (epfd, op, fd, event);
3031 sid = ldp_sid_from_fd (fd);
3033 LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
3034 getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid);
3036 if (sid != INVALID_SESSION_ID)
3038 func_str = "vppcom_epoll_ctl";
3040 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3041 " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd,
3042 func_str, vep_idx, vep_idx, sid, sid, event);
3044 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3045 if (rv != VPPCOM_OK)
3054 u32 size = sizeof (epfd);
3056 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3057 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
3059 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() "
3060 "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd,
3061 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
3065 func_str = "libc_epoll_create1";
3067 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3068 "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd,
3069 vep_idx, vep_idx, func_str);
3071 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3078 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3079 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3080 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3081 getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3084 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3093 else if (PREDICT_FALSE (libc_epfd < 0))
3100 func_str = "libc_epoll_ctl";
3102 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3103 "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str,
3104 libc_epfd, libc_epfd, op, fd, fd, event);
3106 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3114 int errno_val = errno;
3116 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3117 "rv %d, errno = %d", getpid (), fd, fd,
3118 func_str, rv, errno_val);
3122 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3123 getpid (), fd, fd, rv, rv);
3129 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3130 int timeout, const sigset_t * sigmask)
3132 double time_to_wait = (double) 0, time_out, now = 0;
3133 u32 vep_idx = ldp_sid_from_fd (epfd);
3134 int libc_epfd, rv = 0;
3135 const char *func_str;
3137 if ((errno = -ldp_init ()))
3140 if (PREDICT_FALSE (!events || (timeout < -1)))
3146 if (epfd == ldp->vcl_mq_epfd)
3147 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3149 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3151 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3152 getpid (), epfd, epfd, vep_idx, vep_idx);
3157 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3158 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3160 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3161 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3162 if (PREDICT_FALSE (libc_epfd < 0))
3169 LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3170 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3171 getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3172 maxevents, timeout, sigmask, time_to_wait, time_out);
3175 if (!ldp->epoll_wait_vcl)
3177 func_str = "vppcom_epoll_wait";
3179 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3180 " events %p, maxevents %d", getpid (), epfd, epfd, func_str,
3181 vep_idx, vep_idx, events, maxevents);
3183 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3186 ldp->epoll_wait_vcl = 1;
3197 ldp->epoll_wait_vcl = 0;
3201 func_str = "libc_epoll_pwait";
3203 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d "
3204 "(0x%x), events %p, maxevents %d, sigmask %p", getpid (),
3205 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3206 maxevents, sigmask);
3208 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3214 now = clib_time_now (&ldp->clib_time);
3216 while (now < time_out);
3225 int errno_val = errno;
3227 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3228 "rv %d, errno = %d", getpid (), epfd, epfd,
3229 func_str, rv, errno_val);
3233 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3234 getpid (), epfd, epfd, rv, rv);
3240 epoll_pwait (int epfd, struct epoll_event *events,
3241 int maxevents, int timeout, const sigset_t * sigmask)
3243 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3247 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3249 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3253 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3255 const char *func_str = __func__;
3256 int rv, i, n_libc_fds, n_revents;
3259 double wait_for_time;
3262 clib_warning ("LDP<%d>: fds %p, nfds %d, timeout %d",
3263 getpid (), fds, nfds, timeout);
3266 wait_for_time = (f64) timeout / 1000;
3271 for (i = 0; i < nfds; i++)
3276 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), .events = 0x%x, "
3277 ".revents = 0x%x", getpid (), i, fds[i].fd,
3278 fds[i].fd, fds[i].events, fds[i].revents);
3280 sid = ldp_sid_from_fd (fds[i].fd);
3281 if (sid != INVALID_SESSION_ID)
3283 fds[i].fd = -fds[i].fd;
3284 vec_add2 (ldp->vcl_poll, vp, 1);
3287 vp->events = fds[i].events;
3288 #ifdef __USE_XOPEN2K
3289 if (fds[i].events & POLLRDNORM)
3290 vp->events |= POLLIN;
3291 if (fds[i].events & POLLWRNORM)
3292 vp->events |= POLLOUT;
3294 vp->revents = &fds[i].revents;
3304 if (vec_len (ldp->vcl_poll))
3306 func_str = "vppcom_poll";
3309 clib_warning ("LDP<%d>: calling %s(): "
3310 "vcl_poll %p, n_sids %u (0x%x): "
3312 getpid (), func_str, ldp->vcl_poll,
3313 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3316 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3329 func_str = "libc_poll";
3332 clib_warning ("LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3333 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3335 rv = libc_poll (fds, nfds, 0);
3348 while ((wait_for_time == -1) ||
3349 (clib_time_now (&ldp->clib_time) < wait_for_time));
3353 vec_foreach (vp, ldp->vcl_poll)
3355 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3356 #ifdef __USE_XOPEN2K
3357 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3358 (fds[vp->fds_ndx].events & POLLRDNORM))
3359 fds[vp->fds_ndx].revents |= POLLRDNORM;
3360 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3361 (fds[vp->fds_ndx].events & POLLWRNORM))
3362 fds[vp->fds_ndx].revents |= POLLWRNORM;
3365 vec_reset_length (ldp->vcl_poll);
3371 int errno_val = errno;
3373 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3374 "rv %d, errno = %d", getpid (),
3375 func_str, rv, errno_val);
3380 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3381 "n_libc_fds %d", getpid (), rv, rv,
3382 vec_len (ldp->vcl_poll), n_libc_fds);
3384 for (i = 0; i < nfds; i++)
3389 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3390 ".events = 0x%x, .revents = 0x%x",
3391 getpid (), i, fds[i].fd, fds[i].fd,
3392 fds[i].events, fds[i].revents);
3403 ppoll (struct pollfd *fds, nfds_t nfds,
3404 const struct timespec *timeout, const sigset_t * sigmask)
3406 if ((errno = -ldp_init ()))
3409 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3417 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3419 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3422 * This function is called when the library is loaded
3425 ldp_constructor (void)
3427 swrap_constructor ();
3428 if (ldp_init () != 0)
3429 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3431 else if (LDP_DEBUG > 0)
3432 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3436 * This function is called when the library is unloaded
3439 ldp_destructor (void)
3441 swrap_destructor ();
3444 vppcom_app_destroy ();
3448 /* Don't use clib_warning() here because that calls writev()
3449 * which will call ldp_init().
3452 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3453 __func__, __LINE__, getpid ());
3458 * fd.io coding-style-patch-verification: ON
3461 * eval: (c-set-style "gnu")