blob: c23f995d5674edfcd01e5024b9e3811b01f3fbb4 [file] [log] [blame]
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <unistd.h>
16#include <stdio.h>
17#include <signal.h>
18#include <dlfcn.h>
19#include <pthread.h>
20#include <time.h>
21#include <stdarg.h>
shrinivasan ganapathy1d359632017-10-15 15:46:09 -070022#include <sys/resource.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050023#include <netinet/tcp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070024
Dave Wallace2a865272018-02-07 21:00:42 -050025#include <vcl/ldp_socket_wrapper.h>
26#include <vcl/ldp.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070027#include <sys/time.h>
28
Florin Coras7baeb712019-01-04 17:05:43 -080029#include <vcl/vcl_locked.h>
Dave Wallace048b1d62018-01-03 22:24:41 -050030#include <vppinfra/time.h>
31#include <vppinfra/bitmap.h>
Florin Coras30e273b2018-11-27 00:04:59 -080032#include <vppinfra/lock.h>
33#include <vppinfra/pool.h>
34#include <vppinfra/hash.h>
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -070035
36#define HAVE_CONSTRUCTOR_ATTRIBUTE
37#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
38#define CONSTRUCTOR_ATTRIBUTE \
39 __attribute__ ((constructor))
40#else
41#define CONSTRUCTOR_ATTRIBUTE
42#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
43
44#define HAVE_DESTRUCTOR_ATTRIBUTE
45#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
46#define DESTRUCTOR_ATTRIBUTE \
47 __attribute__ ((destructor))
48#else
49#define DESTRUCTOR_ATTRIBUTE
50#endif
51
Florin Corasdfe4cf42018-11-28 22:13:45 -080052#define LDP_MAX_NWORKERS 32
53
Florin Corasdfe4cf42018-11-28 22:13:45 -080054typedef struct ldp_worker_ctx_
Dave Wallace048b1d62018-01-03 22:24:41 -050055{
Dave Wallace048b1d62018-01-03 22:24:41 -050056 u8 *io_buffer;
57 clib_time_t clib_time;
Florin Corasdfe4cf42018-11-28 22:13:45 -080058
59 /*
60 * Select state
61 */
Dave Wallace048b1d62018-01-03 22:24:41 -050062 clib_bitmap_t *rd_bitmap;
63 clib_bitmap_t *wr_bitmap;
64 clib_bitmap_t *ex_bitmap;
Florin Coras294afe22019-01-07 17:49:17 -080065 clib_bitmap_t *si_rd_bitmap;
66 clib_bitmap_t *si_wr_bitmap;
67 clib_bitmap_t *si_ex_bitmap;
Dave Wallace048b1d62018-01-03 22:24:41 -050068 clib_bitmap_t *libc_rd_bitmap;
69 clib_bitmap_t *libc_wr_bitmap;
70 clib_bitmap_t *libc_ex_bitmap;
Florin Corasdfe4cf42018-11-28 22:13:45 -080071
72 /*
73 * Poll state
74 */
Dave Wallace048b1d62018-01-03 22:24:41 -050075 vcl_poll_t *vcl_poll;
Florin Coras6917b942018-11-13 22:44:54 -080076 struct pollfd *libc_poll;
77 u16 *libc_poll_idxs;
Florin Corasdfe4cf42018-11-28 22:13:45 -080078
79 /*
80 * Epoll state
81 */
Dave Wallace048b1d62018-01-03 22:24:41 -050082 u8 epoll_wait_vcl;
Florin Coras99368312018-08-02 10:45:44 -070083 int vcl_mq_epfd;
Florin Corasdfe4cf42018-11-28 22:13:45 -080084
85} ldp_worker_ctx_t;
86
Florin Coras294afe22019-01-07 17:49:17 -080087/* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure
88 * they are the same size */
89STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask),
90 "ldp bitmap size mismatch");
91STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask),
92 "ldp bitmap size mismatch");
93
Florin Corasdfe4cf42018-11-28 22:13:45 -080094typedef struct
95{
96 ldp_worker_ctx_t *workers;
97 int init;
98 char app_name[LDP_APP_NAME_MAX];
Florin Coras7baeb712019-01-04 17:05:43 -080099 u32 vlsh_bit_val;
100 u32 vlsh_bit_mask;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800101 u32 debug;
Yu Ping7b74b072019-05-08 00:40:24 +0800102 u8 transparent_tls;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800103
104 /** vcl needs next epoll_create to go to libc_epoll */
105 u8 vcl_needs_real_epoll;
Dave Wallace2a865272018-02-07 21:00:42 -0500106} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800107
Dave Wallace2a865272018-02-07 21:00:42 -0500108#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700109
Florin Coras99368312018-08-02 10:45:44 -0700110#define LDBG(_lvl, _fmt, _args...) \
111 if (ldp->debug > _lvl) \
hanlin9f3f18f2019-12-30 16:25:20 +0800112 { \
113 int errno_saved = errno; \
114 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args); \
115 errno = errno_saved; \
116 }
Florin Coras99368312018-08-02 10:45:44 -0700117
Dave Wallace2a865272018-02-07 21:00:42 -0500118static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800119 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
120 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500121 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800122 .transparent_tls = 0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500123};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700124
Dave Wallace2a865272018-02-07 21:00:42 -0500125static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700126
Florin Corasdfe4cf42018-11-28 22:13:45 -0800127static inline ldp_worker_ctx_t *
128ldp_worker_get_current (void)
129{
130 return (ldp->workers + vppcom_worker_index ());
131}
132
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700133/*
134 * RETURN: 0 on success or -1 on error.
135 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500136static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500137ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700138{
Benoît Ganne747b3d82019-08-21 18:27:23 +0200139 snprintf (ldp->app_name, LDP_APP_NAME_MAX,
140 "ldp-%d-%s", getpid (), app_name);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700141}
142
Dave Wallace048b1d62018-01-03 22:24:41 -0500143static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500144ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700145{
Dave Wallace2a865272018-02-07 21:00:42 -0500146 if (ldp->app_name[0] == '\0')
147 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500148
Dave Wallace2a865272018-02-07 21:00:42 -0500149 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700150}
151
Dave Wallace048b1d62018-01-03 22:24:41 -0500152static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800153ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500154{
Florin Coras7baeb712019-01-04 17:05:43 -0800155 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500156}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700157
Florin Coras7baeb712019-01-04 17:05:43 -0800158static inline vls_handle_t
159ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500160{
Florin Coras7baeb712019-01-04 17:05:43 -0800161 if (fd < ldp->vlsh_bit_val)
162 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700163
Florin Coras7baeb712019-01-04 17:05:43 -0800164 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500165}
166
Florin Coras2d9b4272019-03-11 10:14:37 -0700167static void
168ldp_alloc_workers (void)
169{
170 if (ldp->workers)
171 return;
172 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
173}
174
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700175static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500176ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700177{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800178 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700179 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700180
Florin Coras99368312018-08-02 10:45:44 -0700181 if (PREDICT_TRUE (ldp->init))
182 return 0;
183
184 ldp->init = 1;
185 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800186 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700187 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700188 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800189 ldp->vcl_needs_real_epoll = 0;
190 if (rv == VPPCOM_EEXIST)
191 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800192 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
193 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700194 ldp->init = 0;
195 return rv;
196 }
197 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700198 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800199 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700200
201 char *env_var_str = getenv (LDP_ENV_DEBUG);
202 if (env_var_str)
203 {
204 u32 tmp;
205 if (sscanf (env_var_str, "%u", &tmp) != 1)
206 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
207 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
208 env_var_str);
209 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700210 {
Florin Coras99368312018-08-02 10:45:44 -0700211 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800212 LDBG (0, "configured LDP debug level (%u) from env var "
213 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700214 }
215 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500216
Florin Coras99368312018-08-02 10:45:44 -0700217 env_var_str = getenv (LDP_ENV_APP_NAME);
218 if (env_var_str)
219 {
220 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800221 LDBG (0, "configured LDP app name (%s) from the env var "
222 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700223 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500224
Florin Coras99368312018-08-02 10:45:44 -0700225 env_var_str = getenv (LDP_ENV_SID_BIT);
226 if (env_var_str)
227 {
228 u32 sb;
229 if (sscanf (env_var_str, "%u", &sb) != 1)
230 {
Florin Coras294afe22019-01-07 17:49:17 -0800231 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
232 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
233 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700234 }
235 else if (sb < LDP_SID_BIT_MIN)
236 {
Florin Coras7baeb712019-01-04 17:05:43 -0800237 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
238 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500239
Florin Coras294afe22019-01-07 17:49:17 -0800240 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
241 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
242 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
243 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700244 }
245 else if (sb > LDP_SID_BIT_MAX)
246 {
Florin Coras7baeb712019-01-04 17:05:43 -0800247 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
248 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500249
Florin Coras294afe22019-01-07 17:49:17 -0800250 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
251 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
252 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
253 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500254 }
255 else
256 {
Florin Coras7baeb712019-01-04 17:05:43 -0800257 ldp->vlsh_bit_val = (1 << sb);
258 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700259
Florin Coras05ecfcc2018-12-12 18:19:39 -0800260 LDBG (0, "configured LDP sid bit (%u) from "
261 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800262 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500263 }
Florin Coras294afe22019-01-07 17:49:17 -0800264
265 /* Make sure there are enough bits in the fd set for vcl sessions */
266 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
267 {
268 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
269 ldp->vlsh_bit_val, FD_SETSIZE / 2);
270 ldp->init = 0;
271 return -1;
272 }
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 Coras99368312018-08-02 10:45:44 -0700279
Florin Coras4dee8cd2019-01-29 21:28:16 -0800280 /* *INDENT-OFF* */
281 pool_foreach (ldpw, ldp->workers, ({
282 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
283 }));
284 /* *INDENT-ON* */
285
Florin Coras05ecfcc2018-12-12 18:19:39 -0800286 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700287
288 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500289}
290
291int
292close (int fd)
293{
Florin Coras7baeb712019-01-04 17:05:43 -0800294 vls_handle_t vlsh;
295 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500296
Dave Wallace2a865272018-02-07 21:00:42 -0500297 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500298 return -1;
299
Florin Coras7baeb712019-01-04 17:05:43 -0800300 vlsh = ldp_fd_to_vlsh (fd);
301 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500302 {
Florin Coras7baeb712019-01-04 17:05:43 -0800303 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500304 if (epfd > 0)
305 {
Florin Coras7baeb712019-01-04 17:05:43 -0800306 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500307
308 rv = libc_close (epfd);
309 if (rv < 0)
310 {
311 u32 size = sizeof (epfd);
312 epfd = 0;
313
Florin Coras7baeb712019-01-04 17:05:43 -0800314 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500315 }
316 }
317 else if (PREDICT_FALSE (epfd < 0))
318 {
319 errno = -epfd;
320 rv = -1;
321 goto done;
322 }
323
Florin Coras7baeb712019-01-04 17:05:43 -0800324 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500325
Florin Coras7baeb712019-01-04 17:05:43 -0800326 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500327 if (rv != VPPCOM_OK)
328 {
329 errno = -rv;
330 rv = -1;
331 }
332 }
333 else
334 {
Florin Coras7baeb712019-01-04 17:05:43 -0800335 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500336 rv = libc_close (fd);
337 }
338
339done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500340 return rv;
341}
342
343ssize_t
344read (int fd, void *buf, size_t nbytes)
345{
Florin Coras7baeb712019-01-04 17:05:43 -0800346 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500347 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500348
Dave Wallace2a865272018-02-07 21:00:42 -0500349 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500350 return -1;
351
Florin Coras7baeb712019-01-04 17:05:43 -0800352 vlsh = ldp_fd_to_vlsh (fd);
353 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500354 {
Florin Coras7baeb712019-01-04 17:05:43 -0800355 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500356 if (size < 0)
357 {
358 errno = -size;
359 size = -1;
360 }
361 }
362 else
363 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500364 size = libc_read (fd, buf, nbytes);
365 }
366
Dave Wallace048b1d62018-01-03 22:24:41 -0500367 return size;
368}
369
370ssize_t
371readv (int fd, const struct iovec * iov, int iovcnt)
372{
Dave Wallace8aaba562018-01-18 17:21:19 -0500373 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800374 vls_handle_t vlsh;
375 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500376
Dave Wallace2a865272018-02-07 21:00:42 -0500377 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500378 return -1;
379
Florin Coras7baeb712019-01-04 17:05:43 -0800380 vlsh = ldp_fd_to_vlsh (fd);
381 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500382 {
Florin Coras067f9542020-02-14 05:33:46 +0000383 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500384 {
Florin Coras067f9542020-02-14 05:33:46 +0000385 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
386 if (rv <= 0)
387 break;
388 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500389 {
Florin Coras067f9542020-02-14 05:33:46 +0000390 total += rv;
391 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500392 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700393 }
394 }
Florin Coras067f9542020-02-14 05:33:46 +0000395 if (rv < 0 && total == 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700396 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500397 errno = -rv;
398 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700399 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500400 else
401 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700402 }
403 else
404 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500405 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700406 }
407
Dave Wallace048b1d62018-01-03 22:24:41 -0500408 return size;
409}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700410
Dave Wallace048b1d62018-01-03 22:24:41 -0500411ssize_t
412write (int fd, const void *buf, size_t nbytes)
413{
Florin Coras7baeb712019-01-04 17:05:43 -0800414 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500415 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500416
Dave Wallace2a865272018-02-07 21:00:42 -0500417 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500418 return -1;
419
Florin Coras7baeb712019-01-04 17:05:43 -0800420 vlsh = ldp_fd_to_vlsh (fd);
421 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500422 {
Florin Coras7baeb712019-01-04 17:05:43 -0800423 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500424 if (size < 0)
425 {
426 errno = -size;
427 size = -1;
428 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700429 }
430 else
431 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500432 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700433 }
434
Dave Wallace048b1d62018-01-03 22:24:41 -0500435 return size;
436}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700437
Dave Wallace048b1d62018-01-03 22:24:41 -0500438ssize_t
439writev (int fd, const struct iovec * iov, int iovcnt)
440{
Dave Wallace048b1d62018-01-03 22:24:41 -0500441 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800442 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500443 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500444
Dave Wallace2a865272018-02-07 21:00:42 -0500445 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500446 return -1;
447
Florin Coras7baeb712019-01-04 17:05:43 -0800448 vlsh = ldp_fd_to_vlsh (fd);
449 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500450 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000451 for (i = 0; i < iovcnt; ++i)
Dave Wallace048b1d62018-01-03 22:24:41 -0500452 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000453 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
454 if (rv < 0)
455 break;
456 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500457 {
Florin Coraseda1b8c2020-03-23 16:00:35 +0000458 total += rv;
459 if (rv < iov[i].iov_len)
Dave Wallace048b1d62018-01-03 22:24:41 -0500460 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500461 }
462 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500463
Florin Coraseda1b8c2020-03-23 16:00:35 +0000464 if (rv < 0 && total == 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500465 {
466 errno = -rv;
467 size = -1;
468 }
469 else
470 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700471 }
472 else
473 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500474 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700475 }
476
Dave Wallace048b1d62018-01-03 22:24:41 -0500477 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700478}
479
Carl Smithe16707b2019-11-13 14:37:39 +1300480#ifdef HAVE_FCNTL64
481int
482fcntl64 (int fd, int cmd, ...)
483#else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700484int
Dave Wallace048b1d62018-01-03 22:24:41 -0500485fcntl (int fd, int cmd, ...)
Carl Smithe16707b2019-11-13 14:37:39 +1300486#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700487{
Florin Coras7baeb712019-01-04 17:05:43 -0800488 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700489 int rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500490 va_list ap;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700491
Dave Wallace2a865272018-02-07 21:00:42 -0500492 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500493 return -1;
494
495 va_start (ap, cmd);
Florin Coras7baeb712019-01-04 17:05:43 -0800496
497 vlsh = ldp_fd_to_vlsh (fd);
498 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
499 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700500 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500501 int flags = va_arg (ap, int);
502 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700503
Dave Wallace048b1d62018-01-03 22:24:41 -0500504 size = sizeof (flags);
505 rv = -EOPNOTSUPP;
506 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700507 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500508 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800509 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500510 break;
511
512 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800513 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500514 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800515 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700516 break;
Florin Coras173bae32018-11-16 18:56:28 -0800517 case F_SETFD:
518 /* TODO handle this */
519 LDBG (0, "F_SETFD ignored flags %u", flags);
520 rv = 0;
521 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700522 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500523 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700524 break;
525 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500526 if (rv < 0)
527 {
528 errno = -rv;
529 rv = -1;
530 }
531 }
532 else
533 {
Carl Smithe16707b2019-11-13 14:37:39 +1300534#ifdef HAVE_FCNTL64
535 rv = libc_vfcntl64 (fd, cmd, ap);
536#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500537 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300538#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700539 }
540
Dave Wallace048b1d62018-01-03 22:24:41 -0500541 va_end (ap);
542
Dave Wallace048b1d62018-01-03 22:24:41 -0500543 return rv;
544}
545
546int
547ioctl (int fd, unsigned long int cmd, ...)
548{
Florin Coras7baeb712019-01-04 17:05:43 -0800549 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500550 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800551 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500552
Dave Wallace2a865272018-02-07 21:00:42 -0500553 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500554 return -1;
555
556 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500557
Florin Coras7baeb712019-01-04 17:05:43 -0800558 vlsh = ldp_fd_to_vlsh (fd);
559 if (vlsh != VLS_INVALID_HANDLE)
560 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500561 switch (cmd)
562 {
563 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800564 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500565 break;
566
567 case FIONBIO:
568 {
569 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
570 u32 size = sizeof (flags);
571
572 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
573 * non-blocking, the flags should be read here and merged
574 * with O_NONBLOCK.
575 */
Florin Coras7baeb712019-01-04 17:05:43 -0800576 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500577 }
578 break;
579
580 default:
581 rv = -EOPNOTSUPP;
582 break;
583 }
584 if (rv < 0)
585 {
586 errno = -rv;
587 rv = -1;
588 }
589 }
590 else
591 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500592 rv = libc_vioctl (fd, cmd, ap);
593 }
594
Dave Wallace048b1d62018-01-03 22:24:41 -0500595 va_end (ap);
596 return rv;
597}
598
Florin Coras294afe22019-01-07 17:49:17 -0800599always_inline void
600ldp_select_init_maps (fd_set * __restrict original,
601 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
602 clib_bitmap_t ** vclb, int nfds, u32 minbits,
603 u32 n_bytes, uword * si_bits, uword * libc_bits)
604{
605 uword si_bits_set, libc_bits_set;
606 vls_handle_t vlsh;
607 int fd;
608
609 clib_bitmap_validate (*vclb, minbits);
610 clib_bitmap_validate (*libcb, minbits);
611 clib_bitmap_validate (*resultb, minbits);
612 clib_memcpy_fast (*resultb, original, n_bytes);
613 memset (original, 0, n_bytes);
614
615 /* *INDENT-OFF* */
616 clib_bitmap_foreach (fd, *resultb, ({
617 if (fd > nfds)
618 break;
619 vlsh = ldp_fd_to_vlsh (fd);
620 if (vlsh == VLS_INVALID_HANDLE)
621 clib_bitmap_set_no_check (*libcb, fd, 1);
622 else
Florin Corascbce80a2020-04-20 01:32:38 +0000623 *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1);
Florin Coras294afe22019-01-07 17:49:17 -0800624 }));
625 /* *INDENT-ON* */
626
627 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
628 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
Florin Corascbce80a2020-04-20 01:32:38 +0000629 clib_bitmap_validate (*resultb, *si_bits);
Florin Coras294afe22019-01-07 17:49:17 -0800630
631 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
632 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
633}
634
635always_inline int
636ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
637{
638 vls_handle_t vlsh;
639 uword si;
640 int fd;
641
642 if (!libcb)
643 return 0;
644
645 /* *INDENT-OFF* */
646 clib_bitmap_foreach (si, vclb, ({
647 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000648 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800649 fd = ldp_vlsh_to_fd (vlsh);
650 if (PREDICT_FALSE (fd < 0))
651 {
652 errno = EBADFD;
653 return -1;
654 }
655 FD_SET (fd, libcb);
656 }));
657 /* *INDENT-ON* */
658
659 return 0;
660}
661
662always_inline void
663ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
664{
665 uword fd;
666
Florin Coras78b5fa62019-02-21 20:04:15 -0800667 if (!libcb)
668 return;
669
Florin Coras294afe22019-01-07 17:49:17 -0800670 /* *INDENT-OFF* */
671 clib_bitmap_foreach (fd, result, ({
672 FD_SET ((int)fd, libcb);
673 }));
674 /* *INDENT-ON* */
675}
676
Dave Wallace048b1d62018-01-03 22:24:41 -0500677int
Dave Wallace2a865272018-02-07 21:00:42 -0500678ldp_pselect (int nfds, fd_set * __restrict readfds,
679 fd_set * __restrict writefds,
680 fd_set * __restrict exceptfds,
681 const struct timespec *__restrict timeout,
682 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500683{
Florin Coras294afe22019-01-07 17:49:17 -0800684 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800685 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800686 struct timespec libc_tspec = { 0 };
687 f64 time_out, vcl_timeout = 0;
688 uword si_bits, libc_bits;
689 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500690
691 if (nfds < 0)
692 {
693 errno = EINVAL;
694 return -1;
695 }
696
Florin Coras4dee8cd2019-01-29 21:28:16 -0800697 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
698 clib_time_init (&ldpw->clib_time);
699
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500700 if (timeout)
701 {
702 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800703 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500704
705 /* select as fine grained sleep */
706 if (!nfds)
707 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800708 time_out += clib_time_now (&ldpw->clib_time);
709 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500710 ;
711 return 0;
712 }
713 }
714 else if (!nfds)
715 {
716 errno = EINVAL;
717 return -1;
718 }
719 else
720 time_out = -1;
721
Florin Coras7baeb712019-01-04 17:05:43 -0800722 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500723 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500724 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
725 timeout, sigmask);
726 goto done;
727 }
728
Florin Coras294afe22019-01-07 17:49:17 -0800729 si_bits = libc_bits = 0;
730 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800731
Dave Wallace048b1d62018-01-03 22:24:41 -0500732 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800733 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
734 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
735 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500736 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800737 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
738 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
739 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500740 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800741 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
742 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
743 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500744
Florin Coras294afe22019-01-07 17:49:17 -0800745 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500746 {
747 errno = EINVAL;
748 rv = -1;
749 goto done;
750 }
751
Florin Coras78b5fa62019-02-21 20:04:15 -0800752 if (!si_bits)
753 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800754
Dave Wallace048b1d62018-01-03 22:24:41 -0500755 do
756 {
Florin Coras294afe22019-01-07 17:49:17 -0800757 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500758 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500759 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800760 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000761 vec_len (ldpw->si_rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500762 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500763 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800764 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000765 vec_len (ldpw->si_wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500766 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500767 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800768 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corascbce80a2020-04-20 01:32:38 +0000769 vec_len (ldpw->si_ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500770 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800771
Florin Coras0ef8ef22019-01-18 08:37:13 -0800772 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
773 writefds ? ldpw->wr_bitmap : NULL,
774 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800775 if (rv < 0)
776 {
777 errno = -rv;
778 rv = -1;
779 }
780 else if (rv > 0)
781 {
782 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
783 {
784 rv = -1;
785 goto done;
786 }
787
788 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
789 {
790 rv = -1;
791 goto done;
792 }
793
794 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
795 {
796 rv = -1;
797 goto done;
798 }
799 bits_set = rv;
800 }
801 }
802 if (libc_bits)
803 {
804 if (readfds)
805 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
806 vec_len (ldpw->libc_rd_bitmap) *
807 sizeof (clib_bitmap_t));
808 if (writefds)
809 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
810 vec_len (ldpw->libc_wr_bitmap) *
811 sizeof (clib_bitmap_t));
812 if (exceptfds)
813 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
814 vec_len (ldpw->libc_ex_bitmap) *
815 sizeof (clib_bitmap_t));
816
Dave Wallace048b1d62018-01-03 22:24:41 -0500817 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800818 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
819 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
820 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
821 &libc_tspec, sigmask);
822 if (rv > 0)
823 {
824 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
825 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
826 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
827 bits_set += rv;
828 }
829 }
830
831 if (bits_set)
832 {
833 rv = bits_set;
834 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500835 }
836 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800837 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500838 rv = 0;
839
840done:
841 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800842 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800843 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800844 clib_bitmap_zero (ldpw->libc_rd_bitmap);
845 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800846 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800847 clib_bitmap_zero (ldpw->libc_wr_bitmap);
848 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800849 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800850 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500851
Dave Wallace048b1d62018-01-03 22:24:41 -0500852 return rv;
853}
854
855int
856select (int nfds, fd_set * __restrict readfds,
857 fd_set * __restrict writefds,
858 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
859{
860 struct timespec tspec;
861
862 if (timeout)
863 {
864 tspec.tv_sec = timeout->tv_sec;
865 tspec.tv_nsec = timeout->tv_usec * 1000;
866 }
Dave Wallace2a865272018-02-07 21:00:42 -0500867 return ldp_pselect (nfds, readfds, writefds, exceptfds,
868 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500869}
870
871#ifdef __USE_XOPEN2K
872int
873pselect (int nfds, fd_set * __restrict readfds,
874 fd_set * __restrict writefds,
875 fd_set * __restrict exceptfds,
876 const struct timespec *__restrict timeout,
877 const __sigset_t * __restrict sigmask)
878{
Dave Wallace2a865272018-02-07 21:00:42 -0500879 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500880}
881#endif
882
Yu Ping7b74b072019-05-08 00:40:24 +0800883/* If transparent TLS mode is turned on, then ldp will load key and cert.
884 */
885static int
886load_tls_cert (vls_handle_t vlsh)
887{
888 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
889 char inbuf[4096];
890 char *tls_cert;
891 int cert_size;
892 FILE *fp;
893
894 if (env_var_str)
895 {
896 fp = fopen (env_var_str, "r");
897 if (fp == NULL)
898 {
899 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
900 return -1;
901 }
902 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
903 tls_cert = inbuf;
904 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
905 cert_size);
906 fclose (fp);
907 }
908 else
909 {
910 LDBG (0, "ERROR: failed to read LDP environment %s\n",
911 LDP_ENV_TLS_CERT);
912 return -1;
913 }
914 return 0;
915}
916
917static int
918load_tls_key (vls_handle_t vlsh)
919{
920 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
921 char inbuf[4096];
922 char *tls_key;
923 int key_size;
924 FILE *fp;
925
926 if (env_var_str)
927 {
928 fp = fopen (env_var_str, "r");
929 if (fp == NULL)
930 {
931 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
932 return -1;
933 }
934 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
935 tls_key = inbuf;
936 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
937 key_size);
938 fclose (fp);
939 }
940 else
941 {
942 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
943 return -1;
944 }
945 return 0;
946}
947
Dave Wallace048b1d62018-01-03 22:24:41 -0500948int
949socket (int domain, int type, int protocol)
950{
Florin Coras7baeb712019-01-04 17:05:43 -0800951 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500952 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800953 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500954
Dave Wallace2a865272018-02-07 21:00:42 -0500955 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500956 return -1;
957
958 if (((domain == AF_INET) || (domain == AF_INET6)) &&
959 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
960 {
Yu Ping7b74b072019-05-08 00:40:24 +0800961 u8 proto;
962 if (ldp->transparent_tls)
963 {
964 proto = VPPCOM_PROTO_TLS;
965 }
966 else
967 proto = ((sock_type == SOCK_DGRAM) ?
968 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500969
Florin Coras7baeb712019-01-04 17:05:43 -0800970 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
971 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500972
Florin Coras7baeb712019-01-04 17:05:43 -0800973 vlsh = vls_create (proto, is_nonblocking);
974 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500975 {
Florin Coras7baeb712019-01-04 17:05:43 -0800976 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500977 rv = -1;
978 }
979 else
980 {
Yu Ping7b74b072019-05-08 00:40:24 +0800981 if (ldp->transparent_tls)
982 {
983 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
984 {
985 return -1;
986 }
987 }
Florin Coras7baeb712019-01-04 17:05:43 -0800988 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500989 }
990 }
991 else
992 {
Florin Coras7baeb712019-01-04 17:05:43 -0800993 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -0500994 rv = libc_socket (domain, type, protocol);
995 }
996
Dave Wallace048b1d62018-01-03 22:24:41 -0500997 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700998}
999
1000/*
1001 * Create two new sockets, of type TYPE in domain DOMAIN and using
1002 * protocol PROTOCOL, which are connected to each other, and put file
1003 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1004 * one will be chosen automatically.
1005 * Returns 0 on success, -1 for errors.
1006 * */
1007int
Dave Wallace048b1d62018-01-03 22:24:41 -05001008socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001009{
Florin Coras7baeb712019-01-04 17:05:43 -08001010 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001011
Dave Wallace2a865272018-02-07 21:00:42 -05001012 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001013 return -1;
1014
1015 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1016 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001017 {
Florin Coras7baeb712019-01-04 17:05:43 -08001018 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001019 errno = ENOSYS;
1020 rv = -1;
1021 }
1022 else
1023 {
Florin Coras7baeb712019-01-04 17:05:43 -08001024 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001025 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001026 }
1027
Dave Wallace048b1d62018-01-03 22:24:41 -05001028 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001029}
1030
1031int
Dave Wallace048b1d62018-01-03 22:24:41 -05001032bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001033{
Florin Coras7baeb712019-01-04 17:05:43 -08001034 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001035 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001036
Dave Wallace2a865272018-02-07 21:00:42 -05001037 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001038 return -1;
1039
Florin Coras7baeb712019-01-04 17:05:43 -08001040 vlsh = ldp_fd_to_vlsh (fd);
1041 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001042 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001043 vppcom_endpt_t ep;
1044
Dave Wallace048b1d62018-01-03 22:24:41 -05001045 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001046 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001047 case AF_INET:
1048 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001049 {
Florin Coras7baeb712019-01-04 17:05:43 -08001050 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1051 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001052 errno = EINVAL;
1053 rv = -1;
1054 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001055 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001056 ep.is_ip4 = VPPCOM_IS_IP4;
1057 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1058 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1059 break;
1060
1061 case AF_INET6:
1062 if (len != sizeof (struct sockaddr_in6))
1063 {
Florin Coras7baeb712019-01-04 17:05:43 -08001064 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1065 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001066 errno = EINVAL;
1067 rv = -1;
1068 goto done;
1069 }
1070 ep.is_ip4 = VPPCOM_IS_IP6;
1071 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1072 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001073 break;
1074
1075 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001076 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1077 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001078 errno = EAFNOSUPPORT;
1079 rv = -1;
1080 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001081 }
Florin Coras7baeb712019-01-04 17:05:43 -08001082 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1083 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001084
Florin Coras7baeb712019-01-04 17:05:43 -08001085 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001086 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001087 {
1088 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001089 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001090 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001091 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001092 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001093 {
Florin Coras7baeb712019-01-04 17:05:43 -08001094 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001095 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001096 }
1097
Dave Wallace048b1d62018-01-03 22:24:41 -05001098done:
Florin Coras7baeb712019-01-04 17:05:43 -08001099 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001100
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001101 return rv;
1102}
1103
1104static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001105ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1106 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001107{
Dave Wallace048b1d62018-01-03 22:24:41 -05001108 int rv = 0;
1109 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001110
Dave Wallace2a865272018-02-07 21:00:42 -05001111 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001112 return -1;
1113
1114 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001115 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001116 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1117 switch (addr->sa_family)
1118 {
1119 case AF_INET:
1120 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1121 if (*len > sizeof (struct sockaddr_in))
1122 *len = sizeof (struct sockaddr_in);
1123 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1124 copy_len = *len - sa_len;
1125 if (copy_len > 0)
1126 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1127 copy_len);
1128 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001129
Dave Wallace048b1d62018-01-03 22:24:41 -05001130 case AF_INET6:
1131 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1132 if (*len > sizeof (struct sockaddr_in6))
1133 *len = sizeof (struct sockaddr_in6);
1134 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1135 copy_len = *len - sa_len;
1136 if (copy_len > 0)
1137 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1138 __in6_u.__u6_addr8, ep->ip, copy_len);
1139 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001140
Dave Wallace048b1d62018-01-03 22:24:41 -05001141 default:
1142 /* Not possible */
1143 rv = -EAFNOSUPPORT;
1144 break;
1145 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001146 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001147 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001148}
1149
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001150int
Dave Wallace048b1d62018-01-03 22:24:41 -05001151getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001152{
Florin Coras7baeb712019-01-04 17:05:43 -08001153 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001154 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001155
Dave Wallace2a865272018-02-07 21:00:42 -05001156 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001157 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001158
Florin Coras7baeb712019-01-04 17:05:43 -08001159 vlsh = ldp_fd_to_vlsh (fd);
1160 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001161 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001162 vppcom_endpt_t ep;
1163 u8 addr_buf[sizeof (struct in6_addr)];
1164 u32 size = sizeof (ep);
1165
1166 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001167
Florin Coras7baeb712019-01-04 17:05:43 -08001168 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001169 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001170 {
1171 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001172 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001173 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001174 else
1175 {
Dave Wallace2a865272018-02-07 21:00:42 -05001176 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001177 if (rv != VPPCOM_OK)
1178 {
1179 errno = -rv;
1180 rv = -1;
1181 }
1182 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001183 }
1184 else
1185 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001186 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001187 }
1188
Dave Wallace048b1d62018-01-03 22:24:41 -05001189 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001190}
1191
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001192int
Dave Wallace048b1d62018-01-03 22:24:41 -05001193connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001194{
Florin Coras7baeb712019-01-04 17:05:43 -08001195 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001196 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001197
Dave Wallace2a865272018-02-07 21:00:42 -05001198 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001199 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001200
Dave Wallace048b1d62018-01-03 22:24:41 -05001201 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001202 {
Florin Coras7baeb712019-01-04 17:05:43 -08001203 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001204 errno = EINVAL;
1205 rv = -1;
1206 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001207 }
1208
Florin Coras7baeb712019-01-04 17:05:43 -08001209 vlsh = ldp_fd_to_vlsh (fd);
1210 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001211 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001212 vppcom_endpt_t ep;
1213
Dave Wallace048b1d62018-01-03 22:24:41 -05001214 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001215 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001216 case AF_INET:
1217 if (len != sizeof (struct sockaddr_in))
1218 {
Florin Coras7baeb712019-01-04 17:05:43 -08001219 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1220 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001221 errno = EINVAL;
1222 rv = -1;
1223 goto done;
1224 }
1225 ep.is_ip4 = VPPCOM_IS_IP4;
1226 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1227 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1228 break;
1229
1230 case AF_INET6:
1231 if (len != sizeof (struct sockaddr_in6))
1232 {
Florin Coras7baeb712019-01-04 17:05:43 -08001233 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1234 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001235 errno = EINVAL;
1236 rv = -1;
1237 goto done;
1238 }
1239 ep.is_ip4 = VPPCOM_IS_IP6;
1240 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1241 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1242 break;
1243
1244 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001245 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1246 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001247 errno = EAFNOSUPPORT;
1248 rv = -1;
1249 goto done;
1250 }
Florin Coras7baeb712019-01-04 17:05:43 -08001251 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1252 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001253
Florin Coras7baeb712019-01-04 17:05:43 -08001254 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001255 if (rv != VPPCOM_OK)
1256 {
1257 errno = -rv;
1258 rv = -1;
1259 }
1260 }
1261 else
1262 {
Florin Coras7baeb712019-01-04 17:05:43 -08001263 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1264 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001265
1266 rv = libc_connect (fd, addr, len);
1267 }
1268
1269done:
Florin Coras7baeb712019-01-04 17:05:43 -08001270 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001271 return rv;
1272}
1273
1274int
1275getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1276{
Florin Coras7baeb712019-01-04 17:05:43 -08001277 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001278 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001279
Dave Wallace2a865272018-02-07 21:00:42 -05001280 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001281 return -1;
1282
Florin Coras7baeb712019-01-04 17:05:43 -08001283 vlsh = ldp_fd_to_vlsh (fd);
1284 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001285 {
1286 vppcom_endpt_t ep;
1287 u8 addr_buf[sizeof (struct in6_addr)];
1288 u32 size = sizeof (ep);
1289
1290 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001291 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001292 if (rv != VPPCOM_OK)
1293 {
1294 errno = -rv;
1295 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001296 }
1297 else
1298 {
Dave Wallace2a865272018-02-07 21:00:42 -05001299 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001300 if (rv != VPPCOM_OK)
1301 {
1302 errno = -rv;
1303 rv = -1;
1304 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001305 }
1306 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001307 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001308 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001309 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001310 }
1311
Dave Wallace048b1d62018-01-03 22:24:41 -05001312 return rv;
1313}
1314
1315ssize_t
1316send (int fd, const void *buf, size_t n, int flags)
1317{
Florin Coras7baeb712019-01-04 17:05:43 -08001318 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001319 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001320
Dave Wallace2a865272018-02-07 21:00:42 -05001321 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001322 return -1;
1323
Florin Coras7baeb712019-01-04 17:05:43 -08001324 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 {
Florin Coras7baeb712019-01-04 17:05:43 -08001326 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001327 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001328 {
1329 errno = -size;
1330 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001331 }
1332 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001333 else
1334 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001335 size = libc_send (fd, buf, n, flags);
1336 }
1337
Dave Wallace048b1d62018-01-03 22:24:41 -05001338 return size;
1339}
1340
1341ssize_t
1342sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1343{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001344 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001345 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001346 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001347
Dave Wallace2a865272018-02-07 21:00:42 -05001348 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001349 return -1;
1350
Florin Coras7baeb712019-01-04 17:05:43 -08001351 vlsh = ldp_fd_to_vlsh (out_fd);
1352 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001353 {
1354 int rv;
1355 ssize_t results = 0;
1356 size_t n_bytes_left = len;
1357 size_t bytes_to_read;
1358 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001359 u8 eagain = 0;
1360 u32 flags, flags_len = sizeof (flags);
1361
Florin Coras7baeb712019-01-04 17:05:43 -08001362 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001363 if (PREDICT_FALSE (rv != VPPCOM_OK))
1364 {
Florin Coras7baeb712019-01-04 17:05:43 -08001365 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1366 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001367
Florin Corasdfe4cf42018-11-28 22:13:45 -08001368 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001369 errno = -rv;
1370 size = -1;
1371 goto done;
1372 }
1373
1374 if (offset)
1375 {
1376 off_t off = lseek (in_fd, *offset, SEEK_SET);
1377 if (PREDICT_FALSE (off == -1))
1378 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001379 size = -1;
1380 goto done;
1381 }
1382
1383 ASSERT (off == *offset);
1384 }
1385
1386 do
1387 {
Florin Coras7baeb712019-01-04 17:05:43 -08001388 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001389 if (size < 0)
1390 {
Florin Coras7baeb712019-01-04 17:05:43 -08001391 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1392 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001393 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001394 errno = -size;
1395 size = -1;
1396 goto done;
1397 }
1398
1399 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001400 if (bytes_to_read == 0)
1401 {
1402 if (flags & O_NONBLOCK)
1403 {
1404 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001405 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001406 goto update_offset;
1407 }
1408 else
1409 continue;
1410 }
1411 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001412 vec_validate (ldpw->io_buffer, bytes_to_read);
1413 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001414 if (nbytes < 0)
1415 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001416 if (results == 0)
1417 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001418 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001419 size = -1;
1420 goto done;
1421 }
1422 goto update_offset;
1423 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001424
Florin Coras7baeb712019-01-04 17:05:43 -08001425 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001426 if (size < 0)
1427 {
1428 if (size == VPPCOM_EAGAIN)
1429 {
1430 if (flags & O_NONBLOCK)
1431 {
1432 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001433 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001434 goto update_offset;
1435 }
1436 else
1437 continue;
1438 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001439 if (results == 0)
1440 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001441 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001442 errno = -size;
1443 size = -1;
1444 goto done;
1445 }
1446 goto update_offset;
1447 }
1448
1449 results += nbytes;
1450 ASSERT (n_bytes_left >= nbytes);
1451 n_bytes_left = n_bytes_left - nbytes;
1452 }
1453 while (n_bytes_left > 0);
1454
1455 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001456 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001457 if (offset)
1458 {
1459 off_t off = lseek (in_fd, *offset, SEEK_SET);
1460 if (PREDICT_FALSE (off == -1))
1461 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001462 size = -1;
1463 goto done;
1464 }
1465
1466 ASSERT (off == *offset);
1467 *offset += results + 1;
1468 }
1469 if (eagain)
1470 {
1471 errno = EAGAIN;
1472 size = -1;
1473 }
1474 else
1475 size = results;
1476 }
1477 else
1478 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001479 size = libc_sendfile (out_fd, in_fd, offset, len);
1480 }
1481
1482done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001483 return size;
1484}
1485
1486ssize_t
1487sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1488{
1489 return sendfile (out_fd, in_fd, offset, len);
1490}
1491
1492ssize_t
1493recv (int fd, void *buf, size_t n, int flags)
1494{
Florin Coras7baeb712019-01-04 17:05:43 -08001495 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001496 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001497
Dave Wallace2a865272018-02-07 21:00:42 -05001498 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001499 return -1;
1500
Florin Coras7baeb712019-01-04 17:05:43 -08001501 vlsh = ldp_fd_to_vlsh (fd);
1502 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001503 {
Florin Coras7baeb712019-01-04 17:05:43 -08001504 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001505 if (size < 0)
Florin Coras2a6642e2020-03-24 15:24:29 +00001506 {
1507 errno = -size;
1508 size = -1;
1509 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001510 }
1511 else
1512 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001513 size = libc_recv (fd, buf, n, flags);
1514 }
1515
Dave Wallace048b1d62018-01-03 22:24:41 -05001516 return size;
1517}
1518
1519ssize_t
1520sendto (int fd, const void *buf, size_t n, int flags,
1521 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1522{
Florin Coras7baeb712019-01-04 17:05:43 -08001523 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001524 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001525
Dave Wallace2a865272018-02-07 21:00:42 -05001526 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001527 return -1;
1528
Florin Coras7baeb712019-01-04 17:05:43 -08001529 vlsh = ldp_fd_to_vlsh (fd);
1530 if (vlsh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001531 {
1532 vppcom_endpt_t *ep = 0;
1533 vppcom_endpt_t _ep;
1534
1535 if (addr)
1536 {
1537 ep = &_ep;
Dave Wallace048b1d62018-01-03 22:24:41 -05001538 switch (addr->sa_family)
1539 {
1540 case AF_INET:
1541 ep->is_ip4 = VPPCOM_IS_IP4;
1542 ep->ip =
1543 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1544 ep->port =
1545 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1546 break;
1547
1548 case AF_INET6:
1549 ep->is_ip4 = VPPCOM_IS_IP6;
1550 ep->ip =
1551 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1552 ep->port =
1553 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1554 break;
1555
1556 default:
1557 errno = EAFNOSUPPORT;
1558 size = -1;
1559 goto done;
1560 }
1561 }
1562
Florin Coras7baeb712019-01-04 17:05:43 -08001563 size = vls_sendto (vlsh, (void *) buf, n, flags, ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001564 if (size < 0)
1565 {
1566 errno = -size;
1567 size = -1;
1568 }
1569 }
1570 else
1571 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001572 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1573 }
1574
1575done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001576 return size;
1577}
1578
1579ssize_t
1580recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1581 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1582{
Florin Coras7baeb712019-01-04 17:05:43 -08001583 vls_handle_t sid;
Haggai Eran46ae2ef2019-02-19 15:04:42 +02001584 ssize_t size, rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001585
Dave Wallace2a865272018-02-07 21:00:42 -05001586 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001587 return -1;
1588
Florin Coras7baeb712019-01-04 17:05:43 -08001589 sid = ldp_fd_to_vlsh (fd);
1590 if (sid != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001591 {
1592 vppcom_endpt_t ep;
1593 u8 src_addr[sizeof (struct sockaddr_in6)];
1594
Dave Wallace048b1d62018-01-03 22:24:41 -05001595 if (addr)
1596 {
1597 ep.ip = src_addr;
Florin Coras7baeb712019-01-04 17:05:43 -08001598 size = vls_recvfrom (sid, buf, n, flags, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001599
1600 if (size > 0)
Haggai Eran46ae2ef2019-02-19 15:04:42 +02001601 {
1602 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1603 if (rv < 0)
1604 size = rv;
1605 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001606 }
1607 else
Florin Coras7baeb712019-01-04 17:05:43 -08001608 size = vls_recvfrom (sid, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001609
1610 if (size < 0)
1611 {
1612 errno = -size;
1613 size = -1;
1614 }
1615 }
1616 else
1617 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001618 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1619 }
1620
Dave Wallace048b1d62018-01-03 22:24:41 -05001621 return size;
1622}
1623
1624ssize_t
1625sendmsg (int fd, const struct msghdr * message, int flags)
1626{
Florin Coras7baeb712019-01-04 17:05:43 -08001627 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001628 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001629
Dave Wallace2a865272018-02-07 21:00:42 -05001630 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001631 return -1;
1632
Florin Coras7baeb712019-01-04 17:05:43 -08001633 vlsh = ldp_fd_to_vlsh (fd);
1634 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001635 {
Florin Coras7baeb712019-01-04 17:05:43 -08001636 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001637 errno = ENOSYS;
1638 size = -1;
1639 }
1640 else
1641 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001642 size = libc_sendmsg (fd, message, flags);
1643 }
1644
Dave Wallace048b1d62018-01-03 22:24:41 -05001645 return size;
1646}
1647
1648#ifdef USE_GNU
1649int
1650sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1651{
1652 ssize_t size;
1653 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001654 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001655
Dave Wallace2a865272018-02-07 21:00:42 -05001656 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001657 return -1;
1658
Florin Coras7baeb712019-01-04 17:05:43 -08001659 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001660 {
1661 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1662 errno = ENOSYS;
1663 size = -1;
1664 }
1665 else
1666 {
1667 func_str = "libc_sendmmsg";
1668
Dave Wallace2a865272018-02-07 21:00:42 -05001669 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001670 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1671 "vmessages %p, vlen %u, flags 0x%x",
1672 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1673
1674 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1675 }
1676
Dave Wallace2a865272018-02-07 21:00:42 -05001677 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001678 {
1679 if (size < 0)
1680 {
1681 int errno_val = errno;
1682 perror (func_str);
1683 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1684 "rv %d, errno = %d", getpid (), fd, fd,
1685 func_str, size, errno_val);
1686 errno = errno_val;
1687 }
1688 else
1689 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1690 getpid (), fd, fd, size, size);
1691 }
1692 return size;
1693}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001694#endif
1695
Dave Wallace048b1d62018-01-03 22:24:41 -05001696ssize_t
1697recvmsg (int fd, struct msghdr * message, int flags)
1698{
Florin Coras7baeb712019-01-04 17:05:43 -08001699 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001700 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001701
Dave Wallace2a865272018-02-07 21:00:42 -05001702 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001703 return -1;
1704
Florin Coras7baeb712019-01-04 17:05:43 -08001705 vlsh = ldp_fd_to_vlsh (fd);
1706 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001707 {
Florin Coras7baeb712019-01-04 17:05:43 -08001708 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001709 errno = ENOSYS;
1710 size = -1;
1711 }
1712 else
1713 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001714 size = libc_recvmsg (fd, message, flags);
1715 }
1716
Dave Wallace048b1d62018-01-03 22:24:41 -05001717 return size;
1718}
1719
1720#ifdef USE_GNU
1721int
1722recvmmsg (int fd, struct mmsghdr *vmessages,
1723 unsigned int vlen, int flags, struct timespec *tmo)
1724{
1725 ssize_t size;
1726 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001727 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001728
Dave Wallace2a865272018-02-07 21:00:42 -05001729 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001730 return -1;
1731
Florin Coras7baeb712019-01-04 17:05:43 -08001732 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001733 {
1734 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1735 errno = ENOSYS;
1736 size = -1;
1737 }
1738 else
1739 {
1740 func_str = "libc_recvmmsg";
1741
Dave Wallace2a865272018-02-07 21:00:42 -05001742 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001743 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1744 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1745 getpid (), fd, fd, func_str, vmessages, vlen,
1746 flags, tmo);
1747
1748 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1749 }
1750
Dave Wallace2a865272018-02-07 21:00:42 -05001751 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001752 {
1753 if (size < 0)
1754 {
1755 int errno_val = errno;
1756 perror (func_str);
1757 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1758 "rv %d, errno = %d", getpid (), fd, fd,
1759 func_str, size, errno_val);
1760 errno = errno_val;
1761 }
1762 else
1763 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1764 getpid (), fd, fd, size, size);
1765 }
1766 return size;
1767}
1768#endif
1769
1770int
1771getsockopt (int fd, int level, int optname,
1772 void *__restrict optval, socklen_t * __restrict optlen)
1773{
Florin Coras7baeb712019-01-04 17:05:43 -08001774 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001775 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001776
Dave Wallace2a865272018-02-07 21:00:42 -05001777 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001778 return -1;
1779
Florin Coras7baeb712019-01-04 17:05:43 -08001780 vlsh = ldp_fd_to_vlsh (fd);
1781 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001782 {
1783 rv = -EOPNOTSUPP;
1784
1785 switch (level)
1786 {
1787 case SOL_TCP:
1788 switch (optname)
1789 {
1790 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001791 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1792 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001793 break;
1794 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001795 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1796 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001797 break;
1798 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001799 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1800 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001801 break;
1802 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001803 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1804 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001805 break;
1806 case TCP_INFO:
1807 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1808 {
Florin Coras7baeb712019-01-04 17:05:43 -08001809 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1810 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001811 memset (optval, 0, *optlen);
1812 rv = VPPCOM_OK;
1813 }
1814 else
1815 rv = -EFAULT;
1816 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001817 case TCP_CONGESTION:
Florin Coras0ed24e92019-01-21 09:03:10 -08001818 *optlen = strlen ("cubic");
Dave Barach02500902020-04-04 18:34:41 -04001819 strncpy (optval, "cubic", *optlen + 1);
Florin Coras0ed24e92019-01-21 09:03:10 -08001820 rv = 0;
1821 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001822 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001823 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1824 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001825 break;
1826 }
1827 break;
1828 case SOL_IPV6:
1829 switch (optname)
1830 {
1831 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001832 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001833 break;
1834 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001835 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1836 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001837 break;
1838 }
1839 break;
1840 case SOL_SOCKET:
1841 switch (optname)
1842 {
1843 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001844 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001845 break;
1846 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001847 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001848 break;
1849 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001850 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001851 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1852 break;
1853 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001854 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1855 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001856 break;
1857 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001858 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1859 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001860 break;
1861 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001862 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001863 break;
1864 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001865 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001866 break;
1867 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001868 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001869 break;
1870 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001871 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1872 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001873 break;
1874 }
1875 break;
1876 default:
1877 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001878 }
1879
Dave Wallace048b1d62018-01-03 22:24:41 -05001880 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001881 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001882 errno = -rv;
1883 rv = -1;
1884 }
1885 }
1886 else
1887 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001888 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1889 }
1890
Dave Wallace048b1d62018-01-03 22:24:41 -05001891 return rv;
1892}
1893
1894int
1895setsockopt (int fd, int level, int optname,
1896 const void *optval, socklen_t optlen)
1897{
Florin Coras7baeb712019-01-04 17:05:43 -08001898 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001899 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001900
Dave Wallace2a865272018-02-07 21:00:42 -05001901 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001902 return -1;
1903
Florin Coras7baeb712019-01-04 17:05:43 -08001904 vlsh = ldp_fd_to_vlsh (fd);
1905 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001906 {
1907 rv = -EOPNOTSUPP;
1908
1909 switch (level)
1910 {
1911 case SOL_TCP:
1912 switch (optname)
1913 {
1914 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001915 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1916 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001917 break;
1918 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001919 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1920 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001921 break;
1922 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001923 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1924 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001925 break;
1926 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001927 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
1928 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001929 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001930 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07001931 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08001932 /* Ignore */
1933 rv = 0;
1934 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001935 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001936 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
1937 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001938 break;
1939 }
1940 break;
1941 case SOL_IPV6:
1942 switch (optname)
1943 {
1944 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001945 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
1946 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001947 break;
1948 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001949 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
1950 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001951 break;
1952 }
1953 break;
1954 case SOL_SOCKET:
1955 switch (optname)
1956 {
1957 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001958 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
1959 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001960 break;
1961 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001962 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
1963 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001964 break;
1965 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001966 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
1967 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001968 break;
1969 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001970 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
1971 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 break;
1973 }
1974 break;
1975 default:
1976 break;
1977 }
1978
1979 if (rv != VPPCOM_OK)
1980 {
1981 errno = -rv;
1982 rv = -1;
1983 }
1984 }
1985 else
1986 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001987 rv = libc_setsockopt (fd, level, optname, optval, optlen);
1988 }
1989
Dave Wallace048b1d62018-01-03 22:24:41 -05001990 return rv;
1991}
1992
1993int
1994listen (int fd, int n)
1995{
Florin Coras7baeb712019-01-04 17:05:43 -08001996 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001997 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001998
Dave Wallace2a865272018-02-07 21:00:42 -05001999 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002000 return -1;
2001
Florin Coras7baeb712019-01-04 17:05:43 -08002002 vlsh = ldp_fd_to_vlsh (fd);
2003 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002004 {
Florin Coras7baeb712019-01-04 17:05:43 -08002005 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002006
Florin Coras7baeb712019-01-04 17:05:43 -08002007 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002008 if (rv != VPPCOM_OK)
2009 {
2010 errno = -rv;
2011 rv = -1;
2012 }
2013 }
2014 else
2015 {
Florin Coras7baeb712019-01-04 17:05:43 -08002016 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002017 rv = libc_listen (fd, n);
2018 }
2019
Florin Coras7baeb712019-01-04 17:05:43 -08002020 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002021 return rv;
2022}
2023
2024static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002025ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2026 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002027{
Florin Coras7baeb712019-01-04 17:05:43 -08002028 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002029 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002030
Dave Wallace2a865272018-02-07 21:00:42 -05002031 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002032 return -1;
2033
Florin Coras7baeb712019-01-04 17:05:43 -08002034 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2035 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002036 {
2037 vppcom_endpt_t ep;
2038 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002039 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002040 ep.ip = src_addr;
2041
Florin Coras7baeb712019-01-04 17:05:43 -08002042 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2043 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002044
Florin Coras7baeb712019-01-04 17:05:43 -08002045 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2046 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002047 {
Florin Coras7baeb712019-01-04 17:05:43 -08002048 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002049 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002050 }
2051 else
2052 {
Dave Wallace2a865272018-02-07 21:00:42 -05002053 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002054 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002055 {
Florin Coras7baeb712019-01-04 17:05:43 -08002056 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 errno = -rv;
2058 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002059 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002060 else
2061 {
Florin Coras7baeb712019-01-04 17:05:43 -08002062 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002063 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002064 }
2065 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002066 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002067 {
Florin Coras7baeb712019-01-04 17:05:43 -08002068 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2069 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002070
Dave Wallace048b1d62018-01-03 22:24:41 -05002071 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002072 }
2073
Florin Coras7baeb712019-01-04 17:05:43 -08002074 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002075
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002076 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002077}
2078
Dave Wallace048b1d62018-01-03 22:24:41 -05002079int
2080accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2081 int flags)
2082{
Dave Wallace2a865272018-02-07 21:00:42 -05002083 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002084}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002085
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002086int
Dave Wallace048b1d62018-01-03 22:24:41 -05002087accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002088{
Dave Wallace2a865272018-02-07 21:00:42 -05002089 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002090}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002091
Dave Wallace048b1d62018-01-03 22:24:41 -05002092int
2093shutdown (int fd, int how)
2094{
Florin Coras7baeb712019-01-04 17:05:43 -08002095 vls_handle_t vlsh;
2096 int rv = 0, flags;
2097 u32 flags_len = sizeof (flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002098
Dave Wallace2a865272018-02-07 21:00:42 -05002099 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002100 return -1;
2101
Florin Coras7baeb712019-01-04 17:05:43 -08002102 vlsh = ldp_fd_to_vlsh (fd);
2103 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002104 {
Florin Coras7baeb712019-01-04 17:05:43 -08002105 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
Florin Corasa7a1a222018-12-30 17:11:31 -08002106
Florin Coras7baeb712019-01-04 17:05:43 -08002107 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
Florin Corasa7a1a222018-12-30 17:11:31 -08002108 {
Florin Coras7baeb712019-01-04 17:05:43 -08002109 close (fd);
Florin Corasa7a1a222018-12-30 17:11:31 -08002110 return -1;
2111 }
2112
Florin Coras7baeb712019-01-04 17:05:43 -08002113 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2114 {
2115 close (fd);
2116 return -1;
2117 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002118
Florin Coras7baeb712019-01-04 17:05:43 -08002119 if (flags == SHUT_RDWR)
Florin Corasa7a1a222018-12-30 17:11:31 -08002120 rv = close (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 }
2122 else
2123 {
Florin Coras7baeb712019-01-04 17:05:43 -08002124 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 rv = libc_shutdown (fd, how);
2126 }
2127
Dave Wallace048b1d62018-01-03 22:24:41 -05002128 return rv;
2129}
2130
2131int
2132epoll_create1 (int flags)
2133{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002134 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002135 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002136 int rv;
2137
Dave Wallace2a865272018-02-07 21:00:42 -05002138 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002139 return -1;
2140
Florin Coras99368312018-08-02 10:45:44 -07002141 if (ldp->vcl_needs_real_epoll)
2142 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002143 /* Make sure workers have been allocated */
2144 if (!ldp->workers)
2145 {
2146 ldp_alloc_workers ();
2147 ldpw = ldp_worker_get_current ();
2148 }
Florin Coras99368312018-08-02 10:45:44 -07002149 rv = libc_epoll_create1 (flags);
2150 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002151 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002152 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002153 return rv;
2154 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002155
Florin Coras7baeb712019-01-04 17:05:43 -08002156 vlsh = vls_epoll_create ();
2157 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002158 {
Florin Coras7baeb712019-01-04 17:05:43 -08002159 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002160 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002161 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002162 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002163 {
Florin Coras7baeb712019-01-04 17:05:43 -08002164 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002165 }
Florin Coras7baeb712019-01-04 17:05:43 -08002166 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002167 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002168}
2169
2170int
Dave Wallace048b1d62018-01-03 22:24:41 -05002171epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002172{
Dave Wallace048b1d62018-01-03 22:24:41 -05002173 return epoll_create1 (0);
2174}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002175
Dave Wallace048b1d62018-01-03 22:24:41 -05002176int
2177epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2178{
Florin Coras7baeb712019-01-04 17:05:43 -08002179 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002180 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002181
Dave Wallace2a865272018-02-07 21:00:42 -05002182 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002183 return -1;
2184
Florin Coras7baeb712019-01-04 17:05:43 -08002185 vep_vlsh = ldp_fd_to_vlsh (epfd);
2186 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002187 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002188 /* The LDP epoll_create1 always creates VCL epfd's.
2189 * The app should never have a kernel base epoll fd unless it
2190 * was acquired outside of the LD_PRELOAD process context.
2191 * In any case, if we get one, punt it to libc_epoll_ctl.
2192 */
Florin Coras7baeb712019-01-04 17:05:43 -08002193 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2194 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002195
2196 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002197 goto done;
2198 }
2199
Florin Coras7baeb712019-01-04 17:05:43 -08002200 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002201
Florin Coras7baeb712019-01-04 17:05:43 -08002202 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2203 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002204
Florin Coras7baeb712019-01-04 17:05:43 -08002205 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002206 {
Florin Coras7baeb712019-01-04 17:05:43 -08002207 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2208 " event %p", epfd, vep_vlsh, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002209
Florin Coras7baeb712019-01-04 17:05:43 -08002210 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002211 if (rv != VPPCOM_OK)
2212 {
2213 errno = -rv;
2214 rv = -1;
2215 }
2216 }
2217 else
2218 {
2219 int libc_epfd;
2220 u32 size = sizeof (epfd);
2221
Florin Coras7baeb712019-01-04 17:05:43 -08002222 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002223 if (!libc_epfd)
2224 {
Florin Coras7baeb712019-01-04 17:05:43 -08002225 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2226 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002227
2228 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2229 if (libc_epfd < 0)
2230 {
2231 rv = libc_epfd;
2232 goto done;
2233 }
2234
Florin Coras7baeb712019-01-04 17:05:43 -08002235 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2236 &size);
Florin Coras99368312018-08-02 10:45:44 -07002237 if (rv < 0)
2238 {
2239 errno = -rv;
2240 rv = -1;
2241 goto done;
2242 }
2243 }
2244 else if (PREDICT_FALSE (libc_epfd < 0))
2245 {
2246 errno = -epfd;
2247 rv = -1;
2248 goto done;
2249 }
2250
Florin Coras7baeb712019-01-04 17:05:43 -08002251 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2252 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002253
2254 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002255 }
2256
2257done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002258 return rv;
2259}
Dave Wallace048b1d62018-01-03 22:24:41 -05002260
2261static inline int
Florin Coras99368312018-08-02 10:45:44 -07002262ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2263 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002264{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002265 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002266 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002267 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002268 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002269
Dave Wallace2a865272018-02-07 21:00:42 -05002270 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002271 return -1;
2272
2273 if (PREDICT_FALSE (!events || (timeout < -1)))
2274 {
2275 errno = EFAULT;
2276 return -1;
2277 }
2278
Florin Corasdfe4cf42018-11-28 22:13:45 -08002279 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002280 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2281
Florin Coras7baeb712019-01-04 17:05:43 -08002282 ep_vlsh = ldp_fd_to_vlsh (epfd);
2283 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002284 {
Florin Coras7baeb712019-01-04 17:05:43 -08002285 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002286 errno = EBADFD;
2287 return -1;
2288 }
2289
Florin Coras4dee8cd2019-01-29 21:28:16 -08002290 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2291 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002292 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002293 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002294
Florin Coras7baeb712019-01-04 17:05:43 -08002295 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002296 if (PREDICT_FALSE (libc_epfd < 0))
2297 {
2298 errno = -libc_epfd;
2299 rv = -1;
2300 goto done;
2301 }
2302
Florin Coras7baeb712019-01-04 17:05:43 -08002303 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2304 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002305 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002306 do
2307 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002308 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002309 {
Florin Coras7baeb712019-01-04 17:05:43 -08002310 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002311 if (rv > 0)
2312 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002313 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002314 goto done;
2315 }
2316 else if (rv < 0)
2317 {
2318 errno = -rv;
2319 rv = -1;
2320 goto done;
2321 }
2322 }
2323 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002324 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002325
2326 if (libc_epfd > 0)
2327 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002328 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002329 if (rv != 0)
2330 goto done;
2331 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002332 }
Florin Coras72f77822019-01-22 19:05:52 -08002333 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002334
2335done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002336 return rv;
2337}
2338
2339int
2340epoll_pwait (int epfd, struct epoll_event *events,
2341 int maxevents, int timeout, const sigset_t * sigmask)
2342{
Dave Wallace2a865272018-02-07 21:00:42 -05002343 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002344}
2345
2346int
2347epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2348{
Dave Wallace2a865272018-02-07 21:00:42 -05002349 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002350}
2351
2352int
2353poll (struct pollfd *fds, nfds_t nfds, int timeout)
2354{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002355 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002356 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002357 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002358 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002359 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002360
Florin Coras05ecfcc2018-12-12 18:19:39 -08002361 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002362
Florin Coras4dee8cd2019-01-29 21:28:16 -08002363 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2364 clib_time_init (&ldpw->clib_time);
2365
2366 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2367 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002368
Dave Wallace048b1d62018-01-03 22:24:41 -05002369 for (i = 0; i < nfds; i++)
2370 {
Florin Coras6917b942018-11-13 22:44:54 -08002371 if (fds[i].fd < 0)
2372 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002373
Florin Coras7baeb712019-01-04 17:05:43 -08002374 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2375 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002376 {
2377 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002378 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002379 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002380 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002381 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002382#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002383 if (fds[i].events & POLLRDNORM)
2384 vp->events |= POLLIN;
2385 if (fds[i].events & POLLWRNORM)
2386 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002387#endif
Florin Coras6917b942018-11-13 22:44:54 -08002388 vp->revents = fds[i].revents;
2389 }
2390 else
2391 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002392 vec_add1 (ldpw->libc_poll, fds[i]);
2393 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002394 }
2395 }
2396
Dave Wallace048b1d62018-01-03 22:24:41 -05002397 do
2398 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002399 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002400 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002401 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002402 if (rv < 0)
2403 {
2404 errno = -rv;
2405 rv = -1;
2406 goto done;
2407 }
2408 else
2409 n_revents += rv;
2410 }
2411
Florin Corasdfe4cf42018-11-28 22:13:45 -08002412 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002413 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002414 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002415 if (rv < 0)
2416 goto done;
2417 else
2418 n_revents += rv;
2419 }
2420
2421 if (n_revents)
2422 {
2423 rv = n_revents;
2424 goto done;
2425 }
2426 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002427 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002428 rv = 0;
2429
2430done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002431 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002432 {
2433 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002434 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002435#ifdef __USE_XOPEN2K
2436 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2437 (fds[vp->fds_ndx].events & POLLRDNORM))
2438 fds[vp->fds_ndx].revents |= POLLRDNORM;
2439 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2440 (fds[vp->fds_ndx].events & POLLWRNORM))
2441 fds[vp->fds_ndx].revents |= POLLWRNORM;
2442#endif
2443 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002444 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002445
Florin Corasdfe4cf42018-11-28 22:13:45 -08002446 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002447 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002448 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002449 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002450 vec_reset_length (ldpw->libc_poll_idxs);
2451 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002452
Dave Wallace048b1d62018-01-03 22:24:41 -05002453 return rv;
2454}
2455
2456#ifdef USE_GNU
2457int
2458ppoll (struct pollfd *fds, nfds_t nfds,
2459 const struct timespec *timeout, const sigset_t * sigmask)
2460{
Dave Wallace2a865272018-02-07 21:00:42 -05002461 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002462 return -1;
2463
2464 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2465 errno = ENOSYS;
2466
2467
2468 return -1;
2469}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002470#endif
2471
Dave Wallace2a865272018-02-07 21:00:42 -05002472void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002473
Dave Wallace2a865272018-02-07 21:00:42 -05002474void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002475
Dave Wallace048b1d62018-01-03 22:24:41 -05002476/*
2477 * This function is called when the library is loaded
2478 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002479void
Dave Wallace2a865272018-02-07 21:00:42 -05002480ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002481{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002482 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002483 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002484 {
2485 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2486 getpid ());
2487 _exit (1);
2488 }
Dave Wallace69d01192018-02-22 16:22:09 -05002489 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002490 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002491}
2492
2493/*
2494 * This function is called when the library is unloaded
2495 */
2496void
Dave Wallace2a865272018-02-07 21:00:42 -05002497ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002498{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002499 /*
2500 swrap_destructor ();
2501 if (ldp->init)
2502 ldp->init = 0;
2503 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002504
2505 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002506 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002507 */
Dave Wallace69d01192018-02-22 16:22:09 -05002508 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002509 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2510 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002511}
2512
2513
2514/*
2515 * fd.io coding-style-patch-verification: ON
2516 *
2517 * Local Variables:
2518 * eval: (c-set-style "gnu")
2519 * End:
2520 */