blob: a4b66feedfc96c122978b9fbd8383c2aa38dd3b2 [file] [log] [blame]
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003 * 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
Florin Coras7baeb712019-01-04 17:05:43 -080029#include <vcl/vcl_locked.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050030#include <vppinfra/time.h>
31#include <vppinfra/bitmap.h>
Florin Coras30e273b2018-11-27 00:04:59 -080032#include <vppinfra/lock.h>
33#include <vppinfra/pool.h>
34#include <vppinfra/hash.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070035
36#define HAVE_CONSTRUCTOR_ATTRIBUTE
37#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
38#define CONSTRUCTOR_ATTRIBUTE \
39 __attribute__ ((constructor))
40#else
41#define CONSTRUCTOR_ATTRIBUTE
42#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
43
44#define HAVE_DESTRUCTOR_ATTRIBUTE
45#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
46#define DESTRUCTOR_ATTRIBUTE \
47 __attribute__ ((destructor))
48#else
49#define DESTRUCTOR_ATTRIBUTE
50#endif
51
Florin Corasdfe4cf42018-11-28 22:13:45 -080052#define LDP_MAX_NWORKERS 32
53
Florin Corasdfe4cf42018-11-28 22:13:45 -080054typedef struct ldp_worker_ctx_
Dave Wallace048b1d62018-01-03 22:24:41 -050055{
Dave Wallace048b1d62018-01-03 22:24:41 -050056 u8 *io_buffer;
57 clib_time_t clib_time;
Florin Corasdfe4cf42018-11-28 22:13:45 -080058
59 /*
60 * Select state
61 */
Dave Wallace048b1d62018-01-03 22:24:41 -050062 clib_bitmap_t *rd_bitmap;
63 clib_bitmap_t *wr_bitmap;
64 clib_bitmap_t *ex_bitmap;
Florin Coras294afe22019-01-07 17:49:17 -080065 clib_bitmap_t *si_rd_bitmap;
66 clib_bitmap_t *si_wr_bitmap;
67 clib_bitmap_t *si_ex_bitmap;
Dave Wallace048b1d62018-01-03 22:24:41 -050068 clib_bitmap_t *libc_rd_bitmap;
69 clib_bitmap_t *libc_wr_bitmap;
70 clib_bitmap_t *libc_ex_bitmap;
Florin Corasdfe4cf42018-11-28 22:13:45 -080071
72 /*
73 * Poll state
74 */
Dave Wallace048b1d62018-01-03 22:24:41 -050075 vcl_poll_t *vcl_poll;
Florin Coras6917b942018-11-13 22:44:54 -080076 struct pollfd *libc_poll;
77 u16 *libc_poll_idxs;
Florin Corasdfe4cf42018-11-28 22:13:45 -080078
79 /*
80 * Epoll state
81 */
Dave Wallace048b1d62018-01-03 22:24:41 -050082 u8 epoll_wait_vcl;
hanlin4266d4d2020-05-19 17:34:17 +080083 u8 mq_epfd_added;
Florin Coras99368312018-08-02 10:45:44 -070084 int vcl_mq_epfd;
Florin Corasdfe4cf42018-11-28 22:13:45 -080085
86} ldp_worker_ctx_t;
87
Florin Coras294afe22019-01-07 17:49:17 -080088/* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
89 * they are the same size */
90STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
91 "ldp bitmap size mismatch");
92STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
93 "ldp bitmap size mismatch");
94
Florin Corasdfe4cf42018-11-28 22:13:45 -080095typedef struct
96{
97 ldp_worker_ctx_t *workers;
98 int init;
99 char app_name[LDP_APP_NAME_MAX];
Florin Coras7baeb712019-01-04 17:05:43 -0800100 u32 vlsh_bit_val;
101 u32 vlsh_bit_mask;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800102 u32 debug;
Yu Ping7b74b072019-05-08 00:40:24 +0800103 u8 transparent_tls;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800104
105 /** vcl needs next epoll_create to go to libc_epoll */
106 u8 vcl_needs_real_epoll;
Dave Wallace2a865272018-02-07 21:00:42 -0500107} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800108
Dave Wallace2a865272018-02-07 21:00:42 -0500109#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700110
Florin Coras99368312018-08-02 10:45:44 -0700111#define LDBG(_lvl, _fmt, _args...) \
112 if (ldp->debug > _lvl) \
hanlin9f3f18f2019-12-30 16:25:20 +0800113 { \
114 int errno_saved = errno; \
115 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args); \
116 errno = errno_saved; \
117 }
Florin Coras99368312018-08-02 10:45:44 -0700118
Dave Wallace2a865272018-02-07 21:00:42 -0500119static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800120 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
121 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500122 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800123 .transparent_tls = 0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500124};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700125
Dave Wallace2a865272018-02-07 21:00:42 -0500126static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700127
Florin Corasdfe4cf42018-11-28 22:13:45 -0800128static inline ldp_worker_ctx_t *
129ldp_worker_get_current (void)
130{
131 return (ldp->workers + vppcom_worker_index ());
132}
133
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700134/*
135 * RETURN: 0 on success or -1 on error.
136 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500137static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500138ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700139{
Benoît Ganne747b3d82019-08-21 18:27:23 +0200140 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
141 "ldp-%d-%s", getpid (), app_name);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700142}
143
Dave Wallace048b1d62018-01-03 22:24:41 -0500144static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500145ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700146{
Dave Wallace2a865272018-02-07 21:00:42 -0500147 if (ldp->app_name[0] == '\0')
148 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500149
Dave Wallace2a865272018-02-07 21:00:42 -0500150 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700151}
152
Dave Wallace048b1d62018-01-03 22:24:41 -0500153static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800154ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500155{
Florin Coras7baeb712019-01-04 17:05:43 -0800156 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500157}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700158
Florin Coras7baeb712019-01-04 17:05:43 -0800159static inline vls_handle_t
160ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500161{
Florin Coras7baeb712019-01-04 17:05:43 -0800162 if (fd < ldp->vlsh_bit_val)
163 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700164
Florin Coras7baeb712019-01-04 17:05:43 -0800165 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500166}
167
Florin Coras2d9b4272019-03-11 10:14:37 -0700168static void
169ldp_alloc_workers (void)
170{
171 if (ldp->workers)
172 return;
173 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
174}
175
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700176static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500177ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700178{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800179 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700180 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700181
Florin Coras99368312018-08-02 10:45:44 -0700182 if (PREDICT_TRUE (ldp->init))
183 return 0;
184
185 ldp->init = 1;
186 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800187 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700188 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700189 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800190 ldp->vcl_needs_real_epoll = 0;
191 if (rv == VPPCOM_EEXIST)
192 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800193 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
194 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700195 ldp->init = 0;
196 return rv;
197 }
198 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700199 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800200 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700201
202 char *env_var_str = getenv (LDP_ENV_DEBUG);
203 if (env_var_str)
204 {
205 u32 tmp;
206 if (sscanf (env_var_str, "%u", &tmp) != 1)
207 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
208 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
209 env_var_str);
210 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700211 {
Florin Coras99368312018-08-02 10:45:44 -0700212 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800213 LDBG (0, "configured LDP debug level (%u) from env var "
214 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700215 }
216 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500217
Florin Coras99368312018-08-02 10:45:44 -0700218 env_var_str = getenv (LDP_ENV_APP_NAME);
219 if (env_var_str)
220 {
221 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800222 LDBG (0, "configured LDP app name (%s) from the env var "
223 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700224 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500225
Florin Coras99368312018-08-02 10:45:44 -0700226 env_var_str = getenv (LDP_ENV_SID_BIT);
227 if (env_var_str)
228 {
229 u32 sb;
230 if (sscanf (env_var_str, "%u", &sb) != 1)
231 {
Florin Coras294afe22019-01-07 17:49:17 -0800232 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
233 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
234 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700235 }
236 else if (sb < LDP_SID_BIT_MIN)
237 {
Florin Coras7baeb712019-01-04 17:05:43 -0800238 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
239 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500240
Florin Coras294afe22019-01-07 17:49:17 -0800241 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
242 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
243 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
244 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700245 }
246 else if (sb > LDP_SID_BIT_MAX)
247 {
Florin Coras7baeb712019-01-04 17:05:43 -0800248 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
249 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500250
Florin Coras294afe22019-01-07 17:49:17 -0800251 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
252 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
253 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
254 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500255 }
256 else
257 {
Florin Coras7baeb712019-01-04 17:05:43 -0800258 ldp->vlsh_bit_val = (1 << sb);
259 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700260
Florin Coras05ecfcc2018-12-12 18:19:39 -0800261 LDBG (0, "configured LDP sid bit (%u) from "
262 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800263 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500264 }
Florin Coras294afe22019-01-07 17:49:17 -0800265
266 /* Make sure there are enough bits in the fd set for vcl sessions */
267 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
268 {
269 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
270 ldp->vlsh_bit_val, FD_SETSIZE / 2);
271 ldp->init = 0;
272 return -1;
273 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500274 }
Yu Ping7b74b072019-05-08 00:40:24 +0800275 env_var_str = getenv (LDP_ENV_TLS_TRANS);
276 if (env_var_str)
277 {
278 ldp->transparent_tls = 1;
279 }
Florin Coras99368312018-08-02 10:45:44 -0700280
Florin Coras4dee8cd2019-01-29 21:28:16 -0800281 /* *INDENT-OFF* */
282 pool_foreach (ldpw, ldp->workers, ({
283 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
284 }));
285 /* *INDENT-ON* */
286
Florin Coras05ecfcc2018-12-12 18:19:39 -0800287 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700288
289 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500290}
291
292int
293close (int fd)
294{
Florin Coras7baeb712019-01-04 17:05:43 -0800295 vls_handle_t vlsh;
296 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500297
Dave Wallace2a865272018-02-07 21:00:42 -0500298 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500299 return -1;
300
Florin Coras7baeb712019-01-04 17:05:43 -0800301 vlsh = ldp_fd_to_vlsh (fd);
302 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500303 {
Florin Coras7baeb712019-01-04 17:05:43 -0800304 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500305 if (epfd > 0)
306 {
Florin Coras7baeb712019-01-04 17:05:43 -0800307 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500308
309 rv = libc_close (epfd);
310 if (rv < 0)
311 {
312 u32 size = sizeof (epfd);
313 epfd = 0;
314
Florin Coras7baeb712019-01-04 17:05:43 -0800315 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500316 }
317 }
318 else if (PREDICT_FALSE (epfd < 0))
319 {
320 errno = -epfd;
321 rv = -1;
322 goto done;
323 }
324
Florin Coras7baeb712019-01-04 17:05:43 -0800325 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500326
Florin Coras7baeb712019-01-04 17:05:43 -0800327 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500328 if (rv != VPPCOM_OK)
329 {
330 errno = -rv;
331 rv = -1;
332 }
333 }
334 else
335 {
Florin Coras7baeb712019-01-04 17:05:43 -0800336 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500337 rv = libc_close (fd);
338 }
339
340done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500341 return rv;
342}
343
344ssize_t
345read (int fd, void *buf, size_t nbytes)
346{
Florin Coras7baeb712019-01-04 17:05:43 -0800347 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500348 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500349
Dave Wallace2a865272018-02-07 21:00:42 -0500350 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500351 return -1;
352
Florin Coras7baeb712019-01-04 17:05:43 -0800353 vlsh = ldp_fd_to_vlsh (fd);
354 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500355 {
Florin Coras7baeb712019-01-04 17:05:43 -0800356 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500357 if (size < 0)
358 {
359 errno = -size;
360 size = -1;
361 }
362 }
363 else
364 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500365 size = libc_read (fd, buf, nbytes);
366 }
367
Dave Wallace048b1d62018-01-03 22:24:41 -0500368 return size;
369}
370
371ssize_t
372readv (int fd, const struct iovec * iov, int iovcnt)
373{
Dave Wallace8aaba562018-01-18 17:21:19 -0500374 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800375 vls_handle_t vlsh;
376 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500377
Dave Wallace2a865272018-02-07 21:00:42 -0500378 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500379 return -1;
380
Florin Coras7baeb712019-01-04 17:05:43 -0800381 vlsh = ldp_fd_to_vlsh (fd);
382 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500383 {
Florin Coras067f9542020-02-14 05:33:46 +0000384 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500385 {
Florin Coras067f9542020-02-14 05:33:46 +0000386 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
387 if (rv <= 0)
388 break;
389 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500390 {
Florin Coras067f9542020-02-14 05:33:46 +0000391 total += rv;
392 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500393 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700394 }
395 }
Florin Coras067f9542020-02-14 05:33:46 +0000396 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700397 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500398 errno = -rv;
399 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700400 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500401 else
402 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700403 }
404 else
405 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500406 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700407 }
408
Dave Wallace048b1d62018-01-03 22:24:41 -0500409 return size;
410}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700411
Dave Wallace048b1d62018-01-03 22:24:41 -0500412ssize_t
413write (int fd, const void *buf, size_t nbytes)
414{
Florin Coras7baeb712019-01-04 17:05:43 -0800415 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500416 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500417
Dave Wallace2a865272018-02-07 21:00:42 -0500418 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500419 return -1;
420
Florin Coras7baeb712019-01-04 17:05:43 -0800421 vlsh = ldp_fd_to_vlsh (fd);
422 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500423 {
Florin Coras7baeb712019-01-04 17:05:43 -0800424 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500425 if (size < 0)
426 {
427 errno = -size;
428 size = -1;
429 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700430 }
431 else
432 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500433 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700434 }
435
Dave Wallace048b1d62018-01-03 22:24:41 -0500436 return size;
437}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700438
Dave Wallace048b1d62018-01-03 22:24:41 -0500439ssize_t
440writev (int fd, const struct iovec * iov, int iovcnt)
441{
Dave Wallace048b1d62018-01-03 22:24:41 -0500442 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800443 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500444 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500445
Dave Wallace2a865272018-02-07 21:00:42 -0500446 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500447 return -1;
448
Florin Coras7baeb712019-01-04 17:05:43 -0800449 vlsh = ldp_fd_to_vlsh (fd);
450 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500451 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000452 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500453 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000454 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
455 if (rv < 0)
456 break;
457 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500458 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000459 total += rv;
460 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500461 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500462 }
463 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500464
Florin Coraseda1b8c2020-03-23 16:00:35 +0000465 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500466 {
467 errno = -rv;
468 size = -1;
469 }
470 else
471 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700472 }
473 else
474 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500475 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700476 }
477
Dave Wallace048b1d62018-01-03 22:24:41 -0500478 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700479}
480
Florin Coras0ab36f52020-05-26 19:45:45 +0000481static int
482fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700483{
Florin Coras7baeb712019-01-04 17:05:43 -0800484 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700485 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800486
487 vlsh = ldp_fd_to_vlsh (fd);
488 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
489 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700490 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500491 int flags = va_arg (ap, int);
492 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700493
Dave Wallace048b1d62018-01-03 22:24:41 -0500494 size = sizeof (flags);
495 rv = -EOPNOTSUPP;
496 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700497 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500498 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800499 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500500 break;
501
502 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800503 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500504 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800505 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700506 break;
Florin Coras173bae32018-11-16 18:56:28 -0800507 case F_SETFD:
508 /* TODO handle this */
509 LDBG (0, "F_SETFD ignored flags %u", flags);
510 rv = 0;
511 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700512 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500513 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700514 break;
515 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500516 if (rv < 0)
517 {
518 errno = -rv;
519 rv = -1;
520 }
521 }
522 else
523 {
Carl Smithe16707b2019-11-13 14:37:39 +1300524#ifdef HAVE_FCNTL64
525 rv = libc_vfcntl64 (fd, cmd, ap);
526#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500527 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300528#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700529 }
530
Florin Coras0ab36f52020-05-26 19:45:45 +0000531 return rv;
532}
533
534int
535fcntl (int fd, int cmd, ...)
536{
537 va_list ap;
538 int rv;
539
540 if ((errno = -ldp_init ()))
541 return -1;
542
543 va_start (ap, cmd);
544 rv = fcntl_internal (fd, cmd, ap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500545 va_end (ap);
546
Dave Wallace048b1d62018-01-03 22:24:41 -0500547 return rv;
548}
549
550int
Florin Corasd7586d52020-04-29 02:19:51 +0000551fcntl64 (int fd, int cmd, ...)
552{
553 va_list ap;
554 int rv;
555
Florin Coras0ab36f52020-05-26 19:45:45 +0000556 if ((errno = -ldp_init ()))
557 return -1;
558
Florin Corasd7586d52020-04-29 02:19:51 +0000559 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000560 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000561 va_end (ap);
562 return rv;
563}
564
565int
Dave Wallace048b1d62018-01-03 22:24:41 -0500566ioctl (int fd, unsigned long int cmd, ...)
567{
Florin Coras7baeb712019-01-04 17:05:43 -0800568 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500569 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800570 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500571
Dave Wallace2a865272018-02-07 21:00:42 -0500572 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500573 return -1;
574
575 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500576
Florin Coras7baeb712019-01-04 17:05:43 -0800577 vlsh = ldp_fd_to_vlsh (fd);
578 if (vlsh != VLS_INVALID_HANDLE)
579 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500580 switch (cmd)
581 {
582 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800583 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500584 break;
585
586 case FIONBIO:
587 {
588 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
589 u32 size = sizeof (flags);
590
591 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
592 * non-blocking, the flags should be read here and merged
593 * with O_NONBLOCK.
594 */
Florin Coras7baeb712019-01-04 17:05:43 -0800595 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500596 }
597 break;
598
599 default:
600 rv = -EOPNOTSUPP;
601 break;
602 }
603 if (rv < 0)
604 {
605 errno = -rv;
606 rv = -1;
607 }
608 }
609 else
610 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500611 rv = libc_vioctl (fd, cmd, ap);
612 }
613
Dave Wallace048b1d62018-01-03 22:24:41 -0500614 va_end (ap);
615 return rv;
616}
617
Florin Coras294afe22019-01-07 17:49:17 -0800618always_inline void
619ldp_select_init_maps (fd_set * __restrict original,
620 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
621 clib_bitmap_t ** vclb, int nfds, u32 minbits,
622 u32 n_bytes, uword * si_bits, uword * libc_bits)
623{
624 uword si_bits_set, libc_bits_set;
625 vls_handle_t vlsh;
626 int fd;
627
628 clib_bitmap_validate (*vclb, minbits);
629 clib_bitmap_validate (*libcb, minbits);
630 clib_bitmap_validate (*resultb, minbits);
631 clib_memcpy_fast (*resultb, original, n_bytes);
632 memset (original, 0, n_bytes);
633
634 /* *INDENT-OFF* */
635 clib_bitmap_foreach (fd, *resultb, ({
636 if (fd > nfds)
637 break;
638 vlsh = ldp_fd_to_vlsh (fd);
639 if (vlsh == VLS_INVALID_HANDLE)
640 clib_bitmap_set_no_check (*libcb, fd, 1);
641 else
Florin Corascbce80a2020-04-20 01:32:38 +0000642 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Florin Coras294afe22019-01-07 17:49:17 -0800643 }));
644 /* *INDENT-ON* */
645
646 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
647 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000648 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800649
650 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
651 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
652}
653
654always_inline int
655ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
656{
657 vls_handle_t vlsh;
658 uword si;
659 int fd;
660
661 if (!libcb)
662 return 0;
663
664 /* *INDENT-OFF* */
665 clib_bitmap_foreach (si, vclb, ({
666 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000667 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800668 fd = ldp_vlsh_to_fd (vlsh);
669 if (PREDICT_FALSE (fd < 0))
670 {
671 errno = EBADFD;
672 return -1;
673 }
674 FD_SET (fd, libcb);
675 }));
676 /* *INDENT-ON* */
677
678 return 0;
679}
680
681always_inline void
682ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
683{
684 uword fd;
685
Florin Coras78b5fa62019-02-21 20:04:15 -0800686 if (!libcb)
687 return;
688
Florin Coras294afe22019-01-07 17:49:17 -0800689 /* *INDENT-OFF* */
690 clib_bitmap_foreach (fd, result, ({
691 FD_SET ((int)fd, libcb);
692 }));
693 /* *INDENT-ON* */
694}
695
Dave Wallace048b1d62018-01-03 22:24:41 -0500696int
Dave Wallace2a865272018-02-07 21:00:42 -0500697ldp_pselect (int nfds, fd_set * __restrict readfds,
698 fd_set * __restrict writefds,
699 fd_set * __restrict exceptfds,
700 const struct timespec *__restrict timeout,
701 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500702{
Florin Coras294afe22019-01-07 17:49:17 -0800703 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800704 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800705 struct timespec libc_tspec = { 0 };
706 f64 time_out, vcl_timeout = 0;
707 uword si_bits, libc_bits;
708 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500709
710 if (nfds < 0)
711 {
712 errno = EINVAL;
713 return -1;
714 }
715
Florin Coras4dee8cd2019-01-29 21:28:16 -0800716 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
717 clib_time_init (&ldpw->clib_time);
718
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500719 if (timeout)
720 {
721 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800722 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500723
724 /* select as fine grained sleep */
725 if (!nfds)
726 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800727 time_out += clib_time_now (&ldpw->clib_time);
728 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500729 ;
730 return 0;
731 }
732 }
733 else if (!nfds)
734 {
735 errno = EINVAL;
736 return -1;
737 }
738 else
739 time_out = -1;
740
Florin Coras7baeb712019-01-04 17:05:43 -0800741 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500742 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500743 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
744 timeout, sigmask);
745 goto done;
746 }
747
Florin Coras294afe22019-01-07 17:49:17 -0800748 si_bits = libc_bits = 0;
749 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800750
Dave Wallace048b1d62018-01-03 22:24:41 -0500751 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800752 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
753 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
754 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500755 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800756 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
757 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
758 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500759 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800760 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
761 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
762 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500763
Florin Coras294afe22019-01-07 17:49:17 -0800764 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500765 {
766 errno = EINVAL;
767 rv = -1;
768 goto done;
769 }
770
Florin Coras78b5fa62019-02-21 20:04:15 -0800771 if (!si_bits)
772 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800773
Dave Wallace048b1d62018-01-03 22:24:41 -0500774 do
775 {
Florin Coras294afe22019-01-07 17:49:17 -0800776 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500777 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500778 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800779 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000780 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500781 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800783 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000784 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500785 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500786 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800787 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000788 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500789 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800790
Florin Coras0ef8ef22019-01-18 08:37:13 -0800791 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
792 writefds ? ldpw->wr_bitmap : NULL,
793 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800794 if (rv < 0)
795 {
796 errno = -rv;
797 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000798 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800799 }
800 else if (rv > 0)
801 {
802 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
803 {
804 rv = -1;
805 goto done;
806 }
807
808 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
809 {
810 rv = -1;
811 goto done;
812 }
813
814 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
815 {
816 rv = -1;
817 goto done;
818 }
819 bits_set = rv;
820 }
821 }
822 if (libc_bits)
823 {
824 if (readfds)
825 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
826 vec_len (ldpw->libc_rd_bitmap) *
827 sizeof (clib_bitmap_t));
828 if (writefds)
829 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
830 vec_len (ldpw->libc_wr_bitmap) *
831 sizeof (clib_bitmap_t));
832 if (exceptfds)
833 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
834 vec_len (ldpw->libc_ex_bitmap) *
835 sizeof (clib_bitmap_t));
836
Dave Wallace048b1d62018-01-03 22:24:41 -0500837 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800838 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
839 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
840 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
841 &libc_tspec, sigmask);
842 if (rv > 0)
843 {
844 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
845 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
846 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
847 bits_set += rv;
848 }
849 }
850
851 if (bits_set)
852 {
853 rv = bits_set;
854 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500855 }
856 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800857 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500858 rv = 0;
859
860done:
861 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800862 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800863 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800864 clib_bitmap_zero (ldpw->libc_rd_bitmap);
865 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800866 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800867 clib_bitmap_zero (ldpw->libc_wr_bitmap);
868 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800869 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800870 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500871
Dave Wallace048b1d62018-01-03 22:24:41 -0500872 return rv;
873}
874
875int
876select (int nfds, fd_set * __restrict readfds,
877 fd_set * __restrict writefds,
878 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
879{
880 struct timespec tspec;
881
882 if (timeout)
883 {
884 tspec.tv_sec = timeout->tv_sec;
885 tspec.tv_nsec = timeout->tv_usec * 1000;
886 }
Dave Wallace2a865272018-02-07 21:00:42 -0500887 return ldp_pselect (nfds, readfds, writefds, exceptfds,
888 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500889}
890
891#ifdef __USE_XOPEN2K
892int
893pselect (int nfds, fd_set * __restrict readfds,
894 fd_set * __restrict writefds,
895 fd_set * __restrict exceptfds,
896 const struct timespec *__restrict timeout,
897 const __sigset_t * __restrict sigmask)
898{
Dave Wallace2a865272018-02-07 21:00:42 -0500899 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500900}
901#endif
902
Yu Ping7b74b072019-05-08 00:40:24 +0800903/* If transparent TLS mode is turned on, then ldp will load key and cert.
904 */
905static int
906load_tls_cert (vls_handle_t vlsh)
907{
908 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
909 char inbuf[4096];
910 char *tls_cert;
911 int cert_size;
912 FILE *fp;
913
914 if (env_var_str)
915 {
916 fp = fopen (env_var_str, "r");
917 if (fp == NULL)
918 {
919 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
920 return -1;
921 }
922 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
923 tls_cert = inbuf;
924 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
925 cert_size);
926 fclose (fp);
927 }
928 else
929 {
930 LDBG (0, "ERROR: failed to read LDP environment %s\n",
931 LDP_ENV_TLS_CERT);
932 return -1;
933 }
934 return 0;
935}
936
937static int
938load_tls_key (vls_handle_t vlsh)
939{
940 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
941 char inbuf[4096];
942 char *tls_key;
943 int key_size;
944 FILE *fp;
945
946 if (env_var_str)
947 {
948 fp = fopen (env_var_str, "r");
949 if (fp == NULL)
950 {
951 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
952 return -1;
953 }
954 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
955 tls_key = inbuf;
956 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
957 key_size);
958 fclose (fp);
959 }
960 else
961 {
962 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
963 return -1;
964 }
965 return 0;
966}
967
Dave Wallace048b1d62018-01-03 22:24:41 -0500968int
969socket (int domain, int type, int protocol)
970{
Florin Coras7baeb712019-01-04 17:05:43 -0800971 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500972 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800973 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500974
Dave Wallace2a865272018-02-07 21:00:42 -0500975 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500976 return -1;
977
978 if (((domain == AF_INET) || (domain == AF_INET6)) &&
979 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
980 {
Yu Ping7b74b072019-05-08 00:40:24 +0800981 u8 proto;
982 if (ldp->transparent_tls)
983 {
984 proto = VPPCOM_PROTO_TLS;
985 }
986 else
987 proto = ((sock_type == SOCK_DGRAM) ?
988 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500989
Florin Coras7baeb712019-01-04 17:05:43 -0800990 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
991 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500992
Florin Coras7baeb712019-01-04 17:05:43 -0800993 vlsh = vls_create (proto, is_nonblocking);
994 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500995 {
Florin Coras7baeb712019-01-04 17:05:43 -0800996 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500997 rv = -1;
998 }
999 else
1000 {
Yu Ping7b74b072019-05-08 00:40:24 +08001001 if (ldp->transparent_tls)
1002 {
1003 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
1004 {
1005 return -1;
1006 }
1007 }
Florin Coras7baeb712019-01-04 17:05:43 -08001008 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001009 }
1010 }
1011 else
1012 {
Florin Coras7baeb712019-01-04 17:05:43 -08001013 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001014 rv = libc_socket (domain, type, protocol);
1015 }
1016
Dave Wallace048b1d62018-01-03 22:24:41 -05001017 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001018}
1019
1020/*
1021 * Create two new sockets, of type TYPE in domain DOMAIN and using
1022 * protocol PROTOCOL, which are connected to each other, and put file
1023 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1024 * one will be chosen automatically.
1025 * Returns 0 on success, -1 for errors.
1026 * */
1027int
Dave Wallace048b1d62018-01-03 22:24:41 -05001028socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001029{
Florin Coras7baeb712019-01-04 17:05:43 -08001030 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001031
Dave Wallace2a865272018-02-07 21:00:42 -05001032 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001033 return -1;
1034
1035 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1036 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001037 {
Florin Coras7baeb712019-01-04 17:05:43 -08001038 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001039 errno = ENOSYS;
1040 rv = -1;
1041 }
1042 else
1043 {
Florin Coras7baeb712019-01-04 17:05:43 -08001044 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001045 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001046 }
1047
Dave Wallace048b1d62018-01-03 22:24:41 -05001048 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001049}
1050
1051int
Dave Wallace048b1d62018-01-03 22:24:41 -05001052bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001053{
Florin Coras7baeb712019-01-04 17:05:43 -08001054 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001055 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001056
Dave Wallace2a865272018-02-07 21:00:42 -05001057 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001058 return -1;
1059
Florin Coras7baeb712019-01-04 17:05:43 -08001060 vlsh = ldp_fd_to_vlsh (fd);
1061 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001062 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001063 vppcom_endpt_t ep;
1064
Dave Wallace048b1d62018-01-03 22:24:41 -05001065 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001066 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001067 case AF_INET:
1068 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001069 {
Florin Coras7baeb712019-01-04 17:05:43 -08001070 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1071 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001072 errno = EINVAL;
1073 rv = -1;
1074 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001075 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001076 ep.is_ip4 = VPPCOM_IS_IP4;
1077 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1078 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1079 break;
1080
1081 case AF_INET6:
1082 if (len != sizeof (struct sockaddr_in6))
1083 {
Florin Coras7baeb712019-01-04 17:05:43 -08001084 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1085 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001086 errno = EINVAL;
1087 rv = -1;
1088 goto done;
1089 }
1090 ep.is_ip4 = VPPCOM_IS_IP6;
1091 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1092 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001093 break;
1094
1095 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001096 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1097 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001098 errno = EAFNOSUPPORT;
1099 rv = -1;
1100 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001101 }
Florin Coras7baeb712019-01-04 17:05:43 -08001102 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1103 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001104
Florin Coras7baeb712019-01-04 17:05:43 -08001105 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001106 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001107 {
1108 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001109 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001110 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001111 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001112 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001113 {
Florin Coras7baeb712019-01-04 17:05:43 -08001114 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001115 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001116 }
1117
Dave Wallace048b1d62018-01-03 22:24:41 -05001118done:
Florin Coras7baeb712019-01-04 17:05:43 -08001119 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001120
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001121 return rv;
1122}
1123
1124static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001125ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1126 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001127{
Dave Wallace048b1d62018-01-03 22:24:41 -05001128 int rv = 0;
1129 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001130
Dave Wallace2a865272018-02-07 21:00:42 -05001131 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001132 return -1;
1133
1134 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001135 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001136 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1137 switch (addr->sa_family)
1138 {
1139 case AF_INET:
1140 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1141 if (*len > sizeof (struct sockaddr_in))
1142 *len = sizeof (struct sockaddr_in);
1143 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1144 copy_len = *len - sa_len;
1145 if (copy_len > 0)
1146 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1147 copy_len);
1148 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001149
Dave Wallace048b1d62018-01-03 22:24:41 -05001150 case AF_INET6:
1151 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1152 if (*len > sizeof (struct sockaddr_in6))
1153 *len = sizeof (struct sockaddr_in6);
1154 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1155 copy_len = *len - sa_len;
1156 if (copy_len > 0)
1157 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1158 __in6_u.__u6_addr8, ep->ip, copy_len);
1159 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001160
Dave Wallace048b1d62018-01-03 22:24:41 -05001161 default:
1162 /* Not possible */
1163 rv = -EAFNOSUPPORT;
1164 break;
1165 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001166 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001167 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001168}
1169
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001170int
Dave Wallace048b1d62018-01-03 22:24:41 -05001171getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001172{
Florin Coras7baeb712019-01-04 17:05:43 -08001173 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001174 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001175
Dave Wallace2a865272018-02-07 21:00:42 -05001176 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001177 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001178
Florin Coras7baeb712019-01-04 17:05:43 -08001179 vlsh = ldp_fd_to_vlsh (fd);
1180 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001181 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001182 vppcom_endpt_t ep;
1183 u8 addr_buf[sizeof (struct in6_addr)];
1184 u32 size = sizeof (ep);
1185
1186 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001187
Florin Coras7baeb712019-01-04 17:05:43 -08001188 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001189 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001190 {
1191 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001192 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001193 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001194 else
1195 {
Dave Wallace2a865272018-02-07 21:00:42 -05001196 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001197 if (rv != VPPCOM_OK)
1198 {
1199 errno = -rv;
1200 rv = -1;
1201 }
1202 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001203 }
1204 else
1205 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001206 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001207 }
1208
Dave Wallace048b1d62018-01-03 22:24:41 -05001209 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001210}
1211
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001212int
Dave Wallace048b1d62018-01-03 22:24:41 -05001213connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001214{
Florin Coras7baeb712019-01-04 17:05:43 -08001215 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001216 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001217
Dave Wallace2a865272018-02-07 21:00:42 -05001218 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001219 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001220
Dave Wallace048b1d62018-01-03 22:24:41 -05001221 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001222 {
Florin Coras7baeb712019-01-04 17:05:43 -08001223 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001224 errno = EINVAL;
1225 rv = -1;
1226 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001227 }
1228
Florin Coras7baeb712019-01-04 17:05:43 -08001229 vlsh = ldp_fd_to_vlsh (fd);
1230 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001231 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001232 vppcom_endpt_t ep;
1233
Dave Wallace048b1d62018-01-03 22:24:41 -05001234 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001235 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001236 case AF_INET:
1237 if (len != sizeof (struct sockaddr_in))
1238 {
Florin Coras7baeb712019-01-04 17:05:43 -08001239 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1240 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001241 errno = EINVAL;
1242 rv = -1;
1243 goto done;
1244 }
1245 ep.is_ip4 = VPPCOM_IS_IP4;
1246 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1247 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1248 break;
1249
1250 case AF_INET6:
1251 if (len != sizeof (struct sockaddr_in6))
1252 {
Florin Coras7baeb712019-01-04 17:05:43 -08001253 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1254 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001255 errno = EINVAL;
1256 rv = -1;
1257 goto done;
1258 }
1259 ep.is_ip4 = VPPCOM_IS_IP6;
1260 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1261 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1262 break;
1263
1264 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001265 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1266 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001267 errno = EAFNOSUPPORT;
1268 rv = -1;
1269 goto done;
1270 }
Florin Coras7baeb712019-01-04 17:05:43 -08001271 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1272 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001273
Florin Coras7baeb712019-01-04 17:05:43 -08001274 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001275 if (rv != VPPCOM_OK)
1276 {
1277 errno = -rv;
1278 rv = -1;
1279 }
1280 }
1281 else
1282 {
Florin Coras7baeb712019-01-04 17:05:43 -08001283 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1284 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001285
1286 rv = libc_connect (fd, addr, len);
1287 }
1288
1289done:
Florin Coras7baeb712019-01-04 17:05:43 -08001290 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001291 return rv;
1292}
1293
1294int
1295getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1296{
Florin Coras7baeb712019-01-04 17:05:43 -08001297 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001298 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001299
Dave Wallace2a865272018-02-07 21:00:42 -05001300 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001301 return -1;
1302
Florin Coras7baeb712019-01-04 17:05:43 -08001303 vlsh = ldp_fd_to_vlsh (fd);
1304 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001305 {
1306 vppcom_endpt_t ep;
1307 u8 addr_buf[sizeof (struct in6_addr)];
1308 u32 size = sizeof (ep);
1309
1310 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001311 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001312 if (rv != VPPCOM_OK)
1313 {
1314 errno = -rv;
1315 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001316 }
1317 else
1318 {
Dave Wallace2a865272018-02-07 21:00:42 -05001319 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001320 if (rv != VPPCOM_OK)
1321 {
1322 errno = -rv;
1323 rv = -1;
1324 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001325 }
1326 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001327 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001328 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001329 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001330 }
1331
Dave Wallace048b1d62018-01-03 22:24:41 -05001332 return rv;
1333}
1334
1335ssize_t
1336send (int fd, const void *buf, size_t n, int flags)
1337{
Florin Coras7baeb712019-01-04 17:05:43 -08001338 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001339 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001340
Dave Wallace2a865272018-02-07 21:00:42 -05001341 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001342 return -1;
1343
Florin Coras7baeb712019-01-04 17:05:43 -08001344 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001345 {
Florin Coras7baeb712019-01-04 17:05:43 -08001346 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001347 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001348 {
1349 errno = -size;
1350 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001351 }
1352 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001353 else
1354 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001355 size = libc_send (fd, buf, n, flags);
1356 }
1357
Dave Wallace048b1d62018-01-03 22:24:41 -05001358 return size;
1359}
1360
1361ssize_t
1362sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1363{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001364 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001365 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001366 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001367
Dave Wallace2a865272018-02-07 21:00:42 -05001368 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001369 return -1;
1370
Florin Coras7baeb712019-01-04 17:05:43 -08001371 vlsh = ldp_fd_to_vlsh (out_fd);
1372 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001373 {
1374 int rv;
1375 ssize_t results = 0;
1376 size_t n_bytes_left = len;
1377 size_t bytes_to_read;
1378 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001379 u8 eagain = 0;
1380 u32 flags, flags_len = sizeof (flags);
1381
Florin Coras7baeb712019-01-04 17:05:43 -08001382 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001383 if (PREDICT_FALSE (rv != VPPCOM_OK))
1384 {
Florin Coras7baeb712019-01-04 17:05:43 -08001385 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1386 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001387
Florin Corasdfe4cf42018-11-28 22:13:45 -08001388 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001389 errno = -rv;
1390 size = -1;
1391 goto done;
1392 }
1393
1394 if (offset)
1395 {
1396 off_t off = lseek (in_fd, *offset, SEEK_SET);
1397 if (PREDICT_FALSE (off == -1))
1398 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001399 size = -1;
1400 goto done;
1401 }
1402
1403 ASSERT (off == *offset);
1404 }
1405
1406 do
1407 {
Florin Coras7baeb712019-01-04 17:05:43 -08001408 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 if (size < 0)
1410 {
Florin Coras7baeb712019-01-04 17:05:43 -08001411 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1412 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001413 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001414 errno = -size;
1415 size = -1;
1416 goto done;
1417 }
1418
1419 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001420 if (bytes_to_read == 0)
1421 {
1422 if (flags & O_NONBLOCK)
1423 {
1424 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001425 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001426 goto update_offset;
1427 }
1428 else
1429 continue;
1430 }
1431 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001432 vec_validate (ldpw->io_buffer, bytes_to_read);
1433 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001434 if (nbytes < 0)
1435 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001436 if (results == 0)
1437 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001438 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001439 size = -1;
1440 goto done;
1441 }
1442 goto update_offset;
1443 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001444
Florin Coras7baeb712019-01-04 17:05:43 -08001445 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001446 if (size < 0)
1447 {
1448 if (size == VPPCOM_EAGAIN)
1449 {
1450 if (flags & O_NONBLOCK)
1451 {
1452 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001453 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001454 goto update_offset;
1455 }
1456 else
1457 continue;
1458 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001459 if (results == 0)
1460 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001461 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001462 errno = -size;
1463 size = -1;
1464 goto done;
1465 }
1466 goto update_offset;
1467 }
1468
1469 results += nbytes;
1470 ASSERT (n_bytes_left >= nbytes);
1471 n_bytes_left = n_bytes_left - nbytes;
1472 }
1473 while (n_bytes_left > 0);
1474
1475 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001476 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001477 if (offset)
1478 {
1479 off_t off = lseek (in_fd, *offset, SEEK_SET);
1480 if (PREDICT_FALSE (off == -1))
1481 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001482 size = -1;
1483 goto done;
1484 }
1485
1486 ASSERT (off == *offset);
1487 *offset += results + 1;
1488 }
1489 if (eagain)
1490 {
1491 errno = EAGAIN;
1492 size = -1;
1493 }
1494 else
1495 size = results;
1496 }
1497 else
1498 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001499 size = libc_sendfile (out_fd, in_fd, offset, len);
1500 }
1501
1502done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001503 return size;
1504}
1505
1506ssize_t
1507sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1508{
1509 return sendfile (out_fd, in_fd, offset, len);
1510}
1511
1512ssize_t
1513recv (int fd, void *buf, size_t n, int flags)
1514{
Florin Coras7baeb712019-01-04 17:05:43 -08001515 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001516 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001517
Dave Wallace2a865272018-02-07 21:00:42 -05001518 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001519 return -1;
1520
Florin Coras7baeb712019-01-04 17:05:43 -08001521 vlsh = ldp_fd_to_vlsh (fd);
1522 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001523 {
Florin Coras7baeb712019-01-04 17:05:43 -08001524 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001525 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001526 {
1527 errno = -size;
1528 size = -1;
1529 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001530 }
1531 else
1532 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001533 size = libc_recv (fd, buf, n, flags);
1534 }
1535
Dave Wallace048b1d62018-01-03 22:24:41 -05001536 return size;
1537}
1538
Florin Corasce17f462020-05-22 20:36:29 +00001539static int
1540ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1541 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1542{
1543 vppcom_endpt_t *ep = 0;
1544 vppcom_endpt_t _ep;
1545
1546 if (addr)
1547 {
1548 ep = &_ep;
1549 switch (addr->sa_family)
1550 {
1551 case AF_INET:
1552 ep->is_ip4 = VPPCOM_IS_IP4;
1553 ep->ip =
1554 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1555 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1556 break;
1557
1558 case AF_INET6:
1559 ep->is_ip4 = VPPCOM_IS_IP6;
1560 ep->ip =
1561 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1562 ep->port =
1563 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1564 break;
1565
1566 default:
1567 return EAFNOSUPPORT;
1568 }
1569 }
1570
1571 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1572}
1573
1574static int
1575ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1576 int flags, __SOCKADDR_ARG addr,
1577 socklen_t * __restrict addr_len)
1578{
1579 u8 src_addr[sizeof (struct sockaddr_in6)];
1580 vppcom_endpt_t ep;
1581 ssize_t size;
1582 int rv;
1583
1584 if (addr)
1585 {
1586 ep.ip = src_addr;
1587 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1588
1589 if (size > 0)
1590 {
1591 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1592 if (rv < 0)
1593 size = rv;
1594 }
1595 }
1596 else
1597 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1598
1599 return size;
1600}
1601
Dave Wallace048b1d62018-01-03 22:24:41 -05001602ssize_t
1603sendto (int fd, const void *buf, size_t n, int flags,
1604 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1605{
Florin Coras7baeb712019-01-04 17:05:43 -08001606 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001607 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001608
Dave Wallace2a865272018-02-07 21:00:42 -05001609 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001610 return -1;
1611
Florin Coras7baeb712019-01-04 17:05:43 -08001612 vlsh = ldp_fd_to_vlsh (fd);
1613 if (vlsh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001614 {
Florin Corasce17f462020-05-22 20:36:29 +00001615 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001616 if (size < 0)
1617 {
1618 errno = -size;
1619 size = -1;
1620 }
1621 }
1622 else
1623 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001624 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1625 }
1626
Dave Wallace048b1d62018-01-03 22:24:41 -05001627 return size;
1628}
1629
1630ssize_t
1631recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1632 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1633{
Florin Corasce17f462020-05-22 20:36:29 +00001634 vls_handle_t vlsh;
1635 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001636
Dave Wallace2a865272018-02-07 21:00:42 -05001637 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001638 return -1;
1639
Florin Corasce17f462020-05-22 20:36:29 +00001640 vlsh = ldp_fd_to_vlsh (fd);
1641 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001642 {
Florin Corasce17f462020-05-22 20:36:29 +00001643 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001644 if (size < 0)
1645 {
1646 errno = -size;
1647 size = -1;
1648 }
1649 }
1650 else
1651 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001652 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1653 }
1654
Dave Wallace048b1d62018-01-03 22:24:41 -05001655 return size;
1656}
1657
1658ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001659sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001660{
Florin Coras7baeb712019-01-04 17:05:43 -08001661 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001662 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001663
Dave Wallace2a865272018-02-07 21:00:42 -05001664 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001665 return -1;
1666
Florin Coras7baeb712019-01-04 17:05:43 -08001667 vlsh = ldp_fd_to_vlsh (fd);
1668 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001669 {
Florin Corasce17f462020-05-22 20:36:29 +00001670 struct iovec *iov = msg->msg_iov;
1671 ssize_t total = 0;
1672 int i, rv;
1673
1674 for (i = 0; i < msg->msg_iovlen; ++i)
1675 {
1676 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1677 msg->msg_name, msg->msg_namelen);
1678 if (rv < 0)
1679 break;
1680 else
1681 {
1682 total += rv;
1683 if (rv < iov[i].iov_len)
1684 break;
1685 }
1686 }
1687
1688 if (rv < 0 && total == 0)
1689 {
1690 errno = -rv;
1691 size = -1;
1692 }
1693 else
1694 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001695 }
1696 else
1697 {
Florin Corasce17f462020-05-22 20:36:29 +00001698 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001699 }
1700
Dave Wallace048b1d62018-01-03 22:24:41 -05001701 return size;
1702}
1703
1704#ifdef USE_GNU
1705int
1706sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1707{
1708 ssize_t size;
1709 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001710 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001711
Dave Wallace2a865272018-02-07 21:00:42 -05001712 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001713 return -1;
1714
Florin Coras7baeb712019-01-04 17:05:43 -08001715 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001716 {
1717 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1718 errno = ENOSYS;
1719 size = -1;
1720 }
1721 else
1722 {
1723 func_str = "libc_sendmmsg";
1724
Dave Wallace2a865272018-02-07 21:00:42 -05001725 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001726 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1727 "vmessages %p, vlen %u, flags 0x%x",
1728 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1729
1730 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1731 }
1732
Dave Wallace2a865272018-02-07 21:00:42 -05001733 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001734 {
1735 if (size < 0)
1736 {
1737 int errno_val = errno;
1738 perror (func_str);
1739 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1740 "rv %d, errno = %d", getpid (), fd, fd,
1741 func_str, size, errno_val);
1742 errno = errno_val;
1743 }
1744 else
1745 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1746 getpid (), fd, fd, size, size);
1747 }
1748 return size;
1749}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001750#endif
1751
Dave Wallace048b1d62018-01-03 22:24:41 -05001752ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001753recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001754{
Florin Coras7baeb712019-01-04 17:05:43 -08001755 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001756 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001757
Dave Wallace2a865272018-02-07 21:00:42 -05001758 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001759 return -1;
1760
Florin Coras7baeb712019-01-04 17:05:43 -08001761 vlsh = ldp_fd_to_vlsh (fd);
1762 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001763 {
Florin Corasce17f462020-05-22 20:36:29 +00001764 struct iovec *iov = msg->msg_iov;
1765 ssize_t max_deq, total = 0;
1766 int i, rv;
1767
1768 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1769 if (!max_deq)
1770 return 0;
1771
1772 for (i = 0; i < msg->msg_iovlen; i++)
1773 {
1774 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1775 (i == 0 ? msg->msg_name : NULL),
1776 (i == 0 ? &msg->msg_namelen : NULL));
1777 if (rv <= 0)
1778 break;
1779 else
1780 {
1781 total += rv;
1782 if (rv < iov[i].iov_len)
1783 break;
1784 }
1785 if (total >= max_deq)
1786 break;
1787 }
1788
1789 if (rv < 0 && total == 0)
1790 {
1791 errno = -rv;
1792 size = -1;
1793 }
1794 else
1795 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001796 }
1797 else
1798 {
Florin Corasce17f462020-05-22 20:36:29 +00001799 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001800 }
1801
Dave Wallace048b1d62018-01-03 22:24:41 -05001802 return size;
1803}
1804
1805#ifdef USE_GNU
1806int
1807recvmmsg (int fd, struct mmsghdr *vmessages,
1808 unsigned int vlen, int flags, struct timespec *tmo)
1809{
1810 ssize_t size;
1811 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001812 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001813
Dave Wallace2a865272018-02-07 21:00:42 -05001814 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001815 return -1;
1816
Florin Coras7baeb712019-01-04 17:05:43 -08001817 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001818 {
1819 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1820 errno = ENOSYS;
1821 size = -1;
1822 }
1823 else
1824 {
1825 func_str = "libc_recvmmsg";
1826
Dave Wallace2a865272018-02-07 21:00:42 -05001827 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001828 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1829 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1830 getpid (), fd, fd, func_str, vmessages, vlen,
1831 flags, tmo);
1832
1833 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1834 }
1835
Dave Wallace2a865272018-02-07 21:00:42 -05001836 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001837 {
1838 if (size < 0)
1839 {
1840 int errno_val = errno;
1841 perror (func_str);
1842 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1843 "rv %d, errno = %d", getpid (), fd, fd,
1844 func_str, size, errno_val);
1845 errno = errno_val;
1846 }
1847 else
1848 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1849 getpid (), fd, fd, size, size);
1850 }
1851 return size;
1852}
1853#endif
1854
1855int
1856getsockopt (int fd, int level, int optname,
1857 void *__restrict optval, socklen_t * __restrict optlen)
1858{
Florin Coras7baeb712019-01-04 17:05:43 -08001859 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001860 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001861
Dave Wallace2a865272018-02-07 21:00:42 -05001862 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001863 return -1;
1864
Florin Coras7baeb712019-01-04 17:05:43 -08001865 vlsh = ldp_fd_to_vlsh (fd);
1866 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001867 {
1868 rv = -EOPNOTSUPP;
1869
1870 switch (level)
1871 {
1872 case SOL_TCP:
1873 switch (optname)
1874 {
1875 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001876 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1877 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001878 break;
1879 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001880 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1881 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001882 break;
1883 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001884 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1885 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001886 break;
1887 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001888 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1889 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001890 break;
1891 case TCP_INFO:
1892 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1893 {
Florin Coras7baeb712019-01-04 17:05:43 -08001894 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1895 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001896 memset (optval, 0, *optlen);
1897 rv = VPPCOM_OK;
1898 }
1899 else
1900 rv = -EFAULT;
1901 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001902 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001903 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001904 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001905 rv = 0;
1906 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001907 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001908 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1909 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001910 break;
1911 }
1912 break;
1913 case SOL_IPV6:
1914 switch (optname)
1915 {
1916 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001917 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001918 break;
1919 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001920 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1921 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001922 break;
1923 }
1924 break;
1925 case SOL_SOCKET:
1926 switch (optname)
1927 {
1928 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001929 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001930 break;
1931 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001932 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001933 break;
1934 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001935 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001936 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1937 break;
1938 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001939 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1940 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001941 break;
1942 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001943 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1944 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001945 break;
1946 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001947 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001948 break;
1949 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001950 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001951 break;
1952 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001953 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001954 break;
1955 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001956 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1957 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001958 break;
1959 }
1960 break;
1961 default:
1962 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001963 }
1964
Dave Wallace048b1d62018-01-03 22:24:41 -05001965 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001966 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001967 errno = -rv;
1968 rv = -1;
1969 }
1970 }
1971 else
1972 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001973 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1974 }
1975
Dave Wallace048b1d62018-01-03 22:24:41 -05001976 return rv;
1977}
1978
1979int
1980setsockopt (int fd, int level, int optname,
1981 const void *optval, socklen_t optlen)
1982{
Florin Coras7baeb712019-01-04 17:05:43 -08001983 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001984 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001985
Dave Wallace2a865272018-02-07 21:00:42 -05001986 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001987 return -1;
1988
Florin Coras7baeb712019-01-04 17:05:43 -08001989 vlsh = ldp_fd_to_vlsh (fd);
1990 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001991 {
1992 rv = -EOPNOTSUPP;
1993
1994 switch (level)
1995 {
1996 case SOL_TCP:
1997 switch (optname)
1998 {
1999 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002000 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2001 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002002 break;
2003 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002004 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2005 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002006 break;
2007 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002008 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2009 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002010 break;
2011 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002012 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2013 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002014 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002015 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002016 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002017 /* Ignore */
2018 rv = 0;
2019 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002020 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002021 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2022 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002023 break;
2024 }
2025 break;
2026 case SOL_IPV6:
2027 switch (optname)
2028 {
2029 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002030 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2031 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002032 break;
2033 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002034 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2035 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002036 break;
2037 }
2038 break;
2039 case SOL_SOCKET:
2040 switch (optname)
2041 {
2042 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002043 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2044 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002045 break;
2046 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002047 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2048 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002049 break;
2050 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002051 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2052 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002053 break;
2054 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002055 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2056 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 break;
2058 }
2059 break;
2060 default:
2061 break;
2062 }
2063
2064 if (rv != VPPCOM_OK)
2065 {
2066 errno = -rv;
2067 rv = -1;
2068 }
2069 }
2070 else
2071 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002072 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2073 }
2074
Dave Wallace048b1d62018-01-03 22:24:41 -05002075 return rv;
2076}
2077
2078int
2079listen (int fd, int n)
2080{
Florin Coras7baeb712019-01-04 17:05:43 -08002081 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002082 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002083
Dave Wallace2a865272018-02-07 21:00:42 -05002084 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002085 return -1;
2086
Florin Coras7baeb712019-01-04 17:05:43 -08002087 vlsh = ldp_fd_to_vlsh (fd);
2088 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002089 {
Florin Coras7baeb712019-01-04 17:05:43 -08002090 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002091
Florin Coras7baeb712019-01-04 17:05:43 -08002092 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002093 if (rv != VPPCOM_OK)
2094 {
2095 errno = -rv;
2096 rv = -1;
2097 }
2098 }
2099 else
2100 {
Florin Coras7baeb712019-01-04 17:05:43 -08002101 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002102 rv = libc_listen (fd, n);
2103 }
2104
Florin Coras7baeb712019-01-04 17:05:43 -08002105 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002106 return rv;
2107}
2108
2109static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002110ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2111 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002112{
Florin Coras7baeb712019-01-04 17:05:43 -08002113 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002114 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002115
Dave Wallace2a865272018-02-07 21:00:42 -05002116 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002117 return -1;
2118
Florin Coras7baeb712019-01-04 17:05:43 -08002119 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2120 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 {
2122 vppcom_endpt_t ep;
2123 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002124 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 ep.ip = src_addr;
2126
Florin Coras7baeb712019-01-04 17:05:43 -08002127 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2128 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002129
Florin Coras7baeb712019-01-04 17:05:43 -08002130 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2131 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002132 {
Florin Coras7baeb712019-01-04 17:05:43 -08002133 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002134 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002135 }
2136 else
2137 {
Dave Wallace2a865272018-02-07 21:00:42 -05002138 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002139 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002140 {
Florin Coras7baeb712019-01-04 17:05:43 -08002141 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002142 errno = -rv;
2143 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002144 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002145 else
2146 {
Florin Coras7baeb712019-01-04 17:05:43 -08002147 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002148 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002149 }
2150 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002151 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002152 {
Florin Coras7baeb712019-01-04 17:05:43 -08002153 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2154 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002155
Dave Wallace048b1d62018-01-03 22:24:41 -05002156 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002157 }
2158
Florin Coras7baeb712019-01-04 17:05:43 -08002159 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002160
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002161 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002162}
2163
Dave Wallace048b1d62018-01-03 22:24:41 -05002164int
2165accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2166 int flags)
2167{
Dave Wallace2a865272018-02-07 21:00:42 -05002168 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002169}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002170
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002171int
Dave Wallace048b1d62018-01-03 22:24:41 -05002172accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002173{
Dave Wallace2a865272018-02-07 21:00:42 -05002174 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002175}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002176
Dave Wallace048b1d62018-01-03 22:24:41 -05002177int
2178shutdown (int fd, int how)
2179{
Florin Coras7baeb712019-01-04 17:05:43 -08002180 vls_handle_t vlsh;
2181 int rv = 0, flags;
2182 u32 flags_len = sizeof (flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002183
Dave Wallace2a865272018-02-07 21:00:42 -05002184 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002185 return -1;
2186
Florin Coras7baeb712019-01-04 17:05:43 -08002187 vlsh = ldp_fd_to_vlsh (fd);
2188 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002189 {
Florin Coras7baeb712019-01-04 17:05:43 -08002190 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
Florin Corasa7a1a222018-12-30 17:11:31 -08002191
Florin Coras7baeb712019-01-04 17:05:43 -08002192 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
Florin Corasa7a1a222018-12-30 17:11:31 -08002193 {
Florin Coras7baeb712019-01-04 17:05:43 -08002194 close (fd);
Florin Corasa7a1a222018-12-30 17:11:31 -08002195 return -1;
2196 }
2197
Florin Coras7baeb712019-01-04 17:05:43 -08002198 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2199 {
2200 close (fd);
2201 return -1;
2202 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002203
Florin Coras7baeb712019-01-04 17:05:43 -08002204 if (flags == SHUT_RDWR)
Florin Corasa7a1a222018-12-30 17:11:31 -08002205 rv = close (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002206 }
2207 else
2208 {
Florin Coras7baeb712019-01-04 17:05:43 -08002209 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002210 rv = libc_shutdown (fd, how);
2211 }
2212
Dave Wallace048b1d62018-01-03 22:24:41 -05002213 return rv;
2214}
2215
2216int
2217epoll_create1 (int flags)
2218{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002219 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002220 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002221 int rv;
2222
Dave Wallace2a865272018-02-07 21:00:42 -05002223 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002224 return -1;
2225
hanlina3a48962020-07-13 11:09:15 +08002226 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002227 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002228 /* Make sure workers have been allocated */
2229 if (!ldp->workers)
2230 {
2231 ldp_alloc_workers ();
2232 ldpw = ldp_worker_get_current ();
2233 }
Florin Coras99368312018-08-02 10:45:44 -07002234 rv = libc_epoll_create1 (flags);
2235 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002236 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002237 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002238 return rv;
2239 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002240
Florin Coras7baeb712019-01-04 17:05:43 -08002241 vlsh = vls_epoll_create ();
2242 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002243 {
Florin Coras7baeb712019-01-04 17:05:43 -08002244 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002245 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002246 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002247 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002248 {
Florin Coras7baeb712019-01-04 17:05:43 -08002249 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002250 }
Florin Coras7baeb712019-01-04 17:05:43 -08002251 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002252 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002253}
2254
2255int
Dave Wallace048b1d62018-01-03 22:24:41 -05002256epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002257{
Dave Wallace048b1d62018-01-03 22:24:41 -05002258 return epoll_create1 (0);
2259}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002260
Dave Wallace048b1d62018-01-03 22:24:41 -05002261int
2262epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2263{
Florin Coras7baeb712019-01-04 17:05:43 -08002264 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002265 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002266
Dave Wallace2a865272018-02-07 21:00:42 -05002267 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002268 return -1;
2269
Florin Coras7baeb712019-01-04 17:05:43 -08002270 vep_vlsh = ldp_fd_to_vlsh (epfd);
2271 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002272 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002273 /* The LDP epoll_create1 always creates VCL epfd's.
2274 * The app should never have a kernel base epoll fd unless it
2275 * was acquired outside of the LD_PRELOAD process context.
2276 * In any case, if we get one, punt it to libc_epoll_ctl.
2277 */
Florin Coras7baeb712019-01-04 17:05:43 -08002278 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2279 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002280
2281 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002282 goto done;
2283 }
2284
Florin Coras7baeb712019-01-04 17:05:43 -08002285 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002286
Florin Coras7baeb712019-01-04 17:05:43 -08002287 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2288 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002289
Florin Coras7baeb712019-01-04 17:05:43 -08002290 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002291 {
Florin Coras7baeb712019-01-04 17:05:43 -08002292 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2293 " event %p", epfd, vep_vlsh, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002294
Florin Coras7baeb712019-01-04 17:05:43 -08002295 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002296 if (rv != VPPCOM_OK)
2297 {
2298 errno = -rv;
2299 rv = -1;
2300 }
2301 }
2302 else
2303 {
2304 int libc_epfd;
2305 u32 size = sizeof (epfd);
2306
Florin Coras7baeb712019-01-04 17:05:43 -08002307 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002308 if (!libc_epfd)
2309 {
Florin Coras7baeb712019-01-04 17:05:43 -08002310 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2311 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002312
2313 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2314 if (libc_epfd < 0)
2315 {
2316 rv = libc_epfd;
2317 goto done;
2318 }
2319
Florin Coras7baeb712019-01-04 17:05:43 -08002320 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2321 &size);
Florin Coras99368312018-08-02 10:45:44 -07002322 if (rv < 0)
2323 {
2324 errno = -rv;
2325 rv = -1;
2326 goto done;
2327 }
2328 }
2329 else if (PREDICT_FALSE (libc_epfd < 0))
2330 {
2331 errno = -epfd;
2332 rv = -1;
2333 goto done;
2334 }
2335
Florin Coras7baeb712019-01-04 17:05:43 -08002336 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2337 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002338
2339 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002340 }
2341
2342done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002343 return rv;
2344}
Dave Wallace048b1d62018-01-03 22:24:41 -05002345
2346static inline int
Florin Coras99368312018-08-02 10:45:44 -07002347ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2348 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002349{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002350 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002351 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002352 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002353 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002354
Dave Wallace2a865272018-02-07 21:00:42 -05002355 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002356 return -1;
2357
2358 if (PREDICT_FALSE (!events || (timeout < -1)))
2359 {
2360 errno = EFAULT;
2361 return -1;
2362 }
2363
Florin Corasdfe4cf42018-11-28 22:13:45 -08002364 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002365 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2366
Florin Coras7baeb712019-01-04 17:05:43 -08002367 ep_vlsh = ldp_fd_to_vlsh (epfd);
2368 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002369 {
Florin Coras7baeb712019-01-04 17:05:43 -08002370 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002371 errno = EBADFD;
2372 return -1;
2373 }
2374
Florin Coras4dee8cd2019-01-29 21:28:16 -08002375 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2376 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002377 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002378 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002379
Florin Coras7baeb712019-01-04 17:05:43 -08002380 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002381 if (PREDICT_FALSE (libc_epfd < 0))
2382 {
2383 errno = -libc_epfd;
2384 rv = -1;
2385 goto done;
2386 }
2387
Florin Coras7baeb712019-01-04 17:05:43 -08002388 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2389 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002390 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002391 do
2392 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002393 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002394 {
Florin Coras7baeb712019-01-04 17:05:43 -08002395 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002396 if (rv > 0)
2397 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002398 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002399 goto done;
2400 }
2401 else if (rv < 0)
2402 {
2403 errno = -rv;
2404 rv = -1;
2405 goto done;
2406 }
2407 }
2408 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002409 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002410
2411 if (libc_epfd > 0)
2412 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002413 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002414 if (rv != 0)
2415 goto done;
2416 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002417 }
Florin Coras72f77822019-01-22 19:05:52 -08002418 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002419
2420done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002421 return rv;
2422}
2423
hanlin4266d4d2020-05-19 17:34:17 +08002424static inline int
2425ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2426 int maxevents, int timeout, const sigset_t * sigmask)
2427{
hanlina3a48962020-07-13 11:09:15 +08002428 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002429 int libc_epfd, rv = 0, num_ev;
2430 vls_handle_t ep_vlsh;
2431
2432 if ((errno = -ldp_init ()))
2433 return -1;
2434
2435 if (PREDICT_FALSE (!events || (timeout < -1)))
2436 {
2437 errno = EFAULT;
2438 return -1;
2439 }
2440
Florin Corasff40d8f2020-08-11 22:05:28 -07002441 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2442 * one thread but it is now used on another */
2443 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2444 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002445
2446 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002447 if (epfd == ldpw->vcl_mq_epfd)
2448 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2449
2450 ep_vlsh = ldp_fd_to_vlsh (epfd);
2451 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2452 {
2453 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2454 errno = EBADFD;
2455 return -1;
2456 }
2457
2458 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2459 if (PREDICT_FALSE (!libc_epfd))
2460 {
2461 u32 size = sizeof (epfd);
2462
2463 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2464 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2465 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2466 if (libc_epfd < 0)
2467 {
2468 rv = libc_epfd;
2469 goto done;
2470 }
2471
2472 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2473 if (rv < 0)
2474 {
2475 errno = -rv;
2476 rv = -1;
2477 goto done;
2478 }
2479 }
2480 if (PREDICT_FALSE (libc_epfd <= 0))
2481 {
2482 errno = -libc_epfd;
2483 rv = -1;
2484 goto done;
2485 }
2486
2487 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2488 {
2489 struct epoll_event e = { 0 };
2490 e.events = EPOLLIN;
2491 e.data.fd = ldpw->vcl_mq_epfd;
2492 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2493 0)
2494 {
2495 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2496 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2497 rv = -1;
2498 goto done;
2499 }
2500 ldpw->mq_epfd_added = 1;
2501 }
2502
2503 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
2504 if (rv > 0)
2505 goto done;
hanlina3a48962020-07-13 11:09:15 +08002506 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002507 {
2508 errno = -rv;
2509 rv = -1;
2510 goto done;
2511 }
2512
2513 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2514 if (rv <= 0)
2515 goto done;
2516 for (int i = 0; i < rv; i++)
2517 {
2518 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2519 {
2520 /* We should remove mq epoll fd from events. */
2521 rv--;
2522 if (i != rv)
2523 {
2524 events[i].events = events[rv].events;
2525 events[i].data.u64 = events[rv].data.u64;
2526 }
2527 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2528 if (PREDICT_TRUE (num_ev > 0))
2529 rv += num_ev;
2530 break;
2531 }
2532 }
2533
2534done:
2535 return rv;
2536}
2537
Dave Wallace048b1d62018-01-03 22:24:41 -05002538int
2539epoll_pwait (int epfd, struct epoll_event *events,
2540 int maxevents, int timeout, const sigset_t * sigmask)
2541{
hanlin4266d4d2020-05-19 17:34:17 +08002542 if (vls_use_eventfd ())
2543 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2544 sigmask);
2545 else
2546 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002547}
2548
2549int
2550epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2551{
hanlin4266d4d2020-05-19 17:34:17 +08002552 if (vls_use_eventfd ())
2553 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2554 else
2555 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002556}
2557
2558int
2559poll (struct pollfd *fds, nfds_t nfds, int timeout)
2560{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002561 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002562 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002563 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002564 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002565 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002566
Florin Coras05ecfcc2018-12-12 18:19:39 -08002567 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002568
Florin Coras4dee8cd2019-01-29 21:28:16 -08002569 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2570 clib_time_init (&ldpw->clib_time);
2571
2572 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2573 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002574
Dave Wallace048b1d62018-01-03 22:24:41 -05002575 for (i = 0; i < nfds; i++)
2576 {
Florin Coras6917b942018-11-13 22:44:54 -08002577 if (fds[i].fd < 0)
2578 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002579
Florin Coras7baeb712019-01-04 17:05:43 -08002580 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2581 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002582 {
2583 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002584 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002585 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002586 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002587 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002588#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002589 if (fds[i].events & POLLRDNORM)
2590 vp->events |= POLLIN;
2591 if (fds[i].events & POLLWRNORM)
2592 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002593#endif
Florin Coras6917b942018-11-13 22:44:54 -08002594 vp->revents = fds[i].revents;
2595 }
2596 else
2597 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002598 vec_add1 (ldpw->libc_poll, fds[i]);
2599 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002600 }
2601 }
2602
Dave Wallace048b1d62018-01-03 22:24:41 -05002603 do
2604 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002605 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002606 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002607 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002608 if (rv < 0)
2609 {
2610 errno = -rv;
2611 rv = -1;
2612 goto done;
2613 }
2614 else
2615 n_revents += rv;
2616 }
2617
Florin Corasdfe4cf42018-11-28 22:13:45 -08002618 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002619 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002620 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002621 if (rv < 0)
2622 goto done;
2623 else
2624 n_revents += rv;
2625 }
2626
2627 if (n_revents)
2628 {
2629 rv = n_revents;
2630 goto done;
2631 }
2632 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002633 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002634 rv = 0;
2635
2636done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002637 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002638 {
2639 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002640 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002641#ifdef __USE_XOPEN2K
2642 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2643 (fds[vp->fds_ndx].events & POLLRDNORM))
2644 fds[vp->fds_ndx].revents |= POLLRDNORM;
2645 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2646 (fds[vp->fds_ndx].events & POLLWRNORM))
2647 fds[vp->fds_ndx].revents |= POLLWRNORM;
2648#endif
2649 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002650 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002651
Florin Corasdfe4cf42018-11-28 22:13:45 -08002652 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002653 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002654 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002655 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002656 vec_reset_length (ldpw->libc_poll_idxs);
2657 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002658
Dave Wallace048b1d62018-01-03 22:24:41 -05002659 return rv;
2660}
2661
2662#ifdef USE_GNU
2663int
2664ppoll (struct pollfd *fds, nfds_t nfds,
2665 const struct timespec *timeout, const sigset_t * sigmask)
2666{
Dave Wallace2a865272018-02-07 21:00:42 -05002667 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002668 return -1;
2669
2670 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2671 errno = ENOSYS;
2672
2673
2674 return -1;
2675}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002676#endif
2677
Dave Wallace2a865272018-02-07 21:00:42 -05002678void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002679
Dave Wallace2a865272018-02-07 21:00:42 -05002680void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002681
Dave Wallace048b1d62018-01-03 22:24:41 -05002682/*
2683 * This function is called when the library is loaded
2684 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002685void
Dave Wallace2a865272018-02-07 21:00:42 -05002686ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002687{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002688 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002689 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002690 {
2691 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2692 getpid ());
2693 _exit (1);
2694 }
Dave Wallace69d01192018-02-22 16:22:09 -05002695 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002696 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002697}
2698
2699/*
2700 * This function is called when the library is unloaded
2701 */
2702void
Dave Wallace2a865272018-02-07 21:00:42 -05002703ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002704{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002705 /*
2706 swrap_destructor ();
2707 if (ldp->init)
2708 ldp->init = 0;
2709 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002710
2711 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002712 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002713 */
Dave Wallace69d01192018-02-22 16:22:09 -05002714 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002715 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2716 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002717}
2718
2719
2720/*
2721 * fd.io coding-style-patch-verification: ON
2722 *
2723 * Local Variables:
2724 * eval: (c-set-style "gnu")
2725 * End:
2726 */