blob: 522e85d9719bab85a30dfebae248145eb2d6fbf8 [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>
Dou Chao243a0432022-11-29 19:41:34 +080024#include <linux/udp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070025
Dave Wallace2a865272018-02-07 21:00:42 -050026#include <vcl/ldp_socket_wrapper.h>
27#include <vcl/ldp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070028#include <sys/time.h>
29
Florin Coras7baeb712019-01-04 17:05:43 -080030#include <vcl/vcl_locked.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050031#include <vppinfra/time.h>
32#include <vppinfra/bitmap.h>
Florin Coras30e273b2018-11-27 00:04:59 -080033#include <vppinfra/lock.h>
34#include <vppinfra/pool.h>
35#include <vppinfra/hash.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070036
37#define HAVE_CONSTRUCTOR_ATTRIBUTE
38#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
39#define CONSTRUCTOR_ATTRIBUTE \
40 __attribute__ ((constructor))
41#else
42#define CONSTRUCTOR_ATTRIBUTE
43#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
44
45#define HAVE_DESTRUCTOR_ATTRIBUTE
46#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
47#define DESTRUCTOR_ATTRIBUTE \
48 __attribute__ ((destructor))
49#else
50#define DESTRUCTOR_ATTRIBUTE
51#endif
52
Florin Corasdfe4cf42018-11-28 22:13:45 -080053#define LDP_MAX_NWORKERS 32
54
Florin Corasdfe4cf42018-11-28 22:13:45 -080055typedef struct ldp_worker_ctx_
Dave Wallace048b1d62018-01-03 22:24:41 -050056{
Dave Wallace048b1d62018-01-03 22:24:41 -050057 u8 *io_buffer;
58 clib_time_t clib_time;
Florin Corasdfe4cf42018-11-28 22:13:45 -080059
60 /*
61 * Select state
62 */
Dave Wallace048b1d62018-01-03 22:24:41 -050063 clib_bitmap_t *rd_bitmap;
64 clib_bitmap_t *wr_bitmap;
65 clib_bitmap_t *ex_bitmap;
Florin Coras294afe22019-01-07 17:49:17 -080066 clib_bitmap_t *si_rd_bitmap;
67 clib_bitmap_t *si_wr_bitmap;
68 clib_bitmap_t *si_ex_bitmap;
Dave Wallace048b1d62018-01-03 22:24:41 -050069 clib_bitmap_t *libc_rd_bitmap;
70 clib_bitmap_t *libc_wr_bitmap;
71 clib_bitmap_t *libc_ex_bitmap;
Florin Corasdfe4cf42018-11-28 22:13:45 -080072
73 /*
74 * Poll state
75 */
Dave Wallace048b1d62018-01-03 22:24:41 -050076 vcl_poll_t *vcl_poll;
Florin Coras6917b942018-11-13 22:44:54 -080077 struct pollfd *libc_poll;
78 u16 *libc_poll_idxs;
Florin Corasdfe4cf42018-11-28 22:13:45 -080079
80 /*
81 * Epoll state
82 */
Dave Wallace048b1d62018-01-03 22:24:41 -050083 u8 epoll_wait_vcl;
hanlin4266d4d2020-05-19 17:34:17 +080084 u8 mq_epfd_added;
Florin Coras99368312018-08-02 10:45:44 -070085 int vcl_mq_epfd;
Florin Corasdfe4cf42018-11-28 22:13:45 -080086
87} ldp_worker_ctx_t;
88
Florin Coras294afe22019-01-07 17:49:17 -080089/* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
90 * they are the same size */
91STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
92 "ldp bitmap size mismatch");
93STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
94 "ldp bitmap size mismatch");
95
Florin Corasdfe4cf42018-11-28 22:13:45 -080096typedef struct
97{
98 ldp_worker_ctx_t *workers;
99 int init;
100 char app_name[LDP_APP_NAME_MAX];
Florin Coras7baeb712019-01-04 17:05:43 -0800101 u32 vlsh_bit_val;
102 u32 vlsh_bit_mask;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800103 u32 debug;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800104
105 /** vcl needs next epoll_create to go to libc_epoll */
106 u8 vcl_needs_real_epoll;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800107
108 /**
109 * crypto state used only for testing
110 */
111 u8 transparent_tls;
112 u32 ckpair_index;
Dave Wallace2a865272018-02-07 21:00:42 -0500113} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800114
Dave Wallace2a865272018-02-07 21:00:42 -0500115#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700116
Florin Coras99368312018-08-02 10:45:44 -0700117#define LDBG(_lvl, _fmt, _args...) \
118 if (ldp->debug > _lvl) \
hanlin9f3f18f2019-12-30 16:25:20 +0800119 { \
120 int errno_saved = errno; \
Florin Coras585c86a2020-10-16 17:57:36 -0700121 fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \
hanlin9f3f18f2019-12-30 16:25:20 +0800122 errno = errno_saved; \
123 }
Florin Coras99368312018-08-02 10:45:44 -0700124
Dave Wallace2a865272018-02-07 21:00:42 -0500125static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800126 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
127 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500128 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800129 .transparent_tls = 0,
Florin Corasa5a9efd2021-01-05 17:03:29 -0800130 .ckpair_index = ~0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500131};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700132
Dave Wallace2a865272018-02-07 21:00:42 -0500133static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700134
Florin Corasdfe4cf42018-11-28 22:13:45 -0800135static inline ldp_worker_ctx_t *
136ldp_worker_get_current (void)
137{
138 return (ldp->workers + vppcom_worker_index ());
139}
140
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700141/*
142 * RETURN: 0 on success or -1 on error.
143 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500144static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500145ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700146{
Benoît Ganne747b3d82019-08-21 18:27:23 +0200147 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
148 "ldp-%d-%s", getpid (), app_name);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700149}
150
Dave Wallace048b1d62018-01-03 22:24:41 -0500151static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500152ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700153{
Dave Wallace2a865272018-02-07 21:00:42 -0500154 if (ldp->app_name[0] == '\0')
155 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500156
Dave Wallace2a865272018-02-07 21:00:42 -0500157 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700158}
159
Dave Wallace048b1d62018-01-03 22:24:41 -0500160static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800161ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500162{
Florin Coras7baeb712019-01-04 17:05:43 -0800163 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500164}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700165
Florin Coras7baeb712019-01-04 17:05:43 -0800166static inline vls_handle_t
167ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500168{
Florin Coras7baeb712019-01-04 17:05:43 -0800169 if (fd < ldp->vlsh_bit_val)
170 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700171
Florin Coras7baeb712019-01-04 17:05:43 -0800172 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500173}
174
Florin Coras2d9b4272019-03-11 10:14:37 -0700175static void
176ldp_alloc_workers (void)
177{
178 if (ldp->workers)
179 return;
180 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
181}
182
Florin Coras5f33d0d2021-06-02 21:22:21 -0700183static int
Dave Wallace2a865272018-02-07 21:00:42 -0500184ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700185{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800186 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700187 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700188
Florin Coras5f33d0d2021-06-02 21:22:21 -0700189 ASSERT (!ldp->init);
Florin Coras99368312018-08-02 10:45:44 -0700190
191 ldp->init = 1;
192 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800193 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700194 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700195 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800196 ldp->vcl_needs_real_epoll = 0;
197 if (rv == VPPCOM_EEXIST)
198 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800199 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
200 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700201 ldp->init = 0;
202 return rv;
203 }
204 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700205 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800206 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700207
208 char *env_var_str = getenv (LDP_ENV_DEBUG);
209 if (env_var_str)
210 {
211 u32 tmp;
212 if (sscanf (env_var_str, "%u", &tmp) != 1)
213 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
214 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
215 env_var_str);
216 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700217 {
Florin Coras99368312018-08-02 10:45:44 -0700218 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800219 LDBG (0, "configured LDP debug level (%u) from env var "
220 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700221 }
222 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500223
Florin Coras99368312018-08-02 10:45:44 -0700224 env_var_str = getenv (LDP_ENV_APP_NAME);
225 if (env_var_str)
226 {
227 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800228 LDBG (0, "configured LDP app name (%s) from the env var "
229 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700230 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500231
Florin Coras99368312018-08-02 10:45:44 -0700232 env_var_str = getenv (LDP_ENV_SID_BIT);
233 if (env_var_str)
234 {
235 u32 sb;
236 if (sscanf (env_var_str, "%u", &sb) != 1)
237 {
Florin Coras294afe22019-01-07 17:49:17 -0800238 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
239 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
240 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700241 }
242 else if (sb < LDP_SID_BIT_MIN)
243 {
Florin Coras7baeb712019-01-04 17:05:43 -0800244 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
245 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500246
Florin Coras294afe22019-01-07 17:49:17 -0800247 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
248 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
249 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
250 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700251 }
252 else if (sb > LDP_SID_BIT_MAX)
253 {
Florin Coras7baeb712019-01-04 17:05:43 -0800254 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
255 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500256
Florin Coras294afe22019-01-07 17:49:17 -0800257 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
258 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
259 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
260 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500261 }
262 else
263 {
Florin Coras7baeb712019-01-04 17:05:43 -0800264 ldp->vlsh_bit_val = (1 << sb);
265 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700266
Florin Coras05ecfcc2018-12-12 18:19:39 -0800267 LDBG (0, "configured LDP sid bit (%u) from "
268 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800269 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500270 }
Florin Coras294afe22019-01-07 17:49:17 -0800271
272 /* Make sure there are enough bits in the fd set for vcl sessions */
273 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
274 {
wanghanlin1eb8fea2021-10-14 11:10:26 +0800275 /* Only valid for select/pselect, so just WARNING and not exit */
276 LDBG (0,
277 "WARNING: LDP vlsh bit value %d > FD_SETSIZE/2 %d, "
278 "select/pselect not supported now!",
Florin Coras294afe22019-01-07 17:49:17 -0800279 ldp->vlsh_bit_val, FD_SETSIZE / 2);
Florin Coras294afe22019-01-07 17:49:17 -0800280 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500281 }
Yu Ping7b74b072019-05-08 00:40:24 +0800282 env_var_str = getenv (LDP_ENV_TLS_TRANS);
283 if (env_var_str)
284 {
285 ldp->transparent_tls = 1;
286 }
Florin Coras99368312018-08-02 10:45:44 -0700287
Florin Coras4dee8cd2019-01-29 21:28:16 -0800288 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100289 pool_foreach (ldpw, ldp->workers) {
Florin Coras4dee8cd2019-01-29 21:28:16 -0800290 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100291 }
Florin Coras4dee8cd2019-01-29 21:28:16 -0800292 /* *INDENT-ON* */
293
Florin Coras05ecfcc2018-12-12 18:19:39 -0800294 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700295
296 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500297}
298
Florin Coras5f33d0d2021-06-02 21:22:21 -0700299#define ldp_init_check() \
300 if (PREDICT_FALSE (!ldp->init)) \
301 { \
302 if ((errno = -ldp_init ())) \
303 return -1; \
304 }
305
Dave Wallace048b1d62018-01-03 22:24:41 -0500306int
307close (int fd)
308{
Florin Coras7baeb712019-01-04 17:05:43 -0800309 vls_handle_t vlsh;
310 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500311
Florin Coras5f33d0d2021-06-02 21:22:21 -0700312 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500313
Florin Coras7baeb712019-01-04 17:05:43 -0800314 vlsh = ldp_fd_to_vlsh (fd);
315 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500316 {
Florin Coras7baeb712019-01-04 17:05:43 -0800317 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500318 if (epfd > 0)
319 {
Florin Coras7baeb712019-01-04 17:05:43 -0800320 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500321
322 rv = libc_close (epfd);
323 if (rv < 0)
324 {
325 u32 size = sizeof (epfd);
326 epfd = 0;
327
Florin Coras7baeb712019-01-04 17:05:43 -0800328 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500329 }
330 }
331 else if (PREDICT_FALSE (epfd < 0))
332 {
333 errno = -epfd;
334 rv = -1;
335 goto done;
336 }
337
Florin Coras7baeb712019-01-04 17:05:43 -0800338 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500339
Florin Coras7baeb712019-01-04 17:05:43 -0800340 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500341 if (rv != VPPCOM_OK)
342 {
343 errno = -rv;
344 rv = -1;
345 }
346 }
347 else
348 {
Florin Coras7baeb712019-01-04 17:05:43 -0800349 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500350 rv = libc_close (fd);
351 }
352
353done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500354 return rv;
355}
356
357ssize_t
358read (int fd, void *buf, size_t nbytes)
359{
Florin Coras7baeb712019-01-04 17:05:43 -0800360 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500361 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500362
Florin Coras5f33d0d2021-06-02 21:22:21 -0700363 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500364
Florin Coras7baeb712019-01-04 17:05:43 -0800365 vlsh = ldp_fd_to_vlsh (fd);
366 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500367 {
Florin Coras7baeb712019-01-04 17:05:43 -0800368 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500369 if (size < 0)
370 {
371 errno = -size;
372 size = -1;
373 }
374 }
375 else
376 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500377 size = libc_read (fd, buf, nbytes);
378 }
379
Dave Wallace048b1d62018-01-03 22:24:41 -0500380 return size;
381}
382
383ssize_t
384readv (int fd, const struct iovec * iov, int iovcnt)
385{
Dave Wallace8aaba562018-01-18 17:21:19 -0500386 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800387 vls_handle_t vlsh;
388 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500389
Florin Coras5f33d0d2021-06-02 21:22:21 -0700390 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500391
Florin Coras7baeb712019-01-04 17:05:43 -0800392 vlsh = ldp_fd_to_vlsh (fd);
393 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500394 {
Florin Coras067f9542020-02-14 05:33:46 +0000395 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500396 {
Florin Coras067f9542020-02-14 05:33:46 +0000397 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
398 if (rv <= 0)
399 break;
400 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500401 {
Florin Coras067f9542020-02-14 05:33:46 +0000402 total += rv;
403 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500404 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700405 }
406 }
Florin Coras067f9542020-02-14 05:33:46 +0000407 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700408 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500409 errno = -rv;
410 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700411 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500412 else
413 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700414 }
415 else
416 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500417 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700418 }
419
Dave Wallace048b1d62018-01-03 22:24:41 -0500420 return size;
421}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700422
Dave Wallace048b1d62018-01-03 22:24:41 -0500423ssize_t
424write (int fd, const void *buf, size_t nbytes)
425{
Florin Coras7baeb712019-01-04 17:05:43 -0800426 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500427 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500428
Florin Coras5f33d0d2021-06-02 21:22:21 -0700429 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500430
Florin Coras7baeb712019-01-04 17:05:43 -0800431 vlsh = ldp_fd_to_vlsh (fd);
432 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500433 {
Florin Coras7baeb712019-01-04 17:05:43 -0800434 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500435 if (size < 0)
436 {
437 errno = -size;
438 size = -1;
439 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700440 }
441 else
442 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500443 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700444 }
445
Dave Wallace048b1d62018-01-03 22:24:41 -0500446 return size;
447}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700448
Dave Wallace048b1d62018-01-03 22:24:41 -0500449ssize_t
450writev (int fd, const struct iovec * iov, int iovcnt)
451{
Dave Wallace048b1d62018-01-03 22:24:41 -0500452 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800453 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500454 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500455
Florin Coras5f33d0d2021-06-02 21:22:21 -0700456 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500457
Florin Coras7baeb712019-01-04 17:05:43 -0800458 vlsh = ldp_fd_to_vlsh (fd);
459 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500460 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000461 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500462 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000463 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
464 if (rv < 0)
465 break;
466 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500467 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000468 total += rv;
469 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500470 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500471 }
472 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500473
Florin Coraseda1b8c2020-03-23 16:00:35 +0000474 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500475 {
476 errno = -rv;
477 size = -1;
478 }
479 else
480 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700481 }
482 else
483 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500484 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700485 }
486
Dave Wallace048b1d62018-01-03 22:24:41 -0500487 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700488}
489
Florin Coras0ab36f52020-05-26 19:45:45 +0000490static int
491fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700492{
Florin Coras7baeb712019-01-04 17:05:43 -0800493 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700494 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800495
496 vlsh = ldp_fd_to_vlsh (fd);
497 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
498 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700499 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500500 int flags = va_arg (ap, int);
501 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700502
Dave Wallace048b1d62018-01-03 22:24:41 -0500503 size = sizeof (flags);
504 rv = -EOPNOTSUPP;
505 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700506 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500507 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800508 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500509 break;
510
511 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800512 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500513 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800514 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700515 break;
Florin Coras173bae32018-11-16 18:56:28 -0800516 case F_SETFD:
517 /* TODO handle this */
518 LDBG (0, "F_SETFD ignored flags %u", flags);
519 rv = 0;
520 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700521 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500522 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700523 break;
524 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500525 if (rv < 0)
526 {
527 errno = -rv;
528 rv = -1;
529 }
530 }
531 else
532 {
Carl Smithe16707b2019-11-13 14:37:39 +1300533#ifdef HAVE_FCNTL64
534 rv = libc_vfcntl64 (fd, cmd, ap);
535#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500536 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300537#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700538 }
539
Florin Coras0ab36f52020-05-26 19:45:45 +0000540 return rv;
541}
542
543int
544fcntl (int fd, int cmd, ...)
545{
546 va_list ap;
547 int rv;
548
Florin Coras5f33d0d2021-06-02 21:22:21 -0700549 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000550
551 va_start (ap, cmd);
552 rv = fcntl_internal (fd, cmd, ap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500553 va_end (ap);
554
Dave Wallace048b1d62018-01-03 22:24:41 -0500555 return rv;
556}
557
558int
Florin Corasd7586d52020-04-29 02:19:51 +0000559fcntl64 (int fd, int cmd, ...)
560{
561 va_list ap;
562 int rv;
563
Florin Coras5f33d0d2021-06-02 21:22:21 -0700564 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000565
Florin Corasd7586d52020-04-29 02:19:51 +0000566 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000567 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000568 va_end (ap);
569 return rv;
570}
571
572int
Dave Wallace048b1d62018-01-03 22:24:41 -0500573ioctl (int fd, unsigned long int cmd, ...)
574{
Florin Coras7baeb712019-01-04 17:05:43 -0800575 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500576 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800577 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500578
Florin Coras5f33d0d2021-06-02 21:22:21 -0700579 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500580
581 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500582
Florin Coras7baeb712019-01-04 17:05:43 -0800583 vlsh = ldp_fd_to_vlsh (fd);
584 if (vlsh != VLS_INVALID_HANDLE)
585 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500586 switch (cmd)
587 {
588 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800589 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500590 break;
591
592 case FIONBIO:
593 {
594 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
595 u32 size = sizeof (flags);
596
597 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
598 * non-blocking, the flags should be read here and merged
599 * with O_NONBLOCK.
600 */
Florin Coras7baeb712019-01-04 17:05:43 -0800601 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500602 }
603 break;
604
605 default:
606 rv = -EOPNOTSUPP;
607 break;
608 }
609 if (rv < 0)
610 {
611 errno = -rv;
612 rv = -1;
613 }
614 }
615 else
616 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500617 rv = libc_vioctl (fd, cmd, ap);
618 }
619
Dave Wallace048b1d62018-01-03 22:24:41 -0500620 va_end (ap);
621 return rv;
622}
623
Florin Coras294afe22019-01-07 17:49:17 -0800624always_inline void
625ldp_select_init_maps (fd_set * __restrict original,
626 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
627 clib_bitmap_t ** vclb, int nfds, u32 minbits,
628 u32 n_bytes, uword * si_bits, uword * libc_bits)
629{
630 uword si_bits_set, libc_bits_set;
631 vls_handle_t vlsh;
632 int fd;
633
634 clib_bitmap_validate (*vclb, minbits);
635 clib_bitmap_validate (*libcb, minbits);
636 clib_bitmap_validate (*resultb, minbits);
637 clib_memcpy_fast (*resultb, original, n_bytes);
638 memset (original, 0, n_bytes);
639
640 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100641 clib_bitmap_foreach (fd, *resultb) {
Florin Coras294afe22019-01-07 17:49:17 -0800642 if (fd > nfds)
643 break;
644 vlsh = ldp_fd_to_vlsh (fd);
645 if (vlsh == VLS_INVALID_HANDLE)
646 clib_bitmap_set_no_check (*libcb, fd, 1);
647 else
Florin Corascbce80a2020-04-20 01:32:38 +0000648 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100649 }
Florin Coras294afe22019-01-07 17:49:17 -0800650 /* *INDENT-ON* */
651
652 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
653 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000654 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800655
656 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
657 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
658}
659
660always_inline int
661ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
662{
663 vls_handle_t vlsh;
664 uword si;
665 int fd;
666
667 if (!libcb)
668 return 0;
669
670 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100671 clib_bitmap_foreach (si, vclb) {
Florin Coras294afe22019-01-07 17:49:17 -0800672 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000673 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800674 fd = ldp_vlsh_to_fd (vlsh);
675 if (PREDICT_FALSE (fd < 0))
676 {
677 errno = EBADFD;
678 return -1;
679 }
680 FD_SET (fd, libcb);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100681 }
Florin Coras294afe22019-01-07 17:49:17 -0800682 /* *INDENT-ON* */
683
684 return 0;
685}
686
687always_inline void
688ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
689{
690 uword fd;
691
Florin Coras78b5fa62019-02-21 20:04:15 -0800692 if (!libcb)
693 return;
694
Florin Coras294afe22019-01-07 17:49:17 -0800695 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100696 clib_bitmap_foreach (fd, result)
Florin Coras294afe22019-01-07 17:49:17 -0800697 FD_SET ((int)fd, libcb);
Florin Coras294afe22019-01-07 17:49:17 -0800698 /* *INDENT-ON* */
699}
700
Dave Wallace048b1d62018-01-03 22:24:41 -0500701int
Dave Wallace2a865272018-02-07 21:00:42 -0500702ldp_pselect (int nfds, fd_set * __restrict readfds,
703 fd_set * __restrict writefds,
704 fd_set * __restrict exceptfds,
705 const struct timespec *__restrict timeout,
706 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500707{
Florin Coras294afe22019-01-07 17:49:17 -0800708 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800709 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800710 struct timespec libc_tspec = { 0 };
711 f64 time_out, vcl_timeout = 0;
712 uword si_bits, libc_bits;
713 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500714
715 if (nfds < 0)
716 {
717 errno = EINVAL;
718 return -1;
719 }
720
Florin Coras4dee8cd2019-01-29 21:28:16 -0800721 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
722 clib_time_init (&ldpw->clib_time);
723
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500724 if (timeout)
725 {
726 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800727 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500728
liuyacanf71796e2021-08-02 10:01:39 +0800729 time_out += clib_time_now (&ldpw->clib_time);
730
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500731 /* select as fine grained sleep */
732 if (!nfds)
733 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800734 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500735 ;
736 return 0;
737 }
738 }
739 else if (!nfds)
740 {
741 errno = EINVAL;
742 return -1;
743 }
744 else
745 time_out = -1;
746
Florin Coras7baeb712019-01-04 17:05:43 -0800747 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500748 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500749 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
750 timeout, sigmask);
751 goto done;
752 }
753
Florin Coras294afe22019-01-07 17:49:17 -0800754 si_bits = libc_bits = 0;
755 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800756
Dave Wallace048b1d62018-01-03 22:24:41 -0500757 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800758 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
759 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
760 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500761 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800762 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
763 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
764 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500765 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800766 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
767 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
768 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500769
Florin Coras294afe22019-01-07 17:49:17 -0800770 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500771 {
772 errno = EINVAL;
773 rv = -1;
774 goto done;
775 }
776
Florin Coras78b5fa62019-02-21 20:04:15 -0800777 if (!si_bits)
778 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800779
Dave Wallace048b1d62018-01-03 22:24:41 -0500780 do
781 {
Florin Coras294afe22019-01-07 17:49:17 -0800782 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500783 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500784 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800785 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000786 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500787 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500788 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800789 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000790 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500791 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500792 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800793 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000794 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500795 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800796
Florin Coras0ef8ef22019-01-18 08:37:13 -0800797 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
798 writefds ? ldpw->wr_bitmap : NULL,
799 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800800 if (rv < 0)
801 {
802 errno = -rv;
803 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000804 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800805 }
806 else if (rv > 0)
807 {
808 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
809 {
810 rv = -1;
811 goto done;
812 }
813
814 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
815 {
816 rv = -1;
817 goto done;
818 }
819
820 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
821 {
822 rv = -1;
823 goto done;
824 }
825 bits_set = rv;
826 }
827 }
828 if (libc_bits)
829 {
830 if (readfds)
831 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
832 vec_len (ldpw->libc_rd_bitmap) *
833 sizeof (clib_bitmap_t));
834 if (writefds)
835 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
836 vec_len (ldpw->libc_wr_bitmap) *
837 sizeof (clib_bitmap_t));
838 if (exceptfds)
839 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
840 vec_len (ldpw->libc_ex_bitmap) *
841 sizeof (clib_bitmap_t));
842
Dave Wallace048b1d62018-01-03 22:24:41 -0500843 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800844 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
845 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
846 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
847 &libc_tspec, sigmask);
848 if (rv > 0)
849 {
850 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
851 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
852 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
853 bits_set += rv;
854 }
855 }
856
857 if (bits_set)
858 {
859 rv = bits_set;
860 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500861 }
862 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800863 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500864 rv = 0;
865
866done:
867 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800868 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800869 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800870 clib_bitmap_zero (ldpw->libc_rd_bitmap);
871 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800872 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800873 clib_bitmap_zero (ldpw->libc_wr_bitmap);
874 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800875 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800876 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500877
Dave Wallace048b1d62018-01-03 22:24:41 -0500878 return rv;
879}
880
881int
882select (int nfds, fd_set * __restrict readfds,
883 fd_set * __restrict writefds,
884 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
885{
886 struct timespec tspec;
887
888 if (timeout)
889 {
890 tspec.tv_sec = timeout->tv_sec;
891 tspec.tv_nsec = timeout->tv_usec * 1000;
892 }
Dave Wallace2a865272018-02-07 21:00:42 -0500893 return ldp_pselect (nfds, readfds, writefds, exceptfds,
894 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500895}
896
897#ifdef __USE_XOPEN2K
898int
899pselect (int nfds, fd_set * __restrict readfds,
900 fd_set * __restrict writefds,
901 fd_set * __restrict exceptfds,
902 const struct timespec *__restrict timeout,
903 const __sigset_t * __restrict sigmask)
904{
Dave Wallace2a865272018-02-07 21:00:42 -0500905 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500906}
907#endif
908
Yu Ping7b74b072019-05-08 00:40:24 +0800909/* If transparent TLS mode is turned on, then ldp will load key and cert.
910 */
911static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800912load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800913{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800914 char *cert_str = getenv (LDP_ENV_TLS_CERT);
915 char *key_str = getenv (LDP_ENV_TLS_KEY);
916 char cert_buf[4096], key_buf[4096];
917 int cert_size, key_size;
918 vppcom_cert_key_pair_t crypto;
919 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800920 FILE *fp;
921
Florin Corasa5a9efd2021-01-05 17:03:29 -0800922 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800923 {
924 LDBG (0, "ERROR: failed to read LDP environment %s\n",
925 LDP_ENV_TLS_CERT);
926 return -1;
927 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800928
929 fp = fopen (cert_str, "r");
930 if (fp == NULL)
931 {
932 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
933 return -1;
934 }
935 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
936 fclose (fp);
937
938 fp = fopen (key_str, "r");
939 if (fp == NULL)
940 {
941 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
942 return -1;
943 }
944 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
945 fclose (fp);
946
947 crypto.cert = cert_buf;
948 crypto.key = key_buf;
949 crypto.cert_len = cert_size;
950 crypto.key_len = key_size;
951 ckp_index = vppcom_add_cert_key_pair (&crypto);
952 if (ckp_index < 0)
953 {
954 LDBG (0, "ERROR: failed to add cert key pair\n");
955 return -1;
956 }
957
958 ldp->ckpair_index = ckp_index;
959
Yu Ping7b74b072019-05-08 00:40:24 +0800960 return 0;
961}
962
963static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800964assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800965{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800966 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800967
Florin Corasa5a9efd2021-01-05 17:03:29 -0800968 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
969 return -1;
970
971 ckp_len = sizeof (ldp->ckpair_index);
liuyacan276675e2021-08-30 13:24:53 +0800972 return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800973}
974
Dave Wallace048b1d62018-01-03 22:24:41 -0500975int
976socket (int domain, int type, int protocol)
977{
Florin Coras7baeb712019-01-04 17:05:43 -0800978 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500979 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800980 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500981
Florin Coras5f33d0d2021-06-02 21:22:21 -0700982 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500983
984 if (((domain == AF_INET) || (domain == AF_INET6)) &&
985 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
986 {
Yu Ping7b74b072019-05-08 00:40:24 +0800987 u8 proto;
988 if (ldp->transparent_tls)
989 {
990 proto = VPPCOM_PROTO_TLS;
991 }
992 else
993 proto = ((sock_type == SOCK_DGRAM) ?
994 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500995
Florin Coras7baeb712019-01-04 17:05:43 -0800996 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
997 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500998
Florin Coras7baeb712019-01-04 17:05:43 -0800999 vlsh = vls_create (proto, is_nonblocking);
1000 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001001 {
Florin Coras7baeb712019-01-04 17:05:43 -08001002 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001003 rv = -1;
1004 }
1005 else
1006 {
Yu Ping7b74b072019-05-08 00:40:24 +08001007 if (ldp->transparent_tls)
1008 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001009 if (assign_cert_key_pair (vlsh) < 0)
1010 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001011 }
Florin Coras7baeb712019-01-04 17:05:43 -08001012 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001013 }
1014 }
1015 else
1016 {
Florin Coras7baeb712019-01-04 17:05:43 -08001017 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001018 rv = libc_socket (domain, type, protocol);
1019 }
1020
Dave Wallace048b1d62018-01-03 22:24:41 -05001021 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001022}
1023
1024/*
1025 * Create two new sockets, of type TYPE in domain DOMAIN and using
1026 * protocol PROTOCOL, which are connected to each other, and put file
1027 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1028 * one will be chosen automatically.
1029 * Returns 0 on success, -1 for errors.
1030 * */
1031int
Dave Wallace048b1d62018-01-03 22:24:41 -05001032socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001033{
Florin Coras7baeb712019-01-04 17:05:43 -08001034 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001035
Florin Coras5f33d0d2021-06-02 21:22:21 -07001036 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001037
1038 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1039 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001040 {
Florin Coras7baeb712019-01-04 17:05:43 -08001041 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001042 errno = ENOSYS;
1043 rv = -1;
1044 }
1045 else
1046 {
Florin Coras7baeb712019-01-04 17:05:43 -08001047 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001048 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001049 }
1050
Dave Wallace048b1d62018-01-03 22:24:41 -05001051 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001052}
1053
1054int
Dave Wallace048b1d62018-01-03 22:24:41 -05001055bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001056{
Florin Coras7baeb712019-01-04 17:05:43 -08001057 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001058 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001059
Florin Coras5f33d0d2021-06-02 21:22:21 -07001060 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001061
Florin Coras7baeb712019-01-04 17:05:43 -08001062 vlsh = ldp_fd_to_vlsh (fd);
1063 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001064 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001065 vppcom_endpt_t ep;
1066
Dave Wallace048b1d62018-01-03 22:24:41 -05001067 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001068 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001069 case AF_INET:
1070 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001071 {
Florin Coras7baeb712019-01-04 17:05:43 -08001072 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1073 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001074 errno = EINVAL;
1075 rv = -1;
1076 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001077 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001078 ep.is_ip4 = VPPCOM_IS_IP4;
1079 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1080 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1081 break;
1082
1083 case AF_INET6:
1084 if (len != sizeof (struct sockaddr_in6))
1085 {
Florin Coras7baeb712019-01-04 17:05:43 -08001086 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1087 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001088 errno = EINVAL;
1089 rv = -1;
1090 goto done;
1091 }
1092 ep.is_ip4 = VPPCOM_IS_IP6;
1093 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1094 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001095 break;
1096
1097 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001098 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1099 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001100 errno = EAFNOSUPPORT;
1101 rv = -1;
1102 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001103 }
Florin Coras7baeb712019-01-04 17:05:43 -08001104 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1105 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001106
Florin Coras7baeb712019-01-04 17:05:43 -08001107 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001108 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001109 {
1110 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001111 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001112 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001113 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001114 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001115 {
Florin Coras7baeb712019-01-04 17:05:43 -08001116 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001117 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001118 }
1119
Dave Wallace048b1d62018-01-03 22:24:41 -05001120done:
Florin Coras7baeb712019-01-04 17:05:43 -08001121 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001122
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001123 return rv;
1124}
1125
1126static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001127ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1128 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001129{
Dave Wallace048b1d62018-01-03 22:24:41 -05001130 int rv = 0;
1131 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001132
Florin Coras5f33d0d2021-06-02 21:22:21 -07001133 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001134
1135 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001136 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001137 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1138 switch (addr->sa_family)
1139 {
1140 case AF_INET:
1141 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1142 if (*len > sizeof (struct sockaddr_in))
1143 *len = sizeof (struct sockaddr_in);
1144 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1145 copy_len = *len - sa_len;
1146 if (copy_len > 0)
1147 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1148 copy_len);
1149 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001150
Dave Wallace048b1d62018-01-03 22:24:41 -05001151 case AF_INET6:
1152 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1153 if (*len > sizeof (struct sockaddr_in6))
1154 *len = sizeof (struct sockaddr_in6);
1155 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1156 copy_len = *len - sa_len;
1157 if (copy_len > 0)
1158 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1159 __in6_u.__u6_addr8, ep->ip, copy_len);
1160 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001161
Dave Wallace048b1d62018-01-03 22:24:41 -05001162 default:
1163 /* Not possible */
1164 rv = -EAFNOSUPPORT;
1165 break;
1166 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001167 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001168 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001169}
1170
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001171int
Dave Wallace048b1d62018-01-03 22:24:41 -05001172getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001173{
Florin Coras7baeb712019-01-04 17:05:43 -08001174 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001175 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001176
Florin Coras5f33d0d2021-06-02 21:22:21 -07001177 ldp_init_check ();
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001178
Florin Coras7baeb712019-01-04 17:05:43 -08001179 vlsh = ldp_fd_to_vlsh (fd);
1180 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001181 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001182 vppcom_endpt_t ep;
1183 u8 addr_buf[sizeof (struct in6_addr)];
1184 u32 size = sizeof (ep);
1185
1186 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001187
Florin Coras7baeb712019-01-04 17:05:43 -08001188 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001189 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001190 {
1191 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001192 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001193 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001194 else
1195 {
Dave Wallace2a865272018-02-07 21:00:42 -05001196 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001197 if (rv != VPPCOM_OK)
1198 {
1199 errno = -rv;
1200 rv = -1;
1201 }
1202 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001203 }
1204 else
1205 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001206 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001207 }
1208
Dave Wallace048b1d62018-01-03 22:24:41 -05001209 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001210}
1211
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001212int
Dave Wallace048b1d62018-01-03 22:24:41 -05001213connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001214{
Florin Coras7baeb712019-01-04 17:05:43 -08001215 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001216 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001217
Florin Coras5f33d0d2021-06-02 21:22:21 -07001218 ldp_init_check ();
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001219
Dave Wallace048b1d62018-01-03 22:24:41 -05001220 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001221 {
Florin Coras7baeb712019-01-04 17:05:43 -08001222 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001223 errno = EINVAL;
1224 rv = -1;
1225 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001226 }
1227
Florin Coras7baeb712019-01-04 17:05:43 -08001228 vlsh = ldp_fd_to_vlsh (fd);
1229 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001230 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001231 vppcom_endpt_t ep;
1232
Dave Wallace048b1d62018-01-03 22:24:41 -05001233 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001234 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001235 case AF_INET:
1236 if (len != sizeof (struct sockaddr_in))
1237 {
Florin Coras7baeb712019-01-04 17:05:43 -08001238 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1239 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001240 errno = EINVAL;
1241 rv = -1;
1242 goto done;
1243 }
1244 ep.is_ip4 = VPPCOM_IS_IP4;
1245 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1246 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1247 break;
1248
1249 case AF_INET6:
1250 if (len != sizeof (struct sockaddr_in6))
1251 {
Florin Coras7baeb712019-01-04 17:05:43 -08001252 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1253 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001254 errno = EINVAL;
1255 rv = -1;
1256 goto done;
1257 }
1258 ep.is_ip4 = VPPCOM_IS_IP6;
1259 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1260 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1261 break;
1262
1263 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001264 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1265 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001266 errno = EAFNOSUPPORT;
1267 rv = -1;
1268 goto done;
1269 }
Florin Coras7baeb712019-01-04 17:05:43 -08001270 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1271 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001272
Florin Coras7baeb712019-01-04 17:05:43 -08001273 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001274 if (rv != VPPCOM_OK)
1275 {
1276 errno = -rv;
1277 rv = -1;
1278 }
1279 }
1280 else
1281 {
Florin Coras7baeb712019-01-04 17:05:43 -08001282 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1283 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001284
1285 rv = libc_connect (fd, addr, len);
1286 }
1287
1288done:
Florin Coras7baeb712019-01-04 17:05:43 -08001289 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001290 return rv;
1291}
1292
1293int
1294getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1295{
Florin Coras7baeb712019-01-04 17:05:43 -08001296 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001297 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001298
Florin Coras5f33d0d2021-06-02 21:22:21 -07001299 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001300
Florin Coras7baeb712019-01-04 17:05:43 -08001301 vlsh = ldp_fd_to_vlsh (fd);
1302 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001303 {
1304 vppcom_endpt_t ep;
1305 u8 addr_buf[sizeof (struct in6_addr)];
1306 u32 size = sizeof (ep);
1307
1308 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001309 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001310 if (rv != VPPCOM_OK)
1311 {
1312 errno = -rv;
1313 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001314 }
1315 else
1316 {
Dave Wallace2a865272018-02-07 21:00:42 -05001317 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001318 if (rv != VPPCOM_OK)
1319 {
1320 errno = -rv;
1321 rv = -1;
1322 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001323 }
1324 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001326 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001327 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001328 }
1329
Dave Wallace048b1d62018-01-03 22:24:41 -05001330 return rv;
1331}
1332
1333ssize_t
1334send (int fd, const void *buf, size_t n, int flags)
1335{
Florin Coras7baeb712019-01-04 17:05:43 -08001336 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001337 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001338
Florin Coras5f33d0d2021-06-02 21:22:21 -07001339 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001340
Florin Coras7baeb712019-01-04 17:05:43 -08001341 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001342 {
Florin Coras7baeb712019-01-04 17:05:43 -08001343 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001344 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001345 {
1346 errno = -size;
1347 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001348 }
1349 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001350 else
1351 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001352 size = libc_send (fd, buf, n, flags);
1353 }
1354
Dave Wallace048b1d62018-01-03 22:24:41 -05001355 return size;
1356}
1357
1358ssize_t
1359sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1360{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001361 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001362 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001363 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001364
Florin Coras5f33d0d2021-06-02 21:22:21 -07001365 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001366
Florin Coras7baeb712019-01-04 17:05:43 -08001367 vlsh = ldp_fd_to_vlsh (out_fd);
1368 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001369 {
1370 int rv;
1371 ssize_t results = 0;
1372 size_t n_bytes_left = len;
1373 size_t bytes_to_read;
1374 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001375 u8 eagain = 0;
1376 u32 flags, flags_len = sizeof (flags);
1377
Florin Coras7baeb712019-01-04 17:05:43 -08001378 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001379 if (PREDICT_FALSE (rv != VPPCOM_OK))
1380 {
Florin Coras7baeb712019-01-04 17:05:43 -08001381 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1382 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001383
Florin Corasdfe4cf42018-11-28 22:13:45 -08001384 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001385 errno = -rv;
1386 size = -1;
1387 goto done;
1388 }
1389
1390 if (offset)
1391 {
1392 off_t off = lseek (in_fd, *offset, SEEK_SET);
1393 if (PREDICT_FALSE (off == -1))
1394 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001395 size = -1;
1396 goto done;
1397 }
1398
1399 ASSERT (off == *offset);
1400 }
1401
1402 do
1403 {
Florin Coras7baeb712019-01-04 17:05:43 -08001404 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001405 if (size < 0)
1406 {
Florin Coraseb801d02020-09-16 17:44:58 -07001407 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001408 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001409 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001410 errno = -size;
1411 size = -1;
1412 goto done;
1413 }
1414
1415 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001416 if (bytes_to_read == 0)
1417 {
1418 if (flags & O_NONBLOCK)
1419 {
1420 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001421 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001422 goto update_offset;
1423 }
1424 else
1425 continue;
1426 }
1427 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001428 vec_validate (ldpw->io_buffer, bytes_to_read);
1429 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001430 if (nbytes < 0)
1431 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001432 if (results == 0)
1433 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001434 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001435 size = -1;
1436 goto done;
1437 }
1438 goto update_offset;
1439 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001440
Florin Coras7baeb712019-01-04 17:05:43 -08001441 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001442 if (size < 0)
1443 {
1444 if (size == VPPCOM_EAGAIN)
1445 {
1446 if (flags & O_NONBLOCK)
1447 {
1448 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001449 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001450 goto update_offset;
1451 }
1452 else
1453 continue;
1454 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001455 if (results == 0)
1456 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001457 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001458 errno = -size;
1459 size = -1;
1460 goto done;
1461 }
1462 goto update_offset;
1463 }
1464
1465 results += nbytes;
1466 ASSERT (n_bytes_left >= nbytes);
1467 n_bytes_left = n_bytes_left - nbytes;
1468 }
1469 while (n_bytes_left > 0);
1470
1471 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001472 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001473 if (offset)
1474 {
1475 off_t off = lseek (in_fd, *offset, SEEK_SET);
1476 if (PREDICT_FALSE (off == -1))
1477 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001478 size = -1;
1479 goto done;
1480 }
1481
1482 ASSERT (off == *offset);
1483 *offset += results + 1;
1484 }
1485 if (eagain)
1486 {
1487 errno = EAGAIN;
1488 size = -1;
1489 }
1490 else
1491 size = results;
1492 }
1493 else
1494 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001495 size = libc_sendfile (out_fd, in_fd, offset, len);
1496 }
1497
1498done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001499 return size;
1500}
1501
1502ssize_t
1503sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1504{
1505 return sendfile (out_fd, in_fd, offset, len);
1506}
1507
1508ssize_t
1509recv (int fd, void *buf, size_t n, int flags)
1510{
Florin Coras7baeb712019-01-04 17:05:43 -08001511 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001512 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001513
Florin Coras5f33d0d2021-06-02 21:22:21 -07001514 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001515
Florin Coras7baeb712019-01-04 17:05:43 -08001516 vlsh = ldp_fd_to_vlsh (fd);
1517 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001518 {
Florin Coras7baeb712019-01-04 17:05:43 -08001519 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001520 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001521 {
1522 errno = -size;
1523 size = -1;
1524 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001525 }
1526 else
1527 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001528 size = libc_recv (fd, buf, n, flags);
1529 }
1530
Dave Wallace048b1d62018-01-03 22:24:41 -05001531 return size;
1532}
1533
Sivaprasad Tummalafdcbd382021-07-31 21:38:19 +05301534ssize_t
1535__recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1536{
1537 if (n > buflen)
1538 return -1;
1539
1540 return recv (fd, buf, n, flags);
1541}
1542
Dou Chao243a0432022-11-29 19:41:34 +08001543static inline int
1544ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n,
1545 vppcom_endpt_tlv_t *ep_tlv, int flags,
Florin Corasce17f462020-05-22 20:36:29 +00001546 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1547{
1548 vppcom_endpt_t *ep = 0;
1549 vppcom_endpt_t _ep;
1550
Dou Chao243a0432022-11-29 19:41:34 +08001551 if (ep_tlv)
1552 {
1553 _ep.app_data = *ep_tlv;
1554 }
1555
Florin Corasce17f462020-05-22 20:36:29 +00001556 if (addr)
1557 {
1558 ep = &_ep;
1559 switch (addr->sa_family)
1560 {
1561 case AF_INET:
1562 ep->is_ip4 = VPPCOM_IS_IP4;
1563 ep->ip =
1564 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1565 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1566 break;
1567
1568 case AF_INET6:
1569 ep->is_ip4 = VPPCOM_IS_IP6;
1570 ep->ip =
1571 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1572 ep->port =
1573 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1574 break;
1575
1576 default:
1577 return EAFNOSUPPORT;
1578 }
1579 }
1580
1581 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1582}
1583
1584static int
1585ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n,
1586 int flags, __SOCKADDR_ARG addr,
1587 socklen_t * __restrict addr_len)
1588{
1589 u8 src_addr[sizeof (struct sockaddr_in6)];
1590 vppcom_endpt_t ep;
1591 ssize_t size;
1592 int rv;
1593
1594 if (addr)
1595 {
1596 ep.ip = src_addr;
1597 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1598
1599 if (size > 0)
1600 {
1601 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1602 if (rv < 0)
1603 size = rv;
1604 }
1605 }
1606 else
1607 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1608
1609 return size;
1610}
1611
Dave Wallace048b1d62018-01-03 22:24:41 -05001612ssize_t
1613sendto (int fd, const void *buf, size_t n, int flags,
1614 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1615{
Florin Coras7baeb712019-01-04 17:05:43 -08001616 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001617 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001618
Florin Coras5f33d0d2021-06-02 21:22:21 -07001619 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001620
Florin Coras7baeb712019-01-04 17:05:43 -08001621 vlsh = ldp_fd_to_vlsh (fd);
wanghanlin97c6e0d2021-07-06 15:01:48 +08001622 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001623 {
Dou Chao243a0432022-11-29 19:41:34 +08001624 size = ldp_vls_sendo (vlsh, buf, n, NULL, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001625 if (size < 0)
1626 {
1627 errno = -size;
1628 size = -1;
1629 }
1630 }
1631 else
1632 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001633 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1634 }
1635
Dave Wallace048b1d62018-01-03 22:24:41 -05001636 return size;
1637}
1638
1639ssize_t
1640recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1641 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1642{
Florin Corasce17f462020-05-22 20:36:29 +00001643 vls_handle_t vlsh;
1644 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001645
Florin Coras5f33d0d2021-06-02 21:22:21 -07001646 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001647
Florin Corasce17f462020-05-22 20:36:29 +00001648 vlsh = ldp_fd_to_vlsh (fd);
1649 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001650 {
Florin Corasce17f462020-05-22 20:36:29 +00001651 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001652 if (size < 0)
1653 {
1654 errno = -size;
1655 size = -1;
1656 }
1657 }
1658 else
1659 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001660 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1661 }
1662
Dave Wallace048b1d62018-01-03 22:24:41 -05001663 return size;
1664}
1665
1666ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001667sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001668{
Florin Coras7baeb712019-01-04 17:05:43 -08001669 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001670 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001671
Florin Coras5f33d0d2021-06-02 21:22:21 -07001672 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001673
Florin Coras7baeb712019-01-04 17:05:43 -08001674 vlsh = ldp_fd_to_vlsh (fd);
1675 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001676 {
Florin Corasce17f462020-05-22 20:36:29 +00001677 struct iovec *iov = msg->msg_iov;
1678 ssize_t total = 0;
Florin Corascba1c222021-11-29 08:12:27 -08001679 int i, rv = 0;
Dou Chao243a0432022-11-29 19:41:34 +08001680 struct cmsghdr *cmsg;
1681 uint16_t *valp;
1682 vppcom_endpt_tlv_t _app_data;
1683 vppcom_endpt_tlv_t *p_app_data = NULL;
1684
1685 cmsg = CMSG_FIRSTHDR (msg);
1686 if (cmsg && cmsg->cmsg_type == UDP_SEGMENT)
1687 {
1688 p_app_data = &_app_data;
1689 valp = (void *) CMSG_DATA (cmsg);
1690 p_app_data->data_type = VCL_UDP_SEGMENT;
1691 p_app_data->data_len = sizeof (*valp);
1692 p_app_data->value = *valp;
1693 }
Florin Corasce17f462020-05-22 20:36:29 +00001694
1695 for (i = 0; i < msg->msg_iovlen; ++i)
1696 {
Dou Chao243a0432022-11-29 19:41:34 +08001697 rv =
1698 ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, p_app_data,
1699 flags, msg->msg_name, msg->msg_namelen);
Florin Corasce17f462020-05-22 20:36:29 +00001700 if (rv < 0)
1701 break;
1702 else
1703 {
1704 total += rv;
1705 if (rv < iov[i].iov_len)
1706 break;
1707 }
1708 }
1709
1710 if (rv < 0 && total == 0)
1711 {
1712 errno = -rv;
1713 size = -1;
1714 }
1715 else
1716 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001717 }
1718 else
1719 {
Florin Corasce17f462020-05-22 20:36:29 +00001720 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001721 }
1722
Dave Wallace048b1d62018-01-03 22:24:41 -05001723 return size;
1724}
1725
1726#ifdef USE_GNU
1727int
1728sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1729{
1730 ssize_t size;
1731 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001732 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001733
Florin Coras5f33d0d2021-06-02 21:22:21 -07001734 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001735
wanghanlin97c6e0d2021-07-06 15:01:48 +08001736 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001737 {
1738 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1739 errno = ENOSYS;
1740 size = -1;
1741 }
1742 else
1743 {
1744 func_str = "libc_sendmmsg";
1745
Dave Wallace2a865272018-02-07 21:00:42 -05001746 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001747 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1748 "vmessages %p, vlen %u, flags 0x%x",
1749 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1750
1751 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1752 }
1753
Dave Wallace2a865272018-02-07 21:00:42 -05001754 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001755 {
1756 if (size < 0)
1757 {
1758 int errno_val = errno;
1759 perror (func_str);
1760 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1761 "rv %d, errno = %d", getpid (), fd, fd,
1762 func_str, size, errno_val);
1763 errno = errno_val;
1764 }
1765 else
1766 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1767 getpid (), fd, fd, size, size);
1768 }
1769 return size;
1770}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001771#endif
1772
Dave Wallace048b1d62018-01-03 22:24:41 -05001773ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001774recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001775{
Florin Coras7baeb712019-01-04 17:05:43 -08001776 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001777 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001778
Florin Coras5f33d0d2021-06-02 21:22:21 -07001779 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001780
Florin Coras7baeb712019-01-04 17:05:43 -08001781 vlsh = ldp_fd_to_vlsh (fd);
1782 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001783 {
Florin Corasce17f462020-05-22 20:36:29 +00001784 struct iovec *iov = msg->msg_iov;
1785 ssize_t max_deq, total = 0;
1786 int i, rv;
1787
1788 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1789 if (!max_deq)
1790 return 0;
1791
1792 for (i = 0; i < msg->msg_iovlen; i++)
1793 {
1794 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1795 (i == 0 ? msg->msg_name : NULL),
1796 (i == 0 ? &msg->msg_namelen : NULL));
1797 if (rv <= 0)
1798 break;
1799 else
1800 {
1801 total += rv;
1802 if (rv < iov[i].iov_len)
1803 break;
1804 }
1805 if (total >= max_deq)
1806 break;
1807 }
1808
1809 if (rv < 0 && total == 0)
1810 {
1811 errno = -rv;
1812 size = -1;
1813 }
1814 else
1815 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001816 }
1817 else
1818 {
Florin Corasce17f462020-05-22 20:36:29 +00001819 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001820 }
1821
Dave Wallace048b1d62018-01-03 22:24:41 -05001822 return size;
1823}
1824
1825#ifdef USE_GNU
1826int
1827recvmmsg (int fd, struct mmsghdr *vmessages,
1828 unsigned int vlen, int flags, struct timespec *tmo)
1829{
1830 ssize_t size;
1831 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001832 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001833
Florin Coras5f33d0d2021-06-02 21:22:21 -07001834 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001835
wanghanlin97c6e0d2021-07-06 15:01:48 +08001836 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001837 {
1838 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1839 errno = ENOSYS;
1840 size = -1;
1841 }
1842 else
1843 {
1844 func_str = "libc_recvmmsg";
1845
Dave Wallace2a865272018-02-07 21:00:42 -05001846 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001847 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1848 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1849 getpid (), fd, fd, func_str, vmessages, vlen,
1850 flags, tmo);
1851
1852 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1853 }
1854
Dave Wallace2a865272018-02-07 21:00:42 -05001855 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001856 {
1857 if (size < 0)
1858 {
1859 int errno_val = errno;
1860 perror (func_str);
1861 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1862 "rv %d, errno = %d", getpid (), fd, fd,
1863 func_str, size, errno_val);
1864 errno = errno_val;
1865 }
1866 else
1867 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1868 getpid (), fd, fd, size, size);
1869 }
1870 return size;
1871}
1872#endif
1873
1874int
1875getsockopt (int fd, int level, int optname,
1876 void *__restrict optval, socklen_t * __restrict optlen)
1877{
Florin Coras7baeb712019-01-04 17:05:43 -08001878 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001879 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001880
Florin Coras5f33d0d2021-06-02 21:22:21 -07001881 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001882
Florin Coras7baeb712019-01-04 17:05:43 -08001883 vlsh = ldp_fd_to_vlsh (fd);
1884 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001885 {
1886 rv = -EOPNOTSUPP;
1887
1888 switch (level)
1889 {
1890 case SOL_TCP:
1891 switch (optname)
1892 {
1893 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001894 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1895 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001896 break;
1897 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001898 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1899 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001900 break;
1901 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001902 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1903 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001904 break;
1905 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001906 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1907 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001908 break;
1909 case TCP_INFO:
1910 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1911 {
Florin Coras7baeb712019-01-04 17:05:43 -08001912 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1913 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001914 memset (optval, 0, *optlen);
1915 rv = VPPCOM_OK;
1916 }
1917 else
1918 rv = -EFAULT;
1919 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001920 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001921 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001922 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001923 rv = 0;
1924 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001925 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001926 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1927 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001928 break;
1929 }
1930 break;
1931 case SOL_IPV6:
1932 switch (optname)
1933 {
1934 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001935 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001936 break;
1937 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001938 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1939 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001940 break;
1941 }
1942 break;
1943 case SOL_SOCKET:
1944 switch (optname)
1945 {
1946 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001947 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001948 break;
1949 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001950 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001951 break;
1952 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001953 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001954 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1955 break;
1956 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001957 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1958 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001959 break;
1960 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001961 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1962 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001963 break;
1964 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001965 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001966 break;
wanghanlin0674f852021-02-22 10:38:36 +08001967 case SO_REUSEPORT:
1968 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1969 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001970 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001971 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 break;
wanghanlin0674f852021-02-22 10:38:36 +08001973 case SO_DOMAIN:
1974 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1975 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001976 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001977 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001978 break;
Florin Coras8eb8d502021-06-16 14:46:57 -07001979 case SO_BINDTODEVICE:
1980 rv = 0;
1981 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001982 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001983 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1984 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001985 break;
1986 }
1987 break;
1988 default:
1989 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001990 }
1991
Dave Wallace048b1d62018-01-03 22:24:41 -05001992 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001993 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001994 errno = -rv;
1995 rv = -1;
1996 }
1997 }
1998 else
1999 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002000 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2001 }
2002
Dave Wallace048b1d62018-01-03 22:24:41 -05002003 return rv;
2004}
2005
2006int
2007setsockopt (int fd, int level, int optname,
2008 const void *optval, socklen_t optlen)
2009{
Florin Coras7baeb712019-01-04 17:05:43 -08002010 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002011 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002012
Florin Coras5f33d0d2021-06-02 21:22:21 -07002013 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002014
Florin Coras7baeb712019-01-04 17:05:43 -08002015 vlsh = ldp_fd_to_vlsh (fd);
2016 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002017 {
2018 rv = -EOPNOTSUPP;
2019
2020 switch (level)
2021 {
2022 case SOL_TCP:
2023 switch (optname)
2024 {
2025 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002026 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2027 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002028 break;
2029 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002030 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2031 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002032 break;
2033 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002034 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2035 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002036 break;
2037 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002038 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2039 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002040 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002041 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002042 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002043 /* Ignore */
2044 rv = 0;
2045 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002046 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002047 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2048 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002049 break;
2050 }
2051 break;
2052 case SOL_IPV6:
2053 switch (optname)
2054 {
2055 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002056 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2057 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002058 break;
2059 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002060 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2061 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002062 break;
2063 }
2064 break;
2065 case SOL_SOCKET:
2066 switch (optname)
2067 {
2068 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002069 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2070 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002071 break;
2072 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002073 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2074 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002075 break;
wanghanlin0674f852021-02-22 10:38:36 +08002076 case SO_REUSEPORT:
2077 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2078 &optlen);
2079 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002080 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002081 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2082 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002083 break;
Florin Coras2f647902021-06-02 08:23:50 -07002084 case SO_LINGER:
2085 rv = 0;
2086 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002087 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002088 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2089 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002090 break;
2091 }
2092 break;
2093 default:
2094 break;
2095 }
2096
2097 if (rv != VPPCOM_OK)
2098 {
2099 errno = -rv;
2100 rv = -1;
2101 }
2102 }
2103 else
2104 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002105 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2106 }
2107
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 return rv;
2109}
2110
2111int
2112listen (int fd, int n)
2113{
Florin Coras7baeb712019-01-04 17:05:43 -08002114 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002116
Florin Coras5f33d0d2021-06-02 21:22:21 -07002117 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002118
Florin Coras7baeb712019-01-04 17:05:43 -08002119 vlsh = ldp_fd_to_vlsh (fd);
2120 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 {
Florin Coras7baeb712019-01-04 17:05:43 -08002122 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002123
Florin Coras7baeb712019-01-04 17:05:43 -08002124 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 if (rv != VPPCOM_OK)
2126 {
2127 errno = -rv;
2128 rv = -1;
2129 }
2130 }
2131 else
2132 {
Florin Coras7baeb712019-01-04 17:05:43 -08002133 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002134 rv = libc_listen (fd, n);
2135 }
2136
Florin Coras7baeb712019-01-04 17:05:43 -08002137 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002138 return rv;
2139}
2140
2141static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002142ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2143 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002144{
Florin Coras7baeb712019-01-04 17:05:43 -08002145 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002146 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002147
Florin Coras5f33d0d2021-06-02 21:22:21 -07002148 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002149
Florin Coras7baeb712019-01-04 17:05:43 -08002150 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2151 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002152 {
2153 vppcom_endpt_t ep;
2154 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002155 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002156 ep.ip = src_addr;
2157
Florin Coras7baeb712019-01-04 17:05:43 -08002158 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002159 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002160
Florin Coras7baeb712019-01-04 17:05:43 -08002161 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2162 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002163 {
Florin Coras7baeb712019-01-04 17:05:43 -08002164 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002165 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002166 }
2167 else
2168 {
Dave Wallace2a865272018-02-07 21:00:42 -05002169 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002170 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002171 {
Florin Coras7baeb712019-01-04 17:05:43 -08002172 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002173 errno = -rv;
2174 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002175 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002176 else
2177 {
Florin Coras7baeb712019-01-04 17:05:43 -08002178 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002179 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002180 }
2181 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002182 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002183 {
Florin Coras7baeb712019-01-04 17:05:43 -08002184 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2185 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002186
Dave Wallace048b1d62018-01-03 22:24:41 -05002187 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002188 }
2189
Florin Coras7baeb712019-01-04 17:05:43 -08002190 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002191
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002192 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002193}
2194
Dave Wallace048b1d62018-01-03 22:24:41 -05002195int
2196accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2197 int flags)
2198{
Dave Wallace2a865272018-02-07 21:00:42 -05002199 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002200}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002201
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002202int
Dave Wallace048b1d62018-01-03 22:24:41 -05002203accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002204{
Dave Wallace2a865272018-02-07 21:00:42 -05002205 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002206}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002207
Dave Wallace048b1d62018-01-03 22:24:41 -05002208int
2209shutdown (int fd, int how)
2210{
Florin Coras7baeb712019-01-04 17:05:43 -08002211 vls_handle_t vlsh;
liuyacan55c952e2021-06-13 14:54:55 +08002212 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002213
Florin Coras5f33d0d2021-06-02 21:22:21 -07002214 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002215
Florin Coras7baeb712019-01-04 17:05:43 -08002216 vlsh = ldp_fd_to_vlsh (fd);
2217 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002218 {
Florin Coras7baeb712019-01-04 17:05:43 -08002219 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
liuyacan55c952e2021-06-13 14:54:55 +08002220 rv = vls_shutdown (vlsh, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002221 }
2222 else
2223 {
Florin Coras7baeb712019-01-04 17:05:43 -08002224 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002225 rv = libc_shutdown (fd, how);
2226 }
2227
Dave Wallace048b1d62018-01-03 22:24:41 -05002228 return rv;
2229}
2230
2231int
2232epoll_create1 (int flags)
2233{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002234 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002235 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002236 int rv;
2237
Florin Coras5f33d0d2021-06-02 21:22:21 -07002238 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002239
hanlina3a48962020-07-13 11:09:15 +08002240 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002241 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002242 /* Make sure workers have been allocated */
2243 if (!ldp->workers)
2244 {
2245 ldp_alloc_workers ();
2246 ldpw = ldp_worker_get_current ();
2247 }
Florin Coras99368312018-08-02 10:45:44 -07002248 rv = libc_epoll_create1 (flags);
2249 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002250 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002251 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002252 return rv;
2253 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002254
Florin Coras7baeb712019-01-04 17:05:43 -08002255 vlsh = vls_epoll_create ();
2256 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002257 {
Florin Coras7baeb712019-01-04 17:05:43 -08002258 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002259 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002260 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002261 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002262 {
Florin Coras7baeb712019-01-04 17:05:43 -08002263 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002264 }
Florin Coras7baeb712019-01-04 17:05:43 -08002265 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002266 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002267}
2268
2269int
Dave Wallace048b1d62018-01-03 22:24:41 -05002270epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002271{
Dave Wallace048b1d62018-01-03 22:24:41 -05002272 return epoll_create1 (0);
2273}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002274
Dave Wallace048b1d62018-01-03 22:24:41 -05002275int
2276epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2277{
Florin Coras7baeb712019-01-04 17:05:43 -08002278 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002279 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002280
Florin Coras5f33d0d2021-06-02 21:22:21 -07002281 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002282
Florin Coras7baeb712019-01-04 17:05:43 -08002283 vep_vlsh = ldp_fd_to_vlsh (epfd);
2284 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002285 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002286 /* The LDP epoll_create1 always creates VCL epfd's.
2287 * The app should never have a kernel base epoll fd unless it
2288 * was acquired outside of the LD_PRELOAD process context.
2289 * In any case, if we get one, punt it to libc_epoll_ctl.
2290 */
Florin Coras7baeb712019-01-04 17:05:43 -08002291 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2292 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002293
2294 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002295 goto done;
2296 }
2297
Florin Coras7baeb712019-01-04 17:05:43 -08002298 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002299
Florin Coras7baeb712019-01-04 17:05:43 -08002300 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2301 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002302
Florin Coras7baeb712019-01-04 17:05:43 -08002303 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002304 {
Florin Coras7baeb712019-01-04 17:05:43 -08002305 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002306 " event %p", epfd, vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002307
Florin Coras7baeb712019-01-04 17:05:43 -08002308 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002309 if (rv != VPPCOM_OK)
2310 {
2311 errno = -rv;
2312 rv = -1;
2313 }
2314 }
2315 else
2316 {
2317 int libc_epfd;
2318 u32 size = sizeof (epfd);
2319
Florin Coras7baeb712019-01-04 17:05:43 -08002320 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002321 if (!libc_epfd)
2322 {
Florin Coras7baeb712019-01-04 17:05:43 -08002323 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2324 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002325
2326 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2327 if (libc_epfd < 0)
2328 {
2329 rv = libc_epfd;
2330 goto done;
2331 }
2332
Florin Coras7baeb712019-01-04 17:05:43 -08002333 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2334 &size);
Florin Coras99368312018-08-02 10:45:44 -07002335 if (rv < 0)
2336 {
2337 errno = -rv;
2338 rv = -1;
2339 goto done;
2340 }
2341 }
2342 else if (PREDICT_FALSE (libc_epfd < 0))
2343 {
2344 errno = -epfd;
2345 rv = -1;
2346 goto done;
2347 }
2348
Florin Coras7baeb712019-01-04 17:05:43 -08002349 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2350 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002351
2352 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002353 }
2354
2355done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002356 return rv;
2357}
Dave Wallace048b1d62018-01-03 22:24:41 -05002358
2359static inline int
Florin Coras99368312018-08-02 10:45:44 -07002360ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2361 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002362{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002363 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002364 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002365 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002366 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002367
Florin Coras5f33d0d2021-06-02 21:22:21 -07002368 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002369
2370 if (PREDICT_FALSE (!events || (timeout < -1)))
2371 {
2372 errno = EFAULT;
2373 return -1;
2374 }
2375
Florin Corasdfe4cf42018-11-28 22:13:45 -08002376 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002377 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2378
Florin Coras7baeb712019-01-04 17:05:43 -08002379 ep_vlsh = ldp_fd_to_vlsh (epfd);
2380 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002381 {
Florin Coras7baeb712019-01-04 17:05:43 -08002382 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002383 errno = EBADFD;
2384 return -1;
2385 }
2386
Florin Coras4dee8cd2019-01-29 21:28:16 -08002387 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2388 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002389 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002390 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002391
Florin Coras7baeb712019-01-04 17:05:43 -08002392 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002393 if (PREDICT_FALSE (libc_epfd < 0))
2394 {
2395 errno = -libc_epfd;
2396 rv = -1;
2397 goto done;
2398 }
2399
Florin Coras7baeb712019-01-04 17:05:43 -08002400 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2401 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002402 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002403 do
2404 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002405 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002406 {
Florin Coras7baeb712019-01-04 17:05:43 -08002407 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002408 if (rv > 0)
2409 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002410 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002411 goto done;
2412 }
2413 else if (rv < 0)
2414 {
2415 errno = -rv;
2416 rv = -1;
2417 goto done;
2418 }
2419 }
2420 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002421 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002422
2423 if (libc_epfd > 0)
2424 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002425 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002426 if (rv != 0)
2427 goto done;
2428 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002429 }
Florin Coras72f77822019-01-22 19:05:52 -08002430 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002431
2432done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002433 return rv;
2434}
2435
hanlin4266d4d2020-05-19 17:34:17 +08002436static inline int
2437ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2438 int maxevents, int timeout, const sigset_t * sigmask)
2439{
hanlina3a48962020-07-13 11:09:15 +08002440 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002441 int libc_epfd, rv = 0, num_ev;
2442 vls_handle_t ep_vlsh;
2443
Florin Coras5f33d0d2021-06-02 21:22:21 -07002444 ldp_init_check ();
hanlin4266d4d2020-05-19 17:34:17 +08002445
2446 if (PREDICT_FALSE (!events || (timeout < -1)))
2447 {
2448 errno = EFAULT;
2449 return -1;
2450 }
2451
Florin Corasff40d8f2020-08-11 22:05:28 -07002452 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2453 * one thread but it is now used on another */
2454 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2455 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002456
2457 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002458 if (epfd == ldpw->vcl_mq_epfd)
2459 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2460
2461 ep_vlsh = ldp_fd_to_vlsh (epfd);
2462 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2463 {
2464 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2465 errno = EBADFD;
2466 return -1;
2467 }
2468
2469 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2470 if (PREDICT_FALSE (!libc_epfd))
2471 {
2472 u32 size = sizeof (epfd);
2473
2474 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2475 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2476 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2477 if (libc_epfd < 0)
2478 {
2479 rv = libc_epfd;
2480 goto done;
2481 }
2482
2483 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2484 if (rv < 0)
2485 {
2486 errno = -rv;
2487 rv = -1;
2488 goto done;
2489 }
2490 }
2491 if (PREDICT_FALSE (libc_epfd <= 0))
2492 {
2493 errno = -libc_epfd;
2494 rv = -1;
2495 goto done;
2496 }
2497
2498 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2499 {
2500 struct epoll_event e = { 0 };
2501 e.events = EPOLLIN;
2502 e.data.fd = ldpw->vcl_mq_epfd;
2503 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2504 0)
2505 {
2506 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2507 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2508 rv = -1;
2509 goto done;
2510 }
2511 ldpw->mq_epfd_added = 1;
2512 }
2513
wanghanlin8919fec2021-03-18 20:00:41 +08002514 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2515 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002516 if (rv > 0)
2517 goto done;
hanlina3a48962020-07-13 11:09:15 +08002518 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002519 {
2520 errno = -rv;
2521 rv = -1;
2522 goto done;
2523 }
2524
2525 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2526 if (rv <= 0)
2527 goto done;
2528 for (int i = 0; i < rv; i++)
2529 {
2530 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2531 {
2532 /* We should remove mq epoll fd from events. */
2533 rv--;
2534 if (i != rv)
2535 {
2536 events[i].events = events[rv].events;
2537 events[i].data.u64 = events[rv].data.u64;
2538 }
2539 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2540 if (PREDICT_TRUE (num_ev > 0))
2541 rv += num_ev;
2542 break;
2543 }
2544 }
2545
2546done:
2547 return rv;
2548}
2549
Dave Wallace048b1d62018-01-03 22:24:41 -05002550int
2551epoll_pwait (int epfd, struct epoll_event *events,
2552 int maxevents, int timeout, const sigset_t * sigmask)
2553{
hanlin4266d4d2020-05-19 17:34:17 +08002554 if (vls_use_eventfd ())
2555 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2556 sigmask);
2557 else
2558 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002559}
2560
2561int
2562epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2563{
hanlin4266d4d2020-05-19 17:34:17 +08002564 if (vls_use_eventfd ())
2565 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2566 else
2567 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002568}
2569
2570int
2571poll (struct pollfd *fds, nfds_t nfds, int timeout)
2572{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002573 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002574 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002575 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002576 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002577 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002578
Florin Coraseb801d02020-09-16 17:44:58 -07002579 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002580
Florin Coras4dee8cd2019-01-29 21:28:16 -08002581 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2582 clib_time_init (&ldpw->clib_time);
2583
2584 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2585 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002586
Dave Wallace048b1d62018-01-03 22:24:41 -05002587 for (i = 0; i < nfds; i++)
2588 {
Florin Coras6917b942018-11-13 22:44:54 -08002589 if (fds[i].fd < 0)
2590 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002591
Florin Coras7baeb712019-01-04 17:05:43 -08002592 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2593 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002594 {
2595 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002596 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002597 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002598 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002599 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002600#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002601 if (fds[i].events & POLLRDNORM)
2602 vp->events |= POLLIN;
2603 if (fds[i].events & POLLWRNORM)
2604 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002605#endif
Florin Coras6917b942018-11-13 22:44:54 -08002606 vp->revents = fds[i].revents;
2607 }
2608 else
2609 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002610 vec_add1 (ldpw->libc_poll, fds[i]);
2611 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002612 }
2613 }
2614
Dave Wallace048b1d62018-01-03 22:24:41 -05002615 do
2616 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002617 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002618 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002619 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002620 if (rv < 0)
2621 {
2622 errno = -rv;
2623 rv = -1;
2624 goto done;
2625 }
2626 else
2627 n_revents += rv;
2628 }
2629
Florin Corasdfe4cf42018-11-28 22:13:45 -08002630 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002631 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002632 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002633 if (rv < 0)
2634 goto done;
2635 else
2636 n_revents += rv;
2637 }
2638
2639 if (n_revents)
2640 {
2641 rv = n_revents;
2642 goto done;
2643 }
2644 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002645 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002646 rv = 0;
2647
2648done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002649 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002650 {
2651 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002652 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002653#ifdef __USE_XOPEN2K
2654 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2655 (fds[vp->fds_ndx].events & POLLRDNORM))
2656 fds[vp->fds_ndx].revents |= POLLRDNORM;
2657 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2658 (fds[vp->fds_ndx].events & POLLWRNORM))
2659 fds[vp->fds_ndx].revents |= POLLWRNORM;
2660#endif
2661 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002662 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002663
Florin Corasdfe4cf42018-11-28 22:13:45 -08002664 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002665 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002666 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002667 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002668 vec_reset_length (ldpw->libc_poll_idxs);
2669 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002670
Dave Wallace048b1d62018-01-03 22:24:41 -05002671 return rv;
2672}
2673
2674#ifdef USE_GNU
2675int
2676ppoll (struct pollfd *fds, nfds_t nfds,
2677 const struct timespec *timeout, const sigset_t * sigmask)
2678{
Florin Coras5f33d0d2021-06-02 21:22:21 -07002679 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002680
2681 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2682 errno = ENOSYS;
2683
2684
2685 return -1;
2686}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002687#endif
2688
Dave Wallace2a865272018-02-07 21:00:42 -05002689void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002690
Dave Wallace2a865272018-02-07 21:00:42 -05002691void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002692
Dave Wallace048b1d62018-01-03 22:24:41 -05002693/*
2694 * This function is called when the library is loaded
2695 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002696void
Dave Wallace2a865272018-02-07 21:00:42 -05002697ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002698{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002699 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002700 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002701 {
2702 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2703 getpid ());
2704 _exit (1);
2705 }
Dave Wallace69d01192018-02-22 16:22:09 -05002706 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002707 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002708}
2709
2710/*
2711 * This function is called when the library is unloaded
2712 */
2713void
Dave Wallace2a865272018-02-07 21:00:42 -05002714ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002715{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002716 /*
2717 swrap_destructor ();
2718 if (ldp->init)
2719 ldp->init = 0;
2720 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002721
2722 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002723 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002724 */
Dave Wallace69d01192018-02-22 16:22:09 -05002725 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002726 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2727 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002728}
2729
2730
2731/*
2732 * fd.io coding-style-patch-verification: ON
2733 *
2734 * Local Variables:
2735 * eval: (c-set-style "gnu")
2736 * End:
2737 */