blob: 95c0edcff0ab4b6a4a1c4272d1b8181516996388 [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 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500451 do
452 {
453 for (i = 0; i < iovcnt; ++i)
454 {
Florin Coras7baeb712019-01-04 17:05:43 -0800455 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
Dave Wallace048b1d62018-01-03 22:24:41 -0500456 if (rv < 0)
457 break;
458 else
459 {
460 total += rv;
461 if (rv < iov[i].iov_len)
Florin Corasb0f662f2018-12-27 14:51:46 -0800462 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500463 }
464 }
465 }
466 while ((rv >= 0) && (total == 0));
467
468 if (rv < 0)
469 {
470 errno = -rv;
471 size = -1;
472 }
473 else
474 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700475 }
476 else
477 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500478 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700479 }
480
Dave Wallace048b1d62018-01-03 22:24:41 -0500481 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700482}
483
Carl Smithe16707b2019-11-13 14:37:39 +1300484#ifdef HAVE_FCNTL64
485int
486fcntl64 (int fd, int cmd, ...)
487#else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700488int
Dave Wallace048b1d62018-01-03 22:24:41 -0500489fcntl (int fd, int cmd, ...)
Carl Smithe16707b2019-11-13 14:37:39 +1300490#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700491{
Florin Coras7baeb712019-01-04 17:05:43 -0800492 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700493 int rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500494 va_list ap;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700495
Dave Wallace2a865272018-02-07 21:00:42 -0500496 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500497 return -1;
498
499 va_start (ap, cmd);
Florin Coras7baeb712019-01-04 17:05:43 -0800500
501 vlsh = ldp_fd_to_vlsh (fd);
502 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
503 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700504 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500505 int flags = va_arg (ap, int);
506 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700507
Dave Wallace048b1d62018-01-03 22:24:41 -0500508 size = sizeof (flags);
509 rv = -EOPNOTSUPP;
510 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700511 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500512 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800513 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500514 break;
515
516 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800517 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500518 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800519 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700520 break;
Florin Coras173bae32018-11-16 18:56:28 -0800521 case F_SETFD:
522 /* TODO handle this */
523 LDBG (0, "F_SETFD ignored flags %u", flags);
524 rv = 0;
525 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700526 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500527 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700528 break;
529 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500530 if (rv < 0)
531 {
532 errno = -rv;
533 rv = -1;
534 }
535 }
536 else
537 {
Carl Smithe16707b2019-11-13 14:37:39 +1300538#ifdef HAVE_FCNTL64
539 rv = libc_vfcntl64 (fd, cmd, ap);
540#else
Dave Wallace048b1d62018-01-03 22:24:41 -0500541 rv = libc_vfcntl (fd, cmd, ap);
Carl Smithe16707b2019-11-13 14:37:39 +1300542#endif
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700543 }
544
Dave Wallace048b1d62018-01-03 22:24:41 -0500545 va_end (ap);
546
Dave Wallace048b1d62018-01-03 22:24:41 -0500547 return rv;
548}
549
550int
551ioctl (int fd, unsigned long int cmd, ...)
552{
Florin Coras7baeb712019-01-04 17:05:43 -0800553 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500554 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800555 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500556
Dave Wallace2a865272018-02-07 21:00:42 -0500557 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500558 return -1;
559
560 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500561
Florin Coras7baeb712019-01-04 17:05:43 -0800562 vlsh = ldp_fd_to_vlsh (fd);
563 if (vlsh != VLS_INVALID_HANDLE)
564 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500565 switch (cmd)
566 {
567 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800568 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500569 break;
570
571 case FIONBIO:
572 {
573 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
574 u32 size = sizeof (flags);
575
576 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
577 * non-blocking, the flags should be read here and merged
578 * with O_NONBLOCK.
579 */
Florin Coras7baeb712019-01-04 17:05:43 -0800580 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500581 }
582 break;
583
584 default:
585 rv = -EOPNOTSUPP;
586 break;
587 }
588 if (rv < 0)
589 {
590 errno = -rv;
591 rv = -1;
592 }
593 }
594 else
595 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500596 rv = libc_vioctl (fd, cmd, ap);
597 }
598
Dave Wallace048b1d62018-01-03 22:24:41 -0500599 va_end (ap);
600 return rv;
601}
602
Florin Coras294afe22019-01-07 17:49:17 -0800603always_inline void
604ldp_select_init_maps (fd_set * __restrict original,
605 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
606 clib_bitmap_t ** vclb, int nfds, u32 minbits,
607 u32 n_bytes, uword * si_bits, uword * libc_bits)
608{
609 uword si_bits_set, libc_bits_set;
610 vls_handle_t vlsh;
611 int fd;
612
613 clib_bitmap_validate (*vclb, minbits);
614 clib_bitmap_validate (*libcb, minbits);
615 clib_bitmap_validate (*resultb, minbits);
616 clib_memcpy_fast (*resultb, original, n_bytes);
617 memset (original, 0, n_bytes);
618
619 /* *INDENT-OFF* */
620 clib_bitmap_foreach (fd, *resultb, ({
621 if (fd > nfds)
622 break;
623 vlsh = ldp_fd_to_vlsh (fd);
624 if (vlsh == VLS_INVALID_HANDLE)
625 clib_bitmap_set_no_check (*libcb, fd, 1);
626 else
627 clib_bitmap_set_no_check (*vclb, vlsh_to_session_index (vlsh), 1);
628 }));
629 /* *INDENT-ON* */
630
631 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
632 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
633
634 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
635 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
636}
637
638always_inline int
639ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
640{
641 vls_handle_t vlsh;
642 uword si;
643 int fd;
644
645 if (!libcb)
646 return 0;
647
648 /* *INDENT-OFF* */
649 clib_bitmap_foreach (si, vclb, ({
650 vlsh = vls_session_index_to_vlsh (si);
Florin Coras54140622020-02-04 19:04:34 +0000651 ASSERT (vlsh != VLS_INVALID_HANDLE);
Florin Coras294afe22019-01-07 17:49:17 -0800652 fd = ldp_vlsh_to_fd (vlsh);
653 if (PREDICT_FALSE (fd < 0))
654 {
655 errno = EBADFD;
656 return -1;
657 }
658 FD_SET (fd, libcb);
659 }));
660 /* *INDENT-ON* */
661
662 return 0;
663}
664
665always_inline void
666ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
667{
668 uword fd;
669
Florin Coras78b5fa62019-02-21 20:04:15 -0800670 if (!libcb)
671 return;
672
Florin Coras294afe22019-01-07 17:49:17 -0800673 /* *INDENT-OFF* */
674 clib_bitmap_foreach (fd, result, ({
675 FD_SET ((int)fd, libcb);
676 }));
677 /* *INDENT-ON* */
678}
679
Dave Wallace048b1d62018-01-03 22:24:41 -0500680int
Dave Wallace2a865272018-02-07 21:00:42 -0500681ldp_pselect (int nfds, fd_set * __restrict readfds,
682 fd_set * __restrict writefds,
683 fd_set * __restrict exceptfds,
684 const struct timespec *__restrict timeout,
685 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500686{
Florin Coras294afe22019-01-07 17:49:17 -0800687 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800688 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800689 struct timespec libc_tspec = { 0 };
690 f64 time_out, vcl_timeout = 0;
691 uword si_bits, libc_bits;
692 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500693
694 if (nfds < 0)
695 {
696 errno = EINVAL;
697 return -1;
698 }
699
Florin Coras4dee8cd2019-01-29 21:28:16 -0800700 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
701 clib_time_init (&ldpw->clib_time);
702
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500703 if (timeout)
704 {
705 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800706 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500707
708 /* select as fine grained sleep */
709 if (!nfds)
710 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800711 time_out += clib_time_now (&ldpw->clib_time);
712 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500713 ;
714 return 0;
715 }
716 }
717 else if (!nfds)
718 {
719 errno = EINVAL;
720 return -1;
721 }
722 else
723 time_out = -1;
724
Florin Coras7baeb712019-01-04 17:05:43 -0800725 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500726 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500727 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
728 timeout, sigmask);
729 goto done;
730 }
731
Florin Coras294afe22019-01-07 17:49:17 -0800732 si_bits = libc_bits = 0;
733 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800734
Dave Wallace048b1d62018-01-03 22:24:41 -0500735 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800736 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
737 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
738 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500739 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800740 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
741 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
742 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500743 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800744 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
745 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
746 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500747
Florin Coras294afe22019-01-07 17:49:17 -0800748 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500749 {
750 errno = EINVAL;
751 rv = -1;
752 goto done;
753 }
754
Florin Coras78b5fa62019-02-21 20:04:15 -0800755 if (!si_bits)
756 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800757
Dave Wallace048b1d62018-01-03 22:24:41 -0500758 do
759 {
Florin Coras294afe22019-01-07 17:49:17 -0800760 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500761 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500762 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800763 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corasdfe4cf42018-11-28 22:13:45 -0800764 vec_len (ldpw->rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500765 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500766 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800767 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corasdfe4cf42018-11-28 22:13:45 -0800768 vec_len (ldpw->wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500769 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500770 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800771 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corasdfe4cf42018-11-28 22:13:45 -0800772 vec_len (ldpw->ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500773 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800774
Florin Coras0ef8ef22019-01-18 08:37:13 -0800775 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
776 writefds ? ldpw->wr_bitmap : NULL,
777 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800778 if (rv < 0)
779 {
780 errno = -rv;
781 rv = -1;
782 }
783 else if (rv > 0)
784 {
785 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
786 {
787 rv = -1;
788 goto done;
789 }
790
791 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
792 {
793 rv = -1;
794 goto done;
795 }
796
797 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
798 {
799 rv = -1;
800 goto done;
801 }
802 bits_set = rv;
803 }
804 }
805 if (libc_bits)
806 {
807 if (readfds)
808 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
809 vec_len (ldpw->libc_rd_bitmap) *
810 sizeof (clib_bitmap_t));
811 if (writefds)
812 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
813 vec_len (ldpw->libc_wr_bitmap) *
814 sizeof (clib_bitmap_t));
815 if (exceptfds)
816 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
817 vec_len (ldpw->libc_ex_bitmap) *
818 sizeof (clib_bitmap_t));
819
Dave Wallace048b1d62018-01-03 22:24:41 -0500820 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800821 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
822 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
823 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
824 &libc_tspec, sigmask);
825 if (rv > 0)
826 {
827 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
828 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
829 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
830 bits_set += rv;
831 }
832 }
833
834 if (bits_set)
835 {
836 rv = bits_set;
837 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500838 }
839 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800840 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500841 rv = 0;
842
843done:
844 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800845 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800846 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800847 clib_bitmap_zero (ldpw->libc_rd_bitmap);
848 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800849 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800850 clib_bitmap_zero (ldpw->libc_wr_bitmap);
851 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800852 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800853 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500854
Dave Wallace048b1d62018-01-03 22:24:41 -0500855 return rv;
856}
857
858int
859select (int nfds, fd_set * __restrict readfds,
860 fd_set * __restrict writefds,
861 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
862{
863 struct timespec tspec;
864
865 if (timeout)
866 {
867 tspec.tv_sec = timeout->tv_sec;
868 tspec.tv_nsec = timeout->tv_usec * 1000;
869 }
Dave Wallace2a865272018-02-07 21:00:42 -0500870 return ldp_pselect (nfds, readfds, writefds, exceptfds,
871 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500872}
873
874#ifdef __USE_XOPEN2K
875int
876pselect (int nfds, fd_set * __restrict readfds,
877 fd_set * __restrict writefds,
878 fd_set * __restrict exceptfds,
879 const struct timespec *__restrict timeout,
880 const __sigset_t * __restrict sigmask)
881{
Dave Wallace2a865272018-02-07 21:00:42 -0500882 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500883}
884#endif
885
Yu Ping7b74b072019-05-08 00:40:24 +0800886/* If transparent TLS mode is turned on, then ldp will load key and cert.
887 */
888static int
889load_tls_cert (vls_handle_t vlsh)
890{
891 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
892 char inbuf[4096];
893 char *tls_cert;
894 int cert_size;
895 FILE *fp;
896
897 if (env_var_str)
898 {
899 fp = fopen (env_var_str, "r");
900 if (fp == NULL)
901 {
902 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
903 return -1;
904 }
905 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
906 tls_cert = inbuf;
907 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
908 cert_size);
909 fclose (fp);
910 }
911 else
912 {
913 LDBG (0, "ERROR: failed to read LDP environment %s\n",
914 LDP_ENV_TLS_CERT);
915 return -1;
916 }
917 return 0;
918}
919
920static int
921load_tls_key (vls_handle_t vlsh)
922{
923 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
924 char inbuf[4096];
925 char *tls_key;
926 int key_size;
927 FILE *fp;
928
929 if (env_var_str)
930 {
931 fp = fopen (env_var_str, "r");
932 if (fp == NULL)
933 {
934 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
935 return -1;
936 }
937 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
938 tls_key = inbuf;
939 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
940 key_size);
941 fclose (fp);
942 }
943 else
944 {
945 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
946 return -1;
947 }
948 return 0;
949}
950
Dave Wallace048b1d62018-01-03 22:24:41 -0500951int
952socket (int domain, int type, int protocol)
953{
Florin Coras7baeb712019-01-04 17:05:43 -0800954 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500955 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800956 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500957
Dave Wallace2a865272018-02-07 21:00:42 -0500958 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500959 return -1;
960
961 if (((domain == AF_INET) || (domain == AF_INET6)) &&
962 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
963 {
Yu Ping7b74b072019-05-08 00:40:24 +0800964 u8 proto;
965 if (ldp->transparent_tls)
966 {
967 proto = VPPCOM_PROTO_TLS;
968 }
969 else
970 proto = ((sock_type == SOCK_DGRAM) ?
971 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500972
Florin Coras7baeb712019-01-04 17:05:43 -0800973 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
974 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500975
Florin Coras7baeb712019-01-04 17:05:43 -0800976 vlsh = vls_create (proto, is_nonblocking);
977 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500978 {
Florin Coras7baeb712019-01-04 17:05:43 -0800979 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500980 rv = -1;
981 }
982 else
983 {
Yu Ping7b74b072019-05-08 00:40:24 +0800984 if (ldp->transparent_tls)
985 {
986 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
987 {
988 return -1;
989 }
990 }
Florin Coras7baeb712019-01-04 17:05:43 -0800991 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500992 }
993 }
994 else
995 {
Florin Coras7baeb712019-01-04 17:05:43 -0800996 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -0500997 rv = libc_socket (domain, type, protocol);
998 }
999
Dave Wallace048b1d62018-01-03 22:24:41 -05001000 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001001}
1002
1003/*
1004 * Create two new sockets, of type TYPE in domain DOMAIN and using
1005 * protocol PROTOCOL, which are connected to each other, and put file
1006 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1007 * one will be chosen automatically.
1008 * Returns 0 on success, -1 for errors.
1009 * */
1010int
Dave Wallace048b1d62018-01-03 22:24:41 -05001011socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001012{
Florin Coras7baeb712019-01-04 17:05:43 -08001013 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001014
Dave Wallace2a865272018-02-07 21:00:42 -05001015 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001016 return -1;
1017
1018 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1019 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001020 {
Florin Coras7baeb712019-01-04 17:05:43 -08001021 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001022 errno = ENOSYS;
1023 rv = -1;
1024 }
1025 else
1026 {
Florin Coras7baeb712019-01-04 17:05:43 -08001027 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001028 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001029 }
1030
Dave Wallace048b1d62018-01-03 22:24:41 -05001031 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001032}
1033
1034int
Dave Wallace048b1d62018-01-03 22:24:41 -05001035bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001036{
Florin Coras7baeb712019-01-04 17:05:43 -08001037 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001038 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001039
Dave Wallace2a865272018-02-07 21:00:42 -05001040 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001041 return -1;
1042
Florin Coras7baeb712019-01-04 17:05:43 -08001043 vlsh = ldp_fd_to_vlsh (fd);
1044 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001045 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001046 vppcom_endpt_t ep;
1047
Dave Wallace048b1d62018-01-03 22:24:41 -05001048 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001049 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001050 case AF_INET:
1051 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001052 {
Florin Coras7baeb712019-01-04 17:05:43 -08001053 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1054 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001055 errno = EINVAL;
1056 rv = -1;
1057 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001058 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001059 ep.is_ip4 = VPPCOM_IS_IP4;
1060 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1061 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1062 break;
1063
1064 case AF_INET6:
1065 if (len != sizeof (struct sockaddr_in6))
1066 {
Florin Coras7baeb712019-01-04 17:05:43 -08001067 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1068 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001069 errno = EINVAL;
1070 rv = -1;
1071 goto done;
1072 }
1073 ep.is_ip4 = VPPCOM_IS_IP6;
1074 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1075 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001076 break;
1077
1078 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001079 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1080 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001081 errno = EAFNOSUPPORT;
1082 rv = -1;
1083 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001084 }
Florin Coras7baeb712019-01-04 17:05:43 -08001085 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1086 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001087
Florin Coras7baeb712019-01-04 17:05:43 -08001088 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001089 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001090 {
1091 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001092 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001093 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001094 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001095 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001096 {
Florin Coras7baeb712019-01-04 17:05:43 -08001097 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001098 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001099 }
1100
Dave Wallace048b1d62018-01-03 22:24:41 -05001101done:
Florin Coras7baeb712019-01-04 17:05:43 -08001102 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001103
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001104 return rv;
1105}
1106
1107static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001108ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1109 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001110{
Dave Wallace048b1d62018-01-03 22:24:41 -05001111 int rv = 0;
1112 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001113
Dave Wallace2a865272018-02-07 21:00:42 -05001114 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001115 return -1;
1116
1117 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001118 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001119 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1120 switch (addr->sa_family)
1121 {
1122 case AF_INET:
1123 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1124 if (*len > sizeof (struct sockaddr_in))
1125 *len = sizeof (struct sockaddr_in);
1126 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1127 copy_len = *len - sa_len;
1128 if (copy_len > 0)
1129 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1130 copy_len);
1131 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001132
Dave Wallace048b1d62018-01-03 22:24:41 -05001133 case AF_INET6:
1134 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1135 if (*len > sizeof (struct sockaddr_in6))
1136 *len = sizeof (struct sockaddr_in6);
1137 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1138 copy_len = *len - sa_len;
1139 if (copy_len > 0)
1140 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1141 __in6_u.__u6_addr8, ep->ip, copy_len);
1142 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001143
Dave Wallace048b1d62018-01-03 22:24:41 -05001144 default:
1145 /* Not possible */
1146 rv = -EAFNOSUPPORT;
1147 break;
1148 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001149 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001150 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001151}
1152
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001153int
Dave Wallace048b1d62018-01-03 22:24:41 -05001154getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001155{
Florin Coras7baeb712019-01-04 17:05:43 -08001156 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001157 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001158
Dave Wallace2a865272018-02-07 21:00:42 -05001159 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001160 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001161
Florin Coras7baeb712019-01-04 17:05:43 -08001162 vlsh = ldp_fd_to_vlsh (fd);
1163 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001164 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001165 vppcom_endpt_t ep;
1166 u8 addr_buf[sizeof (struct in6_addr)];
1167 u32 size = sizeof (ep);
1168
1169 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001170
Florin Coras7baeb712019-01-04 17:05:43 -08001171 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001172 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001173 {
1174 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001175 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001176 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001177 else
1178 {
Dave Wallace2a865272018-02-07 21:00:42 -05001179 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001180 if (rv != VPPCOM_OK)
1181 {
1182 errno = -rv;
1183 rv = -1;
1184 }
1185 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001186 }
1187 else
1188 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001189 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001190 }
1191
Dave Wallace048b1d62018-01-03 22:24:41 -05001192 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001193}
1194
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001195int
Dave Wallace048b1d62018-01-03 22:24:41 -05001196connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001197{
Florin Coras7baeb712019-01-04 17:05:43 -08001198 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001199 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001200
Dave Wallace2a865272018-02-07 21:00:42 -05001201 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001202 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001203
Dave Wallace048b1d62018-01-03 22:24:41 -05001204 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001205 {
Florin Coras7baeb712019-01-04 17:05:43 -08001206 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001207 errno = EINVAL;
1208 rv = -1;
1209 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001210 }
1211
Florin Coras7baeb712019-01-04 17:05:43 -08001212 vlsh = ldp_fd_to_vlsh (fd);
1213 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001214 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001215 vppcom_endpt_t ep;
1216
Dave Wallace048b1d62018-01-03 22:24:41 -05001217 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001218 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001219 case AF_INET:
1220 if (len != sizeof (struct sockaddr_in))
1221 {
Florin Coras7baeb712019-01-04 17:05:43 -08001222 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1223 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001224 errno = EINVAL;
1225 rv = -1;
1226 goto done;
1227 }
1228 ep.is_ip4 = VPPCOM_IS_IP4;
1229 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1230 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1231 break;
1232
1233 case AF_INET6:
1234 if (len != sizeof (struct sockaddr_in6))
1235 {
Florin Coras7baeb712019-01-04 17:05:43 -08001236 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1237 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001238 errno = EINVAL;
1239 rv = -1;
1240 goto done;
1241 }
1242 ep.is_ip4 = VPPCOM_IS_IP6;
1243 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1244 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1245 break;
1246
1247 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001248 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1249 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001250 errno = EAFNOSUPPORT;
1251 rv = -1;
1252 goto done;
1253 }
Florin Coras7baeb712019-01-04 17:05:43 -08001254 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1255 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001256
Florin Coras7baeb712019-01-04 17:05:43 -08001257 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001258 if (rv != VPPCOM_OK)
1259 {
1260 errno = -rv;
1261 rv = -1;
1262 }
1263 }
1264 else
1265 {
Florin Coras7baeb712019-01-04 17:05:43 -08001266 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1267 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001268
1269 rv = libc_connect (fd, addr, len);
1270 }
1271
1272done:
Florin Coras7baeb712019-01-04 17:05:43 -08001273 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001274 return rv;
1275}
1276
1277int
1278getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1279{
Florin Coras7baeb712019-01-04 17:05:43 -08001280 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001281 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001282
Dave Wallace2a865272018-02-07 21:00:42 -05001283 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001284 return -1;
1285
Florin Coras7baeb712019-01-04 17:05:43 -08001286 vlsh = ldp_fd_to_vlsh (fd);
1287 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001288 {
1289 vppcom_endpt_t ep;
1290 u8 addr_buf[sizeof (struct in6_addr)];
1291 u32 size = sizeof (ep);
1292
1293 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001294 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001295 if (rv != VPPCOM_OK)
1296 {
1297 errno = -rv;
1298 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001299 }
1300 else
1301 {
Dave Wallace2a865272018-02-07 21:00:42 -05001302 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001303 if (rv != VPPCOM_OK)
1304 {
1305 errno = -rv;
1306 rv = -1;
1307 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001308 }
1309 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001310 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001311 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001312 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001313 }
1314
Dave Wallace048b1d62018-01-03 22:24:41 -05001315 return rv;
1316}
1317
1318ssize_t
1319send (int fd, const void *buf, size_t n, int flags)
1320{
Florin Coras7baeb712019-01-04 17:05:43 -08001321 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001322 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001323
Dave Wallace2a865272018-02-07 21:00:42 -05001324 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 return -1;
1326
Florin Coras7baeb712019-01-04 17:05:43 -08001327 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001328 {
Florin Coras7baeb712019-01-04 17:05:43 -08001329 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001330 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001331 {
1332 errno = -size;
1333 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001334 }
1335 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001336 else
1337 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001338 size = libc_send (fd, buf, n, flags);
1339 }
1340
Dave Wallace048b1d62018-01-03 22:24:41 -05001341 return size;
1342}
1343
1344ssize_t
1345sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1346{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001347 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001348 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001349 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001350
Dave Wallace2a865272018-02-07 21:00:42 -05001351 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001352 return -1;
1353
Florin Coras7baeb712019-01-04 17:05:43 -08001354 vlsh = ldp_fd_to_vlsh (out_fd);
1355 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001356 {
1357 int rv;
1358 ssize_t results = 0;
1359 size_t n_bytes_left = len;
1360 size_t bytes_to_read;
1361 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001362 u8 eagain = 0;
1363 u32 flags, flags_len = sizeof (flags);
1364
Florin Coras7baeb712019-01-04 17:05:43 -08001365 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001366 if (PREDICT_FALSE (rv != VPPCOM_OK))
1367 {
Florin Coras7baeb712019-01-04 17:05:43 -08001368 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1369 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001370
Florin Corasdfe4cf42018-11-28 22:13:45 -08001371 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001372 errno = -rv;
1373 size = -1;
1374 goto done;
1375 }
1376
1377 if (offset)
1378 {
1379 off_t off = lseek (in_fd, *offset, SEEK_SET);
1380 if (PREDICT_FALSE (off == -1))
1381 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001382 size = -1;
1383 goto done;
1384 }
1385
1386 ASSERT (off == *offset);
1387 }
1388
1389 do
1390 {
Florin Coras7baeb712019-01-04 17:05:43 -08001391 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001392 if (size < 0)
1393 {
Florin Coras7baeb712019-01-04 17:05:43 -08001394 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1395 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001396 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001397 errno = -size;
1398 size = -1;
1399 goto done;
1400 }
1401
1402 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001403 if (bytes_to_read == 0)
1404 {
1405 if (flags & O_NONBLOCK)
1406 {
1407 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001408 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 goto update_offset;
1410 }
1411 else
1412 continue;
1413 }
1414 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001415 vec_validate (ldpw->io_buffer, bytes_to_read);
1416 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001417 if (nbytes < 0)
1418 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001419 if (results == 0)
1420 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001421 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001422 size = -1;
1423 goto done;
1424 }
1425 goto update_offset;
1426 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001427
Florin Coras7baeb712019-01-04 17:05:43 -08001428 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001429 if (size < 0)
1430 {
1431 if (size == VPPCOM_EAGAIN)
1432 {
1433 if (flags & O_NONBLOCK)
1434 {
1435 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001436 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001437 goto update_offset;
1438 }
1439 else
1440 continue;
1441 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001442 if (results == 0)
1443 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001444 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001445 errno = -size;
1446 size = -1;
1447 goto done;
1448 }
1449 goto update_offset;
1450 }
1451
1452 results += nbytes;
1453 ASSERT (n_bytes_left >= nbytes);
1454 n_bytes_left = n_bytes_left - nbytes;
1455 }
1456 while (n_bytes_left > 0);
1457
1458 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001459 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001460 if (offset)
1461 {
1462 off_t off = lseek (in_fd, *offset, SEEK_SET);
1463 if (PREDICT_FALSE (off == -1))
1464 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001465 size = -1;
1466 goto done;
1467 }
1468
1469 ASSERT (off == *offset);
1470 *offset += results + 1;
1471 }
1472 if (eagain)
1473 {
1474 errno = EAGAIN;
1475 size = -1;
1476 }
1477 else
1478 size = results;
1479 }
1480 else
1481 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001482 size = libc_sendfile (out_fd, in_fd, offset, len);
1483 }
1484
1485done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001486 return size;
1487}
1488
1489ssize_t
1490sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1491{
1492 return sendfile (out_fd, in_fd, offset, len);
1493}
1494
1495ssize_t
1496recv (int fd, void *buf, size_t n, int flags)
1497{
Florin Coras7baeb712019-01-04 17:05:43 -08001498 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001499 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001500
Dave Wallace2a865272018-02-07 21:00:42 -05001501 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001502 return -1;
1503
Florin Coras7baeb712019-01-04 17:05:43 -08001504 vlsh = ldp_fd_to_vlsh (fd);
1505 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001506 {
Florin Coras7baeb712019-01-04 17:05:43 -08001507 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001508 if (size < 0)
Florin Corasa7a1a222018-12-30 17:11:31 -08001509 errno = -size;
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:
1818 strcpy (optval, "cubic");
1819 *optlen = strlen ("cubic");
1820 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 */