blob: 4d62a7318d4c6cb91b97562c9cad09f120725a6a [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;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800103
104 /** vcl needs next epoll_create to go to libc_epoll */
105 u8 vcl_needs_real_epoll;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800106
107 /**
108 * crypto state used only for testing
109 */
110 u8 transparent_tls;
111 u32 ckpair_index;
Dave Wallace2a865272018-02-07 21:00:42 -0500112} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800113
Dave Wallace2a865272018-02-07 21:00:42 -0500114#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700115
Florin Coras99368312018-08-02 10:45:44 -0700116#define LDBG(_lvl, _fmt, _args...) \
117 if (ldp->debug > _lvl) \
hanlin9f3f18f2019-12-30 16:25:20 +0800118 { \
119 int errno_saved = errno; \
Florin Coras585c86a2020-10-16 17:57:36 -0700120 fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \
hanlin9f3f18f2019-12-30 16:25:20 +0800121 errno = errno_saved; \
122 }
Florin Coras99368312018-08-02 10:45:44 -0700123
Dave Wallace2a865272018-02-07 21:00:42 -0500124static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800125 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
126 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500127 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800128 .transparent_tls = 0,
Florin Corasa5a9efd2021-01-05 17:03:29 -0800129 .ckpair_index = ~0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500130};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700131
Dave Wallace2a865272018-02-07 21:00:42 -0500132static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700133
Florin Corasdfe4cf42018-11-28 22:13:45 -0800134static inline ldp_worker_ctx_t *
135ldp_worker_get_current (void)
136{
137 return (ldp->workers + vppcom_worker_index ());
138}
139
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700140/*
141 * RETURN: 0 on success or -1 on error.
142 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500143static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500144ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700145{
Benoît Ganne747b3d82019-08-21 18:27:23 +0200146 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
147 "ldp-%d-%s", getpid (), app_name);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700148}
149
Dave Wallace048b1d62018-01-03 22:24:41 -0500150static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500151ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700152{
Dave Wallace2a865272018-02-07 21:00:42 -0500153 if (ldp->app_name[0] == '\0')
154 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500155
Dave Wallace2a865272018-02-07 21:00:42 -0500156 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700157}
158
Dave Wallace048b1d62018-01-03 22:24:41 -0500159static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800160ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500161{
Florin Coras7baeb712019-01-04 17:05:43 -0800162 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500163}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700164
Florin Coras7baeb712019-01-04 17:05:43 -0800165static inline vls_handle_t
166ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500167{
Florin Coras7baeb712019-01-04 17:05:43 -0800168 if (fd < ldp->vlsh_bit_val)
169 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700170
Florin Coras7baeb712019-01-04 17:05:43 -0800171 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500172}
173
Florin Coras2d9b4272019-03-11 10:14:37 -0700174static void
175ldp_alloc_workers (void)
176{
177 if (ldp->workers)
178 return;
179 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
180}
181
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700182static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500183ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700184{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800185 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700186 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700187
Florin Coras99368312018-08-02 10:45:44 -0700188 if (PREDICT_TRUE (ldp->init))
189 return 0;
190
191 ldp->init = 1;
192 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800193 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700194 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700195 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800196 ldp->vcl_needs_real_epoll = 0;
197 if (rv == VPPCOM_EEXIST)
198 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800199 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
200 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700201 ldp->init = 0;
202 return rv;
203 }
204 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700205 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800206 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700207
208 char *env_var_str = getenv (LDP_ENV_DEBUG);
209 if (env_var_str)
210 {
211 u32 tmp;
212 if (sscanf (env_var_str, "%u", &tmp) != 1)
213 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
214 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
215 env_var_str);
216 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700217 {
Florin Coras99368312018-08-02 10:45:44 -0700218 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800219 LDBG (0, "configured LDP debug level (%u) from env var "
220 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700221 }
222 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500223
Florin Coras99368312018-08-02 10:45:44 -0700224 env_var_str = getenv (LDP_ENV_APP_NAME);
225 if (env_var_str)
226 {
227 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800228 LDBG (0, "configured LDP app name (%s) from the env var "
229 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700230 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500231
Florin Coras99368312018-08-02 10:45:44 -0700232 env_var_str = getenv (LDP_ENV_SID_BIT);
233 if (env_var_str)
234 {
235 u32 sb;
236 if (sscanf (env_var_str, "%u", &sb) != 1)
237 {
Florin Coras294afe22019-01-07 17:49:17 -0800238 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
239 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
240 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700241 }
242 else if (sb < LDP_SID_BIT_MIN)
243 {
Florin Coras7baeb712019-01-04 17:05:43 -0800244 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
245 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500246
Florin Coras294afe22019-01-07 17:49:17 -0800247 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
248 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
249 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
250 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700251 }
252 else if (sb > LDP_SID_BIT_MAX)
253 {
Florin Coras7baeb712019-01-04 17:05:43 -0800254 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
255 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500256
Florin Coras294afe22019-01-07 17:49:17 -0800257 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
258 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
259 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
260 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500261 }
262 else
263 {
Florin Coras7baeb712019-01-04 17:05:43 -0800264 ldp->vlsh_bit_val = (1 << sb);
265 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700266
Florin Coras05ecfcc2018-12-12 18:19:39 -0800267 LDBG (0, "configured LDP sid bit (%u) from "
268 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800269 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500270 }
Florin Coras294afe22019-01-07 17:49:17 -0800271
272 /* Make sure there are enough bits in the fd set for vcl sessions */
273 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
274 {
275 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
276 ldp->vlsh_bit_val, FD_SETSIZE / 2);
277 ldp->init = 0;
278 return -1;
279 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500280 }
Yu Ping7b74b072019-05-08 00:40:24 +0800281 env_var_str = getenv (LDP_ENV_TLS_TRANS);
282 if (env_var_str)
283 {
284 ldp->transparent_tls = 1;
285 }
Florin Coras99368312018-08-02 10:45:44 -0700286
Florin Coras4dee8cd2019-01-29 21:28:16 -0800287 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100288 pool_foreach (ldpw, ldp->workers) {
Florin Coras4dee8cd2019-01-29 21:28:16 -0800289 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100290 }
Florin Coras4dee8cd2019-01-29 21:28:16 -0800291 /* *INDENT-ON* */
292
Florin Coras05ecfcc2018-12-12 18:19:39 -0800293 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700294
295 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500296}
297
298int
299close (int fd)
300{
Florin Coras7baeb712019-01-04 17:05:43 -0800301 vls_handle_t vlsh;
302 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500303
Dave Wallace2a865272018-02-07 21:00:42 -0500304 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500305 return -1;
306
Florin Coras7baeb712019-01-04 17:05:43 -0800307 vlsh = ldp_fd_to_vlsh (fd);
308 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500309 {
Florin Coras7baeb712019-01-04 17:05:43 -0800310 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500311 if (epfd > 0)
312 {
Florin Coras7baeb712019-01-04 17:05:43 -0800313 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500314
315 rv = libc_close (epfd);
316 if (rv < 0)
317 {
318 u32 size = sizeof (epfd);
319 epfd = 0;
320
Florin Coras7baeb712019-01-04 17:05:43 -0800321 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500322 }
323 }
324 else if (PREDICT_FALSE (epfd < 0))
325 {
326 errno = -epfd;
327 rv = -1;
328 goto done;
329 }
330
Florin Coras7baeb712019-01-04 17:05:43 -0800331 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500332
Florin Coras7baeb712019-01-04 17:05:43 -0800333 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500334 if (rv != VPPCOM_OK)
335 {
336 errno = -rv;
337 rv = -1;
338 }
339 }
340 else
341 {
Florin Coras7baeb712019-01-04 17:05:43 -0800342 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500343 rv = libc_close (fd);
344 }
345
346done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500347 return rv;
348}
349
350ssize_t
351read (int fd, void *buf, size_t nbytes)
352{
Florin Coras7baeb712019-01-04 17:05:43 -0800353 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500354 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500355
Dave Wallace2a865272018-02-07 21:00:42 -0500356 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500357 return -1;
358
Florin Coras7baeb712019-01-04 17:05:43 -0800359 vlsh = ldp_fd_to_vlsh (fd);
360 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500361 {
Florin Coras7baeb712019-01-04 17:05:43 -0800362 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500363 if (size < 0)
364 {
365 errno = -size;
366 size = -1;
367 }
368 }
369 else
370 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500371 size = libc_read (fd, buf, nbytes);
372 }
373
Dave Wallace048b1d62018-01-03 22:24:41 -0500374 return size;
375}
376
377ssize_t
378readv (int fd, const struct iovec * iov, int iovcnt)
379{
Dave Wallace8aaba562018-01-18 17:21:19 -0500380 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800381 vls_handle_t vlsh;
382 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500383
Dave Wallace2a865272018-02-07 21:00:42 -0500384 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500385 return -1;
386
Florin Coras7baeb712019-01-04 17:05:43 -0800387 vlsh = ldp_fd_to_vlsh (fd);
388 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500389 {
Florin Coras067f9542020-02-14 05:33:46 +0000390 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500391 {
Florin Coras067f9542020-02-14 05:33:46 +0000392 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
393 if (rv <= 0)
394 break;
395 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500396 {
Florin Coras067f9542020-02-14 05:33:46 +0000397 total += rv;
398 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500399 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700400 }
401 }
Florin Coras067f9542020-02-14 05:33:46 +0000402 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700403 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500404 errno = -rv;
405 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700406 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500407 else
408 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700409 }
410 else
411 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500412 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700413 }
414
Dave Wallace048b1d62018-01-03 22:24:41 -0500415 return size;
416}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700417
Dave Wallace048b1d62018-01-03 22:24:41 -0500418ssize_t
419write (int fd, const void *buf, size_t nbytes)
420{
Florin Coras7baeb712019-01-04 17:05:43 -0800421 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500422 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500423
Dave Wallace2a865272018-02-07 21:00:42 -0500424 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500425 return -1;
426
Florin Coras7baeb712019-01-04 17:05:43 -0800427 vlsh = ldp_fd_to_vlsh (fd);
428 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500429 {
Florin Coras7baeb712019-01-04 17:05:43 -0800430 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500431 if (size < 0)
432 {
433 errno = -size;
434 size = -1;
435 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700436 }
437 else
438 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500439 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700440 }
441
Dave Wallace048b1d62018-01-03 22:24:41 -0500442 return size;
443}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700444
Dave Wallace048b1d62018-01-03 22:24:41 -0500445ssize_t
446writev (int fd, const struct iovec * iov, int iovcnt)
447{
Dave Wallace048b1d62018-01-03 22:24:41 -0500448 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800449 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500450 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500451
Dave Wallace2a865272018-02-07 21:00:42 -0500452 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500453 return -1;
454
Florin Coras7baeb712019-01-04 17:05:43 -0800455 vlsh = ldp_fd_to_vlsh (fd);
456 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500457 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000458 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500459 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000460 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
461 if (rv < 0)
462 break;
463 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500464 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000465 total += rv;
466 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500467 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500468 }
469 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500470
Florin Coraseda1b8c2020-03-23 16:00:35 +0000471 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500472 {
473 errno = -rv;
474 size = -1;
475 }
476 else
477 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700478 }
479 else
480 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500481 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700482 }
483
Dave Wallace048b1d62018-01-03 22:24:41 -0500484 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700485}
486
Florin Coras0ab36f52020-05-26 19:45:45 +0000487static int
488fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700489{
Florin Coras7baeb712019-01-04 17:05:43 -0800490 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700491 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800492
493 vlsh = ldp_fd_to_vlsh (fd);
494 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
495 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700496 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500497 int flags = va_arg (ap, int);
498 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700499
Dave Wallace048b1d62018-01-03 22:24:41 -0500500 size = sizeof (flags);
501 rv = -EOPNOTSUPP;
502 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700503 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500504 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800505 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500506 break;
507
508 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800509 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500510 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800511 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700512 break;
Florin Coras173bae32018-11-16 18:56:28 -0800513 case F_SETFD:
514 /* TODO handle this */
515 LDBG (0, "F_SETFD ignored flags %u", flags);
516 rv = 0;
517 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700518 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500519 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700520 break;
521 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500522 if (rv < 0)
523 {
524 errno = -rv;
525 rv = -1;
526 }
527 }
528 else
529 {
Carl Smithe16707b2019-11-13 14:37:39 +1300530#ifdef HAVE_FCNTL64
531 rv = libc_vfcntl64 (fd, cmd, ap);
532#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500533 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300534#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700535 }
536
Florin Coras0ab36f52020-05-26 19:45:45 +0000537 return rv;
538}
539
540int
541fcntl (int fd, int cmd, ...)
542{
543 va_list ap;
544 int rv;
545
546 if ((errno = -ldp_init ()))
547 return -1;
548
549 va_start (ap, cmd);
550 rv = fcntl_internal (fd, cmd, ap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500551 va_end (ap);
552
Dave Wallace048b1d62018-01-03 22:24:41 -0500553 return rv;
554}
555
556int
Florin Corasd7586d52020-04-29 02:19:51 +0000557fcntl64 (int fd, int cmd, ...)
558{
559 va_list ap;
560 int rv;
561
Florin Coras0ab36f52020-05-26 19:45:45 +0000562 if ((errno = -ldp_init ()))
563 return -1;
564
Florin Corasd7586d52020-04-29 02:19:51 +0000565 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000566 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000567 va_end (ap);
568 return rv;
569}
570
571int
Dave Wallace048b1d62018-01-03 22:24:41 -0500572ioctl (int fd, unsigned long int cmd, ...)
573{
Florin Coras7baeb712019-01-04 17:05:43 -0800574 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500575 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800576 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500577
Dave Wallace2a865272018-02-07 21:00:42 -0500578 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500579 return -1;
580
581 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500582
Florin Coras7baeb712019-01-04 17:05:43 -0800583 vlsh = ldp_fd_to_vlsh (fd);
584 if (vlsh != VLS_INVALID_HANDLE)
585 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500586 switch (cmd)
587 {
588 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800589 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500590 break;
591
592 case FIONBIO:
593 {
594 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
595 u32 size = sizeof (flags);
596
597 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
598 * non-blocking, the flags should be read here and merged
599 * with O_NONBLOCK.
600 */
Florin Coras7baeb712019-01-04 17:05:43 -0800601 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500602 }
603 break;
604
605 default:
606 rv = -EOPNOTSUPP;
607 break;
608 }
609 if (rv < 0)
610 {
611 errno = -rv;
612 rv = -1;
613 }
614 }
615 else
616 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500617 rv = libc_vioctl (fd, cmd, ap);
618 }
619
Dave Wallace048b1d62018-01-03 22:24:41 -0500620 va_end (ap);
621 return rv;
622}
623
Florin Coras294afe22019-01-07 17:49:17 -0800624always_inline void
625ldp_select_init_maps (fd_set * __restrict original,
626 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
627 clib_bitmap_t ** vclb, int nfds, u32 minbits,
628 u32 n_bytes, uword * si_bits, uword * libc_bits)
629{
630 uword si_bits_set, libc_bits_set;
631 vls_handle_t vlsh;
632 int fd;
633
634 clib_bitmap_validate (*vclb, minbits);
635 clib_bitmap_validate (*libcb, minbits);
636 clib_bitmap_validate (*resultb, minbits);
637 clib_memcpy_fast (*resultb, original, n_bytes);
638 memset (original, 0, n_bytes);
639
640 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100641 clib_bitmap_foreach (fd, *resultb) {
Florin Coras294afe22019-01-07 17:49:17 -0800642 if (fd > nfds)
643 break;
644 vlsh = ldp_fd_to_vlsh (fd);
645 if (vlsh == VLS_INVALID_HANDLE)
646 clib_bitmap_set_no_check (*libcb, fd, 1);
647 else
Florin Corascbce80a2020-04-20 01:32:38 +0000648 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100649 }
Florin Coras294afe22019-01-07 17:49:17 -0800650 /* *INDENT-ON* */
651
652 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
653 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000654 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800655
656 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
657 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
658}
659
660always_inline int
661ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
662{
663 vls_handle_t vlsh;
664 uword si;
665 int fd;
666
667 if (!libcb)
668 return 0;
669
670 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100671 clib_bitmap_foreach (si, vclb) {
Florin Coras294afe22019-01-07 17:49:17 -0800672 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000673 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800674 fd = ldp_vlsh_to_fd (vlsh);
675 if (PREDICT_FALSE (fd < 0))
676 {
677 errno = EBADFD;
678 return -1;
679 }
680 FD_SET (fd, libcb);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100681 }
Florin Coras294afe22019-01-07 17:49:17 -0800682 /* *INDENT-ON* */
683
684 return 0;
685}
686
687always_inline void
688ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
689{
690 uword fd;
691
Florin Coras78b5fa62019-02-21 20:04:15 -0800692 if (!libcb)
693 return;
694
Florin Coras294afe22019-01-07 17:49:17 -0800695 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100696 clib_bitmap_foreach (fd, result)
Florin Coras294afe22019-01-07 17:49:17 -0800697 FD_SET ((int)fd, libcb);
Florin Coras294afe22019-01-07 17:49:17 -0800698 /* *INDENT-ON* */
699}
700
Dave Wallace048b1d62018-01-03 22:24:41 -0500701int
Dave Wallace2a865272018-02-07 21:00:42 -0500702ldp_pselect (int nfds, fd_set * __restrict readfds,
703 fd_set * __restrict writefds,
704 fd_set * __restrict exceptfds,
705 const struct timespec *__restrict timeout,
706 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500707{
Florin Coras294afe22019-01-07 17:49:17 -0800708 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800709 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800710 struct timespec libc_tspec = { 0 };
711 f64 time_out, vcl_timeout = 0;
712 uword si_bits, libc_bits;
713 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500714
715 if (nfds < 0)
716 {
717 errno = EINVAL;
718 return -1;
719 }
720
Florin Coras4dee8cd2019-01-29 21:28:16 -0800721 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
722 clib_time_init (&ldpw->clib_time);
723
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500724 if (timeout)
725 {
726 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800727 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500728
729 /* select as fine grained sleep */
730 if (!nfds)
731 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800732 time_out += clib_time_now (&ldpw->clib_time);
733 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500734 ;
735 return 0;
736 }
737 }
738 else if (!nfds)
739 {
740 errno = EINVAL;
741 return -1;
742 }
743 else
744 time_out = -1;
745
Florin Coras7baeb712019-01-04 17:05:43 -0800746 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500747 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500748 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
749 timeout, sigmask);
750 goto done;
751 }
752
Florin Coras294afe22019-01-07 17:49:17 -0800753 si_bits = libc_bits = 0;
754 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800755
Dave Wallace048b1d62018-01-03 22:24:41 -0500756 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800757 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
758 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
759 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500760 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800761 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
762 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
763 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500764 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800765 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
766 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
767 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500768
Florin Coras294afe22019-01-07 17:49:17 -0800769 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500770 {
771 errno = EINVAL;
772 rv = -1;
773 goto done;
774 }
775
Florin Coras78b5fa62019-02-21 20:04:15 -0800776 if (!si_bits)
777 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800778
Dave Wallace048b1d62018-01-03 22:24:41 -0500779 do
780 {
Florin Coras294afe22019-01-07 17:49:17 -0800781 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500783 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800784 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000785 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500786 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500787 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800788 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000789 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500790 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500791 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800792 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000793 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500794 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800795
Florin Coras0ef8ef22019-01-18 08:37:13 -0800796 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
797 writefds ? ldpw->wr_bitmap : NULL,
798 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800799 if (rv < 0)
800 {
801 errno = -rv;
802 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000803 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800804 }
805 else if (rv > 0)
806 {
807 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
808 {
809 rv = -1;
810 goto done;
811 }
812
813 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
814 {
815 rv = -1;
816 goto done;
817 }
818
819 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
820 {
821 rv = -1;
822 goto done;
823 }
824 bits_set = rv;
825 }
826 }
827 if (libc_bits)
828 {
829 if (readfds)
830 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
831 vec_len (ldpw->libc_rd_bitmap) *
832 sizeof (clib_bitmap_t));
833 if (writefds)
834 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
835 vec_len (ldpw->libc_wr_bitmap) *
836 sizeof (clib_bitmap_t));
837 if (exceptfds)
838 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
839 vec_len (ldpw->libc_ex_bitmap) *
840 sizeof (clib_bitmap_t));
841
Dave Wallace048b1d62018-01-03 22:24:41 -0500842 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800843 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
844 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
845 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
846 &libc_tspec, sigmask);
847 if (rv > 0)
848 {
849 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
850 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
851 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
852 bits_set += rv;
853 }
854 }
855
856 if (bits_set)
857 {
858 rv = bits_set;
859 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500860 }
861 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800862 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500863 rv = 0;
864
865done:
866 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800867 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800868 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800869 clib_bitmap_zero (ldpw->libc_rd_bitmap);
870 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800871 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800872 clib_bitmap_zero (ldpw->libc_wr_bitmap);
873 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800874 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800875 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500876
Dave Wallace048b1d62018-01-03 22:24:41 -0500877 return rv;
878}
879
880int
881select (int nfds, fd_set * __restrict readfds,
882 fd_set * __restrict writefds,
883 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
884{
885 struct timespec tspec;
886
887 if (timeout)
888 {
889 tspec.tv_sec = timeout->tv_sec;
890 tspec.tv_nsec = timeout->tv_usec * 1000;
891 }
Dave Wallace2a865272018-02-07 21:00:42 -0500892 return ldp_pselect (nfds, readfds, writefds, exceptfds,
893 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500894}
895
896#ifdef __USE_XOPEN2K
897int
898pselect (int nfds, fd_set * __restrict readfds,
899 fd_set * __restrict writefds,
900 fd_set * __restrict exceptfds,
901 const struct timespec *__restrict timeout,
902 const __sigset_t * __restrict sigmask)
903{
Dave Wallace2a865272018-02-07 21:00:42 -0500904 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500905}
906#endif
907
Yu Ping7b74b072019-05-08 00:40:24 +0800908/* If transparent TLS mode is turned on, then ldp will load key and cert.
909 */
910static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800911load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800912{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800913 char *cert_str = getenv (LDP_ENV_TLS_CERT);
914 char *key_str = getenv (LDP_ENV_TLS_KEY);
915 char cert_buf[4096], key_buf[4096];
916 int cert_size, key_size;
917 vppcom_cert_key_pair_t crypto;
918 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800919 FILE *fp;
920
Florin Corasa5a9efd2021-01-05 17:03:29 -0800921 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800922 {
923 LDBG (0, "ERROR: failed to read LDP environment %s\n",
924 LDP_ENV_TLS_CERT);
925 return -1;
926 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800927
928 fp = fopen (cert_str, "r");
929 if (fp == NULL)
930 {
931 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
932 return -1;
933 }
934 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
935 fclose (fp);
936
937 fp = fopen (key_str, "r");
938 if (fp == NULL)
939 {
940 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
941 return -1;
942 }
943 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
944 fclose (fp);
945
946 crypto.cert = cert_buf;
947 crypto.key = key_buf;
948 crypto.cert_len = cert_size;
949 crypto.key_len = key_size;
950 ckp_index = vppcom_add_cert_key_pair (&crypto);
951 if (ckp_index < 0)
952 {
953 LDBG (0, "ERROR: failed to add cert key pair\n");
954 return -1;
955 }
956
957 ldp->ckpair_index = ckp_index;
958
Yu Ping7b74b072019-05-08 00:40:24 +0800959 return 0;
960}
961
962static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800963assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800964{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800965 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800966
Florin Corasa5a9efd2021-01-05 17:03:29 -0800967 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
968 return -1;
969
970 ckp_len = sizeof (ldp->ckpair_index);
971 return vppcom_session_attr (vlsh_to_session_index (vlsh),
972 VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index,
973 &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800974}
975
Dave Wallace048b1d62018-01-03 22:24:41 -0500976int
977socket (int domain, int type, int protocol)
978{
Florin Coras7baeb712019-01-04 17:05:43 -0800979 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500980 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800981 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500982
Dave Wallace2a865272018-02-07 21:00:42 -0500983 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500984 return -1;
985
986 if (((domain == AF_INET) || (domain == AF_INET6)) &&
987 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
988 {
Yu Ping7b74b072019-05-08 00:40:24 +0800989 u8 proto;
990 if (ldp->transparent_tls)
991 {
992 proto = VPPCOM_PROTO_TLS;
993 }
994 else
995 proto = ((sock_type == SOCK_DGRAM) ?
996 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500997
Florin Coras7baeb712019-01-04 17:05:43 -0800998 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
999 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -05001000
Florin Coras7baeb712019-01-04 17:05:43 -08001001 vlsh = vls_create (proto, is_nonblocking);
1002 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001003 {
Florin Coras7baeb712019-01-04 17:05:43 -08001004 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001005 rv = -1;
1006 }
1007 else
1008 {
Yu Ping7b74b072019-05-08 00:40:24 +08001009 if (ldp->transparent_tls)
1010 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001011 if (assign_cert_key_pair (vlsh) < 0)
1012 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001013 }
Florin Coras7baeb712019-01-04 17:05:43 -08001014 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001015 }
1016 }
1017 else
1018 {
Florin Coras7baeb712019-01-04 17:05:43 -08001019 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001020 rv = libc_socket (domain, type, protocol);
1021 }
1022
Dave Wallace048b1d62018-01-03 22:24:41 -05001023 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001024}
1025
1026/*
1027 * Create two new sockets, of type TYPE in domain DOMAIN and using
1028 * protocol PROTOCOL, which are connected to each other, and put file
1029 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1030 * one will be chosen automatically.
1031 * Returns 0 on success, -1 for errors.
1032 * */
1033int
Dave Wallace048b1d62018-01-03 22:24:41 -05001034socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001035{
Florin Coras7baeb712019-01-04 17:05:43 -08001036 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001037
Dave Wallace2a865272018-02-07 21:00:42 -05001038 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001039 return -1;
1040
1041 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1042 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001043 {
Florin Coras7baeb712019-01-04 17:05:43 -08001044 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001045 errno = ENOSYS;
1046 rv = -1;
1047 }
1048 else
1049 {
Florin Coras7baeb712019-01-04 17:05:43 -08001050 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001051 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001052 }
1053
Dave Wallace048b1d62018-01-03 22:24:41 -05001054 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001055}
1056
1057int
Dave Wallace048b1d62018-01-03 22:24:41 -05001058bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001059{
Florin Coras7baeb712019-01-04 17:05:43 -08001060 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001061 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001062
Dave Wallace2a865272018-02-07 21:00:42 -05001063 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001064 return -1;
1065
Florin Coras7baeb712019-01-04 17:05:43 -08001066 vlsh = ldp_fd_to_vlsh (fd);
1067 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001068 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001069 vppcom_endpt_t ep;
1070
Dave Wallace048b1d62018-01-03 22:24:41 -05001071 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001072 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001073 case AF_INET:
1074 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001075 {
Florin Coras7baeb712019-01-04 17:05:43 -08001076 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1077 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001078 errno = EINVAL;
1079 rv = -1;
1080 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001081 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001082 ep.is_ip4 = VPPCOM_IS_IP4;
1083 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1084 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1085 break;
1086
1087 case AF_INET6:
1088 if (len != sizeof (struct sockaddr_in6))
1089 {
Florin Coras7baeb712019-01-04 17:05:43 -08001090 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1091 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001092 errno = EINVAL;
1093 rv = -1;
1094 goto done;
1095 }
1096 ep.is_ip4 = VPPCOM_IS_IP6;
1097 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1098 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001099 break;
1100
1101 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001102 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1103 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001104 errno = EAFNOSUPPORT;
1105 rv = -1;
1106 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001107 }
Florin Coras7baeb712019-01-04 17:05:43 -08001108 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1109 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001110
Florin Coras7baeb712019-01-04 17:05:43 -08001111 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001112 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001113 {
1114 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001115 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001116 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001117 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001118 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001119 {
Florin Coras7baeb712019-01-04 17:05:43 -08001120 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001121 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001122 }
1123
Dave Wallace048b1d62018-01-03 22:24:41 -05001124done:
Florin Coras7baeb712019-01-04 17:05:43 -08001125 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001126
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001127 return rv;
1128}
1129
1130static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001131ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1132 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001133{
Dave Wallace048b1d62018-01-03 22:24:41 -05001134 int rv = 0;
1135 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001136
Dave Wallace2a865272018-02-07 21:00:42 -05001137 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001138 return -1;
1139
1140 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001141 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001142 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1143 switch (addr->sa_family)
1144 {
1145 case AF_INET:
1146 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1147 if (*len > sizeof (struct sockaddr_in))
1148 *len = sizeof (struct sockaddr_in);
1149 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1150 copy_len = *len - sa_len;
1151 if (copy_len > 0)
1152 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1153 copy_len);
1154 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001155
Dave Wallace048b1d62018-01-03 22:24:41 -05001156 case AF_INET6:
1157 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1158 if (*len > sizeof (struct sockaddr_in6))
1159 *len = sizeof (struct sockaddr_in6);
1160 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1161 copy_len = *len - sa_len;
1162 if (copy_len > 0)
1163 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1164 __in6_u.__u6_addr8, ep->ip, copy_len);
1165 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001166
Dave Wallace048b1d62018-01-03 22:24:41 -05001167 default:
1168 /* Not possible */
1169 rv = -EAFNOSUPPORT;
1170 break;
1171 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001172 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001173 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001174}
1175
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001176int
Dave Wallace048b1d62018-01-03 22:24:41 -05001177getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001178{
Florin Coras7baeb712019-01-04 17:05:43 -08001179 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001180 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001181
Dave Wallace2a865272018-02-07 21:00:42 -05001182 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001183 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001184
Florin Coras7baeb712019-01-04 17:05:43 -08001185 vlsh = ldp_fd_to_vlsh (fd);
1186 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001187 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001188 vppcom_endpt_t ep;
1189 u8 addr_buf[sizeof (struct in6_addr)];
1190 u32 size = sizeof (ep);
1191
1192 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001193
Florin Coras7baeb712019-01-04 17:05:43 -08001194 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001195 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001196 {
1197 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001198 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001199 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001200 else
1201 {
Dave Wallace2a865272018-02-07 21:00:42 -05001202 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001203 if (rv != VPPCOM_OK)
1204 {
1205 errno = -rv;
1206 rv = -1;
1207 }
1208 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001209 }
1210 else
1211 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001212 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001213 }
1214
Dave Wallace048b1d62018-01-03 22:24:41 -05001215 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001216}
1217
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001218int
Dave Wallace048b1d62018-01-03 22:24:41 -05001219connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001220{
Florin Coras7baeb712019-01-04 17:05:43 -08001221 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001222 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001223
Dave Wallace2a865272018-02-07 21:00:42 -05001224 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001225 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001226
Dave Wallace048b1d62018-01-03 22:24:41 -05001227 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001228 {
Florin Coras7baeb712019-01-04 17:05:43 -08001229 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001230 errno = EINVAL;
1231 rv = -1;
1232 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001233 }
1234
Florin Coras7baeb712019-01-04 17:05:43 -08001235 vlsh = ldp_fd_to_vlsh (fd);
1236 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001237 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001238 vppcom_endpt_t ep;
1239
Dave Wallace048b1d62018-01-03 22:24:41 -05001240 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001241 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001242 case AF_INET:
1243 if (len != sizeof (struct sockaddr_in))
1244 {
Florin Coras7baeb712019-01-04 17:05:43 -08001245 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1246 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001247 errno = EINVAL;
1248 rv = -1;
1249 goto done;
1250 }
1251 ep.is_ip4 = VPPCOM_IS_IP4;
1252 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1253 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1254 break;
1255
1256 case AF_INET6:
1257 if (len != sizeof (struct sockaddr_in6))
1258 {
Florin Coras7baeb712019-01-04 17:05:43 -08001259 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1260 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001261 errno = EINVAL;
1262 rv = -1;
1263 goto done;
1264 }
1265 ep.is_ip4 = VPPCOM_IS_IP6;
1266 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1267 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1268 break;
1269
1270 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001271 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1272 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001273 errno = EAFNOSUPPORT;
1274 rv = -1;
1275 goto done;
1276 }
Florin Coras7baeb712019-01-04 17:05:43 -08001277 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1278 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001279
Florin Coras7baeb712019-01-04 17:05:43 -08001280 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001281 if (rv != VPPCOM_OK)
1282 {
1283 errno = -rv;
1284 rv = -1;
1285 }
1286 }
1287 else
1288 {
Florin Coras7baeb712019-01-04 17:05:43 -08001289 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1290 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001291
1292 rv = libc_connect (fd, addr, len);
1293 }
1294
1295done:
Florin Coras7baeb712019-01-04 17:05:43 -08001296 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001297 return rv;
1298}
1299
1300int
1301getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1302{
Florin Coras7baeb712019-01-04 17:05:43 -08001303 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001304 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001305
Dave Wallace2a865272018-02-07 21:00:42 -05001306 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001307 return -1;
1308
Florin Coras7baeb712019-01-04 17:05:43 -08001309 vlsh = ldp_fd_to_vlsh (fd);
1310 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001311 {
1312 vppcom_endpt_t ep;
1313 u8 addr_buf[sizeof (struct in6_addr)];
1314 u32 size = sizeof (ep);
1315
1316 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001317 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001318 if (rv != VPPCOM_OK)
1319 {
1320 errno = -rv;
1321 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001322 }
1323 else
1324 {
Dave Wallace2a865272018-02-07 21:00:42 -05001325 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001326 if (rv != VPPCOM_OK)
1327 {
1328 errno = -rv;
1329 rv = -1;
1330 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001331 }
1332 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001333 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001334 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001335 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001336 }
1337
Dave Wallace048b1d62018-01-03 22:24:41 -05001338 return rv;
1339}
1340
1341ssize_t
1342send (int fd, const void *buf, size_t n, int flags)
1343{
Florin Coras7baeb712019-01-04 17:05:43 -08001344 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001345 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001346
Dave Wallace2a865272018-02-07 21:00:42 -05001347 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001348 return -1;
1349
Florin Coras7baeb712019-01-04 17:05:43 -08001350 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001351 {
Florin Coras7baeb712019-01-04 17:05:43 -08001352 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001353 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001354 {
1355 errno = -size;
1356 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001357 }
1358 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001359 else
1360 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001361 size = libc_send (fd, buf, n, flags);
1362 }
1363
Dave Wallace048b1d62018-01-03 22:24:41 -05001364 return size;
1365}
1366
1367ssize_t
1368sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1369{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001370 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001371 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001372 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001373
Dave Wallace2a865272018-02-07 21:00:42 -05001374 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001375 return -1;
1376
Florin Coras7baeb712019-01-04 17:05:43 -08001377 vlsh = ldp_fd_to_vlsh (out_fd);
1378 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001379 {
1380 int rv;
1381 ssize_t results = 0;
1382 size_t n_bytes_left = len;
1383 size_t bytes_to_read;
1384 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001385 u8 eagain = 0;
1386 u32 flags, flags_len = sizeof (flags);
1387
Florin Coras7baeb712019-01-04 17:05:43 -08001388 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001389 if (PREDICT_FALSE (rv != VPPCOM_OK))
1390 {
Florin Coras7baeb712019-01-04 17:05:43 -08001391 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1392 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001393
Florin Corasdfe4cf42018-11-28 22:13:45 -08001394 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001395 errno = -rv;
1396 size = -1;
1397 goto done;
1398 }
1399
1400 if (offset)
1401 {
1402 off_t off = lseek (in_fd, *offset, SEEK_SET);
1403 if (PREDICT_FALSE (off == -1))
1404 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001405 size = -1;
1406 goto done;
1407 }
1408
1409 ASSERT (off == *offset);
1410 }
1411
1412 do
1413 {
Florin Coras7baeb712019-01-04 17:05:43 -08001414 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001415 if (size < 0)
1416 {
Florin Coraseb801d02020-09-16 17:44:58 -07001417 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001418 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001419 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001420 errno = -size;
1421 size = -1;
1422 goto done;
1423 }
1424
1425 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001426 if (bytes_to_read == 0)
1427 {
1428 if (flags & O_NONBLOCK)
1429 {
1430 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001431 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001432 goto update_offset;
1433 }
1434 else
1435 continue;
1436 }
1437 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001438 vec_validate (ldpw->io_buffer, bytes_to_read);
1439 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001440 if (nbytes < 0)
1441 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001442 if (results == 0)
1443 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001444 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001445 size = -1;
1446 goto done;
1447 }
1448 goto update_offset;
1449 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001450
Florin Coras7baeb712019-01-04 17:05:43 -08001451 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001452 if (size < 0)
1453 {
1454 if (size == VPPCOM_EAGAIN)
1455 {
1456 if (flags & O_NONBLOCK)
1457 {
1458 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001459 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001460 goto update_offset;
1461 }
1462 else
1463 continue;
1464 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001465 if (results == 0)
1466 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001467 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001468 errno = -size;
1469 size = -1;
1470 goto done;
1471 }
1472 goto update_offset;
1473 }
1474
1475 results += nbytes;
1476 ASSERT (n_bytes_left >= nbytes);
1477 n_bytes_left = n_bytes_left - nbytes;
1478 }
1479 while (n_bytes_left > 0);
1480
1481 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001482 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001483 if (offset)
1484 {
1485 off_t off = lseek (in_fd, *offset, SEEK_SET);
1486 if (PREDICT_FALSE (off == -1))
1487 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001488 size = -1;
1489 goto done;
1490 }
1491
1492 ASSERT (off == *offset);
1493 *offset += results + 1;
1494 }
1495 if (eagain)
1496 {
1497 errno = EAGAIN;
1498 size = -1;
1499 }
1500 else
1501 size = results;
1502 }
1503 else
1504 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001505 size = libc_sendfile (out_fd, in_fd, offset, len);
1506 }
1507
1508done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001509 return size;
1510}
1511
1512ssize_t
1513sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1514{
1515 return sendfile (out_fd, in_fd, offset, len);
1516}
1517
1518ssize_t
1519recv (int fd, void *buf, size_t n, int flags)
1520{
Florin Coras7baeb712019-01-04 17:05:43 -08001521 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001522 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001523
Dave Wallace2a865272018-02-07 21:00:42 -05001524 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001525 return -1;
1526
Florin Coras7baeb712019-01-04 17:05:43 -08001527 vlsh = ldp_fd_to_vlsh (fd);
1528 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001529 {
Florin Coras7baeb712019-01-04 17:05:43 -08001530 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001531 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001532 {
1533 errno = -size;
1534 size = -1;
1535 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001536 }
1537 else
1538 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001539 size = libc_recv (fd, buf, n, flags);
1540 }
1541
Dave Wallace048b1d62018-01-03 22:24:41 -05001542 return size;
1543}
1544
Florin Corasce17f462020-05-22 20:36:29 +00001545static int
1546ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1547 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1548{
1549 vppcom_endpt_t *ep = 0;
1550 vppcom_endpt_t _ep;
1551
1552 if (addr)
1553 {
1554 ep = &_ep;
1555 switch (addr->sa_family)
1556 {
1557 case AF_INET:
1558 ep->is_ip4 = VPPCOM_IS_IP4;
1559 ep->ip =
1560 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1561 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1562 break;
1563
1564 case AF_INET6:
1565 ep->is_ip4 = VPPCOM_IS_IP6;
1566 ep->ip =
1567 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1568 ep->port =
1569 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1570 break;
1571
1572 default:
1573 return EAFNOSUPPORT;
1574 }
1575 }
1576
1577 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1578}
1579
1580static int
1581ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1582 int flags, __SOCKADDR_ARG addr,
1583 socklen_t * __restrict addr_len)
1584{
1585 u8 src_addr[sizeof (struct sockaddr_in6)];
1586 vppcom_endpt_t ep;
1587 ssize_t size;
1588 int rv;
1589
1590 if (addr)
1591 {
1592 ep.ip = src_addr;
1593 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1594
1595 if (size > 0)
1596 {
1597 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1598 if (rv < 0)
1599 size = rv;
1600 }
1601 }
1602 else
1603 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1604
1605 return size;
1606}
1607
Dave Wallace048b1d62018-01-03 22:24:41 -05001608ssize_t
1609sendto (int fd, const void *buf, size_t n, int flags,
1610 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1611{
Florin Coras7baeb712019-01-04 17:05:43 -08001612 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001613 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001614
Dave Wallace2a865272018-02-07 21:00:42 -05001615 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001616 return -1;
1617
Florin Coras7baeb712019-01-04 17:05:43 -08001618 vlsh = ldp_fd_to_vlsh (fd);
1619 if (vlsh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001620 {
Florin Corasce17f462020-05-22 20:36:29 +00001621 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001622 if (size < 0)
1623 {
1624 errno = -size;
1625 size = -1;
1626 }
1627 }
1628 else
1629 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001630 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1631 }
1632
Dave Wallace048b1d62018-01-03 22:24:41 -05001633 return size;
1634}
1635
1636ssize_t
1637recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1638 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1639{
Florin Corasce17f462020-05-22 20:36:29 +00001640 vls_handle_t vlsh;
1641 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001642
Dave Wallace2a865272018-02-07 21:00:42 -05001643 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001644 return -1;
1645
Florin Corasce17f462020-05-22 20:36:29 +00001646 vlsh = ldp_fd_to_vlsh (fd);
1647 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001648 {
Florin Corasce17f462020-05-22 20:36:29 +00001649 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001650 if (size < 0)
1651 {
1652 errno = -size;
1653 size = -1;
1654 }
1655 }
1656 else
1657 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001658 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1659 }
1660
Dave Wallace048b1d62018-01-03 22:24:41 -05001661 return size;
1662}
1663
1664ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001665sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001666{
Florin Coras7baeb712019-01-04 17:05:43 -08001667 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001668 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001669
Dave Wallace2a865272018-02-07 21:00:42 -05001670 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001671 return -1;
1672
Florin Coras7baeb712019-01-04 17:05:43 -08001673 vlsh = ldp_fd_to_vlsh (fd);
1674 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001675 {
Florin Corasce17f462020-05-22 20:36:29 +00001676 struct iovec *iov = msg->msg_iov;
1677 ssize_t total = 0;
1678 int i, rv;
1679
1680 for (i = 0; i < msg->msg_iovlen; ++i)
1681 {
1682 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1683 msg->msg_name, msg->msg_namelen);
1684 if (rv < 0)
1685 break;
1686 else
1687 {
1688 total += rv;
1689 if (rv < iov[i].iov_len)
1690 break;
1691 }
1692 }
1693
1694 if (rv < 0 && total == 0)
1695 {
1696 errno = -rv;
1697 size = -1;
1698 }
1699 else
1700 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001701 }
1702 else
1703 {
Florin Corasce17f462020-05-22 20:36:29 +00001704 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001705 }
1706
Dave Wallace048b1d62018-01-03 22:24:41 -05001707 return size;
1708}
1709
1710#ifdef USE_GNU
1711int
1712sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1713{
1714 ssize_t size;
1715 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001716 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001717
Dave Wallace2a865272018-02-07 21:00:42 -05001718 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001719 return -1;
1720
Florin Coras7baeb712019-01-04 17:05:43 -08001721 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001722 {
1723 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1724 errno = ENOSYS;
1725 size = -1;
1726 }
1727 else
1728 {
1729 func_str = "libc_sendmmsg";
1730
Dave Wallace2a865272018-02-07 21:00:42 -05001731 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001732 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1733 "vmessages %p, vlen %u, flags 0x%x",
1734 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1735
1736 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1737 }
1738
Dave Wallace2a865272018-02-07 21:00:42 -05001739 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001740 {
1741 if (size < 0)
1742 {
1743 int errno_val = errno;
1744 perror (func_str);
1745 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1746 "rv %d, errno = %d", getpid (), fd, fd,
1747 func_str, size, errno_val);
1748 errno = errno_val;
1749 }
1750 else
1751 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1752 getpid (), fd, fd, size, size);
1753 }
1754 return size;
1755}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001756#endif
1757
Dave Wallace048b1d62018-01-03 22:24:41 -05001758ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001759recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001760{
Florin Coras7baeb712019-01-04 17:05:43 -08001761 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001762 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001763
Dave Wallace2a865272018-02-07 21:00:42 -05001764 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001765 return -1;
1766
Florin Coras7baeb712019-01-04 17:05:43 -08001767 vlsh = ldp_fd_to_vlsh (fd);
1768 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001769 {
Florin Corasce17f462020-05-22 20:36:29 +00001770 struct iovec *iov = msg->msg_iov;
1771 ssize_t max_deq, total = 0;
1772 int i, rv;
1773
1774 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1775 if (!max_deq)
1776 return 0;
1777
1778 for (i = 0; i < msg->msg_iovlen; i++)
1779 {
1780 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1781 (i == 0 ? msg->msg_name : NULL),
1782 (i == 0 ? &msg->msg_namelen : NULL));
1783 if (rv <= 0)
1784 break;
1785 else
1786 {
1787 total += rv;
1788 if (rv < iov[i].iov_len)
1789 break;
1790 }
1791 if (total >= max_deq)
1792 break;
1793 }
1794
1795 if (rv < 0 && total == 0)
1796 {
1797 errno = -rv;
1798 size = -1;
1799 }
1800 else
1801 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001802 }
1803 else
1804 {
Florin Corasce17f462020-05-22 20:36:29 +00001805 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001806 }
1807
Dave Wallace048b1d62018-01-03 22:24:41 -05001808 return size;
1809}
1810
1811#ifdef USE_GNU
1812int
1813recvmmsg (int fd, struct mmsghdr *vmessages,
1814 unsigned int vlen, int flags, struct timespec *tmo)
1815{
1816 ssize_t size;
1817 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001818 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001819
Dave Wallace2a865272018-02-07 21:00:42 -05001820 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001821 return -1;
1822
Florin Coras7baeb712019-01-04 17:05:43 -08001823 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001824 {
1825 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1826 errno = ENOSYS;
1827 size = -1;
1828 }
1829 else
1830 {
1831 func_str = "libc_recvmmsg";
1832
Dave Wallace2a865272018-02-07 21:00:42 -05001833 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001834 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1835 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1836 getpid (), fd, fd, func_str, vmessages, vlen,
1837 flags, tmo);
1838
1839 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1840 }
1841
Dave Wallace2a865272018-02-07 21:00:42 -05001842 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001843 {
1844 if (size < 0)
1845 {
1846 int errno_val = errno;
1847 perror (func_str);
1848 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1849 "rv %d, errno = %d", getpid (), fd, fd,
1850 func_str, size, errno_val);
1851 errno = errno_val;
1852 }
1853 else
1854 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1855 getpid (), fd, fd, size, size);
1856 }
1857 return size;
1858}
1859#endif
1860
1861int
1862getsockopt (int fd, int level, int optname,
1863 void *__restrict optval, socklen_t * __restrict optlen)
1864{
Florin Coras7baeb712019-01-04 17:05:43 -08001865 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001866 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001867
Dave Wallace2a865272018-02-07 21:00:42 -05001868 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001869 return -1;
1870
Florin Coras7baeb712019-01-04 17:05:43 -08001871 vlsh = ldp_fd_to_vlsh (fd);
1872 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001873 {
1874 rv = -EOPNOTSUPP;
1875
1876 switch (level)
1877 {
1878 case SOL_TCP:
1879 switch (optname)
1880 {
1881 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001882 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1883 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001884 break;
1885 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001886 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1887 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001888 break;
1889 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001890 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1891 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001892 break;
1893 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001894 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1895 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001896 break;
1897 case TCP_INFO:
1898 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1899 {
Florin Coras7baeb712019-01-04 17:05:43 -08001900 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1901 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001902 memset (optval, 0, *optlen);
1903 rv = VPPCOM_OK;
1904 }
1905 else
1906 rv = -EFAULT;
1907 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001908 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001909 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001910 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001911 rv = 0;
1912 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001913 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001914 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1915 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001916 break;
1917 }
1918 break;
1919 case SOL_IPV6:
1920 switch (optname)
1921 {
1922 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001923 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001924 break;
1925 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001926 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1927 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001928 break;
1929 }
1930 break;
1931 case SOL_SOCKET:
1932 switch (optname)
1933 {
1934 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001935 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001936 break;
1937 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001938 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001939 break;
1940 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001941 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001942 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1943 break;
1944 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001945 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1946 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001947 break;
1948 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001949 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1950 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001951 break;
1952 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001953 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001954 break;
wanghanlin0674f852021-02-22 10:38:36 +08001955 case SO_REUSEPORT:
1956 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1957 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001958 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001959 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001960 break;
wanghanlin0674f852021-02-22 10:38:36 +08001961 case SO_DOMAIN:
1962 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1963 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001964 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001965 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001966 break;
1967 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001968 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1969 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001970 break;
1971 }
1972 break;
1973 default:
1974 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001975 }
1976
Dave Wallace048b1d62018-01-03 22:24:41 -05001977 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001978 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001979 errno = -rv;
1980 rv = -1;
1981 }
1982 }
1983 else
1984 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001985 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1986 }
1987
Dave Wallace048b1d62018-01-03 22:24:41 -05001988 return rv;
1989}
1990
1991int
1992setsockopt (int fd, int level, int optname,
1993 const void *optval, socklen_t optlen)
1994{
Florin Coras7baeb712019-01-04 17:05:43 -08001995 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001996 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001997
Dave Wallace2a865272018-02-07 21:00:42 -05001998 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001999 return -1;
2000
Florin Coras7baeb712019-01-04 17:05:43 -08002001 vlsh = ldp_fd_to_vlsh (fd);
2002 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002003 {
2004 rv = -EOPNOTSUPP;
2005
2006 switch (level)
2007 {
2008 case SOL_TCP:
2009 switch (optname)
2010 {
2011 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002012 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2013 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002014 break;
2015 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002016 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2017 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002018 break;
2019 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002020 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2021 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002022 break;
2023 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002024 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2025 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002026 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002027 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002028 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002029 /* Ignore */
2030 rv = 0;
2031 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002032 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002033 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2034 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002035 break;
2036 }
2037 break;
2038 case SOL_IPV6:
2039 switch (optname)
2040 {
2041 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002042 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2043 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002044 break;
2045 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002046 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2047 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002048 break;
2049 }
2050 break;
2051 case SOL_SOCKET:
2052 switch (optname)
2053 {
2054 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002055 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2056 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 break;
2058 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002059 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2060 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002061 break;
wanghanlin0674f852021-02-22 10:38:36 +08002062 case SO_REUSEPORT:
2063 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2064 &optlen);
2065 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002066 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002067 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2068 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002069 break;
Florin Coras2f647902021-06-02 08:23:50 -07002070 case SO_LINGER:
2071 rv = 0;
2072 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002073 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002074 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2075 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002076 break;
2077 }
2078 break;
2079 default:
2080 break;
2081 }
2082
2083 if (rv != VPPCOM_OK)
2084 {
2085 errno = -rv;
2086 rv = -1;
2087 }
2088 }
2089 else
2090 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002091 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2092 }
2093
Dave Wallace048b1d62018-01-03 22:24:41 -05002094 return rv;
2095}
2096
2097int
2098listen (int fd, int n)
2099{
Florin Coras7baeb712019-01-04 17:05:43 -08002100 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002101 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002102
Dave Wallace2a865272018-02-07 21:00:42 -05002103 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002104 return -1;
2105
Florin Coras7baeb712019-01-04 17:05:43 -08002106 vlsh = ldp_fd_to_vlsh (fd);
2107 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 {
Florin Coras7baeb712019-01-04 17:05:43 -08002109 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002110
Florin Coras7baeb712019-01-04 17:05:43 -08002111 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002112 if (rv != VPPCOM_OK)
2113 {
2114 errno = -rv;
2115 rv = -1;
2116 }
2117 }
2118 else
2119 {
Florin Coras7baeb712019-01-04 17:05:43 -08002120 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 rv = libc_listen (fd, n);
2122 }
2123
Florin Coras7baeb712019-01-04 17:05:43 -08002124 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 return rv;
2126}
2127
2128static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002129ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2130 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002131{
Florin Coras7baeb712019-01-04 17:05:43 -08002132 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002133 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002134
Dave Wallace2a865272018-02-07 21:00:42 -05002135 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002136 return -1;
2137
Florin Coras7baeb712019-01-04 17:05:43 -08002138 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2139 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002140 {
2141 vppcom_endpt_t ep;
2142 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002143 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002144 ep.ip = src_addr;
2145
Florin Coras7baeb712019-01-04 17:05:43 -08002146 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002147 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002148
Florin Coras7baeb712019-01-04 17:05:43 -08002149 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2150 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002151 {
Florin Coras7baeb712019-01-04 17:05:43 -08002152 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002153 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002154 }
2155 else
2156 {
Dave Wallace2a865272018-02-07 21:00:42 -05002157 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002158 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002159 {
Florin Coras7baeb712019-01-04 17:05:43 -08002160 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002161 errno = -rv;
2162 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002163 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002164 else
2165 {
Florin Coras7baeb712019-01-04 17:05:43 -08002166 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002167 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002168 }
2169 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002170 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002171 {
Florin Coras7baeb712019-01-04 17:05:43 -08002172 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2173 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002174
Dave Wallace048b1d62018-01-03 22:24:41 -05002175 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002176 }
2177
Florin Coras7baeb712019-01-04 17:05:43 -08002178 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002179
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002180 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002181}
2182
Dave Wallace048b1d62018-01-03 22:24:41 -05002183int
2184accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2185 int flags)
2186{
Dave Wallace2a865272018-02-07 21:00:42 -05002187 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002188}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002189
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002190int
Dave Wallace048b1d62018-01-03 22:24:41 -05002191accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002192{
Dave Wallace2a865272018-02-07 21:00:42 -05002193 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002194}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002195
Dave Wallace048b1d62018-01-03 22:24:41 -05002196int
2197shutdown (int fd, int how)
2198{
Florin Coras7baeb712019-01-04 17:05:43 -08002199 vls_handle_t vlsh;
2200 int rv = 0, flags;
2201 u32 flags_len = sizeof (flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002202
Dave Wallace2a865272018-02-07 21:00:42 -05002203 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002204 return -1;
2205
Florin Coras7baeb712019-01-04 17:05:43 -08002206 vlsh = ldp_fd_to_vlsh (fd);
2207 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002208 {
Florin Coras7baeb712019-01-04 17:05:43 -08002209 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
Florin Corasa7a1a222018-12-30 17:11:31 -08002210
Florin Coras7baeb712019-01-04 17:05:43 -08002211 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
Florin Corasa7a1a222018-12-30 17:11:31 -08002212 {
Florin Coras7baeb712019-01-04 17:05:43 -08002213 close (fd);
Florin Corasa7a1a222018-12-30 17:11:31 -08002214 return -1;
2215 }
2216
Florin Coras7baeb712019-01-04 17:05:43 -08002217 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2218 {
2219 close (fd);
2220 return -1;
2221 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002222
Florin Coras7baeb712019-01-04 17:05:43 -08002223 if (flags == SHUT_RDWR)
Florin Corasa7a1a222018-12-30 17:11:31 -08002224 rv = close (fd);
liuyacan534468e2021-05-09 03:50:40 +00002225 else if (flags == SHUT_WR)
2226 rv = vls_shutdown (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002227 }
2228 else
2229 {
Florin Coras7baeb712019-01-04 17:05:43 -08002230 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002231 rv = libc_shutdown (fd, how);
2232 }
2233
Dave Wallace048b1d62018-01-03 22:24:41 -05002234 return rv;
2235}
2236
2237int
2238epoll_create1 (int flags)
2239{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002240 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002241 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002242 int rv;
2243
Dave Wallace2a865272018-02-07 21:00:42 -05002244 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002245 return -1;
2246
hanlina3a48962020-07-13 11:09:15 +08002247 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002248 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002249 /* Make sure workers have been allocated */
2250 if (!ldp->workers)
2251 {
2252 ldp_alloc_workers ();
2253 ldpw = ldp_worker_get_current ();
2254 }
Florin Coras99368312018-08-02 10:45:44 -07002255 rv = libc_epoll_create1 (flags);
2256 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002257 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002258 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002259 return rv;
2260 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002261
Florin Coras7baeb712019-01-04 17:05:43 -08002262 vlsh = vls_epoll_create ();
2263 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002264 {
Florin Coras7baeb712019-01-04 17:05:43 -08002265 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002266 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002267 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002268 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002269 {
Florin Coras7baeb712019-01-04 17:05:43 -08002270 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002271 }
Florin Coras7baeb712019-01-04 17:05:43 -08002272 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002273 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002274}
2275
2276int
Dave Wallace048b1d62018-01-03 22:24:41 -05002277epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002278{
Dave Wallace048b1d62018-01-03 22:24:41 -05002279 return epoll_create1 (0);
2280}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002281
Dave Wallace048b1d62018-01-03 22:24:41 -05002282int
2283epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2284{
Florin Coras7baeb712019-01-04 17:05:43 -08002285 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002286 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002287
Dave Wallace2a865272018-02-07 21:00:42 -05002288 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002289 return -1;
2290
Florin Coras7baeb712019-01-04 17:05:43 -08002291 vep_vlsh = ldp_fd_to_vlsh (epfd);
2292 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002293 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002294 /* The LDP epoll_create1 always creates VCL epfd's.
2295 * The app should never have a kernel base epoll fd unless it
2296 * was acquired outside of the LD_PRELOAD process context.
2297 * In any case, if we get one, punt it to libc_epoll_ctl.
2298 */
Florin Coras7baeb712019-01-04 17:05:43 -08002299 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2300 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002301
2302 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002303 goto done;
2304 }
2305
Florin Coras7baeb712019-01-04 17:05:43 -08002306 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002307
Florin Coras7baeb712019-01-04 17:05:43 -08002308 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2309 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002310
Florin Coras7baeb712019-01-04 17:05:43 -08002311 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002312 {
Florin Coras7baeb712019-01-04 17:05:43 -08002313 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002314 " event %p", epfd, vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002315
Florin Coras7baeb712019-01-04 17:05:43 -08002316 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002317 if (rv != VPPCOM_OK)
2318 {
2319 errno = -rv;
2320 rv = -1;
2321 }
2322 }
2323 else
2324 {
2325 int libc_epfd;
2326 u32 size = sizeof (epfd);
2327
Florin Coras7baeb712019-01-04 17:05:43 -08002328 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002329 if (!libc_epfd)
2330 {
Florin Coras7baeb712019-01-04 17:05:43 -08002331 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2332 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002333
2334 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2335 if (libc_epfd < 0)
2336 {
2337 rv = libc_epfd;
2338 goto done;
2339 }
2340
Florin Coras7baeb712019-01-04 17:05:43 -08002341 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2342 &size);
Florin Coras99368312018-08-02 10:45:44 -07002343 if (rv < 0)
2344 {
2345 errno = -rv;
2346 rv = -1;
2347 goto done;
2348 }
2349 }
2350 else if (PREDICT_FALSE (libc_epfd < 0))
2351 {
2352 errno = -epfd;
2353 rv = -1;
2354 goto done;
2355 }
2356
Florin Coras7baeb712019-01-04 17:05:43 -08002357 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2358 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002359
2360 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002361 }
2362
2363done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002364 return rv;
2365}
Dave Wallace048b1d62018-01-03 22:24:41 -05002366
2367static inline int
Florin Coras99368312018-08-02 10:45:44 -07002368ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2369 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002370{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002371 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002372 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002373 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002374 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002375
Dave Wallace2a865272018-02-07 21:00:42 -05002376 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002377 return -1;
2378
2379 if (PREDICT_FALSE (!events || (timeout < -1)))
2380 {
2381 errno = EFAULT;
2382 return -1;
2383 }
2384
Florin Corasdfe4cf42018-11-28 22:13:45 -08002385 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002386 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2387
Florin Coras7baeb712019-01-04 17:05:43 -08002388 ep_vlsh = ldp_fd_to_vlsh (epfd);
2389 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002390 {
Florin Coras7baeb712019-01-04 17:05:43 -08002391 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002392 errno = EBADFD;
2393 return -1;
2394 }
2395
Florin Coras4dee8cd2019-01-29 21:28:16 -08002396 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2397 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002398 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002399 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002400
Florin Coras7baeb712019-01-04 17:05:43 -08002401 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002402 if (PREDICT_FALSE (libc_epfd < 0))
2403 {
2404 errno = -libc_epfd;
2405 rv = -1;
2406 goto done;
2407 }
2408
Florin Coras7baeb712019-01-04 17:05:43 -08002409 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2410 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002411 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002412 do
2413 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002414 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002415 {
Florin Coras7baeb712019-01-04 17:05:43 -08002416 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002417 if (rv > 0)
2418 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002419 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002420 goto done;
2421 }
2422 else if (rv < 0)
2423 {
2424 errno = -rv;
2425 rv = -1;
2426 goto done;
2427 }
2428 }
2429 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002430 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002431
2432 if (libc_epfd > 0)
2433 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002434 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002435 if (rv != 0)
2436 goto done;
2437 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002438 }
Florin Coras72f77822019-01-22 19:05:52 -08002439 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002440
2441done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002442 return rv;
2443}
2444
hanlin4266d4d2020-05-19 17:34:17 +08002445static inline int
2446ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2447 int maxevents, int timeout, const sigset_t * sigmask)
2448{
hanlina3a48962020-07-13 11:09:15 +08002449 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002450 int libc_epfd, rv = 0, num_ev;
2451 vls_handle_t ep_vlsh;
2452
2453 if ((errno = -ldp_init ()))
2454 return -1;
2455
2456 if (PREDICT_FALSE (!events || (timeout < -1)))
2457 {
2458 errno = EFAULT;
2459 return -1;
2460 }
2461
Florin Corasff40d8f2020-08-11 22:05:28 -07002462 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2463 * one thread but it is now used on another */
2464 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2465 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002466
2467 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002468 if (epfd == ldpw->vcl_mq_epfd)
2469 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2470
2471 ep_vlsh = ldp_fd_to_vlsh (epfd);
2472 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2473 {
2474 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2475 errno = EBADFD;
2476 return -1;
2477 }
2478
2479 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2480 if (PREDICT_FALSE (!libc_epfd))
2481 {
2482 u32 size = sizeof (epfd);
2483
2484 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2485 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2486 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2487 if (libc_epfd < 0)
2488 {
2489 rv = libc_epfd;
2490 goto done;
2491 }
2492
2493 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2494 if (rv < 0)
2495 {
2496 errno = -rv;
2497 rv = -1;
2498 goto done;
2499 }
2500 }
2501 if (PREDICT_FALSE (libc_epfd <= 0))
2502 {
2503 errno = -libc_epfd;
2504 rv = -1;
2505 goto done;
2506 }
2507
2508 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2509 {
2510 struct epoll_event e = { 0 };
2511 e.events = EPOLLIN;
2512 e.data.fd = ldpw->vcl_mq_epfd;
2513 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2514 0)
2515 {
2516 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2517 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2518 rv = -1;
2519 goto done;
2520 }
2521 ldpw->mq_epfd_added = 1;
2522 }
2523
wanghanlin8919fec2021-03-18 20:00:41 +08002524 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2525 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002526 if (rv > 0)
2527 goto done;
hanlina3a48962020-07-13 11:09:15 +08002528 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002529 {
2530 errno = -rv;
2531 rv = -1;
2532 goto done;
2533 }
2534
2535 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2536 if (rv <= 0)
2537 goto done;
2538 for (int i = 0; i < rv; i++)
2539 {
2540 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2541 {
2542 /* We should remove mq epoll fd from events. */
2543 rv--;
2544 if (i != rv)
2545 {
2546 events[i].events = events[rv].events;
2547 events[i].data.u64 = events[rv].data.u64;
2548 }
2549 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2550 if (PREDICT_TRUE (num_ev > 0))
2551 rv += num_ev;
2552 break;
2553 }
2554 }
2555
2556done:
2557 return rv;
2558}
2559
Dave Wallace048b1d62018-01-03 22:24:41 -05002560int
2561epoll_pwait (int epfd, struct epoll_event *events,
2562 int maxevents, int timeout, const sigset_t * sigmask)
2563{
hanlin4266d4d2020-05-19 17:34:17 +08002564 if (vls_use_eventfd ())
2565 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2566 sigmask);
2567 else
2568 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002569}
2570
2571int
2572epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2573{
hanlin4266d4d2020-05-19 17:34:17 +08002574 if (vls_use_eventfd ())
2575 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2576 else
2577 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002578}
2579
2580int
2581poll (struct pollfd *fds, nfds_t nfds, int timeout)
2582{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002583 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002584 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002585 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002586 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002587 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002588
Florin Coraseb801d02020-09-16 17:44:58 -07002589 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002590
Florin Coras4dee8cd2019-01-29 21:28:16 -08002591 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2592 clib_time_init (&ldpw->clib_time);
2593
2594 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2595 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002596
Dave Wallace048b1d62018-01-03 22:24:41 -05002597 for (i = 0; i < nfds; i++)
2598 {
Florin Coras6917b942018-11-13 22:44:54 -08002599 if (fds[i].fd < 0)
2600 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002601
Florin Coras7baeb712019-01-04 17:05:43 -08002602 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2603 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002604 {
2605 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002606 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002607 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002608 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002609 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002610#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002611 if (fds[i].events & POLLRDNORM)
2612 vp->events |= POLLIN;
2613 if (fds[i].events & POLLWRNORM)
2614 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002615#endif
Florin Coras6917b942018-11-13 22:44:54 -08002616 vp->revents = fds[i].revents;
2617 }
2618 else
2619 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002620 vec_add1 (ldpw->libc_poll, fds[i]);
2621 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002622 }
2623 }
2624
Dave Wallace048b1d62018-01-03 22:24:41 -05002625 do
2626 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002627 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002628 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002629 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002630 if (rv < 0)
2631 {
2632 errno = -rv;
2633 rv = -1;
2634 goto done;
2635 }
2636 else
2637 n_revents += rv;
2638 }
2639
Florin Corasdfe4cf42018-11-28 22:13:45 -08002640 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002641 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002642 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002643 if (rv < 0)
2644 goto done;
2645 else
2646 n_revents += rv;
2647 }
2648
2649 if (n_revents)
2650 {
2651 rv = n_revents;
2652 goto done;
2653 }
2654 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002655 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002656 rv = 0;
2657
2658done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002659 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002660 {
2661 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002662 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002663#ifdef __USE_XOPEN2K
2664 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2665 (fds[vp->fds_ndx].events & POLLRDNORM))
2666 fds[vp->fds_ndx].revents |= POLLRDNORM;
2667 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2668 (fds[vp->fds_ndx].events & POLLWRNORM))
2669 fds[vp->fds_ndx].revents |= POLLWRNORM;
2670#endif
2671 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002672 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002673
Florin Corasdfe4cf42018-11-28 22:13:45 -08002674 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002675 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002676 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002677 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002678 vec_reset_length (ldpw->libc_poll_idxs);
2679 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002680
Dave Wallace048b1d62018-01-03 22:24:41 -05002681 return rv;
2682}
2683
2684#ifdef USE_GNU
2685int
2686ppoll (struct pollfd *fds, nfds_t nfds,
2687 const struct timespec *timeout, const sigset_t * sigmask)
2688{
Dave Wallace2a865272018-02-07 21:00:42 -05002689 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002690 return -1;
2691
2692 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2693 errno = ENOSYS;
2694
2695
2696 return -1;
2697}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002698#endif
2699
Dave Wallace2a865272018-02-07 21:00:42 -05002700void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002701
Dave Wallace2a865272018-02-07 21:00:42 -05002702void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002703
Dave Wallace048b1d62018-01-03 22:24:41 -05002704/*
2705 * This function is called when the library is loaded
2706 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002707void
Dave Wallace2a865272018-02-07 21:00:42 -05002708ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002709{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002710 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002711 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002712 {
2713 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2714 getpid ());
2715 _exit (1);
2716 }
Dave Wallace69d01192018-02-22 16:22:09 -05002717 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002718 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002719}
2720
2721/*
2722 * This function is called when the library is unloaded
2723 */
2724void
Dave Wallace2a865272018-02-07 21:00:42 -05002725ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002726{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002727 /*
2728 swrap_destructor ();
2729 if (ldp->init)
2730 ldp->init = 0;
2731 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002732
2733 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002734 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002735 */
Dave Wallace69d01192018-02-22 16:22:09 -05002736 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002737 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2738 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002739}
2740
2741
2742/*
2743 * fd.io coding-style-patch-verification: ON
2744 *
2745 * Local Variables:
2746 * eval: (c-set-style "gnu")
2747 * End:
2748 */