2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <sys/resource.h>
23 #include <netinet/tcp.h>
25 #include <vcl/ldp_socket_wrapper.h>
29 #include <vcl/vppcom.h>
30 #include <vppinfra/time.h>
31 #include <vppinfra/bitmap.h>
33 #define HAVE_CONSTRUCTOR_ATTRIBUTE
34 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
35 #define CONSTRUCTOR_ATTRIBUTE \
36 __attribute__ ((constructor))
38 #define CONSTRUCTOR_ATTRIBUTE
39 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
41 #define HAVE_DESTRUCTOR_ATTRIBUTE
42 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
43 #define DESTRUCTOR_ATTRIBUTE \
44 __attribute__ ((destructor))
46 #define DESTRUCTOR_ATTRIBUTE
52 char app_name[LDP_APP_NAME_MAX];
57 clib_time_t clib_time;
58 clib_bitmap_t *rd_bitmap;
59 clib_bitmap_t *wr_bitmap;
60 clib_bitmap_t *ex_bitmap;
61 clib_bitmap_t *sid_rd_bitmap;
62 clib_bitmap_t *sid_wr_bitmap;
63 clib_bitmap_t *sid_ex_bitmap;
64 clib_bitmap_t *libc_rd_bitmap;
65 clib_bitmap_t *libc_wr_bitmap;
66 clib_bitmap_t *libc_ex_bitmap;
68 struct pollfd *libc_poll;
72 u8 vcl_needs_real_epoll; /*< vcl needs next epoll_create to
77 #define LDP_DEBUG ldp->debug
79 #define LDBG(_lvl, _fmt, _args...) \
80 if (ldp->debug > _lvl) \
81 clib_warning (_fmt, ##_args)
83 static ldp_main_t ldp_main = {
84 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
85 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
86 .debug = LDP_DEBUG_INIT,
89 static ldp_main_t *ldp = &ldp_main;
92 * RETURN: 0 on success or -1 on error.
95 ldp_set_app_name (char *app_name)
97 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
98 "ldp-%d-%s", getpid (), app_name);
100 if (rv >= LDP_APP_NAME_MAX)
101 app_name[LDP_APP_NAME_MAX - 1] = 0;
107 if (ldp->app_name[0] == '\0')
108 ldp_set_app_name ("app");
110 return ldp->app_name;
114 ldp_fd_from_sid (u32 sid)
116 if (PREDICT_FALSE (sid >= ldp->sid_bit_val))
119 return (sid | ldp->sid_bit_val);
123 ldp_fd_is_sid (int fd)
125 return ((u32) fd & ldp->sid_bit_val) ? 1 : 0;
129 ldp_sid_from_fd (int fd)
131 return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) :
140 if (PREDICT_TRUE (ldp->init))
144 ldp->vcl_needs_real_epoll = 1;
145 rv = vppcom_app_create (ldp_get_app_name ());
148 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
149 " failed! rv = %d (%s)\n",
150 getpid (), rv, vppcom_retval_str (rv));
154 ldp->vcl_needs_real_epoll = 0;
156 char *env_var_str = getenv (LDP_ENV_DEBUG);
160 if (sscanf (env_var_str, "%u", &tmp) != 1)
161 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
162 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
167 LDBG (0, "LDP<%d>: configured LDP debug level (%u) from env var "
168 LDP_ENV_DEBUG "!", getpid (), ldp->debug);
172 env_var_str = getenv (LDP_ENV_APP_NAME);
175 ldp_set_app_name (env_var_str);
176 LDBG (0, "LDP<%d>: configured LDP app name (%s) from the env var "
177 LDP_ENV_APP_NAME "!", getpid (), ldp->app_name);
180 env_var_str = getenv (LDP_ENV_SID_BIT);
184 if (sscanf (env_var_str, "%u", &sb) != 1)
186 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
187 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
188 "value %d (0x%x)", getpid (), env_var_str,
189 ldp->sid_bit_val, ldp->sid_bit_val);
191 else if (sb < LDP_SID_BIT_MIN)
193 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
194 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
196 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
197 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
198 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
199 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
200 ldp->sid_bit_val, ldp->sid_bit_val);
202 else if (sb > LDP_SID_BIT_MAX)
204 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
205 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
207 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
208 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
209 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
210 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
211 ldp->sid_bit_val, ldp->sid_bit_val);
215 ldp->sid_bit_val = (1 << sb);
216 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
218 LDBG (0, "LDP<%d>: configured LDP sid bit (%u) from "
219 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", getpid (), sb,
220 ldp->sid_bit_val, ldp->sid_bit_val);
224 clib_time_init (&ldp->clib_time);
225 LDBG (0, "LDP<%d>: LDP initialization: done!", getpid ());
234 const char *func_str;
235 u32 sid = ldp_sid_from_fd (fd);
237 if ((errno = -ldp_init ()))
240 if (sid != INVALID_SESSION_ID)
244 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
245 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
248 func_str = "libc_close";
250 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
251 getpid (), fd, fd, func_str, epfd, epfd);
253 rv = libc_close (epfd);
256 u32 size = sizeof (epfd);
259 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
263 else if (PREDICT_FALSE (epfd < 0))
270 func_str = "vppcom_session_close";
272 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
273 getpid (), fd, fd, func_str, sid, sid);
275 rv = vppcom_session_close (sid);
284 func_str = "libc_close";
286 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s()", getpid (), fd, fd,
289 rv = libc_close (fd);
297 int errno_val = errno;
299 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
300 "rv %d, errno = %d", getpid (), fd, fd,
301 func_str, rv, errno_val);
305 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
306 getpid (), fd, fd, rv, rv);
312 read (int fd, void *buf, size_t nbytes)
315 const char *func_str;
316 u32 sid = ldp_sid_from_fd (fd);
318 if ((errno = -ldp_init ()))
321 if (sid != INVALID_SESSION_ID)
323 func_str = "vppcom_session_read";
326 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
327 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
328 fd, fd, func_str, sid, sid, buf, nbytes);
330 size = vppcom_session_read (sid, buf, nbytes);
339 func_str = "libc_read";
342 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
343 "buf %p, nbytes %u", getpid (),
344 fd, fd, func_str, buf, nbytes);
346 size = libc_read (fd, buf, nbytes);
353 int errno_val = errno;
355 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
356 "rv %d, errno = %d", getpid (), fd, fd,
357 func_str, size, errno_val);
361 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
362 getpid (), fd, fd, size, size);
368 readv (int fd, const struct iovec * iov, int iovcnt)
370 const char *func_str;
372 u32 sid = ldp_sid_from_fd (fd);
373 int rv = 0, i, total = 0;
375 if ((errno = -ldp_init ()))
378 if (sid != INVALID_SESSION_ID)
380 func_str = "vppcom_session_read";
383 for (i = 0; i < iovcnt; ++i)
386 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
387 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
388 getpid (), fd, fd, func_str, i, sid, sid,
391 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
397 if (rv < iov[i].iov_len)
400 clib_warning ("LDP<%d>: fd %d (0x%x): "
401 "rv (%d) < iov[%d].iov_len (%d)",
402 getpid (), fd, fd, rv, i,
409 while ((rv >= 0) && (total == 0));
421 func_str = "libc_readv";
424 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
425 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
427 size = libc_readv (fd, iov, iovcnt);
434 int errno_val = errno;
436 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
437 "rv %d, errno = %d", getpid (), fd, fd,
438 func_str, size, errno_val);
442 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
443 getpid (), fd, fd, size, size);
449 write (int fd, const void *buf, size_t nbytes)
451 const char *func_str;
453 u32 sid = ldp_sid_from_fd (fd);
455 if ((errno = -ldp_init ()))
458 if (sid != INVALID_SESSION_ID)
460 func_str = "vppcom_session_write";
463 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
464 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
465 fd, fd, func_str, sid, sid, buf, nbytes);
467 size = vppcom_session_write (sid, (void *) buf, nbytes);
476 func_str = "libc_write";
479 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
480 "buf %p, nbytes %u", getpid (),
481 fd, fd, func_str, buf, nbytes);
483 size = libc_write (fd, buf, nbytes);
490 int errno_val = errno;
492 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
493 "rv %d, errno = %d", getpid (), fd, fd,
494 func_str, size, errno_val);
498 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
499 getpid (), fd, fd, size, size);
505 writev (int fd, const struct iovec * iov, int iovcnt)
507 const char *func_str;
508 ssize_t size = 0, total = 0;
509 u32 sid = ldp_sid_from_fd (fd);
513 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
516 if ((errno = -ldp_init ()))
519 if (sid != INVALID_SESSION_ID)
521 func_str = "vppcom_session_write";
524 for (i = 0; i < iovcnt; ++i)
527 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
528 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
529 __func__, __LINE__, getpid (), fd, fd, func_str,
530 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
532 rv = vppcom_session_write (sid, iov[i].iov_base,
539 if (rv < iov[i].iov_len)
542 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
543 "rv (%d) < iov[%d].iov_len (%ld)",
544 __func__, __LINE__, getpid (), fd, fd,
545 rv, i, iov[i].iov_len);
551 while ((rv >= 0) && (total == 0));
563 func_str = "libc_writev";
566 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
567 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
568 fd, fd, func_str, iov, iovcnt);
570 size = libc_writev (fd, iov, iovcnt);
577 int errno_val = errno;
580 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
581 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
582 fd, func_str, size, errno_val);
586 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
587 __func__, __LINE__, getpid (), fd, fd, size);
593 fcntl (int fd, int cmd, ...)
595 const char *func_str = __func__;
598 u32 sid = ldp_sid_from_fd (fd);
600 if ((errno = -ldp_init ()))
604 if (sid != INVALID_SESSION_ID)
606 int flags = va_arg (ap, int);
609 size = sizeof (flags);
614 func_str = "vppcom_session_attr[SET_FLAGS]";
615 LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
616 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
617 sid, flags, flags, size);
619 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
624 func_str = "vppcom_session_attr[GET_FLAGS]";
625 LDBG (2, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
626 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
627 sid, flags, flags, size);
629 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
633 LDBG (2, "LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): %s() "
634 "returned flags %d (0x%x)", getpid (), fd, fd, cmd,
635 func_str, flags, flags);
640 /* TODO handle this */
641 LDBG (0, "F_SETFD ignored flags %u", flags);
656 func_str = "libc_vfcntl";
659 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
660 getpid (), fd, fd, func_str, cmd);
662 rv = libc_vfcntl (fd, cmd, ap);
671 int errno_val = errno;
673 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
674 "rv %d, errno = %d", getpid (), fd, fd,
675 func_str, rv, errno_val);
679 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
680 getpid (), fd, fd, rv, rv);
686 ioctl (int fd, unsigned long int cmd, ...)
688 const char *func_str;
691 u32 sid = ldp_sid_from_fd (fd);
693 if ((errno = -ldp_init ()))
697 if (sid != INVALID_SESSION_ID)
699 func_str = "vppcom_session_attr[GET_NREAD]";
706 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
707 getpid (), fd, fd, func_str, sid, sid);
709 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
714 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
715 u32 size = sizeof (flags);
717 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
718 * non-blocking, the flags should be read here and merged
722 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
723 "sid %u (0x%x), flags %d (0x%x), size %d",
724 getpid (), fd, fd, func_str, sid, sid,
727 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
744 func_str = "libc_vioctl";
747 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
748 getpid (), fd, fd, func_str, cmd);
750 rv = libc_vioctl (fd, cmd, ap);
757 int errno_val = errno;
759 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
760 "rv %d, errno = %d", getpid (), fd, fd,
761 func_str, rv, errno_val);
765 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
766 getpid (), fd, fd, rv, rv);
773 ldp_pselect (int nfds, fd_set * __restrict readfds,
774 fd_set * __restrict writefds,
775 fd_set * __restrict exceptfds,
776 const struct timespec *__restrict timeout,
777 const __sigset_t * __restrict sigmask)
780 char *func_str = "##";
783 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
784 u32 minbits = clib_max (nfds, BITS (uword));
795 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
796 (f64) 0 : (f64) timeout->tv_sec +
797 (f64) timeout->tv_nsec / (f64) 1000000000;
799 /* select as fine grained sleep */
802 LDBG (3, "LDP<%d>: sleeping for %.02f seconds", getpid (),
805 time_out += clib_time_now (&ldp->clib_time);
806 while (clib_time_now (&ldp->clib_time) < time_out)
820 if (nfds <= ldp->sid_bit_val)
822 func_str = "libc_pselect";
824 LDBG (3, "LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
825 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
826 readfds, writefds, exceptfds, timeout, sigmask);
828 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
833 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
835 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
836 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
837 ldp->sid_bit_val, ldp->sid_bit_val,
838 FD_SETSIZE / 2, FD_SETSIZE / 2);
843 sid_bits = libc_bits = 0;
844 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
847 clib_bitmap_validate (ldp->sid_rd_bitmap, minbits);
848 clib_bitmap_validate (ldp->libc_rd_bitmap, minbits);
849 clib_bitmap_validate (ldp->rd_bitmap, minbits);
850 clib_memcpy_fast (ldp->rd_bitmap, readfds, n_bytes);
851 memset (readfds, 0, n_bytes);
854 clib_bitmap_foreach (fd, ldp->rd_bitmap, ({
857 sid = ldp_sid_from_fd (fd);
858 LDBG (3, "LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
859 getpid (), fd, fd, sid, sid);
860 if (sid == INVALID_SESSION_ID)
861 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
863 clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1);
867 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
868 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
870 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
871 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
873 LDBG (3, "LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
874 "libc_bits_set %d, libc_bits %d", getpid (), sid_bits_set,
875 sid_bits, libc_bits_set, libc_bits);
879 clib_bitmap_validate (ldp->sid_wr_bitmap, minbits);
880 clib_bitmap_validate (ldp->libc_wr_bitmap, minbits);
881 clib_bitmap_validate (ldp->wr_bitmap, minbits);
882 clib_memcpy_fast (ldp->wr_bitmap, writefds, n_bytes);
883 memset (writefds, 0, n_bytes);
886 clib_bitmap_foreach (fd, ldp->wr_bitmap, ({
889 sid = ldp_sid_from_fd (fd);
890 LDBG (3, "LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
891 getpid (), fd, fd, sid, sid);
892 if (sid == INVALID_SESSION_ID)
893 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
895 clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1);
899 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
900 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
902 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
903 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
905 LDBG (3, "LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
906 "libc_bits_set %d, libc_bits %d", getpid (),
907 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
911 clib_bitmap_validate (ldp->sid_ex_bitmap, minbits);
912 clib_bitmap_validate (ldp->libc_ex_bitmap, minbits);
913 clib_bitmap_validate (ldp->ex_bitmap, minbits);
914 clib_memcpy_fast (ldp->ex_bitmap, exceptfds, n_bytes);
915 memset (exceptfds, 0, n_bytes);
918 clib_bitmap_foreach (fd, ldp->ex_bitmap, ({
921 sid = ldp_sid_from_fd (fd);
922 LDBG (3, "LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
923 getpid (), fd, fd, sid, sid);
924 if (sid == INVALID_SESSION_ID)
925 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
927 clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1);
931 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
932 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
934 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
935 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
937 LDBG (3, "LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
938 "libc_bits_set %d, libc_bits %d", getpid (),
939 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
942 if (PREDICT_FALSE (!sid_bits && !libc_bits))
953 if (!ldp->select_vcl)
955 func_str = "vppcom_select";
958 clib_memcpy_fast (ldp->rd_bitmap, ldp->sid_rd_bitmap,
959 vec_len (ldp->rd_bitmap) *
960 sizeof (clib_bitmap_t));
962 clib_memcpy_fast (ldp->wr_bitmap, ldp->sid_wr_bitmap,
963 vec_len (ldp->wr_bitmap) *
964 sizeof (clib_bitmap_t));
966 clib_memcpy_fast (ldp->ex_bitmap, ldp->sid_ex_bitmap,
967 vec_len (ldp->ex_bitmap) *
968 sizeof (clib_bitmap_t));
970 rv = vppcom_select (sid_bits,
971 readfds ? ldp->rd_bitmap : NULL,
972 writefds ? ldp->wr_bitmap : NULL,
973 exceptfds ? ldp->ex_bitmap : NULL, 0);
984 clib_bitmap_foreach (sid, ldp->rd_bitmap,
986 fd = ldp_fd_from_sid (sid);
987 if (PREDICT_FALSE (fd < 0))
993 FD_SET (fd, readfds);
1000 clib_bitmap_foreach (sid, ldp->wr_bitmap,
1002 fd = ldp_fd_from_sid (sid);
1003 if (PREDICT_FALSE (fd < 0))
1009 FD_SET (fd, writefds);
1016 clib_bitmap_foreach (sid, ldp->ex_bitmap,
1018 fd = ldp_fd_from_sid (sid);
1019 if (PREDICT_FALSE (fd < 0))
1025 FD_SET (fd, exceptfds);
1029 ldp->select_vcl = 1;
1034 ldp->select_vcl = 0;
1038 struct timespec tspec;
1040 func_str = "libc_pselect";
1043 clib_memcpy_fast (readfds, ldp->libc_rd_bitmap,
1044 vec_len (ldp->rd_bitmap) *
1045 sizeof (clib_bitmap_t));
1047 clib_memcpy_fast (writefds, ldp->libc_wr_bitmap,
1048 vec_len (ldp->wr_bitmap) *
1049 sizeof (clib_bitmap_t));
1051 clib_memcpy_fast (exceptfds, ldp->libc_ex_bitmap,
1052 vec_len (ldp->ex_bitmap) *
1053 sizeof (clib_bitmap_t));
1054 tspec.tv_sec = tspec.tv_nsec = 0;
1055 rv = libc_pselect (libc_bits,
1056 readfds ? readfds : NULL,
1057 writefds ? writefds : NULL,
1058 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1063 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1067 /* TBD: set timeout to amount of time left */
1068 clib_bitmap_zero (ldp->rd_bitmap);
1069 clib_bitmap_zero (ldp->sid_rd_bitmap);
1070 clib_bitmap_zero (ldp->libc_rd_bitmap);
1071 clib_bitmap_zero (ldp->wr_bitmap);
1072 clib_bitmap_zero (ldp->sid_wr_bitmap);
1073 clib_bitmap_zero (ldp->libc_wr_bitmap);
1074 clib_bitmap_zero (ldp->ex_bitmap);
1075 clib_bitmap_zero (ldp->sid_ex_bitmap);
1076 clib_bitmap_zero (ldp->libc_ex_bitmap);
1082 int errno_val = errno;
1084 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1085 "rv %d, errno = %d", getpid (),
1086 func_str, rv, errno_val);
1090 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1096 select (int nfds, fd_set * __restrict readfds,
1097 fd_set * __restrict writefds,
1098 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1100 struct timespec tspec;
1104 tspec.tv_sec = timeout->tv_sec;
1105 tspec.tv_nsec = timeout->tv_usec * 1000;
1107 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1108 timeout ? &tspec : NULL, NULL);
1111 #ifdef __USE_XOPEN2K
1113 pselect (int nfds, fd_set * __restrict readfds,
1114 fd_set * __restrict writefds,
1115 fd_set * __restrict exceptfds,
1116 const struct timespec *__restrict timeout,
1117 const __sigset_t * __restrict sigmask)
1119 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1124 socket (int domain, int type, int protocol)
1126 const char *func_str;
1128 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1129 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1131 if ((errno = -ldp_init ()))
1134 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1135 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1138 u8 proto = ((sock_type == SOCK_DGRAM) ?
1139 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1141 func_str = "vppcom_session_create";
1143 LDBG (0, "LDP<%d>: : calling %s(): proto %u (%s), is_nonblocking %u",
1144 getpid (), func_str, proto, vppcom_proto_str (proto),
1147 sid = vppcom_session_create (proto, is_nonblocking);
1155 func_str = "ldp_fd_from_sid";
1156 rv = ldp_fd_from_sid (sid);
1159 (void) vppcom_session_close (sid);
1167 func_str = "libc_socket";
1169 LDBG (0, "LDP<%d>: : calling %s()", getpid (), func_str);
1171 rv = libc_socket (domain, type, protocol);
1178 int errno_val = errno;
1180 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1181 "rv %d, errno = %d",
1182 getpid (), func_str, rv, errno_val);
1186 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1192 * Create two new sockets, of type TYPE in domain DOMAIN and using
1193 * protocol PROTOCOL, which are connected to each other, and put file
1194 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1195 * one will be chosen automatically.
1196 * Returns 0 on success, -1 for errors.
1199 socketpair (int domain, int type, int protocol, int fds[2])
1201 const char *func_str;
1203 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1205 if ((errno = -ldp_init ()))
1208 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1209 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1211 func_str = __func__;
1213 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1219 func_str = "libc_socket";
1221 LDBG (1, "LDP<%d>: : calling %s()", getpid (), func_str);
1223 rv = libc_socketpair (domain, type, protocol, fds);
1230 int errno_val = errno;
1232 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1233 "rv %d, errno = %d",
1234 getpid (), func_str, rv, errno_val);
1238 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1244 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1247 const char *func_str;
1248 u32 sid = ldp_sid_from_fd (fd);
1250 if ((errno = -ldp_init ()))
1253 if (sid != INVALID_SESSION_ID)
1257 func_str = "vppcom_session_bind";
1259 switch (addr->sa_family)
1262 if (len != sizeof (struct sockaddr_in))
1265 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1266 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1271 ep.is_ip4 = VPPCOM_IS_IP4;
1272 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1273 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1277 if (len != sizeof (struct sockaddr_in6))
1280 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1281 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1286 ep.is_ip4 = VPPCOM_IS_IP6;
1287 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1288 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1292 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1293 "Unsupported address family %u!",
1294 getpid (), fd, fd, sid, sid, addr->sa_family);
1295 errno = EAFNOSUPPORT;
1300 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1302 getpid (), fd, fd, func_str, sid, sid, addr, len);
1304 rv = vppcom_session_bind (sid, &ep);
1305 if (rv != VPPCOM_OK)
1313 func_str = "libc_bind";
1316 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1318 getpid (), fd, fd, func_str, addr, len);
1320 rv = libc_bind (fd, addr, len);
1328 int errno_val = errno;
1330 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1331 "rv %d, errno = %d", getpid (), fd, fd,
1332 func_str, rv, errno_val);
1336 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1337 getpid (), fd, fd, rv, rv);
1343 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1344 vppcom_endpt_t * ep)
1347 int sa_len, copy_len;
1349 if ((errno = -ldp_init ()))
1352 if (addr && len && ep)
1354 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1355 switch (addr->sa_family)
1358 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1359 if (*len > sizeof (struct sockaddr_in))
1360 *len = sizeof (struct sockaddr_in);
1361 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1362 copy_len = *len - sa_len;
1364 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1369 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1370 if (*len > sizeof (struct sockaddr_in6))
1371 *len = sizeof (struct sockaddr_in6);
1372 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1373 copy_len = *len - sa_len;
1375 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1376 __in6_u.__u6_addr8, ep->ip, copy_len);
1389 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1392 const char *func_str;
1393 u32 sid = ldp_sid_from_fd (fd);
1395 if ((errno = -ldp_init ()))
1398 if (sid != INVALID_SESSION_ID)
1401 u8 addr_buf[sizeof (struct in6_addr)];
1402 u32 size = sizeof (ep);
1405 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1408 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1410 getpid (), fd, fd, func_str, sid, sid, addr, len);
1412 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1413 if (rv != VPPCOM_OK)
1420 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1421 if (rv != VPPCOM_OK)
1430 func_str = "libc_getsockname";
1433 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1435 getpid (), fd, fd, func_str, addr, len);
1437 rv = libc_getsockname (fd, addr, len);
1444 int errno_val = errno;
1446 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1447 "rv %d, errno = %d", getpid (), fd, fd,
1448 func_str, rv, errno_val);
1452 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1453 getpid (), fd, fd, rv, rv);
1459 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1462 const char *func_str = __func__;
1463 u32 sid = ldp_sid_from_fd (fd);
1465 if ((errno = -ldp_init ()))
1470 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1471 getpid (), fd, fd, len);
1477 if (sid != INVALID_SESSION_ID)
1481 func_str = "vppcom_session_connect";
1483 switch (addr->sa_family)
1486 if (len != sizeof (struct sockaddr_in))
1489 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1490 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1495 ep.is_ip4 = VPPCOM_IS_IP4;
1496 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1497 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1501 if (len != sizeof (struct sockaddr_in6))
1504 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1505 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1510 ep.is_ip4 = VPPCOM_IS_IP6;
1511 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1512 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1516 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1517 "Unsupported address family %u!",
1518 getpid (), fd, fd, sid, sid, addr->sa_family);
1519 errno = EAFNOSUPPORT;
1524 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1526 getpid (), fd, fd, func_str, sid, sid, addr, len);
1528 rv = vppcom_session_connect (sid, &ep);
1529 if (rv != VPPCOM_OK)
1537 func_str = "libc_connect";
1540 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1542 getpid (), fd, fd, func_str, addr, len);
1544 rv = libc_connect (fd, addr, len);
1552 int errno_val = errno;
1554 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1555 "rv %d, errno = %d", getpid (), fd, fd,
1556 func_str, rv, errno_val);
1560 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1561 getpid (), fd, fd, rv, rv);
1567 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1570 const char *func_str;
1571 u32 sid = ldp_sid_from_fd (fd);
1573 if ((errno = -ldp_init ()))
1576 if (sid != INVALID_SESSION_ID)
1579 u8 addr_buf[sizeof (struct in6_addr)];
1580 u32 size = sizeof (ep);
1583 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1586 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1588 getpid (), fd, fd, func_str, sid, sid, addr, len);
1590 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1591 if (rv != VPPCOM_OK)
1598 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1599 if (rv != VPPCOM_OK)
1608 func_str = "libc_getpeername";
1611 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1613 getpid (), fd, fd, func_str, addr, len);
1615 rv = libc_getpeername (fd, addr, len);
1622 int errno_val = errno;
1624 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1625 "rv %d, errno = %d", getpid (), fd, fd,
1626 func_str, rv, errno_val);
1630 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1631 getpid (), fd, fd, rv, rv);
1637 send (int fd, const void *buf, size_t n, int flags)
1640 const char *func_str;
1641 u32 sid = ldp_sid_from_fd (fd);
1643 if ((errno = -ldp_init ()))
1646 if (sid != INVALID_SESSION_ID)
1649 func_str = "vppcom_session_sendto";
1652 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1653 "buf %p, n %u, flags 0x%x",
1654 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1656 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1657 if (size < VPPCOM_OK)
1665 func_str = "libc_send";
1668 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1669 "buf %p, n %u, flags 0x%x",
1670 getpid (), fd, fd, func_str, buf, n, flags);
1672 size = libc_send (fd, buf, n, flags);
1679 int errno_val = errno;
1681 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1682 "rv %d, errno = %d", getpid (), fd, fd,
1683 func_str, size, errno_val);
1687 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1688 getpid (), fd, fd, size, size);
1694 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1697 const char *func_str;
1698 u32 sid = ldp_sid_from_fd (out_fd);
1700 if ((errno = -ldp_init ()))
1703 if (sid != INVALID_SESSION_ID)
1706 ssize_t results = 0;
1707 size_t n_bytes_left = len;
1708 size_t bytes_to_read;
1712 u32 flags, flags_len = sizeof (flags);
1714 func_str = "vppcom_session_attr[GET_FLAGS]";
1715 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1717 if (PREDICT_FALSE (rv != VPPCOM_OK))
1719 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1720 "sid %u (0x%x), returned %d (%s)!", getpid (),
1721 out_fd, out_fd, func_str, sid, sid, rv,
1722 vppcom_retval_str (rv));
1724 vec_reset_length (ldp->io_buffer);
1732 off_t off = lseek (in_fd, *offset, SEEK_SET);
1733 if (PREDICT_FALSE (off == -1))
1737 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1738 "SEEK_SET failed: in_fd %d, offset %p, "
1739 "*offset %ld, rv %ld, errno %d", getpid (),
1740 out_fd, out_fd, in_fd, offset, *offset, off,
1747 ASSERT (off == *offset);
1752 func_str = "vppcom_session_attr[GET_NWRITE]";
1753 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1757 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1758 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1759 sid, sid, size, vppcom_retval_str (size));
1760 vec_reset_length (ldp->io_buffer);
1766 bytes_to_read = size;
1769 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1770 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1771 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1774 if (bytes_to_read == 0)
1776 if (flags & O_NONBLOCK)
1781 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1783 getpid (), out_fd, out_fd, sid, sid);
1791 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1792 vec_validate (ldp->io_buffer, bytes_to_read);
1793 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1796 func_str = "libc_read";
1798 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1799 "io_buffer %p, bytes_to_read %lu, rv %d, "
1800 "errno %d", getpid (), out_fd, out_fd, func_str,
1801 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1807 vec_reset_length (ldp->io_buffer);
1813 func_str = "vppcom_session_write";
1816 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1817 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1818 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1819 results, n_bytes_left);
1821 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1824 if (size == VPPCOM_EAGAIN)
1826 if (flags & O_NONBLOCK)
1832 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1833 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1843 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1844 "sid %u, io_buffer %p, nbytes %u "
1846 getpid (), out_fd, out_fd, func_str,
1847 sid, ldp->io_buffer, nbytes,
1848 size, vppcom_retval_str (size));
1852 vec_reset_length (ldp->io_buffer);
1861 ASSERT (n_bytes_left >= nbytes);
1862 n_bytes_left = n_bytes_left - nbytes;
1864 while (n_bytes_left > 0);
1867 vec_reset_length (ldp->io_buffer);
1870 off_t off = lseek (in_fd, *offset, SEEK_SET);
1871 if (PREDICT_FALSE (off == -1))
1875 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1876 "in_fd %d, offset %p, *offset %ld, "
1877 "rv %ld, errno %d", getpid (), in_fd,
1878 offset, *offset, off, errno_val);
1884 ASSERT (off == *offset);
1885 *offset += results + 1;
1897 func_str = "libc_send";
1900 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1901 "in_fd %d, offset %p, len %u",
1902 getpid (), out_fd, out_fd, func_str,
1903 in_fd, offset, len);
1905 size = libc_sendfile (out_fd, in_fd, offset, len);
1913 int errno_val = errno;
1915 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1916 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1917 func_str, size, errno_val);
1921 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1922 getpid (), out_fd, out_fd, size, size);
1928 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1930 return sendfile (out_fd, in_fd, offset, len);
1934 recv (int fd, void *buf, size_t n, int flags)
1937 const char *func_str;
1938 u32 sid = ldp_sid_from_fd (fd);
1940 if ((errno = -ldp_init ()))
1943 if (sid != INVALID_SESSION_ID)
1945 func_str = "vppcom_session_recvfrom";
1948 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1949 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1950 fd, fd, func_str, sid, sid, buf, n, flags);
1952 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1961 func_str = "libc_recv";
1964 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1965 "buf %p, n %u, flags 0x%x", getpid (),
1966 fd, fd, func_str, buf, n, flags);
1968 size = libc_recv (fd, buf, n, flags);
1975 int errno_val = errno;
1977 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1978 "rv %d, errno = %d", getpid (), fd, fd,
1979 func_str, size, errno_val);
1983 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1984 getpid (), fd, fd, size, size);
1990 sendto (int fd, const void *buf, size_t n, int flags,
1991 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1994 const char *func_str = __func__;
1995 u32 sid = ldp_sid_from_fd (fd);
1997 if ((errno = -ldp_init ()))
2000 if (sid != INVALID_SESSION_ID)
2002 vppcom_endpt_t *ep = 0;
2008 switch (addr->sa_family)
2011 ep->is_ip4 = VPPCOM_IS_IP4;
2013 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2015 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2019 ep->is_ip4 = VPPCOM_IS_IP6;
2021 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2023 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2027 errno = EAFNOSUPPORT;
2033 func_str = "vppcom_session_sendto";
2036 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2037 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2038 getpid (), fd, fd, func_str, sid, sid, buf, n,
2041 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2050 func_str = "libc_sendto";
2053 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2054 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2055 getpid (), fd, fd, func_str, buf, n, flags,
2058 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2066 int errno_val = errno;
2068 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2069 "rv %d, errno = %d", getpid (), fd, fd,
2070 func_str, size, errno_val);
2074 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2075 getpid (), fd, fd, size, size);
2081 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2082 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2085 const char *func_str;
2086 u32 sid = ldp_sid_from_fd (fd);
2088 if ((errno = -ldp_init ()))
2091 if (sid != INVALID_SESSION_ID)
2094 u8 src_addr[sizeof (struct sockaddr_in6)];
2096 func_str = "vppcom_session_recvfrom";
2099 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2100 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2101 getpid (), fd, fd, func_str, sid, sid, buf, n,
2106 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2109 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2112 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2122 func_str = "libc_recvfrom";
2125 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2126 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2127 getpid (), fd, fd, func_str, buf, n, flags,
2130 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2137 int errno_val = errno;
2139 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2140 "rv %d, errno = %d", getpid (), fd, fd,
2141 func_str, size, errno_val);
2145 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2146 getpid (), fd, fd, size, size);
2152 sendmsg (int fd, const struct msghdr * message, int flags)
2155 const char *func_str;
2156 u32 sid = ldp_sid_from_fd (fd);
2158 if ((errno = -ldp_init ()))
2161 if (sid != INVALID_SESSION_ID)
2163 func_str = __func__;
2165 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2171 func_str = "libc_sendmsg";
2174 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2175 "message %p, flags 0x%x",
2176 getpid (), fd, fd, func_str, message, flags);
2178 size = libc_sendmsg (fd, message, flags);
2185 int errno_val = errno;
2187 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2188 "rv %d, errno = %d", getpid (), fd, fd,
2189 func_str, size, errno_val);
2193 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2194 getpid (), fd, fd, size, size);
2201 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2204 const char *func_str;
2205 u32 sid = ldp_sid_from_fd (fd);
2207 if ((errno = -ldp_init ()))
2210 if (sid != INVALID_SESSION_ID)
2212 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2218 func_str = "libc_sendmmsg";
2221 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2222 "vmessages %p, vlen %u, flags 0x%x",
2223 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2225 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2232 int errno_val = errno;
2234 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2235 "rv %d, errno = %d", getpid (), fd, fd,
2236 func_str, size, errno_val);
2240 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2241 getpid (), fd, fd, size, size);
2248 recvmsg (int fd, struct msghdr * message, int flags)
2251 const char *func_str;
2252 u32 sid = ldp_sid_from_fd (fd);
2254 if ((errno = -ldp_init ()))
2257 if (sid != INVALID_SESSION_ID)
2259 func_str = __func__;
2261 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2267 func_str = "libc_recvmsg";
2270 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2271 "message %p, flags 0x%x",
2272 getpid (), fd, fd, func_str, message, flags);
2274 size = libc_recvmsg (fd, message, flags);
2281 int errno_val = errno;
2283 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2284 "rv %d, errno = %d", getpid (), fd, fd,
2285 func_str, size, errno_val);
2289 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2290 getpid (), fd, fd, size, size);
2297 recvmmsg (int fd, struct mmsghdr *vmessages,
2298 unsigned int vlen, int flags, struct timespec *tmo)
2301 const char *func_str;
2302 u32 sid = ldp_sid_from_fd (fd);
2304 if ((errno = -ldp_init ()))
2307 if (sid != INVALID_SESSION_ID)
2309 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2315 func_str = "libc_recvmmsg";
2318 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2319 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2320 getpid (), fd, fd, func_str, vmessages, vlen,
2323 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2330 int errno_val = errno;
2332 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2333 "rv %d, errno = %d", getpid (), fd, fd,
2334 func_str, size, errno_val);
2338 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2339 getpid (), fd, fd, size, size);
2346 getsockopt (int fd, int level, int optname,
2347 void *__restrict optval, socklen_t * __restrict optlen)
2350 const char *func_str = __func__;
2351 u32 sid = ldp_sid_from_fd (fd);
2352 u32 buflen = optlen ? (u32) * optlen : 0;
2354 if ((errno = -ldp_init ()))
2357 if (sid != INVALID_SESSION_ID)
2367 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2369 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2371 getpid (), fd, fd, func_str, sid, sid);
2372 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2376 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
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_USER_MSS,
2385 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
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_KEEPIDLE,
2394 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2396 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2397 "sid %u (0x%x), SOL_TCP",
2398 getpid (), fd, fd, func_str, sid, sid);
2399 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2403 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2406 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2407 "SOL_TCP, TCP_INFO, optval %p, "
2408 "optlen %d: #LDP-NOP#",
2409 getpid (), fd, fd, sid, sid,
2411 memset (optval, 0, *optlen);
2419 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2420 "sid %u (0x%x), SOL_TCP, "
2421 "optname %d unsupported!",
2422 getpid (), fd, fd, func_str, sid, sid, optname);
2430 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2432 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2434 getpid (), fd, fd, func_str, sid, sid);
2435 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2440 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2441 "sid %u (0x%x), SOL_IPV6, "
2442 "optname %d unsupported!",
2443 getpid (), fd, fd, func_str, sid, sid, optname);
2451 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2453 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2455 getpid (), fd, fd, func_str, sid, sid);
2456 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2460 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
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_KEEPALIVE,
2469 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
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_PROTOCOL,
2476 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2479 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2481 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2482 "sid %u (0x%x), optlen %d",
2483 getpid (), fd, fd, func_str, sid, sid, buflen);
2484 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2488 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_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_RX_FIFO_LEN,
2497 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2499 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2501 getpid (), fd, fd, func_str, sid, sid);
2502 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2506 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
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_BROADCAST,
2515 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
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_ERROR,
2525 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2526 "sid %u (0x%x), SOL_SOCKET, "
2527 "optname %d unsupported!",
2528 getpid (), fd, fd, func_str, sid, sid, optname);
2536 if (rv != VPPCOM_OK)
2544 func_str = "libc_getsockopt";
2547 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2548 "optname %d, optval %p, optlen %d",
2549 getpid (), fd, fd, func_str, level, optname,
2552 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2559 int errno_val = errno;
2561 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2562 "rv %d, errno = %d", getpid (), fd, fd,
2563 func_str, rv, errno_val);
2567 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2568 getpid (), fd, fd, rv, rv);
2574 setsockopt (int fd, int level, int optname,
2575 const void *optval, socklen_t optlen)
2578 const char *func_str = __func__;
2579 u32 sid = ldp_sid_from_fd (fd);
2581 if ((errno = -ldp_init ()))
2584 if (sid != INVALID_SESSION_ID)
2594 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2596 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2598 getpid (), fd, fd, func_str, sid, sid);
2599 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2600 (void *) optval, &optlen);
2603 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
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_USER_MSS,
2609 (void *) optval, &optlen);
2612 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
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_KEEPIDLE,
2618 (void *) optval, &optlen);
2621 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2623 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2624 "sid %u (0x%x), SOL_TCP",
2625 getpid (), fd, fd, func_str, sid, sid);
2626 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2627 (void *) optval, &optlen);
2631 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2632 "sid %u (0x%x), SOL_TCP, "
2633 "optname %d unsupported!",
2634 getpid (), fd, fd, func_str, sid, sid, optname);
2642 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2644 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2646 getpid (), fd, fd, func_str, sid, sid);
2647 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2648 (void *) optval, &optlen);
2652 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2653 "sid %u (0x%x), SOL_IPV6, "
2654 "optname %d unsupported!",
2655 getpid (), fd, fd, func_str, sid, sid, optname);
2663 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2665 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2667 getpid (), fd, fd, func_str, sid, sid);
2668 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2669 (void *) optval, &optlen);
2672 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
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_REUSEADDR,
2678 (void *) optval, &optlen);
2681 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
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_BROADCAST,
2687 (void *) optval, &optlen);
2691 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2692 "sid %u (0x%x), SOL_SOCKET, "
2693 "optname %d unsupported!",
2694 getpid (), fd, fd, func_str, sid, sid, optname);
2702 if (rv != VPPCOM_OK)
2710 func_str = "libc_setsockopt";
2713 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2714 "optname %d, optval %p, optlen %d",
2715 getpid (), fd, fd, func_str, level, optname,
2718 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2725 int errno_val = errno;
2727 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2728 "rv %d, errno = %d", getpid (), fd, fd,
2729 func_str, rv, errno_val);
2733 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2734 getpid (), fd, fd, rv, rv);
2740 listen (int fd, int n)
2743 const char *func_str;
2744 u32 sid = ldp_sid_from_fd (fd);
2746 if ((errno = -ldp_init ()))
2749 if (sid != INVALID_SESSION_ID)
2751 func_str = "vppcom_session_listen";
2753 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2754 getpid (), fd, fd, func_str, sid, sid, n);
2756 rv = vppcom_session_listen (sid, n);
2757 if (rv != VPPCOM_OK)
2765 func_str = "libc_listen";
2767 LDBG (0, "LDP<%d>: fd %d (0x%x): calling %s(): n %d", getpid (), fd,
2770 rv = libc_listen (fd, n);
2777 int errno_val = errno;
2779 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2780 "rv %d, errno = %d", getpid (), fd, fd,
2781 func_str, rv, errno_val);
2785 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2786 getpid (), fd, fd, rv, rv);
2792 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2793 socklen_t * __restrict addr_len, int flags)
2796 const char *func_str;
2797 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2800 if ((errno = -ldp_init ()))
2803 if (listen_sid != INVALID_SESSION_ID)
2806 u8 src_addr[sizeof (struct sockaddr_in6)];
2807 memset (&ep, 0, sizeof (ep));
2810 func_str = "vppcom_session_accept";
2813 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2814 "listen sid %u (0x%x), ep %p, flags 0x%x",
2815 getpid (), listen_fd, listen_fd, func_str,
2816 listen_sid, listen_sid, ep, flags);
2818 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2821 errno = -accept_sid;
2826 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2827 if (rv != VPPCOM_OK)
2829 (void) vppcom_session_close ((u32) accept_sid);
2835 func_str = "ldp_fd_from_sid";
2837 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2838 "accept sid %u (0x%x), ep %p, flags 0x%x",
2839 getpid (), listen_fd, listen_fd,
2840 func_str, accept_sid, accept_sid, ep, flags);
2841 rv = ldp_fd_from_sid ((u32) accept_sid);
2844 (void) vppcom_session_close ((u32) accept_sid);
2853 func_str = "libc_accept4";
2856 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2857 "addr %p, addr_len %p, flags 0x%x",
2858 getpid (), listen_fd, listen_fd, func_str,
2859 addr, addr_len, flags);
2861 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2868 int errno_val = errno;
2870 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2871 "rv %d, errno = %d", getpid (), listen_fd,
2872 listen_fd, func_str, rv, errno_val);
2876 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2877 getpid (), listen_fd, listen_fd, rv, rv);
2883 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2886 return ldp_accept4 (fd, addr, addr_len, flags);
2890 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2892 return ldp_accept4 (fd, addr, addr_len, 0);
2896 shutdown (int fd, int how)
2899 const char *func_str;
2900 u32 sid = ldp_sid_from_fd (fd);
2902 if ((errno = -ldp_init ()))
2905 if (sid != INVALID_SESSION_ID)
2907 func_str = "vppcom_session_close[TODO]";
2912 func_str = "libc_shutdown";
2915 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2916 getpid (), fd, fd, func_str, how);
2918 rv = libc_shutdown (fd, how);
2925 int errno_val = errno;
2927 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2928 "rv %d, errno = %d", getpid (), fd, fd,
2929 func_str, rv, errno_val);
2933 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2934 getpid (), fd, fd, rv, rv);
2940 epoll_create1 (int flags)
2942 const char *func_str;
2945 if ((errno = -ldp_init ()))
2948 if (ldp->vcl_needs_real_epoll)
2950 rv = libc_epoll_create1 (flags);
2951 ldp->vcl_needs_real_epoll = 0;
2952 ldp->vcl_mq_epfd = rv;
2953 LDBG (0, "LDP<%d>: created vcl epfd %u", getpid (), rv);
2956 func_str = "vppcom_epoll_create";
2958 LDBG (1, "LDP<%d>: calling %s()", getpid (), func_str);
2960 rv = vppcom_epoll_create ();
2962 if (PREDICT_FALSE (rv < 0))
2968 rv = ldp_fd_from_sid ((u32) rv);
2974 int errno_val = errno;
2976 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2977 "rv %d, errno = %d",
2978 getpid (), func_str, rv, errno_val);
2982 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2988 epoll_create (int size)
2990 return epoll_create1 (0);
2994 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2996 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
2997 const char *func_str;
3000 if ((errno = -ldp_init ()))
3003 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3005 /* The LDP epoll_create1 always creates VCL epfd's.
3006 * The app should never have a kernel base epoll fd unless it
3007 * was acquired outside of the LD_PRELOAD process context.
3008 * In any case, if we get one, punt it to libc_epoll_ctl.
3010 func_str = "libc_epoll_ctl";
3012 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
3013 " event %p", getpid (), epfd, epfd, func_str, op, fd, fd, event);
3015 rv = libc_epoll_ctl (epfd, op, fd, event);
3019 sid = ldp_sid_from_fd (fd);
3021 LDBG (0, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
3022 getpid (), epfd, epfd, vep_idx, vep_idx, sid, sid);
3024 if (sid != INVALID_SESSION_ID)
3026 func_str = "vppcom_epoll_ctl";
3028 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3029 " op %d, sid %u (0x%x), event %p", getpid (), epfd, epfd,
3030 func_str, vep_idx, vep_idx, sid, sid, event);
3032 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3033 if (rv != VPPCOM_OK)
3042 u32 size = sizeof (epfd);
3044 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3045 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
3047 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): %s() "
3048 "returned libc_epfd %d (0x%x)", getpid (), epfd, epfd,
3049 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
3053 func_str = "libc_epoll_create1";
3055 LDBG (1, "LDP<%d>: epfd %d (0x%x), vep_idx %d (0x%x): "
3056 "calling %s(): EPOLL_CLOEXEC", getpid (), epfd, epfd,
3057 vep_idx, vep_idx, func_str);
3059 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3066 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3067 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3068 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
3069 getpid (), epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
3072 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3081 else if (PREDICT_FALSE (libc_epfd < 0))
3088 func_str = "libc_epoll_ctl";
3090 LDBG (1, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
3091 "op %d, fd %d (0x%x), event %p", getpid (), epfd, epfd, func_str,
3092 libc_epfd, libc_epfd, op, fd, fd, event);
3094 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
3102 int errno_val = errno;
3104 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3105 "rv %d, errno = %d", getpid (), fd, fd,
3106 func_str, rv, errno_val);
3110 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3111 getpid (), fd, fd, rv, rv);
3117 ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
3118 int timeout, const sigset_t * sigmask)
3120 double time_to_wait = (double) 0, time_out, now = 0;
3121 u32 vep_idx = ldp_sid_from_fd (epfd);
3122 int libc_epfd, rv = 0;
3123 const char *func_str;
3125 if ((errno = -ldp_init ()))
3128 if (PREDICT_FALSE (!events || (timeout < -1)))
3134 if (epfd == ldp->vcl_mq_epfd)
3135 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3137 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3139 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3140 getpid (), epfd, epfd, vep_idx, vep_idx);
3145 time_to_wait = ((timeout >= 0) ? (double) timeout : 0);
3146 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3148 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3149 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3150 if (PREDICT_FALSE (libc_epfd < 0))
3157 LDBG (2, "LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
3158 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
3159 getpid (), epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3160 maxevents, timeout, sigmask, time_to_wait, time_out);
3163 if (!ldp->epoll_wait_vcl)
3165 func_str = "vppcom_epoll_wait";
3167 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
3168 " events %p, maxevents %d", getpid (), epfd, epfd, func_str,
3169 vep_idx, vep_idx, events, maxevents);
3171 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3174 ldp->epoll_wait_vcl = 1;
3185 ldp->epoll_wait_vcl = 0;
3189 func_str = "libc_epoll_pwait";
3191 LDBG (3, "LDP<%d>: epfd %d (0x%x): calling %s(): libc_epfd %d "
3192 "(0x%x), events %p, maxevents %d, sigmask %p", getpid (),
3193 epfd, epfd, func_str, libc_epfd, libc_epfd, events,
3194 maxevents, sigmask);
3196 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3202 now = clib_time_now (&ldp->clib_time);
3204 while (now < time_out);
3213 int errno_val = errno;
3215 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3216 "rv %d, errno = %d", getpid (), epfd, epfd,
3217 func_str, rv, errno_val);
3221 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3222 getpid (), epfd, epfd, rv, rv);
3228 epoll_pwait (int epfd, struct epoll_event *events,
3229 int maxevents, int timeout, const sigset_t * sigmask)
3231 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3235 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3237 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3241 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3243 const char *func_str = __func__;
3244 int rv, i, n_revents = 0;
3247 double wait_for_time;
3249 LDBG (3, "LDP<%d>: fds %p, nfds %d, timeout %d", getpid (), fds, nfds,
3253 wait_for_time = (f64) timeout / 1000;
3257 for (i = 0; i < nfds; i++)
3262 LDBG (3, "LDP<%d>: fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3263 getpid (), i, fds[i].fd, fds[i].fd, fds[i].events,
3266 sid = ldp_sid_from_fd (fds[i].fd);
3267 if (sid != INVALID_SESSION_ID)
3269 fds[i].fd = -fds[i].fd;
3270 vec_add2 (ldp->vcl_poll, vp, 1);
3273 vp->events = fds[i].events;
3274 #ifdef __USE_XOPEN2K
3275 if (fds[i].events & POLLRDNORM)
3276 vp->events |= POLLIN;
3277 if (fds[i].events & POLLWRNORM)
3278 vp->events |= POLLOUT;
3280 vp->revents = fds[i].revents;
3284 vec_add1 (ldp->libc_poll, fds[i]);
3285 vec_add1 (ldp->libc_poll_idxs, i);
3291 if (vec_len (ldp->vcl_poll))
3293 func_str = "vppcom_poll";
3295 LDBG (3, "LDP<%d>: calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3296 "n_libc_fds %u", getpid (), func_str, ldp->vcl_poll,
3297 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3298 vec_len (ldp->libc_poll));
3300 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3311 if (vec_len (ldp->libc_poll))
3313 func_str = "libc_poll";
3315 LDBG (3, "LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3316 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3318 rv = libc_poll (ldp->libc_poll, vec_len (ldp->libc_poll), 0);
3331 while ((wait_for_time == -1) ||
3332 (clib_time_now (&ldp->clib_time) < wait_for_time));
3336 vec_foreach (vp, ldp->vcl_poll)
3338 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3339 fds[vp->fds_ndx].revents = vp->revents;
3340 #ifdef __USE_XOPEN2K
3341 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3342 (fds[vp->fds_ndx].events & POLLRDNORM))
3343 fds[vp->fds_ndx].revents |= POLLRDNORM;
3344 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3345 (fds[vp->fds_ndx].events & POLLWRNORM))
3346 fds[vp->fds_ndx].revents |= POLLWRNORM;
3349 vec_reset_length (ldp->vcl_poll);
3351 for (i = 0; i < vec_len (ldp->libc_poll); i++)
3353 fds[ldp->libc_poll_idxs[i]].revents = ldp->libc_poll[i].revents;
3355 vec_reset_length (ldp->libc_poll_idxs);
3356 vec_reset_length (ldp->libc_poll);
3362 int errno_val = errno;
3364 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3365 "rv %d, errno = %d", getpid (),
3366 func_str, rv, errno_val);
3371 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3372 "n_libc_fds %d", getpid (), rv, rv,
3373 vec_len (ldp->vcl_poll), vec_len (ldp->libc_poll));
3375 for (i = 0; i < nfds; i++)
3380 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3381 ".events = 0x%x, .revents = 0x%x",
3382 getpid (), i, fds[i].fd, fds[i].fd,
3383 fds[i].events, fds[i].revents);
3394 ppoll (struct pollfd *fds, nfds_t nfds,
3395 const struct timespec *timeout, const sigset_t * sigmask)
3397 if ((errno = -ldp_init ()))
3400 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3408 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3410 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3413 * This function is called when the library is loaded
3416 ldp_constructor (void)
3418 swrap_constructor ();
3419 if (ldp_init () != 0)
3420 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3422 else if (LDP_DEBUG > 0)
3423 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3427 * This function is called when the library is unloaded
3430 ldp_destructor (void)
3432 swrap_destructor ();
3435 vppcom_app_destroy ();
3439 /* Don't use clib_warning() here because that calls writev()
3440 * which will call ldp_init().
3443 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3444 __func__, __LINE__, getpid ());
3449 * fd.io coding-style-patch-verification: ON
3452 * eval: (c-set-style "gnu")