blob: 316d97df2e2b454eef27ca7b9113d4dc33a50f44 [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
Florin Coras5f33d0d2021-06-02 21:22:21 -0700182static 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 Coras5f33d0d2021-06-02 21:22:21 -0700188 ASSERT (!ldp->init);
Florin Coras99368312018-08-02 10:45:44 -0700189
190 ldp->init = 1;
191 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800192 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700193 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700194 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800195 ldp->vcl_needs_real_epoll = 0;
196 if (rv == VPPCOM_EEXIST)
197 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800198 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
199 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700200 ldp->init = 0;
201 return rv;
202 }
203 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700204 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800205 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700206
207 char *env_var_str = getenv (LDP_ENV_DEBUG);
208 if (env_var_str)
209 {
210 u32 tmp;
211 if (sscanf (env_var_str, "%u", &tmp) != 1)
212 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
213 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
214 env_var_str);
215 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700216 {
Florin Coras99368312018-08-02 10:45:44 -0700217 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800218 LDBG (0, "configured LDP debug level (%u) from env var "
219 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700220 }
221 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500222
Florin Coras99368312018-08-02 10:45:44 -0700223 env_var_str = getenv (LDP_ENV_APP_NAME);
224 if (env_var_str)
225 {
226 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800227 LDBG (0, "configured LDP app name (%s) from the env var "
228 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700229 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500230
Florin Coras99368312018-08-02 10:45:44 -0700231 env_var_str = getenv (LDP_ENV_SID_BIT);
232 if (env_var_str)
233 {
234 u32 sb;
235 if (sscanf (env_var_str, "%u", &sb) != 1)
236 {
Florin Coras294afe22019-01-07 17:49:17 -0800237 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
238 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
239 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700240 }
241 else if (sb < LDP_SID_BIT_MIN)
242 {
Florin Coras7baeb712019-01-04 17:05:43 -0800243 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
244 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500245
Florin Coras294afe22019-01-07 17:49:17 -0800246 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
247 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
248 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
249 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700250 }
251 else if (sb > LDP_SID_BIT_MAX)
252 {
Florin Coras7baeb712019-01-04 17:05:43 -0800253 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
254 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500255
Florin Coras294afe22019-01-07 17:49:17 -0800256 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
257 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
258 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
259 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500260 }
261 else
262 {
Florin Coras7baeb712019-01-04 17:05:43 -0800263 ldp->vlsh_bit_val = (1 << sb);
264 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700265
Florin Coras05ecfcc2018-12-12 18:19:39 -0800266 LDBG (0, "configured LDP sid bit (%u) from "
267 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800268 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500269 }
Florin Coras294afe22019-01-07 17:49:17 -0800270
271 /* Make sure there are enough bits in the fd set for vcl sessions */
272 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
273 {
274 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
275 ldp->vlsh_bit_val, FD_SETSIZE / 2);
276 ldp->init = 0;
277 return -1;
278 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500279 }
Yu Ping7b74b072019-05-08 00:40:24 +0800280 env_var_str = getenv (LDP_ENV_TLS_TRANS);
281 if (env_var_str)
282 {
283 ldp->transparent_tls = 1;
284 }
Florin Coras99368312018-08-02 10:45:44 -0700285
Florin Coras4dee8cd2019-01-29 21:28:16 -0800286 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100287 pool_foreach (ldpw, ldp->workers) {
Florin Coras4dee8cd2019-01-29 21:28:16 -0800288 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100289 }
Florin Coras4dee8cd2019-01-29 21:28:16 -0800290 /* *INDENT-ON* */
291
Florin Coras05ecfcc2018-12-12 18:19:39 -0800292 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700293
294 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500295}
296
Florin Coras5f33d0d2021-06-02 21:22:21 -0700297#define ldp_init_check() \
298 if (PREDICT_FALSE (!ldp->init)) \
299 { \
300 if ((errno = -ldp_init ())) \
301 return -1; \
302 }
303
Dave Wallace048b1d62018-01-03 22:24:41 -0500304int
305close (int fd)
306{
Florin Coras7baeb712019-01-04 17:05:43 -0800307 vls_handle_t vlsh;
308 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500309
Florin Coras5f33d0d2021-06-02 21:22:21 -0700310 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500311
Florin Coras7baeb712019-01-04 17:05:43 -0800312 vlsh = ldp_fd_to_vlsh (fd);
313 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500314 {
Florin Coras7baeb712019-01-04 17:05:43 -0800315 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500316 if (epfd > 0)
317 {
Florin Coras7baeb712019-01-04 17:05:43 -0800318 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500319
320 rv = libc_close (epfd);
321 if (rv < 0)
322 {
323 u32 size = sizeof (epfd);
324 epfd = 0;
325
Florin Coras7baeb712019-01-04 17:05:43 -0800326 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500327 }
328 }
329 else if (PREDICT_FALSE (epfd < 0))
330 {
331 errno = -epfd;
332 rv = -1;
333 goto done;
334 }
335
Florin Coras7baeb712019-01-04 17:05:43 -0800336 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500337
Florin Coras7baeb712019-01-04 17:05:43 -0800338 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500339 if (rv != VPPCOM_OK)
340 {
341 errno = -rv;
342 rv = -1;
343 }
344 }
345 else
346 {
Florin Coras7baeb712019-01-04 17:05:43 -0800347 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500348 rv = libc_close (fd);
349 }
350
351done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500352 return rv;
353}
354
355ssize_t
356read (int fd, void *buf, size_t nbytes)
357{
Florin Coras7baeb712019-01-04 17:05:43 -0800358 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500359 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500360
Florin Coras5f33d0d2021-06-02 21:22:21 -0700361 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500362
Florin Coras7baeb712019-01-04 17:05:43 -0800363 vlsh = ldp_fd_to_vlsh (fd);
364 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500365 {
Florin Coras7baeb712019-01-04 17:05:43 -0800366 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500367 if (size < 0)
368 {
369 errno = -size;
370 size = -1;
371 }
372 }
373 else
374 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500375 size = libc_read (fd, buf, nbytes);
376 }
377
Dave Wallace048b1d62018-01-03 22:24:41 -0500378 return size;
379}
380
381ssize_t
382readv (int fd, const struct iovec * iov, int iovcnt)
383{
Dave Wallace8aaba562018-01-18 17:21:19 -0500384 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800385 vls_handle_t vlsh;
386 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500387
Florin Coras5f33d0d2021-06-02 21:22:21 -0700388 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500389
Florin Coras7baeb712019-01-04 17:05:43 -0800390 vlsh = ldp_fd_to_vlsh (fd);
391 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500392 {
Florin Coras067f9542020-02-14 05:33:46 +0000393 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500394 {
Florin Coras067f9542020-02-14 05:33:46 +0000395 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
396 if (rv <= 0)
397 break;
398 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500399 {
Florin Coras067f9542020-02-14 05:33:46 +0000400 total += rv;
401 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500402 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700403 }
404 }
Florin Coras067f9542020-02-14 05:33:46 +0000405 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700406 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500407 errno = -rv;
408 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700409 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500410 else
411 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700412 }
413 else
414 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500415 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700416 }
417
Dave Wallace048b1d62018-01-03 22:24:41 -0500418 return size;
419}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700420
Dave Wallace048b1d62018-01-03 22:24:41 -0500421ssize_t
422write (int fd, const void *buf, size_t nbytes)
423{
Florin Coras7baeb712019-01-04 17:05:43 -0800424 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500425 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500426
Florin Coras5f33d0d2021-06-02 21:22:21 -0700427 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500428
Florin Coras7baeb712019-01-04 17:05:43 -0800429 vlsh = ldp_fd_to_vlsh (fd);
430 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500431 {
Florin Coras7baeb712019-01-04 17:05:43 -0800432 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500433 if (size < 0)
434 {
435 errno = -size;
436 size = -1;
437 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700438 }
439 else
440 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500441 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700442 }
443
Dave Wallace048b1d62018-01-03 22:24:41 -0500444 return size;
445}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700446
Dave Wallace048b1d62018-01-03 22:24:41 -0500447ssize_t
448writev (int fd, const struct iovec * iov, int iovcnt)
449{
Dave Wallace048b1d62018-01-03 22:24:41 -0500450 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800451 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500452 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500453
Florin Coras5f33d0d2021-06-02 21:22:21 -0700454 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500455
Florin Coras7baeb712019-01-04 17:05:43 -0800456 vlsh = ldp_fd_to_vlsh (fd);
457 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500458 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000459 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500460 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000461 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
462 if (rv < 0)
463 break;
464 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500465 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000466 total += rv;
467 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500468 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500469 }
470 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500471
Florin Coraseda1b8c2020-03-23 16:00:35 +0000472 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500473 {
474 errno = -rv;
475 size = -1;
476 }
477 else
478 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700479 }
480 else
481 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500482 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700483 }
484
Dave Wallace048b1d62018-01-03 22:24:41 -0500485 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700486}
487
Florin Coras0ab36f52020-05-26 19:45:45 +0000488static int
489fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700490{
Florin Coras7baeb712019-01-04 17:05:43 -0800491 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700492 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800493
494 vlsh = ldp_fd_to_vlsh (fd);
495 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
496 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700497 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500498 int flags = va_arg (ap, int);
499 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700500
Dave Wallace048b1d62018-01-03 22:24:41 -0500501 size = sizeof (flags);
502 rv = -EOPNOTSUPP;
503 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700504 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500505 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800506 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500507 break;
508
509 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800510 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500511 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800512 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700513 break;
Florin Coras173bae32018-11-16 18:56:28 -0800514 case F_SETFD:
515 /* TODO handle this */
516 LDBG (0, "F_SETFD ignored flags %u", flags);
517 rv = 0;
518 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700519 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500520 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700521 break;
522 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500523 if (rv < 0)
524 {
525 errno = -rv;
526 rv = -1;
527 }
528 }
529 else
530 {
Carl Smithe16707b2019-11-13 14:37:39 +1300531#ifdef HAVE_FCNTL64
532 rv = libc_vfcntl64 (fd, cmd, ap);
533#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500534 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300535#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700536 }
537
Florin Coras0ab36f52020-05-26 19:45:45 +0000538 return rv;
539}
540
541int
542fcntl (int fd, int cmd, ...)
543{
544 va_list ap;
545 int rv;
546
Florin Coras5f33d0d2021-06-02 21:22:21 -0700547 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000548
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 Coras5f33d0d2021-06-02 21:22:21 -0700562 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000563
Florin Corasd7586d52020-04-29 02:19:51 +0000564 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000565 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000566 va_end (ap);
567 return rv;
568}
569
570int
Dave Wallace048b1d62018-01-03 22:24:41 -0500571ioctl (int fd, unsigned long int cmd, ...)
572{
Florin Coras7baeb712019-01-04 17:05:43 -0800573 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500574 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800575 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500576
Florin Coras5f33d0d2021-06-02 21:22:21 -0700577 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500578
579 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500580
Florin Coras7baeb712019-01-04 17:05:43 -0800581 vlsh = ldp_fd_to_vlsh (fd);
582 if (vlsh != VLS_INVALID_HANDLE)
583 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500584 switch (cmd)
585 {
586 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800587 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500588 break;
589
590 case FIONBIO:
591 {
592 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
593 u32 size = sizeof (flags);
594
595 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
596 * non-blocking, the flags should be read here and merged
597 * with O_NONBLOCK.
598 */
Florin Coras7baeb712019-01-04 17:05:43 -0800599 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500600 }
601 break;
602
603 default:
604 rv = -EOPNOTSUPP;
605 break;
606 }
607 if (rv < 0)
608 {
609 errno = -rv;
610 rv = -1;
611 }
612 }
613 else
614 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500615 rv = libc_vioctl (fd, cmd, ap);
616 }
617
Dave Wallace048b1d62018-01-03 22:24:41 -0500618 va_end (ap);
619 return rv;
620}
621
Florin Coras294afe22019-01-07 17:49:17 -0800622always_inline void
623ldp_select_init_maps (fd_set * __restrict original,
624 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
625 clib_bitmap_t ** vclb, int nfds, u32 minbits,
626 u32 n_bytes, uword * si_bits, uword * libc_bits)
627{
628 uword si_bits_set, libc_bits_set;
629 vls_handle_t vlsh;
630 int fd;
631
632 clib_bitmap_validate (*vclb, minbits);
633 clib_bitmap_validate (*libcb, minbits);
634 clib_bitmap_validate (*resultb, minbits);
635 clib_memcpy_fast (*resultb, original, n_bytes);
636 memset (original, 0, n_bytes);
637
638 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100639 clib_bitmap_foreach (fd, *resultb) {
Florin Coras294afe22019-01-07 17:49:17 -0800640 if (fd > nfds)
641 break;
642 vlsh = ldp_fd_to_vlsh (fd);
643 if (vlsh == VLS_INVALID_HANDLE)
644 clib_bitmap_set_no_check (*libcb, fd, 1);
645 else
Florin Corascbce80a2020-04-20 01:32:38 +0000646 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100647 }
Florin Coras294afe22019-01-07 17:49:17 -0800648 /* *INDENT-ON* */
649
650 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
651 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000652 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800653
654 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
655 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
656}
657
658always_inline int
659ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
660{
661 vls_handle_t vlsh;
662 uword si;
663 int fd;
664
665 if (!libcb)
666 return 0;
667
668 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100669 clib_bitmap_foreach (si, vclb) {
Florin Coras294afe22019-01-07 17:49:17 -0800670 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000671 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800672 fd = ldp_vlsh_to_fd (vlsh);
673 if (PREDICT_FALSE (fd < 0))
674 {
675 errno = EBADFD;
676 return -1;
677 }
678 FD_SET (fd, libcb);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100679 }
Florin Coras294afe22019-01-07 17:49:17 -0800680 /* *INDENT-ON* */
681
682 return 0;
683}
684
685always_inline void
686ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
687{
688 uword fd;
689
Florin Coras78b5fa62019-02-21 20:04:15 -0800690 if (!libcb)
691 return;
692
Florin Coras294afe22019-01-07 17:49:17 -0800693 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100694 clib_bitmap_foreach (fd, result)
Florin Coras294afe22019-01-07 17:49:17 -0800695 FD_SET ((int)fd, libcb);
Florin Coras294afe22019-01-07 17:49:17 -0800696 /* *INDENT-ON* */
697}
698
Dave Wallace048b1d62018-01-03 22:24:41 -0500699int
Dave Wallace2a865272018-02-07 21:00:42 -0500700ldp_pselect (int nfds, fd_set * __restrict readfds,
701 fd_set * __restrict writefds,
702 fd_set * __restrict exceptfds,
703 const struct timespec *__restrict timeout,
704 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500705{
Florin Coras294afe22019-01-07 17:49:17 -0800706 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800707 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800708 struct timespec libc_tspec = { 0 };
709 f64 time_out, vcl_timeout = 0;
710 uword si_bits, libc_bits;
711 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500712
713 if (nfds < 0)
714 {
715 errno = EINVAL;
716 return -1;
717 }
718
Florin Coras4dee8cd2019-01-29 21:28:16 -0800719 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
720 clib_time_init (&ldpw->clib_time);
721
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500722 if (timeout)
723 {
724 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800725 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500726
727 /* select as fine grained sleep */
728 if (!nfds)
729 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800730 time_out += clib_time_now (&ldpw->clib_time);
731 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500732 ;
733 return 0;
734 }
735 }
736 else if (!nfds)
737 {
738 errno = EINVAL;
739 return -1;
740 }
741 else
742 time_out = -1;
743
Florin Coras7baeb712019-01-04 17:05:43 -0800744 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500745 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500746 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
747 timeout, sigmask);
748 goto done;
749 }
750
Florin Coras294afe22019-01-07 17:49:17 -0800751 si_bits = libc_bits = 0;
752 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800753
Dave Wallace048b1d62018-01-03 22:24:41 -0500754 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800755 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
756 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
757 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500758 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800759 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
760 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
761 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500762 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800763 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
764 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
765 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500766
Florin Coras294afe22019-01-07 17:49:17 -0800767 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500768 {
769 errno = EINVAL;
770 rv = -1;
771 goto done;
772 }
773
Florin Coras78b5fa62019-02-21 20:04:15 -0800774 if (!si_bits)
775 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800776
Dave Wallace048b1d62018-01-03 22:24:41 -0500777 do
778 {
Florin Coras294afe22019-01-07 17:49:17 -0800779 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500780 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500781 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800782 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000783 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500784 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500785 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800786 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000787 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500788 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500789 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800790 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000791 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500792 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800793
Florin Coras0ef8ef22019-01-18 08:37:13 -0800794 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
795 writefds ? ldpw->wr_bitmap : NULL,
796 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800797 if (rv < 0)
798 {
799 errno = -rv;
800 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000801 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800802 }
803 else if (rv > 0)
804 {
805 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
806 {
807 rv = -1;
808 goto done;
809 }
810
811 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
812 {
813 rv = -1;
814 goto done;
815 }
816
817 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
818 {
819 rv = -1;
820 goto done;
821 }
822 bits_set = rv;
823 }
824 }
825 if (libc_bits)
826 {
827 if (readfds)
828 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
829 vec_len (ldpw->libc_rd_bitmap) *
830 sizeof (clib_bitmap_t));
831 if (writefds)
832 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
833 vec_len (ldpw->libc_wr_bitmap) *
834 sizeof (clib_bitmap_t));
835 if (exceptfds)
836 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
837 vec_len (ldpw->libc_ex_bitmap) *
838 sizeof (clib_bitmap_t));
839
Dave Wallace048b1d62018-01-03 22:24:41 -0500840 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800841 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
842 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
843 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
844 &libc_tspec, sigmask);
845 if (rv > 0)
846 {
847 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
848 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
849 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
850 bits_set += rv;
851 }
852 }
853
854 if (bits_set)
855 {
856 rv = bits_set;
857 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500858 }
859 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800860 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500861 rv = 0;
862
863done:
864 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800865 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800866 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800867 clib_bitmap_zero (ldpw->libc_rd_bitmap);
868 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800869 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800870 clib_bitmap_zero (ldpw->libc_wr_bitmap);
871 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800872 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800873 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500874
Dave Wallace048b1d62018-01-03 22:24:41 -0500875 return rv;
876}
877
878int
879select (int nfds, fd_set * __restrict readfds,
880 fd_set * __restrict writefds,
881 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
882{
883 struct timespec tspec;
884
885 if (timeout)
886 {
887 tspec.tv_sec = timeout->tv_sec;
888 tspec.tv_nsec = timeout->tv_usec * 1000;
889 }
Dave Wallace2a865272018-02-07 21:00:42 -0500890 return ldp_pselect (nfds, readfds, writefds, exceptfds,
891 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500892}
893
894#ifdef __USE_XOPEN2K
895int
896pselect (int nfds, fd_set * __restrict readfds,
897 fd_set * __restrict writefds,
898 fd_set * __restrict exceptfds,
899 const struct timespec *__restrict timeout,
900 const __sigset_t * __restrict sigmask)
901{
Dave Wallace2a865272018-02-07 21:00:42 -0500902 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500903}
904#endif
905
Yu Ping7b74b072019-05-08 00:40:24 +0800906/* If transparent TLS mode is turned on, then ldp will load key and cert.
907 */
908static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800909load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800910{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800911 char *cert_str = getenv (LDP_ENV_TLS_CERT);
912 char *key_str = getenv (LDP_ENV_TLS_KEY);
913 char cert_buf[4096], key_buf[4096];
914 int cert_size, key_size;
915 vppcom_cert_key_pair_t crypto;
916 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800917 FILE *fp;
918
Florin Corasa5a9efd2021-01-05 17:03:29 -0800919 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800920 {
921 LDBG (0, "ERROR: failed to read LDP environment %s\n",
922 LDP_ENV_TLS_CERT);
923 return -1;
924 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800925
926 fp = fopen (cert_str, "r");
927 if (fp == NULL)
928 {
929 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
930 return -1;
931 }
932 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
933 fclose (fp);
934
935 fp = fopen (key_str, "r");
936 if (fp == NULL)
937 {
938 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
939 return -1;
940 }
941 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
942 fclose (fp);
943
944 crypto.cert = cert_buf;
945 crypto.key = key_buf;
946 crypto.cert_len = cert_size;
947 crypto.key_len = key_size;
948 ckp_index = vppcom_add_cert_key_pair (&crypto);
949 if (ckp_index < 0)
950 {
951 LDBG (0, "ERROR: failed to add cert key pair\n");
952 return -1;
953 }
954
955 ldp->ckpair_index = ckp_index;
956
Yu Ping7b74b072019-05-08 00:40:24 +0800957 return 0;
958}
959
960static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800961assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800962{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800963 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800964
Florin Corasa5a9efd2021-01-05 17:03:29 -0800965 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
966 return -1;
967
968 ckp_len = sizeof (ldp->ckpair_index);
969 return vppcom_session_attr (vlsh_to_session_index (vlsh),
970 VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index,
971 &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800972}
973
Dave Wallace048b1d62018-01-03 22:24:41 -0500974int
975socket (int domain, int type, int protocol)
976{
Florin Coras7baeb712019-01-04 17:05:43 -0800977 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500978 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800979 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500980
Florin Coras5f33d0d2021-06-02 21:22:21 -0700981 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500982
983 if (((domain == AF_INET) || (domain == AF_INET6)) &&
984 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
985 {
Yu Ping7b74b072019-05-08 00:40:24 +0800986 u8 proto;
987 if (ldp->transparent_tls)
988 {
989 proto = VPPCOM_PROTO_TLS;
990 }
991 else
992 proto = ((sock_type == SOCK_DGRAM) ?
993 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500994
Florin Coras7baeb712019-01-04 17:05:43 -0800995 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
996 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500997
Florin Coras7baeb712019-01-04 17:05:43 -0800998 vlsh = vls_create (proto, is_nonblocking);
999 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001000 {
Florin Coras7baeb712019-01-04 17:05:43 -08001001 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001002 rv = -1;
1003 }
1004 else
1005 {
Yu Ping7b74b072019-05-08 00:40:24 +08001006 if (ldp->transparent_tls)
1007 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001008 if (assign_cert_key_pair (vlsh) < 0)
1009 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001010 }
Florin Coras7baeb712019-01-04 17:05:43 -08001011 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001012 }
1013 }
1014 else
1015 {
Florin Coras7baeb712019-01-04 17:05:43 -08001016 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001017 rv = libc_socket (domain, type, protocol);
1018 }
1019
Dave Wallace048b1d62018-01-03 22:24:41 -05001020 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001021}
1022
1023/*
1024 * Create two new sockets, of type TYPE in domain DOMAIN and using
1025 * protocol PROTOCOL, which are connected to each other, and put file
1026 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1027 * one will be chosen automatically.
1028 * Returns 0 on success, -1 for errors.
1029 * */
1030int
Dave Wallace048b1d62018-01-03 22:24:41 -05001031socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001032{
Florin Coras7baeb712019-01-04 17:05:43 -08001033 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001034
Florin Coras5f33d0d2021-06-02 21:22:21 -07001035 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001036
1037 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1038 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001039 {
Florin Coras7baeb712019-01-04 17:05:43 -08001040 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001041 errno = ENOSYS;
1042 rv = -1;
1043 }
1044 else
1045 {
Florin Coras7baeb712019-01-04 17:05:43 -08001046 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001047 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001048 }
1049
Dave Wallace048b1d62018-01-03 22:24:41 -05001050 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001051}
1052
1053int
Dave Wallace048b1d62018-01-03 22:24:41 -05001054bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001055{
Florin Coras7baeb712019-01-04 17:05:43 -08001056 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001057 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001058
Florin Coras5f33d0d2021-06-02 21:22:21 -07001059 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001060
Florin Coras7baeb712019-01-04 17:05:43 -08001061 vlsh = ldp_fd_to_vlsh (fd);
1062 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001063 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001064 vppcom_endpt_t ep;
1065
Dave Wallace048b1d62018-01-03 22:24:41 -05001066 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001067 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001068 case AF_INET:
1069 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001070 {
Florin Coras7baeb712019-01-04 17:05:43 -08001071 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1072 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001073 errno = EINVAL;
1074 rv = -1;
1075 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001076 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001077 ep.is_ip4 = VPPCOM_IS_IP4;
1078 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1079 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1080 break;
1081
1082 case AF_INET6:
1083 if (len != sizeof (struct sockaddr_in6))
1084 {
Florin Coras7baeb712019-01-04 17:05:43 -08001085 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1086 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001087 errno = EINVAL;
1088 rv = -1;
1089 goto done;
1090 }
1091 ep.is_ip4 = VPPCOM_IS_IP6;
1092 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1093 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001094 break;
1095
1096 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001097 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1098 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001099 errno = EAFNOSUPPORT;
1100 rv = -1;
1101 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001102 }
Florin Coras7baeb712019-01-04 17:05:43 -08001103 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1104 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001105
Florin Coras7baeb712019-01-04 17:05:43 -08001106 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001107 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001108 {
1109 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001110 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001111 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001112 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001113 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001114 {
Florin Coras7baeb712019-01-04 17:05:43 -08001115 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001116 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001117 }
1118
Dave Wallace048b1d62018-01-03 22:24:41 -05001119done:
Florin Coras7baeb712019-01-04 17:05:43 -08001120 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001121
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001122 return rv;
1123}
1124
1125static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001126ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1127 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001128{
Dave Wallace048b1d62018-01-03 22:24:41 -05001129 int rv = 0;
1130 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001131
Florin Coras5f33d0d2021-06-02 21:22:21 -07001132 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001133
1134 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001135 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001136 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1137 switch (addr->sa_family)
1138 {
1139 case AF_INET:
1140 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1141 if (*len > sizeof (struct sockaddr_in))
1142 *len = sizeof (struct sockaddr_in);
1143 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1144 copy_len = *len - sa_len;
1145 if (copy_len > 0)
1146 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1147 copy_len);
1148 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001149
Dave Wallace048b1d62018-01-03 22:24:41 -05001150 case AF_INET6:
1151 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1152 if (*len > sizeof (struct sockaddr_in6))
1153 *len = sizeof (struct sockaddr_in6);
1154 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1155 copy_len = *len - sa_len;
1156 if (copy_len > 0)
1157 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1158 __in6_u.__u6_addr8, ep->ip, copy_len);
1159 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001160
Dave Wallace048b1d62018-01-03 22:24:41 -05001161 default:
1162 /* Not possible */
1163 rv = -EAFNOSUPPORT;
1164 break;
1165 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001166 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001167 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001168}
1169
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001170int
Dave Wallace048b1d62018-01-03 22:24:41 -05001171getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001172{
Florin Coras7baeb712019-01-04 17:05:43 -08001173 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001174 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001175
Florin Coras5f33d0d2021-06-02 21:22:21 -07001176 ldp_init_check ();
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001177
Florin Coras7baeb712019-01-04 17:05:43 -08001178 vlsh = ldp_fd_to_vlsh (fd);
1179 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001180 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001181 vppcom_endpt_t ep;
1182 u8 addr_buf[sizeof (struct in6_addr)];
1183 u32 size = sizeof (ep);
1184
1185 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001186
Florin Coras7baeb712019-01-04 17:05:43 -08001187 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001188 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001189 {
1190 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001191 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001192 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001193 else
1194 {
Dave Wallace2a865272018-02-07 21:00:42 -05001195 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001196 if (rv != VPPCOM_OK)
1197 {
1198 errno = -rv;
1199 rv = -1;
1200 }
1201 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001202 }
1203 else
1204 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001205 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001206 }
1207
Dave Wallace048b1d62018-01-03 22:24:41 -05001208 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001209}
1210
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001211int
Dave Wallace048b1d62018-01-03 22:24:41 -05001212connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001213{
Florin Coras7baeb712019-01-04 17:05:43 -08001214 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001215 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001216
Florin Coras5f33d0d2021-06-02 21:22:21 -07001217 ldp_init_check ();
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001218
Dave Wallace048b1d62018-01-03 22:24:41 -05001219 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001220 {
Florin Coras7baeb712019-01-04 17:05:43 -08001221 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001222 errno = EINVAL;
1223 rv = -1;
1224 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001225 }
1226
Florin Coras7baeb712019-01-04 17:05:43 -08001227 vlsh = ldp_fd_to_vlsh (fd);
1228 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001229 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001230 vppcom_endpt_t ep;
1231
Dave Wallace048b1d62018-01-03 22:24:41 -05001232 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001233 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001234 case AF_INET:
1235 if (len != sizeof (struct sockaddr_in))
1236 {
Florin Coras7baeb712019-01-04 17:05:43 -08001237 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1238 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001239 errno = EINVAL;
1240 rv = -1;
1241 goto done;
1242 }
1243 ep.is_ip4 = VPPCOM_IS_IP4;
1244 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1245 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1246 break;
1247
1248 case AF_INET6:
1249 if (len != sizeof (struct sockaddr_in6))
1250 {
Florin Coras7baeb712019-01-04 17:05:43 -08001251 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1252 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001253 errno = EINVAL;
1254 rv = -1;
1255 goto done;
1256 }
1257 ep.is_ip4 = VPPCOM_IS_IP6;
1258 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1259 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1260 break;
1261
1262 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001263 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1264 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001265 errno = EAFNOSUPPORT;
1266 rv = -1;
1267 goto done;
1268 }
Florin Coras7baeb712019-01-04 17:05:43 -08001269 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1270 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001271
Florin Coras7baeb712019-01-04 17:05:43 -08001272 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001273 if (rv != VPPCOM_OK)
1274 {
1275 errno = -rv;
1276 rv = -1;
1277 }
1278 }
1279 else
1280 {
Florin Coras7baeb712019-01-04 17:05:43 -08001281 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1282 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001283
1284 rv = libc_connect (fd, addr, len);
1285 }
1286
1287done:
Florin Coras7baeb712019-01-04 17:05:43 -08001288 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001289 return rv;
1290}
1291
1292int
1293getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1294{
Florin Coras7baeb712019-01-04 17:05:43 -08001295 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001296 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001297
Florin Coras5f33d0d2021-06-02 21:22:21 -07001298 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001299
Florin Coras7baeb712019-01-04 17:05:43 -08001300 vlsh = ldp_fd_to_vlsh (fd);
1301 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001302 {
1303 vppcom_endpt_t ep;
1304 u8 addr_buf[sizeof (struct in6_addr)];
1305 u32 size = sizeof (ep);
1306
1307 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001308 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001309 if (rv != VPPCOM_OK)
1310 {
1311 errno = -rv;
1312 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001313 }
1314 else
1315 {
Dave Wallace2a865272018-02-07 21:00:42 -05001316 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001317 if (rv != VPPCOM_OK)
1318 {
1319 errno = -rv;
1320 rv = -1;
1321 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001322 }
1323 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001324 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001325 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001326 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001327 }
1328
Dave Wallace048b1d62018-01-03 22:24:41 -05001329 return rv;
1330}
1331
1332ssize_t
1333send (int fd, const void *buf, size_t n, int flags)
1334{
Florin Coras7baeb712019-01-04 17:05:43 -08001335 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001336 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001337
Florin Coras5f33d0d2021-06-02 21:22:21 -07001338 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001339
Florin Coras7baeb712019-01-04 17:05:43 -08001340 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001341 {
Florin Coras7baeb712019-01-04 17:05:43 -08001342 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001343 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001344 {
1345 errno = -size;
1346 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001347 }
1348 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001349 else
1350 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001351 size = libc_send (fd, buf, n, flags);
1352 }
1353
Dave Wallace048b1d62018-01-03 22:24:41 -05001354 return size;
1355}
1356
1357ssize_t
1358sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1359{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001360 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001361 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001362 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001363
Florin Coras5f33d0d2021-06-02 21:22:21 -07001364 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001365
Florin Coras7baeb712019-01-04 17:05:43 -08001366 vlsh = ldp_fd_to_vlsh (out_fd);
1367 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001368 {
1369 int rv;
1370 ssize_t results = 0;
1371 size_t n_bytes_left = len;
1372 size_t bytes_to_read;
1373 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001374 u8 eagain = 0;
1375 u32 flags, flags_len = sizeof (flags);
1376
Florin Coras7baeb712019-01-04 17:05:43 -08001377 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001378 if (PREDICT_FALSE (rv != VPPCOM_OK))
1379 {
Florin Coras7baeb712019-01-04 17:05:43 -08001380 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1381 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001382
Florin Corasdfe4cf42018-11-28 22:13:45 -08001383 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001384 errno = -rv;
1385 size = -1;
1386 goto done;
1387 }
1388
1389 if (offset)
1390 {
1391 off_t off = lseek (in_fd, *offset, SEEK_SET);
1392 if (PREDICT_FALSE (off == -1))
1393 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001394 size = -1;
1395 goto done;
1396 }
1397
1398 ASSERT (off == *offset);
1399 }
1400
1401 do
1402 {
Florin Coras7baeb712019-01-04 17:05:43 -08001403 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001404 if (size < 0)
1405 {
Florin Coraseb801d02020-09-16 17:44:58 -07001406 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001407 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001408 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 errno = -size;
1410 size = -1;
1411 goto done;
1412 }
1413
1414 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001415 if (bytes_to_read == 0)
1416 {
1417 if (flags & O_NONBLOCK)
1418 {
1419 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001420 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001421 goto update_offset;
1422 }
1423 else
1424 continue;
1425 }
1426 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001427 vec_validate (ldpw->io_buffer, bytes_to_read);
1428 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001429 if (nbytes < 0)
1430 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001431 if (results == 0)
1432 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001433 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001434 size = -1;
1435 goto done;
1436 }
1437 goto update_offset;
1438 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001439
Florin Coras7baeb712019-01-04 17:05:43 -08001440 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001441 if (size < 0)
1442 {
1443 if (size == VPPCOM_EAGAIN)
1444 {
1445 if (flags & O_NONBLOCK)
1446 {
1447 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001448 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001449 goto update_offset;
1450 }
1451 else
1452 continue;
1453 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001454 if (results == 0)
1455 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001456 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001457 errno = -size;
1458 size = -1;
1459 goto done;
1460 }
1461 goto update_offset;
1462 }
1463
1464 results += nbytes;
1465 ASSERT (n_bytes_left >= nbytes);
1466 n_bytes_left = n_bytes_left - nbytes;
1467 }
1468 while (n_bytes_left > 0);
1469
1470 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001471 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001472 if (offset)
1473 {
1474 off_t off = lseek (in_fd, *offset, SEEK_SET);
1475 if (PREDICT_FALSE (off == -1))
1476 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001477 size = -1;
1478 goto done;
1479 }
1480
1481 ASSERT (off == *offset);
1482 *offset += results + 1;
1483 }
1484 if (eagain)
1485 {
1486 errno = EAGAIN;
1487 size = -1;
1488 }
1489 else
1490 size = results;
1491 }
1492 else
1493 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001494 size = libc_sendfile (out_fd, in_fd, offset, len);
1495 }
1496
1497done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001498 return size;
1499}
1500
1501ssize_t
1502sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1503{
1504 return sendfile (out_fd, in_fd, offset, len);
1505}
1506
1507ssize_t
1508recv (int fd, void *buf, size_t n, int flags)
1509{
Florin Coras7baeb712019-01-04 17:05:43 -08001510 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001511 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001512
Florin Coras5f33d0d2021-06-02 21:22:21 -07001513 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001514
Florin Coras7baeb712019-01-04 17:05:43 -08001515 vlsh = ldp_fd_to_vlsh (fd);
1516 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001517 {
Florin Coras7baeb712019-01-04 17:05:43 -08001518 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001519 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001520 {
1521 errno = -size;
1522 size = -1;
1523 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001524 }
1525 else
1526 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001527 size = libc_recv (fd, buf, n, flags);
1528 }
1529
Dave Wallace048b1d62018-01-03 22:24:41 -05001530 return size;
1531}
1532
Florin Corasce17f462020-05-22 20:36:29 +00001533static int
1534ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1535 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1536{
1537 vppcom_endpt_t *ep = 0;
1538 vppcom_endpt_t _ep;
1539
1540 if (addr)
1541 {
1542 ep = &_ep;
1543 switch (addr->sa_family)
1544 {
1545 case AF_INET:
1546 ep->is_ip4 = VPPCOM_IS_IP4;
1547 ep->ip =
1548 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1549 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1550 break;
1551
1552 case AF_INET6:
1553 ep->is_ip4 = VPPCOM_IS_IP6;
1554 ep->ip =
1555 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1556 ep->port =
1557 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1558 break;
1559
1560 default:
1561 return EAFNOSUPPORT;
1562 }
1563 }
1564
1565 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1566}
1567
1568static int
1569ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1570 int flags, __SOCKADDR_ARG addr,
1571 socklen_t * __restrict addr_len)
1572{
1573 u8 src_addr[sizeof (struct sockaddr_in6)];
1574 vppcom_endpt_t ep;
1575 ssize_t size;
1576 int rv;
1577
1578 if (addr)
1579 {
1580 ep.ip = src_addr;
1581 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1582
1583 if (size > 0)
1584 {
1585 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1586 if (rv < 0)
1587 size = rv;
1588 }
1589 }
1590 else
1591 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1592
1593 return size;
1594}
1595
Dave Wallace048b1d62018-01-03 22:24:41 -05001596ssize_t
1597sendto (int fd, const void *buf, size_t n, int flags,
1598 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1599{
Florin Coras7baeb712019-01-04 17:05:43 -08001600 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001601 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001602
Florin Coras5f33d0d2021-06-02 21:22:21 -07001603 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001604
Florin Coras7baeb712019-01-04 17:05:43 -08001605 vlsh = ldp_fd_to_vlsh (fd);
1606 if (vlsh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001607 {
Florin Corasce17f462020-05-22 20:36:29 +00001608 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001609 if (size < 0)
1610 {
1611 errno = -size;
1612 size = -1;
1613 }
1614 }
1615 else
1616 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001617 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1618 }
1619
Dave Wallace048b1d62018-01-03 22:24:41 -05001620 return size;
1621}
1622
1623ssize_t
1624recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1625 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1626{
Florin Corasce17f462020-05-22 20:36:29 +00001627 vls_handle_t vlsh;
1628 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001629
Florin Coras5f33d0d2021-06-02 21:22:21 -07001630 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001631
Florin Corasce17f462020-05-22 20:36:29 +00001632 vlsh = ldp_fd_to_vlsh (fd);
1633 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001634 {
Florin Corasce17f462020-05-22 20:36:29 +00001635 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001636 if (size < 0)
1637 {
1638 errno = -size;
1639 size = -1;
1640 }
1641 }
1642 else
1643 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001644 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1645 }
1646
Dave Wallace048b1d62018-01-03 22:24:41 -05001647 return size;
1648}
1649
1650ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001651sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001652{
Florin Coras7baeb712019-01-04 17:05:43 -08001653 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001654 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001655
Florin Coras5f33d0d2021-06-02 21:22:21 -07001656 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001657
Florin Coras7baeb712019-01-04 17:05:43 -08001658 vlsh = ldp_fd_to_vlsh (fd);
1659 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001660 {
Florin Corasce17f462020-05-22 20:36:29 +00001661 struct iovec *iov = msg->msg_iov;
1662 ssize_t total = 0;
1663 int i, rv;
1664
1665 for (i = 0; i < msg->msg_iovlen; ++i)
1666 {
1667 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1668 msg->msg_name, msg->msg_namelen);
1669 if (rv < 0)
1670 break;
1671 else
1672 {
1673 total += rv;
1674 if (rv < iov[i].iov_len)
1675 break;
1676 }
1677 }
1678
1679 if (rv < 0 && total == 0)
1680 {
1681 errno = -rv;
1682 size = -1;
1683 }
1684 else
1685 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001686 }
1687 else
1688 {
Florin Corasce17f462020-05-22 20:36:29 +00001689 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001690 }
1691
Dave Wallace048b1d62018-01-03 22:24:41 -05001692 return size;
1693}
1694
1695#ifdef USE_GNU
1696int
1697sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1698{
1699 ssize_t size;
1700 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001701 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001702
Florin Coras5f33d0d2021-06-02 21:22:21 -07001703 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001704
Florin Coras7baeb712019-01-04 17:05:43 -08001705 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001706 {
1707 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1708 errno = ENOSYS;
1709 size = -1;
1710 }
1711 else
1712 {
1713 func_str = "libc_sendmmsg";
1714
Dave Wallace2a865272018-02-07 21:00:42 -05001715 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001716 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1717 "vmessages %p, vlen %u, flags 0x%x",
1718 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1719
1720 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1721 }
1722
Dave Wallace2a865272018-02-07 21:00:42 -05001723 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001724 {
1725 if (size < 0)
1726 {
1727 int errno_val = errno;
1728 perror (func_str);
1729 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1730 "rv %d, errno = %d", getpid (), fd, fd,
1731 func_str, size, errno_val);
1732 errno = errno_val;
1733 }
1734 else
1735 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1736 getpid (), fd, fd, size, size);
1737 }
1738 return size;
1739}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001740#endif
1741
Dave Wallace048b1d62018-01-03 22:24:41 -05001742ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001743recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001744{
Florin Coras7baeb712019-01-04 17:05:43 -08001745 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001746 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001747
Florin Coras5f33d0d2021-06-02 21:22:21 -07001748 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001749
Florin Coras7baeb712019-01-04 17:05:43 -08001750 vlsh = ldp_fd_to_vlsh (fd);
1751 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001752 {
Florin Corasce17f462020-05-22 20:36:29 +00001753 struct iovec *iov = msg->msg_iov;
1754 ssize_t max_deq, total = 0;
1755 int i, rv;
1756
1757 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1758 if (!max_deq)
1759 return 0;
1760
1761 for (i = 0; i < msg->msg_iovlen; i++)
1762 {
1763 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1764 (i == 0 ? msg->msg_name : NULL),
1765 (i == 0 ? &msg->msg_namelen : NULL));
1766 if (rv <= 0)
1767 break;
1768 else
1769 {
1770 total += rv;
1771 if (rv < iov[i].iov_len)
1772 break;
1773 }
1774 if (total >= max_deq)
1775 break;
1776 }
1777
1778 if (rv < 0 && total == 0)
1779 {
1780 errno = -rv;
1781 size = -1;
1782 }
1783 else
1784 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001785 }
1786 else
1787 {
Florin Corasce17f462020-05-22 20:36:29 +00001788 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001789 }
1790
Dave Wallace048b1d62018-01-03 22:24:41 -05001791 return size;
1792}
1793
1794#ifdef USE_GNU
1795int
1796recvmmsg (int fd, struct mmsghdr *vmessages,
1797 unsigned int vlen, int flags, struct timespec *tmo)
1798{
1799 ssize_t size;
1800 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001801 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001802
Florin Coras5f33d0d2021-06-02 21:22:21 -07001803 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001804
Florin Coras7baeb712019-01-04 17:05:43 -08001805 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001806 {
1807 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1808 errno = ENOSYS;
1809 size = -1;
1810 }
1811 else
1812 {
1813 func_str = "libc_recvmmsg";
1814
Dave Wallace2a865272018-02-07 21:00:42 -05001815 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001816 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1817 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1818 getpid (), fd, fd, func_str, vmessages, vlen,
1819 flags, tmo);
1820
1821 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1822 }
1823
Dave Wallace2a865272018-02-07 21:00:42 -05001824 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001825 {
1826 if (size < 0)
1827 {
1828 int errno_val = errno;
1829 perror (func_str);
1830 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1831 "rv %d, errno = %d", getpid (), fd, fd,
1832 func_str, size, errno_val);
1833 errno = errno_val;
1834 }
1835 else
1836 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1837 getpid (), fd, fd, size, size);
1838 }
1839 return size;
1840}
1841#endif
1842
1843int
1844getsockopt (int fd, int level, int optname,
1845 void *__restrict optval, socklen_t * __restrict optlen)
1846{
Florin Coras7baeb712019-01-04 17:05:43 -08001847 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001848 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001849
Florin Coras5f33d0d2021-06-02 21:22:21 -07001850 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001851
Florin Coras7baeb712019-01-04 17:05:43 -08001852 vlsh = ldp_fd_to_vlsh (fd);
1853 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001854 {
1855 rv = -EOPNOTSUPP;
1856
1857 switch (level)
1858 {
1859 case SOL_TCP:
1860 switch (optname)
1861 {
1862 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001863 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1864 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001865 break;
1866 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001867 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1868 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001869 break;
1870 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001871 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1872 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001873 break;
1874 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001875 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1876 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001877 break;
1878 case TCP_INFO:
1879 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1880 {
Florin Coras7baeb712019-01-04 17:05:43 -08001881 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1882 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001883 memset (optval, 0, *optlen);
1884 rv = VPPCOM_OK;
1885 }
1886 else
1887 rv = -EFAULT;
1888 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001889 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001890 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001891 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001892 rv = 0;
1893 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001894 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001895 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1896 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001897 break;
1898 }
1899 break;
1900 case SOL_IPV6:
1901 switch (optname)
1902 {
1903 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001904 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001905 break;
1906 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001907 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1908 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001909 break;
1910 }
1911 break;
1912 case SOL_SOCKET:
1913 switch (optname)
1914 {
1915 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001916 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001917 break;
1918 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001919 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001920 break;
1921 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001922 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001923 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1924 break;
1925 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001926 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1927 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001928 break;
1929 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001930 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1931 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001932 break;
1933 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001934 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001935 break;
wanghanlin0674f852021-02-22 10:38:36 +08001936 case SO_REUSEPORT:
1937 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1938 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001939 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001940 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001941 break;
wanghanlin0674f852021-02-22 10:38:36 +08001942 case SO_DOMAIN:
1943 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1944 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001945 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001946 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001947 break;
Florin Coras8eb8d502021-06-16 14:46:57 -07001948 case SO_BINDTODEVICE:
1949 rv = 0;
1950 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001951 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001952 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1953 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001954 break;
1955 }
1956 break;
1957 default:
1958 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001959 }
1960
Dave Wallace048b1d62018-01-03 22:24:41 -05001961 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001962 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001963 errno = -rv;
1964 rv = -1;
1965 }
1966 }
1967 else
1968 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001969 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1970 }
1971
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 return rv;
1973}
1974
1975int
1976setsockopt (int fd, int level, int optname,
1977 const void *optval, socklen_t optlen)
1978{
Florin Coras7baeb712019-01-04 17:05:43 -08001979 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001980 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001981
Florin Coras5f33d0d2021-06-02 21:22:21 -07001982 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001983
Florin Coras7baeb712019-01-04 17:05:43 -08001984 vlsh = ldp_fd_to_vlsh (fd);
1985 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001986 {
1987 rv = -EOPNOTSUPP;
1988
1989 switch (level)
1990 {
1991 case SOL_TCP:
1992 switch (optname)
1993 {
1994 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001995 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1996 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001997 break;
1998 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001999 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2000 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002001 break;
2002 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002003 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2004 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002005 break;
2006 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002007 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2008 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002009 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002010 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002011 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002012 /* Ignore */
2013 rv = 0;
2014 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002015 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002016 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2017 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002018 break;
2019 }
2020 break;
2021 case SOL_IPV6:
2022 switch (optname)
2023 {
2024 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002025 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2026 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002027 break;
2028 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002029 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2030 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002031 break;
2032 }
2033 break;
2034 case SOL_SOCKET:
2035 switch (optname)
2036 {
2037 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002038 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2039 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002040 break;
2041 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002042 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2043 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002044 break;
wanghanlin0674f852021-02-22 10:38:36 +08002045 case SO_REUSEPORT:
2046 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2047 &optlen);
2048 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002049 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002050 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2051 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002052 break;
Florin Coras2f647902021-06-02 08:23:50 -07002053 case SO_LINGER:
2054 rv = 0;
2055 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002056 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002057 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2058 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002059 break;
2060 }
2061 break;
2062 default:
2063 break;
2064 }
2065
2066 if (rv != VPPCOM_OK)
2067 {
2068 errno = -rv;
2069 rv = -1;
2070 }
2071 }
2072 else
2073 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002074 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2075 }
2076
Dave Wallace048b1d62018-01-03 22:24:41 -05002077 return rv;
2078}
2079
2080int
2081listen (int fd, int n)
2082{
Florin Coras7baeb712019-01-04 17:05:43 -08002083 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002084 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002085
Florin Coras5f33d0d2021-06-02 21:22:21 -07002086 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002087
Florin Coras7baeb712019-01-04 17:05:43 -08002088 vlsh = ldp_fd_to_vlsh (fd);
2089 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002090 {
Florin Coras7baeb712019-01-04 17:05:43 -08002091 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002092
Florin Coras7baeb712019-01-04 17:05:43 -08002093 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002094 if (rv != VPPCOM_OK)
2095 {
2096 errno = -rv;
2097 rv = -1;
2098 }
2099 }
2100 else
2101 {
Florin Coras7baeb712019-01-04 17:05:43 -08002102 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002103 rv = libc_listen (fd, n);
2104 }
2105
Florin Coras7baeb712019-01-04 17:05:43 -08002106 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002107 return rv;
2108}
2109
2110static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002111ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2112 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002113{
Florin Coras7baeb712019-01-04 17:05:43 -08002114 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002116
Florin Coras5f33d0d2021-06-02 21:22:21 -07002117 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002118
Florin Coras7baeb712019-01-04 17:05:43 -08002119 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2120 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 {
2122 vppcom_endpt_t ep;
2123 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002124 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 ep.ip = src_addr;
2126
Florin Coras7baeb712019-01-04 17:05:43 -08002127 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002128 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002129
Florin Coras7baeb712019-01-04 17:05:43 -08002130 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2131 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002132 {
Florin Coras7baeb712019-01-04 17:05:43 -08002133 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002134 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002135 }
2136 else
2137 {
Dave Wallace2a865272018-02-07 21:00:42 -05002138 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002139 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002140 {
Florin Coras7baeb712019-01-04 17:05:43 -08002141 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002142 errno = -rv;
2143 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002144 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002145 else
2146 {
Florin Coras7baeb712019-01-04 17:05:43 -08002147 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002148 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002149 }
2150 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002151 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002152 {
Florin Coras7baeb712019-01-04 17:05:43 -08002153 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2154 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002155
Dave Wallace048b1d62018-01-03 22:24:41 -05002156 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002157 }
2158
Florin Coras7baeb712019-01-04 17:05:43 -08002159 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002160
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002161 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002162}
2163
Dave Wallace048b1d62018-01-03 22:24:41 -05002164int
2165accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2166 int flags)
2167{
Dave Wallace2a865272018-02-07 21:00:42 -05002168 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002169}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002170
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002171int
Dave Wallace048b1d62018-01-03 22:24:41 -05002172accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002173{
Dave Wallace2a865272018-02-07 21:00:42 -05002174 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002175}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002176
Dave Wallace048b1d62018-01-03 22:24:41 -05002177int
2178shutdown (int fd, int how)
2179{
Florin Coras7baeb712019-01-04 17:05:43 -08002180 vls_handle_t vlsh;
liuyacan55c952e2021-06-13 14:54:55 +08002181 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002182
Florin Coras5f33d0d2021-06-02 21:22:21 -07002183 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002184
Florin Coras7baeb712019-01-04 17:05:43 -08002185 vlsh = ldp_fd_to_vlsh (fd);
2186 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002187 {
Florin Coras7baeb712019-01-04 17:05:43 -08002188 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
liuyacan55c952e2021-06-13 14:54:55 +08002189 rv = vls_shutdown (vlsh, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002190 }
2191 else
2192 {
Florin Coras7baeb712019-01-04 17:05:43 -08002193 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002194 rv = libc_shutdown (fd, how);
2195 }
2196
Dave Wallace048b1d62018-01-03 22:24:41 -05002197 return rv;
2198}
2199
2200int
2201epoll_create1 (int flags)
2202{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002203 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002204 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002205 int rv;
2206
Florin Coras5f33d0d2021-06-02 21:22:21 -07002207 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002208
hanlina3a48962020-07-13 11:09:15 +08002209 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002210 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002211 /* Make sure workers have been allocated */
2212 if (!ldp->workers)
2213 {
2214 ldp_alloc_workers ();
2215 ldpw = ldp_worker_get_current ();
2216 }
Florin Coras99368312018-08-02 10:45:44 -07002217 rv = libc_epoll_create1 (flags);
2218 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002219 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002220 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002221 return rv;
2222 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002223
Florin Coras7baeb712019-01-04 17:05:43 -08002224 vlsh = vls_epoll_create ();
2225 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002226 {
Florin Coras7baeb712019-01-04 17:05:43 -08002227 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002228 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002229 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002230 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002231 {
Florin Coras7baeb712019-01-04 17:05:43 -08002232 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002233 }
Florin Coras7baeb712019-01-04 17:05:43 -08002234 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002235 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002236}
2237
2238int
Dave Wallace048b1d62018-01-03 22:24:41 -05002239epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002240{
Dave Wallace048b1d62018-01-03 22:24:41 -05002241 return epoll_create1 (0);
2242}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002243
Dave Wallace048b1d62018-01-03 22:24:41 -05002244int
2245epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2246{
Florin Coras7baeb712019-01-04 17:05:43 -08002247 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002248 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002249
Florin Coras5f33d0d2021-06-02 21:22:21 -07002250 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002251
Florin Coras7baeb712019-01-04 17:05:43 -08002252 vep_vlsh = ldp_fd_to_vlsh (epfd);
2253 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002254 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002255 /* The LDP epoll_create1 always creates VCL epfd's.
2256 * The app should never have a kernel base epoll fd unless it
2257 * was acquired outside of the LD_PRELOAD process context.
2258 * In any case, if we get one, punt it to libc_epoll_ctl.
2259 */
Florin Coras7baeb712019-01-04 17:05:43 -08002260 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2261 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002262
2263 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002264 goto done;
2265 }
2266
Florin Coras7baeb712019-01-04 17:05:43 -08002267 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002268
Florin Coras7baeb712019-01-04 17:05:43 -08002269 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2270 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002271
Florin Coras7baeb712019-01-04 17:05:43 -08002272 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002273 {
Florin Coras7baeb712019-01-04 17:05:43 -08002274 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002275 " event %p", epfd, vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002276
Florin Coras7baeb712019-01-04 17:05:43 -08002277 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002278 if (rv != VPPCOM_OK)
2279 {
2280 errno = -rv;
2281 rv = -1;
2282 }
2283 }
2284 else
2285 {
2286 int libc_epfd;
2287 u32 size = sizeof (epfd);
2288
Florin Coras7baeb712019-01-04 17:05:43 -08002289 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002290 if (!libc_epfd)
2291 {
Florin Coras7baeb712019-01-04 17:05:43 -08002292 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2293 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002294
2295 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2296 if (libc_epfd < 0)
2297 {
2298 rv = libc_epfd;
2299 goto done;
2300 }
2301
Florin Coras7baeb712019-01-04 17:05:43 -08002302 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2303 &size);
Florin Coras99368312018-08-02 10:45:44 -07002304 if (rv < 0)
2305 {
2306 errno = -rv;
2307 rv = -1;
2308 goto done;
2309 }
2310 }
2311 else if (PREDICT_FALSE (libc_epfd < 0))
2312 {
2313 errno = -epfd;
2314 rv = -1;
2315 goto done;
2316 }
2317
Florin Coras7baeb712019-01-04 17:05:43 -08002318 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2319 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002320
2321 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002322 }
2323
2324done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002325 return rv;
2326}
Dave Wallace048b1d62018-01-03 22:24:41 -05002327
2328static inline int
Florin Coras99368312018-08-02 10:45:44 -07002329ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2330 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002331{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002332 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002333 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002334 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002335 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002336
Florin Coras5f33d0d2021-06-02 21:22:21 -07002337 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002338
2339 if (PREDICT_FALSE (!events || (timeout < -1)))
2340 {
2341 errno = EFAULT;
2342 return -1;
2343 }
2344
Florin Corasdfe4cf42018-11-28 22:13:45 -08002345 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002346 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2347
Florin Coras7baeb712019-01-04 17:05:43 -08002348 ep_vlsh = ldp_fd_to_vlsh (epfd);
2349 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002350 {
Florin Coras7baeb712019-01-04 17:05:43 -08002351 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002352 errno = EBADFD;
2353 return -1;
2354 }
2355
Florin Coras4dee8cd2019-01-29 21:28:16 -08002356 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2357 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002358 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002359 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002360
Florin Coras7baeb712019-01-04 17:05:43 -08002361 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002362 if (PREDICT_FALSE (libc_epfd < 0))
2363 {
2364 errno = -libc_epfd;
2365 rv = -1;
2366 goto done;
2367 }
2368
Florin Coras7baeb712019-01-04 17:05:43 -08002369 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2370 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002371 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002372 do
2373 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002374 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002375 {
Florin Coras7baeb712019-01-04 17:05:43 -08002376 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002377 if (rv > 0)
2378 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002379 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002380 goto done;
2381 }
2382 else if (rv < 0)
2383 {
2384 errno = -rv;
2385 rv = -1;
2386 goto done;
2387 }
2388 }
2389 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002390 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002391
2392 if (libc_epfd > 0)
2393 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002394 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002395 if (rv != 0)
2396 goto done;
2397 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002398 }
Florin Coras72f77822019-01-22 19:05:52 -08002399 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002400
2401done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002402 return rv;
2403}
2404
hanlin4266d4d2020-05-19 17:34:17 +08002405static inline int
2406ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2407 int maxevents, int timeout, const sigset_t * sigmask)
2408{
hanlina3a48962020-07-13 11:09:15 +08002409 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002410 int libc_epfd, rv = 0, num_ev;
2411 vls_handle_t ep_vlsh;
2412
Florin Coras5f33d0d2021-06-02 21:22:21 -07002413 ldp_init_check ();
hanlin4266d4d2020-05-19 17:34:17 +08002414
2415 if (PREDICT_FALSE (!events || (timeout < -1)))
2416 {
2417 errno = EFAULT;
2418 return -1;
2419 }
2420
Florin Corasff40d8f2020-08-11 22:05:28 -07002421 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2422 * one thread but it is now used on another */
2423 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2424 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002425
2426 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002427 if (epfd == ldpw->vcl_mq_epfd)
2428 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2429
2430 ep_vlsh = ldp_fd_to_vlsh (epfd);
2431 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2432 {
2433 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2434 errno = EBADFD;
2435 return -1;
2436 }
2437
2438 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2439 if (PREDICT_FALSE (!libc_epfd))
2440 {
2441 u32 size = sizeof (epfd);
2442
2443 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2444 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2445 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2446 if (libc_epfd < 0)
2447 {
2448 rv = libc_epfd;
2449 goto done;
2450 }
2451
2452 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2453 if (rv < 0)
2454 {
2455 errno = -rv;
2456 rv = -1;
2457 goto done;
2458 }
2459 }
2460 if (PREDICT_FALSE (libc_epfd <= 0))
2461 {
2462 errno = -libc_epfd;
2463 rv = -1;
2464 goto done;
2465 }
2466
2467 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2468 {
2469 struct epoll_event e = { 0 };
2470 e.events = EPOLLIN;
2471 e.data.fd = ldpw->vcl_mq_epfd;
2472 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2473 0)
2474 {
2475 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2476 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2477 rv = -1;
2478 goto done;
2479 }
2480 ldpw->mq_epfd_added = 1;
2481 }
2482
wanghanlin8919fec2021-03-18 20:00:41 +08002483 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2484 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002485 if (rv > 0)
2486 goto done;
hanlina3a48962020-07-13 11:09:15 +08002487 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002488 {
2489 errno = -rv;
2490 rv = -1;
2491 goto done;
2492 }
2493
2494 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2495 if (rv <= 0)
2496 goto done;
2497 for (int i = 0; i < rv; i++)
2498 {
2499 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2500 {
2501 /* We should remove mq epoll fd from events. */
2502 rv--;
2503 if (i != rv)
2504 {
2505 events[i].events = events[rv].events;
2506 events[i].data.u64 = events[rv].data.u64;
2507 }
2508 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2509 if (PREDICT_TRUE (num_ev > 0))
2510 rv += num_ev;
2511 break;
2512 }
2513 }
2514
2515done:
2516 return rv;
2517}
2518
Dave Wallace048b1d62018-01-03 22:24:41 -05002519int
2520epoll_pwait (int epfd, struct epoll_event *events,
2521 int maxevents, int timeout, const sigset_t * sigmask)
2522{
hanlin4266d4d2020-05-19 17:34:17 +08002523 if (vls_use_eventfd ())
2524 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2525 sigmask);
2526 else
2527 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002528}
2529
2530int
2531epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2532{
hanlin4266d4d2020-05-19 17:34:17 +08002533 if (vls_use_eventfd ())
2534 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2535 else
2536 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002537}
2538
2539int
2540poll (struct pollfd *fds, nfds_t nfds, int timeout)
2541{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002542 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002543 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002544 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002545 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002546 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002547
Florin Coraseb801d02020-09-16 17:44:58 -07002548 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002549
Florin Coras4dee8cd2019-01-29 21:28:16 -08002550 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2551 clib_time_init (&ldpw->clib_time);
2552
2553 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2554 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002555
Dave Wallace048b1d62018-01-03 22:24:41 -05002556 for (i = 0; i < nfds; i++)
2557 {
Florin Coras6917b942018-11-13 22:44:54 -08002558 if (fds[i].fd < 0)
2559 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002560
Florin Coras7baeb712019-01-04 17:05:43 -08002561 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2562 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002563 {
2564 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002565 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002566 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002567 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002568 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002569#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002570 if (fds[i].events & POLLRDNORM)
2571 vp->events |= POLLIN;
2572 if (fds[i].events & POLLWRNORM)
2573 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002574#endif
Florin Coras6917b942018-11-13 22:44:54 -08002575 vp->revents = fds[i].revents;
2576 }
2577 else
2578 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002579 vec_add1 (ldpw->libc_poll, fds[i]);
2580 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002581 }
2582 }
2583
Dave Wallace048b1d62018-01-03 22:24:41 -05002584 do
2585 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002586 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002587 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002588 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002589 if (rv < 0)
2590 {
2591 errno = -rv;
2592 rv = -1;
2593 goto done;
2594 }
2595 else
2596 n_revents += rv;
2597 }
2598
Florin Corasdfe4cf42018-11-28 22:13:45 -08002599 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002600 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002601 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002602 if (rv < 0)
2603 goto done;
2604 else
2605 n_revents += rv;
2606 }
2607
2608 if (n_revents)
2609 {
2610 rv = n_revents;
2611 goto done;
2612 }
2613 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002614 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002615 rv = 0;
2616
2617done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002618 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002619 {
2620 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002621 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002622#ifdef __USE_XOPEN2K
2623 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2624 (fds[vp->fds_ndx].events & POLLRDNORM))
2625 fds[vp->fds_ndx].revents |= POLLRDNORM;
2626 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2627 (fds[vp->fds_ndx].events & POLLWRNORM))
2628 fds[vp->fds_ndx].revents |= POLLWRNORM;
2629#endif
2630 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002631 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002632
Florin Corasdfe4cf42018-11-28 22:13:45 -08002633 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002634 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002635 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002636 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002637 vec_reset_length (ldpw->libc_poll_idxs);
2638 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002639
Dave Wallace048b1d62018-01-03 22:24:41 -05002640 return rv;
2641}
2642
2643#ifdef USE_GNU
2644int
2645ppoll (struct pollfd *fds, nfds_t nfds,
2646 const struct timespec *timeout, const sigset_t * sigmask)
2647{
Florin Coras5f33d0d2021-06-02 21:22:21 -07002648 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002649
2650 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2651 errno = ENOSYS;
2652
2653
2654 return -1;
2655}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002656#endif
2657
Dave Wallace2a865272018-02-07 21:00:42 -05002658void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002659
Dave Wallace2a865272018-02-07 21:00:42 -05002660void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002661
Dave Wallace048b1d62018-01-03 22:24:41 -05002662/*
2663 * This function is called when the library is loaded
2664 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002665void
Dave Wallace2a865272018-02-07 21:00:42 -05002666ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002667{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002668 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002669 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002670 {
2671 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2672 getpid ());
2673 _exit (1);
2674 }
Dave Wallace69d01192018-02-22 16:22:09 -05002675 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002676 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002677}
2678
2679/*
2680 * This function is called when the library is unloaded
2681 */
2682void
Dave Wallace2a865272018-02-07 21:00:42 -05002683ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002684{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002685 /*
2686 swrap_destructor ();
2687 if (ldp->init)
2688 ldp->init = 0;
2689 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002690
2691 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002692 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002693 */
Dave Wallace69d01192018-02-22 16:22:09 -05002694 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002695 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2696 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002697}
2698
2699
2700/*
2701 * fd.io coding-style-patch-verification: ON
2702 *
2703 * Local Variables:
2704 * eval: (c-set-style "gnu")
2705 * End:
2706 */