blob: 5f1eeb9d540b41b6cbb1a376c6ec9dfa372c3ba5 [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
liuyacanf71796e2021-08-02 10:01:39 +0800727 time_out += clib_time_now (&ldpw->clib_time);
728
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500729 /* select as fine grained sleep */
730 if (!nfds)
731 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800732 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500733 ;
734 return 0;
735 }
736 }
737 else if (!nfds)
738 {
739 errno = EINVAL;
740 return -1;
741 }
742 else
743 time_out = -1;
744
Florin Coras7baeb712019-01-04 17:05:43 -0800745 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500746 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500747 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
748 timeout, sigmask);
749 goto done;
750 }
751
Florin Coras294afe22019-01-07 17:49:17 -0800752 si_bits = libc_bits = 0;
753 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800754
Dave Wallace048b1d62018-01-03 22:24:41 -0500755 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800756 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
757 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
758 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500759 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800760 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
761 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
762 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500763 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800764 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
765 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
766 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500767
Florin Coras294afe22019-01-07 17:49:17 -0800768 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500769 {
770 errno = EINVAL;
771 rv = -1;
772 goto done;
773 }
774
Florin Coras78b5fa62019-02-21 20:04:15 -0800775 if (!si_bits)
776 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800777
Dave Wallace048b1d62018-01-03 22:24:41 -0500778 do
779 {
Florin Coras294afe22019-01-07 17:49:17 -0800780 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500781 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800783 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000784 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500785 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500786 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800787 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000788 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500789 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500790 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800791 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000792 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500793 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800794
Florin Coras0ef8ef22019-01-18 08:37:13 -0800795 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
796 writefds ? ldpw->wr_bitmap : NULL,
797 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800798 if (rv < 0)
799 {
800 errno = -rv;
801 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000802 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800803 }
804 else if (rv > 0)
805 {
806 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
807 {
808 rv = -1;
809 goto done;
810 }
811
812 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
813 {
814 rv = -1;
815 goto done;
816 }
817
818 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
819 {
820 rv = -1;
821 goto done;
822 }
823 bits_set = rv;
824 }
825 }
826 if (libc_bits)
827 {
828 if (readfds)
829 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
830 vec_len (ldpw->libc_rd_bitmap) *
831 sizeof (clib_bitmap_t));
832 if (writefds)
833 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
834 vec_len (ldpw->libc_wr_bitmap) *
835 sizeof (clib_bitmap_t));
836 if (exceptfds)
837 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
838 vec_len (ldpw->libc_ex_bitmap) *
839 sizeof (clib_bitmap_t));
840
Dave Wallace048b1d62018-01-03 22:24:41 -0500841 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800842 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
843 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
844 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
845 &libc_tspec, sigmask);
846 if (rv > 0)
847 {
848 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
849 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
850 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
851 bits_set += rv;
852 }
853 }
854
855 if (bits_set)
856 {
857 rv = bits_set;
858 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500859 }
860 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800861 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500862 rv = 0;
863
864done:
865 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800866 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800867 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800868 clib_bitmap_zero (ldpw->libc_rd_bitmap);
869 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800870 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800871 clib_bitmap_zero (ldpw->libc_wr_bitmap);
872 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800873 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800874 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500875
Dave Wallace048b1d62018-01-03 22:24:41 -0500876 return rv;
877}
878
879int
880select (int nfds, fd_set * __restrict readfds,
881 fd_set * __restrict writefds,
882 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
883{
884 struct timespec tspec;
885
886 if (timeout)
887 {
888 tspec.tv_sec = timeout->tv_sec;
889 tspec.tv_nsec = timeout->tv_usec * 1000;
890 }
Dave Wallace2a865272018-02-07 21:00:42 -0500891 return ldp_pselect (nfds, readfds, writefds, exceptfds,
892 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500893}
894
895#ifdef __USE_XOPEN2K
896int
897pselect (int nfds, fd_set * __restrict readfds,
898 fd_set * __restrict writefds,
899 fd_set * __restrict exceptfds,
900 const struct timespec *__restrict timeout,
901 const __sigset_t * __restrict sigmask)
902{
Dave Wallace2a865272018-02-07 21:00:42 -0500903 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500904}
905#endif
906
Yu Ping7b74b072019-05-08 00:40:24 +0800907/* If transparent TLS mode is turned on, then ldp will load key and cert.
908 */
909static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800910load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800911{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800912 char *cert_str = getenv (LDP_ENV_TLS_CERT);
913 char *key_str = getenv (LDP_ENV_TLS_KEY);
914 char cert_buf[4096], key_buf[4096];
915 int cert_size, key_size;
916 vppcom_cert_key_pair_t crypto;
917 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800918 FILE *fp;
919
Florin Corasa5a9efd2021-01-05 17:03:29 -0800920 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800921 {
922 LDBG (0, "ERROR: failed to read LDP environment %s\n",
923 LDP_ENV_TLS_CERT);
924 return -1;
925 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800926
927 fp = fopen (cert_str, "r");
928 if (fp == NULL)
929 {
930 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
931 return -1;
932 }
933 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
934 fclose (fp);
935
936 fp = fopen (key_str, "r");
937 if (fp == NULL)
938 {
939 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
940 return -1;
941 }
942 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
943 fclose (fp);
944
945 crypto.cert = cert_buf;
946 crypto.key = key_buf;
947 crypto.cert_len = cert_size;
948 crypto.key_len = key_size;
949 ckp_index = vppcom_add_cert_key_pair (&crypto);
950 if (ckp_index < 0)
951 {
952 LDBG (0, "ERROR: failed to add cert key pair\n");
953 return -1;
954 }
955
956 ldp->ckpair_index = ckp_index;
957
Yu Ping7b74b072019-05-08 00:40:24 +0800958 return 0;
959}
960
961static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800962assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800963{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800964 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800965
Florin Corasa5a9efd2021-01-05 17:03:29 -0800966 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
967 return -1;
968
969 ckp_len = sizeof (ldp->ckpair_index);
liuyacan276675e2021-08-30 13:24:53 +0800970 return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800971}
972
Dave Wallace048b1d62018-01-03 22:24:41 -0500973int
974socket (int domain, int type, int protocol)
975{
Florin Coras7baeb712019-01-04 17:05:43 -0800976 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500977 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800978 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500979
Florin Coras5f33d0d2021-06-02 21:22:21 -0700980 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500981
982 if (((domain == AF_INET) || (domain == AF_INET6)) &&
983 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
984 {
Yu Ping7b74b072019-05-08 00:40:24 +0800985 u8 proto;
986 if (ldp->transparent_tls)
987 {
988 proto = VPPCOM_PROTO_TLS;
989 }
990 else
991 proto = ((sock_type == SOCK_DGRAM) ?
992 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500993
Florin Coras7baeb712019-01-04 17:05:43 -0800994 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
995 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500996
Florin Coras7baeb712019-01-04 17:05:43 -0800997 vlsh = vls_create (proto, is_nonblocking);
998 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500999 {
Florin Coras7baeb712019-01-04 17:05:43 -08001000 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001001 rv = -1;
1002 }
1003 else
1004 {
Yu Ping7b74b072019-05-08 00:40:24 +08001005 if (ldp->transparent_tls)
1006 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001007 if (assign_cert_key_pair (vlsh) < 0)
1008 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001009 }
Florin Coras7baeb712019-01-04 17:05:43 -08001010 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001011 }
1012 }
1013 else
1014 {
Florin Coras7baeb712019-01-04 17:05:43 -08001015 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001016 rv = libc_socket (domain, type, protocol);
1017 }
1018
Dave Wallace048b1d62018-01-03 22:24:41 -05001019 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001020}
1021
1022/*
1023 * Create two new sockets, of type TYPE in domain DOMAIN and using
1024 * protocol PROTOCOL, which are connected to each other, and put file
1025 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1026 * one will be chosen automatically.
1027 * Returns 0 on success, -1 for errors.
1028 * */
1029int
Dave Wallace048b1d62018-01-03 22:24:41 -05001030socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001031{
Florin Coras7baeb712019-01-04 17:05:43 -08001032 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001033
Florin Coras5f33d0d2021-06-02 21:22:21 -07001034 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001035
1036 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1037 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001038 {
Florin Coras7baeb712019-01-04 17:05:43 -08001039 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001040 errno = ENOSYS;
1041 rv = -1;
1042 }
1043 else
1044 {
Florin Coras7baeb712019-01-04 17:05:43 -08001045 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001046 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001047 }
1048
Dave Wallace048b1d62018-01-03 22:24:41 -05001049 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001050}
1051
1052int
Dave Wallace048b1d62018-01-03 22:24:41 -05001053bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001054{
Florin Coras7baeb712019-01-04 17:05:43 -08001055 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001056 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001057
Florin Coras5f33d0d2021-06-02 21:22:21 -07001058 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001059
Florin Coras7baeb712019-01-04 17:05:43 -08001060 vlsh = ldp_fd_to_vlsh (fd);
1061 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001062 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001063 vppcom_endpt_t ep;
1064
Dave Wallace048b1d62018-01-03 22:24:41 -05001065 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001066 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001067 case AF_INET:
1068 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001069 {
Florin Coras7baeb712019-01-04 17:05:43 -08001070 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1071 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001072 errno = EINVAL;
1073 rv = -1;
1074 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001075 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001076 ep.is_ip4 = VPPCOM_IS_IP4;
1077 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1078 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1079 break;
1080
1081 case AF_INET6:
1082 if (len != sizeof (struct sockaddr_in6))
1083 {
Florin Coras7baeb712019-01-04 17:05:43 -08001084 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1085 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001086 errno = EINVAL;
1087 rv = -1;
1088 goto done;
1089 }
1090 ep.is_ip4 = VPPCOM_IS_IP6;
1091 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1092 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001093 break;
1094
1095 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001096 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1097 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001098 errno = EAFNOSUPPORT;
1099 rv = -1;
1100 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001101 }
Florin Coras7baeb712019-01-04 17:05:43 -08001102 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1103 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001104
Florin Coras7baeb712019-01-04 17:05:43 -08001105 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001106 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001107 {
1108 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001109 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001110 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001111 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001112 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001113 {
Florin Coras7baeb712019-01-04 17:05:43 -08001114 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001115 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001116 }
1117
Dave Wallace048b1d62018-01-03 22:24:41 -05001118done:
Florin Coras7baeb712019-01-04 17:05:43 -08001119 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001120
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001121 return rv;
1122}
1123
1124static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001125ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1126 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001127{
Dave Wallace048b1d62018-01-03 22:24:41 -05001128 int rv = 0;
1129 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001130
Florin Coras5f33d0d2021-06-02 21:22:21 -07001131 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001132
1133 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001134 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001135 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1136 switch (addr->sa_family)
1137 {
1138 case AF_INET:
1139 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1140 if (*len > sizeof (struct sockaddr_in))
1141 *len = sizeof (struct sockaddr_in);
1142 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1143 copy_len = *len - sa_len;
1144 if (copy_len > 0)
1145 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1146 copy_len);
1147 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001148
Dave Wallace048b1d62018-01-03 22:24:41 -05001149 case AF_INET6:
1150 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1151 if (*len > sizeof (struct sockaddr_in6))
1152 *len = sizeof (struct sockaddr_in6);
1153 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1154 copy_len = *len - sa_len;
1155 if (copy_len > 0)
1156 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1157 __in6_u.__u6_addr8, ep->ip, copy_len);
1158 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001159
Dave Wallace048b1d62018-01-03 22:24:41 -05001160 default:
1161 /* Not possible */
1162 rv = -EAFNOSUPPORT;
1163 break;
1164 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001165 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001166 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001167}
1168
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001169int
Dave Wallace048b1d62018-01-03 22:24:41 -05001170getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001171{
Florin Coras7baeb712019-01-04 17:05:43 -08001172 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001173 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001174
Florin Coras5f33d0d2021-06-02 21:22:21 -07001175 ldp_init_check ();
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001176
Florin Coras7baeb712019-01-04 17:05:43 -08001177 vlsh = ldp_fd_to_vlsh (fd);
1178 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001179 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001180 vppcom_endpt_t ep;
1181 u8 addr_buf[sizeof (struct in6_addr)];
1182 u32 size = sizeof (ep);
1183
1184 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001185
Florin Coras7baeb712019-01-04 17:05:43 -08001186 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001187 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001188 {
1189 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001190 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001191 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001192 else
1193 {
Dave Wallace2a865272018-02-07 21:00:42 -05001194 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001195 if (rv != VPPCOM_OK)
1196 {
1197 errno = -rv;
1198 rv = -1;
1199 }
1200 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001201 }
1202 else
1203 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001204 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001205 }
1206
Dave Wallace048b1d62018-01-03 22:24:41 -05001207 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001208}
1209
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001210int
Dave Wallace048b1d62018-01-03 22:24:41 -05001211connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001212{
Florin Coras7baeb712019-01-04 17:05:43 -08001213 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001214 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001215
Florin Coras5f33d0d2021-06-02 21:22:21 -07001216 ldp_init_check ();
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001217
Dave Wallace048b1d62018-01-03 22:24:41 -05001218 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001219 {
Florin Coras7baeb712019-01-04 17:05:43 -08001220 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001221 errno = EINVAL;
1222 rv = -1;
1223 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001224 }
1225
Florin Coras7baeb712019-01-04 17:05:43 -08001226 vlsh = ldp_fd_to_vlsh (fd);
1227 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001228 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001229 vppcom_endpt_t ep;
1230
Dave Wallace048b1d62018-01-03 22:24:41 -05001231 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001232 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001233 case AF_INET:
1234 if (len != sizeof (struct sockaddr_in))
1235 {
Florin Coras7baeb712019-01-04 17:05:43 -08001236 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1237 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001238 errno = EINVAL;
1239 rv = -1;
1240 goto done;
1241 }
1242 ep.is_ip4 = VPPCOM_IS_IP4;
1243 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1244 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1245 break;
1246
1247 case AF_INET6:
1248 if (len != sizeof (struct sockaddr_in6))
1249 {
Florin Coras7baeb712019-01-04 17:05:43 -08001250 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1251 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001252 errno = EINVAL;
1253 rv = -1;
1254 goto done;
1255 }
1256 ep.is_ip4 = VPPCOM_IS_IP6;
1257 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1258 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1259 break;
1260
1261 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001262 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1263 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001264 errno = EAFNOSUPPORT;
1265 rv = -1;
1266 goto done;
1267 }
Florin Coras7baeb712019-01-04 17:05:43 -08001268 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1269 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001270
Florin Coras7baeb712019-01-04 17:05:43 -08001271 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001272 if (rv != VPPCOM_OK)
1273 {
1274 errno = -rv;
1275 rv = -1;
1276 }
1277 }
1278 else
1279 {
Florin Coras7baeb712019-01-04 17:05:43 -08001280 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1281 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001282
1283 rv = libc_connect (fd, addr, len);
1284 }
1285
1286done:
Florin Coras7baeb712019-01-04 17:05:43 -08001287 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001288 return rv;
1289}
1290
1291int
1292getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1293{
Florin Coras7baeb712019-01-04 17:05:43 -08001294 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001295 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001296
Florin Coras5f33d0d2021-06-02 21:22:21 -07001297 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001298
Florin Coras7baeb712019-01-04 17:05:43 -08001299 vlsh = ldp_fd_to_vlsh (fd);
1300 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001301 {
1302 vppcom_endpt_t ep;
1303 u8 addr_buf[sizeof (struct in6_addr)];
1304 u32 size = sizeof (ep);
1305
1306 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001307 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001308 if (rv != VPPCOM_OK)
1309 {
1310 errno = -rv;
1311 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001312 }
1313 else
1314 {
Dave Wallace2a865272018-02-07 21:00:42 -05001315 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001316 if (rv != VPPCOM_OK)
1317 {
1318 errno = -rv;
1319 rv = -1;
1320 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001321 }
1322 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001323 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001324 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001326 }
1327
Dave Wallace048b1d62018-01-03 22:24:41 -05001328 return rv;
1329}
1330
1331ssize_t
1332send (int fd, const void *buf, size_t n, int flags)
1333{
Florin Coras7baeb712019-01-04 17:05:43 -08001334 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001335 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001336
Florin Coras5f33d0d2021-06-02 21:22:21 -07001337 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001338
Florin Coras7baeb712019-01-04 17:05:43 -08001339 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001340 {
Florin Coras7baeb712019-01-04 17:05:43 -08001341 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001342 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001343 {
1344 errno = -size;
1345 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001346 }
1347 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001348 else
1349 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001350 size = libc_send (fd, buf, n, flags);
1351 }
1352
Dave Wallace048b1d62018-01-03 22:24:41 -05001353 return size;
1354}
1355
1356ssize_t
1357sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1358{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001359 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001360 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001361 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001362
Florin Coras5f33d0d2021-06-02 21:22:21 -07001363 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001364
Florin Coras7baeb712019-01-04 17:05:43 -08001365 vlsh = ldp_fd_to_vlsh (out_fd);
1366 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001367 {
1368 int rv;
1369 ssize_t results = 0;
1370 size_t n_bytes_left = len;
1371 size_t bytes_to_read;
1372 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001373 u8 eagain = 0;
1374 u32 flags, flags_len = sizeof (flags);
1375
Florin Coras7baeb712019-01-04 17:05:43 -08001376 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001377 if (PREDICT_FALSE (rv != VPPCOM_OK))
1378 {
Florin Coras7baeb712019-01-04 17:05:43 -08001379 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1380 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001381
Florin Corasdfe4cf42018-11-28 22:13:45 -08001382 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001383 errno = -rv;
1384 size = -1;
1385 goto done;
1386 }
1387
1388 if (offset)
1389 {
1390 off_t off = lseek (in_fd, *offset, SEEK_SET);
1391 if (PREDICT_FALSE (off == -1))
1392 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001393 size = -1;
1394 goto done;
1395 }
1396
1397 ASSERT (off == *offset);
1398 }
1399
1400 do
1401 {
Florin Coras7baeb712019-01-04 17:05:43 -08001402 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001403 if (size < 0)
1404 {
Florin Coraseb801d02020-09-16 17:44:58 -07001405 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001406 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001407 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001408 errno = -size;
1409 size = -1;
1410 goto done;
1411 }
1412
1413 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001414 if (bytes_to_read == 0)
1415 {
1416 if (flags & O_NONBLOCK)
1417 {
1418 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001419 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001420 goto update_offset;
1421 }
1422 else
1423 continue;
1424 }
1425 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001426 vec_validate (ldpw->io_buffer, bytes_to_read);
1427 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001428 if (nbytes < 0)
1429 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001430 if (results == 0)
1431 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001432 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001433 size = -1;
1434 goto done;
1435 }
1436 goto update_offset;
1437 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001438
Florin Coras7baeb712019-01-04 17:05:43 -08001439 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001440 if (size < 0)
1441 {
1442 if (size == VPPCOM_EAGAIN)
1443 {
1444 if (flags & O_NONBLOCK)
1445 {
1446 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001447 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001448 goto update_offset;
1449 }
1450 else
1451 continue;
1452 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001453 if (results == 0)
1454 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001455 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001456 errno = -size;
1457 size = -1;
1458 goto done;
1459 }
1460 goto update_offset;
1461 }
1462
1463 results += nbytes;
1464 ASSERT (n_bytes_left >= nbytes);
1465 n_bytes_left = n_bytes_left - nbytes;
1466 }
1467 while (n_bytes_left > 0);
1468
1469 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001470 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001471 if (offset)
1472 {
1473 off_t off = lseek (in_fd, *offset, SEEK_SET);
1474 if (PREDICT_FALSE (off == -1))
1475 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001476 size = -1;
1477 goto done;
1478 }
1479
1480 ASSERT (off == *offset);
1481 *offset += results + 1;
1482 }
1483 if (eagain)
1484 {
1485 errno = EAGAIN;
1486 size = -1;
1487 }
1488 else
1489 size = results;
1490 }
1491 else
1492 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001493 size = libc_sendfile (out_fd, in_fd, offset, len);
1494 }
1495
1496done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001497 return size;
1498}
1499
1500ssize_t
1501sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1502{
1503 return sendfile (out_fd, in_fd, offset, len);
1504}
1505
1506ssize_t
1507recv (int fd, void *buf, size_t n, int flags)
1508{
Florin Coras7baeb712019-01-04 17:05:43 -08001509 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001510 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001511
Florin Coras5f33d0d2021-06-02 21:22:21 -07001512 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001513
Florin Coras7baeb712019-01-04 17:05:43 -08001514 vlsh = ldp_fd_to_vlsh (fd);
1515 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001516 {
Florin Coras7baeb712019-01-04 17:05:43 -08001517 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001518 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001519 {
1520 errno = -size;
1521 size = -1;
1522 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001523 }
1524 else
1525 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001526 size = libc_recv (fd, buf, n, flags);
1527 }
1528
Dave Wallace048b1d62018-01-03 22:24:41 -05001529 return size;
1530}
1531
Sivaprasad Tummalafdcbd382021-07-31 21:38:19 +05301532ssize_t
1533__recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1534{
1535 if (n > buflen)
1536 return -1;
1537
1538 return recv (fd, buf, n, flags);
1539}
1540
Florin Corasce17f462020-05-22 20:36:29 +00001541static int
1542ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1543 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1544{
1545 vppcom_endpt_t *ep = 0;
1546 vppcom_endpt_t _ep;
1547
1548 if (addr)
1549 {
1550 ep = &_ep;
1551 switch (addr->sa_family)
1552 {
1553 case AF_INET:
1554 ep->is_ip4 = VPPCOM_IS_IP4;
1555 ep->ip =
1556 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1557 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1558 break;
1559
1560 case AF_INET6:
1561 ep->is_ip4 = VPPCOM_IS_IP6;
1562 ep->ip =
1563 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1564 ep->port =
1565 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1566 break;
1567
1568 default:
1569 return EAFNOSUPPORT;
1570 }
1571 }
1572
1573 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1574}
1575
1576static int
1577ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1578 int flags, __SOCKADDR_ARG addr,
1579 socklen_t * __restrict addr_len)
1580{
1581 u8 src_addr[sizeof (struct sockaddr_in6)];
1582 vppcom_endpt_t ep;
1583 ssize_t size;
1584 int rv;
1585
1586 if (addr)
1587 {
1588 ep.ip = src_addr;
1589 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1590
1591 if (size > 0)
1592 {
1593 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1594 if (rv < 0)
1595 size = rv;
1596 }
1597 }
1598 else
1599 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1600
1601 return size;
1602}
1603
Dave Wallace048b1d62018-01-03 22:24:41 -05001604ssize_t
1605sendto (int fd, const void *buf, size_t n, int flags,
1606 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1607{
Florin Coras7baeb712019-01-04 17:05:43 -08001608 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001609 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001610
Florin Coras5f33d0d2021-06-02 21:22:21 -07001611 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001612
Florin Coras7baeb712019-01-04 17:05:43 -08001613 vlsh = ldp_fd_to_vlsh (fd);
wanghanlin97c6e0d2021-07-06 15:01:48 +08001614 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001615 {
Florin Corasce17f462020-05-22 20:36:29 +00001616 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001617 if (size < 0)
1618 {
1619 errno = -size;
1620 size = -1;
1621 }
1622 }
1623 else
1624 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001625 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1626 }
1627
Dave Wallace048b1d62018-01-03 22:24:41 -05001628 return size;
1629}
1630
1631ssize_t
1632recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1633 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1634{
Florin Corasce17f462020-05-22 20:36:29 +00001635 vls_handle_t vlsh;
1636 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001637
Florin Coras5f33d0d2021-06-02 21:22:21 -07001638 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001639
Florin Corasce17f462020-05-22 20:36:29 +00001640 vlsh = ldp_fd_to_vlsh (fd);
1641 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001642 {
Florin Corasce17f462020-05-22 20:36:29 +00001643 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001644 if (size < 0)
1645 {
1646 errno = -size;
1647 size = -1;
1648 }
1649 }
1650 else
1651 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001652 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1653 }
1654
Dave Wallace048b1d62018-01-03 22:24:41 -05001655 return size;
1656}
1657
1658ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001659sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001660{
Florin Coras7baeb712019-01-04 17:05:43 -08001661 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001662 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001663
Florin Coras5f33d0d2021-06-02 21:22:21 -07001664 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001665
Florin Coras7baeb712019-01-04 17:05:43 -08001666 vlsh = ldp_fd_to_vlsh (fd);
1667 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001668 {
Florin Corasce17f462020-05-22 20:36:29 +00001669 struct iovec *iov = msg->msg_iov;
1670 ssize_t total = 0;
1671 int i, rv;
1672
1673 for (i = 0; i < msg->msg_iovlen; ++i)
1674 {
1675 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1676 msg->msg_name, msg->msg_namelen);
1677 if (rv < 0)
1678 break;
1679 else
1680 {
1681 total += rv;
1682 if (rv < iov[i].iov_len)
1683 break;
1684 }
1685 }
1686
1687 if (rv < 0 && total == 0)
1688 {
1689 errno = -rv;
1690 size = -1;
1691 }
1692 else
1693 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001694 }
1695 else
1696 {
Florin Corasce17f462020-05-22 20:36:29 +00001697 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001698 }
1699
Dave Wallace048b1d62018-01-03 22:24:41 -05001700 return size;
1701}
1702
1703#ifdef USE_GNU
1704int
1705sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1706{
1707 ssize_t size;
1708 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001709 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001710
Florin Coras5f33d0d2021-06-02 21:22:21 -07001711 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001712
wanghanlin97c6e0d2021-07-06 15:01:48 +08001713 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001714 {
1715 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1716 errno = ENOSYS;
1717 size = -1;
1718 }
1719 else
1720 {
1721 func_str = "libc_sendmmsg";
1722
Dave Wallace2a865272018-02-07 21:00:42 -05001723 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001724 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1725 "vmessages %p, vlen %u, flags 0x%x",
1726 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1727
1728 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1729 }
1730
Dave Wallace2a865272018-02-07 21:00:42 -05001731 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001732 {
1733 if (size < 0)
1734 {
1735 int errno_val = errno;
1736 perror (func_str);
1737 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1738 "rv %d, errno = %d", getpid (), fd, fd,
1739 func_str, size, errno_val);
1740 errno = errno_val;
1741 }
1742 else
1743 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1744 getpid (), fd, fd, size, size);
1745 }
1746 return size;
1747}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001748#endif
1749
Dave Wallace048b1d62018-01-03 22:24:41 -05001750ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001751recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001752{
Florin Coras7baeb712019-01-04 17:05:43 -08001753 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001754 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001755
Florin Coras5f33d0d2021-06-02 21:22:21 -07001756 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001757
Florin Coras7baeb712019-01-04 17:05:43 -08001758 vlsh = ldp_fd_to_vlsh (fd);
1759 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001760 {
Florin Corasce17f462020-05-22 20:36:29 +00001761 struct iovec *iov = msg->msg_iov;
1762 ssize_t max_deq, total = 0;
1763 int i, rv;
1764
1765 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1766 if (!max_deq)
1767 return 0;
1768
1769 for (i = 0; i < msg->msg_iovlen; i++)
1770 {
1771 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1772 (i == 0 ? msg->msg_name : NULL),
1773 (i == 0 ? &msg->msg_namelen : NULL));
1774 if (rv <= 0)
1775 break;
1776 else
1777 {
1778 total += rv;
1779 if (rv < iov[i].iov_len)
1780 break;
1781 }
1782 if (total >= max_deq)
1783 break;
1784 }
1785
1786 if (rv < 0 && total == 0)
1787 {
1788 errno = -rv;
1789 size = -1;
1790 }
1791 else
1792 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001793 }
1794 else
1795 {
Florin Corasce17f462020-05-22 20:36:29 +00001796 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001797 }
1798
Dave Wallace048b1d62018-01-03 22:24:41 -05001799 return size;
1800}
1801
1802#ifdef USE_GNU
1803int
1804recvmmsg (int fd, struct mmsghdr *vmessages,
1805 unsigned int vlen, int flags, struct timespec *tmo)
1806{
1807 ssize_t size;
1808 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001809 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001810
Florin Coras5f33d0d2021-06-02 21:22:21 -07001811 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001812
wanghanlin97c6e0d2021-07-06 15:01:48 +08001813 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001814 {
1815 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1816 errno = ENOSYS;
1817 size = -1;
1818 }
1819 else
1820 {
1821 func_str = "libc_recvmmsg";
1822
Dave Wallace2a865272018-02-07 21:00:42 -05001823 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001824 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1825 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1826 getpid (), fd, fd, func_str, vmessages, vlen,
1827 flags, tmo);
1828
1829 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1830 }
1831
Dave Wallace2a865272018-02-07 21:00:42 -05001832 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001833 {
1834 if (size < 0)
1835 {
1836 int errno_val = errno;
1837 perror (func_str);
1838 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1839 "rv %d, errno = %d", getpid (), fd, fd,
1840 func_str, size, errno_val);
1841 errno = errno_val;
1842 }
1843 else
1844 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1845 getpid (), fd, fd, size, size);
1846 }
1847 return size;
1848}
1849#endif
1850
1851int
1852getsockopt (int fd, int level, int optname,
1853 void *__restrict optval, socklen_t * __restrict optlen)
1854{
Florin Coras7baeb712019-01-04 17:05:43 -08001855 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001856 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001857
Florin Coras5f33d0d2021-06-02 21:22:21 -07001858 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001859
Florin Coras7baeb712019-01-04 17:05:43 -08001860 vlsh = ldp_fd_to_vlsh (fd);
1861 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001862 {
1863 rv = -EOPNOTSUPP;
1864
1865 switch (level)
1866 {
1867 case SOL_TCP:
1868 switch (optname)
1869 {
1870 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001871 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1872 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001873 break;
1874 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001875 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1876 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001877 break;
1878 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001879 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1880 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001881 break;
1882 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001883 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1884 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001885 break;
1886 case TCP_INFO:
1887 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1888 {
Florin Coras7baeb712019-01-04 17:05:43 -08001889 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1890 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001891 memset (optval, 0, *optlen);
1892 rv = VPPCOM_OK;
1893 }
1894 else
1895 rv = -EFAULT;
1896 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001897 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001898 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001899 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001900 rv = 0;
1901 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001902 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001903 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1904 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001905 break;
1906 }
1907 break;
1908 case SOL_IPV6:
1909 switch (optname)
1910 {
1911 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001912 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001913 break;
1914 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001915 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1916 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001917 break;
1918 }
1919 break;
1920 case SOL_SOCKET:
1921 switch (optname)
1922 {
1923 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001924 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001925 break;
1926 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001927 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001928 break;
1929 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001930 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001931 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1932 break;
1933 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001934 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1935 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001936 break;
1937 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001938 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1939 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001940 break;
1941 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001942 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001943 break;
wanghanlin0674f852021-02-22 10:38:36 +08001944 case SO_REUSEPORT:
1945 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1946 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001947 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001948 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001949 break;
wanghanlin0674f852021-02-22 10:38:36 +08001950 case SO_DOMAIN:
1951 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1952 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001953 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001954 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001955 break;
Florin Coras8eb8d502021-06-16 14:46:57 -07001956 case SO_BINDTODEVICE:
1957 rv = 0;
1958 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001959 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001960 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1961 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001962 break;
1963 }
1964 break;
1965 default:
1966 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001967 }
1968
Dave Wallace048b1d62018-01-03 22:24:41 -05001969 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001970 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001971 errno = -rv;
1972 rv = -1;
1973 }
1974 }
1975 else
1976 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001977 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1978 }
1979
Dave Wallace048b1d62018-01-03 22:24:41 -05001980 return rv;
1981}
1982
1983int
1984setsockopt (int fd, int level, int optname,
1985 const void *optval, socklen_t optlen)
1986{
Florin Coras7baeb712019-01-04 17:05:43 -08001987 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001988 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001989
Florin Coras5f33d0d2021-06-02 21:22:21 -07001990 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001991
Florin Coras7baeb712019-01-04 17:05:43 -08001992 vlsh = ldp_fd_to_vlsh (fd);
1993 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001994 {
1995 rv = -EOPNOTSUPP;
1996
1997 switch (level)
1998 {
1999 case SOL_TCP:
2000 switch (optname)
2001 {
2002 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002003 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2004 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002005 break;
2006 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002007 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2008 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002009 break;
2010 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002011 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2012 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002013 break;
2014 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002015 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2016 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002017 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002018 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002019 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002020 /* Ignore */
2021 rv = 0;
2022 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002023 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002024 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2025 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002026 break;
2027 }
2028 break;
2029 case SOL_IPV6:
2030 switch (optname)
2031 {
2032 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002033 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2034 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002035 break;
2036 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002037 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2038 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002039 break;
2040 }
2041 break;
2042 case SOL_SOCKET:
2043 switch (optname)
2044 {
2045 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002046 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2047 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002048 break;
2049 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002050 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2051 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002052 break;
wanghanlin0674f852021-02-22 10:38:36 +08002053 case SO_REUSEPORT:
2054 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2055 &optlen);
2056 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002058 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2059 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002060 break;
Florin Coras2f647902021-06-02 08:23:50 -07002061 case SO_LINGER:
2062 rv = 0;
2063 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002064 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002065 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2066 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002067 break;
2068 }
2069 break;
2070 default:
2071 break;
2072 }
2073
2074 if (rv != VPPCOM_OK)
2075 {
2076 errno = -rv;
2077 rv = -1;
2078 }
2079 }
2080 else
2081 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002082 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2083 }
2084
Dave Wallace048b1d62018-01-03 22:24:41 -05002085 return rv;
2086}
2087
2088int
2089listen (int fd, int n)
2090{
Florin Coras7baeb712019-01-04 17:05:43 -08002091 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002092 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002093
Florin Coras5f33d0d2021-06-02 21:22:21 -07002094 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002095
Florin Coras7baeb712019-01-04 17:05:43 -08002096 vlsh = ldp_fd_to_vlsh (fd);
2097 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002098 {
Florin Coras7baeb712019-01-04 17:05:43 -08002099 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002100
Florin Coras7baeb712019-01-04 17:05:43 -08002101 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002102 if (rv != VPPCOM_OK)
2103 {
2104 errno = -rv;
2105 rv = -1;
2106 }
2107 }
2108 else
2109 {
Florin Coras7baeb712019-01-04 17:05:43 -08002110 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002111 rv = libc_listen (fd, n);
2112 }
2113
Florin Coras7baeb712019-01-04 17:05:43 -08002114 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 return rv;
2116}
2117
2118static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002119ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2120 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002121{
Florin Coras7baeb712019-01-04 17:05:43 -08002122 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002123 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002124
Florin Coras5f33d0d2021-06-02 21:22:21 -07002125 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002126
Florin Coras7baeb712019-01-04 17:05:43 -08002127 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2128 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002129 {
2130 vppcom_endpt_t ep;
2131 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002132 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002133 ep.ip = src_addr;
2134
Florin Coras7baeb712019-01-04 17:05:43 -08002135 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002136 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002137
Florin Coras7baeb712019-01-04 17:05:43 -08002138 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2139 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002140 {
Florin Coras7baeb712019-01-04 17:05:43 -08002141 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002142 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002143 }
2144 else
2145 {
Dave Wallace2a865272018-02-07 21:00:42 -05002146 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002147 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002148 {
Florin Coras7baeb712019-01-04 17:05:43 -08002149 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002150 errno = -rv;
2151 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002152 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002153 else
2154 {
Florin Coras7baeb712019-01-04 17:05:43 -08002155 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002156 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002157 }
2158 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002159 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002160 {
Florin Coras7baeb712019-01-04 17:05:43 -08002161 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2162 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002163
Dave Wallace048b1d62018-01-03 22:24:41 -05002164 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002165 }
2166
Florin Coras7baeb712019-01-04 17:05:43 -08002167 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002168
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002169 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002170}
2171
Dave Wallace048b1d62018-01-03 22:24:41 -05002172int
2173accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2174 int flags)
2175{
Dave Wallace2a865272018-02-07 21:00:42 -05002176 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002177}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002178
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002179int
Dave Wallace048b1d62018-01-03 22:24:41 -05002180accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002181{
Dave Wallace2a865272018-02-07 21:00:42 -05002182 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002183}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002184
Dave Wallace048b1d62018-01-03 22:24:41 -05002185int
2186shutdown (int fd, int how)
2187{
Florin Coras7baeb712019-01-04 17:05:43 -08002188 vls_handle_t vlsh;
liuyacan55c952e2021-06-13 14:54:55 +08002189 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002190
Florin Coras5f33d0d2021-06-02 21:22:21 -07002191 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002192
Florin Coras7baeb712019-01-04 17:05:43 -08002193 vlsh = ldp_fd_to_vlsh (fd);
2194 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002195 {
Florin Coras7baeb712019-01-04 17:05:43 -08002196 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
liuyacan55c952e2021-06-13 14:54:55 +08002197 rv = vls_shutdown (vlsh, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002198 }
2199 else
2200 {
Florin Coras7baeb712019-01-04 17:05:43 -08002201 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002202 rv = libc_shutdown (fd, how);
2203 }
2204
Dave Wallace048b1d62018-01-03 22:24:41 -05002205 return rv;
2206}
2207
2208int
2209epoll_create1 (int flags)
2210{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002211 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002212 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002213 int rv;
2214
Florin Coras5f33d0d2021-06-02 21:22:21 -07002215 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002216
hanlina3a48962020-07-13 11:09:15 +08002217 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002218 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002219 /* Make sure workers have been allocated */
2220 if (!ldp->workers)
2221 {
2222 ldp_alloc_workers ();
2223 ldpw = ldp_worker_get_current ();
2224 }
Florin Coras99368312018-08-02 10:45:44 -07002225 rv = libc_epoll_create1 (flags);
2226 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002227 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002228 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002229 return rv;
2230 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002231
Florin Coras7baeb712019-01-04 17:05:43 -08002232 vlsh = vls_epoll_create ();
2233 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002234 {
Florin Coras7baeb712019-01-04 17:05:43 -08002235 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002236 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002237 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002238 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002239 {
Florin Coras7baeb712019-01-04 17:05:43 -08002240 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002241 }
Florin Coras7baeb712019-01-04 17:05:43 -08002242 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002243 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002244}
2245
2246int
Dave Wallace048b1d62018-01-03 22:24:41 -05002247epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002248{
Dave Wallace048b1d62018-01-03 22:24:41 -05002249 return epoll_create1 (0);
2250}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002251
Dave Wallace048b1d62018-01-03 22:24:41 -05002252int
2253epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2254{
Florin Coras7baeb712019-01-04 17:05:43 -08002255 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002256 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002257
Florin Coras5f33d0d2021-06-02 21:22:21 -07002258 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002259
Florin Coras7baeb712019-01-04 17:05:43 -08002260 vep_vlsh = ldp_fd_to_vlsh (epfd);
2261 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002262 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002263 /* The LDP epoll_create1 always creates VCL epfd's.
2264 * The app should never have a kernel base epoll fd unless it
2265 * was acquired outside of the LD_PRELOAD process context.
2266 * In any case, if we get one, punt it to libc_epoll_ctl.
2267 */
Florin Coras7baeb712019-01-04 17:05:43 -08002268 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2269 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002270
2271 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002272 goto done;
2273 }
2274
Florin Coras7baeb712019-01-04 17:05:43 -08002275 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002276
Florin Coras7baeb712019-01-04 17:05:43 -08002277 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2278 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002279
Florin Coras7baeb712019-01-04 17:05:43 -08002280 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002281 {
Florin Coras7baeb712019-01-04 17:05:43 -08002282 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002283 " event %p", epfd, vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002284
Florin Coras7baeb712019-01-04 17:05:43 -08002285 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002286 if (rv != VPPCOM_OK)
2287 {
2288 errno = -rv;
2289 rv = -1;
2290 }
2291 }
2292 else
2293 {
2294 int libc_epfd;
2295 u32 size = sizeof (epfd);
2296
Florin Coras7baeb712019-01-04 17:05:43 -08002297 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002298 if (!libc_epfd)
2299 {
Florin Coras7baeb712019-01-04 17:05:43 -08002300 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2301 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002302
2303 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2304 if (libc_epfd < 0)
2305 {
2306 rv = libc_epfd;
2307 goto done;
2308 }
2309
Florin Coras7baeb712019-01-04 17:05:43 -08002310 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2311 &size);
Florin Coras99368312018-08-02 10:45:44 -07002312 if (rv < 0)
2313 {
2314 errno = -rv;
2315 rv = -1;
2316 goto done;
2317 }
2318 }
2319 else if (PREDICT_FALSE (libc_epfd < 0))
2320 {
2321 errno = -epfd;
2322 rv = -1;
2323 goto done;
2324 }
2325
Florin Coras7baeb712019-01-04 17:05:43 -08002326 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2327 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002328
2329 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002330 }
2331
2332done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002333 return rv;
2334}
Dave Wallace048b1d62018-01-03 22:24:41 -05002335
2336static inline int
Florin Coras99368312018-08-02 10:45:44 -07002337ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2338 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002339{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002340 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002341 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002342 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002343 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002344
Florin Coras5f33d0d2021-06-02 21:22:21 -07002345 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002346
2347 if (PREDICT_FALSE (!events || (timeout < -1)))
2348 {
2349 errno = EFAULT;
2350 return -1;
2351 }
2352
Florin Corasdfe4cf42018-11-28 22:13:45 -08002353 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002354 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2355
Florin Coras7baeb712019-01-04 17:05:43 -08002356 ep_vlsh = ldp_fd_to_vlsh (epfd);
2357 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002358 {
Florin Coras7baeb712019-01-04 17:05:43 -08002359 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002360 errno = EBADFD;
2361 return -1;
2362 }
2363
Florin Coras4dee8cd2019-01-29 21:28:16 -08002364 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2365 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002366 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002367 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002368
Florin Coras7baeb712019-01-04 17:05:43 -08002369 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002370 if (PREDICT_FALSE (libc_epfd < 0))
2371 {
2372 errno = -libc_epfd;
2373 rv = -1;
2374 goto done;
2375 }
2376
Florin Coras7baeb712019-01-04 17:05:43 -08002377 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2378 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002379 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002380 do
2381 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002382 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002383 {
Florin Coras7baeb712019-01-04 17:05:43 -08002384 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002385 if (rv > 0)
2386 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002387 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002388 goto done;
2389 }
2390 else if (rv < 0)
2391 {
2392 errno = -rv;
2393 rv = -1;
2394 goto done;
2395 }
2396 }
2397 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002398 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002399
2400 if (libc_epfd > 0)
2401 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002402 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002403 if (rv != 0)
2404 goto done;
2405 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002406 }
Florin Coras72f77822019-01-22 19:05:52 -08002407 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002408
2409done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002410 return rv;
2411}
2412
hanlin4266d4d2020-05-19 17:34:17 +08002413static inline int
2414ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2415 int maxevents, int timeout, const sigset_t * sigmask)
2416{
hanlina3a48962020-07-13 11:09:15 +08002417 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002418 int libc_epfd, rv = 0, num_ev;
2419 vls_handle_t ep_vlsh;
2420
Florin Coras5f33d0d2021-06-02 21:22:21 -07002421 ldp_init_check ();
hanlin4266d4d2020-05-19 17:34:17 +08002422
2423 if (PREDICT_FALSE (!events || (timeout < -1)))
2424 {
2425 errno = EFAULT;
2426 return -1;
2427 }
2428
Florin Corasff40d8f2020-08-11 22:05:28 -07002429 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2430 * one thread but it is now used on another */
2431 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2432 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002433
2434 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002435 if (epfd == ldpw->vcl_mq_epfd)
2436 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2437
2438 ep_vlsh = ldp_fd_to_vlsh (epfd);
2439 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2440 {
2441 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2442 errno = EBADFD;
2443 return -1;
2444 }
2445
2446 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2447 if (PREDICT_FALSE (!libc_epfd))
2448 {
2449 u32 size = sizeof (epfd);
2450
2451 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2452 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2453 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2454 if (libc_epfd < 0)
2455 {
2456 rv = libc_epfd;
2457 goto done;
2458 }
2459
2460 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2461 if (rv < 0)
2462 {
2463 errno = -rv;
2464 rv = -1;
2465 goto done;
2466 }
2467 }
2468 if (PREDICT_FALSE (libc_epfd <= 0))
2469 {
2470 errno = -libc_epfd;
2471 rv = -1;
2472 goto done;
2473 }
2474
2475 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2476 {
2477 struct epoll_event e = { 0 };
2478 e.events = EPOLLIN;
2479 e.data.fd = ldpw->vcl_mq_epfd;
2480 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2481 0)
2482 {
2483 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2484 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2485 rv = -1;
2486 goto done;
2487 }
2488 ldpw->mq_epfd_added = 1;
2489 }
2490
wanghanlin8919fec2021-03-18 20:00:41 +08002491 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2492 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002493 if (rv > 0)
2494 goto done;
hanlina3a48962020-07-13 11:09:15 +08002495 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002496 {
2497 errno = -rv;
2498 rv = -1;
2499 goto done;
2500 }
2501
2502 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2503 if (rv <= 0)
2504 goto done;
2505 for (int i = 0; i < rv; i++)
2506 {
2507 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2508 {
2509 /* We should remove mq epoll fd from events. */
2510 rv--;
2511 if (i != rv)
2512 {
2513 events[i].events = events[rv].events;
2514 events[i].data.u64 = events[rv].data.u64;
2515 }
2516 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2517 if (PREDICT_TRUE (num_ev > 0))
2518 rv += num_ev;
2519 break;
2520 }
2521 }
2522
2523done:
2524 return rv;
2525}
2526
Dave Wallace048b1d62018-01-03 22:24:41 -05002527int
2528epoll_pwait (int epfd, struct epoll_event *events,
2529 int maxevents, int timeout, const sigset_t * sigmask)
2530{
hanlin4266d4d2020-05-19 17:34:17 +08002531 if (vls_use_eventfd ())
2532 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2533 sigmask);
2534 else
2535 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002536}
2537
2538int
2539epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2540{
hanlin4266d4d2020-05-19 17:34:17 +08002541 if (vls_use_eventfd ())
2542 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2543 else
2544 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002545}
2546
2547int
2548poll (struct pollfd *fds, nfds_t nfds, int timeout)
2549{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002550 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002551 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002552 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002553 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002554 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002555
Florin Coraseb801d02020-09-16 17:44:58 -07002556 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002557
Florin Coras4dee8cd2019-01-29 21:28:16 -08002558 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2559 clib_time_init (&ldpw->clib_time);
2560
2561 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2562 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002563
Dave Wallace048b1d62018-01-03 22:24:41 -05002564 for (i = 0; i < nfds; i++)
2565 {
Florin Coras6917b942018-11-13 22:44:54 -08002566 if (fds[i].fd < 0)
2567 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002568
Florin Coras7baeb712019-01-04 17:05:43 -08002569 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2570 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002571 {
2572 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002573 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002574 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002575 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002576 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002577#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002578 if (fds[i].events & POLLRDNORM)
2579 vp->events |= POLLIN;
2580 if (fds[i].events & POLLWRNORM)
2581 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002582#endif
Florin Coras6917b942018-11-13 22:44:54 -08002583 vp->revents = fds[i].revents;
2584 }
2585 else
2586 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002587 vec_add1 (ldpw->libc_poll, fds[i]);
2588 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002589 }
2590 }
2591
Dave Wallace048b1d62018-01-03 22:24:41 -05002592 do
2593 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002594 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002595 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002596 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002597 if (rv < 0)
2598 {
2599 errno = -rv;
2600 rv = -1;
2601 goto done;
2602 }
2603 else
2604 n_revents += rv;
2605 }
2606
Florin Corasdfe4cf42018-11-28 22:13:45 -08002607 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002608 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002609 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002610 if (rv < 0)
2611 goto done;
2612 else
2613 n_revents += rv;
2614 }
2615
2616 if (n_revents)
2617 {
2618 rv = n_revents;
2619 goto done;
2620 }
2621 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002622 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002623 rv = 0;
2624
2625done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002626 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002627 {
2628 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002629 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002630#ifdef __USE_XOPEN2K
2631 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2632 (fds[vp->fds_ndx].events & POLLRDNORM))
2633 fds[vp->fds_ndx].revents |= POLLRDNORM;
2634 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2635 (fds[vp->fds_ndx].events & POLLWRNORM))
2636 fds[vp->fds_ndx].revents |= POLLWRNORM;
2637#endif
2638 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002639 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002640
Florin Corasdfe4cf42018-11-28 22:13:45 -08002641 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002642 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002643 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002644 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002645 vec_reset_length (ldpw->libc_poll_idxs);
2646 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002647
Dave Wallace048b1d62018-01-03 22:24:41 -05002648 return rv;
2649}
2650
2651#ifdef USE_GNU
2652int
2653ppoll (struct pollfd *fds, nfds_t nfds,
2654 const struct timespec *timeout, const sigset_t * sigmask)
2655{
Florin Coras5f33d0d2021-06-02 21:22:21 -07002656 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002657
2658 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2659 errno = ENOSYS;
2660
2661
2662 return -1;
2663}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002664#endif
2665
Dave Wallace2a865272018-02-07 21:00:42 -05002666void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002667
Dave Wallace2a865272018-02-07 21:00:42 -05002668void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002669
Dave Wallace048b1d62018-01-03 22:24:41 -05002670/*
2671 * This function is called when the library is loaded
2672 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002673void
Dave Wallace2a865272018-02-07 21:00:42 -05002674ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002675{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002676 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002677 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002678 {
2679 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2680 getpid ());
2681 _exit (1);
2682 }
Dave Wallace69d01192018-02-22 16:22:09 -05002683 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002684 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002685}
2686
2687/*
2688 * This function is called when the library is unloaded
2689 */
2690void
Dave Wallace2a865272018-02-07 21:00:42 -05002691ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002692{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002693 /*
2694 swrap_destructor ();
2695 if (ldp->init)
2696 ldp->init = 0;
2697 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002698
2699 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002700 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002701 */
Dave Wallace69d01192018-02-22 16:22:09 -05002702 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002703 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2704 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002705}
2706
2707
2708/*
2709 * fd.io coding-style-patch-verification: ON
2710 *
2711 * Local Variables:
2712 * eval: (c-set-style "gnu")
2713 * End:
2714 */