blob: 482af3f1f82996974f7d67f76493da7e135e12bc [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 */
Florin Coras36847942023-02-02 12:56:16 -080015
16#ifdef HAVE_GNU_SOURCE
17#define _GNU_SOURCE
18#endif
19
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070020#include <unistd.h>
21#include <stdio.h>
22#include <signal.h>
23#include <dlfcn.h>
24#include <pthread.h>
25#include <time.h>
26#include <stdarg.h>
shrinivasan ganapathy1d359632017-10-15 15:46:09 -070027#include <sys/resource.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050028#include <netinet/tcp.h>
Dou Chao243a0432022-11-29 19:41:34 +080029#include <linux/udp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070030
Dave Wallace2a865272018-02-07 21:00:42 -050031#include <vcl/ldp_socket_wrapper.h>
32#include <vcl/ldp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070033#include <sys/time.h>
34
Florin Coras7baeb712019-01-04 17:05:43 -080035#include <vcl/vcl_locked.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050036#include <vppinfra/time.h>
37#include <vppinfra/bitmap.h>
Florin Coras30e273b2018-11-27 00:04:59 -080038#include <vppinfra/lock.h>
39#include <vppinfra/pool.h>
40#include <vppinfra/hash.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070041
42#define HAVE_CONSTRUCTOR_ATTRIBUTE
43#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
44#define CONSTRUCTOR_ATTRIBUTE \
45 __attribute__ ((constructor))
46#else
47#define CONSTRUCTOR_ATTRIBUTE
48#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
49
50#define HAVE_DESTRUCTOR_ATTRIBUTE
51#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
52#define DESTRUCTOR_ATTRIBUTE \
53 __attribute__ ((destructor))
54#else
55#define DESTRUCTOR_ATTRIBUTE
56#endif
57
Florin Corasdfe4cf42018-11-28 22:13:45 -080058#define LDP_MAX_NWORKERS 32
59
Florin Coras36847942023-02-02 12:56:16 -080060#ifdef HAVE_GNU_SOURCE
61#define SOCKADDR_GET_SA(__addr) __addr.__sockaddr__;
62#else
63#define SOCKADDR_GET_SA(__addr) _addr;
64#endif
65
Florin Corasdfe4cf42018-11-28 22:13:45 -080066typedef struct ldp_worker_ctx_
Dave Wallace048b1d62018-01-03 22:24:41 -050067{
Dave Wallace048b1d62018-01-03 22:24:41 -050068 u8 *io_buffer;
69 clib_time_t clib_time;
Florin Corasdfe4cf42018-11-28 22:13:45 -080070
71 /*
72 * Select state
73 */
Dave Wallace048b1d62018-01-03 22:24:41 -050074 clib_bitmap_t *rd_bitmap;
75 clib_bitmap_t *wr_bitmap;
76 clib_bitmap_t *ex_bitmap;
Florin Coras294afe22019-01-07 17:49:17 -080077 clib_bitmap_t *si_rd_bitmap;
78 clib_bitmap_t *si_wr_bitmap;
79 clib_bitmap_t *si_ex_bitmap;
Dave Wallace048b1d62018-01-03 22:24:41 -050080 clib_bitmap_t *libc_rd_bitmap;
81 clib_bitmap_t *libc_wr_bitmap;
82 clib_bitmap_t *libc_ex_bitmap;
Florin Corasdfe4cf42018-11-28 22:13:45 -080083
84 /*
85 * Poll state
86 */
Dave Wallace048b1d62018-01-03 22:24:41 -050087 vcl_poll_t *vcl_poll;
Florin Coras6917b942018-11-13 22:44:54 -080088 struct pollfd *libc_poll;
89 u16 *libc_poll_idxs;
Florin Corasdfe4cf42018-11-28 22:13:45 -080090
91 /*
92 * Epoll state
93 */
Dave Wallace048b1d62018-01-03 22:24:41 -050094 u8 epoll_wait_vcl;
hanlin4266d4d2020-05-19 17:34:17 +080095 u8 mq_epfd_added;
Florin Coras99368312018-08-02 10:45:44 -070096 int vcl_mq_epfd;
Florin Corasdfe4cf42018-11-28 22:13:45 -080097
98} ldp_worker_ctx_t;
99
Florin Coras294afe22019-01-07 17:49:17 -0800100/* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
101 * they are the same size */
102STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
103 "ldp bitmap size mismatch");
104STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
105 "ldp bitmap size mismatch");
106
Florin Corasdfe4cf42018-11-28 22:13:45 -0800107typedef struct
108{
109 ldp_worker_ctx_t *workers;
110 int init;
111 char app_name[LDP_APP_NAME_MAX];
Florin Coras7baeb712019-01-04 17:05:43 -0800112 u32 vlsh_bit_val;
113 u32 vlsh_bit_mask;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800114 u32 debug;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800115
116 /** vcl needs next epoll_create to go to libc_epoll */
117 u8 vcl_needs_real_epoll;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800118
119 /**
120 * crypto state used only for testing
121 */
122 u8 transparent_tls;
123 u32 ckpair_index;
Dave Wallace2a865272018-02-07 21:00:42 -0500124} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800125
Dave Wallace2a865272018-02-07 21:00:42 -0500126#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700127
Florin Coras99368312018-08-02 10:45:44 -0700128#define LDBG(_lvl, _fmt, _args...) \
129 if (ldp->debug > _lvl) \
hanlin9f3f18f2019-12-30 16:25:20 +0800130 { \
131 int errno_saved = errno; \
Florin Coras585c86a2020-10-16 17:57:36 -0700132 fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \
hanlin9f3f18f2019-12-30 16:25:20 +0800133 errno = errno_saved; \
134 }
Florin Coras99368312018-08-02 10:45:44 -0700135
Dave Wallace2a865272018-02-07 21:00:42 -0500136static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800137 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
138 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500139 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800140 .transparent_tls = 0,
Florin Corasa5a9efd2021-01-05 17:03:29 -0800141 .ckpair_index = ~0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500142};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700143
Dave Wallace2a865272018-02-07 21:00:42 -0500144static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700145
Florin Corasdfe4cf42018-11-28 22:13:45 -0800146static inline ldp_worker_ctx_t *
147ldp_worker_get_current (void)
148{
149 return (ldp->workers + vppcom_worker_index ());
150}
151
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700152/*
153 * RETURN: 0 on success or -1 on error.
154 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500155static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500156ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700157{
Benoît Ganne747b3d82019-08-21 18:27:23 +0200158 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
159 "ldp-%d-%s", getpid (), app_name);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700160}
161
Dave Wallace048b1d62018-01-03 22:24:41 -0500162static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500163ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700164{
Dave Wallace2a865272018-02-07 21:00:42 -0500165 if (ldp->app_name[0] == '\0')
166 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500167
Dave Wallace2a865272018-02-07 21:00:42 -0500168 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700169}
170
Dave Wallace048b1d62018-01-03 22:24:41 -0500171static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800172ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500173{
Florin Coras7baeb712019-01-04 17:05:43 -0800174 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500175}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700176
Florin Coras7baeb712019-01-04 17:05:43 -0800177static inline vls_handle_t
178ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500179{
Florin Coras7baeb712019-01-04 17:05:43 -0800180 if (fd < ldp->vlsh_bit_val)
181 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700182
Florin Coras7baeb712019-01-04 17:05:43 -0800183 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500184}
185
Florin Coras2d9b4272019-03-11 10:14:37 -0700186static void
187ldp_alloc_workers (void)
188{
189 if (ldp->workers)
190 return;
191 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
192}
193
Florin Coras5f33d0d2021-06-02 21:22:21 -0700194static int
Dave Wallace2a865272018-02-07 21:00:42 -0500195ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700196{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800197 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700198 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700199
Florin Coras5f33d0d2021-06-02 21:22:21 -0700200 ASSERT (!ldp->init);
Florin Coras99368312018-08-02 10:45:44 -0700201
202 ldp->init = 1;
203 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800204 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700205 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700206 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800207 ldp->vcl_needs_real_epoll = 0;
208 if (rv == VPPCOM_EEXIST)
209 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800210 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
211 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700212 ldp->init = 0;
213 return rv;
214 }
215 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700216 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800217 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700218
219 char *env_var_str = getenv (LDP_ENV_DEBUG);
220 if (env_var_str)
221 {
222 u32 tmp;
223 if (sscanf (env_var_str, "%u", &tmp) != 1)
224 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
225 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
226 env_var_str);
227 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700228 {
Florin Coras99368312018-08-02 10:45:44 -0700229 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800230 LDBG (0, "configured LDP debug level (%u) from env var "
231 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700232 }
233 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500234
Florin Coras99368312018-08-02 10:45:44 -0700235 env_var_str = getenv (LDP_ENV_APP_NAME);
236 if (env_var_str)
237 {
238 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800239 LDBG (0, "configured LDP app name (%s) from the env var "
240 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700241 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500242
Florin Coras99368312018-08-02 10:45:44 -0700243 env_var_str = getenv (LDP_ENV_SID_BIT);
244 if (env_var_str)
245 {
246 u32 sb;
247 if (sscanf (env_var_str, "%u", &sb) != 1)
248 {
Florin Coras294afe22019-01-07 17:49:17 -0800249 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
250 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
251 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700252 }
253 else if (sb < LDP_SID_BIT_MIN)
254 {
Florin Coras7baeb712019-01-04 17:05:43 -0800255 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
256 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500257
Florin Coras294afe22019-01-07 17:49:17 -0800258 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
259 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
260 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
261 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700262 }
263 else if (sb > LDP_SID_BIT_MAX)
264 {
Florin Coras7baeb712019-01-04 17:05:43 -0800265 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
266 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500267
Florin Coras294afe22019-01-07 17:49:17 -0800268 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
269 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
270 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
271 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500272 }
273 else
274 {
Florin Coras7baeb712019-01-04 17:05:43 -0800275 ldp->vlsh_bit_val = (1 << sb);
276 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700277
Florin Coras05ecfcc2018-12-12 18:19:39 -0800278 LDBG (0, "configured LDP sid bit (%u) from "
279 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800280 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500281 }
Florin Coras294afe22019-01-07 17:49:17 -0800282
283 /* Make sure there are enough bits in the fd set for vcl sessions */
284 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
285 {
wanghanlin1eb8fea2021-10-14 11:10:26 +0800286 /* Only valid for select/pselect, so just WARNING and not exit */
287 LDBG (0,
288 "WARNING: LDP vlsh bit value %d > FD_SETSIZE/2 %d, "
289 "select/pselect not supported now!",
Florin Coras294afe22019-01-07 17:49:17 -0800290 ldp->vlsh_bit_val, FD_SETSIZE / 2);
Florin Coras294afe22019-01-07 17:49:17 -0800291 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500292 }
Yu Ping7b74b072019-05-08 00:40:24 +0800293 env_var_str = getenv (LDP_ENV_TLS_TRANS);
294 if (env_var_str)
295 {
296 ldp->transparent_tls = 1;
297 }
Florin Coras99368312018-08-02 10:45:44 -0700298
Florin Coras4dee8cd2019-01-29 21:28:16 -0800299 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100300 pool_foreach (ldpw, ldp->workers) {
Florin Coras4dee8cd2019-01-29 21:28:16 -0800301 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100302 }
Florin Coras4dee8cd2019-01-29 21:28:16 -0800303 /* *INDENT-ON* */
304
Florin Coras05ecfcc2018-12-12 18:19:39 -0800305 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700306
307 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500308}
309
Florin Coras5f33d0d2021-06-02 21:22:21 -0700310#define ldp_init_check() \
311 if (PREDICT_FALSE (!ldp->init)) \
312 { \
313 if ((errno = -ldp_init ())) \
314 return -1; \
315 }
316
Dave Wallace048b1d62018-01-03 22:24:41 -0500317int
318close (int fd)
319{
Florin Coras7baeb712019-01-04 17:05:43 -0800320 vls_handle_t vlsh;
321 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500322
Florin Coras5f33d0d2021-06-02 21:22:21 -0700323 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500324
Florin Coras7baeb712019-01-04 17:05:43 -0800325 vlsh = ldp_fd_to_vlsh (fd);
326 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500327 {
Florin Coras7baeb712019-01-04 17:05:43 -0800328 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500329 if (epfd > 0)
330 {
Florin Coras7baeb712019-01-04 17:05:43 -0800331 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500332
333 rv = libc_close (epfd);
334 if (rv < 0)
335 {
336 u32 size = sizeof (epfd);
337 epfd = 0;
338
Florin Coras7baeb712019-01-04 17:05:43 -0800339 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500340 }
341 }
342 else if (PREDICT_FALSE (epfd < 0))
343 {
344 errno = -epfd;
345 rv = -1;
346 goto done;
347 }
348
Florin Coras7baeb712019-01-04 17:05:43 -0800349 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500350
Florin Coras7baeb712019-01-04 17:05:43 -0800351 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500352 if (rv != VPPCOM_OK)
353 {
354 errno = -rv;
355 rv = -1;
356 }
357 }
358 else
359 {
Florin Coras7baeb712019-01-04 17:05:43 -0800360 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500361 rv = libc_close (fd);
362 }
363
364done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500365 return rv;
366}
367
368ssize_t
369read (int fd, void *buf, size_t nbytes)
370{
Florin Coras7baeb712019-01-04 17:05:43 -0800371 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500372 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500373
Florin Coras5f33d0d2021-06-02 21:22:21 -0700374 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500375
Florin Coras7baeb712019-01-04 17:05:43 -0800376 vlsh = ldp_fd_to_vlsh (fd);
377 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500378 {
Florin Coras7baeb712019-01-04 17:05:43 -0800379 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500380 if (size < 0)
381 {
382 errno = -size;
383 size = -1;
384 }
385 }
386 else
387 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500388 size = libc_read (fd, buf, nbytes);
389 }
390
Dave Wallace048b1d62018-01-03 22:24:41 -0500391 return size;
392}
393
394ssize_t
395readv (int fd, const struct iovec * iov, int iovcnt)
396{
Dave Wallace8aaba562018-01-18 17:21:19 -0500397 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800398 vls_handle_t vlsh;
399 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500400
Florin Coras5f33d0d2021-06-02 21:22:21 -0700401 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500402
Florin Coras7baeb712019-01-04 17:05:43 -0800403 vlsh = ldp_fd_to_vlsh (fd);
404 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500405 {
Florin Coras067f9542020-02-14 05:33:46 +0000406 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500407 {
Florin Coras067f9542020-02-14 05:33:46 +0000408 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
409 if (rv <= 0)
410 break;
411 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500412 {
Florin Coras067f9542020-02-14 05:33:46 +0000413 total += rv;
414 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500415 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700416 }
417 }
Florin Coras067f9542020-02-14 05:33:46 +0000418 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700419 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500420 errno = -rv;
421 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700422 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500423 else
424 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700425 }
426 else
427 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500428 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700429 }
430
Dave Wallace048b1d62018-01-03 22:24:41 -0500431 return size;
432}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700433
Dave Wallace048b1d62018-01-03 22:24:41 -0500434ssize_t
435write (int fd, const void *buf, size_t nbytes)
436{
Florin Coras7baeb712019-01-04 17:05:43 -0800437 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500438 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500439
Florin Coras5f33d0d2021-06-02 21:22:21 -0700440 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500441
Florin Coras7baeb712019-01-04 17:05:43 -0800442 vlsh = ldp_fd_to_vlsh (fd);
443 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500444 {
Florin Coras7baeb712019-01-04 17:05:43 -0800445 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500446 if (size < 0)
447 {
448 errno = -size;
449 size = -1;
450 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700451 }
452 else
453 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500454 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700455 }
456
Dave Wallace048b1d62018-01-03 22:24:41 -0500457 return size;
458}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700459
Dave Wallace048b1d62018-01-03 22:24:41 -0500460ssize_t
461writev (int fd, const struct iovec * iov, int iovcnt)
462{
Dave Wallace048b1d62018-01-03 22:24:41 -0500463 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800464 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500465 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500466
Florin Coras5f33d0d2021-06-02 21:22:21 -0700467 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500468
Florin Coras7baeb712019-01-04 17:05:43 -0800469 vlsh = ldp_fd_to_vlsh (fd);
470 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500471 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000472 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500473 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000474 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
475 if (rv < 0)
476 break;
477 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500478 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000479 total += rv;
480 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500481 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500482 }
483 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500484
Florin Coraseda1b8c2020-03-23 16:00:35 +0000485 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500486 {
487 errno = -rv;
488 size = -1;
489 }
490 else
491 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700492 }
493 else
494 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500495 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700496 }
497
Dave Wallace048b1d62018-01-03 22:24:41 -0500498 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700499}
500
Florin Coras0ab36f52020-05-26 19:45:45 +0000501static int
502fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700503{
Florin Coras7baeb712019-01-04 17:05:43 -0800504 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700505 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800506
507 vlsh = ldp_fd_to_vlsh (fd);
508 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
509 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700510 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500511 int flags = va_arg (ap, int);
512 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700513
Dave Wallace048b1d62018-01-03 22:24:41 -0500514 size = sizeof (flags);
515 rv = -EOPNOTSUPP;
516 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700517 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500518 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800519 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500520 break;
521
522 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800523 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500524 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800525 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700526 break;
Florin Coras173bae32018-11-16 18:56:28 -0800527 case F_SETFD:
528 /* TODO handle this */
529 LDBG (0, "F_SETFD ignored flags %u", flags);
530 rv = 0;
531 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700532 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500533 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700534 break;
535 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500536 if (rv < 0)
537 {
538 errno = -rv;
539 rv = -1;
540 }
541 }
542 else
543 {
Carl Smithe16707b2019-11-13 14:37:39 +1300544#ifdef HAVE_FCNTL64
545 rv = libc_vfcntl64 (fd, cmd, ap);
546#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500547 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300548#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700549 }
550
Florin Coras0ab36f52020-05-26 19:45:45 +0000551 return rv;
552}
553
554int
555fcntl (int fd, int cmd, ...)
556{
557 va_list ap;
558 int rv;
559
Florin Coras5f33d0d2021-06-02 21:22:21 -0700560 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000561
562 va_start (ap, cmd);
563 rv = fcntl_internal (fd, cmd, ap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500564 va_end (ap);
565
Dave Wallace048b1d62018-01-03 22:24:41 -0500566 return rv;
567}
568
569int
Florin Corasd7586d52020-04-29 02:19:51 +0000570fcntl64 (int fd, int cmd, ...)
571{
572 va_list ap;
573 int rv;
574
Florin Coras5f33d0d2021-06-02 21:22:21 -0700575 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000576
Florin Corasd7586d52020-04-29 02:19:51 +0000577 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000578 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000579 va_end (ap);
580 return rv;
581}
582
583int
Dave Wallace048b1d62018-01-03 22:24:41 -0500584ioctl (int fd, unsigned long int cmd, ...)
585{
Florin Coras7baeb712019-01-04 17:05:43 -0800586 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500587 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800588 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500589
Florin Coras5f33d0d2021-06-02 21:22:21 -0700590 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500591
592 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500593
Florin Coras7baeb712019-01-04 17:05:43 -0800594 vlsh = ldp_fd_to_vlsh (fd);
595 if (vlsh != VLS_INVALID_HANDLE)
596 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500597 switch (cmd)
598 {
599 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800600 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500601 break;
602
603 case FIONBIO:
604 {
605 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
606 u32 size = sizeof (flags);
607
608 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
609 * non-blocking, the flags should be read here and merged
610 * with O_NONBLOCK.
611 */
Florin Coras7baeb712019-01-04 17:05:43 -0800612 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500613 }
614 break;
615
616 default:
617 rv = -EOPNOTSUPP;
618 break;
619 }
620 if (rv < 0)
621 {
622 errno = -rv;
623 rv = -1;
624 }
625 }
626 else
627 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500628 rv = libc_vioctl (fd, cmd, ap);
629 }
630
Dave Wallace048b1d62018-01-03 22:24:41 -0500631 va_end (ap);
632 return rv;
633}
634
Florin Coras294afe22019-01-07 17:49:17 -0800635always_inline void
636ldp_select_init_maps (fd_set * __restrict original,
637 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
638 clib_bitmap_t ** vclb, int nfds, u32 minbits,
639 u32 n_bytes, uword * si_bits, uword * libc_bits)
640{
641 uword si_bits_set, libc_bits_set;
642 vls_handle_t vlsh;
643 int fd;
644
645 clib_bitmap_validate (*vclb, minbits);
646 clib_bitmap_validate (*libcb, minbits);
647 clib_bitmap_validate (*resultb, minbits);
648 clib_memcpy_fast (*resultb, original, n_bytes);
649 memset (original, 0, n_bytes);
650
651 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100652 clib_bitmap_foreach (fd, *resultb) {
Florin Coras294afe22019-01-07 17:49:17 -0800653 if (fd > nfds)
654 break;
655 vlsh = ldp_fd_to_vlsh (fd);
656 if (vlsh == VLS_INVALID_HANDLE)
657 clib_bitmap_set_no_check (*libcb, fd, 1);
658 else
Florin Corascbce80a2020-04-20 01:32:38 +0000659 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100660 }
Florin Coras294afe22019-01-07 17:49:17 -0800661 /* *INDENT-ON* */
662
663 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
664 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000665 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800666
667 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
668 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
669}
670
671always_inline int
672ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
673{
674 vls_handle_t vlsh;
675 uword si;
676 int fd;
677
678 if (!libcb)
679 return 0;
680
681 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100682 clib_bitmap_foreach (si, vclb) {
Florin Coras294afe22019-01-07 17:49:17 -0800683 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000684 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800685 fd = ldp_vlsh_to_fd (vlsh);
686 if (PREDICT_FALSE (fd < 0))
687 {
688 errno = EBADFD;
689 return -1;
690 }
691 FD_SET (fd, libcb);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100692 }
Florin Coras294afe22019-01-07 17:49:17 -0800693 /* *INDENT-ON* */
694
695 return 0;
696}
697
698always_inline void
699ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
700{
701 uword fd;
702
Florin Coras78b5fa62019-02-21 20:04:15 -0800703 if (!libcb)
704 return;
705
Florin Coras294afe22019-01-07 17:49:17 -0800706 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100707 clib_bitmap_foreach (fd, result)
Florin Coras294afe22019-01-07 17:49:17 -0800708 FD_SET ((int)fd, libcb);
Florin Coras294afe22019-01-07 17:49:17 -0800709 /* *INDENT-ON* */
710}
711
Dave Wallace048b1d62018-01-03 22:24:41 -0500712int
Dave Wallace2a865272018-02-07 21:00:42 -0500713ldp_pselect (int nfds, fd_set * __restrict readfds,
714 fd_set * __restrict writefds,
715 fd_set * __restrict exceptfds,
716 const struct timespec *__restrict timeout,
717 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500718{
Florin Coras294afe22019-01-07 17:49:17 -0800719 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800720 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800721 struct timespec libc_tspec = { 0 };
722 f64 time_out, vcl_timeout = 0;
723 uword si_bits, libc_bits;
724 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500725
726 if (nfds < 0)
727 {
728 errno = EINVAL;
729 return -1;
730 }
731
Florin Coras4dee8cd2019-01-29 21:28:16 -0800732 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
733 clib_time_init (&ldpw->clib_time);
734
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500735 if (timeout)
736 {
737 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800738 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500739
liuyacanf71796e2021-08-02 10:01:39 +0800740 time_out += clib_time_now (&ldpw->clib_time);
741
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500742 /* select as fine grained sleep */
743 if (!nfds)
744 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800745 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500746 ;
747 return 0;
748 }
749 }
750 else if (!nfds)
751 {
752 errno = EINVAL;
753 return -1;
754 }
755 else
756 time_out = -1;
757
Florin Coras7baeb712019-01-04 17:05:43 -0800758 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500759 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500760 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
761 timeout, sigmask);
762 goto done;
763 }
764
Florin Coras294afe22019-01-07 17:49:17 -0800765 si_bits = libc_bits = 0;
766 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800767
Dave Wallace048b1d62018-01-03 22:24:41 -0500768 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800769 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
770 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
771 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500772 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800773 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
774 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
775 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500776 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800777 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
778 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
779 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500780
Florin Coras294afe22019-01-07 17:49:17 -0800781 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 {
783 errno = EINVAL;
784 rv = -1;
785 goto done;
786 }
787
Florin Coras78b5fa62019-02-21 20:04:15 -0800788 if (!si_bits)
789 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800790
Dave Wallace048b1d62018-01-03 22:24:41 -0500791 do
792 {
Florin Coras294afe22019-01-07 17:49:17 -0800793 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500794 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500795 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800796 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000797 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500798 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500799 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800800 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000801 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500802 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500803 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800804 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000805 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500806 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800807
Florin Coras0ef8ef22019-01-18 08:37:13 -0800808 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
809 writefds ? ldpw->wr_bitmap : NULL,
810 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800811 if (rv < 0)
812 {
813 errno = -rv;
814 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000815 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800816 }
817 else if (rv > 0)
818 {
819 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
820 {
821 rv = -1;
822 goto done;
823 }
824
825 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
826 {
827 rv = -1;
828 goto done;
829 }
830
831 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
832 {
833 rv = -1;
834 goto done;
835 }
836 bits_set = rv;
837 }
838 }
839 if (libc_bits)
840 {
841 if (readfds)
842 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
843 vec_len (ldpw->libc_rd_bitmap) *
844 sizeof (clib_bitmap_t));
845 if (writefds)
846 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
847 vec_len (ldpw->libc_wr_bitmap) *
848 sizeof (clib_bitmap_t));
849 if (exceptfds)
850 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
851 vec_len (ldpw->libc_ex_bitmap) *
852 sizeof (clib_bitmap_t));
853
Dave Wallace048b1d62018-01-03 22:24:41 -0500854 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800855 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
856 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
857 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
858 &libc_tspec, sigmask);
859 if (rv > 0)
860 {
861 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
862 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
863 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
864 bits_set += rv;
865 }
866 }
867
868 if (bits_set)
869 {
870 rv = bits_set;
871 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500872 }
873 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800874 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500875 rv = 0;
876
877done:
878 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800879 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800880 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800881 clib_bitmap_zero (ldpw->libc_rd_bitmap);
882 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800883 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800884 clib_bitmap_zero (ldpw->libc_wr_bitmap);
885 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800886 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800887 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500888
Dave Wallace048b1d62018-01-03 22:24:41 -0500889 return rv;
890}
891
892int
893select (int nfds, fd_set * __restrict readfds,
894 fd_set * __restrict writefds,
895 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
896{
897 struct timespec tspec;
898
899 if (timeout)
900 {
901 tspec.tv_sec = timeout->tv_sec;
902 tspec.tv_nsec = timeout->tv_usec * 1000;
903 }
Dave Wallace2a865272018-02-07 21:00:42 -0500904 return ldp_pselect (nfds, readfds, writefds, exceptfds,
905 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500906}
907
908#ifdef __USE_XOPEN2K
909int
910pselect (int nfds, fd_set * __restrict readfds,
911 fd_set * __restrict writefds,
912 fd_set * __restrict exceptfds,
913 const struct timespec *__restrict timeout,
914 const __sigset_t * __restrict sigmask)
915{
Dave Wallace2a865272018-02-07 21:00:42 -0500916 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500917}
918#endif
919
Yu Ping7b74b072019-05-08 00:40:24 +0800920/* If transparent TLS mode is turned on, then ldp will load key and cert.
921 */
922static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800923load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800924{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800925 char *cert_str = getenv (LDP_ENV_TLS_CERT);
926 char *key_str = getenv (LDP_ENV_TLS_KEY);
927 char cert_buf[4096], key_buf[4096];
928 int cert_size, key_size;
929 vppcom_cert_key_pair_t crypto;
930 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800931 FILE *fp;
932
Florin Corasa5a9efd2021-01-05 17:03:29 -0800933 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800934 {
935 LDBG (0, "ERROR: failed to read LDP environment %s\n",
936 LDP_ENV_TLS_CERT);
937 return -1;
938 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800939
940 fp = fopen (cert_str, "r");
941 if (fp == NULL)
942 {
943 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
944 return -1;
945 }
946 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
947 fclose (fp);
948
949 fp = fopen (key_str, "r");
950 if (fp == NULL)
951 {
952 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
953 return -1;
954 }
955 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
956 fclose (fp);
957
958 crypto.cert = cert_buf;
959 crypto.key = key_buf;
960 crypto.cert_len = cert_size;
961 crypto.key_len = key_size;
962 ckp_index = vppcom_add_cert_key_pair (&crypto);
963 if (ckp_index < 0)
964 {
965 LDBG (0, "ERROR: failed to add cert key pair\n");
966 return -1;
967 }
968
969 ldp->ckpair_index = ckp_index;
970
Yu Ping7b74b072019-05-08 00:40:24 +0800971 return 0;
972}
973
974static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800975assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800976{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800977 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800978
Florin Corasa5a9efd2021-01-05 17:03:29 -0800979 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
980 return -1;
981
982 ckp_len = sizeof (ldp->ckpair_index);
liuyacan276675e2021-08-30 13:24:53 +0800983 return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800984}
985
Dave Wallace048b1d62018-01-03 22:24:41 -0500986int
987socket (int domain, int type, int protocol)
988{
Florin Coras7baeb712019-01-04 17:05:43 -0800989 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500990 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800991 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500992
Florin Coras5f33d0d2021-06-02 21:22:21 -0700993 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500994
995 if (((domain == AF_INET) || (domain == AF_INET6)) &&
996 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
997 {
Yu Ping7b74b072019-05-08 00:40:24 +0800998 u8 proto;
999 if (ldp->transparent_tls)
1000 {
1001 proto = VPPCOM_PROTO_TLS;
1002 }
1003 else
1004 proto = ((sock_type == SOCK_DGRAM) ?
1005 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -05001006
Florin Coras7baeb712019-01-04 17:05:43 -08001007 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
1008 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -05001009
Florin Coras7baeb712019-01-04 17:05:43 -08001010 vlsh = vls_create (proto, is_nonblocking);
1011 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001012 {
Florin Coras7baeb712019-01-04 17:05:43 -08001013 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001014 rv = -1;
1015 }
1016 else
1017 {
Yu Ping7b74b072019-05-08 00:40:24 +08001018 if (ldp->transparent_tls)
1019 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001020 if (assign_cert_key_pair (vlsh) < 0)
1021 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001022 }
Florin Coras7baeb712019-01-04 17:05:43 -08001023 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001024 }
1025 }
1026 else
1027 {
Florin Coras7baeb712019-01-04 17:05:43 -08001028 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001029 rv = libc_socket (domain, type, protocol);
1030 }
1031
Dave Wallace048b1d62018-01-03 22:24:41 -05001032 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001033}
1034
1035/*
1036 * Create two new sockets, of type TYPE in domain DOMAIN and using
1037 * protocol PROTOCOL, which are connected to each other, and put file
1038 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1039 * one will be chosen automatically.
1040 * Returns 0 on success, -1 for errors.
1041 * */
1042int
Dave Wallace048b1d62018-01-03 22:24:41 -05001043socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001044{
Florin Coras7baeb712019-01-04 17:05:43 -08001045 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001046
Florin Coras5f33d0d2021-06-02 21:22:21 -07001047 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001048
1049 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1050 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001051 {
Florin Coras7baeb712019-01-04 17:05:43 -08001052 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001053 errno = ENOSYS;
1054 rv = -1;
1055 }
1056 else
1057 {
Florin Coras7baeb712019-01-04 17:05:43 -08001058 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001059 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001060 }
1061
Dave Wallace048b1d62018-01-03 22:24:41 -05001062 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001063}
1064
1065int
Florin Coras36847942023-02-02 12:56:16 -08001066bind (int fd, __CONST_SOCKADDR_ARG _addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001067{
Florin Coras36847942023-02-02 12:56:16 -08001068 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001069 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001070 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001071
Florin Coras5f33d0d2021-06-02 21:22:21 -07001072 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001073
Florin Coras7baeb712019-01-04 17:05:43 -08001074 vlsh = ldp_fd_to_vlsh (fd);
1075 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001076 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001077 vppcom_endpt_t ep;
1078
Dave Wallace048b1d62018-01-03 22:24:41 -05001079 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001080 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001081 case AF_INET:
1082 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001083 {
Florin Coras7baeb712019-01-04 17:05:43 -08001084 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1085 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001086 errno = EINVAL;
1087 rv = -1;
1088 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001089 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001090 ep.is_ip4 = VPPCOM_IS_IP4;
1091 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1092 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1093 break;
1094
1095 case AF_INET6:
1096 if (len != sizeof (struct sockaddr_in6))
1097 {
Florin Coras7baeb712019-01-04 17:05:43 -08001098 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1099 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001100 errno = EINVAL;
1101 rv = -1;
1102 goto done;
1103 }
1104 ep.is_ip4 = VPPCOM_IS_IP6;
1105 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1106 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001107 break;
1108
1109 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001110 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1111 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001112 errno = EAFNOSUPPORT;
1113 rv = -1;
1114 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001115 }
Florin Coras7baeb712019-01-04 17:05:43 -08001116 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1117 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001118
Florin Coras7baeb712019-01-04 17:05:43 -08001119 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001120 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001121 {
1122 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001123 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001124 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001125 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001126 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001127 {
Florin Coras7baeb712019-01-04 17:05:43 -08001128 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001129 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001130 }
1131
Dave Wallace048b1d62018-01-03 22:24:41 -05001132done:
Florin Coras7baeb712019-01-04 17:05:43 -08001133 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001134
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001135 return rv;
1136}
1137
1138static inline int
Florin Coras36847942023-02-02 12:56:16 -08001139ldp_copy_ep_to_sockaddr (struct sockaddr *addr, socklen_t *__restrict len,
1140 vppcom_endpt_t *ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001141{
Florin Coras36847942023-02-02 12:56:16 -08001142 int rv = 0, sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001143
Florin Coras5f33d0d2021-06-02 21:22:21 -07001144 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001145
1146 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001147 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001148 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1149 switch (addr->sa_family)
1150 {
1151 case AF_INET:
1152 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1153 if (*len > sizeof (struct sockaddr_in))
1154 *len = sizeof (struct sockaddr_in);
1155 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1156 copy_len = *len - sa_len;
1157 if (copy_len > 0)
1158 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1159 copy_len);
1160 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001161
Dave Wallace048b1d62018-01-03 22:24:41 -05001162 case AF_INET6:
1163 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1164 if (*len > sizeof (struct sockaddr_in6))
1165 *len = sizeof (struct sockaddr_in6);
1166 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1167 copy_len = *len - sa_len;
1168 if (copy_len > 0)
1169 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1170 __in6_u.__u6_addr8, ep->ip, copy_len);
1171 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001172
Dave Wallace048b1d62018-01-03 22:24:41 -05001173 default:
1174 /* Not possible */
1175 rv = -EAFNOSUPPORT;
1176 break;
1177 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001178 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001179 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001180}
1181
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001182int
Florin Coras36847942023-02-02 12:56:16 -08001183getsockname (int fd, __SOCKADDR_ARG _addr, socklen_t *__restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001184{
Florin Coras36847942023-02-02 12:56:16 -08001185 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001186 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001187 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001188
Florin Coras5f33d0d2021-06-02 21:22:21 -07001189 ldp_init_check ();
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001190
Florin Coras7baeb712019-01-04 17:05:43 -08001191 vlsh = ldp_fd_to_vlsh (fd);
1192 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001193 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001194 vppcom_endpt_t ep;
1195 u8 addr_buf[sizeof (struct in6_addr)];
1196 u32 size = sizeof (ep);
1197
1198 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001199
Florin Coras7baeb712019-01-04 17:05:43 -08001200 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001201 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001202 {
1203 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001204 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001205 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001206 else
1207 {
Dave Wallace2a865272018-02-07 21:00:42 -05001208 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001209 if (rv != VPPCOM_OK)
1210 {
1211 errno = -rv;
1212 rv = -1;
1213 }
1214 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001215 }
1216 else
1217 {
Florin Coras36847942023-02-02 12:56:16 -08001218 rv = libc_getsockname (fd, _addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001219 }
1220
Dave Wallace048b1d62018-01-03 22:24:41 -05001221 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001222}
1223
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001224int
Florin Coras36847942023-02-02 12:56:16 -08001225connect (int fd, __CONST_SOCKADDR_ARG _addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001226{
Florin Coras36847942023-02-02 12:56:16 -08001227 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001228 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001229 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001230
Florin Coras5f33d0d2021-06-02 21:22:21 -07001231 ldp_init_check ();
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001232
Dave Wallace048b1d62018-01-03 22:24:41 -05001233 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001234 {
Florin Coras7baeb712019-01-04 17:05:43 -08001235 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001236 errno = EINVAL;
1237 rv = -1;
1238 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001239 }
1240
Florin Coras7baeb712019-01-04 17:05:43 -08001241 vlsh = ldp_fd_to_vlsh (fd);
1242 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001243 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001244 vppcom_endpt_t ep;
1245
Dave Wallace048b1d62018-01-03 22:24:41 -05001246 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001247 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001248 case AF_INET:
1249 if (len != sizeof (struct sockaddr_in))
1250 {
Florin Coras7baeb712019-01-04 17:05:43 -08001251 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1252 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001253 errno = EINVAL;
1254 rv = -1;
1255 goto done;
1256 }
1257 ep.is_ip4 = VPPCOM_IS_IP4;
1258 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1259 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1260 break;
1261
1262 case AF_INET6:
1263 if (len != sizeof (struct sockaddr_in6))
1264 {
Florin Coras7baeb712019-01-04 17:05:43 -08001265 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1266 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001267 errno = EINVAL;
1268 rv = -1;
1269 goto done;
1270 }
1271 ep.is_ip4 = VPPCOM_IS_IP6;
1272 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1273 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1274 break;
1275
1276 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001277 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1278 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001279 errno = EAFNOSUPPORT;
1280 rv = -1;
1281 goto done;
1282 }
Florin Coras7baeb712019-01-04 17:05:43 -08001283 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1284 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001285
Florin Coras7baeb712019-01-04 17:05:43 -08001286 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001287 if (rv != VPPCOM_OK)
1288 {
1289 errno = -rv;
1290 rv = -1;
1291 }
1292 }
1293 else
1294 {
Florin Coras7baeb712019-01-04 17:05:43 -08001295 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1296 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001297
1298 rv = libc_connect (fd, addr, len);
1299 }
1300
1301done:
Florin Coras7baeb712019-01-04 17:05:43 -08001302 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001303 return rv;
1304}
1305
1306int
Florin Coras36847942023-02-02 12:56:16 -08001307getpeername (int fd, __SOCKADDR_ARG _addr, socklen_t *__restrict len)
Dave Wallace048b1d62018-01-03 22:24:41 -05001308{
Florin Coras36847942023-02-02 12:56:16 -08001309 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001310 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001311 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001312
Florin Coras5f33d0d2021-06-02 21:22:21 -07001313 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001314
Florin Coras7baeb712019-01-04 17:05:43 -08001315 vlsh = ldp_fd_to_vlsh (fd);
1316 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001317 {
1318 vppcom_endpt_t ep;
1319 u8 addr_buf[sizeof (struct in6_addr)];
1320 u32 size = sizeof (ep);
1321
1322 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001323 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001324 if (rv != VPPCOM_OK)
1325 {
1326 errno = -rv;
1327 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001328 }
1329 else
1330 {
Dave Wallace2a865272018-02-07 21:00:42 -05001331 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001332 if (rv != VPPCOM_OK)
1333 {
1334 errno = -rv;
1335 rv = -1;
1336 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001337 }
1338 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001339 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001340 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001341 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001342 }
1343
Dave Wallace048b1d62018-01-03 22:24:41 -05001344 return rv;
1345}
1346
1347ssize_t
1348send (int fd, const void *buf, size_t n, int flags)
1349{
Florin Coras7baeb712019-01-04 17:05:43 -08001350 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001351 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001352
Florin Coras5f33d0d2021-06-02 21:22:21 -07001353 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001354
Florin Coras7baeb712019-01-04 17:05:43 -08001355 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001356 {
Florin Coras7baeb712019-01-04 17:05:43 -08001357 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001358 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001359 {
1360 errno = -size;
1361 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001362 }
1363 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001364 else
1365 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001366 size = libc_send (fd, buf, n, flags);
1367 }
1368
Dave Wallace048b1d62018-01-03 22:24:41 -05001369 return size;
1370}
1371
1372ssize_t
1373sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1374{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001375 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001376 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001377 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001378
Florin Coras5f33d0d2021-06-02 21:22:21 -07001379 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001380
Florin Coras7baeb712019-01-04 17:05:43 -08001381 vlsh = ldp_fd_to_vlsh (out_fd);
1382 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001383 {
1384 int rv;
1385 ssize_t results = 0;
1386 size_t n_bytes_left = len;
1387 size_t bytes_to_read;
1388 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001389 u8 eagain = 0;
1390 u32 flags, flags_len = sizeof (flags);
1391
Florin Coras7baeb712019-01-04 17:05:43 -08001392 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001393 if (PREDICT_FALSE (rv != VPPCOM_OK))
1394 {
Florin Coras7baeb712019-01-04 17:05:43 -08001395 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1396 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001397
Florin Corasdfe4cf42018-11-28 22:13:45 -08001398 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001399 errno = -rv;
1400 size = -1;
1401 goto done;
1402 }
1403
1404 if (offset)
1405 {
1406 off_t off = lseek (in_fd, *offset, SEEK_SET);
1407 if (PREDICT_FALSE (off == -1))
1408 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 size = -1;
1410 goto done;
1411 }
1412
1413 ASSERT (off == *offset);
1414 }
1415
1416 do
1417 {
Florin Coras7baeb712019-01-04 17:05:43 -08001418 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001419 if (size < 0)
1420 {
Florin Coraseb801d02020-09-16 17:44:58 -07001421 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001422 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001423 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001424 errno = -size;
1425 size = -1;
1426 goto done;
1427 }
1428
1429 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001430 if (bytes_to_read == 0)
1431 {
1432 if (flags & O_NONBLOCK)
1433 {
1434 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001435 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001436 goto update_offset;
1437 }
1438 else
1439 continue;
1440 }
1441 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001442 vec_validate (ldpw->io_buffer, bytes_to_read);
1443 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001444 if (nbytes < 0)
1445 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001446 if (results == 0)
1447 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001448 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001449 size = -1;
1450 goto done;
1451 }
1452 goto update_offset;
1453 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001454
Florin Coras7baeb712019-01-04 17:05:43 -08001455 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001456 if (size < 0)
1457 {
1458 if (size == VPPCOM_EAGAIN)
1459 {
1460 if (flags & O_NONBLOCK)
1461 {
1462 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001463 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001464 goto update_offset;
1465 }
1466 else
1467 continue;
1468 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001469 if (results == 0)
1470 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001471 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001472 errno = -size;
1473 size = -1;
1474 goto done;
1475 }
1476 goto update_offset;
1477 }
1478
1479 results += nbytes;
1480 ASSERT (n_bytes_left >= nbytes);
1481 n_bytes_left = n_bytes_left - nbytes;
1482 }
1483 while (n_bytes_left > 0);
1484
1485 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001486 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001487 if (offset)
1488 {
1489 off_t off = lseek (in_fd, *offset, SEEK_SET);
1490 if (PREDICT_FALSE (off == -1))
1491 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001492 size = -1;
1493 goto done;
1494 }
1495
1496 ASSERT (off == *offset);
1497 *offset += results + 1;
1498 }
1499 if (eagain)
1500 {
1501 errno = EAGAIN;
1502 size = -1;
1503 }
1504 else
1505 size = results;
1506 }
1507 else
1508 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001509 size = libc_sendfile (out_fd, in_fd, offset, len);
1510 }
1511
1512done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001513 return size;
1514}
1515
1516ssize_t
1517sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1518{
1519 return sendfile (out_fd, in_fd, offset, len);
1520}
1521
1522ssize_t
1523recv (int fd, void *buf, size_t n, int flags)
1524{
Florin Coras7baeb712019-01-04 17:05:43 -08001525 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001526 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001527
Florin Coras5f33d0d2021-06-02 21:22:21 -07001528 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001529
Florin Coras7baeb712019-01-04 17:05:43 -08001530 vlsh = ldp_fd_to_vlsh (fd);
1531 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001532 {
Florin Coras7baeb712019-01-04 17:05:43 -08001533 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001534 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001535 {
1536 errno = -size;
1537 size = -1;
1538 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001539 }
1540 else
1541 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001542 size = libc_recv (fd, buf, n, flags);
1543 }
1544
Dave Wallace048b1d62018-01-03 22:24:41 -05001545 return size;
1546}
1547
Sivaprasad Tummalafdcbd382021-07-31 21:38:19 +05301548ssize_t
1549__recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1550{
1551 if (n > buflen)
1552 return -1;
1553
1554 return recv (fd, buf, n, flags);
1555}
1556
Dou Chao243a0432022-11-29 19:41:34 +08001557static inline int
1558ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n,
1559 vppcom_endpt_tlv_t *ep_tlv, int flags,
Florin Coras36847942023-02-02 12:56:16 -08001560 __CONST_SOCKADDR_ARG _addr, socklen_t addr_len)
Florin Corasce17f462020-05-22 20:36:29 +00001561{
Florin Coras36847942023-02-02 12:56:16 -08001562 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Corasce17f462020-05-22 20:36:29 +00001563 vppcom_endpt_t *ep = 0;
1564 vppcom_endpt_t _ep;
1565
Dou Chao243a0432022-11-29 19:41:34 +08001566 if (ep_tlv)
1567 {
1568 _ep.app_data = *ep_tlv;
1569 }
1570
Florin Corasce17f462020-05-22 20:36:29 +00001571 if (addr)
1572 {
1573 ep = &_ep;
1574 switch (addr->sa_family)
1575 {
1576 case AF_INET:
1577 ep->is_ip4 = VPPCOM_IS_IP4;
1578 ep->ip =
1579 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1580 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1581 break;
1582
1583 case AF_INET6:
1584 ep->is_ip4 = VPPCOM_IS_IP6;
1585 ep->ip =
1586 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1587 ep->port =
1588 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1589 break;
1590
1591 default:
1592 return EAFNOSUPPORT;
1593 }
1594 }
1595
1596 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1597}
1598
1599static int
Florin Coras36847942023-02-02 12:56:16 -08001600ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n, int flags,
1601 __SOCKADDR_ARG _addr, socklen_t *__restrict addr_len)
Florin Corasce17f462020-05-22 20:36:29 +00001602{
1603 u8 src_addr[sizeof (struct sockaddr_in6)];
Florin Coras36847942023-02-02 12:56:16 -08001604 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Corasce17f462020-05-22 20:36:29 +00001605 vppcom_endpt_t ep;
1606 ssize_t size;
1607 int rv;
1608
1609 if (addr)
1610 {
1611 ep.ip = src_addr;
1612 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1613
1614 if (size > 0)
1615 {
1616 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1617 if (rv < 0)
1618 size = rv;
1619 }
1620 }
1621 else
1622 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1623
1624 return size;
1625}
1626
Dave Wallace048b1d62018-01-03 22:24:41 -05001627ssize_t
1628sendto (int fd, const void *buf, size_t n, int flags,
Florin Coras36847942023-02-02 12:56:16 -08001629 __CONST_SOCKADDR_ARG _addr, socklen_t addr_len)
Dave Wallace048b1d62018-01-03 22:24:41 -05001630{
Florin Coras36847942023-02-02 12:56:16 -08001631 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001632 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001633 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001634
Florin Coras5f33d0d2021-06-02 21:22:21 -07001635 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001636
Florin Coras7baeb712019-01-04 17:05:43 -08001637 vlsh = ldp_fd_to_vlsh (fd);
wanghanlin97c6e0d2021-07-06 15:01:48 +08001638 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001639 {
Dou Chao243a0432022-11-29 19:41:34 +08001640 size = ldp_vls_sendo (vlsh, buf, n, NULL, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001641 if (size < 0)
1642 {
1643 errno = -size;
1644 size = -1;
1645 }
1646 }
1647 else
1648 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001649 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1650 }
1651
Dave Wallace048b1d62018-01-03 22:24:41 -05001652 return size;
1653}
1654
1655ssize_t
1656recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1657 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1658{
Florin Corasce17f462020-05-22 20:36:29 +00001659 vls_handle_t vlsh;
1660 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001661
Florin Coras5f33d0d2021-06-02 21:22:21 -07001662 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001663
Florin Corasce17f462020-05-22 20:36:29 +00001664 vlsh = ldp_fd_to_vlsh (fd);
1665 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001666 {
Florin Corasce17f462020-05-22 20:36:29 +00001667 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001668 if (size < 0)
1669 {
1670 errno = -size;
1671 size = -1;
1672 }
1673 }
1674 else
1675 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001676 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1677 }
1678
Dave Wallace048b1d62018-01-03 22:24:41 -05001679 return size;
1680}
1681
1682ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001683sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001684{
Florin Coras7baeb712019-01-04 17:05:43 -08001685 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001686 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001687
Florin Coras5f33d0d2021-06-02 21:22:21 -07001688 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001689
Florin Coras7baeb712019-01-04 17:05:43 -08001690 vlsh = ldp_fd_to_vlsh (fd);
1691 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001692 {
Florin Corasce17f462020-05-22 20:36:29 +00001693 struct iovec *iov = msg->msg_iov;
1694 ssize_t total = 0;
Florin Corascba1c222021-11-29 08:12:27 -08001695 int i, rv = 0;
Dou Chao243a0432022-11-29 19:41:34 +08001696 struct cmsghdr *cmsg;
1697 uint16_t *valp;
1698 vppcom_endpt_tlv_t _app_data;
1699 vppcom_endpt_tlv_t *p_app_data = NULL;
1700
1701 cmsg = CMSG_FIRSTHDR (msg);
1702 if (cmsg && cmsg->cmsg_type == UDP_SEGMENT)
1703 {
1704 p_app_data = &_app_data;
1705 valp = (void *) CMSG_DATA (cmsg);
1706 p_app_data->data_type = VCL_UDP_SEGMENT;
1707 p_app_data->data_len = sizeof (*valp);
1708 p_app_data->value = *valp;
1709 }
Florin Corasce17f462020-05-22 20:36:29 +00001710
1711 for (i = 0; i < msg->msg_iovlen; ++i)
1712 {
Dou Chao243a0432022-11-29 19:41:34 +08001713 rv =
1714 ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, p_app_data,
1715 flags, msg->msg_name, msg->msg_namelen);
Florin Corasce17f462020-05-22 20:36:29 +00001716 if (rv < 0)
1717 break;
1718 else
1719 {
1720 total += rv;
1721 if (rv < iov[i].iov_len)
1722 break;
1723 }
1724 }
1725
1726 if (rv < 0 && total == 0)
1727 {
1728 errno = -rv;
1729 size = -1;
1730 }
1731 else
1732 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001733 }
1734 else
1735 {
Florin Corasce17f462020-05-22 20:36:29 +00001736 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001737 }
1738
Dave Wallace048b1d62018-01-03 22:24:41 -05001739 return size;
1740}
1741
Florin Coras36847942023-02-02 12:56:16 -08001742#ifdef _GNU_SOURCE
Dave Wallace048b1d62018-01-03 22:24:41 -05001743int
1744sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1745{
1746 ssize_t size;
1747 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001748 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001749
Florin Coras5f33d0d2021-06-02 21:22:21 -07001750 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001751
wanghanlin97c6e0d2021-07-06 15:01:48 +08001752 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001753 {
1754 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1755 errno = ENOSYS;
1756 size = -1;
1757 }
1758 else
1759 {
1760 func_str = "libc_sendmmsg";
1761
Dave Wallace2a865272018-02-07 21:00:42 -05001762 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001763 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1764 "vmessages %p, vlen %u, flags 0x%x",
1765 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1766
1767 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1768 }
1769
Dave Wallace2a865272018-02-07 21:00:42 -05001770 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001771 {
1772 if (size < 0)
1773 {
1774 int errno_val = errno;
1775 perror (func_str);
1776 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1777 "rv %d, errno = %d", getpid (), fd, fd,
1778 func_str, size, errno_val);
1779 errno = errno_val;
1780 }
1781 else
1782 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1783 getpid (), fd, fd, size, size);
1784 }
1785 return size;
1786}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001787#endif
1788
Dave Wallace048b1d62018-01-03 22:24:41 -05001789ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001790recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001791{
Florin Coras7baeb712019-01-04 17:05:43 -08001792 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001793 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001794
Florin Coras5f33d0d2021-06-02 21:22:21 -07001795 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001796
Florin Coras7baeb712019-01-04 17:05:43 -08001797 vlsh = ldp_fd_to_vlsh (fd);
1798 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001799 {
Florin Corasce17f462020-05-22 20:36:29 +00001800 struct iovec *iov = msg->msg_iov;
1801 ssize_t max_deq, total = 0;
1802 int i, rv;
1803
1804 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1805 if (!max_deq)
1806 return 0;
1807
1808 for (i = 0; i < msg->msg_iovlen; i++)
1809 {
1810 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1811 (i == 0 ? msg->msg_name : NULL),
1812 (i == 0 ? &msg->msg_namelen : NULL));
1813 if (rv <= 0)
1814 break;
1815 else
1816 {
1817 total += rv;
1818 if (rv < iov[i].iov_len)
1819 break;
1820 }
1821 if (total >= max_deq)
1822 break;
1823 }
1824
1825 if (rv < 0 && total == 0)
1826 {
1827 errno = -rv;
1828 size = -1;
1829 }
1830 else
1831 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001832 }
1833 else
1834 {
Florin Corasce17f462020-05-22 20:36:29 +00001835 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001836 }
1837
Dave Wallace048b1d62018-01-03 22:24:41 -05001838 return size;
1839}
1840
Florin Coras36847942023-02-02 12:56:16 -08001841#ifdef _GNU_SOURCE
Dave Wallace048b1d62018-01-03 22:24:41 -05001842int
1843recvmmsg (int fd, struct mmsghdr *vmessages,
1844 unsigned int vlen, int flags, struct timespec *tmo)
1845{
1846 ssize_t size;
1847 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001848 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001849
Florin Coras5f33d0d2021-06-02 21:22:21 -07001850 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001851
wanghanlin97c6e0d2021-07-06 15:01:48 +08001852 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001853 {
1854 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1855 errno = ENOSYS;
1856 size = -1;
1857 }
1858 else
1859 {
1860 func_str = "libc_recvmmsg";
1861
Dave Wallace2a865272018-02-07 21:00:42 -05001862 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001863 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1864 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1865 getpid (), fd, fd, func_str, vmessages, vlen,
1866 flags, tmo);
1867
1868 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1869 }
1870
Dave Wallace2a865272018-02-07 21:00:42 -05001871 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001872 {
1873 if (size < 0)
1874 {
1875 int errno_val = errno;
1876 perror (func_str);
1877 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1878 "rv %d, errno = %d", getpid (), fd, fd,
1879 func_str, size, errno_val);
1880 errno = errno_val;
1881 }
1882 else
1883 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1884 getpid (), fd, fd, size, size);
1885 }
1886 return size;
1887}
1888#endif
1889
1890int
1891getsockopt (int fd, int level, int optname,
1892 void *__restrict optval, socklen_t * __restrict optlen)
1893{
Florin Coras7baeb712019-01-04 17:05:43 -08001894 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001895 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001896
Florin Coras5f33d0d2021-06-02 21:22:21 -07001897 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001898
Florin Coras7baeb712019-01-04 17:05:43 -08001899 vlsh = ldp_fd_to_vlsh (fd);
1900 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001901 {
1902 rv = -EOPNOTSUPP;
1903
1904 switch (level)
1905 {
1906 case SOL_TCP:
1907 switch (optname)
1908 {
1909 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001910 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1911 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001912 break;
1913 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001914 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1915 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001916 break;
1917 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001918 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1919 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001920 break;
1921 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001922 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1923 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001924 break;
1925 case TCP_INFO:
1926 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1927 {
Florin Coras7baeb712019-01-04 17:05:43 -08001928 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1929 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001930 memset (optval, 0, *optlen);
1931 rv = VPPCOM_OK;
1932 }
1933 else
1934 rv = -EFAULT;
1935 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001936 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001937 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001938 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001939 rv = 0;
1940 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001941 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001942 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1943 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001944 break;
1945 }
1946 break;
1947 case SOL_IPV6:
1948 switch (optname)
1949 {
1950 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001951 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001952 break;
1953 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001954 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1955 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001956 break;
1957 }
1958 break;
1959 case SOL_SOCKET:
1960 switch (optname)
1961 {
1962 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001963 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001964 break;
1965 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001966 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001967 break;
1968 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001969 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001970 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1971 break;
1972 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001973 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1974 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001975 break;
1976 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001977 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1978 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001979 break;
1980 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001981 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001982 break;
wanghanlin0674f852021-02-22 10:38:36 +08001983 case SO_REUSEPORT:
1984 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
1985 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001986 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001987 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001988 break;
wanghanlin0674f852021-02-22 10:38:36 +08001989 case SO_DOMAIN:
1990 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
1991 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001992 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001993 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001994 break;
Florin Coras8eb8d502021-06-16 14:46:57 -07001995 case SO_BINDTODEVICE:
1996 rv = 0;
1997 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001998 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001999 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
2000 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002001 break;
2002 }
2003 break;
2004 default:
2005 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002006 }
2007
Dave Wallace048b1d62018-01-03 22:24:41 -05002008 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002009 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002010 errno = -rv;
2011 rv = -1;
2012 }
2013 }
2014 else
2015 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002016 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2017 }
2018
Dave Wallace048b1d62018-01-03 22:24:41 -05002019 return rv;
2020}
2021
2022int
2023setsockopt (int fd, int level, int optname,
2024 const void *optval, socklen_t optlen)
2025{
Florin Coras7baeb712019-01-04 17:05:43 -08002026 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002027 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002028
Florin Coras5f33d0d2021-06-02 21:22:21 -07002029 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002030
Florin Coras7baeb712019-01-04 17:05:43 -08002031 vlsh = ldp_fd_to_vlsh (fd);
2032 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002033 {
2034 rv = -EOPNOTSUPP;
2035
2036 switch (level)
2037 {
2038 case SOL_TCP:
2039 switch (optname)
2040 {
2041 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002042 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2043 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002044 break;
2045 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002046 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2047 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002048 break;
2049 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002050 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2051 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002052 break;
2053 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002054 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2055 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002056 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002057 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002058 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002059 /* Ignore */
2060 rv = 0;
2061 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002062 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002063 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2064 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002065 break;
2066 }
2067 break;
2068 case SOL_IPV6:
2069 switch (optname)
2070 {
2071 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002072 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2073 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002074 break;
2075 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002076 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2077 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002078 break;
2079 }
2080 break;
2081 case SOL_SOCKET:
2082 switch (optname)
2083 {
2084 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002085 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2086 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002087 break;
2088 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002089 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2090 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002091 break;
wanghanlin0674f852021-02-22 10:38:36 +08002092 case SO_REUSEPORT:
2093 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2094 &optlen);
2095 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002096 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002097 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2098 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002099 break;
Florin Coras2f647902021-06-02 08:23:50 -07002100 case SO_LINGER:
2101 rv = 0;
2102 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002103 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002104 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2105 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002106 break;
2107 }
2108 break;
2109 default:
2110 break;
2111 }
2112
2113 if (rv != VPPCOM_OK)
2114 {
2115 errno = -rv;
2116 rv = -1;
2117 }
2118 }
2119 else
2120 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2122 }
2123
Dave Wallace048b1d62018-01-03 22:24:41 -05002124 return rv;
2125}
2126
2127int
2128listen (int fd, int n)
2129{
Florin Coras7baeb712019-01-04 17:05:43 -08002130 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002131 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002132
Florin Coras5f33d0d2021-06-02 21:22:21 -07002133 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002134
Florin Coras7baeb712019-01-04 17:05:43 -08002135 vlsh = ldp_fd_to_vlsh (fd);
2136 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002137 {
Florin Coras7baeb712019-01-04 17:05:43 -08002138 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002139
Florin Coras7baeb712019-01-04 17:05:43 -08002140 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002141 if (rv != VPPCOM_OK)
2142 {
2143 errno = -rv;
2144 rv = -1;
2145 }
2146 }
2147 else
2148 {
Florin Coras7baeb712019-01-04 17:05:43 -08002149 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002150 rv = libc_listen (fd, n);
2151 }
2152
Florin Coras7baeb712019-01-04 17:05:43 -08002153 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002154 return rv;
2155}
2156
2157static inline int
Florin Coras36847942023-02-02 12:56:16 -08002158ldp_accept4 (int listen_fd, __SOCKADDR_ARG _addr,
2159 socklen_t *__restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002160{
Florin Coras36847942023-02-02 12:56:16 -08002161 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08002162 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002163 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002164
Florin Coras5f33d0d2021-06-02 21:22:21 -07002165 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002166
Florin Coras7baeb712019-01-04 17:05:43 -08002167 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2168 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002169 {
2170 vppcom_endpt_t ep;
2171 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002172 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002173 ep.ip = src_addr;
2174
Florin Coras7baeb712019-01-04 17:05:43 -08002175 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002176 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002177
Florin Coras7baeb712019-01-04 17:05:43 -08002178 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2179 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002180 {
Florin Coras7baeb712019-01-04 17:05:43 -08002181 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002182 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002183 }
2184 else
2185 {
Dave Wallace2a865272018-02-07 21:00:42 -05002186 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002187 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002188 {
Florin Coras7baeb712019-01-04 17:05:43 -08002189 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002190 errno = -rv;
2191 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002192 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002193 else
2194 {
Florin Coras7baeb712019-01-04 17:05:43 -08002195 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002196 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002197 }
2198 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002199 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002200 {
Florin Coras7baeb712019-01-04 17:05:43 -08002201 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2202 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002203
Dave Wallace048b1d62018-01-03 22:24:41 -05002204 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002205 }
2206
Florin Coras7baeb712019-01-04 17:05:43 -08002207 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002208
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002209 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002210}
2211
Dave Wallace048b1d62018-01-03 22:24:41 -05002212int
2213accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2214 int flags)
2215{
Dave Wallace2a865272018-02-07 21:00:42 -05002216 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002217}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002218
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002219int
Dave Wallace048b1d62018-01-03 22:24:41 -05002220accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002221{
Dave Wallace2a865272018-02-07 21:00:42 -05002222 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002223}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002224
Dave Wallace048b1d62018-01-03 22:24:41 -05002225int
2226shutdown (int fd, int how)
2227{
Florin Coras7baeb712019-01-04 17:05:43 -08002228 vls_handle_t vlsh;
liuyacan55c952e2021-06-13 14:54:55 +08002229 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002230
Florin Coras5f33d0d2021-06-02 21:22:21 -07002231 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002232
Florin Coras7baeb712019-01-04 17:05:43 -08002233 vlsh = ldp_fd_to_vlsh (fd);
2234 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002235 {
Florin Coras7baeb712019-01-04 17:05:43 -08002236 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
liuyacan55c952e2021-06-13 14:54:55 +08002237 rv = vls_shutdown (vlsh, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002238 }
2239 else
2240 {
Florin Coras7baeb712019-01-04 17:05:43 -08002241 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002242 rv = libc_shutdown (fd, how);
2243 }
2244
Dave Wallace048b1d62018-01-03 22:24:41 -05002245 return rv;
2246}
2247
2248int
2249epoll_create1 (int flags)
2250{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002251 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002252 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002253 int rv;
2254
Florin Coras5f33d0d2021-06-02 21:22:21 -07002255 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002256
hanlina3a48962020-07-13 11:09:15 +08002257 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002258 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002259 /* Make sure workers have been allocated */
2260 if (!ldp->workers)
2261 {
2262 ldp_alloc_workers ();
2263 ldpw = ldp_worker_get_current ();
2264 }
Florin Coras99368312018-08-02 10:45:44 -07002265 rv = libc_epoll_create1 (flags);
2266 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002267 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002268 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002269 return rv;
2270 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002271
Florin Coras7baeb712019-01-04 17:05:43 -08002272 vlsh = vls_epoll_create ();
2273 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002274 {
Florin Coras7baeb712019-01-04 17:05:43 -08002275 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002276 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002277 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002278 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002279 {
Florin Coras7baeb712019-01-04 17:05:43 -08002280 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002281 }
Florin Coras7baeb712019-01-04 17:05:43 -08002282 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002283 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002284}
2285
2286int
Dave Wallace048b1d62018-01-03 22:24:41 -05002287epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002288{
Dave Wallace048b1d62018-01-03 22:24:41 -05002289 return epoll_create1 (0);
2290}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002291
Dave Wallace048b1d62018-01-03 22:24:41 -05002292int
2293epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2294{
Florin Coras7baeb712019-01-04 17:05:43 -08002295 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002296 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002297
Florin Coras5f33d0d2021-06-02 21:22:21 -07002298 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002299
Florin Coras7baeb712019-01-04 17:05:43 -08002300 vep_vlsh = ldp_fd_to_vlsh (epfd);
2301 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002302 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002303 /* The LDP epoll_create1 always creates VCL epfd's.
2304 * The app should never have a kernel base epoll fd unless it
2305 * was acquired outside of the LD_PRELOAD process context.
2306 * In any case, if we get one, punt it to libc_epoll_ctl.
2307 */
Florin Coras7baeb712019-01-04 17:05:43 -08002308 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2309 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002310
2311 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002312 goto done;
2313 }
2314
Florin Coras7baeb712019-01-04 17:05:43 -08002315 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002316
Florin Coras7baeb712019-01-04 17:05:43 -08002317 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2318 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002319
Florin Coras7baeb712019-01-04 17:05:43 -08002320 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002321 {
Florin Coras7baeb712019-01-04 17:05:43 -08002322 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002323 " event %p", epfd, vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002324
Florin Coras7baeb712019-01-04 17:05:43 -08002325 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002326 if (rv != VPPCOM_OK)
2327 {
2328 errno = -rv;
2329 rv = -1;
2330 }
2331 }
2332 else
2333 {
2334 int libc_epfd;
2335 u32 size = sizeof (epfd);
2336
Florin Coras7baeb712019-01-04 17:05:43 -08002337 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002338 if (!libc_epfd)
2339 {
Florin Coras7baeb712019-01-04 17:05:43 -08002340 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2341 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002342
2343 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2344 if (libc_epfd < 0)
2345 {
2346 rv = libc_epfd;
2347 goto done;
2348 }
2349
Florin Coras7baeb712019-01-04 17:05:43 -08002350 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2351 &size);
Florin Coras99368312018-08-02 10:45:44 -07002352 if (rv < 0)
2353 {
2354 errno = -rv;
2355 rv = -1;
2356 goto done;
2357 }
2358 }
2359 else if (PREDICT_FALSE (libc_epfd < 0))
2360 {
2361 errno = -epfd;
2362 rv = -1;
2363 goto done;
2364 }
2365
Florin Coras7baeb712019-01-04 17:05:43 -08002366 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2367 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002368
2369 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002370 }
2371
2372done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002373 return rv;
2374}
Dave Wallace048b1d62018-01-03 22:24:41 -05002375
2376static inline int
Florin Coras99368312018-08-02 10:45:44 -07002377ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2378 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002379{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002380 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002381 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002382 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002383 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002384
Florin Coras5f33d0d2021-06-02 21:22:21 -07002385 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002386
2387 if (PREDICT_FALSE (!events || (timeout < -1)))
2388 {
2389 errno = EFAULT;
2390 return -1;
2391 }
2392
Florin Corasdfe4cf42018-11-28 22:13:45 -08002393 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002394 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2395
Florin Coras7baeb712019-01-04 17:05:43 -08002396 ep_vlsh = ldp_fd_to_vlsh (epfd);
2397 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002398 {
Florin Coras7baeb712019-01-04 17:05:43 -08002399 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002400 errno = EBADFD;
2401 return -1;
2402 }
2403
Florin Coras4dee8cd2019-01-29 21:28:16 -08002404 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2405 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002406 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002407 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002408
Florin Coras7baeb712019-01-04 17:05:43 -08002409 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002410 if (PREDICT_FALSE (libc_epfd < 0))
2411 {
2412 errno = -libc_epfd;
2413 rv = -1;
2414 goto done;
2415 }
2416
Florin Coras7baeb712019-01-04 17:05:43 -08002417 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2418 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002419 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002420 do
2421 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002422 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002423 {
Florin Coras7baeb712019-01-04 17:05:43 -08002424 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002425 if (rv > 0)
2426 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002427 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002428 goto done;
2429 }
2430 else if (rv < 0)
2431 {
2432 errno = -rv;
2433 rv = -1;
2434 goto done;
2435 }
2436 }
2437 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002438 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002439
2440 if (libc_epfd > 0)
2441 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002442 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002443 if (rv != 0)
2444 goto done;
2445 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002446 }
Florin Coras72f77822019-01-22 19:05:52 -08002447 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002448
2449done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002450 return rv;
2451}
2452
hanlin4266d4d2020-05-19 17:34:17 +08002453static inline int
2454ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2455 int maxevents, int timeout, const sigset_t * sigmask)
2456{
hanlina3a48962020-07-13 11:09:15 +08002457 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002458 int libc_epfd, rv = 0, num_ev;
2459 vls_handle_t ep_vlsh;
2460
Florin Coras5f33d0d2021-06-02 21:22:21 -07002461 ldp_init_check ();
hanlin4266d4d2020-05-19 17:34:17 +08002462
2463 if (PREDICT_FALSE (!events || (timeout < -1)))
2464 {
2465 errno = EFAULT;
2466 return -1;
2467 }
2468
Florin Corasff40d8f2020-08-11 22:05:28 -07002469 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2470 * one thread but it is now used on another */
2471 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2472 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002473
2474 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002475 if (epfd == ldpw->vcl_mq_epfd)
2476 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2477
2478 ep_vlsh = ldp_fd_to_vlsh (epfd);
2479 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2480 {
2481 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2482 errno = EBADFD;
2483 return -1;
2484 }
2485
2486 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2487 if (PREDICT_FALSE (!libc_epfd))
2488 {
2489 u32 size = sizeof (epfd);
2490
2491 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2492 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2493 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2494 if (libc_epfd < 0)
2495 {
2496 rv = libc_epfd;
2497 goto done;
2498 }
2499
2500 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2501 if (rv < 0)
2502 {
2503 errno = -rv;
2504 rv = -1;
2505 goto done;
2506 }
2507 }
2508 if (PREDICT_FALSE (libc_epfd <= 0))
2509 {
2510 errno = -libc_epfd;
2511 rv = -1;
2512 goto done;
2513 }
2514
2515 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2516 {
2517 struct epoll_event e = { 0 };
2518 e.events = EPOLLIN;
2519 e.data.fd = ldpw->vcl_mq_epfd;
2520 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2521 0)
2522 {
2523 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2524 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2525 rv = -1;
2526 goto done;
2527 }
2528 ldpw->mq_epfd_added = 1;
2529 }
2530
wanghanlin8919fec2021-03-18 20:00:41 +08002531 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2532 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002533 if (rv > 0)
2534 goto done;
hanlina3a48962020-07-13 11:09:15 +08002535 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002536 {
2537 errno = -rv;
2538 rv = -1;
2539 goto done;
2540 }
2541
2542 rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask);
2543 if (rv <= 0)
2544 goto done;
2545 for (int i = 0; i < rv; i++)
2546 {
2547 if (events[i].data.fd == ldpw->vcl_mq_epfd)
2548 {
2549 /* We should remove mq epoll fd from events. */
2550 rv--;
2551 if (i != rv)
2552 {
2553 events[i].events = events[rv].events;
2554 events[i].data.u64 = events[rv].data.u64;
2555 }
2556 num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0);
2557 if (PREDICT_TRUE (num_ev > 0))
2558 rv += num_ev;
2559 break;
2560 }
2561 }
2562
2563done:
2564 return rv;
2565}
2566
Dave Wallace048b1d62018-01-03 22:24:41 -05002567int
2568epoll_pwait (int epfd, struct epoll_event *events,
2569 int maxevents, int timeout, const sigset_t * sigmask)
2570{
hanlin4266d4d2020-05-19 17:34:17 +08002571 if (vls_use_eventfd ())
2572 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2573 sigmask);
2574 else
2575 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002576}
2577
2578int
2579epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2580{
hanlin4266d4d2020-05-19 17:34:17 +08002581 if (vls_use_eventfd ())
2582 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2583 else
2584 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002585}
2586
2587int
2588poll (struct pollfd *fds, nfds_t nfds, int timeout)
2589{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002590 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002591 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002592 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002593 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002594 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002595
Florin Coraseb801d02020-09-16 17:44:58 -07002596 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002597
Florin Coras4dee8cd2019-01-29 21:28:16 -08002598 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2599 clib_time_init (&ldpw->clib_time);
2600
2601 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2602 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002603
Dave Wallace048b1d62018-01-03 22:24:41 -05002604 for (i = 0; i < nfds; i++)
2605 {
Florin Coras6917b942018-11-13 22:44:54 -08002606 if (fds[i].fd < 0)
2607 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002608
Florin Coras7baeb712019-01-04 17:05:43 -08002609 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2610 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002611 {
2612 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002613 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002614 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002615 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002616 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002617#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002618 if (fds[i].events & POLLRDNORM)
2619 vp->events |= POLLIN;
2620 if (fds[i].events & POLLWRNORM)
2621 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002622#endif
Florin Coras6917b942018-11-13 22:44:54 -08002623 vp->revents = fds[i].revents;
2624 }
2625 else
2626 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002627 vec_add1 (ldpw->libc_poll, fds[i]);
2628 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002629 }
2630 }
2631
Dave Wallace048b1d62018-01-03 22:24:41 -05002632 do
2633 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002634 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002635 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002636 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002637 if (rv < 0)
2638 {
2639 errno = -rv;
2640 rv = -1;
2641 goto done;
2642 }
2643 else
2644 n_revents += rv;
2645 }
2646
Florin Corasdfe4cf42018-11-28 22:13:45 -08002647 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002648 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002649 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002650 if (rv < 0)
2651 goto done;
2652 else
2653 n_revents += rv;
2654 }
2655
2656 if (n_revents)
2657 {
2658 rv = n_revents;
2659 goto done;
2660 }
2661 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002662 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002663 rv = 0;
2664
2665done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002666 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002667 {
2668 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002669 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002670#ifdef __USE_XOPEN2K
2671 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2672 (fds[vp->fds_ndx].events & POLLRDNORM))
2673 fds[vp->fds_ndx].revents |= POLLRDNORM;
2674 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2675 (fds[vp->fds_ndx].events & POLLWRNORM))
2676 fds[vp->fds_ndx].revents |= POLLWRNORM;
2677#endif
2678 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002679 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002680
Florin Corasdfe4cf42018-11-28 22:13:45 -08002681 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002682 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002683 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002684 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002685 vec_reset_length (ldpw->libc_poll_idxs);
2686 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002687
Dave Wallace048b1d62018-01-03 22:24:41 -05002688 return rv;
2689}
2690
Florin Coras36847942023-02-02 12:56:16 -08002691#ifdef _GNU_SOURCE
Dave Wallace048b1d62018-01-03 22:24:41 -05002692int
2693ppoll (struct pollfd *fds, nfds_t nfds,
2694 const struct timespec *timeout, const sigset_t * sigmask)
2695{
Florin Coras5f33d0d2021-06-02 21:22:21 -07002696 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002697
2698 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2699 errno = ENOSYS;
2700
2701
2702 return -1;
2703}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002704#endif
2705
Dave Wallace2a865272018-02-07 21:00:42 -05002706void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002707
Dave Wallace2a865272018-02-07 21:00:42 -05002708void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002709
Dave Wallace048b1d62018-01-03 22:24:41 -05002710/*
2711 * This function is called when the library is loaded
2712 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002713void
Dave Wallace2a865272018-02-07 21:00:42 -05002714ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002715{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002716 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002717 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002718 {
2719 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2720 getpid ());
2721 _exit (1);
2722 }
Dave Wallace69d01192018-02-22 16:22:09 -05002723 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002724 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002725}
2726
2727/*
2728 * This function is called when the library is unloaded
2729 */
2730void
Dave Wallace2a865272018-02-07 21:00:42 -05002731ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002732{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002733 /*
2734 swrap_destructor ();
2735 if (ldp->init)
2736 ldp->init = 0;
2737 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002738
2739 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002740 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002741 */
Dave Wallace69d01192018-02-22 16:22:09 -05002742 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002743 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2744 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002745}
2746
2747
2748/*
2749 * fd.io coding-style-patch-verification: ON
2750 *
2751 * Local Variables:
2752 * eval: (c-set-style "gnu")
2753 * End:
2754 */