blob: 73a5bc20cb4f36a58861990216656c5ff8442b70 [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 {
wanghanlin1eb8fea2021-10-14 11:10:26 +0800274 /* Only valid for select/pselect, so just WARNING and not exit */
275 LDBG (0,
276 "WARNING: LDP vlsh bit value %d > FD_SETSIZE/2 %d, "
277 "select/pselect not supported now!",
Florin Coras294afe22019-01-07 17:49:17 -0800278 ldp->vlsh_bit_val, FD_SETSIZE / 2);
Florin Coras294afe22019-01-07 17:49:17 -0800279 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500280 }
Yu Ping7b74b072019-05-08 00:40:24 +0800281 env_var_str = getenv (LDP_ENV_TLS_TRANS);
282 if (env_var_str)
283 {
284 ldp->transparent_tls = 1;
285 }
Florin Coras99368312018-08-02 10:45:44 -0700286
Florin Coras4dee8cd2019-01-29 21:28:16 -0800287 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100288 pool_foreach (ldpw, ldp->workers) {
Florin Coras4dee8cd2019-01-29 21:28:16 -0800289 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100290 }
Florin Coras4dee8cd2019-01-29 21:28:16 -0800291 /* *INDENT-ON* */
292
Florin Coras05ecfcc2018-12-12 18:19:39 -0800293 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700294
295 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500296}
297
Florin Coras5f33d0d2021-06-02 21:22:21 -0700298#define ldp_init_check() \
299 if (PREDICT_FALSE (!ldp->init)) \
300 { \
301 if ((errno = -ldp_init ())) \
302 return -1; \
303 }
304
Dave Wallace048b1d62018-01-03 22:24:41 -0500305int
306close (int fd)
307{
Florin Coras7baeb712019-01-04 17:05:43 -0800308 vls_handle_t vlsh;
309 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500310
Florin Coras5f33d0d2021-06-02 21:22:21 -0700311 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500312
Florin Coras7baeb712019-01-04 17:05:43 -0800313 vlsh = ldp_fd_to_vlsh (fd);
314 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500315 {
Florin Coras7baeb712019-01-04 17:05:43 -0800316 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500317 if (epfd > 0)
318 {
Florin Coras7baeb712019-01-04 17:05:43 -0800319 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500320
321 rv = libc_close (epfd);
322 if (rv < 0)
323 {
324 u32 size = sizeof (epfd);
325 epfd = 0;
326
Florin Coras7baeb712019-01-04 17:05:43 -0800327 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500328 }
329 }
330 else if (PREDICT_FALSE (epfd < 0))
331 {
332 errno = -epfd;
333 rv = -1;
334 goto done;
335 }
336
Florin Coras7baeb712019-01-04 17:05:43 -0800337 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500338
Florin Coras7baeb712019-01-04 17:05:43 -0800339 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500340 if (rv != VPPCOM_OK)
341 {
342 errno = -rv;
343 rv = -1;
344 }
345 }
346 else
347 {
Florin Coras7baeb712019-01-04 17:05:43 -0800348 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500349 rv = libc_close (fd);
350 }
351
352done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500353 return rv;
354}
355
356ssize_t
357read (int fd, void *buf, size_t nbytes)
358{
Florin Coras7baeb712019-01-04 17:05:43 -0800359 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500360 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500361
Florin Coras5f33d0d2021-06-02 21:22:21 -0700362 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500363
Florin Coras7baeb712019-01-04 17:05:43 -0800364 vlsh = ldp_fd_to_vlsh (fd);
365 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500366 {
Florin Coras7baeb712019-01-04 17:05:43 -0800367 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500368 if (size < 0)
369 {
370 errno = -size;
371 size = -1;
372 }
373 }
374 else
375 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500376 size = libc_read (fd, buf, nbytes);
377 }
378
Dave Wallace048b1d62018-01-03 22:24:41 -0500379 return size;
380}
381
382ssize_t
383readv (int fd, const struct iovec * iov, int iovcnt)
384{
Dave Wallace8aaba562018-01-18 17:21:19 -0500385 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800386 vls_handle_t vlsh;
387 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500388
Florin Coras5f33d0d2021-06-02 21:22:21 -0700389 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500390
Florin Coras7baeb712019-01-04 17:05:43 -0800391 vlsh = ldp_fd_to_vlsh (fd);
392 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500393 {
Florin Coras067f9542020-02-14 05:33:46 +0000394 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500395 {
Florin Coras067f9542020-02-14 05:33:46 +0000396 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
397 if (rv <= 0)
398 break;
399 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500400 {
Florin Coras067f9542020-02-14 05:33:46 +0000401 total += rv;
402 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500403 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700404 }
405 }
Florin Coras067f9542020-02-14 05:33:46 +0000406 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700407 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500408 errno = -rv;
409 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700410 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500411 else
412 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700413 }
414 else
415 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500416 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700417 }
418
Dave Wallace048b1d62018-01-03 22:24:41 -0500419 return size;
420}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700421
Dave Wallace048b1d62018-01-03 22:24:41 -0500422ssize_t
423write (int fd, const void *buf, size_t nbytes)
424{
Florin Coras7baeb712019-01-04 17:05:43 -0800425 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500426 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500427
Florin Coras5f33d0d2021-06-02 21:22:21 -0700428 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500429
Florin Coras7baeb712019-01-04 17:05:43 -0800430 vlsh = ldp_fd_to_vlsh (fd);
431 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500432 {
Florin Coras7baeb712019-01-04 17:05:43 -0800433 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500434 if (size < 0)
435 {
436 errno = -size;
437 size = -1;
438 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700439 }
440 else
441 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500442 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700443 }
444
Dave Wallace048b1d62018-01-03 22:24:41 -0500445 return size;
446}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700447
Dave Wallace048b1d62018-01-03 22:24:41 -0500448ssize_t
449writev (int fd, const struct iovec * iov, int iovcnt)
450{
Dave Wallace048b1d62018-01-03 22:24:41 -0500451 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800452 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500453 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500454
Florin Coras5f33d0d2021-06-02 21:22:21 -0700455 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500456
Florin Coras7baeb712019-01-04 17:05:43 -0800457 vlsh = ldp_fd_to_vlsh (fd);
458 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500459 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000460 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500461 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000462 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
463 if (rv < 0)
464 break;
465 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500466 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000467 total += rv;
468 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500469 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500470 }
471 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500472
Florin Coraseda1b8c2020-03-23 16:00:35 +0000473 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500474 {
475 errno = -rv;
476 size = -1;
477 }
478 else
479 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700480 }
481 else
482 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500483 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700484 }
485
Dave Wallace048b1d62018-01-03 22:24:41 -0500486 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700487}
488
Florin Coras0ab36f52020-05-26 19:45:45 +0000489static int
490fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700491{
Florin Coras7baeb712019-01-04 17:05:43 -0800492 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700493 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800494
495 vlsh = ldp_fd_to_vlsh (fd);
496 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
497 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700498 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500499 int flags = va_arg (ap, int);
500 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700501
Dave Wallace048b1d62018-01-03 22:24:41 -0500502 size = sizeof (flags);
503 rv = -EOPNOTSUPP;
504 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700505 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500506 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800507 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500508 break;
509
510 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800511 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500512 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800513 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700514 break;
Florin Coras173bae32018-11-16 18:56:28 -0800515 case F_SETFD:
516 /* TODO handle this */
517 LDBG (0, "F_SETFD ignored flags %u", flags);
518 rv = 0;
519 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700520 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500521 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700522 break;
523 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500524 if (rv < 0)
525 {
526 errno = -rv;
527 rv = -1;
528 }
529 }
530 else
531 {
Carl Smithe16707b2019-11-13 14:37:39 +1300532#ifdef HAVE_FCNTL64
533 rv = libc_vfcntl64 (fd, cmd, ap);
534#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500535 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300536#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700537 }
538
Florin Coras0ab36f52020-05-26 19:45:45 +0000539 return rv;
540}
541
542int
543fcntl (int fd, int cmd, ...)
544{
545 va_list ap;
546 int rv;
547
Florin Coras5f33d0d2021-06-02 21:22:21 -0700548 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000549
550 va_start (ap, cmd);
551 rv = fcntl_internal (fd, cmd, ap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500552 va_end (ap);
553
Dave Wallace048b1d62018-01-03 22:24:41 -0500554 return rv;
555}
556
557int
Florin Corasd7586d52020-04-29 02:19:51 +0000558fcntl64 (int fd, int cmd, ...)
559{
560 va_list ap;
561 int rv;
562
Florin Coras5f33d0d2021-06-02 21:22:21 -0700563 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000564
Florin Corasd7586d52020-04-29 02:19:51 +0000565 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000566 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000567 va_end (ap);
568 return rv;
569}
570
571int
Dave Wallace048b1d62018-01-03 22:24:41 -0500572ioctl (int fd, unsigned long int cmd, ...)
573{
Florin Coras7baeb712019-01-04 17:05:43 -0800574 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500575 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800576 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500577
Florin Coras5f33d0d2021-06-02 21:22:21 -0700578 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500579
580 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500581
Florin Coras7baeb712019-01-04 17:05:43 -0800582 vlsh = ldp_fd_to_vlsh (fd);
583 if (vlsh != VLS_INVALID_HANDLE)
584 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500585 switch (cmd)
586 {
587 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800588 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500589 break;
590
591 case FIONBIO:
592 {
593 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
594 u32 size = sizeof (flags);
595
596 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
597 * non-blocking, the flags should be read here and merged
598 * with O_NONBLOCK.
599 */
Florin Coras7baeb712019-01-04 17:05:43 -0800600 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500601 }
602 break;
603
604 default:
605 rv = -EOPNOTSUPP;
606 break;
607 }
608 if (rv < 0)
609 {
610 errno = -rv;
611 rv = -1;
612 }
613 }
614 else
615 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500616 rv = libc_vioctl (fd, cmd, ap);
617 }
618
Dave Wallace048b1d62018-01-03 22:24:41 -0500619 va_end (ap);
620 return rv;
621}
622
Florin Coras294afe22019-01-07 17:49:17 -0800623always_inline void
624ldp_select_init_maps (fd_set * __restrict original,
625 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
626 clib_bitmap_t ** vclb, int nfds, u32 minbits,
627 u32 n_bytes, uword * si_bits, uword * libc_bits)
628{
629 uword si_bits_set, libc_bits_set;
630 vls_handle_t vlsh;
631 int fd;
632
633 clib_bitmap_validate (*vclb, minbits);
634 clib_bitmap_validate (*libcb, minbits);
635 clib_bitmap_validate (*resultb, minbits);
636 clib_memcpy_fast (*resultb, original, n_bytes);
637 memset (original, 0, n_bytes);
638
639 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100640 clib_bitmap_foreach (fd, *resultb) {
Florin Coras294afe22019-01-07 17:49:17 -0800641 if (fd > nfds)
642 break;
643 vlsh = ldp_fd_to_vlsh (fd);
644 if (vlsh == VLS_INVALID_HANDLE)
645 clib_bitmap_set_no_check (*libcb, fd, 1);
646 else
Florin Corascbce80a2020-04-20 01:32:38 +0000647 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100648 }
Florin Coras294afe22019-01-07 17:49:17 -0800649 /* *INDENT-ON* */
650
651 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
652 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000653 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800654
655 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
656 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
657}
658
659always_inline int
660ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
661{
662 vls_handle_t vlsh;
663 uword si;
664 int fd;
665
666 if (!libcb)
667 return 0;
668
669 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100670 clib_bitmap_foreach (si, vclb) {
Florin Coras294afe22019-01-07 17:49:17 -0800671 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000672 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800673 fd = ldp_vlsh_to_fd (vlsh);
674 if (PREDICT_FALSE (fd < 0))
675 {
676 errno = EBADFD;
677 return -1;
678 }
679 FD_SET (fd, libcb);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100680 }
Florin Coras294afe22019-01-07 17:49:17 -0800681 /* *INDENT-ON* */
682
683 return 0;
684}
685
686always_inline void
687ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
688{
689 uword fd;
690
Florin Coras78b5fa62019-02-21 20:04:15 -0800691 if (!libcb)
692 return;
693
Florin Coras294afe22019-01-07 17:49:17 -0800694 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100695 clib_bitmap_foreach (fd, result)
Florin Coras294afe22019-01-07 17:49:17 -0800696 FD_SET ((int)fd, libcb);
Florin Coras294afe22019-01-07 17:49:17 -0800697 /* *INDENT-ON* */
698}
699
Dave Wallace048b1d62018-01-03 22:24:41 -0500700int
Dave Wallace2a865272018-02-07 21:00:42 -0500701ldp_pselect (int nfds, fd_set * __restrict readfds,
702 fd_set * __restrict writefds,
703 fd_set * __restrict exceptfds,
704 const struct timespec *__restrict timeout,
705 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500706{
Florin Coras294afe22019-01-07 17:49:17 -0800707 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800708 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800709 struct timespec libc_tspec = { 0 };
710 f64 time_out, vcl_timeout = 0;
711 uword si_bits, libc_bits;
712 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500713
714 if (nfds < 0)
715 {
716 errno = EINVAL;
717 return -1;
718 }
719
Florin Coras4dee8cd2019-01-29 21:28:16 -0800720 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
721 clib_time_init (&ldpw->clib_time);
722
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500723 if (timeout)
724 {
725 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800726 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500727
liuyacanf71796e2021-08-02 10:01:39 +0800728 time_out += clib_time_now (&ldpw->clib_time);
729
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500730 /* select as fine grained sleep */
731 if (!nfds)
732 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800733 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500734 ;
735 return 0;
736 }
737 }
738 else if (!nfds)
739 {
740 errno = EINVAL;
741 return -1;
742 }
743 else
744 time_out = -1;
745
Florin Coras7baeb712019-01-04 17:05:43 -0800746 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500747 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500748 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
749 timeout, sigmask);
750 goto done;
751 }
752
Florin Coras294afe22019-01-07 17:49:17 -0800753 si_bits = libc_bits = 0;
754 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800755
Dave Wallace048b1d62018-01-03 22:24:41 -0500756 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800757 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
758 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
759 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500760 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800761 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
762 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
763 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500764 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800765 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
766 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
767 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500768
Florin Coras294afe22019-01-07 17:49:17 -0800769 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500770 {
771 errno = EINVAL;
772 rv = -1;
773 goto done;
774 }
775
Florin Coras78b5fa62019-02-21 20:04:15 -0800776 if (!si_bits)
777 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800778
Dave Wallace048b1d62018-01-03 22:24:41 -0500779 do
780 {
Florin Coras294afe22019-01-07 17:49:17 -0800781 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500783 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800784 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000785 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500786 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500787 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800788 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000789 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500790 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500791 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800792 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000793 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500794 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800795
Florin Coras0ef8ef22019-01-18 08:37:13 -0800796 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
797 writefds ? ldpw->wr_bitmap : NULL,
798 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800799 if (rv < 0)
800 {
801 errno = -rv;
802 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000803 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800804 }
805 else if (rv > 0)
806 {
807 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
808 {
809 rv = -1;
810 goto done;
811 }
812
813 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
814 {
815 rv = -1;
816 goto done;
817 }
818
819 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
820 {
821 rv = -1;
822 goto done;
823 }
824 bits_set = rv;
825 }
826 }
827 if (libc_bits)
828 {
829 if (readfds)
830 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
831 vec_len (ldpw->libc_rd_bitmap) *
832 sizeof (clib_bitmap_t));
833 if (writefds)
834 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
835 vec_len (ldpw->libc_wr_bitmap) *
836 sizeof (clib_bitmap_t));
837 if (exceptfds)
838 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
839 vec_len (ldpw->libc_ex_bitmap) *
840 sizeof (clib_bitmap_t));
841
Dave Wallace048b1d62018-01-03 22:24:41 -0500842 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800843 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
844 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
845 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
846 &libc_tspec, sigmask);
847 if (rv > 0)
848 {
849 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
850 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
851 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
852 bits_set += rv;
853 }
854 }
855
856 if (bits_set)
857 {
858 rv = bits_set;
859 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500860 }
861 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800862 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500863 rv = 0;
864
865done:
866 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800867 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800868 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800869 clib_bitmap_zero (ldpw->libc_rd_bitmap);
870 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800871 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800872 clib_bitmap_zero (ldpw->libc_wr_bitmap);
873 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800874 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800875 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500876
Dave Wallace048b1d62018-01-03 22:24:41 -0500877 return rv;
878}
879
880int
881select (int nfds, fd_set * __restrict readfds,
882 fd_set * __restrict writefds,
883 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
884{
885 struct timespec tspec;
886
887 if (timeout)
888 {
889 tspec.tv_sec = timeout->tv_sec;
890 tspec.tv_nsec = timeout->tv_usec * 1000;
891 }
Dave Wallace2a865272018-02-07 21:00:42 -0500892 return ldp_pselect (nfds, readfds, writefds, exceptfds,
893 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500894}
895
896#ifdef __USE_XOPEN2K
897int
898pselect (int nfds, fd_set * __restrict readfds,
899 fd_set * __restrict writefds,
900 fd_set * __restrict exceptfds,
901 const struct timespec *__restrict timeout,
902 const __sigset_t * __restrict sigmask)
903{
Dave Wallace2a865272018-02-07 21:00:42 -0500904 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500905}
906#endif
907
Yu Ping7b74b072019-05-08 00:40:24 +0800908/* If transparent TLS mode is turned on, then ldp will load key and cert.
909 */
910static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800911load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800912{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800913 char *cert_str = getenv (LDP_ENV_TLS_CERT);
914 char *key_str = getenv (LDP_ENV_TLS_KEY);
915 char cert_buf[4096], key_buf[4096];
916 int cert_size, key_size;
917 vppcom_cert_key_pair_t crypto;
918 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800919 FILE *fp;
920
Florin Corasa5a9efd2021-01-05 17:03:29 -0800921 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800922 {
923 LDBG (0, "ERROR: failed to read LDP environment %s\n",
924 LDP_ENV_TLS_CERT);
925 return -1;
926 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800927
928 fp = fopen (cert_str, "r");
929 if (fp == NULL)
930 {
931 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
932 return -1;
933 }
934 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
935 fclose (fp);
936
937 fp = fopen (key_str, "r");
938 if (fp == NULL)
939 {
940 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
941 return -1;
942 }
943 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
944 fclose (fp);
945
946 crypto.cert = cert_buf;
947 crypto.key = key_buf;
948 crypto.cert_len = cert_size;
949 crypto.key_len = key_size;
950 ckp_index = vppcom_add_cert_key_pair (&crypto);
951 if (ckp_index < 0)
952 {
953 LDBG (0, "ERROR: failed to add cert key pair\n");
954 return -1;
955 }
956
957 ldp->ckpair_index = ckp_index;
958
Yu Ping7b74b072019-05-08 00:40:24 +0800959 return 0;
960}
961
962static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800963assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800964{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800965 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800966
Florin Corasa5a9efd2021-01-05 17:03:29 -0800967 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
968 return -1;
969
970 ckp_len = sizeof (ldp->ckpair_index);
liuyacan276675e2021-08-30 13:24:53 +0800971 return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800972}
973
Dave Wallace048b1d62018-01-03 22:24:41 -0500974int
975socket (int domain, int type, int protocol)
976{
Florin Coras7baeb712019-01-04 17:05:43 -0800977 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500978 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800979 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500980
Florin Coras5f33d0d2021-06-02 21:22:21 -0700981 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500982
983 if (((domain == AF_INET) || (domain == AF_INET6)) &&
984 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
985 {
Yu Ping7b74b072019-05-08 00:40:24 +0800986 u8 proto;
987 if (ldp->transparent_tls)
988 {
989 proto = VPPCOM_PROTO_TLS;
990 }
991 else
992 proto = ((sock_type == SOCK_DGRAM) ?
993 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500994
Florin Coras7baeb712019-01-04 17:05:43 -0800995 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
996 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500997
Florin Coras7baeb712019-01-04 17:05:43 -0800998 vlsh = vls_create (proto, is_nonblocking);
999 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001000 {
Florin Coras7baeb712019-01-04 17:05:43 -08001001 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001002 rv = -1;
1003 }
1004 else
1005 {
Yu Ping7b74b072019-05-08 00:40:24 +08001006 if (ldp->transparent_tls)
1007 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001008 if (assign_cert_key_pair (vlsh) < 0)
1009 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001010 }
Florin Coras7baeb712019-01-04 17:05:43 -08001011 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001012 }
1013 }
1014 else
1015 {
Florin Coras7baeb712019-01-04 17:05:43 -08001016 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001017 rv = libc_socket (domain, type, protocol);
1018 }
1019
Dave Wallace048b1d62018-01-03 22:24:41 -05001020 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001021}
1022
1023/*
1024 * Create two new sockets, of type TYPE in domain DOMAIN and using
1025 * protocol PROTOCOL, which are connected to each other, and put file
1026 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1027 * one will be chosen automatically.
1028 * Returns 0 on success, -1 for errors.
1029 * */
1030int
Dave Wallace048b1d62018-01-03 22:24:41 -05001031socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001032{
Florin Coras7baeb712019-01-04 17:05:43 -08001033 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001034
Florin Coras5f33d0d2021-06-02 21:22:21 -07001035 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001036
1037 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1038 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001039 {
Florin Coras7baeb712019-01-04 17:05:43 -08001040 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001041 errno = ENOSYS;
1042 rv = -1;
1043 }
1044 else
1045 {
Florin Coras7baeb712019-01-04 17:05:43 -08001046 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001047 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001048 }
1049
Dave Wallace048b1d62018-01-03 22:24:41 -05001050 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001051}
1052
1053int
Dave Wallace048b1d62018-01-03 22:24:41 -05001054bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001055{
Florin Coras7baeb712019-01-04 17:05:43 -08001056 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001057 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001058
Florin Coras5f33d0d2021-06-02 21:22:21 -07001059 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001060
Florin Coras7baeb712019-01-04 17:05:43 -08001061 vlsh = ldp_fd_to_vlsh (fd);
1062 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001063 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001064 vppcom_endpt_t ep;
1065
Dave Wallace048b1d62018-01-03 22:24:41 -05001066 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001067 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001068 case AF_INET:
1069 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001070 {
Florin Coras7baeb712019-01-04 17:05:43 -08001071 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1072 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001073 errno = EINVAL;
1074 rv = -1;
1075 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001076 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001077 ep.is_ip4 = VPPCOM_IS_IP4;
1078 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1079 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1080 break;
1081
1082 case AF_INET6:
1083 if (len != sizeof (struct sockaddr_in6))
1084 {
Florin Coras7baeb712019-01-04 17:05:43 -08001085 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1086 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001087 errno = EINVAL;
1088 rv = -1;
1089 goto done;
1090 }
1091 ep.is_ip4 = VPPCOM_IS_IP6;
1092 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1093 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001094 break;
1095
1096 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001097 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1098 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001099 errno = EAFNOSUPPORT;
1100 rv = -1;
1101 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001102 }
Florin Coras7baeb712019-01-04 17:05:43 -08001103 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1104 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001105
Florin Coras7baeb712019-01-04 17:05:43 -08001106 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001107 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001108 {
1109 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001110 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001111 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001112 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001113 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001114 {
Florin Coras7baeb712019-01-04 17:05:43 -08001115 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001116 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001117 }
1118
Dave Wallace048b1d62018-01-03 22:24:41 -05001119done:
Florin Coras7baeb712019-01-04 17:05:43 -08001120 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001121
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001122 return rv;
1123}
1124
1125static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001126ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1127 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001128{
Dave Wallace048b1d62018-01-03 22:24:41 -05001129 int rv = 0;
1130 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001131
Florin Coras5f33d0d2021-06-02 21:22:21 -07001132 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001133
1134 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001135 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001136 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1137 switch (addr->sa_family)
1138 {
1139 case AF_INET:
1140 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1141 if (*len > sizeof (struct sockaddr_in))
1142 *len = sizeof (struct sockaddr_in);
1143 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1144 copy_len = *len - sa_len;
1145 if (copy_len > 0)
1146 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1147 copy_len);
1148 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001149
Dave Wallace048b1d62018-01-03 22:24:41 -05001150 case AF_INET6:
1151 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1152 if (*len > sizeof (struct sockaddr_in6))
1153 *len = sizeof (struct sockaddr_in6);
1154 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1155 copy_len = *len - sa_len;
1156 if (copy_len > 0)
1157 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1158 __in6_u.__u6_addr8, ep->ip, copy_len);
1159 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001160
Dave Wallace048b1d62018-01-03 22:24:41 -05001161 default:
1162 /* Not possible */
1163 rv = -EAFNOSUPPORT;
1164 break;
1165 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001166 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001167 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001168}
1169
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001170int
Dave Wallace048b1d62018-01-03 22:24:41 -05001171getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001172{
Florin Coras7baeb712019-01-04 17:05:43 -08001173 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001174 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001175
Florin Coras5f33d0d2021-06-02 21:22:21 -07001176 ldp_init_check ();
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001177
Florin Coras7baeb712019-01-04 17:05:43 -08001178 vlsh = ldp_fd_to_vlsh (fd);
1179 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001180 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001181 vppcom_endpt_t ep;
1182 u8 addr_buf[sizeof (struct in6_addr)];
1183 u32 size = sizeof (ep);
1184
1185 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001186
Florin Coras7baeb712019-01-04 17:05:43 -08001187 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001188 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001189 {
1190 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001191 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001192 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001193 else
1194 {
Dave Wallace2a865272018-02-07 21:00:42 -05001195 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001196 if (rv != VPPCOM_OK)
1197 {
1198 errno = -rv;
1199 rv = -1;
1200 }
1201 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001202 }
1203 else
1204 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001205 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001206 }
1207
Dave Wallace048b1d62018-01-03 22:24:41 -05001208 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001209}
1210
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001211int
Dave Wallace048b1d62018-01-03 22:24:41 -05001212connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001213{
Florin Coras7baeb712019-01-04 17:05:43 -08001214 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001215 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001216
Florin Coras5f33d0d2021-06-02 21:22:21 -07001217 ldp_init_check ();
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001218
Dave Wallace048b1d62018-01-03 22:24:41 -05001219 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001220 {
Florin Coras7baeb712019-01-04 17:05:43 -08001221 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001222 errno = EINVAL;
1223 rv = -1;
1224 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001225 }
1226
Florin Coras7baeb712019-01-04 17:05:43 -08001227 vlsh = ldp_fd_to_vlsh (fd);
1228 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001229 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001230 vppcom_endpt_t ep;
1231
Dave Wallace048b1d62018-01-03 22:24:41 -05001232 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001233 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001234 case AF_INET:
1235 if (len != sizeof (struct sockaddr_in))
1236 {
Florin Coras7baeb712019-01-04 17:05:43 -08001237 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1238 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001239 errno = EINVAL;
1240 rv = -1;
1241 goto done;
1242 }
1243 ep.is_ip4 = VPPCOM_IS_IP4;
1244 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1245 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1246 break;
1247
1248 case AF_INET6:
1249 if (len != sizeof (struct sockaddr_in6))
1250 {
Florin Coras7baeb712019-01-04 17:05:43 -08001251 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1252 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001253 errno = EINVAL;
1254 rv = -1;
1255 goto done;
1256 }
1257 ep.is_ip4 = VPPCOM_IS_IP6;
1258 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1259 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1260 break;
1261
1262 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001263 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1264 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001265 errno = EAFNOSUPPORT;
1266 rv = -1;
1267 goto done;
1268 }
Florin Coras7baeb712019-01-04 17:05:43 -08001269 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1270 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001271
Florin Coras7baeb712019-01-04 17:05:43 -08001272 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001273 if (rv != VPPCOM_OK)
1274 {
1275 errno = -rv;
1276 rv = -1;
1277 }
1278 }
1279 else
1280 {
Florin Coras7baeb712019-01-04 17:05:43 -08001281 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1282 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001283
1284 rv = libc_connect (fd, addr, len);
1285 }
1286
1287done:
Florin Coras7baeb712019-01-04 17:05:43 -08001288 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001289 return rv;
1290}
1291
1292int
1293getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1294{
Florin Coras7baeb712019-01-04 17:05:43 -08001295 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001296 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001297
Florin Coras5f33d0d2021-06-02 21:22:21 -07001298 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001299
Florin Coras7baeb712019-01-04 17:05:43 -08001300 vlsh = ldp_fd_to_vlsh (fd);
1301 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001302 {
1303 vppcom_endpt_t ep;
1304 u8 addr_buf[sizeof (struct in6_addr)];
1305 u32 size = sizeof (ep);
1306
1307 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001308 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001309 if (rv != VPPCOM_OK)
1310 {
1311 errno = -rv;
1312 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001313 }
1314 else
1315 {
Dave Wallace2a865272018-02-07 21:00:42 -05001316 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001317 if (rv != VPPCOM_OK)
1318 {
1319 errno = -rv;
1320 rv = -1;
1321 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001322 }
1323 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001324 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001325 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001326 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001327 }
1328
Dave Wallace048b1d62018-01-03 22:24:41 -05001329 return rv;
1330}
1331
1332ssize_t
1333send (int fd, const void *buf, size_t n, int flags)
1334{
Florin Coras7baeb712019-01-04 17:05:43 -08001335 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001336 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001337
Florin Coras5f33d0d2021-06-02 21:22:21 -07001338 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001339
Florin Coras7baeb712019-01-04 17:05:43 -08001340 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001341 {
Florin Coras7baeb712019-01-04 17:05:43 -08001342 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001343 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001344 {
1345 errno = -size;
1346 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001347 }
1348 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001349 else
1350 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001351 size = libc_send (fd, buf, n, flags);
1352 }
1353
Dave Wallace048b1d62018-01-03 22:24:41 -05001354 return size;
1355}
1356
1357ssize_t
1358sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1359{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001360 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001361 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001362 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001363
Florin Coras5f33d0d2021-06-02 21:22:21 -07001364 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001365
Florin Coras7baeb712019-01-04 17:05:43 -08001366 vlsh = ldp_fd_to_vlsh (out_fd);
1367 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001368 {
1369 int rv;
1370 ssize_t results = 0;
1371 size_t n_bytes_left = len;
1372 size_t bytes_to_read;
1373 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001374 u8 eagain = 0;
1375 u32 flags, flags_len = sizeof (flags);
1376
Florin Coras7baeb712019-01-04 17:05:43 -08001377 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001378 if (PREDICT_FALSE (rv != VPPCOM_OK))
1379 {
Florin Coras7baeb712019-01-04 17:05:43 -08001380 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1381 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001382
Florin Corasdfe4cf42018-11-28 22:13:45 -08001383 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001384 errno = -rv;
1385 size = -1;
1386 goto done;
1387 }
1388
1389 if (offset)
1390 {
1391 off_t off = lseek (in_fd, *offset, SEEK_SET);
1392 if (PREDICT_FALSE (off == -1))
1393 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001394 size = -1;
1395 goto done;
1396 }
1397
1398 ASSERT (off == *offset);
1399 }
1400
1401 do
1402 {
Florin Coras7baeb712019-01-04 17:05:43 -08001403 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001404 if (size < 0)
1405 {
Florin Coraseb801d02020-09-16 17:44:58 -07001406 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001407 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001408 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 errno = -size;
1410 size = -1;
1411 goto done;
1412 }
1413
1414 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001415 if (bytes_to_read == 0)
1416 {
1417 if (flags & O_NONBLOCK)
1418 {
1419 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001420 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001421 goto update_offset;
1422 }
1423 else
1424 continue;
1425 }
1426 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001427 vec_validate (ldpw->io_buffer, bytes_to_read);
1428 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001429 if (nbytes < 0)
1430 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001431 if (results == 0)
1432 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001433 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001434 size = -1;
1435 goto done;
1436 }
1437 goto update_offset;
1438 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001439
Florin Coras7baeb712019-01-04 17:05:43 -08001440 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001441 if (size < 0)
1442 {
1443 if (size == VPPCOM_EAGAIN)
1444 {
1445 if (flags & O_NONBLOCK)
1446 {
1447 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001448 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001449 goto update_offset;
1450 }
1451 else
1452 continue;
1453 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001454 if (results == 0)
1455 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001456 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001457 errno = -size;
1458 size = -1;
1459 goto done;
1460 }
1461 goto update_offset;
1462 }
1463
1464 results += nbytes;
1465 ASSERT (n_bytes_left >= nbytes);
1466 n_bytes_left = n_bytes_left - nbytes;
1467 }
1468 while (n_bytes_left > 0);
1469
1470 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001471 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001472 if (offset)
1473 {
1474 off_t off = lseek (in_fd, *offset, SEEK_SET);
1475 if (PREDICT_FALSE (off == -1))
1476 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001477 size = -1;
1478 goto done;
1479 }
1480
1481 ASSERT (off == *offset);
1482 *offset += results + 1;
1483 }
1484 if (eagain)
1485 {
1486 errno = EAGAIN;
1487 size = -1;
1488 }
1489 else
1490 size = results;
1491 }
1492 else
1493 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001494 size = libc_sendfile (out_fd, in_fd, offset, len);
1495 }
1496
1497done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001498 return size;
1499}
1500
1501ssize_t
1502sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1503{
1504 return sendfile (out_fd, in_fd, offset, len);
1505}
1506
1507ssize_t
1508recv (int fd, void *buf, size_t n, int flags)
1509{
Florin Coras7baeb712019-01-04 17:05:43 -08001510 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001511 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001512
Florin Coras5f33d0d2021-06-02 21:22:21 -07001513 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001514
Florin Coras7baeb712019-01-04 17:05:43 -08001515 vlsh = ldp_fd_to_vlsh (fd);
1516 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001517 {
Florin Coras7baeb712019-01-04 17:05:43 -08001518 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001519 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001520 {
1521 errno = -size;
1522 size = -1;
1523 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001524 }
1525 else
1526 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001527 size = libc_recv (fd, buf, n, flags);
1528 }
1529
Dave Wallace048b1d62018-01-03 22:24:41 -05001530 return size;
1531}
1532
Sivaprasad Tummalafdcbd382021-07-31 21:38:19 +05301533ssize_t
1534__recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1535{
1536 if (n > buflen)
1537 return -1;
1538
1539 return recv (fd, buf, n, flags);
1540}
1541
Florin Corasce17f462020-05-22 20:36:29 +00001542static int
1543ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags,
1544 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1545{
1546 vppcom_endpt_t *ep = 0;
1547 vppcom_endpt_t _ep;
1548
1549 if (addr)
1550 {
1551 ep = &_ep;
1552 switch (addr->sa_family)
1553 {
1554 case AF_INET:
1555 ep->is_ip4 = VPPCOM_IS_IP4;
1556 ep->ip =
1557 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1558 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1559 break;
1560
1561 case AF_INET6:
1562 ep->is_ip4 = VPPCOM_IS_IP6;
1563 ep->ip =
1564 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1565 ep->port =
1566 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1567 break;
1568
1569 default:
1570 return EAFNOSUPPORT;
1571 }
1572 }
1573
1574 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1575}
1576
1577static int
1578ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1579 int flags, __SOCKADDR_ARG addr,
1580 socklen_t * __restrict addr_len)
1581{
1582 u8 src_addr[sizeof (struct sockaddr_in6)];
1583 vppcom_endpt_t ep;
1584 ssize_t size;
1585 int rv;
1586
1587 if (addr)
1588 {
1589 ep.ip = src_addr;
1590 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1591
1592 if (size > 0)
1593 {
1594 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1595 if (rv < 0)
1596 size = rv;
1597 }
1598 }
1599 else
1600 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1601
1602 return size;
1603}
1604
Dave Wallace048b1d62018-01-03 22:24:41 -05001605ssize_t
1606sendto (int fd, const void *buf, size_t n, int flags,
1607 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1608{
Florin Coras7baeb712019-01-04 17:05:43 -08001609 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001610 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001611
Florin Coras5f33d0d2021-06-02 21:22:21 -07001612 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001613
Florin Coras7baeb712019-01-04 17:05:43 -08001614 vlsh = ldp_fd_to_vlsh (fd);
wanghanlin97c6e0d2021-07-06 15:01:48 +08001615 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001616 {
Florin Corasce17f462020-05-22 20:36:29 +00001617 size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001618 if (size < 0)
1619 {
1620 errno = -size;
1621 size = -1;
1622 }
1623 }
1624 else
1625 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001626 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1627 }
1628
Dave Wallace048b1d62018-01-03 22:24:41 -05001629 return size;
1630}
1631
1632ssize_t
1633recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1634 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1635{
Florin Corasce17f462020-05-22 20:36:29 +00001636 vls_handle_t vlsh;
1637 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001638
Florin Coras5f33d0d2021-06-02 21:22:21 -07001639 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001640
Florin Corasce17f462020-05-22 20:36:29 +00001641 vlsh = ldp_fd_to_vlsh (fd);
1642 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001643 {
Florin Corasce17f462020-05-22 20:36:29 +00001644 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001645 if (size < 0)
1646 {
1647 errno = -size;
1648 size = -1;
1649 }
1650 }
1651 else
1652 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001653 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1654 }
1655
Dave Wallace048b1d62018-01-03 22:24:41 -05001656 return size;
1657}
1658
1659ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001660sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001661{
Florin Coras7baeb712019-01-04 17:05:43 -08001662 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001663 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001664
Florin Coras5f33d0d2021-06-02 21:22:21 -07001665 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001666
Florin Coras7baeb712019-01-04 17:05:43 -08001667 vlsh = ldp_fd_to_vlsh (fd);
1668 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001669 {
Florin Corasce17f462020-05-22 20:36:29 +00001670 struct iovec *iov = msg->msg_iov;
1671 ssize_t total = 0;
Florin Corascba1c222021-11-29 08:12:27 -08001672 int i, rv = 0;
Florin Corasce17f462020-05-22 20:36:29 +00001673
1674 for (i = 0; i < msg->msg_iovlen; ++i)
1675 {
1676 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1677 msg->msg_name, msg->msg_namelen);
1678 if (rv < 0)
1679 break;
1680 else
1681 {
1682 total += rv;
1683 if (rv < iov[i].iov_len)
1684 break;
1685 }
1686 }
1687
1688 if (rv < 0 && total == 0)
1689 {
1690 errno = -rv;
1691 size = -1;
1692 }
1693 else
1694 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001695 }
1696 else
1697 {
Florin Corasce17f462020-05-22 20:36:29 +00001698 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001699 }
1700
Dave Wallace048b1d62018-01-03 22:24:41 -05001701 return size;
1702}
1703
1704#ifdef USE_GNU
1705int
1706sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1707{
1708 ssize_t size;
1709 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001710 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001711
Florin Coras5f33d0d2021-06-02 21:22:21 -07001712 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001713
wanghanlin97c6e0d2021-07-06 15:01:48 +08001714 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001715 {
1716 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1717 errno = ENOSYS;
1718 size = -1;
1719 }
1720 else
1721 {
1722 func_str = "libc_sendmmsg";
1723
Dave Wallace2a865272018-02-07 21:00:42 -05001724 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001725 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1726 "vmessages %p, vlen %u, flags 0x%x",
1727 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1728
1729 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1730 }
1731
Dave Wallace2a865272018-02-07 21:00:42 -05001732 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001733 {
1734 if (size < 0)
1735 {
1736 int errno_val = errno;
1737 perror (func_str);
1738 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1739 "rv %d, errno = %d", getpid (), fd, fd,
1740 func_str, size, errno_val);
1741 errno = errno_val;
1742 }
1743 else
1744 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1745 getpid (), fd, fd, size, size);
1746 }
1747 return size;
1748}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001749#endif
1750
Dave Wallace048b1d62018-01-03 22:24:41 -05001751ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001752recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001753{
Florin Coras7baeb712019-01-04 17:05:43 -08001754 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001755 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001756
Florin Coras5f33d0d2021-06-02 21:22:21 -07001757 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001758
Florin Coras7baeb712019-01-04 17:05:43 -08001759 vlsh = ldp_fd_to_vlsh (fd);
1760 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001761 {
Florin Corasce17f462020-05-22 20:36:29 +00001762 struct iovec *iov = msg->msg_iov;
1763 ssize_t max_deq, total = 0;
1764 int i, rv;
1765
1766 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1767 if (!max_deq)
1768 return 0;
1769
1770 for (i = 0; i < msg->msg_iovlen; i++)
1771 {
1772 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1773 (i == 0 ? msg->msg_name : NULL),
1774 (i == 0 ? &msg->msg_namelen : NULL));
1775 if (rv <= 0)
1776 break;
1777 else
1778 {
1779 total += rv;
1780 if (rv < iov[i].iov_len)
1781 break;
1782 }
1783 if (total >= max_deq)
1784 break;
1785 }
1786
1787 if (rv < 0 && total == 0)
1788 {
1789 errno = -rv;
1790 size = -1;
1791 }
1792 else
1793 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001794 }
1795 else
1796 {
Florin Corasce17f462020-05-22 20:36:29 +00001797 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001798 }
1799
Dave Wallace048b1d62018-01-03 22:24:41 -05001800 return size;
1801}
1802
1803#ifdef USE_GNU
1804int
1805recvmmsg (int fd, struct mmsghdr *vmessages,
1806 unsigned int vlen, int flags, struct timespec *tmo)
1807{
1808 ssize_t size;
1809 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001810 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001811
Florin Coras5f33d0d2021-06-02 21:22:21 -07001812 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001813
wanghanlin97c6e0d2021-07-06 15:01:48 +08001814 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001815 {
1816 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1817 errno = ENOSYS;
1818 size = -1;
1819 }
1820 else
1821 {
1822 func_str = "libc_recvmmsg";
1823
Dave Wallace2a865272018-02-07 21:00:42 -05001824 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001825 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1826 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1827 getpid (), fd, fd, func_str, vmessages, vlen,
1828 flags, tmo);
1829
1830 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1831 }
1832
Dave Wallace2a865272018-02-07 21:00:42 -05001833 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001834 {
1835 if (size < 0)
1836 {
1837 int errno_val = errno;
1838 perror (func_str);
1839 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1840 "rv %d, errno = %d", getpid (), fd, fd,
1841 func_str, size, errno_val);
1842 errno = errno_val;
1843 }
1844 else
1845 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1846 getpid (), fd, fd, size, size);
1847 }
1848 return size;
1849}
1850#endif
1851
1852int
1853getsockopt (int fd, int level, int optname,
1854 void *__restrict optval, socklen_t * __restrict optlen)
1855{
Florin Coras7baeb712019-01-04 17:05:43 -08001856 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001857 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001858
Florin Coras5f33d0d2021-06-02 21:22:21 -07001859 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001860
Florin Coras7baeb712019-01-04 17:05:43 -08001861 vlsh = ldp_fd_to_vlsh (fd);
1862 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001863 {
1864 rv = -EOPNOTSUPP;
1865
1866 switch (level)
1867 {
1868 case SOL_TCP:
1869 switch (optname)
1870 {
1871 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001872 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1873 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001874 break;
1875 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001876 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1877 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001878 break;
1879 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001880 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1881 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001882 break;
1883 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001884 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1885 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001886 break;
1887 case TCP_INFO:
1888 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1889 {
Florin Coras7baeb712019-01-04 17:05:43 -08001890 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1891 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001892 memset (optval, 0, *optlen);
1893 rv = VPPCOM_OK;
1894 }
1895 else
1896 rv = -EFAULT;
1897 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001898 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001899 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001900 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001901 rv = 0;
1902 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001903 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001904 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1905 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001906 break;
1907 }
1908 break;
1909 case SOL_IPV6:
1910 switch (optname)
1911 {
1912 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001913 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001914 break;
1915 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001916 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1917 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001918 break;
1919 }
1920 break;
1921 case SOL_SOCKET:
1922 switch (optname)
1923 {
1924 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001925 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001926 break;
1927 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001928 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001929 break;
1930 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001931 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001932 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1933 break;
1934 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001935 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1936 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001937 break;
1938 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001939 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1940 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001941 break;
1942 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001943 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001944 break;
wanghanlin0674f852021-02-22 10:38:36 +08001945 case SO_REUSEPORT:
1946 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1947 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001948 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001949 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001950 break;
wanghanlin0674f852021-02-22 10:38:36 +08001951 case SO_DOMAIN:
1952 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1953 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001954 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001955 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001956 break;
Florin Coras8eb8d502021-06-16 14:46:57 -07001957 case SO_BINDTODEVICE:
1958 rv = 0;
1959 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001960 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001961 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1962 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001963 break;
1964 }
1965 break;
1966 default:
1967 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001968 }
1969
Dave Wallace048b1d62018-01-03 22:24:41 -05001970 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001971 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 errno = -rv;
1973 rv = -1;
1974 }
1975 }
1976 else
1977 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001978 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1979 }
1980
Dave Wallace048b1d62018-01-03 22:24:41 -05001981 return rv;
1982}
1983
1984int
1985setsockopt (int fd, int level, int optname,
1986 const void *optval, socklen_t optlen)
1987{
Florin Coras7baeb712019-01-04 17:05:43 -08001988 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001989 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001990
Florin Coras5f33d0d2021-06-02 21:22:21 -07001991 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001992
Florin Coras7baeb712019-01-04 17:05:43 -08001993 vlsh = ldp_fd_to_vlsh (fd);
1994 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001995 {
1996 rv = -EOPNOTSUPP;
1997
1998 switch (level)
1999 {
2000 case SOL_TCP:
2001 switch (optname)
2002 {
2003 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002004 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2005 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002006 break;
2007 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002008 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2009 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002010 break;
2011 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002012 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2013 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002014 break;
2015 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002016 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2017 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002018 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002019 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002020 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002021 /* Ignore */
2022 rv = 0;
2023 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002024 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002025 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2026 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002027 break;
2028 }
2029 break;
2030 case SOL_IPV6:
2031 switch (optname)
2032 {
2033 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002034 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2035 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002036 break;
2037 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002038 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2039 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002040 break;
2041 }
2042 break;
2043 case SOL_SOCKET:
2044 switch (optname)
2045 {
2046 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002047 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2048 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002049 break;
2050 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002051 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2052 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002053 break;
wanghanlin0674f852021-02-22 10:38:36 +08002054 case SO_REUSEPORT:
2055 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2056 &optlen);
2057 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002058 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002059 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2060 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002061 break;
Florin Coras2f647902021-06-02 08:23:50 -07002062 case SO_LINGER:
2063 rv = 0;
2064 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002065 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002066 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2067 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002068 break;
2069 }
2070 break;
2071 default:
2072 break;
2073 }
2074
2075 if (rv != VPPCOM_OK)
2076 {
2077 errno = -rv;
2078 rv = -1;
2079 }
2080 }
2081 else
2082 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002083 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2084 }
2085
Dave Wallace048b1d62018-01-03 22:24:41 -05002086 return rv;
2087}
2088
2089int
2090listen (int fd, int n)
2091{
Florin Coras7baeb712019-01-04 17:05:43 -08002092 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002093 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002094
Florin Coras5f33d0d2021-06-02 21:22:21 -07002095 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002096
Florin Coras7baeb712019-01-04 17:05:43 -08002097 vlsh = ldp_fd_to_vlsh (fd);
2098 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002099 {
Florin Coras7baeb712019-01-04 17:05:43 -08002100 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002101
Florin Coras7baeb712019-01-04 17:05:43 -08002102 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002103 if (rv != VPPCOM_OK)
2104 {
2105 errno = -rv;
2106 rv = -1;
2107 }
2108 }
2109 else
2110 {
Florin Coras7baeb712019-01-04 17:05:43 -08002111 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002112 rv = libc_listen (fd, n);
2113 }
2114
Florin Coras7baeb712019-01-04 17:05:43 -08002115 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002116 return rv;
2117}
2118
2119static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002120ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2121 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002122{
Florin Coras7baeb712019-01-04 17:05:43 -08002123 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002124 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002125
Florin Coras5f33d0d2021-06-02 21:22:21 -07002126 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002127
Florin Coras7baeb712019-01-04 17:05:43 -08002128 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2129 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002130 {
2131 vppcom_endpt_t ep;
2132 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002133 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002134 ep.ip = src_addr;
2135
Florin Coras7baeb712019-01-04 17:05:43 -08002136 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002137 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002138
Florin Coras7baeb712019-01-04 17:05:43 -08002139 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2140 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002141 {
Florin Coras7baeb712019-01-04 17:05:43 -08002142 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002143 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002144 }
2145 else
2146 {
Dave Wallace2a865272018-02-07 21:00:42 -05002147 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002148 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002149 {
Florin Coras7baeb712019-01-04 17:05:43 -08002150 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002151 errno = -rv;
2152 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002153 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002154 else
2155 {
Florin Coras7baeb712019-01-04 17:05:43 -08002156 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002157 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002158 }
2159 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002160 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002161 {
Florin Coras7baeb712019-01-04 17:05:43 -08002162 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2163 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002164
Dave Wallace048b1d62018-01-03 22:24:41 -05002165 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002166 }
2167
Florin Coras7baeb712019-01-04 17:05:43 -08002168 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002169
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002170 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002171}
2172
Dave Wallace048b1d62018-01-03 22:24:41 -05002173int
2174accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2175 int flags)
2176{
Dave Wallace2a865272018-02-07 21:00:42 -05002177 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002178}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002179
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002180int
Dave Wallace048b1d62018-01-03 22:24:41 -05002181accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002182{
Dave Wallace2a865272018-02-07 21:00:42 -05002183 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002184}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002185
Dave Wallace048b1d62018-01-03 22:24:41 -05002186int
2187shutdown (int fd, int how)
2188{
Florin Coras7baeb712019-01-04 17:05:43 -08002189 vls_handle_t vlsh;
liuyacan55c952e2021-06-13 14:54:55 +08002190 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002191
Florin Coras5f33d0d2021-06-02 21:22:21 -07002192 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002193
Florin Coras7baeb712019-01-04 17:05:43 -08002194 vlsh = ldp_fd_to_vlsh (fd);
2195 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002196 {
Florin Coras7baeb712019-01-04 17:05:43 -08002197 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
liuyacan55c952e2021-06-13 14:54:55 +08002198 rv = vls_shutdown (vlsh, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002199 }
2200 else
2201 {
Florin Coras7baeb712019-01-04 17:05:43 -08002202 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002203 rv = libc_shutdown (fd, how);
2204 }
2205
Dave Wallace048b1d62018-01-03 22:24:41 -05002206 return rv;
2207}
2208
2209int
2210epoll_create1 (int flags)
2211{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002212 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002213 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002214 int rv;
2215
Florin Coras5f33d0d2021-06-02 21:22:21 -07002216 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002217
hanlina3a48962020-07-13 11:09:15 +08002218 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002219 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002220 /* Make sure workers have been allocated */
2221 if (!ldp->workers)
2222 {
2223 ldp_alloc_workers ();
2224 ldpw = ldp_worker_get_current ();
2225 }
Florin Coras99368312018-08-02 10:45:44 -07002226 rv = libc_epoll_create1 (flags);
2227 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002228 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002229 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002230 return rv;
2231 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002232
Florin Coras7baeb712019-01-04 17:05:43 -08002233 vlsh = vls_epoll_create ();
2234 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002235 {
Florin Coras7baeb712019-01-04 17:05:43 -08002236 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002237 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002238 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002239 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002240 {
Florin Coras7baeb712019-01-04 17:05:43 -08002241 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002242 }
Florin Coras7baeb712019-01-04 17:05:43 -08002243 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002244 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002245}
2246
2247int
Dave Wallace048b1d62018-01-03 22:24:41 -05002248epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002249{
Dave Wallace048b1d62018-01-03 22:24:41 -05002250 return epoll_create1 (0);
2251}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002252
Dave Wallace048b1d62018-01-03 22:24:41 -05002253int
2254epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2255{
Florin Coras7baeb712019-01-04 17:05:43 -08002256 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002257 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002258
Florin Coras5f33d0d2021-06-02 21:22:21 -07002259 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002260
Florin Coras7baeb712019-01-04 17:05:43 -08002261 vep_vlsh = ldp_fd_to_vlsh (epfd);
2262 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002263 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002264 /* The LDP epoll_create1 always creates VCL epfd's.
2265 * The app should never have a kernel base epoll fd unless it
2266 * was acquired outside of the LD_PRELOAD process context.
2267 * In any case, if we get one, punt it to libc_epoll_ctl.
2268 */
Florin Coras7baeb712019-01-04 17:05:43 -08002269 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2270 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002271
2272 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002273 goto done;
2274 }
2275
Florin Coras7baeb712019-01-04 17:05:43 -08002276 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002277
Florin Coras7baeb712019-01-04 17:05:43 -08002278 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2279 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002280
Florin Coras7baeb712019-01-04 17:05:43 -08002281 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002282 {
Florin Coras7baeb712019-01-04 17:05:43 -08002283 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002284 " event %p", epfd, vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002285
Florin Coras7baeb712019-01-04 17:05:43 -08002286 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002287 if (rv != VPPCOM_OK)
2288 {
2289 errno = -rv;
2290 rv = -1;
2291 }
2292 }
2293 else
2294 {
2295 int libc_epfd;
2296 u32 size = sizeof (epfd);
2297
Florin Coras7baeb712019-01-04 17:05:43 -08002298 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002299 if (!libc_epfd)
2300 {
Florin Coras7baeb712019-01-04 17:05:43 -08002301 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2302 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002303
2304 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2305 if (libc_epfd < 0)
2306 {
2307 rv = libc_epfd;
2308 goto done;
2309 }
2310
Florin Coras7baeb712019-01-04 17:05:43 -08002311 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2312 &size);
Florin Coras99368312018-08-02 10:45:44 -07002313 if (rv < 0)
2314 {
2315 errno = -rv;
2316 rv = -1;
2317 goto done;
2318 }
2319 }
2320 else if (PREDICT_FALSE (libc_epfd < 0))
2321 {
2322 errno = -epfd;
2323 rv = -1;
2324 goto done;
2325 }
2326
Florin Coras7baeb712019-01-04 17:05:43 -08002327 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2328 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002329
2330 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002331 }
2332
2333done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002334 return rv;
2335}
Dave Wallace048b1d62018-01-03 22:24:41 -05002336
2337static inline int
Florin Coras99368312018-08-02 10:45:44 -07002338ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2339 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002340{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002341 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002342 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002343 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002344 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002345
Florin Coras5f33d0d2021-06-02 21:22:21 -07002346 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002347
2348 if (PREDICT_FALSE (!events || (timeout < -1)))
2349 {
2350 errno = EFAULT;
2351 return -1;
2352 }
2353
Florin Corasdfe4cf42018-11-28 22:13:45 -08002354 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002355 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2356
Florin Coras7baeb712019-01-04 17:05:43 -08002357 ep_vlsh = ldp_fd_to_vlsh (epfd);
2358 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002359 {
Florin Coras7baeb712019-01-04 17:05:43 -08002360 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002361 errno = EBADFD;
2362 return -1;
2363 }
2364
Florin Coras4dee8cd2019-01-29 21:28:16 -08002365 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2366 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002367 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002368 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002369
Florin Coras7baeb712019-01-04 17:05:43 -08002370 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002371 if (PREDICT_FALSE (libc_epfd < 0))
2372 {
2373 errno = -libc_epfd;
2374 rv = -1;
2375 goto done;
2376 }
2377
Florin Coras7baeb712019-01-04 17:05:43 -08002378 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2379 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002380 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002381 do
2382 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002383 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002384 {
Florin Coras7baeb712019-01-04 17:05:43 -08002385 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002386 if (rv > 0)
2387 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002388 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002389 goto done;
2390 }
2391 else if (rv < 0)
2392 {
2393 errno = -rv;
2394 rv = -1;
2395 goto done;
2396 }
2397 }
2398 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002399 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002400
2401 if (libc_epfd > 0)
2402 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002403 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002404 if (rv != 0)
2405 goto done;
2406 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002407 }
Florin Coras72f77822019-01-22 19:05:52 -08002408 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002409
2410done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002411 return rv;
2412}
2413
hanlin4266d4d2020-05-19 17:34:17 +08002414static inline int
2415ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2416 int maxevents, int timeout, const sigset_t * sigmask)
2417{
hanlina3a48962020-07-13 11:09:15 +08002418 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002419 int libc_epfd, rv = 0, num_ev;
2420 vls_handle_t ep_vlsh;
2421
Florin Coras5f33d0d2021-06-02 21:22:21 -07002422 ldp_init_check ();
hanlin4266d4d2020-05-19 17:34:17 +08002423
2424 if (PREDICT_FALSE (!events || (timeout < -1)))
2425 {
2426 errno = EFAULT;
2427 return -1;
2428 }
2429
Florin Corasff40d8f2020-08-11 22:05:28 -07002430 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2431 * one thread but it is now used on another */
2432 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2433 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002434
2435 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002436 if (epfd == ldpw->vcl_mq_epfd)
2437 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2438
2439 ep_vlsh = ldp_fd_to_vlsh (epfd);
2440 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2441 {
2442 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2443 errno = EBADFD;
2444 return -1;
2445 }
2446
2447 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2448 if (PREDICT_FALSE (!libc_epfd))
2449 {
2450 u32 size = sizeof (epfd);
2451
2452 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2453 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2454 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2455 if (libc_epfd < 0)
2456 {
2457 rv = libc_epfd;
2458 goto done;
2459 }
2460
2461 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2462 if (rv < 0)
2463 {
2464 errno = -rv;
2465 rv = -1;
2466 goto done;
2467 }
2468 }
2469 if (PREDICT_FALSE (libc_epfd <= 0))
2470 {
2471 errno = -libc_epfd;
2472 rv = -1;
2473 goto done;
2474 }
2475
2476 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2477 {
2478 struct epoll_event e = { 0 };
2479 e.events = EPOLLIN;
2480 e.data.fd = ldpw->vcl_mq_epfd;
2481 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2482 0)
2483 {
2484 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2485 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2486 rv = -1;
2487 goto done;
2488 }
2489 ldpw->mq_epfd_added = 1;
2490 }
2491
wanghanlin8919fec2021-03-18 20:00:41 +08002492 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2493 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002494 if (rv > 0)
2495 goto done;
hanlina3a48962020-07-13 11:09:15 +08002496 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002497 {
2498 errno = -rv;
2499 rv = -1;
2500 goto done;
2501 }
2502
2503 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2504 if (rv <= 0)
2505 goto done;
2506 for (int i = 0; i < rv; i++)
2507 {
2508 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2509 {
2510 /* We should remove mq epoll fd from events. */
2511 rv--;
2512 if (i != rv)
2513 {
2514 events[i].events = events[rv].events;
2515 events[i].data.u64 = events[rv].data.u64;
2516 }
2517 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2518 if (PREDICT_TRUE (num_ev > 0))
2519 rv += num_ev;
2520 break;
2521 }
2522 }
2523
2524done:
2525 return rv;
2526}
2527
Dave Wallace048b1d62018-01-03 22:24:41 -05002528int
2529epoll_pwait (int epfd, struct epoll_event *events,
2530 int maxevents, int timeout, const sigset_t * sigmask)
2531{
hanlin4266d4d2020-05-19 17:34:17 +08002532 if (vls_use_eventfd ())
2533 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2534 sigmask);
2535 else
2536 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002537}
2538
2539int
2540epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2541{
hanlin4266d4d2020-05-19 17:34:17 +08002542 if (vls_use_eventfd ())
2543 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2544 else
2545 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002546}
2547
2548int
2549poll (struct pollfd *fds, nfds_t nfds, int timeout)
2550{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002551 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002552 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002553 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002554 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002555 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002556
Florin Coraseb801d02020-09-16 17:44:58 -07002557 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002558
Florin Coras4dee8cd2019-01-29 21:28:16 -08002559 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2560 clib_time_init (&ldpw->clib_time);
2561
2562 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2563 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002564
Dave Wallace048b1d62018-01-03 22:24:41 -05002565 for (i = 0; i < nfds; i++)
2566 {
Florin Coras6917b942018-11-13 22:44:54 -08002567 if (fds[i].fd < 0)
2568 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002569
Florin Coras7baeb712019-01-04 17:05:43 -08002570 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2571 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002572 {
2573 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002574 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002575 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002576 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002577 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002578#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002579 if (fds[i].events & POLLRDNORM)
2580 vp->events |= POLLIN;
2581 if (fds[i].events & POLLWRNORM)
2582 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002583#endif
Florin Coras6917b942018-11-13 22:44:54 -08002584 vp->revents = fds[i].revents;
2585 }
2586 else
2587 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002588 vec_add1 (ldpw->libc_poll, fds[i]);
2589 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002590 }
2591 }
2592
Dave Wallace048b1d62018-01-03 22:24:41 -05002593 do
2594 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002595 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002596 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002597 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002598 if (rv < 0)
2599 {
2600 errno = -rv;
2601 rv = -1;
2602 goto done;
2603 }
2604 else
2605 n_revents += rv;
2606 }
2607
Florin Corasdfe4cf42018-11-28 22:13:45 -08002608 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002609 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002610 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002611 if (rv < 0)
2612 goto done;
2613 else
2614 n_revents += rv;
2615 }
2616
2617 if (n_revents)
2618 {
2619 rv = n_revents;
2620 goto done;
2621 }
2622 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002623 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002624 rv = 0;
2625
2626done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002627 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002628 {
2629 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002630 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002631#ifdef __USE_XOPEN2K
2632 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2633 (fds[vp->fds_ndx].events & POLLRDNORM))
2634 fds[vp->fds_ndx].revents |= POLLRDNORM;
2635 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2636 (fds[vp->fds_ndx].events & POLLWRNORM))
2637 fds[vp->fds_ndx].revents |= POLLWRNORM;
2638#endif
2639 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002640 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002641
Florin Corasdfe4cf42018-11-28 22:13:45 -08002642 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002643 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002644 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002645 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002646 vec_reset_length (ldpw->libc_poll_idxs);
2647 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002648
Dave Wallace048b1d62018-01-03 22:24:41 -05002649 return rv;
2650}
2651
2652#ifdef USE_GNU
2653int
2654ppoll (struct pollfd *fds, nfds_t nfds,
2655 const struct timespec *timeout, const sigset_t * sigmask)
2656{
Florin Coras5f33d0d2021-06-02 21:22:21 -07002657 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002658
2659 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2660 errno = ENOSYS;
2661
2662
2663 return -1;
2664}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002665#endif
2666
Dave Wallace2a865272018-02-07 21:00:42 -05002667void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002668
Dave Wallace2a865272018-02-07 21:00:42 -05002669void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002670
Dave Wallace048b1d62018-01-03 22:24:41 -05002671/*
2672 * This function is called when the library is loaded
2673 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002674void
Dave Wallace2a865272018-02-07 21:00:42 -05002675ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002676{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002677 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002678 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002679 {
2680 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2681 getpid ());
2682 _exit (1);
2683 }
Dave Wallace69d01192018-02-22 16:22:09 -05002684 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002685 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002686}
2687
2688/*
2689 * This function is called when the library is unloaded
2690 */
2691void
Dave Wallace2a865272018-02-07 21:00:42 -05002692ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002693{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002694 /*
2695 swrap_destructor ();
2696 if (ldp->init)
2697 ldp->init = 0;
2698 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002699
2700 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002701 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002702 */
Dave Wallace69d01192018-02-22 16:22:09 -05002703 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002704 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2705 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002706}
2707
2708
2709/*
2710 * fd.io coding-style-patch-verification: ON
2711 *
2712 * Local Variables:
2713 * eval: (c-set-style "gnu")
2714 * End:
2715 */