2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <sys/resource.h>
23 #include <netinet/tcp.h>
25 #include <vcl/ldp_socket_wrapper.h>
29 #include <vcl/vppcom.h>
30 #include <vppinfra/time.h>
31 #include <vppinfra/bitmap.h>
33 #define HAVE_CONSTRUCTOR_ATTRIBUTE
34 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
35 #define CONSTRUCTOR_ATTRIBUTE \
36 __attribute__ ((constructor))
38 #define CONSTRUCTOR_ATTRIBUTE
39 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
41 #define HAVE_DESTRUCTOR_ATTRIBUTE
42 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
43 #define DESTRUCTOR_ATTRIBUTE \
44 __attribute__ ((destructor))
46 #define DESTRUCTOR_ATTRIBUTE
52 char app_name[LDP_APP_NAME_MAX];
57 clib_time_t clib_time;
58 clib_bitmap_t *rd_bitmap;
59 clib_bitmap_t *wr_bitmap;
60 clib_bitmap_t *ex_bitmap;
61 clib_bitmap_t *sid_rd_bitmap;
62 clib_bitmap_t *sid_wr_bitmap;
63 clib_bitmap_t *sid_ex_bitmap;
64 clib_bitmap_t *libc_rd_bitmap;
65 clib_bitmap_t *libc_wr_bitmap;
66 clib_bitmap_t *libc_ex_bitmap;
71 #define LDP_DEBUG ldp->debug
73 static ldp_main_t ldp_main = {
74 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
75 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
76 .debug = LDP_DEBUG_INIT,
79 static ldp_main_t *ldp = &ldp_main;
82 * RETURN: 0 on success or -1 on error.
85 ldp_set_app_name (char *app_name)
87 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
88 "ldp-%d-%s", getpid (), app_name);
90 if (rv >= LDP_APP_NAME_MAX)
91 app_name[LDP_APP_NAME_MAX - 1] = 0;
97 if (ldp->app_name[0] == '\0')
98 ldp_set_app_name ("app");
100 return ldp->app_name;
104 ldp_fd_from_sid (u32 sid)
106 if (PREDICT_FALSE (sid >= ldp->sid_bit_val))
109 return (sid | ldp->sid_bit_val);
113 ldp_fd_is_sid (int fd)
115 return ((u32) fd & ldp->sid_bit_val) ? 1 : 0;
119 ldp_sid_from_fd (int fd)
121 return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) :
130 if (PREDICT_FALSE (!ldp->init))
133 rv = vppcom_app_create (ldp_get_app_name ());
136 char *env_var_str = getenv (LDP_ENV_DEBUG);
140 if (sscanf (env_var_str, "%u", &tmp) != 1)
141 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level "
142 "specified in the env var " LDP_ENV_DEBUG
143 " (%s)!", getpid (), env_var_str);
147 clib_warning ("LDP<%d>: configured LDP debug level (%u) "
148 "from the env var " LDP_ENV_DEBUG "!",
149 getpid (), ldp->debug);
153 env_var_str = getenv (LDP_ENV_APP_NAME);
156 ldp_set_app_name (env_var_str);
157 clib_warning ("LDP<%d>: configured LDP app name (%s) "
158 "from the env var " LDP_ENV_APP_NAME "!",
159 getpid (), ldp->app_name);
162 env_var_str = getenv (LDP_ENV_SID_BIT);
166 if (sscanf (env_var_str, "%u", &sb) != 1)
168 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit "
169 "specified in the env var "
170 LDP_ENV_SID_BIT " (%s)!"
171 "sid bit value %d (0x%x)",
172 getpid (), env_var_str,
173 ldp->sid_bit_val, ldp->sid_bit_val);
175 else if (sb < LDP_SID_BIT_MIN)
177 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
178 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
180 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) "
181 "specified in the env var "
182 LDP_ENV_SID_BIT " (%s) is too small. "
183 "Using LDP_SID_BIT_MIN (%d)! "
184 "sid bit value %d (0x%x)",
185 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
186 ldp->sid_bit_val, ldp->sid_bit_val);
188 else if (sb > LDP_SID_BIT_MAX)
190 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
191 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
193 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) "
194 "specified in the env var "
195 LDP_ENV_SID_BIT " (%s) is too big. "
196 "Using LDP_SID_BIT_MAX (%d)! "
197 "sid bit value %d (0x%x)",
198 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
199 ldp->sid_bit_val, ldp->sid_bit_val);
203 ldp->sid_bit_val = (1 << sb);
204 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
206 clib_warning ("LDP<%d>: configured LDP sid bit (%u) "
207 "from " LDP_ENV_SID_BIT
208 "! sid bit value %d (0x%x)", getpid (),
209 sb, ldp->sid_bit_val, ldp->sid_bit_val);
213 clib_time_init (&ldp->clib_time);
214 clib_warning ("LDP<%d>: LDP initialization: done!", getpid ());
218 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
219 " failed! rv = %d (%s)\n",
220 getpid (), rv, vppcom_retval_str (rv));
231 const char *func_str;
232 u32 sid = ldp_sid_from_fd (fd);
234 if ((errno = -ldp_init ()))
237 if (sid != INVALID_SESSION_ID)
241 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
242 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
245 func_str = "libc_close";
249 ("LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
250 getpid (), fd, fd, func_str, epfd, epfd);
252 rv = libc_close (epfd);
255 u32 size = sizeof (epfd);
258 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
262 else if (PREDICT_FALSE (epfd < 0))
269 func_str = "vppcom_session_close";
272 clib_warning ("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";
287 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s()",
288 getpid (), fd, fd, func_str);
290 rv = libc_close (fd);
298 int errno_val = errno;
300 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
301 "rv %d, errno = %d", getpid (), fd, fd,
302 func_str, rv, errno_val);
306 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
307 getpid (), fd, fd, rv, rv);
313 read (int fd, void *buf, size_t nbytes)
316 const char *func_str;
317 u32 sid = ldp_sid_from_fd (fd);
319 if ((errno = -ldp_init ()))
322 if (sid != INVALID_SESSION_ID)
324 func_str = "vppcom_session_read";
327 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
328 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
329 fd, fd, func_str, sid, sid, buf, nbytes);
331 size = vppcom_session_read (sid, buf, nbytes);
340 func_str = "libc_read";
343 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
344 "buf %p, nbytes %u", getpid (),
345 fd, fd, func_str, buf, nbytes);
347 size = libc_read (fd, buf, nbytes);
354 int errno_val = errno;
356 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
357 "rv %d, errno = %d", getpid (), fd, fd,
358 func_str, size, errno_val);
362 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
363 getpid (), fd, fd, size, size);
369 readv (int fd, const struct iovec * iov, int iovcnt)
371 const char *func_str;
373 u32 sid = ldp_sid_from_fd (fd);
374 int rv = 0, i, total = 0;
376 if ((errno = -ldp_init ()))
379 if (sid != INVALID_SESSION_ID)
381 func_str = "vppcom_session_read";
384 for (i = 0; i < iovcnt; ++i)
387 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
388 "sid %u (0x%x), iov %p, iovcnt %d, total %d",
389 getpid (), fd, fd, func_str, i, sid, sid,
392 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
398 if (rv < iov[i].iov_len)
401 clib_warning ("LDP<%d>: fd %d (0x%x): "
402 "rv (%d) < iov[%d].iov_len (%d)",
403 getpid (), fd, fd, rv, i,
410 while ((rv >= 0) && (total == 0));
422 func_str = "libc_readv";
425 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
426 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
428 size = libc_readv (fd, iov, iovcnt);
435 int errno_val = errno;
437 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
438 "rv %d, errno = %d", getpid (), fd, fd,
439 func_str, size, errno_val);
443 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
444 getpid (), fd, fd, size, size);
450 write (int fd, const void *buf, size_t nbytes)
452 const char *func_str;
454 u32 sid = ldp_sid_from_fd (fd);
456 if ((errno = -ldp_init ()))
459 if (sid != INVALID_SESSION_ID)
461 func_str = "vppcom_session_write";
464 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
465 "sid %u (0x%x), buf %p, nbytes %u", getpid (),
466 fd, fd, func_str, sid, sid, buf, nbytes);
468 size = vppcom_session_write (sid, (void *) buf, nbytes);
477 func_str = "libc_write";
480 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
481 "buf %p, nbytes %u", getpid (),
482 fd, fd, func_str, buf, nbytes);
484 size = libc_write (fd, buf, nbytes);
491 int errno_val = errno;
493 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
494 "rv %d, errno = %d", getpid (), fd, fd,
495 func_str, size, errno_val);
499 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
500 getpid (), fd, fd, size, size);
506 writev (int fd, const struct iovec * iov, int iovcnt)
508 const char *func_str;
509 ssize_t size = 0, total = 0;
510 u32 sid = ldp_sid_from_fd (fd);
514 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
517 if ((errno = -ldp_init ()))
520 if (sid != INVALID_SESSION_ID)
522 func_str = "vppcom_session_write";
525 for (i = 0; i < iovcnt; ++i)
528 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s() [%d]: "
529 "sid %u (0x%x), buf %p, nbytes %ld, total %ld",
530 __func__, __LINE__, getpid (), fd, fd, func_str,
531 i, sid, sid, iov[i].iov_base, iov[i].iov_len, total);
533 rv = vppcom_session_write (sid, iov[i].iov_base,
540 if (rv < iov[i].iov_len)
543 printf ("%s:%d: LDP<%d>: fd %d (0x%x): "
544 "rv (%d) < iov[%d].iov_len (%ld)",
545 __func__, __LINE__, getpid (), fd, fd,
546 rv, i, iov[i].iov_len);
552 while ((rv >= 0) && (total == 0));
564 func_str = "libc_writev";
567 printf ("%s:%d: LDP<%d>: fd %d (0x%x): calling %s(): "
568 "iov %p, iovcnt %d\n", __func__, __LINE__, getpid (),
569 fd, fd, func_str, iov, iovcnt);
571 size = libc_writev (fd, iov, iovcnt);
578 int errno_val = errno;
581 "%s:%d: LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
582 "rv %ld, errno = %d\n", __func__, __LINE__, getpid (), fd,
583 fd, func_str, size, errno_val);
587 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
588 __func__, __LINE__, getpid (), fd, fd, size);
594 fcntl (int fd, int cmd, ...)
596 const char *func_str = __func__;
599 u32 sid = ldp_sid_from_fd (fd);
601 if ((errno = -ldp_init ()))
605 if (sid != INVALID_SESSION_ID)
607 int flags = va_arg (ap, int);
610 size = sizeof (flags);
615 func_str = "vppcom_session_attr[SET_FLAGS]";
617 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
618 "sid %u (0x%x) flags %d (0x%x), size %d",
619 getpid (), fd, fd, func_str, sid, sid,
623 vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
627 func_str = "vppcom_session_attr[GET_FLAGS]";
630 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
631 "flags %d (0x%x), size %d", getpid (), fd, fd, func_str, sid,
632 sid, flags, flags, size);
635 vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
639 clib_warning ("LDP<%d>: fd %d (0x%x), cmd %d (F_GETFL): "
640 "%s() returned flags %d (0x%x)",
641 getpid (), fd, fd, cmd, func_str, flags, flags);
658 func_str = "libc_vfcntl";
661 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
662 getpid (), fd, fd, func_str, cmd);
664 rv = libc_vfcntl (fd, cmd, ap);
673 int errno_val = errno;
675 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
676 "rv %d, errno = %d", getpid (), fd, fd,
677 func_str, rv, errno_val);
681 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
682 getpid (), fd, fd, rv, rv);
688 ioctl (int fd, unsigned long int cmd, ...)
690 const char *func_str;
693 u32 sid = ldp_sid_from_fd (fd);
695 if ((errno = -ldp_init ()))
699 if (sid != INVALID_SESSION_ID)
701 func_str = "vppcom_session_attr[GET_NREAD]";
708 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
709 getpid (), fd, fd, func_str, sid, sid);
711 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
716 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
717 u32 size = sizeof (flags);
719 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
720 * non-blocking, the flags should be read here and merged
724 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
725 "sid %u (0x%x), flags %d (0x%x), size %d",
726 getpid (), fd, fd, func_str, sid, sid,
729 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
746 func_str = "libc_vioctl";
749 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
750 getpid (), fd, fd, func_str, cmd);
752 rv = libc_vioctl (fd, cmd, ap);
759 int errno_val = errno;
761 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
762 "rv %d, errno = %d", getpid (), fd, fd,
763 func_str, rv, errno_val);
767 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
768 getpid (), fd, fd, rv, rv);
775 ldp_pselect (int nfds, fd_set * __restrict readfds,
776 fd_set * __restrict writefds,
777 fd_set * __restrict exceptfds,
778 const struct timespec *__restrict timeout,
779 const __sigset_t * __restrict sigmask)
782 char *func_str = "##";
785 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
786 u32 minbits = clib_max (nfds, BITS (uword));
795 if (nfds <= ldp->sid_bit_val)
797 func_str = "libc_pselect";
801 ("LDP<%d>: calling %s(): nfds %d, readfds %p, writefds %p, "
802 "exceptfds %p, timeout %p, sigmask %p", getpid (), func_str, nfds,
803 readfds, writefds, exceptfds, timeout, sigmask);
805 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
810 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
812 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
813 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
814 ldp->sid_bit_val, ldp->sid_bit_val,
815 FD_SETSIZE / 2, FD_SETSIZE / 2);
822 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
823 (f64) 0 : (f64) timeout->tv_sec +
824 (f64) timeout->tv_nsec / (f64) 1000000000 +
825 (f64) (timeout->tv_nsec % 1000000000) / (f64) 1000000000;
827 /* select as fine grained sleep */
831 clib_warning ("LDP<%d>: sleeping for %f seconds",
832 getpid (), time_out);
834 time_out += clib_time_now (&ldp->clib_time);
835 while (clib_time_now (&ldp->clib_time) < time_out)
848 sid_bits = libc_bits = 0;
851 clib_bitmap_validate (ldp->sid_rd_bitmap, minbits);
852 clib_bitmap_validate (ldp->libc_rd_bitmap, minbits);
853 clib_bitmap_validate (ldp->rd_bitmap, minbits);
854 clib_memcpy (ldp->rd_bitmap, readfds,
855 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
859 clib_bitmap_foreach (fd, ldp->rd_bitmap,
861 sid = ldp_sid_from_fd (fd);
863 clib_warning ("LDP<%d>: readfds: fd %d (0x%x), sid %u (0x%x)",
864 getpid (), fd, fd, sid, sid);
865 if (sid == INVALID_SESSION_ID)
866 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
868 clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1);
872 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
873 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
875 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
876 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
879 clib_warning ("LDP<%d>: readfds: sid_bits_set %d, sid_bits %d, "
880 "libc_bits_set %d, libc_bits %d", getpid (),
881 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
885 clib_bitmap_validate (ldp->sid_wr_bitmap, minbits);
886 clib_bitmap_validate (ldp->libc_wr_bitmap, minbits);
887 clib_bitmap_validate (ldp->wr_bitmap, minbits);
888 clib_memcpy (ldp->wr_bitmap, writefds,
889 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
893 clib_bitmap_foreach (fd, ldp->wr_bitmap,
895 sid = ldp_sid_from_fd (fd);
897 clib_warning ("LDP<%d>: writefds: fd %d (0x%x), sid %u (0x%x)",
898 getpid (), fd, fd, sid, sid);
899 if (sid == INVALID_SESSION_ID)
900 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
902 clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1);
906 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
907 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
909 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
910 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
913 clib_warning ("LDP<%d>: writefds: sid_bits_set %d, sid_bits %d, "
914 "libc_bits_set %d, libc_bits %d", getpid (),
915 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
919 clib_bitmap_validate (ldp->sid_ex_bitmap, minbits);
920 clib_bitmap_validate (ldp->libc_ex_bitmap, minbits);
921 clib_bitmap_validate (ldp->ex_bitmap, minbits);
922 clib_memcpy (ldp->ex_bitmap, exceptfds,
923 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
927 clib_bitmap_foreach (fd, ldp->ex_bitmap,
929 sid = ldp_sid_from_fd (fd);
931 clib_warning ("LDP<%d>: exceptfds: fd %d (0x%x), sid %u (0x%x)",
932 getpid (), fd, fd, sid, sid);
933 if (sid == INVALID_SESSION_ID)
934 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
936 clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1);
940 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
941 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
943 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
944 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
947 clib_warning ("LDP<%d>: exceptfds: sid_bits_set %d, sid_bits %d, "
948 "libc_bits_set %d, libc_bits %d", getpid (),
949 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
952 if (PREDICT_FALSE (!sid_bits && !libc_bits))
963 if (!ldp->select_vcl)
965 func_str = "vppcom_select";
968 clib_memcpy (ldp->rd_bitmap, ldp->sid_rd_bitmap,
969 vec_len (ldp->rd_bitmap) *
970 sizeof (clib_bitmap_t));
972 clib_memcpy (ldp->wr_bitmap, ldp->sid_wr_bitmap,
973 vec_len (ldp->wr_bitmap) *
974 sizeof (clib_bitmap_t));
976 clib_memcpy (ldp->ex_bitmap, ldp->sid_ex_bitmap,
977 vec_len (ldp->ex_bitmap) *
978 sizeof (clib_bitmap_t));
980 rv = vppcom_select (sid_bits,
981 readfds ? ldp->rd_bitmap : NULL,
982 writefds ? ldp->wr_bitmap : NULL,
983 exceptfds ? ldp->ex_bitmap : NULL, 0);
994 clib_bitmap_foreach (sid, ldp->rd_bitmap,
996 fd = ldp_fd_from_sid (sid);
997 if (PREDICT_FALSE (fd < 0))
1003 FD_SET (fd, readfds);
1010 clib_bitmap_foreach (sid, ldp->wr_bitmap,
1012 fd = ldp_fd_from_sid (sid);
1013 if (PREDICT_FALSE (fd < 0))
1019 FD_SET (fd, writefds);
1026 clib_bitmap_foreach (sid, ldp->ex_bitmap,
1028 fd = ldp_fd_from_sid (sid);
1029 if (PREDICT_FALSE (fd < 0))
1035 FD_SET (fd, exceptfds);
1039 ldp->select_vcl = 1;
1044 ldp->select_vcl = 0;
1048 struct timespec tspec;
1050 func_str = "libc_pselect";
1053 clib_memcpy (readfds, ldp->libc_rd_bitmap,
1054 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
1056 clib_memcpy (writefds, ldp->libc_wr_bitmap,
1057 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
1059 clib_memcpy (exceptfds, ldp->libc_ex_bitmap,
1060 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
1061 tspec.tv_sec = tspec.tv_nsec = 0;
1062 rv = libc_pselect (libc_bits,
1063 readfds ? readfds : NULL,
1064 writefds ? writefds : NULL,
1065 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1070 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
1074 /* TBD: set timeout to amount of time left */
1075 vec_reset_length (ldp->rd_bitmap);
1076 vec_reset_length (ldp->sid_rd_bitmap);
1077 vec_reset_length (ldp->libc_rd_bitmap);
1078 vec_reset_length (ldp->wr_bitmap);
1079 vec_reset_length (ldp->sid_wr_bitmap);
1080 vec_reset_length (ldp->libc_wr_bitmap);
1081 vec_reset_length (ldp->ex_bitmap);
1082 vec_reset_length (ldp->sid_ex_bitmap);
1083 vec_reset_length (ldp->libc_ex_bitmap);
1089 int errno_val = errno;
1091 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1092 "rv %d, errno = %d", getpid (),
1093 func_str, rv, errno_val);
1097 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1103 select (int nfds, fd_set * __restrict readfds,
1104 fd_set * __restrict writefds,
1105 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1107 struct timespec tspec;
1111 tspec.tv_sec = timeout->tv_sec;
1112 tspec.tv_nsec = timeout->tv_usec * 1000;
1114 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1115 timeout ? &tspec : NULL, NULL);
1118 #ifdef __USE_XOPEN2K
1120 pselect (int nfds, fd_set * __restrict readfds,
1121 fd_set * __restrict writefds,
1122 fd_set * __restrict exceptfds,
1123 const struct timespec *__restrict timeout,
1124 const __sigset_t * __restrict sigmask)
1126 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
1131 socket (int domain, int type, int protocol)
1133 const char *func_str;
1135 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1136 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1138 if ((errno = -ldp_init ()))
1141 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1142 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1145 u8 proto = ((sock_type == SOCK_DGRAM) ?
1146 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1148 func_str = "vppcom_session_create";
1151 clib_warning ("LDP<%d>: : calling %s(): "
1152 "proto %u (%s), is_nonblocking %u",
1153 getpid (), func_str, proto,
1154 vppcom_proto_str (proto), is_nonblocking);
1156 sid = vppcom_session_create (proto, is_nonblocking);
1164 func_str = "ldp_fd_from_sid";
1165 rv = ldp_fd_from_sid (sid);
1168 (void) vppcom_session_close (sid);
1176 func_str = "libc_socket";
1179 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1181 rv = libc_socket (domain, type, protocol);
1188 int errno_val = errno;
1190 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1191 "rv %d, errno = %d",
1192 getpid (), func_str, rv, errno_val);
1196 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1202 * Create two new sockets, of type TYPE in domain DOMAIN and using
1203 * protocol PROTOCOL, which are connected to each other, and put file
1204 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1205 * one will be chosen automatically.
1206 * Returns 0 on success, -1 for errors.
1209 socketpair (int domain, int type, int protocol, int fds[2])
1211 const char *func_str;
1213 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1215 if ((errno = -ldp_init ()))
1218 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1219 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1221 func_str = __func__;
1223 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1229 func_str = "libc_socket";
1232 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1234 rv = libc_socket (domain, type, protocol);
1241 int errno_val = errno;
1243 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1244 "rv %d, errno = %d",
1245 getpid (), func_str, rv, errno_val);
1249 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1255 bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1258 const char *func_str;
1259 u32 sid = ldp_sid_from_fd (fd);
1261 if ((errno = -ldp_init ()))
1264 if (sid != INVALID_SESSION_ID)
1268 func_str = "vppcom_session_bind";
1270 switch (addr->sa_family)
1273 if (len != sizeof (struct sockaddr_in))
1276 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1277 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1282 ep.is_ip4 = VPPCOM_IS_IP4;
1283 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1284 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1288 if (len != sizeof (struct sockaddr_in6))
1291 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1292 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1297 ep.is_ip4 = VPPCOM_IS_IP6;
1298 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1299 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1303 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1304 "Unsupported address family %u!",
1305 getpid (), fd, fd, sid, sid, addr->sa_family);
1306 errno = EAFNOSUPPORT;
1311 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1313 getpid (), fd, fd, func_str, sid, sid, addr, len);
1315 rv = vppcom_session_bind (sid, &ep);
1316 if (rv != VPPCOM_OK)
1324 func_str = "libc_bind";
1327 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1329 getpid (), fd, fd, func_str, addr, len);
1331 rv = libc_bind (fd, addr, len);
1339 int errno_val = errno;
1341 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1342 "rv %d, errno = %d", getpid (), fd, fd,
1343 func_str, rv, errno_val);
1347 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1348 getpid (), fd, fd, rv, rv);
1354 ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1355 vppcom_endpt_t * ep)
1358 int sa_len, copy_len;
1360 if ((errno = -ldp_init ()))
1363 if (addr && len && ep)
1365 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1366 switch (addr->sa_family)
1369 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1370 if (*len > sizeof (struct sockaddr_in))
1371 *len = sizeof (struct sockaddr_in);
1372 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1373 copy_len = *len - sa_len;
1375 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1380 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1381 if (*len > sizeof (struct sockaddr_in6))
1382 *len = sizeof (struct sockaddr_in6);
1383 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1384 copy_len = *len - sa_len;
1386 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1387 __in6_u.__u6_addr8, ep->ip, copy_len);
1400 getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1403 const char *func_str;
1404 u32 sid = ldp_sid_from_fd (fd);
1406 if ((errno = -ldp_init ()))
1409 if (sid != INVALID_SESSION_ID)
1412 u8 addr_buf[sizeof (struct in6_addr)];
1413 u32 size = sizeof (ep);
1416 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1419 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1421 getpid (), fd, fd, func_str, sid, sid, addr, len);
1423 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1424 if (rv != VPPCOM_OK)
1431 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1432 if (rv != VPPCOM_OK)
1441 func_str = "libc_getsockname";
1444 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1446 getpid (), fd, fd, func_str, addr, len);
1448 rv = libc_getsockname (fd, addr, len);
1455 int errno_val = errno;
1457 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1458 "rv %d, errno = %d", getpid (), fd, fd,
1459 func_str, rv, errno_val);
1463 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1464 getpid (), fd, fd, rv, rv);
1470 connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
1473 const char *func_str = __func__;
1474 u32 sid = ldp_sid_from_fd (fd);
1476 if ((errno = -ldp_init ()))
1481 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1482 getpid (), fd, fd, len);
1488 if (sid != INVALID_SESSION_ID)
1492 func_str = "vppcom_session_connect";
1494 switch (addr->sa_family)
1497 if (len != sizeof (struct sockaddr_in))
1500 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1501 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1506 ep.is_ip4 = VPPCOM_IS_IP4;
1507 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1508 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1512 if (len != sizeof (struct sockaddr_in6))
1515 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1516 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1521 ep.is_ip4 = VPPCOM_IS_IP6;
1522 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1523 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1527 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1528 "Unsupported address family %u!",
1529 getpid (), fd, fd, sid, sid, addr->sa_family);
1530 errno = EAFNOSUPPORT;
1535 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1537 getpid (), fd, fd, func_str, sid, sid, addr, len);
1539 rv = vppcom_session_connect (sid, &ep);
1540 if (rv != VPPCOM_OK)
1548 func_str = "libc_connect";
1551 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1553 getpid (), fd, fd, func_str, addr, len);
1555 rv = libc_connect (fd, addr, len);
1563 int errno_val = errno;
1565 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1566 "rv %d, errno = %d", getpid (), fd, fd,
1567 func_str, rv, errno_val);
1571 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1572 getpid (), fd, fd, rv, rv);
1578 getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1581 const char *func_str;
1582 u32 sid = ldp_sid_from_fd (fd);
1584 if ((errno = -ldp_init ()))
1587 if (sid != INVALID_SESSION_ID)
1590 u8 addr_buf[sizeof (struct in6_addr)];
1591 u32 size = sizeof (ep);
1594 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1597 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1599 getpid (), fd, fd, func_str, sid, sid, addr, len);
1601 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1602 if (rv != VPPCOM_OK)
1609 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
1610 if (rv != VPPCOM_OK)
1619 func_str = "libc_getpeername";
1622 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1624 getpid (), fd, fd, func_str, addr, len);
1626 rv = libc_getpeername (fd, addr, len);
1633 int errno_val = errno;
1635 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1636 "rv %d, errno = %d", getpid (), fd, fd,
1637 func_str, rv, errno_val);
1641 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1642 getpid (), fd, fd, rv, rv);
1648 send (int fd, const void *buf, size_t n, int flags)
1651 const char *func_str;
1652 u32 sid = ldp_sid_from_fd (fd);
1654 if ((errno = -ldp_init ()))
1657 if (sid != INVALID_SESSION_ID)
1660 func_str = "vppcom_session_sendto";
1663 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1664 "buf %p, n %u, flags 0x%x",
1665 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1667 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1668 if (size != VPPCOM_OK)
1676 func_str = "libc_send";
1679 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1680 "buf %p, n %u, flags 0x%x",
1681 getpid (), fd, fd, func_str, buf, n, flags);
1683 size = libc_send (fd, buf, n, flags);
1690 int errno_val = errno;
1692 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1693 "rv %d, errno = %d", getpid (), fd, fd,
1694 func_str, size, errno_val);
1698 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1699 getpid (), fd, fd, size, size);
1705 sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1708 const char *func_str;
1709 u32 sid = ldp_sid_from_fd (out_fd);
1711 if ((errno = -ldp_init ()))
1714 if (sid != INVALID_SESSION_ID)
1717 ssize_t results = 0;
1718 size_t n_bytes_left = len;
1719 size_t bytes_to_read;
1723 u32 flags, flags_len = sizeof (flags);
1725 func_str = "vppcom_session_attr[GET_FLAGS]";
1726 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1728 if (PREDICT_FALSE (rv != VPPCOM_OK))
1730 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1731 "sid %u (0x%x), returned %d (%s)!", getpid (),
1732 out_fd, out_fd, func_str, sid, sid, rv,
1733 vppcom_retval_str (rv));
1735 vec_reset_length (ldp->io_buffer);
1743 off_t off = lseek (in_fd, *offset, SEEK_SET);
1744 if (PREDICT_FALSE (off == -1))
1748 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1749 "SEEK_SET failed: in_fd %d, offset %p, "
1750 "*offset %ld, rv %ld, errno %d", getpid (),
1751 out_fd, out_fd, in_fd, offset, *offset, off,
1758 ASSERT (off == *offset);
1763 func_str = "vppcom_session_attr[GET_NWRITE]";
1764 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1768 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1769 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1770 sid, sid, size, vppcom_retval_str (size));
1771 vec_reset_length (ldp->io_buffer);
1777 bytes_to_read = size;
1780 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1781 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1782 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1785 if (bytes_to_read == 0)
1787 if (flags & O_NONBLOCK)
1792 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1794 getpid (), out_fd, out_fd, sid, sid);
1802 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
1803 vec_validate (ldp->io_buffer, bytes_to_read);
1804 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
1807 func_str = "libc_read";
1809 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1810 "io_buffer %p, bytes_to_read %lu, rv %d, "
1811 "errno %d", getpid (), out_fd, out_fd, func_str,
1812 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
1818 vec_reset_length (ldp->io_buffer);
1824 func_str = "vppcom_session_write";
1827 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1828 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
1829 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
1830 results, n_bytes_left);
1832 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
1835 if (size == VPPCOM_EAGAIN)
1837 if (flags & O_NONBLOCK)
1843 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1844 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1854 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1855 "sid %u, io_buffer %p, nbytes %u "
1857 getpid (), out_fd, out_fd, func_str,
1858 sid, ldp->io_buffer, nbytes,
1859 size, vppcom_retval_str (size));
1863 vec_reset_length (ldp->io_buffer);
1872 ASSERT (n_bytes_left >= nbytes);
1873 n_bytes_left = n_bytes_left - nbytes;
1875 while (n_bytes_left > 0);
1878 vec_reset_length (ldp->io_buffer);
1881 off_t off = lseek (in_fd, *offset, SEEK_SET);
1882 if (PREDICT_FALSE (off == -1))
1886 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1887 "in_fd %d, offset %p, *offset %ld, "
1888 "rv %ld, errno %d", getpid (), in_fd,
1889 offset, *offset, off, errno_val);
1895 ASSERT (off == *offset);
1896 *offset += results + 1;
1908 func_str = "libc_send";
1911 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1912 "in_fd %d, offset %p, len %u",
1913 getpid (), out_fd, out_fd, func_str,
1914 in_fd, offset, len);
1916 size = libc_sendfile (out_fd, in_fd, offset, len);
1924 int errno_val = errno;
1926 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1927 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1928 func_str, size, errno_val);
1932 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1933 getpid (), out_fd, out_fd, size, size);
1939 sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1941 return sendfile (out_fd, in_fd, offset, len);
1945 recv (int fd, void *buf, size_t n, int flags)
1948 const char *func_str;
1949 u32 sid = ldp_sid_from_fd (fd);
1951 if ((errno = -ldp_init ()))
1954 if (sid != INVALID_SESSION_ID)
1956 func_str = "vppcom_session_recvfrom";
1959 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1960 "sid %u (0x%x), buf %p, n %u, flags 0x%x", getpid (),
1961 fd, fd, func_str, sid, sid, buf, n, flags);
1963 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1972 func_str = "libc_recv";
1975 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1976 "buf %p, n %u, flags 0x%x", getpid (),
1977 fd, fd, func_str, buf, n, flags);
1979 size = libc_recv (fd, buf, n, flags);
1986 int errno_val = errno;
1988 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1989 "rv %d, errno = %d", getpid (), fd, fd,
1990 func_str, size, errno_val);
1994 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1995 getpid (), fd, fd, size, size);
2001 sendto (int fd, const void *buf, size_t n, int flags,
2002 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2005 const char *func_str = __func__;
2006 u32 sid = ldp_sid_from_fd (fd);
2008 if ((errno = -ldp_init ()))
2011 if (sid != INVALID_SESSION_ID)
2013 vppcom_endpt_t *ep = 0;
2019 switch (addr->sa_family)
2022 ep->is_ip4 = VPPCOM_IS_IP4;
2024 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2026 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2030 ep->is_ip4 = VPPCOM_IS_IP6;
2032 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2034 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2038 errno = EAFNOSUPPORT;
2044 func_str = "vppcom_session_sendto";
2047 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2048 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2049 getpid (), fd, fd, func_str, sid, sid, buf, n,
2052 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2061 func_str = "libc_sendto";
2064 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2065 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2066 getpid (), fd, fd, func_str, buf, n, flags,
2069 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2077 int errno_val = errno;
2079 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2080 "rv %d, errno = %d", getpid (), fd, fd,
2081 func_str, size, errno_val);
2085 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2086 getpid (), fd, fd, size, size);
2092 recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2093 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2096 const char *func_str;
2097 u32 sid = ldp_sid_from_fd (fd);
2099 if ((errno = -ldp_init ()))
2102 if (sid != INVALID_SESSION_ID)
2105 u8 src_addr[sizeof (struct sockaddr_in6)];
2107 func_str = "vppcom_session_recvfrom";
2110 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2111 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2112 getpid (), fd, fd, func_str, sid, sid, buf, n,
2117 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2120 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2123 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2133 func_str = "libc_recvfrom";
2136 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2137 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2138 getpid (), fd, fd, func_str, buf, n, flags,
2141 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2148 int errno_val = errno;
2150 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2151 "rv %d, errno = %d", getpid (), fd, fd,
2152 func_str, size, errno_val);
2156 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2157 getpid (), fd, fd, size, size);
2163 sendmsg (int fd, const struct msghdr * message, int flags)
2166 const char *func_str;
2167 u32 sid = ldp_sid_from_fd (fd);
2169 if ((errno = -ldp_init ()))
2172 if (sid != INVALID_SESSION_ID)
2174 func_str = __func__;
2176 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2182 func_str = "libc_sendmsg";
2185 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2186 "message %p, flags 0x%x",
2187 getpid (), fd, fd, func_str, message, flags);
2189 size = libc_sendmsg (fd, message, flags);
2196 int errno_val = errno;
2198 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2199 "rv %d, errno = %d", getpid (), fd, fd,
2200 func_str, size, errno_val);
2204 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2205 getpid (), fd, fd, size, size);
2212 sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2215 const char *func_str;
2216 u32 sid = ldp_sid_from_fd (fd);
2218 if ((errno = -ldp_init ()))
2221 if (sid != INVALID_SESSION_ID)
2223 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2229 func_str = "libc_sendmmsg";
2232 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2233 "vmessages %p, vlen %u, flags 0x%x",
2234 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2236 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2243 int errno_val = errno;
2245 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2246 "rv %d, errno = %d", getpid (), fd, fd,
2247 func_str, size, errno_val);
2251 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2252 getpid (), fd, fd, size, size);
2259 recvmsg (int fd, struct msghdr * message, int flags)
2262 const char *func_str;
2263 u32 sid = ldp_sid_from_fd (fd);
2265 if ((errno = -ldp_init ()))
2268 if (sid != INVALID_SESSION_ID)
2270 func_str = __func__;
2272 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2278 func_str = "libc_recvmsg";
2281 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2282 "message %p, flags 0x%x",
2283 getpid (), fd, fd, func_str, message, flags);
2285 size = libc_recvmsg (fd, message, flags);
2292 int errno_val = errno;
2294 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2295 "rv %d, errno = %d", getpid (), fd, fd,
2296 func_str, size, errno_val);
2300 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2301 getpid (), fd, fd, size, size);
2308 recvmmsg (int fd, struct mmsghdr *vmessages,
2309 unsigned int vlen, int flags, struct timespec *tmo)
2312 const char *func_str;
2313 u32 sid = ldp_sid_from_fd (fd);
2315 if ((errno = -ldp_init ()))
2318 if (sid != INVALID_SESSION_ID)
2320 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2326 func_str = "libc_recvmmsg";
2329 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2330 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2331 getpid (), fd, fd, func_str, vmessages, vlen,
2334 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2341 int errno_val = errno;
2343 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2344 "rv %d, errno = %d", getpid (), fd, fd,
2345 func_str, size, errno_val);
2349 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2350 getpid (), fd, fd, size, size);
2357 getsockopt (int fd, int level, int optname,
2358 void *__restrict optval, socklen_t * __restrict optlen)
2361 const char *func_str = __func__;
2362 u32 sid = ldp_sid_from_fd (fd);
2363 u32 buflen = optlen ? (u32) * optlen : 0;
2365 if ((errno = -ldp_init ()))
2368 if (sid != INVALID_SESSION_ID)
2378 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
2380 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2382 getpid (), fd, fd, func_str, sid, sid);
2383 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2387 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
2389 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2391 getpid (), fd, fd, func_str, sid, sid);
2392 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2396 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
2398 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2400 getpid (), fd, fd, func_str, sid, sid);
2401 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2405 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
2407 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2408 "sid %u (0x%x), SOL_TCP",
2409 getpid (), fd, fd, func_str, sid, sid);
2410 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2414 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2417 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2418 "SOL_TCP, TCP_INFO, optval %p, "
2419 "optlen %d: #LDP-NOP#",
2420 getpid (), fd, fd, sid, sid,
2422 memset (optval, 0, *optlen);
2430 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2431 "sid %u (0x%x), SOL_TCP, "
2432 "optname %d unsupported!",
2433 getpid (), fd, fd, func_str, sid, sid, optname);
2441 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
2443 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2445 getpid (), fd, fd, func_str, sid, sid);
2446 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2451 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2452 "sid %u (0x%x), SOL_IPV6, "
2453 "optname %d unsupported!",
2454 getpid (), fd, fd, func_str, sid, sid, optname);
2462 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
2464 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2466 getpid (), fd, fd, func_str, sid, sid);
2467 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2471 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
2473 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2475 getpid (), fd, fd, func_str, sid, sid);
2476 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2480 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
2482 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2484 getpid (), fd, fd, func_str, sid, sid);
2485 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2487 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2490 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
2492 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2493 "sid %u (0x%x), optlen %d",
2494 getpid (), fd, fd, func_str, sid, sid, buflen);
2495 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2499 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
2501 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2502 "sid %u (0x%x), optlen %d",
2503 getpid (), fd, fd, func_str, sid, sid, buflen);
2504 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2508 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
2510 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2512 getpid (), fd, fd, func_str, sid, sid);
2513 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2517 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
2519 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2521 getpid (), fd, fd, func_str, sid, sid);
2522 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2526 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
2528 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2530 getpid (), fd, fd, func_str, sid, sid);
2531 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2536 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2537 "sid %u (0x%x), SOL_SOCKET, "
2538 "optname %d unsupported!",
2539 getpid (), fd, fd, func_str, sid, sid, optname);
2547 if (rv != VPPCOM_OK)
2555 func_str = "libc_getsockopt";
2558 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2559 "optname %d, optval %p, optlen %d",
2560 getpid (), fd, fd, func_str, level, optname,
2563 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2570 int errno_val = errno;
2572 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2573 "rv %d, errno = %d", getpid (), fd, fd,
2574 func_str, rv, errno_val);
2578 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2579 getpid (), fd, fd, rv, rv);
2585 setsockopt (int fd, int level, int optname,
2586 const void *optval, socklen_t optlen)
2589 const char *func_str = __func__;
2590 u32 sid = ldp_sid_from_fd (fd);
2592 if ((errno = -ldp_init ()))
2595 if (sid != INVALID_SESSION_ID)
2605 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
2607 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2609 getpid (), fd, fd, func_str, sid, sid);
2610 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2611 (void *) optval, &optlen);
2614 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
2616 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2618 getpid (), fd, fd, func_str, sid, sid);
2619 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2620 (void *) optval, &optlen);
2623 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
2625 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2627 getpid (), fd, fd, func_str, sid, sid);
2628 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2629 (void *) optval, &optlen);
2632 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
2634 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2635 "sid %u (0x%x), SOL_TCP",
2636 getpid (), fd, fd, func_str, sid, sid);
2637 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2638 (void *) optval, &optlen);
2642 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2643 "sid %u (0x%x), SOL_TCP, "
2644 "optname %d unsupported!",
2645 getpid (), fd, fd, func_str, sid, sid, optname);
2653 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
2655 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2657 getpid (), fd, fd, func_str, sid, sid);
2658 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2659 (void *) optval, &optlen);
2663 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2664 "sid %u (0x%x), SOL_IPV6, "
2665 "optname %d unsupported!",
2666 getpid (), fd, fd, func_str, sid, sid, optname);
2674 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
2676 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2678 getpid (), fd, fd, func_str, sid, sid);
2679 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2680 (void *) optval, &optlen);
2683 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
2685 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2687 getpid (), fd, fd, func_str, sid, sid);
2688 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2689 (void *) optval, &optlen);
2692 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
2694 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2696 getpid (), fd, fd, func_str, sid, sid);
2697 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2698 (void *) optval, &optlen);
2702 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2703 "sid %u (0x%x), SOL_SOCKET, "
2704 "optname %d unsupported!",
2705 getpid (), fd, fd, func_str, sid, sid, optname);
2713 if (rv != VPPCOM_OK)
2721 func_str = "libc_setsockopt";
2724 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2725 "optname %d, optval %p, optlen %d",
2726 getpid (), fd, fd, func_str, level, optname,
2729 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2736 int errno_val = errno;
2738 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2739 "rv %d, errno = %d", getpid (), fd, fd,
2740 func_str, rv, errno_val);
2744 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2745 getpid (), fd, fd, rv, rv);
2751 listen (int fd, int n)
2754 const char *func_str;
2755 u32 sid = ldp_sid_from_fd (fd);
2757 if ((errno = -ldp_init ()))
2760 if (sid != INVALID_SESSION_ID)
2762 func_str = "vppcom_session_listen";
2766 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2767 getpid (), fd, fd, func_str, sid, sid, n);
2769 rv = vppcom_session_listen (sid, n);
2770 if (rv != VPPCOM_OK)
2778 func_str = "libc_listen";
2781 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): n %d",
2782 getpid (), fd, fd, func_str, n);
2784 rv = libc_listen (fd, n);
2791 int errno_val = errno;
2793 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2794 "rv %d, errno = %d", getpid (), fd, fd,
2795 func_str, rv, errno_val);
2799 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2800 getpid (), fd, fd, rv, rv);
2806 ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2807 socklen_t * __restrict addr_len, int flags)
2810 const char *func_str;
2811 u32 listen_sid = ldp_sid_from_fd (listen_fd);
2814 if ((errno = -ldp_init ()))
2817 if (listen_sid != INVALID_SESSION_ID)
2820 u8 src_addr[sizeof (struct sockaddr_in6)];
2821 memset (&ep, 0, sizeof (ep));
2824 func_str = "vppcom_session_accept";
2827 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2828 "listen sid %u (0x%x), ep %p, flags 0x%x",
2829 getpid (), listen_fd, listen_fd, func_str,
2830 listen_sid, listen_sid, ep, flags);
2832 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2835 errno = -accept_sid;
2840 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
2841 if (rv != VPPCOM_OK)
2843 (void) vppcom_session_close ((u32) accept_sid);
2849 func_str = "ldp_fd_from_sid";
2851 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2852 "accept sid %u (0x%x), ep %p, flags 0x%x",
2853 getpid (), listen_fd, listen_fd,
2854 func_str, accept_sid, accept_sid, ep, flags);
2855 rv = ldp_fd_from_sid ((u32) accept_sid);
2858 (void) vppcom_session_close ((u32) accept_sid);
2867 func_str = "libc_accept4";
2870 clib_warning ("LDP<%d>: listen fd %d (0x%x): calling %s(): "
2871 "addr %p, addr_len %p, flags 0x%x",
2872 getpid (), listen_fd, listen_fd, func_str,
2873 addr, addr_len, flags);
2875 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
2882 int errno_val = errno;
2884 clib_warning ("LDP<%d>: ERROR: listen fd %d (0x%x): %s() failed! "
2885 "rv %d, errno = %d", getpid (), listen_fd,
2886 listen_fd, func_str, rv, errno_val);
2890 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2891 getpid (), listen_fd, listen_fd, rv, rv);
2897 accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2900 return ldp_accept4 (fd, addr, addr_len, flags);
2904 accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2906 return ldp_accept4 (fd, addr, addr_len, 0);
2910 shutdown (int fd, int how)
2913 const char *func_str;
2914 u32 sid = ldp_sid_from_fd (fd);
2916 if ((errno = -ldp_init ()))
2919 if (sid != INVALID_SESSION_ID)
2921 func_str = __func__;
2923 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2929 func_str = "libc_shutdown";
2932 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2933 getpid (), fd, fd, func_str, how);
2935 rv = libc_shutdown (fd, how);
2942 int errno_val = errno;
2944 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2945 "rv %d, errno = %d", getpid (), fd, fd,
2946 func_str, rv, errno_val);
2950 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2951 getpid (), fd, fd, rv, rv);
2957 epoll_create1 (int flags)
2959 const char *func_str;
2962 if ((errno = -ldp_init ()))
2965 func_str = "vppcom_epoll_create";
2968 clib_warning ("LDP<%d>: calling %s()", getpid (), func_str);
2970 rv = vppcom_epoll_create ();
2972 if (PREDICT_FALSE (rv < 0))
2978 rv = ldp_fd_from_sid ((u32) rv);
2984 int errno_val = errno;
2986 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2987 "rv %d, errno = %d",
2988 getpid (), func_str, rv, errno_val);
2992 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
2998 epoll_create (int size)
3000 return epoll_create1 (0);
3004 epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3007 const char *func_str;
3008 u32 vep_idx = ldp_sid_from_fd (epfd);
3010 if ((errno = -ldp_init ()))
3013 if (vep_idx != INVALID_SESSION_ID)
3015 u32 sid = ldp_sid_from_fd (fd);
3017 if (sid != INVALID_SESSION_ID)
3019 func_str = "vppcom_epoll_create";
3022 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3023 "vep_idx %d (0x%x), op %d, sid %u (0x%x), event %p",
3024 getpid (), epfd, epfd, func_str, vep_idx, vep_idx,
3027 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3028 if (rv != VPPCOM_OK)
3037 u32 size = sizeof (epfd);
3039 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3040 epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD,
3044 func_str = "libc_epoll_create1";
3047 clib_warning ("LDP<%d>: calling %s(): EPOLL_CLOEXEC",
3048 getpid (), func_str);
3050 epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3057 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3058 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3067 else if (PREDICT_FALSE (epfd < 0))
3074 rv = libc_epoll_ctl (epfd, op, fd, event);
3079 func_str = "libc_epoll_ctl";
3082 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3083 "op %d, fd %d (0x%x), event %p",
3084 getpid (), epfd, epfd, func_str, op, fd, fd, event);
3086 rv = libc_epoll_ctl (epfd, op, fd, event);
3094 int errno_val = errno;
3096 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
3097 "rv %d, errno = %d", getpid (), fd, fd,
3098 func_str, rv, errno_val);
3102 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3103 getpid (), fd, fd, rv, rv);
3109 ldp_epoll_pwait (int epfd, struct epoll_event *events,
3110 int maxevents, int timeout, const sigset_t * sigmask)
3112 const char *func_str;
3114 double time_to_wait = (double) 0;
3115 double time_out, now = 0;
3116 u32 vep_idx = ldp_sid_from_fd (epfd);
3119 if ((errno = -ldp_init ()))
3122 if (PREDICT_FALSE (!events || (timeout < -1)))
3128 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3130 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3131 getpid (), epfd, epfd, vep_idx, vep_idx);
3136 time_to_wait = ((timeout >= 0) ? (double) timeout / (double) 1000 : 0);
3137 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
3139 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3140 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3141 if (PREDICT_FALSE (libc_epfd < 0))
3149 clib_warning ("LDP<%d>: epfd %d (0x%x): vep_idx %d (0x%x), "
3150 "libc_epfd %d (0x%x), events %p, maxevents %d, "
3151 "timeout %d, sigmask %p", getpid (), epfd, epfd,
3152 vep_idx, vep_idx, libc_epfd, libc_epfd, events,
3153 maxevents, timeout, sigmask);
3156 if (!ldp->epoll_wait_vcl)
3158 func_str = "vppcom_epoll_wait";
3161 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3162 "vep_idx %d (0x%x), events %p, maxevents %d",
3163 getpid (), epfd, epfd, func_str,
3164 vep_idx, vep_idx, events, maxevents);
3166 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3169 ldp->epoll_wait_vcl = 1;
3180 ldp->epoll_wait_vcl = 0;
3184 func_str = "libc_epoll_pwait";
3187 clib_warning ("LDP<%d>: epfd %d (0x%x): calling %s(): "
3188 "libc_epfd %d (0x%x), events %p, "
3189 "maxevents %d, sigmask %p",
3190 getpid (), epfd, epfd, func_str,
3191 libc_epfd, libc_epfd, events, maxevents, sigmask);
3193 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
3199 now = clib_time_now (&ldp->clib_time);
3201 while (now < time_out);
3210 int errno_val = errno;
3212 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): %s() failed! "
3213 "rv %d, errno = %d", getpid (), epfd, epfd,
3214 func_str, rv, errno_val);
3218 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3219 getpid (), epfd, epfd, rv, rv);
3225 epoll_pwait (int epfd, struct epoll_event *events,
3226 int maxevents, int timeout, const sigset_t * sigmask)
3228 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3232 epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3234 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
3238 poll (struct pollfd *fds, nfds_t nfds, int timeout)
3240 const char *func_str = __func__;
3241 int rv, i, n_libc_fds, n_revents;
3244 double wait_for_time;
3247 clib_warning ("LDP<%d>: fds %p, nfds %d, timeout %d",
3248 getpid (), fds, nfds, timeout);
3251 wait_for_time = (f64) timeout / 1000;
3256 for (i = 0; i < nfds; i++)
3261 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), .events = 0x%x, "
3262 ".revents = 0x%x", getpid (), i, fds[i].fd,
3263 fds[i].fd, fds[i].events, fds[i].revents);
3265 sid = ldp_sid_from_fd (fds[i].fd);
3266 if (sid != INVALID_SESSION_ID)
3268 fds[i].fd = -fds[i].fd;
3269 vec_add2 (ldp->vcl_poll, vp, 1);
3272 vp->events = fds[i].events;
3273 #ifdef __USE_XOPEN2K
3274 if (fds[i].events & POLLRDNORM)
3275 vp->events |= POLLIN;
3276 if (fds[i].events & POLLWRNORM)
3277 vp->events |= POLLOUT;
3279 vp->revents = &fds[i].revents;
3289 if (vec_len (ldp->vcl_poll))
3291 func_str = "vppcom_poll";
3294 clib_warning ("LDP<%d>: calling %s(): "
3295 "vcl_poll %p, n_sids %u (0x%x): "
3297 getpid (), func_str, ldp->vcl_poll,
3298 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
3301 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
3314 func_str = "libc_poll";
3317 clib_warning ("LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
3318 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
3320 rv = libc_poll (fds, nfds, 0);
3333 while ((wait_for_time == -1) ||
3334 (clib_time_now (&ldp->clib_time) < wait_for_time));
3338 vec_foreach (vp, ldp->vcl_poll)
3340 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
3341 #ifdef __USE_XOPEN2K
3342 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3343 (fds[vp->fds_ndx].events & POLLRDNORM))
3344 fds[vp->fds_ndx].revents |= POLLRDNORM;
3345 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3346 (fds[vp->fds_ndx].events & POLLWRNORM))
3347 fds[vp->fds_ndx].revents |= POLLWRNORM;
3350 vec_reset_length (ldp->vcl_poll);
3356 int errno_val = errno;
3358 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3359 "rv %d, errno = %d", getpid (),
3360 func_str, rv, errno_val);
3365 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3366 "n_libc_fds %d", getpid (), rv, rv,
3367 vec_len (ldp->vcl_poll), n_libc_fds);
3369 for (i = 0; i < nfds; i++)
3374 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3375 ".events = 0x%x, .revents = 0x%x",
3376 getpid (), i, fds[i].fd, fds[i].fd,
3377 fds[i].events, fds[i].revents);
3388 ppoll (struct pollfd *fds, nfds_t nfds,
3389 const struct timespec *timeout, const sigset_t * sigmask)
3391 if ((errno = -ldp_init ()))
3394 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3402 void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
3404 void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
3407 * This function is called when the library is loaded
3410 ldp_constructor (void)
3412 swrap_constructor ();
3413 if (ldp_init () != 0)
3414 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
3417 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
3421 * This function is called when the library is unloaded
3424 ldp_destructor (void)
3426 swrap_destructor ();
3429 vppcom_app_destroy ();
3433 /* Don't use clib_warning() here because that calls writev()
3434 * which will call ldp_init().
3436 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3437 __func__, __LINE__, getpid ());
3442 * fd.io coding-style-patch-verification: ON
3445 * eval: (c-set-style "gnu")