blob: ffe1673d0ef5a8fda01346fa3455917100aaff11 [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>
Florin Coraseff5f7a2023-02-07 17:36:17 -080029#include <netinet/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
Tianyu Li2073fe42023-02-27 09:14:34 +000066#ifndef UDP_SEGMENT
67#define UDP_SEGMENT 103
68#endif
69
Florin Corasdfe4cf42018-11-28 22:13:45 -080070typedef struct ldp_worker_ctx_
Dave Wallace048b1d62018-01-03 22:24:41 -050071{
Dave Wallace048b1d62018-01-03 22:24:41 -050072 u8 *io_buffer;
73 clib_time_t clib_time;
Florin Corasdfe4cf42018-11-28 22:13:45 -080074
75 /*
76 * Select state
77 */
Dave Wallace048b1d62018-01-03 22:24:41 -050078 clib_bitmap_t *rd_bitmap;
79 clib_bitmap_t *wr_bitmap;
80 clib_bitmap_t *ex_bitmap;
Florin Coras294afe22019-01-07 17:49:17 -080081 clib_bitmap_t *si_rd_bitmap;
82 clib_bitmap_t *si_wr_bitmap;
83 clib_bitmap_t *si_ex_bitmap;
Dave Wallace048b1d62018-01-03 22:24:41 -050084 clib_bitmap_t *libc_rd_bitmap;
85 clib_bitmap_t *libc_wr_bitmap;
86 clib_bitmap_t *libc_ex_bitmap;
Florin Corasdfe4cf42018-11-28 22:13:45 -080087
88 /*
89 * Poll state
90 */
Dave Wallace048b1d62018-01-03 22:24:41 -050091 vcl_poll_t *vcl_poll;
Florin Coras6917b942018-11-13 22:44:54 -080092 struct pollfd *libc_poll;
93 u16 *libc_poll_idxs;
Florin Corasdfe4cf42018-11-28 22:13:45 -080094
95 /*
96 * Epoll state
97 */
Dave Wallace048b1d62018-01-03 22:24:41 -050098 u8 epoll_wait_vcl;
hanlin4266d4d2020-05-19 17:34:17 +080099 u8 mq_epfd_added;
Florin Coras99368312018-08-02 10:45:44 -0700100 int vcl_mq_epfd;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800101
102} ldp_worker_ctx_t;
103
Florin Coras294afe22019-01-07 17:49:17 -0800104/* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
105 * they are the same size */
106STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
107 "ldp bitmap size mismatch");
108STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
109 "ldp bitmap size mismatch");
110
Florin Corasdfe4cf42018-11-28 22:13:45 -0800111typedef struct
112{
113 ldp_worker_ctx_t *workers;
114 int init;
115 char app_name[LDP_APP_NAME_MAX];
Florin Coras7baeb712019-01-04 17:05:43 -0800116 u32 vlsh_bit_val;
117 u32 vlsh_bit_mask;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800118 u32 debug;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800119
120 /** vcl needs next epoll_create to go to libc_epoll */
121 u8 vcl_needs_real_epoll;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800122
123 /**
124 * crypto state used only for testing
125 */
126 u8 transparent_tls;
127 u32 ckpair_index;
Dave Wallace2a865272018-02-07 21:00:42 -0500128} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800129
Dave Wallace2a865272018-02-07 21:00:42 -0500130#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700131
Florin Coras99368312018-08-02 10:45:44 -0700132#define LDBG(_lvl, _fmt, _args...) \
133 if (ldp->debug > _lvl) \
hanlin9f3f18f2019-12-30 16:25:20 +0800134 { \
135 int errno_saved = errno; \
Florin Coras585c86a2020-10-16 17:57:36 -0700136 fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \
hanlin9f3f18f2019-12-30 16:25:20 +0800137 errno = errno_saved; \
138 }
Florin Coras99368312018-08-02 10:45:44 -0700139
Dave Wallace2a865272018-02-07 21:00:42 -0500140static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800141 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
142 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500143 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800144 .transparent_tls = 0,
Florin Corasa5a9efd2021-01-05 17:03:29 -0800145 .ckpair_index = ~0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500146};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700147
Dave Wallace2a865272018-02-07 21:00:42 -0500148static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700149
Florin Corasdfe4cf42018-11-28 22:13:45 -0800150static inline ldp_worker_ctx_t *
151ldp_worker_get_current (void)
152{
153 return (ldp->workers + vppcom_worker_index ());
154}
155
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700156/*
157 * RETURN: 0 on success or -1 on error.
158 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500159static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500160ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700161{
Florin Coras729b9c92023-02-28 10:58:08 -0800162 snprintf (ldp->app_name, LDP_APP_NAME_MAX, "%s-ldp-%d", app_name, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700163}
164
Dave Wallace048b1d62018-01-03 22:24:41 -0500165static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500166ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700167{
Dave Wallace2a865272018-02-07 21:00:42 -0500168 if (ldp->app_name[0] == '\0')
Florin Coras729b9c92023-02-28 10:58:08 -0800169 ldp_set_app_name (program_invocation_short_name);
Dave Wallace048b1d62018-01-03 22:24:41 -0500170
Dave Wallace2a865272018-02-07 21:00:42 -0500171 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700172}
173
Dave Wallace048b1d62018-01-03 22:24:41 -0500174static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800175ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500176{
Florin Coras7baeb712019-01-04 17:05:43 -0800177 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500178}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700179
Florin Coras7baeb712019-01-04 17:05:43 -0800180static inline vls_handle_t
181ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500182{
Florin Coras7baeb712019-01-04 17:05:43 -0800183 if (fd < ldp->vlsh_bit_val)
184 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700185
Florin Coras7baeb712019-01-04 17:05:43 -0800186 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500187}
188
Florin Coras2d9b4272019-03-11 10:14:37 -0700189static void
190ldp_alloc_workers (void)
191{
192 if (ldp->workers)
193 return;
194 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
195}
196
Florin Corasc6c4e862023-03-09 18:23:05 -0800197static void
198ldp_init_cfg (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700199{
Florin Coras99368312018-08-02 10:45:44 -0700200 char *env_var_str = getenv (LDP_ENV_DEBUG);
201 if (env_var_str)
202 {
203 u32 tmp;
204 if (sscanf (env_var_str, "%u", &tmp) != 1)
205 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
206 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
207 env_var_str);
208 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700209 {
Florin Coras99368312018-08-02 10:45:44 -0700210 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800211 LDBG (0, "configured LDP debug level (%u) from env var "
212 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700213 }
214 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500215
Florin Coras99368312018-08-02 10:45:44 -0700216 env_var_str = getenv (LDP_ENV_APP_NAME);
217 if (env_var_str)
218 {
219 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800220 LDBG (0, "configured LDP app name (%s) from the env var "
221 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700222 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500223
Florin Coras99368312018-08-02 10:45:44 -0700224 env_var_str = getenv (LDP_ENV_SID_BIT);
225 if (env_var_str)
226 {
227 u32 sb;
228 if (sscanf (env_var_str, "%u", &sb) != 1)
229 {
Florin Coras294afe22019-01-07 17:49:17 -0800230 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
231 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
232 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700233 }
234 else if (sb < LDP_SID_BIT_MIN)
235 {
Florin Coras7baeb712019-01-04 17:05:43 -0800236 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
237 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500238
Florin Coras294afe22019-01-07 17:49:17 -0800239 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
240 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
241 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
242 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700243 }
244 else if (sb > LDP_SID_BIT_MAX)
245 {
Florin Coras7baeb712019-01-04 17:05:43 -0800246 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
247 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500248
Florin Coras294afe22019-01-07 17:49:17 -0800249 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
250 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
251 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
252 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500253 }
254 else
255 {
Florin Coras7baeb712019-01-04 17:05:43 -0800256 ldp->vlsh_bit_val = (1 << sb);
257 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700258
Florin Coras05ecfcc2018-12-12 18:19:39 -0800259 LDBG (0, "configured LDP sid bit (%u) from "
260 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800261 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500262 }
Florin Coras294afe22019-01-07 17:49:17 -0800263
264 /* Make sure there are enough bits in the fd set for vcl sessions */
265 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
266 {
wanghanlin1eb8fea2021-10-14 11:10:26 +0800267 /* Only valid for select/pselect, so just WARNING and not exit */
268 LDBG (0,
269 "WARNING: LDP vlsh bit value %d > FD_SETSIZE/2 %d, "
270 "select/pselect not supported now!",
Florin Coras294afe22019-01-07 17:49:17 -0800271 ldp->vlsh_bit_val, FD_SETSIZE / 2);
Florin Coras294afe22019-01-07 17:49:17 -0800272 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500273 }
Yu Ping7b74b072019-05-08 00:40:24 +0800274 env_var_str = getenv (LDP_ENV_TLS_TRANS);
275 if (env_var_str)
276 {
277 ldp->transparent_tls = 1;
278 }
Florin Corasc6c4e862023-03-09 18:23:05 -0800279}
Florin Coras99368312018-08-02 10:45:44 -0700280
Florin Corasc6c4e862023-03-09 18:23:05 -0800281static int
282ldp_init (void)
283{
284 ldp_worker_ctx_t *ldpw;
285 int rv;
286
287 ASSERT (!ldp->init);
288
289 ldp_init_cfg ();
290 ldp->init = 1;
291 ldp->vcl_needs_real_epoll = 1;
292 rv = vls_app_create (ldp_get_app_name ());
293 if (rv != VPPCOM_OK)
294 {
295 ldp->vcl_needs_real_epoll = 0;
296 if (rv == VPPCOM_EEXIST)
297 return 0;
298 LDBG (2,
299 "\nERROR: ldp_init: vppcom_app_create()"
300 " failed! rv = %d (%s)\n",
301 rv, vppcom_retval_str (rv));
302 ldp->init = 0;
303 return rv;
304 }
305 ldp->vcl_needs_real_epoll = 0;
306 ldp_alloc_workers ();
307 ldpw = ldp_worker_get_current ();
308
Damjan Marionb2c31b62020-12-13 21:47:40 +0100309 pool_foreach (ldpw, ldp->workers) {
Florin Coras4dee8cd2019-01-29 21:28:16 -0800310 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
Damjan Marionb2c31b62020-12-13 21:47:40 +0100311 }
Florin Coras4dee8cd2019-01-29 21:28:16 -0800312
Florin Coras05ecfcc2018-12-12 18:19:39 -0800313 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700314
315 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500316}
317
Florin Coras5f33d0d2021-06-02 21:22:21 -0700318#define ldp_init_check() \
319 if (PREDICT_FALSE (!ldp->init)) \
320 { \
321 if ((errno = -ldp_init ())) \
322 return -1; \
323 }
324
Dave Wallace048b1d62018-01-03 22:24:41 -0500325int
326close (int fd)
327{
Florin Coras7baeb712019-01-04 17:05:43 -0800328 vls_handle_t vlsh;
329 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500330
Florin Coras5f33d0d2021-06-02 21:22:21 -0700331 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500332
Florin Coras7baeb712019-01-04 17:05:43 -0800333 vlsh = ldp_fd_to_vlsh (fd);
334 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500335 {
Florin Coras7baeb712019-01-04 17:05:43 -0800336 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500337 if (epfd > 0)
338 {
Florin Coras06defbc2023-02-28 22:32:31 -0800339 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
340 u32 size = sizeof (epfd);
341
Florin Coras7baeb712019-01-04 17:05:43 -0800342 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500343
Florin Coras06defbc2023-02-28 22:32:31 -0800344 libc_close (epfd);
345 ldpw->mq_epfd_added = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500346
Florin Coras06defbc2023-02-28 22:32:31 -0800347 epfd = 0;
348 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500349 }
350 else if (PREDICT_FALSE (epfd < 0))
351 {
352 errno = -epfd;
353 rv = -1;
354 goto done;
355 }
356
Florin Coras7baeb712019-01-04 17:05:43 -0800357 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500358
Florin Coras7baeb712019-01-04 17:05:43 -0800359 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500360 if (rv != VPPCOM_OK)
361 {
362 errno = -rv;
363 rv = -1;
364 }
365 }
366 else
367 {
Florin Coras7baeb712019-01-04 17:05:43 -0800368 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500369 rv = libc_close (fd);
370 }
371
372done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500373 return rv;
374}
375
376ssize_t
377read (int fd, void *buf, size_t nbytes)
378{
Florin Coras7baeb712019-01-04 17:05:43 -0800379 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500380 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500381
Florin Coras5f33d0d2021-06-02 21:22:21 -0700382 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500383
Florin Coras7baeb712019-01-04 17:05:43 -0800384 vlsh = ldp_fd_to_vlsh (fd);
385 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500386 {
Florin Coras7baeb712019-01-04 17:05:43 -0800387 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500388 if (size < 0)
389 {
390 errno = -size;
391 size = -1;
392 }
393 }
394 else
395 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500396 size = libc_read (fd, buf, nbytes);
397 }
398
Dave Wallace048b1d62018-01-03 22:24:41 -0500399 return size;
400}
401
402ssize_t
403readv (int fd, const struct iovec * iov, int iovcnt)
404{
Dave Wallace8aaba562018-01-18 17:21:19 -0500405 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800406 vls_handle_t vlsh;
407 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500408
Florin Coras5f33d0d2021-06-02 21:22:21 -0700409 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500410
Florin Coras7baeb712019-01-04 17:05:43 -0800411 vlsh = ldp_fd_to_vlsh (fd);
412 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500413 {
Florin Coras067f9542020-02-14 05:33:46 +0000414 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500415 {
Florin Coras067f9542020-02-14 05:33:46 +0000416 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
417 if (rv <= 0)
418 break;
419 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500420 {
Florin Coras067f9542020-02-14 05:33:46 +0000421 total += rv;
422 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500423 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700424 }
425 }
Florin Coras067f9542020-02-14 05:33:46 +0000426 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700427 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500428 errno = -rv;
429 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700430 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500431 else
432 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700433 }
434 else
435 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500436 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700437 }
438
Dave Wallace048b1d62018-01-03 22:24:41 -0500439 return size;
440}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700441
Dave Wallace048b1d62018-01-03 22:24:41 -0500442ssize_t
443write (int fd, const void *buf, size_t nbytes)
444{
Florin Coras7baeb712019-01-04 17:05:43 -0800445 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500446 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500447
Florin Coras5f33d0d2021-06-02 21:22:21 -0700448 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500449
Florin Coras7baeb712019-01-04 17:05:43 -0800450 vlsh = ldp_fd_to_vlsh (fd);
451 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500452 {
Florin Coras7baeb712019-01-04 17:05:43 -0800453 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500454 if (size < 0)
455 {
456 errno = -size;
457 size = -1;
458 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700459 }
460 else
461 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500462 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700463 }
464
Dave Wallace048b1d62018-01-03 22:24:41 -0500465 return size;
466}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700467
Dave Wallace048b1d62018-01-03 22:24:41 -0500468ssize_t
469writev (int fd, const struct iovec * iov, int iovcnt)
470{
Dave Wallace048b1d62018-01-03 22:24:41 -0500471 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800472 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500473 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500474
Florin Coras5f33d0d2021-06-02 21:22:21 -0700475 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500476
Florin Coras7baeb712019-01-04 17:05:43 -0800477 vlsh = ldp_fd_to_vlsh (fd);
478 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500479 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000480 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500481 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000482 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
483 if (rv < 0)
484 break;
485 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500486 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000487 total += rv;
488 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500489 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500490 }
491 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500492
Florin Coraseda1b8c2020-03-23 16:00:35 +0000493 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500494 {
495 errno = -rv;
496 size = -1;
497 }
498 else
499 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700500 }
501 else
502 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500503 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700504 }
505
Dave Wallace048b1d62018-01-03 22:24:41 -0500506 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700507}
508
Florin Coras0ab36f52020-05-26 19:45:45 +0000509static int
510fcntl_internal (int fd, int cmd, va_list ap)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700511{
Florin Coras7baeb712019-01-04 17:05:43 -0800512 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700513 int rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800514
515 vlsh = ldp_fd_to_vlsh (fd);
516 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
517 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700518 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500519 int flags = va_arg (ap, int);
520 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700521
Dave Wallace048b1d62018-01-03 22:24:41 -0500522 size = sizeof (flags);
523 rv = -EOPNOTSUPP;
524 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700525 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500526 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800527 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500528 break;
529
530 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800531 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500532 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800533 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700534 break;
Florin Coras173bae32018-11-16 18:56:28 -0800535 case F_SETFD:
536 /* TODO handle this */
537 LDBG (0, "F_SETFD ignored flags %u", flags);
538 rv = 0;
539 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700540 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500541 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700542 break;
543 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500544 if (rv < 0)
545 {
546 errno = -rv;
547 rv = -1;
548 }
549 }
550 else
551 {
Carl Smithe16707b2019-11-13 14:37:39 +1300552#ifdef HAVE_FCNTL64
553 rv = libc_vfcntl64 (fd, cmd, ap);
554#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500555 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300556#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700557 }
558
Florin Coras0ab36f52020-05-26 19:45:45 +0000559 return rv;
560}
561
562int
563fcntl (int fd, int cmd, ...)
564{
565 va_list ap;
566 int rv;
567
Florin Coras5f33d0d2021-06-02 21:22:21 -0700568 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000569
570 va_start (ap, cmd);
571 rv = fcntl_internal (fd, cmd, ap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500572 va_end (ap);
573
Dave Wallace048b1d62018-01-03 22:24:41 -0500574 return rv;
575}
576
577int
Florin Corasd7586d52020-04-29 02:19:51 +0000578fcntl64 (int fd, int cmd, ...)
579{
580 va_list ap;
581 int rv;
582
Florin Coras5f33d0d2021-06-02 21:22:21 -0700583 ldp_init_check ();
Florin Coras0ab36f52020-05-26 19:45:45 +0000584
Florin Corasd7586d52020-04-29 02:19:51 +0000585 va_start (ap, cmd);
Florin Coras0ab36f52020-05-26 19:45:45 +0000586 rv = fcntl_internal (fd, cmd, ap);
Florin Corasd7586d52020-04-29 02:19:51 +0000587 va_end (ap);
588 return rv;
589}
590
591int
Dave Wallace048b1d62018-01-03 22:24:41 -0500592ioctl (int fd, unsigned long int cmd, ...)
593{
Florin Coras7baeb712019-01-04 17:05:43 -0800594 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500595 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800596 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500597
Florin Coras5f33d0d2021-06-02 21:22:21 -0700598 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500599
600 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500601
Florin Coras7baeb712019-01-04 17:05:43 -0800602 vlsh = ldp_fd_to_vlsh (fd);
603 if (vlsh != VLS_INVALID_HANDLE)
604 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500605 switch (cmd)
606 {
607 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800608 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500609 break;
610
611 case FIONBIO:
612 {
Florin Coras4a0e08e2023-05-11 11:45:34 -0700613 u32 flags = *(va_arg (ap, int *)) ? O_NONBLOCK : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500614 u32 size = sizeof (flags);
615
616 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
617 * non-blocking, the flags should be read here and merged
618 * with O_NONBLOCK.
619 */
Florin Coras7baeb712019-01-04 17:05:43 -0800620 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500621 }
622 break;
623
624 default:
625 rv = -EOPNOTSUPP;
626 break;
627 }
628 if (rv < 0)
629 {
630 errno = -rv;
631 rv = -1;
632 }
633 }
634 else
635 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500636 rv = libc_vioctl (fd, cmd, ap);
637 }
638
Dave Wallace048b1d62018-01-03 22:24:41 -0500639 va_end (ap);
640 return rv;
641}
642
Florin Coras294afe22019-01-07 17:49:17 -0800643always_inline void
644ldp_select_init_maps (fd_set * __restrict original,
645 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
646 clib_bitmap_t ** vclb, int nfds, u32 minbits,
647 u32 n_bytes, uword * si_bits, uword * libc_bits)
648{
649 uword si_bits_set, libc_bits_set;
650 vls_handle_t vlsh;
651 int fd;
652
653 clib_bitmap_validate (*vclb, minbits);
654 clib_bitmap_validate (*libcb, minbits);
655 clib_bitmap_validate (*resultb, minbits);
656 clib_memcpy_fast (*resultb, original, n_bytes);
657 memset (original, 0, n_bytes);
658
659 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100660 clib_bitmap_foreach (fd, *resultb) {
Florin Coras294afe22019-01-07 17:49:17 -0800661 if (fd > nfds)
662 break;
663 vlsh = ldp_fd_to_vlsh (fd);
664 if (vlsh == VLS_INVALID_HANDLE)
665 clib_bitmap_set_no_check (*libcb, fd, 1);
666 else
Florin Corascbce80a2020-04-20 01:32:38 +0000667 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100668 }
Florin Coras294afe22019-01-07 17:49:17 -0800669 /* *INDENT-ON* */
670
671 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
672 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000673 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800674
675 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
676 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
677}
678
679always_inline int
680ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
681{
682 vls_handle_t vlsh;
683 uword si;
684 int fd;
685
686 if (!libcb)
687 return 0;
688
689 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100690 clib_bitmap_foreach (si, vclb) {
Florin Coras294afe22019-01-07 17:49:17 -0800691 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000692 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800693 fd = ldp_vlsh_to_fd (vlsh);
694 if (PREDICT_FALSE (fd < 0))
695 {
696 errno = EBADFD;
697 return -1;
698 }
699 FD_SET (fd, libcb);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100700 }
Florin Coras294afe22019-01-07 17:49:17 -0800701 /* *INDENT-ON* */
702
703 return 0;
704}
705
706always_inline void
707ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
708{
709 uword fd;
710
Florin Coras78b5fa62019-02-21 20:04:15 -0800711 if (!libcb)
712 return;
713
Florin Coras294afe22019-01-07 17:49:17 -0800714 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100715 clib_bitmap_foreach (fd, result)
Florin Coras294afe22019-01-07 17:49:17 -0800716 FD_SET ((int)fd, libcb);
Florin Coras294afe22019-01-07 17:49:17 -0800717 /* *INDENT-ON* */
718}
719
Dave Wallace048b1d62018-01-03 22:24:41 -0500720int
Dave Wallace2a865272018-02-07 21:00:42 -0500721ldp_pselect (int nfds, fd_set * __restrict readfds,
722 fd_set * __restrict writefds,
723 fd_set * __restrict exceptfds,
724 const struct timespec *__restrict timeout,
725 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500726{
Florin Coras294afe22019-01-07 17:49:17 -0800727 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800728 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800729 struct timespec libc_tspec = { 0 };
730 f64 time_out, vcl_timeout = 0;
731 uword si_bits, libc_bits;
732 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500733
734 if (nfds < 0)
735 {
736 errno = EINVAL;
737 return -1;
738 }
739
Florin Coras4dee8cd2019-01-29 21:28:16 -0800740 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
741 clib_time_init (&ldpw->clib_time);
742
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500743 if (timeout)
744 {
745 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800746 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500747
liuyacanf71796e2021-08-02 10:01:39 +0800748 time_out += clib_time_now (&ldpw->clib_time);
749
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500750 /* select as fine grained sleep */
751 if (!nfds)
752 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800753 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500754 ;
755 return 0;
756 }
757 }
758 else if (!nfds)
759 {
760 errno = EINVAL;
761 return -1;
762 }
763 else
764 time_out = -1;
765
Florin Coras7baeb712019-01-04 17:05:43 -0800766 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500767 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500768 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
769 timeout, sigmask);
770 goto done;
771 }
772
Florin Coras294afe22019-01-07 17:49:17 -0800773 si_bits = libc_bits = 0;
774 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800775
Dave Wallace048b1d62018-01-03 22:24:41 -0500776 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800777 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
778 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
779 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500780 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800781 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
782 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
783 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500784 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800785 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
786 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
787 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500788
Florin Coras294afe22019-01-07 17:49:17 -0800789 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500790 {
791 errno = EINVAL;
792 rv = -1;
793 goto done;
794 }
795
Florin Coras78b5fa62019-02-21 20:04:15 -0800796 if (!si_bits)
797 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800798
Dave Wallace048b1d62018-01-03 22:24:41 -0500799 do
800 {
Florin Coras294afe22019-01-07 17:49:17 -0800801 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500802 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500803 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800804 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000805 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500806 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500807 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800808 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000809 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500810 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500811 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800812 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000813 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500814 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800815
Florin Coras0ef8ef22019-01-18 08:37:13 -0800816 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
817 writefds ? ldpw->wr_bitmap : NULL,
818 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800819 if (rv < 0)
820 {
821 errno = -rv;
822 rv = -1;
Florin Coras5e6222a2020-04-24 17:09:25 +0000823 goto done;
Florin Coras294afe22019-01-07 17:49:17 -0800824 }
825 else if (rv > 0)
826 {
827 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
828 {
829 rv = -1;
830 goto done;
831 }
832
833 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
834 {
835 rv = -1;
836 goto done;
837 }
838
839 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
840 {
841 rv = -1;
842 goto done;
843 }
844 bits_set = rv;
845 }
846 }
847 if (libc_bits)
848 {
849 if (readfds)
850 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
851 vec_len (ldpw->libc_rd_bitmap) *
852 sizeof (clib_bitmap_t));
853 if (writefds)
854 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
855 vec_len (ldpw->libc_wr_bitmap) *
856 sizeof (clib_bitmap_t));
857 if (exceptfds)
858 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
859 vec_len (ldpw->libc_ex_bitmap) *
860 sizeof (clib_bitmap_t));
861
Dave Wallace048b1d62018-01-03 22:24:41 -0500862 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800863 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
864 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
865 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
866 &libc_tspec, sigmask);
867 if (rv > 0)
868 {
869 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
870 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
871 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
872 bits_set += rv;
873 }
874 }
875
876 if (bits_set)
877 {
878 rv = bits_set;
879 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500880 }
881 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800882 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500883 rv = 0;
884
885done:
886 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800887 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800888 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800889 clib_bitmap_zero (ldpw->libc_rd_bitmap);
890 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800891 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800892 clib_bitmap_zero (ldpw->libc_wr_bitmap);
893 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800894 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800895 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500896
Dave Wallace048b1d62018-01-03 22:24:41 -0500897 return rv;
898}
899
900int
901select (int nfds, fd_set * __restrict readfds,
902 fd_set * __restrict writefds,
903 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
904{
905 struct timespec tspec;
906
907 if (timeout)
908 {
909 tspec.tv_sec = timeout->tv_sec;
910 tspec.tv_nsec = timeout->tv_usec * 1000;
911 }
Dave Wallace2a865272018-02-07 21:00:42 -0500912 return ldp_pselect (nfds, readfds, writefds, exceptfds,
913 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500914}
915
916#ifdef __USE_XOPEN2K
917int
918pselect (int nfds, fd_set * __restrict readfds,
919 fd_set * __restrict writefds,
920 fd_set * __restrict exceptfds,
921 const struct timespec *__restrict timeout,
922 const __sigset_t * __restrict sigmask)
923{
Dave Wallace2a865272018-02-07 21:00:42 -0500924 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500925}
926#endif
927
Yu Ping7b74b072019-05-08 00:40:24 +0800928/* If transparent TLS mode is turned on, then ldp will load key and cert.
929 */
930static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800931load_cert_key_pair (void)
Yu Ping7b74b072019-05-08 00:40:24 +0800932{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800933 char *cert_str = getenv (LDP_ENV_TLS_CERT);
934 char *key_str = getenv (LDP_ENV_TLS_KEY);
935 char cert_buf[4096], key_buf[4096];
936 int cert_size, key_size;
937 vppcom_cert_key_pair_t crypto;
938 int ckp_index;
Yu Ping7b74b072019-05-08 00:40:24 +0800939 FILE *fp;
940
Florin Corasa5a9efd2021-01-05 17:03:29 -0800941 if (!cert_str || !key_str)
Yu Ping7b74b072019-05-08 00:40:24 +0800942 {
943 LDBG (0, "ERROR: failed to read LDP environment %s\n",
944 LDP_ENV_TLS_CERT);
945 return -1;
946 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800947
948 fp = fopen (cert_str, "r");
949 if (fp == NULL)
950 {
951 LDBG (0, "ERROR: failed to open cert file %s \n", cert_str);
952 return -1;
953 }
954 cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp);
955 fclose (fp);
956
957 fp = fopen (key_str, "r");
958 if (fp == NULL)
959 {
960 LDBG (0, "ERROR: failed to open key file %s \n", key_str);
961 return -1;
962 }
963 key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp);
964 fclose (fp);
965
966 crypto.cert = cert_buf;
967 crypto.key = key_buf;
968 crypto.cert_len = cert_size;
969 crypto.key_len = key_size;
970 ckp_index = vppcom_add_cert_key_pair (&crypto);
971 if (ckp_index < 0)
972 {
973 LDBG (0, "ERROR: failed to add cert key pair\n");
974 return -1;
975 }
976
977 ldp->ckpair_index = ckp_index;
978
Yu Ping7b74b072019-05-08 00:40:24 +0800979 return 0;
980}
981
982static int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800983assign_cert_key_pair (vls_handle_t vlsh)
Yu Ping7b74b072019-05-08 00:40:24 +0800984{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800985 uint32_t ckp_len;
Yu Ping7b74b072019-05-08 00:40:24 +0800986
Florin Corasa5a9efd2021-01-05 17:03:29 -0800987 if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0)
988 return -1;
989
990 ckp_len = sizeof (ldp->ckpair_index);
liuyacan276675e2021-08-30 13:24:53 +0800991 return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &ckp_len);
Yu Ping7b74b072019-05-08 00:40:24 +0800992}
993
Dave Wallace048b1d62018-01-03 22:24:41 -0500994int
995socket (int domain, int type, int protocol)
996{
Florin Coras7baeb712019-01-04 17:05:43 -0800997 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500998 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800999 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001000
Florin Coras5f33d0d2021-06-02 21:22:21 -07001001 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001002
1003 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1004 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1005 {
Yu Ping7b74b072019-05-08 00:40:24 +08001006 u8 proto;
1007 if (ldp->transparent_tls)
1008 {
1009 proto = VPPCOM_PROTO_TLS;
1010 }
1011 else
1012 proto = ((sock_type == SOCK_DGRAM) ?
1013 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -05001014
Florin Coras7baeb712019-01-04 17:05:43 -08001015 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
1016 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -05001017
Florin Coras7baeb712019-01-04 17:05:43 -08001018 vlsh = vls_create (proto, is_nonblocking);
1019 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001020 {
Florin Coras7baeb712019-01-04 17:05:43 -08001021 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001022 rv = -1;
1023 }
1024 else
1025 {
Yu Ping7b74b072019-05-08 00:40:24 +08001026 if (ldp->transparent_tls)
1027 {
Florin Corasa5a9efd2021-01-05 17:03:29 -08001028 if (assign_cert_key_pair (vlsh) < 0)
1029 return -1;
Yu Ping7b74b072019-05-08 00:40:24 +08001030 }
Florin Coras7baeb712019-01-04 17:05:43 -08001031 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05001032 }
1033 }
1034 else
1035 {
Florin Coras7baeb712019-01-04 17:05:43 -08001036 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -05001037 rv = libc_socket (domain, type, protocol);
1038 }
1039
Dave Wallace048b1d62018-01-03 22:24:41 -05001040 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001041}
1042
1043/*
1044 * Create two new sockets, of type TYPE in domain DOMAIN and using
1045 * protocol PROTOCOL, which are connected to each other, and put file
1046 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1047 * one will be chosen automatically.
1048 * Returns 0 on success, -1 for errors.
1049 * */
1050int
Dave Wallace048b1d62018-01-03 22:24:41 -05001051socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001052{
Florin Coras7baeb712019-01-04 17:05:43 -08001053 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001054
Florin Coras5f33d0d2021-06-02 21:22:21 -07001055 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001056
1057 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1058 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001059 {
Florin Coras7baeb712019-01-04 17:05:43 -08001060 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001061 errno = ENOSYS;
1062 rv = -1;
1063 }
1064 else
1065 {
Florin Coras7baeb712019-01-04 17:05:43 -08001066 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001067 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001068 }
1069
Dave Wallace048b1d62018-01-03 22:24:41 -05001070 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001071}
1072
1073int
Florin Coras36847942023-02-02 12:56:16 -08001074bind (int fd, __CONST_SOCKADDR_ARG _addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001075{
Florin Coras36847942023-02-02 12:56:16 -08001076 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001077 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001078 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001079
Florin Coras5f33d0d2021-06-02 21:22:21 -07001080 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001081
Florin Coras7baeb712019-01-04 17:05:43 -08001082 vlsh = ldp_fd_to_vlsh (fd);
1083 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001084 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001085 vppcom_endpt_t ep;
1086
Dave Wallace048b1d62018-01-03 22:24:41 -05001087 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001088 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001089 case AF_INET:
1090 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001091 {
Florin Coras7baeb712019-01-04 17:05:43 -08001092 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1093 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001094 errno = EINVAL;
1095 rv = -1;
1096 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001097 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001098 ep.is_ip4 = VPPCOM_IS_IP4;
1099 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1100 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1101 break;
1102
1103 case AF_INET6:
1104 if (len != sizeof (struct sockaddr_in6))
1105 {
Florin Coras7baeb712019-01-04 17:05:43 -08001106 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1107 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001108 errno = EINVAL;
1109 rv = -1;
1110 goto done;
1111 }
1112 ep.is_ip4 = VPPCOM_IS_IP6;
1113 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1114 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001115 break;
1116
1117 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001118 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1119 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001120 errno = EAFNOSUPPORT;
1121 rv = -1;
1122 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001123 }
Florin Coras7baeb712019-01-04 17:05:43 -08001124 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1125 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001126
Florin Coras7baeb712019-01-04 17:05:43 -08001127 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001128 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001129 {
1130 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001131 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001132 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001133 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001134 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001135 {
Florin Coras7baeb712019-01-04 17:05:43 -08001136 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001137 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001138 }
1139
Dave Wallace048b1d62018-01-03 22:24:41 -05001140done:
Florin Coras7baeb712019-01-04 17:05:43 -08001141 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001142
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001143 return rv;
1144}
1145
1146static inline int
Florin Coras36847942023-02-02 12:56:16 -08001147ldp_copy_ep_to_sockaddr (struct sockaddr *addr, socklen_t *__restrict len,
1148 vppcom_endpt_t *ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001149{
Florin Coras36847942023-02-02 12:56:16 -08001150 int rv = 0, sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001151
Florin Coras5f33d0d2021-06-02 21:22:21 -07001152 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001153
1154 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001155 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001156 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1157 switch (addr->sa_family)
1158 {
1159 case AF_INET:
1160 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1161 if (*len > sizeof (struct sockaddr_in))
1162 *len = sizeof (struct sockaddr_in);
1163 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1164 copy_len = *len - sa_len;
1165 if (copy_len > 0)
1166 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1167 copy_len);
1168 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001169
Dave Wallace048b1d62018-01-03 22:24:41 -05001170 case AF_INET6:
1171 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1172 if (*len > sizeof (struct sockaddr_in6))
1173 *len = sizeof (struct sockaddr_in6);
1174 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1175 copy_len = *len - sa_len;
1176 if (copy_len > 0)
1177 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1178 __in6_u.__u6_addr8, ep->ip, copy_len);
1179 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001180
Dave Wallace048b1d62018-01-03 22:24:41 -05001181 default:
1182 /* Not possible */
1183 rv = -EAFNOSUPPORT;
1184 break;
1185 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001186 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001187 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001188}
1189
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001190int
Florin Coras36847942023-02-02 12:56:16 -08001191getsockname (int fd, __SOCKADDR_ARG _addr, socklen_t *__restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001192{
Florin Coras36847942023-02-02 12:56:16 -08001193 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001194 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001195 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001196
Florin Coras5f33d0d2021-06-02 21:22:21 -07001197 ldp_init_check ();
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001198
Florin Coras7baeb712019-01-04 17:05:43 -08001199 vlsh = ldp_fd_to_vlsh (fd);
1200 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001201 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001202 vppcom_endpt_t ep;
1203 u8 addr_buf[sizeof (struct in6_addr)];
1204 u32 size = sizeof (ep);
1205
1206 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001207
Florin Coras7baeb712019-01-04 17:05:43 -08001208 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001209 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001210 {
1211 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001212 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001213 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001214 else
1215 {
Dave Wallace2a865272018-02-07 21:00:42 -05001216 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001217 if (rv != VPPCOM_OK)
1218 {
1219 errno = -rv;
1220 rv = -1;
1221 }
1222 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001223 }
1224 else
1225 {
Florin Coras36847942023-02-02 12:56:16 -08001226 rv = libc_getsockname (fd, _addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001227 }
1228
Dave Wallace048b1d62018-01-03 22:24:41 -05001229 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001230}
1231
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001232int
Florin Coras36847942023-02-02 12:56:16 -08001233connect (int fd, __CONST_SOCKADDR_ARG _addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001234{
Florin Coras36847942023-02-02 12:56:16 -08001235 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001236 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001237 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001238
Florin Coras5f33d0d2021-06-02 21:22:21 -07001239 ldp_init_check ();
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001240
Dave Wallace048b1d62018-01-03 22:24:41 -05001241 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001242 {
Florin Coras7baeb712019-01-04 17:05:43 -08001243 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001244 errno = EINVAL;
1245 rv = -1;
1246 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001247 }
1248
Florin Coras7baeb712019-01-04 17:05:43 -08001249 vlsh = ldp_fd_to_vlsh (fd);
1250 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001251 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001252 vppcom_endpt_t ep;
1253
Dave Wallace048b1d62018-01-03 22:24:41 -05001254 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001255 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001256 case AF_INET:
1257 if (len != sizeof (struct sockaddr_in))
1258 {
Florin Coras7baeb712019-01-04 17:05:43 -08001259 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1260 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001261 errno = EINVAL;
1262 rv = -1;
1263 goto done;
1264 }
1265 ep.is_ip4 = VPPCOM_IS_IP4;
1266 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1267 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1268 break;
1269
1270 case AF_INET6:
1271 if (len != sizeof (struct sockaddr_in6))
1272 {
Florin Coras7baeb712019-01-04 17:05:43 -08001273 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1274 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001275 errno = EINVAL;
1276 rv = -1;
1277 goto done;
1278 }
1279 ep.is_ip4 = VPPCOM_IS_IP6;
1280 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1281 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1282 break;
1283
1284 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001285 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1286 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001287 errno = EAFNOSUPPORT;
1288 rv = -1;
1289 goto done;
1290 }
Florin Coras7baeb712019-01-04 17:05:43 -08001291 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1292 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001293
Florin Coras7baeb712019-01-04 17:05:43 -08001294 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001295 if (rv != VPPCOM_OK)
1296 {
1297 errno = -rv;
1298 rv = -1;
1299 }
1300 }
1301 else
1302 {
Florin Coras7baeb712019-01-04 17:05:43 -08001303 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1304 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001305
1306 rv = libc_connect (fd, addr, len);
1307 }
1308
1309done:
Florin Coras7baeb712019-01-04 17:05:43 -08001310 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001311 return rv;
1312}
1313
1314int
Florin Coras36847942023-02-02 12:56:16 -08001315getpeername (int fd, __SOCKADDR_ARG _addr, socklen_t *__restrict len)
Dave Wallace048b1d62018-01-03 22:24:41 -05001316{
Florin Coras36847942023-02-02 12:56:16 -08001317 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001318 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001319 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001320
Florin Coras5f33d0d2021-06-02 21:22:21 -07001321 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001322
Florin Coras7baeb712019-01-04 17:05:43 -08001323 vlsh = ldp_fd_to_vlsh (fd);
1324 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 {
1326 vppcom_endpt_t ep;
1327 u8 addr_buf[sizeof (struct in6_addr)];
1328 u32 size = sizeof (ep);
1329
1330 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001331 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001332 if (rv != VPPCOM_OK)
1333 {
1334 errno = -rv;
1335 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001336 }
1337 else
1338 {
Dave Wallace2a865272018-02-07 21:00:42 -05001339 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001340 if (rv != VPPCOM_OK)
1341 {
1342 errno = -rv;
1343 rv = -1;
1344 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001345 }
1346 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001347 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001348 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001349 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001350 }
1351
Dave Wallace048b1d62018-01-03 22:24:41 -05001352 return rv;
1353}
1354
1355ssize_t
1356send (int fd, const void *buf, size_t n, int flags)
1357{
Florin Coras7baeb712019-01-04 17:05:43 -08001358 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001359 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001360
Florin Coras5f33d0d2021-06-02 21:22:21 -07001361 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001362
Florin Coras7baeb712019-01-04 17:05:43 -08001363 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001364 {
Florin Coras7baeb712019-01-04 17:05:43 -08001365 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001366 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001367 {
1368 errno = -size;
1369 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001370 }
1371 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001372 else
1373 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001374 size = libc_send (fd, buf, n, flags);
1375 }
1376
Dave Wallace048b1d62018-01-03 22:24:41 -05001377 return size;
1378}
1379
1380ssize_t
1381sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1382{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001383 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001384 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001385 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001386
Florin Coras5f33d0d2021-06-02 21:22:21 -07001387 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001388
Florin Coras7baeb712019-01-04 17:05:43 -08001389 vlsh = ldp_fd_to_vlsh (out_fd);
1390 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001391 {
1392 int rv;
1393 ssize_t results = 0;
1394 size_t n_bytes_left = len;
1395 size_t bytes_to_read;
1396 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001397 u8 eagain = 0;
1398 u32 flags, flags_len = sizeof (flags);
1399
Florin Coras7baeb712019-01-04 17:05:43 -08001400 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001401 if (PREDICT_FALSE (rv != VPPCOM_OK))
1402 {
Florin Coras7baeb712019-01-04 17:05:43 -08001403 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1404 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001405
Florin Corasdfe4cf42018-11-28 22:13:45 -08001406 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001407 errno = -rv;
1408 size = -1;
1409 goto done;
1410 }
1411
1412 if (offset)
1413 {
1414 off_t off = lseek (in_fd, *offset, SEEK_SET);
1415 if (PREDICT_FALSE (off == -1))
1416 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001417 size = -1;
1418 goto done;
1419 }
1420
1421 ASSERT (off == *offset);
1422 }
1423
1424 do
1425 {
Florin Coras7baeb712019-01-04 17:05:43 -08001426 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001427 if (size < 0)
1428 {
Florin Coraseb801d02020-09-16 17:44:58 -07001429 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!",
Florin Coras7baeb712019-01-04 17:05:43 -08001430 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001431 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001432 errno = -size;
1433 size = -1;
1434 goto done;
1435 }
1436
1437 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001438 if (bytes_to_read == 0)
1439 {
1440 if (flags & O_NONBLOCK)
1441 {
1442 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001443 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001444 goto update_offset;
1445 }
1446 else
1447 continue;
1448 }
1449 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001450 vec_validate (ldpw->io_buffer, bytes_to_read);
1451 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001452 if (nbytes < 0)
1453 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001454 if (results == 0)
1455 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001456 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001457 size = -1;
1458 goto done;
1459 }
1460 goto update_offset;
1461 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001462
Florin Coras7baeb712019-01-04 17:05:43 -08001463 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001464 if (size < 0)
1465 {
1466 if (size == VPPCOM_EAGAIN)
1467 {
1468 if (flags & O_NONBLOCK)
1469 {
1470 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001471 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001472 goto update_offset;
1473 }
1474 else
1475 continue;
1476 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001477 if (results == 0)
1478 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001479 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001480 errno = -size;
1481 size = -1;
1482 goto done;
1483 }
1484 goto update_offset;
1485 }
1486
1487 results += nbytes;
1488 ASSERT (n_bytes_left >= nbytes);
1489 n_bytes_left = n_bytes_left - nbytes;
1490 }
1491 while (n_bytes_left > 0);
1492
1493 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001494 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001495 if (offset)
1496 {
1497 off_t off = lseek (in_fd, *offset, SEEK_SET);
1498 if (PREDICT_FALSE (off == -1))
1499 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001500 size = -1;
1501 goto done;
1502 }
1503
1504 ASSERT (off == *offset);
1505 *offset += results + 1;
1506 }
1507 if (eagain)
1508 {
1509 errno = EAGAIN;
1510 size = -1;
1511 }
1512 else
1513 size = results;
1514 }
1515 else
1516 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001517 size = libc_sendfile (out_fd, in_fd, offset, len);
1518 }
1519
1520done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001521 return size;
1522}
1523
1524ssize_t
1525sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1526{
1527 return sendfile (out_fd, in_fd, offset, len);
1528}
1529
1530ssize_t
1531recv (int fd, void *buf, size_t n, int flags)
1532{
Florin Coras7baeb712019-01-04 17:05:43 -08001533 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001534 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001535
Florin Coras5f33d0d2021-06-02 21:22:21 -07001536 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001537
Florin Coras7baeb712019-01-04 17:05:43 -08001538 vlsh = ldp_fd_to_vlsh (fd);
1539 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001540 {
Florin Coras7baeb712019-01-04 17:05:43 -08001541 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001542 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001543 {
1544 errno = -size;
1545 size = -1;
1546 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001547 }
1548 else
1549 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001550 size = libc_recv (fd, buf, n, flags);
1551 }
1552
Dave Wallace048b1d62018-01-03 22:24:41 -05001553 return size;
1554}
1555
Sivaprasad Tummalafdcbd382021-07-31 21:38:19 +05301556ssize_t
1557__recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags)
1558{
1559 if (n > buflen)
1560 return -1;
1561
1562 return recv (fd, buf, n, flags);
1563}
1564
Dou Chao243a0432022-11-29 19:41:34 +08001565static inline int
1566ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n,
Florin Coraseff5f7a2023-02-07 17:36:17 -08001567 vppcom_endpt_tlv_t *app_tlvs, int flags,
Florin Coras36847942023-02-02 12:56:16 -08001568 __CONST_SOCKADDR_ARG _addr, socklen_t addr_len)
Florin Corasce17f462020-05-22 20:36:29 +00001569{
Florin Coras36847942023-02-02 12:56:16 -08001570 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Corasce17f462020-05-22 20:36:29 +00001571 vppcom_endpt_t *ep = 0;
1572 vppcom_endpt_t _ep;
1573
Florin Coraseff5f7a2023-02-07 17:36:17 -08001574 _ep.app_tlvs = app_tlvs;
Dou Chao243a0432022-11-29 19:41:34 +08001575
Florin Corasce17f462020-05-22 20:36:29 +00001576 if (addr)
1577 {
1578 ep = &_ep;
1579 switch (addr->sa_family)
1580 {
1581 case AF_INET:
1582 ep->is_ip4 = VPPCOM_IS_IP4;
1583 ep->ip =
1584 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1585 ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1586 break;
1587
1588 case AF_INET6:
1589 ep->is_ip4 = VPPCOM_IS_IP6;
1590 ep->ip =
1591 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1592 ep->port =
1593 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1594 break;
1595
1596 default:
1597 return EAFNOSUPPORT;
1598 }
1599 }
1600
1601 return vls_sendto (vlsh, (void *) buf, n, flags, ep);
1602}
1603
1604static int
Florin Coras36847942023-02-02 12:56:16 -08001605ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n, int flags,
1606 __SOCKADDR_ARG _addr, socklen_t *__restrict addr_len)
Florin Corasce17f462020-05-22 20:36:29 +00001607{
1608 u8 src_addr[sizeof (struct sockaddr_in6)];
Florin Coras36847942023-02-02 12:56:16 -08001609 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Corasce17f462020-05-22 20:36:29 +00001610 vppcom_endpt_t ep;
1611 ssize_t size;
1612 int rv;
1613
1614 if (addr)
1615 {
1616 ep.ip = src_addr;
1617 size = vls_recvfrom (vlsh, buf, n, flags, &ep);
1618
1619 if (size > 0)
1620 {
1621 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1622 if (rv < 0)
1623 size = rv;
1624 }
1625 }
1626 else
1627 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
1628
1629 return size;
1630}
1631
Dave Wallace048b1d62018-01-03 22:24:41 -05001632ssize_t
1633sendto (int fd, const void *buf, size_t n, int flags,
Florin Coras36847942023-02-02 12:56:16 -08001634 __CONST_SOCKADDR_ARG _addr, socklen_t addr_len)
Dave Wallace048b1d62018-01-03 22:24:41 -05001635{
Florin Coras36847942023-02-02 12:56:16 -08001636 const struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08001637 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001638 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001639
Florin Coras5f33d0d2021-06-02 21:22:21 -07001640 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001641
Florin Coras7baeb712019-01-04 17:05:43 -08001642 vlsh = ldp_fd_to_vlsh (fd);
wanghanlin97c6e0d2021-07-06 15:01:48 +08001643 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001644 {
Dou Chao243a0432022-11-29 19:41:34 +08001645 size = ldp_vls_sendo (vlsh, buf, n, NULL, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001646 if (size < 0)
1647 {
1648 errno = -size;
1649 size = -1;
1650 }
1651 }
1652 else
1653 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001654 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1655 }
1656
Dave Wallace048b1d62018-01-03 22:24:41 -05001657 return size;
1658}
1659
1660ssize_t
1661recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1662 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1663{
Florin Corasce17f462020-05-22 20:36:29 +00001664 vls_handle_t vlsh;
1665 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001666
Florin Coras5f33d0d2021-06-02 21:22:21 -07001667 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001668
Florin Corasce17f462020-05-22 20:36:29 +00001669 vlsh = ldp_fd_to_vlsh (fd);
1670 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001671 {
Florin Corasce17f462020-05-22 20:36:29 +00001672 size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001673 if (size < 0)
1674 {
1675 errno = -size;
1676 size = -1;
1677 }
1678 }
1679 else
1680 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001681 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1682 }
1683
Dave Wallace048b1d62018-01-03 22:24:41 -05001684 return size;
1685}
1686
Florin Coraseff5f7a2023-02-07 17:36:17 -08001687static int
1688ldp_parse_cmsg (vls_handle_t vlsh, const struct msghdr *msg,
1689 vppcom_endpt_tlv_t **app_tlvs)
1690{
1691 uint8_t *ad, *at = (uint8_t *) *app_tlvs;
1692 vppcom_endpt_tlv_t *adh;
1693 struct in_pktinfo *pi;
1694 struct cmsghdr *cmsg;
1695
1696 cmsg = CMSG_FIRSTHDR (msg);
1697
1698 while (cmsg != NULL)
1699 {
1700 switch (cmsg->cmsg_level)
1701 {
1702 case SOL_UDP:
1703 switch (cmsg->cmsg_type)
1704 {
1705 case UDP_SEGMENT:
1706 vec_add2 (at, adh, sizeof (*adh));
1707 adh->data_type = VCL_UDP_SEGMENT;
1708 adh->data_len = sizeof (uint16_t);
1709 vec_add2 (at, ad, sizeof (uint16_t));
1710 *(uint16_t *) ad = *(uint16_t *) CMSG_DATA (cmsg);
1711 break;
1712 default:
1713 LDBG (1, "SOL_UDP cmsg_type %u not supported", cmsg->cmsg_type);
1714 break;
1715 }
1716 break;
1717 case SOL_IP:
1718 switch (cmsg->cmsg_type)
1719 {
1720 case IP_PKTINFO:
1721 vec_add2 (at, adh, sizeof (*adh));
1722 adh->data_type = VCL_IP_PKTINFO;
1723 adh->data_len = sizeof (struct in_addr);
1724 vec_add2 (at, ad, sizeof (struct in_addr));
1725 pi = (void *) CMSG_DATA (cmsg);
1726 clib_memcpy_fast (ad, &pi->ipi_spec_dst,
1727 sizeof (struct in_addr));
1728 break;
1729 default:
1730 LDBG (1, "SOL_IP cmsg_type %u not supported", cmsg->cmsg_type);
1731 break;
1732 }
1733 break;
1734 default:
1735 LDBG (1, "cmsg_level %u not supported", cmsg->cmsg_level);
1736 break;
1737 }
1738 cmsg = CMSG_NXTHDR ((struct msghdr *) msg, cmsg);
1739 }
1740 *app_tlvs = (vppcom_endpt_tlv_t *) at;
1741 return 0;
1742}
1743
1744static int
1745ldp_make_cmsg (vls_handle_t vlsh, struct msghdr *msg)
1746{
1747 u32 optval, optlen = sizeof (optval);
1748 struct cmsghdr *cmsg;
1749
1750 cmsg = CMSG_FIRSTHDR (msg);
1751
1752 if (!vls_attr (vlsh, VPPCOM_ATTR_GET_IP_PKTINFO, (void *) &optval, &optlen))
1753 return 0;
1754
1755 if (optval)
1756 {
1757 vppcom_endpt_t ep;
1758 u8 addr_buf[sizeof (struct in_addr)];
1759 u32 size = sizeof (ep);
1760
1761 ep.ip = addr_buf;
1762
1763 if (!vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size))
1764 {
1765 struct in_pktinfo pi = {};
1766
1767 clib_memcpy (&pi.ipi_addr, ep.ip, sizeof (struct in_addr));
1768 cmsg->cmsg_level = SOL_IP;
1769 cmsg->cmsg_type = IP_PKTINFO;
1770 cmsg->cmsg_len = CMSG_LEN (sizeof (pi));
1771 clib_memcpy (CMSG_DATA (cmsg), &pi, sizeof (pi));
1772 }
1773 }
1774
1775 return 0;
1776}
1777
Dave Wallace048b1d62018-01-03 22:24:41 -05001778ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001779sendmsg (int fd, const struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001780{
Florin Coras7baeb712019-01-04 17:05:43 -08001781 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001782 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001783
Florin Coras5f33d0d2021-06-02 21:22:21 -07001784 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001785
Florin Coras7baeb712019-01-04 17:05:43 -08001786 vlsh = ldp_fd_to_vlsh (fd);
1787 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001788 {
Florin Coraseff5f7a2023-02-07 17:36:17 -08001789 vppcom_endpt_tlv_t *app_tlvs = 0;
Florin Corasce17f462020-05-22 20:36:29 +00001790 struct iovec *iov = msg->msg_iov;
1791 ssize_t total = 0;
Florin Corascba1c222021-11-29 08:12:27 -08001792 int i, rv = 0;
Dou Chao243a0432022-11-29 19:41:34 +08001793
Florin Coraseff5f7a2023-02-07 17:36:17 -08001794 ldp_parse_cmsg (vlsh, msg, &app_tlvs);
Florin Corasce17f462020-05-22 20:36:29 +00001795
1796 for (i = 0; i < msg->msg_iovlen; ++i)
1797 {
Florin Coraseff5f7a2023-02-07 17:36:17 -08001798 rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, app_tlvs,
1799 flags, msg->msg_name, msg->msg_namelen);
Florin Corasce17f462020-05-22 20:36:29 +00001800 if (rv < 0)
1801 break;
1802 else
1803 {
1804 total += rv;
1805 if (rv < iov[i].iov_len)
1806 break;
1807 }
1808 }
1809
Florin Coraseff5f7a2023-02-07 17:36:17 -08001810 vec_free (app_tlvs);
1811
Florin Corasce17f462020-05-22 20:36:29 +00001812 if (rv < 0 && total == 0)
1813 {
1814 errno = -rv;
1815 size = -1;
1816 }
1817 else
1818 size = total;
Dave Wallace048b1d62018-01-03 22:24:41 -05001819 }
1820 else
1821 {
Florin Corasce17f462020-05-22 20:36:29 +00001822 size = libc_sendmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001823 }
1824
Dave Wallace048b1d62018-01-03 22:24:41 -05001825 return size;
1826}
1827
Florin Coras36847942023-02-02 12:56:16 -08001828#ifdef _GNU_SOURCE
Dave Wallace048b1d62018-01-03 22:24:41 -05001829int
1830sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1831{
1832 ssize_t size;
1833 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001834 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001835
Florin Coras5f33d0d2021-06-02 21:22:21 -07001836 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001837
wanghanlin97c6e0d2021-07-06 15:01:48 +08001838 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001839 {
1840 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1841 errno = ENOSYS;
1842 size = -1;
1843 }
1844 else
1845 {
1846 func_str = "libc_sendmmsg";
1847
Dave Wallace2a865272018-02-07 21:00:42 -05001848 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001849 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1850 "vmessages %p, vlen %u, flags 0x%x",
1851 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1852
1853 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1854 }
1855
Dave Wallace2a865272018-02-07 21:00:42 -05001856 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001857 {
1858 if (size < 0)
1859 {
1860 int errno_val = errno;
1861 perror (func_str);
1862 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1863 "rv %d, errno = %d", getpid (), fd, fd,
1864 func_str, size, errno_val);
1865 errno = errno_val;
1866 }
1867 else
1868 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1869 getpid (), fd, fd, size, size);
1870 }
1871 return size;
1872}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001873#endif
1874
Dave Wallace048b1d62018-01-03 22:24:41 -05001875ssize_t
Florin Corasce17f462020-05-22 20:36:29 +00001876recvmsg (int fd, struct msghdr * msg, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05001877{
Florin Coras7baeb712019-01-04 17:05:43 -08001878 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001879 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001880
Florin Coras5f33d0d2021-06-02 21:22:21 -07001881 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001882
Florin Coras7baeb712019-01-04 17:05:43 -08001883 vlsh = ldp_fd_to_vlsh (fd);
1884 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001885 {
Florin Corasce17f462020-05-22 20:36:29 +00001886 struct iovec *iov = msg->msg_iov;
1887 ssize_t max_deq, total = 0;
1888 int i, rv;
1889
1890 max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
1891 if (!max_deq)
1892 return 0;
1893
1894 for (i = 0; i < msg->msg_iovlen; i++)
1895 {
1896 rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags,
1897 (i == 0 ? msg->msg_name : NULL),
1898 (i == 0 ? &msg->msg_namelen : NULL));
1899 if (rv <= 0)
1900 break;
1901 else
1902 {
1903 total += rv;
1904 if (rv < iov[i].iov_len)
1905 break;
1906 }
1907 if (total >= max_deq)
1908 break;
1909 }
1910
1911 if (rv < 0 && total == 0)
1912 {
1913 errno = -rv;
1914 size = -1;
1915 }
1916 else
Florin Coraseff5f7a2023-02-07 17:36:17 -08001917 {
1918 if (msg->msg_controllen)
1919 ldp_make_cmsg (vlsh, msg);
1920 size = total;
1921 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001922 }
1923 else
1924 {
Florin Corasce17f462020-05-22 20:36:29 +00001925 size = libc_recvmsg (fd, msg, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001926 }
1927
Dave Wallace048b1d62018-01-03 22:24:41 -05001928 return size;
1929}
1930
Florin Coras36847942023-02-02 12:56:16 -08001931#ifdef _GNU_SOURCE
Dave Wallace048b1d62018-01-03 22:24:41 -05001932int
1933recvmmsg (int fd, struct mmsghdr *vmessages,
1934 unsigned int vlen, int flags, struct timespec *tmo)
1935{
Florin Corasf1a232f2023-02-02 22:56:03 -08001936 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
1937 u32 sh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001938
Florin Coras5f33d0d2021-06-02 21:22:21 -07001939 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001940
Florin Corasf1a232f2023-02-02 22:56:03 -08001941 sh = ldp_fd_to_vlsh (fd);
1942
wanghanlin97c6e0d2021-07-06 15:01:48 +08001943 if (sh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001944 {
Florin Corasf1a232f2023-02-02 22:56:03 -08001945 struct mmsghdr *mh;
1946 ssize_t rv = 0;
1947 u32 nvecs = 0;
1948 f64 time_out;
1949
1950 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
1951 clib_time_init (&ldpw->clib_time);
1952 if (tmo)
1953 {
1954 time_out = (f64) tmo->tv_sec + (f64) tmo->tv_nsec / (f64) 1e9;
1955 time_out += clib_time_now (&ldpw->clib_time);
1956 }
1957 else
1958 {
1959 time_out = (f64) ~0;
1960 }
1961
1962 while (nvecs < vlen)
1963 {
1964 mh = &vmessages[nvecs];
1965 rv = recvmsg (fd, &mh->msg_hdr, flags);
1966 if (rv > 0)
1967 {
1968 mh->msg_len = rv;
1969 nvecs += 1;
1970 continue;
1971 }
1972
1973 if (!time_out || clib_time_now (&ldpw->clib_time) >= time_out)
1974 break;
1975
1976 usleep (1);
1977 }
1978
1979 return nvecs > 0 ? nvecs : rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001980 }
1981 else
1982 {
Florin Corasf1a232f2023-02-02 22:56:03 -08001983 return libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
Dave Wallace048b1d62018-01-03 22:24:41 -05001984 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001985}
1986#endif
1987
1988int
1989getsockopt (int fd, int level, int optname,
1990 void *__restrict optval, socklen_t * __restrict optlen)
1991{
Florin Coras7baeb712019-01-04 17:05:43 -08001992 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001993 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001994
Florin Coras5f33d0d2021-06-02 21:22:21 -07001995 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001996
Florin Coras7baeb712019-01-04 17:05:43 -08001997 vlsh = ldp_fd_to_vlsh (fd);
1998 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001999 {
2000 rv = -EOPNOTSUPP;
2001
2002 switch (level)
2003 {
2004 case SOL_TCP:
2005 switch (optname)
2006 {
2007 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002008 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
2009 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002010 break;
2011 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002012 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
2013 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002014 break;
2015 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002016 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2017 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002018 break;
2019 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002020 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2021 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002022 break;
2023 case TCP_INFO:
2024 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2025 {
Florin Coras7baeb712019-01-04 17:05:43 -08002026 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
2027 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002028 memset (optval, 0, *optlen);
2029 rv = VPPCOM_OK;
2030 }
2031 else
2032 rv = -EFAULT;
2033 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002034 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08002035 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04002036 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08002037 rv = 0;
2038 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002039 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002040 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
2041 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002042 break;
2043 }
2044 break;
2045 case SOL_IPV6:
2046 switch (optname)
2047 {
2048 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002049 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002050 break;
2051 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002052 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
2053 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002054 break;
2055 }
2056 break;
2057 case SOL_SOCKET:
2058 switch (optname)
2059 {
2060 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08002061 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002062 break;
2063 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002064 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002065 break;
2066 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08002067 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002068 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2069 break;
2070 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08002071 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2072 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002073 break;
2074 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08002075 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2076 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002077 break;
2078 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002079 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002080 break;
wanghanlin0674f852021-02-22 10:38:36 +08002081 case SO_REUSEPORT:
2082 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen);
2083 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002084 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002085 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002086 break;
wanghanlin0674f852021-02-22 10:38:36 +08002087 case SO_DOMAIN:
2088 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen);
2089 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002090 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08002091 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002092 break;
Florin Coras8eb8d502021-06-16 14:46:57 -07002093 case SO_BINDTODEVICE:
2094 rv = 0;
2095 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002096 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002097 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
2098 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002099 break;
2100 }
2101 break;
2102 default:
2103 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002104 }
2105
Dave Wallace048b1d62018-01-03 22:24:41 -05002106 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002107 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 errno = -rv;
2109 rv = -1;
2110 }
2111 }
2112 else
2113 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002114 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2115 }
2116
Dave Wallace048b1d62018-01-03 22:24:41 -05002117 return rv;
2118}
2119
2120int
2121setsockopt (int fd, int level, int optname,
2122 const void *optval, socklen_t optlen)
2123{
Florin Coras7baeb712019-01-04 17:05:43 -08002124 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002126
Florin Coras5f33d0d2021-06-02 21:22:21 -07002127 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002128
Florin Coras7baeb712019-01-04 17:05:43 -08002129 vlsh = ldp_fd_to_vlsh (fd);
2130 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002131 {
2132 rv = -EOPNOTSUPP;
2133
2134 switch (level)
2135 {
2136 case SOL_TCP:
2137 switch (optname)
2138 {
2139 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08002140 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
2141 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002142 break;
2143 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08002144 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
2145 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002146 break;
2147 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08002148 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2149 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002150 break;
2151 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08002152 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2153 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002154 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08002155 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07002156 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08002157 /* Ignore */
2158 rv = 0;
2159 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002160 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002161 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
2162 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002163 break;
2164 }
2165 break;
2166 case SOL_IPV6:
2167 switch (optname)
2168 {
2169 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08002170 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
2171 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002172 break;
2173 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002174 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
2175 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002176 break;
2177 }
2178 break;
2179 case SOL_SOCKET:
2180 switch (optname)
2181 {
2182 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08002183 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
2184 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002185 break;
2186 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08002187 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
2188 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002189 break;
wanghanlin0674f852021-02-22 10:38:36 +08002190 case SO_REUSEPORT:
2191 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval,
2192 &optlen);
2193 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002194 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08002195 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
2196 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002197 break;
Florin Coras2f647902021-06-02 08:23:50 -07002198 case SO_LINGER:
2199 rv = 0;
2200 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002201 default:
Florin Coras7baeb712019-01-04 17:05:43 -08002202 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
2203 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05002204 break;
2205 }
2206 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08002207 case SOL_IP:
2208 switch (optname)
2209 {
2210 case IP_PKTINFO:
2211 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_IP_PKTINFO, (void *) optval,
2212 &optlen);
2213 break;
2214 default:
2215 LDBG (0,
2216 "ERROR: fd %d: setsockopt SOL_IP: vlsh %u optname %d"
2217 "unsupported!",
2218 fd, vlsh, optname);
2219 break;
2220 }
2221 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05002222 default:
2223 break;
2224 }
2225
2226 if (rv != VPPCOM_OK)
2227 {
2228 errno = -rv;
2229 rv = -1;
2230 }
2231 }
2232 else
2233 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002234 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2235 }
2236
Dave Wallace048b1d62018-01-03 22:24:41 -05002237 return rv;
2238}
2239
2240int
2241listen (int fd, int n)
2242{
Florin Coras7baeb712019-01-04 17:05:43 -08002243 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002244 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002245
Florin Coras5f33d0d2021-06-02 21:22:21 -07002246 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002247
Florin Coras7baeb712019-01-04 17:05:43 -08002248 vlsh = ldp_fd_to_vlsh (fd);
2249 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002250 {
Florin Coras7baeb712019-01-04 17:05:43 -08002251 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002252
Florin Coras7baeb712019-01-04 17:05:43 -08002253 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002254 if (rv != VPPCOM_OK)
2255 {
2256 errno = -rv;
2257 rv = -1;
2258 }
2259 }
2260 else
2261 {
Florin Coras7baeb712019-01-04 17:05:43 -08002262 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002263 rv = libc_listen (fd, n);
2264 }
2265
Florin Coras7baeb712019-01-04 17:05:43 -08002266 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002267 return rv;
2268}
2269
2270static inline int
Florin Coras36847942023-02-02 12:56:16 -08002271ldp_accept4 (int listen_fd, __SOCKADDR_ARG _addr,
2272 socklen_t *__restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002273{
Florin Coras36847942023-02-02 12:56:16 -08002274 struct sockaddr *addr = SOCKADDR_GET_SA (_addr);
Florin Coras7baeb712019-01-04 17:05:43 -08002275 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002276 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002277
Florin Coras5f33d0d2021-06-02 21:22:21 -07002278 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002279
Florin Coras7baeb712019-01-04 17:05:43 -08002280 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2281 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002282 {
2283 vppcom_endpt_t ep;
2284 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002285 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002286 ep.ip = src_addr;
2287
Florin Coras7baeb712019-01-04 17:05:43 -08002288 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
Florin Coraseb801d02020-09-16 17:44:58 -07002289 " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002290
Florin Coras7baeb712019-01-04 17:05:43 -08002291 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2292 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002293 {
Florin Coras7baeb712019-01-04 17:05:43 -08002294 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002295 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002296 }
2297 else
2298 {
Dave Wallace2a865272018-02-07 21:00:42 -05002299 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002300 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002301 {
Florin Coras7baeb712019-01-04 17:05:43 -08002302 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002303 errno = -rv;
2304 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002305 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002306 else
2307 {
Florin Coras7baeb712019-01-04 17:05:43 -08002308 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002309 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002310 }
2311 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002312 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002313 {
Florin Coras7baeb712019-01-04 17:05:43 -08002314 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2315 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002316
Dave Wallace048b1d62018-01-03 22:24:41 -05002317 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002318 }
2319
Florin Coras7baeb712019-01-04 17:05:43 -08002320 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002321
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002322 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002323}
2324
Dave Wallace048b1d62018-01-03 22:24:41 -05002325int
2326accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2327 int flags)
2328{
Dave Wallace2a865272018-02-07 21:00:42 -05002329 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002330}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002331
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002332int
Dave Wallace048b1d62018-01-03 22:24:41 -05002333accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002334{
Dave Wallace2a865272018-02-07 21:00:42 -05002335 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002336}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002337
Dave Wallace048b1d62018-01-03 22:24:41 -05002338int
2339shutdown (int fd, int how)
2340{
Florin Coras7baeb712019-01-04 17:05:43 -08002341 vls_handle_t vlsh;
liuyacan55c952e2021-06-13 14:54:55 +08002342 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002343
Florin Coras5f33d0d2021-06-02 21:22:21 -07002344 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002345
Florin Coras7baeb712019-01-04 17:05:43 -08002346 vlsh = ldp_fd_to_vlsh (fd);
2347 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002348 {
Florin Coras7baeb712019-01-04 17:05:43 -08002349 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
liuyacan55c952e2021-06-13 14:54:55 +08002350 rv = vls_shutdown (vlsh, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002351 }
2352 else
2353 {
Florin Coras7baeb712019-01-04 17:05:43 -08002354 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002355 rv = libc_shutdown (fd, how);
2356 }
2357
Dave Wallace048b1d62018-01-03 22:24:41 -05002358 return rv;
2359}
2360
2361int
2362epoll_create1 (int flags)
2363{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002364 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002365 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002366 int rv;
2367
Florin Coras5f33d0d2021-06-02 21:22:21 -07002368 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002369
hanlina3a48962020-07-13 11:09:15 +08002370 if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ())
Florin Coras99368312018-08-02 10:45:44 -07002371 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002372 /* Make sure workers have been allocated */
2373 if (!ldp->workers)
2374 {
2375 ldp_alloc_workers ();
2376 ldpw = ldp_worker_get_current ();
2377 }
Florin Coras99368312018-08-02 10:45:44 -07002378 rv = libc_epoll_create1 (flags);
2379 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002380 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002381 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002382 return rv;
2383 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002384
Florin Coras7baeb712019-01-04 17:05:43 -08002385 vlsh = vls_epoll_create ();
2386 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002387 {
Florin Coras7baeb712019-01-04 17:05:43 -08002388 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002389 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002390 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002391 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002392 {
Florin Coras7baeb712019-01-04 17:05:43 -08002393 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002394 }
Florin Coras7baeb712019-01-04 17:05:43 -08002395 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002396 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002397}
2398
2399int
Dave Wallace048b1d62018-01-03 22:24:41 -05002400epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002401{
Dave Wallace048b1d62018-01-03 22:24:41 -05002402 return epoll_create1 (0);
2403}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002404
Dave Wallace048b1d62018-01-03 22:24:41 -05002405int
2406epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2407{
Florin Coras7baeb712019-01-04 17:05:43 -08002408 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002409 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002410
Florin Coras5f33d0d2021-06-02 21:22:21 -07002411 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002412
Florin Coras7baeb712019-01-04 17:05:43 -08002413 vep_vlsh = ldp_fd_to_vlsh (epfd);
2414 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002415 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002416 /* The LDP epoll_create1 always creates VCL epfd's.
2417 * The app should never have a kernel base epoll fd unless it
2418 * was acquired outside of the LD_PRELOAD process context.
2419 * In any case, if we get one, punt it to libc_epoll_ctl.
2420 */
Florin Corasc86662a2023-03-05 11:45:38 -08002421 LDBG (1,
2422 "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2423 " events 0x%x",
2424 epfd, op, fd, event ? event->events : 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002425
2426 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002427 goto done;
2428 }
2429
Florin Coras7baeb712019-01-04 17:05:43 -08002430 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002431
Florin Coras7baeb712019-01-04 17:05:43 -08002432 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2433 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002434
Florin Coras7baeb712019-01-04 17:05:43 -08002435 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002436 {
Florin Corasc86662a2023-03-05 11:45:38 -08002437 LDBG (1,
2438 "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2439 " events 0x%x",
2440 epfd, vep_vlsh, op, vlsh, event ? event->events : 0);
Florin Coras99368312018-08-02 10:45:44 -07002441
Florin Coras7baeb712019-01-04 17:05:43 -08002442 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002443 if (rv != VPPCOM_OK)
2444 {
2445 errno = -rv;
2446 rv = -1;
2447 }
2448 }
2449 else
2450 {
2451 int libc_epfd;
2452 u32 size = sizeof (epfd);
2453
Florin Coras7baeb712019-01-04 17:05:43 -08002454 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002455 if (!libc_epfd)
2456 {
Florin Coras7baeb712019-01-04 17:05:43 -08002457 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2458 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002459
2460 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2461 if (libc_epfd < 0)
2462 {
2463 rv = libc_epfd;
2464 goto done;
2465 }
2466
Florin Coras7baeb712019-01-04 17:05:43 -08002467 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2468 &size);
Florin Coras99368312018-08-02 10:45:44 -07002469 if (rv < 0)
2470 {
2471 errno = -rv;
2472 rv = -1;
2473 goto done;
2474 }
2475 }
2476 else if (PREDICT_FALSE (libc_epfd < 0))
2477 {
2478 errno = -epfd;
2479 rv = -1;
2480 goto done;
2481 }
2482
Florin Coras7baeb712019-01-04 17:05:43 -08002483 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2484 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002485
2486 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002487 }
2488
2489done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002490 return rv;
2491}
Dave Wallace048b1d62018-01-03 22:24:41 -05002492
2493static inline int
Florin Coras99368312018-08-02 10:45:44 -07002494ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2495 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002496{
Liangxing Wang7c7231f2023-02-16 09:31:01 +00002497 ldp_worker_ctx_t *ldpw;
Florin Coras72f77822019-01-22 19:05:52 -08002498 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002499 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002500 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002501
Florin Coras5f33d0d2021-06-02 21:22:21 -07002502 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002503
2504 if (PREDICT_FALSE (!events || (timeout < -1)))
2505 {
2506 errno = EFAULT;
2507 return -1;
2508 }
2509
Liangxing Wang7c7231f2023-02-16 09:31:01 +00002510 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2511 vls_register_vcl_worker ();
2512
2513 ldpw = ldp_worker_get_current ();
Florin Corasdfe4cf42018-11-28 22:13:45 -08002514 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002515 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2516
Florin Coras7baeb712019-01-04 17:05:43 -08002517 ep_vlsh = ldp_fd_to_vlsh (epfd);
2518 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002519 {
Florin Coras7baeb712019-01-04 17:05:43 -08002520 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002521 errno = EBADFD;
2522 return -1;
2523 }
2524
Florin Coras4dee8cd2019-01-29 21:28:16 -08002525 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2526 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002527 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002528 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002529
Florin Coras7baeb712019-01-04 17:05:43 -08002530 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002531 if (PREDICT_FALSE (libc_epfd < 0))
2532 {
2533 errno = -libc_epfd;
2534 rv = -1;
2535 goto done;
2536 }
2537
Florin Coras7baeb712019-01-04 17:05:43 -08002538 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2539 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002540 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002541 do
2542 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002543 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002544 {
Florin Coras7baeb712019-01-04 17:05:43 -08002545 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002546 if (rv > 0)
2547 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002548 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002549 goto done;
2550 }
2551 else if (rv < 0)
2552 {
2553 errno = -rv;
2554 rv = -1;
2555 goto done;
2556 }
2557 }
2558 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002559 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002560
2561 if (libc_epfd > 0)
2562 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002563 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002564 if (rv != 0)
2565 goto done;
2566 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002567 }
Florin Coras72f77822019-01-22 19:05:52 -08002568 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002569
2570done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002571 return rv;
2572}
2573
hanlin4266d4d2020-05-19 17:34:17 +08002574static inline int
2575ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events,
2576 int maxevents, int timeout, const sigset_t * sigmask)
2577{
Florin Corasc86662a2023-03-05 11:45:38 -08002578 int libc_epfd, rv = 0, num_ev, libc_num_ev, vcl_wups = 0;
2579 struct epoll_event *libc_evts;
hanlina3a48962020-07-13 11:09:15 +08002580 ldp_worker_ctx_t *ldpw;
hanlin4266d4d2020-05-19 17:34:17 +08002581 vls_handle_t ep_vlsh;
2582
Florin Coras5f33d0d2021-06-02 21:22:21 -07002583 ldp_init_check ();
hanlin4266d4d2020-05-19 17:34:17 +08002584
2585 if (PREDICT_FALSE (!events || (timeout < -1)))
2586 {
2587 errno = EFAULT;
2588 return -1;
2589 }
2590
Florin Corasff40d8f2020-08-11 22:05:28 -07002591 /* Make sure the vcl worker is valid. Could be that epoll fd was created on
2592 * one thread but it is now used on another */
2593 if (PREDICT_FALSE (vppcom_worker_index () == ~0))
2594 vls_register_vcl_worker ();
hanlina3a48962020-07-13 11:09:15 +08002595
2596 ldpw = ldp_worker_get_current ();
hanlin4266d4d2020-05-19 17:34:17 +08002597 if (epfd == ldpw->vcl_mq_epfd)
2598 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2599
2600 ep_vlsh = ldp_fd_to_vlsh (epfd);
2601 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
2602 {
2603 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
2604 errno = EBADFD;
2605 return -1;
2606 }
2607
2608 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
2609 if (PREDICT_FALSE (!libc_epfd))
2610 {
2611 u32 size = sizeof (epfd);
2612
2613 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2614 "EPOLL_CLOEXEC", epfd, ep_vlsh);
2615 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2616 if (libc_epfd < 0)
2617 {
2618 rv = libc_epfd;
2619 goto done;
2620 }
2621
2622 rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size);
2623 if (rv < 0)
2624 {
2625 errno = -rv;
2626 rv = -1;
2627 goto done;
2628 }
2629 }
2630 if (PREDICT_FALSE (libc_epfd <= 0))
2631 {
2632 errno = -libc_epfd;
2633 rv = -1;
2634 goto done;
2635 }
2636
2637 if (PREDICT_FALSE (!ldpw->mq_epfd_added))
2638 {
2639 struct epoll_event e = { 0 };
2640 e.events = EPOLLIN;
2641 e.data.fd = ldpw->vcl_mq_epfd;
2642 if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) <
2643 0)
2644 {
2645 LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d",
2646 epfd, ldpw->vcl_mq_epfd, libc_epfd);
2647 rv = -1;
2648 goto done;
2649 }
2650 ldpw->mq_epfd_added = 1;
2651 }
2652
wanghanlin8919fec2021-03-18 20:00:41 +08002653 /* Request to only drain unhandled to prevent libc_epoll_wait starved */
2654 rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2);
hanlin4266d4d2020-05-19 17:34:17 +08002655 if (rv > 0)
Florin Corasc86662a2023-03-05 11:45:38 -08002656 {
2657 timeout = 0;
2658 if (rv >= maxevents)
2659 goto done;
2660 }
hanlina3a48962020-07-13 11:09:15 +08002661 else if (PREDICT_FALSE (rv < 0))
hanlin4266d4d2020-05-19 17:34:17 +08002662 {
2663 errno = -rv;
2664 rv = -1;
2665 goto done;
2666 }
2667
Florin Coras7a1e95a2023-03-01 00:45:31 -08002668epoll_again:
Florin Corasc86662a2023-03-05 11:45:38 -08002669
2670 libc_evts = &events[rv];
2671 libc_num_ev =
2672 libc_epoll_pwait (libc_epfd, libc_evts, maxevents - rv, timeout, sigmask);
2673 if (libc_num_ev <= 0)
hanlin4266d4d2020-05-19 17:34:17 +08002674 {
Florin Corasc86662a2023-03-05 11:45:38 -08002675 rv = rv >= 0 ? rv : -1;
2676 goto done;
2677 }
2678
2679 for (int i = 0; i < libc_num_ev; i++)
2680 {
2681 if (libc_evts[i].data.fd == ldpw->vcl_mq_epfd)
hanlin4266d4d2020-05-19 17:34:17 +08002682 {
2683 /* We should remove mq epoll fd from events. */
Florin Corasc86662a2023-03-05 11:45:38 -08002684 libc_num_ev--;
2685 if (i != libc_num_ev)
hanlin4266d4d2020-05-19 17:34:17 +08002686 {
Florin Corasc86662a2023-03-05 11:45:38 -08002687 libc_evts[i].events = libc_evts[libc_num_ev].events;
2688 libc_evts[i].data.u64 = libc_evts[libc_num_ev].data.u64;
hanlin4266d4d2020-05-19 17:34:17 +08002689 }
Florin Corasc86662a2023-03-05 11:45:38 -08002690 num_ev = vls_epoll_wait (ep_vlsh, &libc_evts[libc_num_ev],
2691 maxevents - libc_num_ev, 0);
hanlin4266d4d2020-05-19 17:34:17 +08002692 if (PREDICT_TRUE (num_ev > 0))
2693 rv += num_ev;
Florin Coras7a1e95a2023-03-01 00:45:31 -08002694 /* Woken up by vcl but no events generated. Accept it once */
Florin Corasc86662a2023-03-05 11:45:38 -08002695 if (rv == 0 && libc_num_ev == 0 && timeout && vcl_wups++ < 1)
Florin Coras7a1e95a2023-03-01 00:45:31 -08002696 goto epoll_again;
hanlin4266d4d2020-05-19 17:34:17 +08002697 break;
2698 }
2699 }
2700
Florin Corasc86662a2023-03-05 11:45:38 -08002701 rv += libc_num_ev;
2702
hanlin4266d4d2020-05-19 17:34:17 +08002703done:
2704 return rv;
2705}
2706
Dave Wallace048b1d62018-01-03 22:24:41 -05002707int
2708epoll_pwait (int epfd, struct epoll_event *events,
2709 int maxevents, int timeout, const sigset_t * sigmask)
2710{
hanlin4266d4d2020-05-19 17:34:17 +08002711 if (vls_use_eventfd ())
2712 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout,
2713 sigmask);
2714 else
2715 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002716}
2717
2718int
2719epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2720{
hanlin4266d4d2020-05-19 17:34:17 +08002721 if (vls_use_eventfd ())
2722 return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL);
2723 else
2724 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002725}
2726
2727int
2728poll (struct pollfd *fds, nfds_t nfds, int timeout)
2729{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002730 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002731 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002732 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002733 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002734 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002735
Florin Coraseb801d02020-09-16 17:44:58 -07002736 LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002737
Florin Coras4dee8cd2019-01-29 21:28:16 -08002738 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2739 clib_time_init (&ldpw->clib_time);
2740
2741 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2742 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002743
Dave Wallace048b1d62018-01-03 22:24:41 -05002744 for (i = 0; i < nfds; i++)
2745 {
Florin Coras6917b942018-11-13 22:44:54 -08002746 if (fds[i].fd < 0)
2747 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002748
Florin Coras7baeb712019-01-04 17:05:43 -08002749 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2750 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002751 {
2752 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002753 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002754 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002755 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002756 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002757#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002758 if (fds[i].events & POLLRDNORM)
2759 vp->events |= POLLIN;
2760 if (fds[i].events & POLLWRNORM)
2761 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002762#endif
Florin Coras6917b942018-11-13 22:44:54 -08002763 vp->revents = fds[i].revents;
2764 }
2765 else
2766 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002767 vec_add1 (ldpw->libc_poll, fds[i]);
2768 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002769 }
2770 }
2771
Dave Wallace048b1d62018-01-03 22:24:41 -05002772 do
2773 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002774 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002775 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002776 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002777 if (rv < 0)
2778 {
2779 errno = -rv;
2780 rv = -1;
2781 goto done;
2782 }
2783 else
2784 n_revents += rv;
2785 }
2786
Florin Corasdfe4cf42018-11-28 22:13:45 -08002787 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002788 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002789 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002790 if (rv < 0)
2791 goto done;
2792 else
2793 n_revents += rv;
2794 }
2795
2796 if (n_revents)
2797 {
2798 rv = n_revents;
2799 goto done;
2800 }
2801 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002802 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002803 rv = 0;
2804
2805done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002806 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002807 {
2808 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002809 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002810#ifdef __USE_XOPEN2K
2811 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2812 (fds[vp->fds_ndx].events & POLLRDNORM))
2813 fds[vp->fds_ndx].revents |= POLLRDNORM;
2814 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2815 (fds[vp->fds_ndx].events & POLLWRNORM))
2816 fds[vp->fds_ndx].revents |= POLLWRNORM;
2817#endif
2818 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002819 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002820
Florin Corasdfe4cf42018-11-28 22:13:45 -08002821 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002822 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002823 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002824 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002825 vec_reset_length (ldpw->libc_poll_idxs);
2826 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002827
Dave Wallace048b1d62018-01-03 22:24:41 -05002828 return rv;
2829}
2830
Florin Coras36847942023-02-02 12:56:16 -08002831#ifdef _GNU_SOURCE
Dave Wallace048b1d62018-01-03 22:24:41 -05002832int
2833ppoll (struct pollfd *fds, nfds_t nfds,
2834 const struct timespec *timeout, const sigset_t * sigmask)
2835{
Florin Coras5f33d0d2021-06-02 21:22:21 -07002836 ldp_init_check ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002837
2838 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2839 errno = ENOSYS;
2840
2841
2842 return -1;
2843}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002844#endif
2845
Dave Wallace2a865272018-02-07 21:00:42 -05002846void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002847
Dave Wallace2a865272018-02-07 21:00:42 -05002848void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002849
Dave Wallace048b1d62018-01-03 22:24:41 -05002850/*
2851 * This function is called when the library is loaded
2852 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002853void
Dave Wallace2a865272018-02-07 21:00:42 -05002854ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002855{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002856 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002857 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002858 {
2859 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2860 getpid ());
2861 _exit (1);
2862 }
Dave Wallace69d01192018-02-22 16:22:09 -05002863 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002864 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002865}
2866
2867/*
2868 * This function is called when the library is unloaded
2869 */
2870void
Dave Wallace2a865272018-02-07 21:00:42 -05002871ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002872{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002873 /*
2874 swrap_destructor ();
2875 if (ldp->init)
2876 ldp->init = 0;
2877 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002878
2879 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002880 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002881 */
Dave Wallace69d01192018-02-22 16:22:09 -05002882 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002883 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2884 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002885}
2886
2887
2888/*
2889 * fd.io coding-style-patch-verification: ON
2890 *
2891 * Local Variables:
2892 * eval: (c-set-style "gnu")
2893 * End:
2894 */