blob: 1cf2445f4fcfde63994747713aaf858f12a40593 [file] [log] [blame]
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <unistd.h>
16#include <stdio.h>
17#include <signal.h>
18#include <dlfcn.h>
19#include <pthread.h>
20#include <time.h>
21#include <stdarg.h>
shrinivasan ganapathy1d359632017-10-15 15:46:09 -070022#include <sys/resource.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050023#include <netinet/tcp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070024
Dave Wallace2a865272018-02-07 21:00:42 -050025#include <vcl/ldp_socket_wrapper.h>
26#include <vcl/ldp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070027#include <sys/time.h>
28
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040029#include <vcl/vppcom.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050030#include <vppinfra/time.h>
31#include <vppinfra/bitmap.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070032
33#define HAVE_CONSTRUCTOR_ATTRIBUTE
34#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
35#define CONSTRUCTOR_ATTRIBUTE \
36 __attribute__ ((constructor))
37#else
38#define CONSTRUCTOR_ATTRIBUTE
39#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
40
41#define HAVE_DESTRUCTOR_ATTRIBUTE
42#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
43#define DESTRUCTOR_ATTRIBUTE \
44 __attribute__ ((destructor))
45#else
46#define DESTRUCTOR_ATTRIBUTE
47#endif
48
Dave Wallace048b1d62018-01-03 22:24:41 -050049typedef struct
50{
51 int init;
Dave Wallace2a865272018-02-07 21:00:42 -050052 char app_name[LDP_APP_NAME_MAX];
Dave Wallace048b1d62018-01-03 22:24:41 -050053 u32 sid_bit_val;
54 u32 sid_bit_mask;
55 u32 debug;
56 u8 *io_buffer;
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;
67 vcl_poll_t *vcl_poll;
68 u8 select_vcl;
69 u8 epoll_wait_vcl;
Dave Wallace2a865272018-02-07 21:00:42 -050070} ldp_main_t;
71#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070072
Dave Wallace2a865272018-02-07 21:00:42 -050073static 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,
Dave Wallace048b1d62018-01-03 22:24:41 -050077};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070078
Dave Wallace2a865272018-02-07 21:00:42 -050079static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070080
81/*
82 * RETURN: 0 on success or -1 on error.
83 * */
Dave Wallace048b1d62018-01-03 22:24:41 -050084static inline void
Dave Wallace2a865272018-02-07 21:00:42 -050085ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070086{
Dave Wallace2a865272018-02-07 21:00:42 -050087 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
88 "ldp-%d-%s", getpid (), app_name);
Dave Wallace048b1d62018-01-03 22:24:41 -050089
Dave Wallace2a865272018-02-07 21:00:42 -050090 if (rv >= LDP_APP_NAME_MAX)
91 app_name[LDP_APP_NAME_MAX - 1] = 0;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070092}
93
Dave Wallace048b1d62018-01-03 22:24:41 -050094static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -050095ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070096{
Dave Wallace2a865272018-02-07 21:00:42 -050097 if (ldp->app_name[0] == '\0')
98 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -050099
Dave Wallace2a865272018-02-07 21:00:42 -0500100 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700101}
102
Dave Wallace048b1d62018-01-03 22:24:41 -0500103static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500104ldp_fd_from_sid (u32 sid)
Dave Wallace048b1d62018-01-03 22:24:41 -0500105{
Dave Wallace2a865272018-02-07 21:00:42 -0500106 if (PREDICT_FALSE (sid >= ldp->sid_bit_val))
Dave Wallace048b1d62018-01-03 22:24:41 -0500107 return -EMFILE;
108 else
Dave Wallace2a865272018-02-07 21:00:42 -0500109 return (sid | ldp->sid_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500110}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700111
Dave Wallace048b1d62018-01-03 22:24:41 -0500112static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500113ldp_fd_is_sid (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500114{
Dave Wallace2a865272018-02-07 21:00:42 -0500115 return ((u32) fd & ldp->sid_bit_val) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500116}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700117
Dave Wallace048b1d62018-01-03 22:24:41 -0500118static inline u32
Dave Wallace2a865272018-02-07 21:00:42 -0500119ldp_sid_from_fd (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500120{
Dave Wallace2a865272018-02-07 21:00:42 -0500121 return (ldp_fd_is_sid (fd) ? ((u32) fd & ldp->sid_bit_mask) :
Dave Wallace048b1d62018-01-03 22:24:41 -0500122 INVALID_SESSION_ID);
123}
124
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700125static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500126ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700127{
Dave Wallace048b1d62018-01-03 22:24:41 -0500128 int rv = 0;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700129
Dave Wallace2a865272018-02-07 21:00:42 -0500130 if (PREDICT_FALSE (!ldp->init))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700131 {
Dave Wallace2a865272018-02-07 21:00:42 -0500132 ldp->init = 1;
133 rv = vppcom_app_create (ldp_get_app_name ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500134 if (rv == VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700135 {
Dave Wallace2a865272018-02-07 21:00:42 -0500136 char *env_var_str = getenv (LDP_ENV_DEBUG);
Dave Wallace048b1d62018-01-03 22:24:41 -0500137 if (env_var_str)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700138 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500139 u32 tmp;
140 if (sscanf (env_var_str, "%u", &tmp) != 1)
Dave Wallace2a865272018-02-07 21:00:42 -0500141 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level "
142 "specified in the env var " LDP_ENV_DEBUG
Dave Wallace048b1d62018-01-03 22:24:41 -0500143 " (%s)!", getpid (), env_var_str);
144 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700145 {
Dave Wallace2a865272018-02-07 21:00:42 -0500146 ldp->debug = tmp;
147 clib_warning ("LDP<%d>: configured LDP debug level (%u) "
148 "from the env var " LDP_ENV_DEBUG "!",
149 getpid (), ldp->debug);
Dave Wallace048b1d62018-01-03 22:24:41 -0500150 }
151 }
152
Dave Wallace2a865272018-02-07 21:00:42 -0500153 env_var_str = getenv (LDP_ENV_APP_NAME);
Dave Wallace048b1d62018-01-03 22:24:41 -0500154 if (env_var_str)
155 {
Dave Wallace2a865272018-02-07 21:00:42 -0500156 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);
Dave Wallace048b1d62018-01-03 22:24:41 -0500160 }
161
Dave Wallace2a865272018-02-07 21:00:42 -0500162 env_var_str = getenv (LDP_ENV_SID_BIT);
Dave Wallace048b1d62018-01-03 22:24:41 -0500163 if (env_var_str)
164 {
165 u32 sb;
166 if (sscanf (env_var_str, "%u", &sb) != 1)
167 {
Dave Wallace2a865272018-02-07 21:00:42 -0500168 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit "
Dave Wallace048b1d62018-01-03 22:24:41 -0500169 "specified in the env var "
Dave Wallace2a865272018-02-07 21:00:42 -0500170 LDP_ENV_SID_BIT " (%s)!"
Dave Wallace048b1d62018-01-03 22:24:41 -0500171 "sid bit value %d (0x%x)",
172 getpid (), env_var_str,
Dave Wallace2a865272018-02-07 21:00:42 -0500173 ldp->sid_bit_val, ldp->sid_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500174 }
Dave Wallace2a865272018-02-07 21:00:42 -0500175 else if (sb < LDP_SID_BIT_MIN)
Dave Wallace048b1d62018-01-03 22:24:41 -0500176 {
Dave Wallace2a865272018-02-07 21:00:42 -0500177 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
178 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500179
Dave Wallace2a865272018-02-07 21:00:42 -0500180 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) "
Dave Wallace048b1d62018-01-03 22:24:41 -0500181 "specified in the env var "
Dave Wallace2a865272018-02-07 21:00:42 -0500182 LDP_ENV_SID_BIT " (%s) is too small. "
183 "Using LDP_SID_BIT_MIN (%d)! "
Dave Wallace048b1d62018-01-03 22:24:41 -0500184 "sid bit value %d (0x%x)",
Dave Wallace2a865272018-02-07 21:00:42 -0500185 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
186 ldp->sid_bit_val, ldp->sid_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500187 }
Dave Wallace2a865272018-02-07 21:00:42 -0500188 else if (sb > LDP_SID_BIT_MAX)
Dave Wallace048b1d62018-01-03 22:24:41 -0500189 {
Dave Wallace2a865272018-02-07 21:00:42 -0500190 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
191 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500192
Dave Wallace2a865272018-02-07 21:00:42 -0500193 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) "
Dave Wallace048b1d62018-01-03 22:24:41 -0500194 "specified in the env var "
Dave Wallace2a865272018-02-07 21:00:42 -0500195 LDP_ENV_SID_BIT " (%s) is too big. "
196 "Using LDP_SID_BIT_MAX (%d)! "
Dave Wallace048b1d62018-01-03 22:24:41 -0500197 "sid bit value %d (0x%x)",
Dave Wallace2a865272018-02-07 21:00:42 -0500198 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
199 ldp->sid_bit_val, ldp->sid_bit_val);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700200 }
201 else
202 {
Dave Wallace2a865272018-02-07 21:00:42 -0500203 ldp->sid_bit_val = (1 << sb);
204 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500205
Dave Wallace2a865272018-02-07 21:00:42 -0500206 clib_warning ("LDP<%d>: configured LDP sid bit (%u) "
207 "from " LDP_ENV_SID_BIT
Dave Wallace048b1d62018-01-03 22:24:41 -0500208 "! sid bit value %d (0x%x)", getpid (),
Dave Wallace2a865272018-02-07 21:00:42 -0500209 sb, ldp->sid_bit_val, ldp->sid_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500210 }
211 }
212
Dave Wallace2a865272018-02-07 21:00:42 -0500213 clib_time_init (&ldp->clib_time);
214 clib_warning ("LDP<%d>: LDP initialization: done!", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500215 }
216 else
217 {
Dave Wallace2a865272018-02-07 21:00:42 -0500218 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_init: vppcom_app_create()"
Dave Wallace048b1d62018-01-03 22:24:41 -0500219 " failed! rv = %d (%s)\n",
220 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace2a865272018-02-07 21:00:42 -0500221 ldp->init = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500222 }
223 }
224 return rv;
225}
226
227int
228close (int fd)
229{
230 int rv;
231 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -0500232 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500233
Dave Wallace2a865272018-02-07 21:00:42 -0500234 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500235 return -1;
236
237 if (sid != INVALID_SESSION_ID)
238 {
239 int epfd;
240
241 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
242 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
243 if (epfd > 0)
244 {
245 func_str = "libc_close";
246
Dave Wallace2a865272018-02-07 21:00:42 -0500247 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500248 clib_warning
249 ("LDP<%d>: fd %d (0x%x): calling %s(): epfd %u (0x%x)",
250 getpid (), fd, fd, func_str, epfd, epfd);
251
252 rv = libc_close (epfd);
253 if (rv < 0)
254 {
255 u32 size = sizeof (epfd);
256 epfd = 0;
257
258 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
259 &epfd, &size);
260 }
261 }
262 else if (PREDICT_FALSE (epfd < 0))
263 {
264 errno = -epfd;
265 rv = -1;
266 goto done;
267 }
268
269 func_str = "vppcom_session_close";
270
Dave Wallace2a865272018-02-07 21:00:42 -0500271 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500272 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
273 getpid (), fd, fd, func_str, sid, sid);
274
275 rv = vppcom_session_close (sid);
276 if (rv != VPPCOM_OK)
277 {
278 errno = -rv;
279 rv = -1;
280 }
281 }
282 else
283 {
284 func_str = "libc_close";
285
Dave Wallace2a865272018-02-07 21:00:42 -0500286 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500287 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s()",
288 getpid (), fd, fd, func_str);
289
290 rv = libc_close (fd);
291 }
292
293done:
Dave Wallace2a865272018-02-07 21:00:42 -0500294 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500295 {
296 if (rv < 0)
297 {
298 int errno_val = errno;
299 perror (func_str);
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);
303 errno = errno_val;
304 }
305 else
306 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
307 getpid (), fd, fd, rv, rv);
308 }
309 return rv;
310}
311
312ssize_t
313read (int fd, void *buf, size_t nbytes)
314{
315 ssize_t size;
316 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -0500317 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500318
Dave Wallace2a865272018-02-07 21:00:42 -0500319 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500320 return -1;
321
322 if (sid != INVALID_SESSION_ID)
323 {
324 func_str = "vppcom_session_read";
325
Dave Wallace2a865272018-02-07 21:00:42 -0500326 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500327 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);
330
331 size = vppcom_session_read (sid, buf, nbytes);
332 if (size < 0)
333 {
334 errno = -size;
335 size = -1;
336 }
337 }
338 else
339 {
340 func_str = "libc_read";
341
Dave Wallace2a865272018-02-07 21:00:42 -0500342 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500343 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
344 "buf %p, nbytes %u", getpid (),
345 fd, fd, func_str, buf, nbytes);
346
347 size = libc_read (fd, buf, nbytes);
348 }
349
Dave Wallace2a865272018-02-07 21:00:42 -0500350 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500351 {
352 if (size < 0)
353 {
354 int errno_val = errno;
355 perror (func_str);
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);
359 errno = errno_val;
360 }
361 else
362 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
363 getpid (), fd, fd, size, size);
364 }
365 return size;
366}
367
368ssize_t
369readv (int fd, const struct iovec * iov, int iovcnt)
370{
371 const char *func_str;
372 ssize_t size = 0;
Dave Wallace2a865272018-02-07 21:00:42 -0500373 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace8aaba562018-01-18 17:21:19 -0500374 int rv = 0, i, total = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500375
Dave Wallace2a865272018-02-07 21:00:42 -0500376 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500377 return -1;
378
379 if (sid != INVALID_SESSION_ID)
380 {
381 func_str = "vppcom_session_read";
382 do
383 {
384 for (i = 0; i < iovcnt; ++i)
385 {
Dave Wallace2a865272018-02-07 21:00:42 -0500386 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500387 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,
390 iov, iovcnt, total);
391
392 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
393 if (rv < 0)
394 break;
395 else
396 {
397 total += rv;
398 if (rv < iov[i].iov_len)
399 {
Dave Wallace2a865272018-02-07 21:00:42 -0500400 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500401 clib_warning ("LDP<%d>: fd %d (0x%x): "
402 "rv (%d) < iov[%d].iov_len (%d)",
403 getpid (), fd, fd, rv, i,
404 iov[i].iov_len);
405 break;
406 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700407 }
408 }
409 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500410 while ((rv >= 0) && (total == 0));
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700411
Dave Wallace048b1d62018-01-03 22:24:41 -0500412 if (rv < 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700413 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500414 errno = -rv;
415 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700416 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500417 else
418 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700419 }
420 else
421 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500422 func_str = "libc_readv";
423
Dave Wallace2a865272018-02-07 21:00:42 -0500424 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500425 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
426 "iov %p, iovcnt %d", getpid (), fd, fd, iov, iovcnt);
427
428 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700429 }
430
Dave Wallace2a865272018-02-07 21:00:42 -0500431 if (LDP_DEBUG > 2)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700432 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500433 if (size < 0)
434 {
435 int errno_val = errno;
436 perror (func_str);
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);
440 errno = errno_val;
441 }
442 else
443 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
444 getpid (), fd, fd, size, size);
445 }
446 return size;
447}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700448
Dave Wallace048b1d62018-01-03 22:24:41 -0500449ssize_t
450write (int fd, const void *buf, size_t nbytes)
451{
452 const char *func_str;
453 ssize_t size = 0;
Dave Wallace2a865272018-02-07 21:00:42 -0500454 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500455
Dave Wallace2a865272018-02-07 21:00:42 -0500456 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500457 return -1;
458
459 if (sid != INVALID_SESSION_ID)
460 {
461 func_str = "vppcom_session_write";
462
Dave Wallace2a865272018-02-07 21:00:42 -0500463 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500464 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);
467
468 size = vppcom_session_write (sid, (void *) buf, nbytes);
469 if (size < 0)
470 {
471 errno = -size;
472 size = -1;
473 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700474 }
475 else
476 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500477 func_str = "libc_write";
478
Dave Wallace2a865272018-02-07 21:00:42 -0500479 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500480 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
481 "buf %p, nbytes %u", getpid (),
482 fd, fd, func_str, buf, nbytes);
483
484 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700485 }
486
Dave Wallace2a865272018-02-07 21:00:42 -0500487 if (LDP_DEBUG > 2)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700488 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500489 if (size < 0)
490 {
491 int errno_val = errno;
492 perror (func_str);
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);
496 errno = errno_val;
497 }
498 else
499 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
500 getpid (), fd, fd, size, size);
501 }
502 return size;
503}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700504
Dave Wallace048b1d62018-01-03 22:24:41 -0500505ssize_t
506writev (int fd, const struct iovec * iov, int iovcnt)
507{
508 const char *func_str;
509 ssize_t size = 0, total = 0;
Dave Wallace2a865272018-02-07 21:00:42 -0500510 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace8aaba562018-01-18 17:21:19 -0500511 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500512
513 /*
514 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
515 */
516
Dave Wallace2a865272018-02-07 21:00:42 -0500517 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500518 return -1;
519
520 if (sid != INVALID_SESSION_ID)
521 {
522 func_str = "vppcom_session_write";
523 do
524 {
525 for (i = 0; i < iovcnt; ++i)
526 {
Dave Wallace2a865272018-02-07 21:00:42 -0500527 if (LDP_DEBUG > 4)
Dave Wallace048b1d62018-01-03 22:24:41 -0500528 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);
532
533 rv = vppcom_session_write (sid, iov[i].iov_base,
534 iov[i].iov_len);
535 if (rv < 0)
536 break;
537 else
538 {
539 total += rv;
540 if (rv < iov[i].iov_len)
541 {
Dave Wallace2a865272018-02-07 21:00:42 -0500542 if (LDP_DEBUG > 4)
Dave Wallace048b1d62018-01-03 22:24:41 -0500543 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);
547 break;
548 }
549 }
550 }
551 }
552 while ((rv >= 0) && (total == 0));
553
554 if (rv < 0)
555 {
556 errno = -rv;
557 size = -1;
558 }
559 else
560 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700561 }
562 else
563 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500564 func_str = "libc_writev";
565
Dave Wallace2a865272018-02-07 21:00:42 -0500566 if (LDP_DEBUG > 4)
Dave Wallace048b1d62018-01-03 22:24:41 -0500567 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);
570
571 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700572 }
573
Dave Wallace2a865272018-02-07 21:00:42 -0500574 if (LDP_DEBUG > 4)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700575 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500576 if (size < 0)
577 {
578 int errno_val = errno;
579 perror (func_str);
580 fprintf (stderr,
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);
584 errno = errno_val;
585 }
586 else
587 printf ("%s:%d: LDP<%d>: fd %d (0x%x): returning %ld\n",
588 __func__, __LINE__, getpid (), fd, fd, size);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700589 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500590 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700591}
592
593int
Dave Wallace048b1d62018-01-03 22:24:41 -0500594fcntl (int fd, int cmd, ...)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700595{
Dave Wallace048b1d62018-01-03 22:24:41 -0500596 const char *func_str = __func__;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700597 int rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500598 va_list ap;
Dave Wallace2a865272018-02-07 21:00:42 -0500599 u32 sid = ldp_sid_from_fd (fd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700600
Dave Wallace2a865272018-02-07 21:00:42 -0500601 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500602 return -1;
603
604 va_start (ap, cmd);
605 if (sid != INVALID_SESSION_ID)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700606 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500607 int flags = va_arg (ap, int);
608 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700609
Dave Wallace048b1d62018-01-03 22:24:41 -0500610 size = sizeof (flags);
611 rv = -EOPNOTSUPP;
612 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700613 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500614 case F_SETFL:
615 func_str = "vppcom_session_attr[SET_FLAGS]";
Dave Wallace2a865272018-02-07 21:00:42 -0500616 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500617 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,
620 flags, flags, size);
621
622 rv =
623 vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
624 break;
625
626 case F_GETFL:
627 func_str = "vppcom_session_attr[GET_FLAGS]";
Dave Wallace2a865272018-02-07 21:00:42 -0500628 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500629 clib_warning
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);
633
634 rv =
635 vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
636 if (rv == VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700637 {
Dave Wallace2a865272018-02-07 21:00:42 -0500638 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500639 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);
642 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700643 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700644 break;
645
646 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500647 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700648 break;
649 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500650 if (rv < 0)
651 {
652 errno = -rv;
653 rv = -1;
654 }
655 }
656 else
657 {
658 func_str = "libc_vfcntl";
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700659
Dave Wallace2a865272018-02-07 21:00:42 -0500660 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500661 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
662 getpid (), fd, fd, func_str, cmd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700663
Dave Wallace048b1d62018-01-03 22:24:41 -0500664 rv = libc_vfcntl (fd, cmd, ap);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700665 }
666
Dave Wallace048b1d62018-01-03 22:24:41 -0500667 va_end (ap);
668
Dave Wallace2a865272018-02-07 21:00:42 -0500669 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500670 {
671 if (rv < 0)
672 {
673 int errno_val = errno;
674 perror (func_str);
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);
678 errno = errno_val;
679 }
680 else
681 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
682 getpid (), fd, fd, rv, rv);
683 }
684 return rv;
685}
686
687int
688ioctl (int fd, unsigned long int cmd, ...)
689{
690 const char *func_str;
691 int rv;
692 va_list ap;
Dave Wallace2a865272018-02-07 21:00:42 -0500693 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500694
Dave Wallace2a865272018-02-07 21:00:42 -0500695 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500696 return -1;
697
698 va_start (ap, cmd);
699 if (sid != INVALID_SESSION_ID)
700 {
701 func_str = "vppcom_session_attr[GET_NREAD]";
702
703 switch (cmd)
704 {
705 case FIONREAD:
Dave Wallace2a865272018-02-07 21:00:42 -0500706 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500707 clib_warning
708 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
709 getpid (), fd, fd, func_str, sid, sid);
710
711 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
712 break;
713
714 case FIONBIO:
715 {
716 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
717 u32 size = sizeof (flags);
718
719 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
720 * non-blocking, the flags should be read here and merged
721 * with O_NONBLOCK.
722 */
Dave Wallace2a865272018-02-07 21:00:42 -0500723 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500724 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,
727 flags, flags, size);
728
729 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
730 &size);
731 }
732 break;
733
734 default:
735 rv = -EOPNOTSUPP;
736 break;
737 }
738 if (rv < 0)
739 {
740 errno = -rv;
741 rv = -1;
742 }
743 }
744 else
745 {
746 func_str = "libc_vioctl";
747
Dave Wallace2a865272018-02-07 21:00:42 -0500748 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500749 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
750 getpid (), fd, fd, func_str, cmd);
751
752 rv = libc_vioctl (fd, cmd, ap);
753 }
754
Dave Wallace2a865272018-02-07 21:00:42 -0500755 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500756 {
757 if (rv < 0)
758 {
759 int errno_val = errno;
760 perror (func_str);
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);
764 errno = errno_val;
765 }
766 else
767 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
768 getpid (), fd, fd, rv, rv);
769 }
770 va_end (ap);
771 return rv;
772}
773
774int
Dave Wallace2a865272018-02-07 21:00:42 -0500775ldp_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)
Dave Wallace048b1d62018-01-03 22:24:41 -0500780{
781 int rv;
782 char *func_str = "##";
783 f64 time_out;
784 int fd;
785 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
786 u32 minbits = clib_max (nfds, BITS (uword));
787 u32 sid;
788
789 if (nfds < 0)
790 {
791 errno = EINVAL;
792 return -1;
793 }
794
Dave Wallace2a865272018-02-07 21:00:42 -0500795 if (nfds <= ldp->sid_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500796 {
797 func_str = "libc_pselect";
798
Dave Wallace2a865272018-02-07 21:00:42 -0500799 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500800 clib_warning
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);
804
805 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
806 timeout, sigmask);
807 goto done;
808 }
809
Dave Wallace2a865272018-02-07 21:00:42 -0500810 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
Dave Wallace048b1d62018-01-03 22:24:41 -0500811 {
Dave Wallace2a865272018-02-07 21:00:42 -0500812 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
Dave Wallace048b1d62018-01-03 22:24:41 -0500813 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
Dave Wallace2a865272018-02-07 21:00:42 -0500814 ldp->sid_bit_val, ldp->sid_bit_val,
Dave Wallace048b1d62018-01-03 22:24:41 -0500815 FD_SETSIZE / 2, FD_SETSIZE / 2);
816 errno = EOVERFLOW;
817 return -1;
818 }
819
820 if (timeout)
821 {
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;
826
827 /* select as fine grained sleep */
828 if (!nfds)
829 {
Dave Wallace2a865272018-02-07 21:00:42 -0500830 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500831 clib_warning ("LDP<%d>: sleeping for %f seconds",
832 getpid (), time_out);
833
Dave Wallace2a865272018-02-07 21:00:42 -0500834 time_out += clib_time_now (&ldp->clib_time);
835 while (clib_time_now (&ldp->clib_time) < time_out)
Dave Wallace048b1d62018-01-03 22:24:41 -0500836 ;
837 return 0;
838 }
839 }
840 else if (!nfds)
841 {
842 errno = EINVAL;
843 return -1;
844 }
845 else
846 time_out = -1;
847
848 sid_bits = libc_bits = 0;
849 if (readfds)
850 {
Dave Wallace2a865272018-02-07 21:00:42 -0500851 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));
Dave Wallace048b1d62018-01-03 22:24:41 -0500856 FD_ZERO (readfds);
857
858 /* *INDENT-OFF* */
Dave Wallace2a865272018-02-07 21:00:42 -0500859 clib_bitmap_foreach (fd, ldp->rd_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -0500860 ({
Dave Wallace2a865272018-02-07 21:00:42 -0500861 sid = ldp_sid_from_fd (fd);
862 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500863 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)
Dave Wallace2a865272018-02-07 21:00:42 -0500866 clib_bitmap_set_no_check (ldp->libc_rd_bitmap, fd, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -0500867 else
Dave Wallace2a865272018-02-07 21:00:42 -0500868 clib_bitmap_set_no_check (ldp->sid_rd_bitmap, sid, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -0500869 }));
870 /* *INDENT-ON* */
871
Dave Wallace2a865272018-02-07 21:00:42 -0500872 sid_bits_set = clib_bitmap_last_set (ldp->sid_rd_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500873 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
874
Dave Wallace2a865272018-02-07 21:00:42 -0500875 libc_bits_set = clib_bitmap_last_set (ldp->libc_rd_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500876 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
877
Dave Wallace2a865272018-02-07 21:00:42 -0500878 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500879 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);
882 }
883 if (writefds)
884 {
Dave Wallace2a865272018-02-07 21:00:42 -0500885 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));
Dave Wallace048b1d62018-01-03 22:24:41 -0500890 FD_ZERO (writefds);
891
892 /* *INDENT-OFF* */
Dave Wallace2a865272018-02-07 21:00:42 -0500893 clib_bitmap_foreach (fd, ldp->wr_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -0500894 ({
Dave Wallace2a865272018-02-07 21:00:42 -0500895 sid = ldp_sid_from_fd (fd);
896 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500897 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)
Dave Wallace2a865272018-02-07 21:00:42 -0500900 clib_bitmap_set_no_check (ldp->libc_wr_bitmap, fd, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -0500901 else
Dave Wallace2a865272018-02-07 21:00:42 -0500902 clib_bitmap_set_no_check (ldp->sid_wr_bitmap, sid, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -0500903 }));
904 /* *INDENT-ON* */
905
Dave Wallace2a865272018-02-07 21:00:42 -0500906 sid_bits_set = clib_bitmap_last_set (ldp->sid_wr_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500907 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
908
Dave Wallace2a865272018-02-07 21:00:42 -0500909 libc_bits_set = clib_bitmap_last_set (ldp->libc_wr_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500910 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
911
Dave Wallace2a865272018-02-07 21:00:42 -0500912 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500913 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);
916 }
917 if (exceptfds)
918 {
Dave Wallace2a865272018-02-07 21:00:42 -0500919 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));
Dave Wallace048b1d62018-01-03 22:24:41 -0500924 FD_ZERO (exceptfds);
925
926 /* *INDENT-OFF* */
Dave Wallace2a865272018-02-07 21:00:42 -0500927 clib_bitmap_foreach (fd, ldp->ex_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -0500928 ({
Dave Wallace2a865272018-02-07 21:00:42 -0500929 sid = ldp_sid_from_fd (fd);
930 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500931 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)
Dave Wallace2a865272018-02-07 21:00:42 -0500934 clib_bitmap_set_no_check (ldp->libc_ex_bitmap, fd, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -0500935 else
Dave Wallace2a865272018-02-07 21:00:42 -0500936 clib_bitmap_set_no_check (ldp->sid_ex_bitmap, sid, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -0500937 }));
938 /* *INDENT-ON* */
939
Dave Wallace2a865272018-02-07 21:00:42 -0500940 sid_bits_set = clib_bitmap_last_set (ldp->sid_ex_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500941 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
942
Dave Wallace2a865272018-02-07 21:00:42 -0500943 libc_bits_set = clib_bitmap_last_set (ldp->libc_ex_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500944 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
945
Dave Wallace2a865272018-02-07 21:00:42 -0500946 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -0500947 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);
950 }
951
952 if (PREDICT_FALSE (!sid_bits && !libc_bits))
953 {
954 errno = EINVAL;
955 rv = -1;
956 goto done;
957 }
958
959 do
960 {
961 if (sid_bits)
962 {
Dave Wallace2a865272018-02-07 21:00:42 -0500963 if (!ldp->select_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -0500964 {
965 func_str = "vppcom_select";
966
967 if (readfds)
Dave Wallace2a865272018-02-07 21:00:42 -0500968 clib_memcpy (ldp->rd_bitmap, ldp->sid_rd_bitmap,
969 vec_len (ldp->rd_bitmap) *
Dave Wallace048b1d62018-01-03 22:24:41 -0500970 sizeof (clib_bitmap_t));
971 if (writefds)
Dave Wallace2a865272018-02-07 21:00:42 -0500972 clib_memcpy (ldp->wr_bitmap, ldp->sid_wr_bitmap,
973 vec_len (ldp->wr_bitmap) *
Dave Wallace048b1d62018-01-03 22:24:41 -0500974 sizeof (clib_bitmap_t));
975 if (exceptfds)
Dave Wallace2a865272018-02-07 21:00:42 -0500976 clib_memcpy (ldp->ex_bitmap, ldp->sid_ex_bitmap,
977 vec_len (ldp->ex_bitmap) *
Dave Wallace048b1d62018-01-03 22:24:41 -0500978 sizeof (clib_bitmap_t));
979
980 rv = vppcom_select (sid_bits,
Dave Wallace2a865272018-02-07 21:00:42 -0500981 readfds ? ldp->rd_bitmap : NULL,
982 writefds ? ldp->wr_bitmap : NULL,
983 exceptfds ? ldp->ex_bitmap : NULL, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500984 if (rv < 0)
985 {
986 errno = -rv;
987 rv = -1;
988 }
989 else if (rv > 0)
990 {
991 if (readfds)
992 {
993 /* *INDENT-OFF* */
Dave Wallace2a865272018-02-07 21:00:42 -0500994 clib_bitmap_foreach (sid, ldp->rd_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -0500995 ({
Dave Wallace2a865272018-02-07 21:00:42 -0500996 fd = ldp_fd_from_sid (sid);
Dave Wallace048b1d62018-01-03 22:24:41 -0500997 if (PREDICT_FALSE (fd < 0))
998 {
999 errno = EBADFD;
1000 rv = -1;
1001 goto done;
1002 }
1003 FD_SET (fd, readfds);
1004 }));
1005 /* *INDENT-ON* */
1006 }
1007 if (writefds)
1008 {
1009 /* *INDENT-OFF* */
Dave Wallace2a865272018-02-07 21:00:42 -05001010 clib_bitmap_foreach (sid, ldp->wr_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -05001011 ({
Dave Wallace2a865272018-02-07 21:00:42 -05001012 fd = ldp_fd_from_sid (sid);
Dave Wallace048b1d62018-01-03 22:24:41 -05001013 if (PREDICT_FALSE (fd < 0))
1014 {
1015 errno = EBADFD;
1016 rv = -1;
1017 goto done;
1018 }
1019 FD_SET (fd, writefds);
1020 }));
1021 /* *INDENT-ON* */
1022 }
1023 if (exceptfds)
1024 {
1025 /* *INDENT-OFF* */
Dave Wallace2a865272018-02-07 21:00:42 -05001026 clib_bitmap_foreach (sid, ldp->ex_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -05001027 ({
Dave Wallace2a865272018-02-07 21:00:42 -05001028 fd = ldp_fd_from_sid (sid);
Dave Wallace048b1d62018-01-03 22:24:41 -05001029 if (PREDICT_FALSE (fd < 0))
1030 {
1031 errno = EBADFD;
1032 rv = -1;
1033 goto done;
1034 }
1035 FD_SET (fd, exceptfds);
1036 }));
1037 /* *INDENT-ON* */
1038 }
Dave Wallace2a865272018-02-07 21:00:42 -05001039 ldp->select_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001040 goto done;
1041 }
1042 }
1043 else
Dave Wallace2a865272018-02-07 21:00:42 -05001044 ldp->select_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001045 }
1046 if (libc_bits)
1047 {
1048 struct timespec tspec;
1049
1050 func_str = "libc_pselect";
1051
1052 if (readfds)
Dave Wallace2a865272018-02-07 21:00:42 -05001053 clib_memcpy (readfds, ldp->libc_rd_bitmap,
1054 vec_len (ldp->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001055 if (writefds)
Dave Wallace2a865272018-02-07 21:00:42 -05001056 clib_memcpy (writefds, ldp->libc_wr_bitmap,
1057 vec_len (ldp->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001058 if (exceptfds)
Dave Wallace2a865272018-02-07 21:00:42 -05001059 clib_memcpy (exceptfds, ldp->libc_ex_bitmap,
1060 vec_len (ldp->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001061 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);
1066 if (rv != 0)
1067 goto done;
1068 }
1069 }
Dave Wallace2a865272018-02-07 21:00:42 -05001070 while ((time_out == -1) || (clib_time_now (&ldp->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -05001071 rv = 0;
1072
1073done:
1074 /* TBD: set timeout to amount of time left */
Dave Wallace2a865272018-02-07 21:00:42 -05001075 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);
Dave Wallace048b1d62018-01-03 22:24:41 -05001084
Dave Wallace2a865272018-02-07 21:00:42 -05001085 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05001086 {
1087 if (rv < 0)
1088 {
1089 int errno_val = errno;
1090 perror (func_str);
1091 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1092 "rv %d, errno = %d", getpid (),
1093 func_str, rv, errno_val);
1094 errno = errno_val;
1095 }
1096 else
1097 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1098 }
1099 return rv;
1100}
1101
1102int
1103select (int nfds, fd_set * __restrict readfds,
1104 fd_set * __restrict writefds,
1105 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1106{
1107 struct timespec tspec;
1108
1109 if (timeout)
1110 {
1111 tspec.tv_sec = timeout->tv_sec;
1112 tspec.tv_nsec = timeout->tv_usec * 1000;
1113 }
Dave Wallace2a865272018-02-07 21:00:42 -05001114 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1115 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001116}
1117
1118#ifdef __USE_XOPEN2K
1119int
1120pselect (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)
1125{
Dave Wallace2a865272018-02-07 21:00:42 -05001126 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001127}
1128#endif
1129
1130int
1131socket (int domain, int type, int protocol)
1132{
1133 const char *func_str;
1134 int rv;
1135 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1136 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1137
Dave Wallace2a865272018-02-07 21:00:42 -05001138 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001139 return -1;
1140
1141 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1142 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1143 {
1144 int sid;
Dave Wallace048b1d62018-01-03 22:24:41 -05001145 u8 proto = ((sock_type == SOCK_DGRAM) ?
1146 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1147
1148 func_str = "vppcom_session_create";
1149
Dave Wallace2a865272018-02-07 21:00:42 -05001150 if (LDP_DEBUG > 0)
Dave Wallacec04cbf12018-02-07 18:14:02 -05001151 clib_warning ("LDP<%d>: : calling %s(): "
Dave Wallace048b1d62018-01-03 22:24:41 -05001152 "proto %u (%s), is_nonblocking %u",
Dave Wallacec04cbf12018-02-07 18:14:02 -05001153 getpid (), func_str, proto,
Dave Wallace048b1d62018-01-03 22:24:41 -05001154 vppcom_proto_str (proto), is_nonblocking);
1155
Dave Wallacec04cbf12018-02-07 18:14:02 -05001156 sid = vppcom_session_create (proto, is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -05001157 if (sid < 0)
1158 {
1159 errno = -sid;
1160 rv = -1;
1161 }
1162 else
1163 {
Dave Wallace2a865272018-02-07 21:00:42 -05001164 func_str = "ldp_fd_from_sid";
1165 rv = ldp_fd_from_sid (sid);
Dave Wallace048b1d62018-01-03 22:24:41 -05001166 if (rv < 0)
1167 {
1168 (void) vppcom_session_close (sid);
1169 errno = -rv;
1170 rv = -1;
1171 }
1172 }
1173 }
1174 else
1175 {
1176 func_str = "libc_socket";
1177
Dave Wallace2a865272018-02-07 21:00:42 -05001178 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001179 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1180
1181 rv = libc_socket (domain, type, protocol);
1182 }
1183
Dave Wallace2a865272018-02-07 21:00:42 -05001184 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001185 {
1186 if (rv < 0)
1187 {
1188 int errno_val = errno;
1189 perror (func_str);
1190 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1191 "rv %d, errno = %d",
1192 getpid (), func_str, rv, errno_val);
1193 errno = errno_val;
1194 }
1195 else
1196 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1197 }
1198 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001199}
1200
1201/*
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.
1207 * */
1208int
Dave Wallace048b1d62018-01-03 22:24:41 -05001209socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001210{
Dave Wallace048b1d62018-01-03 22:24:41 -05001211 const char *func_str;
1212 int rv;
1213 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1214
Dave Wallace2a865272018-02-07 21:00:42 -05001215 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001216 return -1;
1217
1218 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1219 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001220 {
Dave Wallace8aaba562018-01-18 17:21:19 -05001221 func_str = __func__;
1222
Dave Wallace048b1d62018-01-03 22:24:41 -05001223 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1224 errno = ENOSYS;
1225 rv = -1;
1226 }
1227 else
1228 {
1229 func_str = "libc_socket";
1230
Dave Wallace2a865272018-02-07 21:00:42 -05001231 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001232 clib_warning ("LDP<%d>: : calling %s()", getpid (), func_str);
1233
1234 rv = libc_socket (domain, type, protocol);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001235 }
1236
Dave Wallace2a865272018-02-07 21:00:42 -05001237 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001238 {
1239 if (rv < 0)
1240 {
1241 int errno_val = errno;
1242 perror (func_str);
1243 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1244 "rv %d, errno = %d",
1245 getpid (), func_str, rv, errno_val);
1246 errno = errno_val;
1247 }
1248 else
1249 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1250 }
1251 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001252}
1253
1254int
Dave Wallace048b1d62018-01-03 22:24:41 -05001255bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001256{
1257 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001258 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001259 u32 sid = ldp_sid_from_fd (fd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001260
Dave Wallace2a865272018-02-07 21:00:42 -05001261 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001262 return -1;
1263
1264 if (sid != INVALID_SESSION_ID)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001265 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001266 vppcom_endpt_t ep;
1267
1268 func_str = "vppcom_session_bind";
1269
Dave Wallace048b1d62018-01-03 22:24:41 -05001270 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001271 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001272 case AF_INET:
1273 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001274 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001275 clib_warning
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);
1278 errno = EINVAL;
1279 rv = -1;
1280 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001281 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001282 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;
1285 break;
1286
1287 case AF_INET6:
1288 if (len != sizeof (struct sockaddr_in6))
1289 {
1290 clib_warning
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);
1293 errno = EINVAL;
1294 rv = -1;
1295 goto done;
1296 }
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;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001300 break;
1301
1302 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05001303 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;
1307 rv = -1;
1308 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001309 }
Dave Wallace2a865272018-02-07 21:00:42 -05001310 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001311 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1312 "addr %p, len %u",
1313 getpid (), fd, fd, func_str, sid, sid, addr, len);
1314
1315 rv = vppcom_session_bind (sid, &ep);
1316 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001317 {
1318 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001319 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001320 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001321 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001322 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001323 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001324 func_str = "libc_bind";
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001325
Dave Wallace2a865272018-02-07 21:00:42 -05001326 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001327 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1328 "addr %p, len %u",
1329 getpid (), fd, fd, func_str, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001330
Dave Wallace048b1d62018-01-03 22:24:41 -05001331 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001332 }
1333
Dave Wallace048b1d62018-01-03 22:24:41 -05001334done:
Dave Wallace2a865272018-02-07 21:00:42 -05001335 if (LDP_DEBUG > 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001336 {
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001337 if (rv < 0)
1338 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001339 int errno_val = errno;
1340 perror (func_str);
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);
1344 errno = errno_val;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001345 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001346 else
1347 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1348 getpid (), fd, fd, rv, rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001349 }
1350 return rv;
1351}
1352
1353static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001354ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1355 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001356{
Dave Wallace048b1d62018-01-03 22:24:41 -05001357 int rv = 0;
1358 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001359
Dave Wallace2a865272018-02-07 21:00:42 -05001360 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001361 return -1;
1362
1363 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001364 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001365 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1366 switch (addr->sa_family)
1367 {
1368 case AF_INET:
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;
1374 if (copy_len > 0)
1375 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1376 copy_len);
1377 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001378
Dave Wallace048b1d62018-01-03 22:24:41 -05001379 case AF_INET6:
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;
1385 if (copy_len > 0)
1386 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1387 __in6_u.__u6_addr8, ep->ip, copy_len);
1388 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001389
Dave Wallace048b1d62018-01-03 22:24:41 -05001390 default:
1391 /* Not possible */
1392 rv = -EAFNOSUPPORT;
1393 break;
1394 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001395 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001396 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001397}
1398
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001399int
Dave Wallace048b1d62018-01-03 22:24:41 -05001400getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001401{
1402 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001403 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001404 u32 sid = ldp_sid_from_fd (fd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001405
Dave Wallace2a865272018-02-07 21:00:42 -05001406 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001407 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001408
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 if (sid != INVALID_SESSION_ID)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001410 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001411 vppcom_endpt_t ep;
1412 u8 addr_buf[sizeof (struct in6_addr)];
1413 u32 size = sizeof (ep);
1414
1415 ep.ip = addr_buf;
1416 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1417
Dave Wallace2a865272018-02-07 21:00:42 -05001418 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001419 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1420 "addr %p, len %u",
1421 getpid (), fd, fd, func_str, sid, sid, addr, len);
1422
1423 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1424 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001425 {
1426 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001427 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001428 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001429 else
1430 {
Dave Wallace2a865272018-02-07 21:00:42 -05001431 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001432 if (rv != VPPCOM_OK)
1433 {
1434 errno = -rv;
1435 rv = -1;
1436 }
1437 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001438 }
1439 else
1440 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001441 func_str = "libc_getsockname";
1442
Dave Wallace2a865272018-02-07 21:00:42 -05001443 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001444 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1445 "addr %p, len %u",
1446 getpid (), fd, fd, func_str, addr, len);
1447
1448 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001449 }
1450
Dave Wallace2a865272018-02-07 21:00:42 -05001451 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001452 {
1453 if (rv < 0)
1454 {
1455 int errno_val = errno;
1456 perror (func_str);
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);
1460 errno = errno_val;
1461 }
1462 else
1463 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1464 getpid (), fd, fd, rv, rv);
1465 }
1466 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001467}
1468
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001469int
Dave Wallace048b1d62018-01-03 22:24:41 -05001470connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001471{
Dave Wallace048b1d62018-01-03 22:24:41 -05001472 int rv;
1473 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05001474 u32 sid = ldp_sid_from_fd (fd);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001475
Dave Wallace2a865272018-02-07 21:00:42 -05001476 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001477 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001478
Dave Wallace048b1d62018-01-03 22:24:41 -05001479 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001480 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001481 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1482 getpid (), fd, fd, len);
1483 errno = EINVAL;
1484 rv = -1;
1485 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001486 }
1487
Dave Wallace048b1d62018-01-03 22:24:41 -05001488 if (sid != INVALID_SESSION_ID)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001489 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001490 vppcom_endpt_t ep;
1491
1492 func_str = "vppcom_session_connect";
1493
Dave Wallace048b1d62018-01-03 22:24:41 -05001494 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001495 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001496 case AF_INET:
1497 if (len != sizeof (struct sockaddr_in))
1498 {
1499 clib_warning
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);
1502 errno = EINVAL;
1503 rv = -1;
1504 goto done;
1505 }
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;
1509 break;
1510
1511 case AF_INET6:
1512 if (len != sizeof (struct sockaddr_in6))
1513 {
1514 clib_warning
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);
1517 errno = EINVAL;
1518 rv = -1;
1519 goto done;
1520 }
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;
1524 break;
1525
1526 default:
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;
1531 rv = -1;
1532 goto done;
1533 }
Dave Wallace2a865272018-02-07 21:00:42 -05001534 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001535 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x) "
1536 "addr %p len %u",
1537 getpid (), fd, fd, func_str, sid, sid, addr, len);
1538
1539 rv = vppcom_session_connect (sid, &ep);
1540 if (rv != VPPCOM_OK)
1541 {
1542 errno = -rv;
1543 rv = -1;
1544 }
1545 }
1546 else
1547 {
1548 func_str = "libc_connect";
1549
Dave Wallace2a865272018-02-07 21:00:42 -05001550 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001551 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1552 "addr %p, len %u",
1553 getpid (), fd, fd, func_str, addr, len);
1554
1555 rv = libc_connect (fd, addr, len);
1556 }
1557
1558done:
Dave Wallace2a865272018-02-07 21:00:42 -05001559 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001560 {
1561 if (rv < 0)
1562 {
1563 int errno_val = errno;
1564 perror (func_str);
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);
1568 errno = errno_val;
1569 }
1570 else
1571 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1572 getpid (), fd, fd, rv, rv);
1573 }
1574 return rv;
1575}
1576
1577int
1578getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1579{
1580 int rv;
1581 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001582 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001583
Dave Wallace2a865272018-02-07 21:00:42 -05001584 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001585 return -1;
1586
Dave Wallace048b1d62018-01-03 22:24:41 -05001587 if (sid != INVALID_SESSION_ID)
1588 {
1589 vppcom_endpt_t ep;
1590 u8 addr_buf[sizeof (struct in6_addr)];
1591 u32 size = sizeof (ep);
1592
1593 ep.ip = addr_buf;
1594 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1595
Dave Wallace2a865272018-02-07 21:00:42 -05001596 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001597 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1598 "addr %p, len %u",
1599 getpid (), fd, fd, func_str, sid, sid, addr, len);
1600
1601 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1602 if (rv != VPPCOM_OK)
1603 {
1604 errno = -rv;
1605 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001606 }
1607 else
1608 {
Dave Wallace2a865272018-02-07 21:00:42 -05001609 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001610 if (rv != VPPCOM_OK)
1611 {
1612 errno = -rv;
1613 rv = -1;
1614 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001615 }
1616 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001617 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001618 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001619 func_str = "libc_getpeername";
1620
Dave Wallace2a865272018-02-07 21:00:42 -05001621 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001622 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1623 "addr %p, len %u",
1624 getpid (), fd, fd, func_str, addr, len);
1625
1626 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001627 }
1628
Dave Wallace2a865272018-02-07 21:00:42 -05001629 if (LDP_DEBUG > 2)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001630 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001631 if (rv < 0)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001632 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001633 int errno_val = errno;
1634 perror (func_str);
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);
1638 errno = errno_val;
1639 }
1640 else
1641 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1642 getpid (), fd, fd, rv, rv);
1643 }
1644 return rv;
1645}
1646
1647ssize_t
1648send (int fd, const void *buf, size_t n, int flags)
1649{
1650 ssize_t size;
1651 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001652 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001653
Dave Wallace2a865272018-02-07 21:00:42 -05001654 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001655 return -1;
1656
1657 if (sid != INVALID_SESSION_ID)
1658 {
1659
1660 func_str = "vppcom_session_sendto";
1661
Dave Wallace2a865272018-02-07 21:00:42 -05001662 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001663 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);
1666
1667 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
1668 if (size != VPPCOM_OK)
1669 {
1670 errno = -size;
1671 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001672 }
1673 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001674 else
1675 {
1676 func_str = "libc_send";
1677
Dave Wallace2a865272018-02-07 21:00:42 -05001678 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001679 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);
1682
1683 size = libc_send (fd, buf, n, flags);
1684 }
1685
Dave Wallace2a865272018-02-07 21:00:42 -05001686 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001687 {
1688 if (size < 0)
1689 {
1690 int errno_val = errno;
1691 perror (func_str);
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);
1695 errno = errno_val;
1696 }
1697 else
1698 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1699 getpid (), fd, fd, size, size);
1700 }
1701 return size;
1702}
1703
1704ssize_t
1705sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1706{
1707 ssize_t size = 0;
1708 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001709 u32 sid = ldp_sid_from_fd (out_fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001710
Dave Wallace2a865272018-02-07 21:00:42 -05001711 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001712 return -1;
1713
1714 if (sid != INVALID_SESSION_ID)
1715 {
1716 int rv;
1717 ssize_t results = 0;
1718 size_t n_bytes_left = len;
1719 size_t bytes_to_read;
1720 int nbytes;
1721 int errno_val;
1722 u8 eagain = 0;
1723 u32 flags, flags_len = sizeof (flags);
1724
1725 func_str = "vppcom_session_attr[GET_FLAGS]";
1726 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1727 &flags_len);
1728 if (PREDICT_FALSE (rv != VPPCOM_OK))
1729 {
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));
1734
Dave Wallace2a865272018-02-07 21:00:42 -05001735 vec_reset_length (ldp->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001736 errno = -rv;
1737 size = -1;
1738 goto done;
1739 }
1740
1741 if (offset)
1742 {
1743 off_t off = lseek (in_fd, *offset, SEEK_SET);
1744 if (PREDICT_FALSE (off == -1))
1745 {
1746 func_str = "lseek";
1747 errno_val = errno;
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,
1752 errno_val);
1753 errno = errno_val;
1754 size = -1;
1755 goto done;
1756 }
1757
1758 ASSERT (off == *offset);
1759 }
1760
1761 do
1762 {
1763 func_str = "vppcom_session_attr[GET_NWRITE]";
1764 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1765 if (size < 0)
1766 {
1767 clib_warning
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));
Dave Wallace2a865272018-02-07 21:00:42 -05001771 vec_reset_length (ldp->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001772 errno = -size;
1773 size = -1;
1774 goto done;
1775 }
1776
1777 bytes_to_read = size;
Dave Wallace2a865272018-02-07 21:00:42 -05001778 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001779 clib_warning
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,
1783 bytes_to_read);
1784
1785 if (bytes_to_read == 0)
1786 {
1787 if (flags & O_NONBLOCK)
1788 {
1789 if (!results)
1790 {
Dave Wallace2a865272018-02-07 21:00:42 -05001791 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001792 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1793 "EAGAIN",
1794 getpid (), out_fd, out_fd, sid, sid);
1795 eagain = 1;
1796 }
1797 goto update_offset;
1798 }
1799 else
1800 continue;
1801 }
1802 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Dave Wallace2a865272018-02-07 21:00:42 -05001803 vec_validate (ldp->io_buffer, bytes_to_read);
1804 nbytes = libc_read (in_fd, ldp->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001805 if (nbytes < 0)
1806 {
1807 func_str = "libc_read";
1808 errno_val = errno;
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,
Dave Wallace2a865272018-02-07 21:00:42 -05001812 in_fd, ldp->io_buffer, bytes_to_read, nbytes,
Dave Wallace048b1d62018-01-03 22:24:41 -05001813 errno_val);
1814 errno = errno_val;
1815
1816 if (results == 0)
1817 {
Dave Wallace2a865272018-02-07 21:00:42 -05001818 vec_reset_length (ldp->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001819 size = -1;
1820 goto done;
1821 }
1822 goto update_offset;
1823 }
1824 func_str = "vppcom_session_write";
Dave Wallace2a865272018-02-07 21:00:42 -05001825 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001826 clib_warning
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 (),
Dave Wallace2a865272018-02-07 21:00:42 -05001829 out_fd, out_fd, func_str, sid, sid, ldp->io_buffer, nbytes,
Dave Wallace048b1d62018-01-03 22:24:41 -05001830 results, n_bytes_left);
1831
Dave Wallace2a865272018-02-07 21:00:42 -05001832 size = vppcom_session_write (sid, ldp->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001833 if (size < 0)
1834 {
1835 if (size == VPPCOM_EAGAIN)
1836 {
1837 if (flags & O_NONBLOCK)
1838 {
1839 if (!results)
1840 {
Dave Wallace2a865272018-02-07 21:00:42 -05001841 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001842 clib_warning
1843 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1844 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1845 eagain = 1;
1846 }
1847 goto update_offset;
1848 }
1849 else
1850 continue;
1851 }
1852 else
1853 {
1854 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1855 "sid %u, io_buffer %p, nbytes %u "
1856 "returned %d (%s)",
1857 getpid (), out_fd, out_fd, func_str,
Dave Wallace2a865272018-02-07 21:00:42 -05001858 sid, ldp->io_buffer, nbytes,
Dave Wallace048b1d62018-01-03 22:24:41 -05001859 size, vppcom_retval_str (size));
1860 }
1861 if (results == 0)
1862 {
Dave Wallace2a865272018-02-07 21:00:42 -05001863 vec_reset_length (ldp->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001864 errno = -size;
1865 size = -1;
1866 goto done;
1867 }
1868 goto update_offset;
1869 }
1870
1871 results += nbytes;
1872 ASSERT (n_bytes_left >= nbytes);
1873 n_bytes_left = n_bytes_left - nbytes;
1874 }
1875 while (n_bytes_left > 0);
1876
1877 update_offset:
Dave Wallace2a865272018-02-07 21:00:42 -05001878 vec_reset_length (ldp->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001879 if (offset)
1880 {
1881 off_t off = lseek (in_fd, *offset, SEEK_SET);
1882 if (PREDICT_FALSE (off == -1))
1883 {
1884 func_str = "lseek";
1885 errno_val = errno;
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);
1890 errno = errno_val;
1891 size = -1;
1892 goto done;
1893 }
1894
1895 ASSERT (off == *offset);
1896 *offset += results + 1;
1897 }
1898 if (eagain)
1899 {
1900 errno = EAGAIN;
1901 size = -1;
1902 }
1903 else
1904 size = results;
1905 }
1906 else
1907 {
1908 func_str = "libc_send";
1909
Dave Wallace2a865272018-02-07 21:00:42 -05001910 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001911 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);
1915
1916 size = libc_sendfile (out_fd, in_fd, offset, len);
1917 }
1918
1919done:
Dave Wallace2a865272018-02-07 21:00:42 -05001920 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001921 {
1922 if (size < 0)
1923 {
1924 int errno_val = errno;
1925 perror (func_str);
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);
1929 errno = errno_val;
1930 }
1931 else
1932 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1933 getpid (), out_fd, out_fd, size, size);
1934 }
1935 return size;
1936}
1937
1938ssize_t
1939sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1940{
1941 return sendfile (out_fd, in_fd, offset, len);
1942}
1943
1944ssize_t
1945recv (int fd, void *buf, size_t n, int flags)
1946{
1947 ssize_t size;
1948 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001949 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001950
Dave Wallace2a865272018-02-07 21:00:42 -05001951 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001952 return -1;
1953
1954 if (sid != INVALID_SESSION_ID)
1955 {
1956 func_str = "vppcom_session_recvfrom";
1957
Dave Wallace2a865272018-02-07 21:00:42 -05001958 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001959 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);
1962
1963 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1964 if (size < 0)
1965 {
1966 errno = -size;
1967 size = -1;
1968 }
1969 }
1970 else
1971 {
1972 func_str = "libc_recv";
1973
Dave Wallace2a865272018-02-07 21:00:42 -05001974 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001975 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);
1978
1979 size = libc_recv (fd, buf, n, flags);
1980 }
1981
Dave Wallace2a865272018-02-07 21:00:42 -05001982 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001983 {
1984 if (size < 0)
1985 {
1986 int errno_val = errno;
1987 perror (func_str);
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);
1991 errno = errno_val;
1992 }
1993 else
1994 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1995 getpid (), fd, fd, size, size);
1996 }
1997 return size;
1998}
1999
2000ssize_t
2001sendto (int fd, const void *buf, size_t n, int flags,
2002 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
2003{
2004 ssize_t size;
2005 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05002006 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002007
Dave Wallace2a865272018-02-07 21:00:42 -05002008 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002009 return -1;
2010
2011 if (sid != INVALID_SESSION_ID)
2012 {
2013 vppcom_endpt_t *ep = 0;
2014 vppcom_endpt_t _ep;
2015
2016 if (addr)
2017 {
2018 ep = &_ep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002019 switch (addr->sa_family)
2020 {
2021 case AF_INET:
2022 ep->is_ip4 = VPPCOM_IS_IP4;
2023 ep->ip =
2024 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
2025 ep->port =
2026 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
2027 break;
2028
2029 case AF_INET6:
2030 ep->is_ip4 = VPPCOM_IS_IP6;
2031 ep->ip =
2032 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
2033 ep->port =
2034 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
2035 break;
2036
2037 default:
2038 errno = EAFNOSUPPORT;
2039 size = -1;
2040 goto done;
2041 }
2042 }
2043
2044 func_str = "vppcom_session_sendto";
2045
Dave Wallace2a865272018-02-07 21:00:42 -05002046 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002047 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,
2050 flags, ep);
2051
2052 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
2053 if (size < 0)
2054 {
2055 errno = -size;
2056 size = -1;
2057 }
2058 }
2059 else
2060 {
2061 func_str = "libc_sendto";
2062
Dave Wallace2a865272018-02-07 21:00:42 -05002063 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002064 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,
2067 addr, addr_len);
2068
2069 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
2070 }
2071
2072done:
Dave Wallace2a865272018-02-07 21:00:42 -05002073 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002074 {
2075 if (size < 0)
2076 {
2077 int errno_val = errno;
2078 perror (func_str);
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);
2082 errno = errno_val;
2083 }
2084 else
2085 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2086 getpid (), fd, fd, size, size);
2087 }
2088 return size;
2089}
2090
2091ssize_t
2092recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2093 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2094{
2095 ssize_t size;
2096 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002097 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002098
Dave Wallace2a865272018-02-07 21:00:42 -05002099 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002100 return -1;
2101
2102 if (sid != INVALID_SESSION_ID)
2103 {
2104 vppcom_endpt_t ep;
2105 u8 src_addr[sizeof (struct sockaddr_in6)];
2106
2107 func_str = "vppcom_session_recvfrom";
2108
Dave Wallace2a865272018-02-07 21:00:42 -05002109 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002110 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,
2113 flags, &ep);
2114 if (addr)
2115 {
2116 ep.ip = src_addr;
2117 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2118
2119 if (size > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002120 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 }
2122 else
2123 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2124
2125 if (size < 0)
2126 {
2127 errno = -size;
2128 size = -1;
2129 }
2130 }
2131 else
2132 {
2133 func_str = "libc_recvfrom";
2134
Dave Wallace2a865272018-02-07 21:00:42 -05002135 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002136 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,
2139 addr, addr_len);
2140
2141 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2142 }
2143
Dave Wallace2a865272018-02-07 21:00:42 -05002144 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002145 {
2146 if (size < 0)
2147 {
2148 int errno_val = errno;
2149 perror (func_str);
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);
2153 errno = errno_val;
2154 }
2155 else
2156 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2157 getpid (), fd, fd, size, size);
2158 }
2159 return size;
2160}
2161
2162ssize_t
2163sendmsg (int fd, const struct msghdr * message, int flags)
2164{
2165 ssize_t size;
2166 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002167 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002168
Dave Wallace2a865272018-02-07 21:00:42 -05002169 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002170 return -1;
2171
2172 if (sid != INVALID_SESSION_ID)
2173 {
Dave Wallace8aaba562018-01-18 17:21:19 -05002174 func_str = __func__;
2175
Dave Wallace048b1d62018-01-03 22:24:41 -05002176 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2177 errno = ENOSYS;
2178 size = -1;
2179 }
2180 else
2181 {
2182 func_str = "libc_sendmsg";
2183
Dave Wallace2a865272018-02-07 21:00:42 -05002184 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002185 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2186 "message %p, flags 0x%x",
2187 getpid (), fd, fd, func_str, message, flags);
2188
2189 size = libc_sendmsg (fd, message, flags);
2190 }
2191
Dave Wallace2a865272018-02-07 21:00:42 -05002192 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002193 {
2194 if (size < 0)
2195 {
2196 int errno_val = errno;
2197 perror (func_str);
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);
2201 errno = errno_val;
2202 }
2203 else
2204 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2205 getpid (), fd, fd, size, size);
2206 }
2207 return size;
2208}
2209
2210#ifdef USE_GNU
2211int
2212sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2213{
2214 ssize_t size;
2215 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002216 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002217
Dave Wallace2a865272018-02-07 21:00:42 -05002218 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002219 return -1;
2220
2221 if (sid != INVALID_SESSION_ID)
2222 {
2223 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2224 errno = ENOSYS;
2225 size = -1;
2226 }
2227 else
2228 {
2229 func_str = "libc_sendmmsg";
2230
Dave Wallace2a865272018-02-07 21:00:42 -05002231 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002232 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);
2235
2236 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2237 }
2238
Dave Wallace2a865272018-02-07 21:00:42 -05002239 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002240 {
2241 if (size < 0)
2242 {
2243 int errno_val = errno;
2244 perror (func_str);
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);
2248 errno = errno_val;
2249 }
2250 else
2251 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2252 getpid (), fd, fd, size, size);
2253 }
2254 return size;
2255}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002256#endif
2257
Dave Wallace048b1d62018-01-03 22:24:41 -05002258ssize_t
2259recvmsg (int fd, struct msghdr * message, int flags)
2260{
2261 ssize_t size;
2262 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002263 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002264
Dave Wallace2a865272018-02-07 21:00:42 -05002265 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002266 return -1;
2267
2268 if (sid != INVALID_SESSION_ID)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002269 {
Dave Wallace8aaba562018-01-18 17:21:19 -05002270 func_str = __func__;
2271
Dave Wallace048b1d62018-01-03 22:24:41 -05002272 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2273 errno = ENOSYS;
2274 size = -1;
2275 }
2276 else
2277 {
2278 func_str = "libc_recvmsg";
2279
Dave Wallace2a865272018-02-07 21:00:42 -05002280 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002281 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2282 "message %p, flags 0x%x",
2283 getpid (), fd, fd, func_str, message, flags);
2284
2285 size = libc_recvmsg (fd, message, flags);
2286 }
2287
Dave Wallace2a865272018-02-07 21:00:42 -05002288 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002289 {
2290 if (size < 0)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002291 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002292 int errno_val = errno;
2293 perror (func_str);
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);
2297 errno = errno_val;
2298 }
2299 else
2300 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2301 getpid (), fd, fd, size, size);
2302 }
2303 return size;
2304}
2305
2306#ifdef USE_GNU
2307int
2308recvmmsg (int fd, struct mmsghdr *vmessages,
2309 unsigned int vlen, int flags, struct timespec *tmo)
2310{
2311 ssize_t size;
2312 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002313 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002314
Dave Wallace2a865272018-02-07 21:00:42 -05002315 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002316 return -1;
2317
2318 if (sid != INVALID_SESSION_ID)
2319 {
2320 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2321 errno = ENOSYS;
2322 size = -1;
2323 }
2324 else
2325 {
2326 func_str = "libc_recvmmsg";
2327
Dave Wallace2a865272018-02-07 21:00:42 -05002328 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002329 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,
2332 flags, tmo);
2333
2334 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2335 }
2336
Dave Wallace2a865272018-02-07 21:00:42 -05002337 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002338 {
2339 if (size < 0)
2340 {
2341 int errno_val = errno;
2342 perror (func_str);
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);
2346 errno = errno_val;
2347 }
2348 else
2349 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2350 getpid (), fd, fd, size, size);
2351 }
2352 return size;
2353}
2354#endif
2355
2356int
2357getsockopt (int fd, int level, int optname,
2358 void *__restrict optval, socklen_t * __restrict optlen)
2359{
2360 int rv;
2361 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05002362 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace8aaba562018-01-18 17:21:19 -05002363 u32 buflen = optlen ? (u32) * optlen : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002364
Dave Wallace2a865272018-02-07 21:00:42 -05002365 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002366 return -1;
2367
2368 if (sid != INVALID_SESSION_ID)
2369 {
2370 rv = -EOPNOTSUPP;
2371
2372 switch (level)
2373 {
2374 case SOL_TCP:
2375 switch (optname)
2376 {
2377 case TCP_NODELAY:
2378 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002379 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002380 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2381 "sid %u (0x%x)",
2382 getpid (), fd, fd, func_str, sid, sid);
2383 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2384 optval, optlen);
2385 break;
2386 case TCP_MAXSEG:
2387 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
Dave Wallace2a865272018-02-07 21:00:42 -05002388 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002389 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2390 "sid %u (0x%x)",
2391 getpid (), fd, fd, func_str, sid, sid);
2392 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2393 optval, optlen);
2394 break;
2395 case TCP_KEEPIDLE:
2396 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002397 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002398 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2399 "sid %u (0x%x)",
2400 getpid (), fd, fd, func_str, sid, sid);
2401 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2402 optval, optlen);
2403 break;
2404 case TCP_KEEPINTVL:
2405 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
Dave Wallace2a865272018-02-07 21:00:42 -05002406 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002407 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,
2411 optval, optlen);
2412 break;
2413 case TCP_INFO:
2414 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2415 {
Dave Wallace2a865272018-02-07 21:00:42 -05002416 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002417 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,
2421 optval, *optlen);
2422 memset (optval, 0, *optlen);
2423 rv = VPPCOM_OK;
2424 }
2425 else
2426 rv = -EFAULT;
2427 break;
2428 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002429 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002430 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);
2434 break;
2435 }
2436 break;
2437 case SOL_IPV6:
2438 switch (optname)
2439 {
2440 case IPV6_V6ONLY:
2441 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002442 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002443 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2444 "sid %u (0x%x)",
2445 getpid (), fd, fd, func_str, sid, sid);
2446 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2447 optval, optlen);
2448 break;
2449 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002450 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002451 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);
2455 break;
2456 }
2457 break;
2458 case SOL_SOCKET:
2459 switch (optname)
2460 {
2461 case SO_ACCEPTCONN:
2462 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
Dave Wallace2a865272018-02-07 21:00:42 -05002463 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002464 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2465 "sid %u (0x%x)",
2466 getpid (), fd, fd, func_str, sid, sid);
2467 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2468 optval, optlen);
2469 break;
2470 case SO_KEEPALIVE:
2471 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002472 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002473 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2474 "sid %u (0x%x)",
2475 getpid (), fd, fd, func_str, sid, sid);
2476 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2477 optval, optlen);
2478 break;
2479 case SO_PROTOCOL:
2480 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
Dave Wallace2a865272018-02-07 21:00:42 -05002481 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002482 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2483 "sid %u (0x%x)",
2484 getpid (), fd, fd, func_str, sid, sid);
2485 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2486 optval, optlen);
2487 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2488 break;
2489 case SO_SNDBUF:
2490 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
Dave Wallace2a865272018-02-07 21:00:42 -05002491 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002492 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,
2496 optval, optlen);
2497 break;
2498 case SO_RCVBUF:
2499 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
Dave Wallace2a865272018-02-07 21:00:42 -05002500 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002501 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2502 "sid %u (0x%x), optlen %d",
Dave Wallaceb4cd4ff2018-01-19 12:17:08 -05002503 getpid (), fd, fd, func_str, sid, sid, buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002504 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2505 optval, optlen);
2506 break;
2507 case SO_REUSEADDR:
2508 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
Dave Wallace2a865272018-02-07 21:00:42 -05002509 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002510 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2511 "sid %u (0x%x)",
2512 getpid (), fd, fd, func_str, sid, sid);
2513 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2514 optval, optlen);
2515 break;
2516 case SO_BROADCAST:
2517 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
Dave Wallace2a865272018-02-07 21:00:42 -05002518 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002519 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2520 "sid %u (0x%x)",
2521 getpid (), fd, fd, func_str, sid, sid);
2522 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2523 optval, optlen);
2524 break;
2525 case SO_ERROR:
2526 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
Dave Wallace2a865272018-02-07 21:00:42 -05002527 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002528 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2529 "sid %u (0x%x)",
2530 getpid (), fd, fd, func_str, sid, sid);
2531 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2532 optval, optlen);
2533 break;
2534 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002535 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002536 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);
2540 break;
2541 }
2542 break;
2543 default:
2544 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002545 }
2546
Dave Wallace048b1d62018-01-03 22:24:41 -05002547 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002548 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002549 errno = -rv;
2550 rv = -1;
2551 }
2552 }
2553 else
2554 {
2555 func_str = "libc_getsockopt";
2556
Dave Wallace2a865272018-02-07 21:00:42 -05002557 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002558 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,
2561 optval, optlen);
2562
2563 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2564 }
2565
Dave Wallace2a865272018-02-07 21:00:42 -05002566 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002567 {
2568 if (rv < 0)
2569 {
2570 int errno_val = errno;
2571 perror (func_str);
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);
2575 errno = errno_val;
2576 }
2577 else
2578 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2579 getpid (), fd, fd, rv, rv);
2580 }
2581 return rv;
2582}
2583
2584int
2585setsockopt (int fd, int level, int optname,
2586 const void *optval, socklen_t optlen)
2587{
2588 int rv;
2589 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05002590 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002591
Dave Wallace2a865272018-02-07 21:00:42 -05002592 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002593 return -1;
2594
2595 if (sid != INVALID_SESSION_ID)
2596 {
2597 rv = -EOPNOTSUPP;
2598
2599 switch (level)
2600 {
2601 case SOL_TCP:
2602 switch (optname)
2603 {
2604 case TCP_NODELAY:
2605 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002606 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002607 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2608 "sid %u (0x%x)",
2609 getpid (), fd, fd, func_str, sid, sid);
2610 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2611 (void *) optval, &optlen);
2612 break;
2613 case TCP_MAXSEG:
2614 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
Dave Wallace2a865272018-02-07 21:00:42 -05002615 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002616 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2617 "sid %u (0x%x)",
2618 getpid (), fd, fd, func_str, sid, sid);
2619 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2620 (void *) optval, &optlen);
2621 break;
2622 case TCP_KEEPIDLE:
2623 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002624 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002625 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2626 "sid %u (0x%x)",
2627 getpid (), fd, fd, func_str, sid, sid);
2628 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2629 (void *) optval, &optlen);
2630 break;
2631 case TCP_KEEPINTVL:
2632 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
Dave Wallace2a865272018-02-07 21:00:42 -05002633 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002634 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);
2639 break;
2640 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002641 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002642 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);
2646 break;
2647 }
2648 break;
2649 case SOL_IPV6:
2650 switch (optname)
2651 {
2652 case IPV6_V6ONLY:
2653 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002654 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002655 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2656 "sid %u (0x%x)",
2657 getpid (), fd, fd, func_str, sid, sid);
2658 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2659 (void *) optval, &optlen);
2660 break;
2661 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002662 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002663 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);
2667 break;
2668 }
2669 break;
2670 case SOL_SOCKET:
2671 switch (optname)
2672 {
2673 case SO_KEEPALIVE:
2674 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002675 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002676 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2677 "sid %u (0x%x)",
2678 getpid (), fd, fd, func_str, sid, sid);
2679 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2680 (void *) optval, &optlen);
2681 break;
2682 case SO_REUSEADDR:
2683 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
Dave Wallace2a865272018-02-07 21:00:42 -05002684 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002685 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2686 "sid %u (0x%x)",
2687 getpid (), fd, fd, func_str, sid, sid);
2688 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2689 (void *) optval, &optlen);
2690 break;
2691 case SO_BROADCAST:
2692 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
Dave Wallace2a865272018-02-07 21:00:42 -05002693 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002694 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2695 "sid %u (0x%x)",
2696 getpid (), fd, fd, func_str, sid, sid);
2697 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2698 (void *) optval, &optlen);
2699 break;
2700 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002701 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002702 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);
2706 break;
2707 }
2708 break;
2709 default:
2710 break;
2711 }
2712
2713 if (rv != VPPCOM_OK)
2714 {
2715 errno = -rv;
2716 rv = -1;
2717 }
2718 }
2719 else
2720 {
2721 func_str = "libc_setsockopt";
2722
Dave Wallace2a865272018-02-07 21:00:42 -05002723 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002724 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,
2727 optval, optlen);
2728
2729 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2730 }
2731
Dave Wallace2a865272018-02-07 21:00:42 -05002732 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002733 {
2734 if (rv < 0)
2735 {
2736 int errno_val = errno;
2737 perror (func_str);
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);
2741 errno = errno_val;
2742 }
2743 else
2744 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2745 getpid (), fd, fd, rv, rv);
2746 }
2747 return rv;
2748}
2749
2750int
2751listen (int fd, int n)
2752{
2753 int rv;
2754 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002755 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002756
Dave Wallace2a865272018-02-07 21:00:42 -05002757 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002758 return -1;
2759
2760 if (sid != INVALID_SESSION_ID)
2761 {
2762 func_str = "vppcom_session_listen";
2763
Dave Wallace2a865272018-02-07 21:00:42 -05002764 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002765 clib_warning
2766 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), n %d",
2767 getpid (), fd, fd, func_str, sid, sid, n);
2768
2769 rv = vppcom_session_listen (sid, n);
2770 if (rv != VPPCOM_OK)
2771 {
2772 errno = -rv;
2773 rv = -1;
2774 }
2775 }
2776 else
2777 {
2778 func_str = "libc_listen";
2779
Dave Wallace2a865272018-02-07 21:00:42 -05002780 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002781 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): n %d",
2782 getpid (), fd, fd, func_str, n);
2783
2784 rv = libc_listen (fd, n);
2785 }
2786
Dave Wallace2a865272018-02-07 21:00:42 -05002787 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002788 {
2789 if (rv < 0)
2790 {
2791 int errno_val = errno;
2792 perror (func_str);
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);
2796 errno = errno_val;
2797 }
2798 else
2799 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2800 getpid (), fd, fd, rv, rv);
2801 }
2802 return rv;
2803}
2804
2805static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002806ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2807 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002808{
2809 int rv;
2810 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002811 u32 listen_sid = ldp_sid_from_fd (listen_fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002812 int accept_sid;
2813
Dave Wallace2a865272018-02-07 21:00:42 -05002814 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002815 return -1;
2816
2817 if (listen_sid != INVALID_SESSION_ID)
2818 {
2819 vppcom_endpt_t ep;
2820 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002821 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002822 ep.ip = src_addr;
2823
2824 func_str = "vppcom_session_accept";
2825
Dave Wallace2a865272018-02-07 21:00:42 -05002826 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002827 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);
2831
2832 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2833 if (accept_sid < 0)
2834 {
2835 errno = -accept_sid;
2836 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002837 }
2838 else
2839 {
Dave Wallace2a865272018-02-07 21:00:42 -05002840 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002841 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002842 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002843 (void) vppcom_session_close ((u32) accept_sid);
2844 errno = -rv;
2845 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002846 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002847 else
2848 {
Dave Wallace2a865272018-02-07 21:00:42 -05002849 func_str = "ldp_fd_from_sid";
2850 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002851 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);
Dave Wallace2a865272018-02-07 21:00:42 -05002855 rv = ldp_fd_from_sid ((u32) accept_sid);
Dave Wallace048b1d62018-01-03 22:24:41 -05002856 if (rv < 0)
2857 {
2858 (void) vppcom_session_close ((u32) accept_sid);
2859 errno = -rv;
2860 rv = -1;
2861 }
2862 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002863 }
2864 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002865 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002866 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002867 func_str = "libc_accept4";
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002868
Dave Wallace2a865272018-02-07 21:00:42 -05002869 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002870 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);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002874
Dave Wallace048b1d62018-01-03 22:24:41 -05002875 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002876 }
2877
Dave Wallace2a865272018-02-07 21:00:42 -05002878 if (LDP_DEBUG > 0)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002879 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002880 if (rv < 0)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002881 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002882 int errno_val = errno;
2883 perror (func_str);
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);
2887 errno = errno_val;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002888 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002889 else
2890 clib_warning ("LDP<%d>: listen fd %d (0x%x): returning %d (0x%x)",
2891 getpid (), listen_fd, listen_fd, rv, rv);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002892 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002893 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002894}
2895
Dave Wallace048b1d62018-01-03 22:24:41 -05002896int
2897accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2898 int flags)
2899{
Dave Wallace2a865272018-02-07 21:00:42 -05002900 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002901}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002902
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002903int
Dave Wallace048b1d62018-01-03 22:24:41 -05002904accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002905{
Dave Wallace2a865272018-02-07 21:00:42 -05002906 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002907}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002908
Dave Wallace048b1d62018-01-03 22:24:41 -05002909int
2910shutdown (int fd, int how)
2911{
2912 int rv;
2913 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002914 u32 sid = ldp_sid_from_fd (fd);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002915
Dave Wallace2a865272018-02-07 21:00:42 -05002916 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002917 return -1;
2918
2919 if (sid != INVALID_SESSION_ID)
2920 {
Dave Wallace8aaba562018-01-18 17:21:19 -05002921 func_str = __func__;
2922
Dave Wallace048b1d62018-01-03 22:24:41 -05002923 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2924 errno = ENOSYS;
2925 rv = -1;
2926 }
2927 else
2928 {
2929 func_str = "libc_shutdown";
2930
Dave Wallace2a865272018-02-07 21:00:42 -05002931 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002932 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): how %d",
2933 getpid (), fd, fd, func_str, how);
2934
2935 rv = libc_shutdown (fd, how);
2936 }
2937
Dave Wallace2a865272018-02-07 21:00:42 -05002938 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002939 {
2940 if (rv < 0)
2941 {
2942 int errno_val = errno;
2943 perror (func_str);
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);
2947 errno = errno_val;
2948 }
2949 else
2950 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2951 getpid (), fd, fd, rv, rv);
2952 }
2953 return rv;
2954}
2955
2956int
2957epoll_create1 (int flags)
2958{
2959 const char *func_str;
2960 int rv;
2961
Dave Wallace2a865272018-02-07 21:00:42 -05002962 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002963 return -1;
2964
2965 func_str = "vppcom_epoll_create";
2966
Dave Wallace2a865272018-02-07 21:00:42 -05002967 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002968 clib_warning ("LDP<%d>: calling %s()", getpid (), func_str);
2969
2970 rv = vppcom_epoll_create ();
2971
2972 if (PREDICT_FALSE (rv < 0))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002973 {
2974 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002975 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002976 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002977 else
Dave Wallace2a865272018-02-07 21:00:42 -05002978 rv = ldp_fd_from_sid ((u32) rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002979
Dave Wallace2a865272018-02-07 21:00:42 -05002980 if (LDP_DEBUG > 1)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002981 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002982 if (rv < 0)
2983 {
2984 int errno_val = errno;
2985 perror (func_str);
2986 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2987 "rv %d, errno = %d",
2988 getpid (), func_str, rv, errno_val);
2989 errno = errno_val;
2990 }
2991 else
2992 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002993 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002994 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002995}
2996
2997int
Dave Wallace048b1d62018-01-03 22:24:41 -05002998epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002999{
Dave Wallace048b1d62018-01-03 22:24:41 -05003000 return epoll_create1 (0);
3001}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003002
Dave Wallace048b1d62018-01-03 22:24:41 -05003003int
3004epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
3005{
3006 int rv;
3007 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05003008 u32 vep_idx = ldp_sid_from_fd (epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -05003009
Dave Wallace2a865272018-02-07 21:00:42 -05003010 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05003011 return -1;
3012
3013 if (vep_idx != INVALID_SESSION_ID)
3014 {
Dave Wallace2a865272018-02-07 21:00:42 -05003015 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05003016
3017 if (sid != INVALID_SESSION_ID)
3018 {
3019 func_str = "vppcom_epoll_create";
3020
Dave Wallace2a865272018-02-07 21:00:42 -05003021 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003022 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,
3025 sid, sid, event);
3026
3027 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
3028 if (rv != VPPCOM_OK)
3029 {
3030 errno = -rv;
3031 rv = -1;
3032 }
3033 }
3034 else
3035 {
3036 int epfd;
3037 u32 size = sizeof (epfd);
3038
3039 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
3040 epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD,
3041 0, 0);
3042 if (!epfd)
3043 {
3044 func_str = "libc_epoll_create1";
3045
Dave Wallace2a865272018-02-07 21:00:42 -05003046 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003047 clib_warning ("LDP<%d>: calling %s(): EPOLL_CLOEXEC",
3048 getpid (), func_str);
3049
3050 epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
3051 if (epfd < 0)
3052 {
3053 rv = epfd;
3054 goto done;
3055 }
3056
3057 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
3058 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
3059 &epfd, &size);
3060 if (rv < 0)
3061 {
3062 errno = -rv;
3063 rv = -1;
3064 goto done;
3065 }
3066 }
3067 else if (PREDICT_FALSE (epfd < 0))
3068 {
3069 errno = -epfd;
3070 rv = -1;
3071 goto done;
3072 }
3073
3074 rv = libc_epoll_ctl (epfd, op, fd, event);
3075 }
3076 }
3077 else
3078 {
3079 func_str = "libc_epoll_ctl";
3080
Dave Wallace2a865272018-02-07 21:00:42 -05003081 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003082 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);
3085
3086 rv = libc_epoll_ctl (epfd, op, fd, event);
3087 }
3088
3089done:
Dave Wallace2a865272018-02-07 21:00:42 -05003090 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003091 {
3092 if (rv < 0)
3093 {
3094 int errno_val = errno;
3095 perror (func_str);
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);
3099 errno = errno_val;
3100 }
3101 else
3102 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
3103 getpid (), fd, fd, rv, rv);
3104 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003105 return rv;
3106}
Dave Wallace048b1d62018-01-03 22:24:41 -05003107
3108static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05003109ldp_epoll_pwait (int epfd, struct epoll_event *events,
3110 int maxevents, int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05003111{
3112 const char *func_str;
Dave Wallace8aaba562018-01-18 17:21:19 -05003113 int rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003114 double time_to_wait = (double) 0;
3115 double time_out, now = 0;
Dave Wallace2a865272018-02-07 21:00:42 -05003116 u32 vep_idx = ldp_sid_from_fd (epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -05003117 int libc_epfd;
3118
Dave Wallace2a865272018-02-07 21:00:42 -05003119 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05003120 return -1;
3121
3122 if (PREDICT_FALSE (!events || (timeout < -1)))
3123 {
3124 errno = EFAULT;
3125 return -1;
3126 }
3127
3128 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3129 {
3130 clib_warning ("LDP<%d>: ERROR: epfd %d (0x%x): bad vep_idx %d (0x%x)!",
3131 getpid (), epfd, epfd, vep_idx, vep_idx);
3132 errno = EBADFD;
3133 return -1;
3134 }
3135
3136 time_to_wait = ((timeout >= 0) ? (double) timeout / (double) 1000 : 0);
Dave Wallace2a865272018-02-07 21:00:42 -05003137 time_out = clib_time_now (&ldp->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05003138
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))
3142 {
3143 errno = -libc_epfd;
3144 rv = -1;
3145 goto done;
3146 }
3147
Dave Wallace2a865272018-02-07 21:00:42 -05003148 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05003149 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);
3154 do
3155 {
Dave Wallace2a865272018-02-07 21:00:42 -05003156 if (!ldp->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05003157 {
3158 func_str = "vppcom_epoll_wait";
3159
Dave Wallace2a865272018-02-07 21:00:42 -05003160 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003161 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);
3165
3166 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3167 if (rv > 0)
3168 {
Dave Wallace2a865272018-02-07 21:00:42 -05003169 ldp->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05003170 goto done;
3171 }
3172 else if (rv < 0)
3173 {
3174 errno = -rv;
3175 rv = -1;
3176 goto done;
3177 }
3178 }
3179 else
Dave Wallace2a865272018-02-07 21:00:42 -05003180 ldp->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003181
3182 if (libc_epfd > 0)
3183 {
3184 func_str = "libc_epoll_pwait";
3185
Dave Wallace2a865272018-02-07 21:00:42 -05003186 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003187 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);
3192
Keith Burns (alagalah)5368efa2018-02-07 13:20:28 -08003193 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 1, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05003194 if (rv != 0)
3195 goto done;
3196 }
3197
3198 if (timeout != -1)
Dave Wallace2a865272018-02-07 21:00:42 -05003199 now = clib_time_now (&ldp->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003200 }
3201 while (now < time_out);
3202
3203done:
Dave Wallace2a865272018-02-07 21:00:42 -05003204 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003205 {
Keith Burns (alagalah)5368efa2018-02-07 13:20:28 -08003206 if (libc_epfd > 0)
3207 epfd = libc_epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -05003208 if (rv < 0)
3209 {
3210 int errno_val = errno;
3211 perror (func_str);
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);
3215 errno = errno_val;
3216 }
3217 else
3218 clib_warning ("LDP<%d>: epfd %d (0x%x): returning %d (0x%x)",
3219 getpid (), epfd, epfd, rv, rv);
3220 }
3221 return rv;
3222}
3223
3224int
3225epoll_pwait (int epfd, struct epoll_event *events,
3226 int maxevents, int timeout, const sigset_t * sigmask)
3227{
Dave Wallace2a865272018-02-07 21:00:42 -05003228 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05003229}
3230
3231int
3232epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3233{
Dave Wallace2a865272018-02-07 21:00:42 -05003234 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003235}
3236
3237int
3238poll (struct pollfd *fds, nfds_t nfds, int timeout)
3239{
3240 const char *func_str = __func__;
3241 int rv, i, n_libc_fds, n_revents;
3242 u32 sid;
3243 vcl_poll_t *vp;
3244 double wait_for_time;
3245
Dave Wallace2a865272018-02-07 21:00:42 -05003246 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003247 clib_warning ("LDP<%d>: fds %p, nfds %d, timeout %d",
3248 getpid (), fds, nfds, timeout);
3249
3250 if (timeout >= 0)
3251 wait_for_time = (f64) timeout / 1000;
3252 else
3253 wait_for_time = -1;
3254
3255 n_libc_fds = 0;
3256 for (i = 0; i < nfds; i++)
3257 {
3258 if (fds[i].fd >= 0)
3259 {
Dave Wallace2a865272018-02-07 21:00:42 -05003260 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003261 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);
3264
Dave Wallace2a865272018-02-07 21:00:42 -05003265 sid = ldp_sid_from_fd (fds[i].fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05003266 if (sid != INVALID_SESSION_ID)
3267 {
3268 fds[i].fd = -fds[i].fd;
Dave Wallace2a865272018-02-07 21:00:42 -05003269 vec_add2 (ldp->vcl_poll, vp, 1);
Dave Wallace048b1d62018-01-03 22:24:41 -05003270 vp->fds_ndx = i;
3271 vp->sid = sid;
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;
3278#endif
3279 vp->revents = &fds[i].revents;
3280 }
3281 else
3282 n_libc_fds++;
3283 }
3284 }
3285
3286 n_revents = 0;
3287 do
3288 {
Dave Wallace2a865272018-02-07 21:00:42 -05003289 if (vec_len (ldp->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05003290 {
3291 func_str = "vppcom_poll";
3292
Dave Wallace2a865272018-02-07 21:00:42 -05003293 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003294 clib_warning ("LDP<%d>: calling %s(): "
3295 "vcl_poll %p, n_sids %u (0x%x): "
3296 "n_libc_fds %u",
Dave Wallace2a865272018-02-07 21:00:42 -05003297 getpid (), func_str, ldp->vcl_poll,
3298 vec_len (ldp->vcl_poll), vec_len (ldp->vcl_poll),
Dave Wallace048b1d62018-01-03 22:24:41 -05003299 n_libc_fds);
3300
Dave Wallace2a865272018-02-07 21:00:42 -05003301 rv = vppcom_poll (ldp->vcl_poll, vec_len (ldp->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05003302 if (rv < 0)
3303 {
3304 errno = -rv;
3305 rv = -1;
3306 goto done;
3307 }
3308 else
3309 n_revents += rv;
3310 }
3311
3312 if (n_libc_fds)
3313 {
3314 func_str = "libc_poll";
3315
Dave Wallace2a865272018-02-07 21:00:42 -05003316 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003317 clib_warning ("LDP<%d>: calling %s(): fds %p, nfds %u: n_sids %u",
Dave Wallace2a865272018-02-07 21:00:42 -05003318 getpid (), fds, nfds, vec_len (ldp->vcl_poll));
Dave Wallace048b1d62018-01-03 22:24:41 -05003319
3320 rv = libc_poll (fds, nfds, 0);
3321 if (rv < 0)
3322 goto done;
3323 else
3324 n_revents += rv;
3325 }
3326
3327 if (n_revents)
3328 {
3329 rv = n_revents;
3330 goto done;
3331 }
3332 }
3333 while ((wait_for_time == -1) ||
Dave Wallace2a865272018-02-07 21:00:42 -05003334 (clib_time_now (&ldp->clib_time) < wait_for_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05003335 rv = 0;
3336
3337done:
Dave Wallace2a865272018-02-07 21:00:42 -05003338 vec_foreach (vp, ldp->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 {
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;
3348#endif
3349 }
Dave Wallace2a865272018-02-07 21:00:42 -05003350 vec_reset_length (ldp->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05003351
Dave Wallace2a865272018-02-07 21:00:42 -05003352 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003353 {
3354 if (rv < 0)
3355 {
3356 int errno_val = errno;
3357 perror (func_str);
3358 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3359 "rv %d, errno = %d", getpid (),
3360 func_str, rv, errno_val);
3361 errno = errno_val;
3362 }
3363 else
3364 {
3365 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3366 "n_libc_fds %d", getpid (), rv, rv,
Dave Wallace2a865272018-02-07 21:00:42 -05003367 vec_len (ldp->vcl_poll), n_libc_fds);
Dave Wallace048b1d62018-01-03 22:24:41 -05003368
3369 for (i = 0; i < nfds; i++)
3370 {
3371 if (fds[i].fd >= 0)
3372 {
Dave Wallace2a865272018-02-07 21:00:42 -05003373 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003374 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);
3378 }
3379 }
3380 }
3381 }
3382
3383 return rv;
3384}
3385
3386#ifdef USE_GNU
3387int
3388ppoll (struct pollfd *fds, nfds_t nfds,
3389 const struct timespec *timeout, const sigset_t * sigmask)
3390{
Dave Wallace2a865272018-02-07 21:00:42 -05003391 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05003392 return -1;
3393
3394 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3395 errno = ENOSYS;
3396
3397
3398 return -1;
3399}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003400#endif
3401
Dave Wallace2a865272018-02-07 21:00:42 -05003402void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003403
Dave Wallace2a865272018-02-07 21:00:42 -05003404void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003405
Dave Wallace048b1d62018-01-03 22:24:41 -05003406/*
3407 * This function is called when the library is loaded
3408 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003409void
Dave Wallace2a865272018-02-07 21:00:42 -05003410ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003411{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003412 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05003413 if (ldp_init () != 0)
3414 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
Dave Wallace048b1d62018-01-03 22:24:41 -05003415 getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003416 else
Dave Wallace2a865272018-02-07 21:00:42 -05003417 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003418}
3419
3420/*
3421 * This function is called when the library is unloaded
3422 */
3423void
Dave Wallace2a865272018-02-07 21:00:42 -05003424ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003425{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003426 swrap_destructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05003427 if (ldp->init)
Dave Wallace048b1d62018-01-03 22:24:41 -05003428 {
3429 vppcom_app_destroy ();
Dave Wallace2a865272018-02-07 21:00:42 -05003430 ldp->init = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003431 }
3432
3433 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05003434 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05003435 */
Dave Wallace2a865272018-02-07 21:00:42 -05003436 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
Dave Wallace048b1d62018-01-03 22:24:41 -05003437 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003438}
3439
3440
3441/*
3442 * fd.io coding-style-patch-verification: ON
3443 *
3444 * Local Variables:
3445 * eval: (c-set-style "gnu")
3446 * End:
3447 */