blob: 70a4299083ed613b15d8193b8e04dbc9044ecf8d [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) \
Florin Coras05ecfcc2018-12-12 18:19:39 -0800112 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args)
Florin Coras99368312018-08-02 10:45:44 -0700113
Dave Wallace2a865272018-02-07 21:00:42 -0500114static ldp_main_t ldp_main = {
Florin Coras7baeb712019-01-04 17:05:43 -0800115 .vlsh_bit_val = (1 << LDP_SID_BIT_MIN),
116 .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
Dave Wallace2a865272018-02-07 21:00:42 -0500117 .debug = LDP_DEBUG_INIT,
Yu Ping7b74b072019-05-08 00:40:24 +0800118 .transparent_tls = 0,
Dave Wallace048b1d62018-01-03 22:24:41 -0500119};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700120
Dave Wallace2a865272018-02-07 21:00:42 -0500121static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700122
Florin Corasdfe4cf42018-11-28 22:13:45 -0800123static inline ldp_worker_ctx_t *
124ldp_worker_get_current (void)
125{
126 return (ldp->workers + vppcom_worker_index ());
127}
128
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700129/*
130 * RETURN: 0 on success or -1 on error.
131 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500132static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500133ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700134{
Dave Wallace2a865272018-02-07 21:00:42 -0500135 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
136 "ldp-%d-%s", getpid (), app_name);
Dave Wallace048b1d62018-01-03 22:24:41 -0500137
Dave Wallace2a865272018-02-07 21:00:42 -0500138 if (rv >= LDP_APP_NAME_MAX)
139 app_name[LDP_APP_NAME_MAX - 1] = 0;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700140}
141
Dave Wallace048b1d62018-01-03 22:24:41 -0500142static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500143ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700144{
Dave Wallace2a865272018-02-07 21:00:42 -0500145 if (ldp->app_name[0] == '\0')
146 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500147
Dave Wallace2a865272018-02-07 21:00:42 -0500148 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700149}
150
Dave Wallace048b1d62018-01-03 22:24:41 -0500151static inline int
Florin Coras7baeb712019-01-04 17:05:43 -0800152ldp_vlsh_to_fd (vls_handle_t vlsh)
Dave Wallace048b1d62018-01-03 22:24:41 -0500153{
Florin Coras7baeb712019-01-04 17:05:43 -0800154 return (vlsh + ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500155}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700156
Florin Coras7baeb712019-01-04 17:05:43 -0800157static inline vls_handle_t
158ldp_fd_to_vlsh (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500159{
Florin Coras7baeb712019-01-04 17:05:43 -0800160 if (fd < ldp->vlsh_bit_val)
161 return VLS_INVALID_HANDLE;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700162
Florin Coras7baeb712019-01-04 17:05:43 -0800163 return (fd - ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500164}
165
Florin Coras2d9b4272019-03-11 10:14:37 -0700166static void
167ldp_alloc_workers (void)
168{
169 if (ldp->workers)
170 return;
171 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
172}
173
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700174static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500175ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700176{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800177 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700178 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700179
Florin Coras99368312018-08-02 10:45:44 -0700180 if (PREDICT_TRUE (ldp->init))
181 return 0;
182
183 ldp->init = 1;
184 ldp->vcl_needs_real_epoll = 1;
Florin Coras7baeb712019-01-04 17:05:43 -0800185 rv = vls_app_create (ldp_get_app_name ());
Florin Coras99368312018-08-02 10:45:44 -0700186 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700187 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800188 ldp->vcl_needs_real_epoll = 0;
189 if (rv == VPPCOM_EEXIST)
190 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800191 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
192 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700193 ldp->init = 0;
194 return rv;
195 }
196 ldp->vcl_needs_real_epoll = 0;
Florin Coras2d9b4272019-03-11 10:14:37 -0700197 ldp_alloc_workers ();
Florin Corasdfe4cf42018-11-28 22:13:45 -0800198 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700199
200 char *env_var_str = getenv (LDP_ENV_DEBUG);
201 if (env_var_str)
202 {
203 u32 tmp;
204 if (sscanf (env_var_str, "%u", &tmp) != 1)
205 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
206 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
207 env_var_str);
208 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700209 {
Florin Coras99368312018-08-02 10:45:44 -0700210 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800211 LDBG (0, "configured LDP debug level (%u) from env var "
212 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700213 }
214 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500215
Florin Coras99368312018-08-02 10:45:44 -0700216 env_var_str = getenv (LDP_ENV_APP_NAME);
217 if (env_var_str)
218 {
219 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800220 LDBG (0, "configured LDP app name (%s) from the env var "
221 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700222 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500223
Florin Coras99368312018-08-02 10:45:44 -0700224 env_var_str = getenv (LDP_ENV_SID_BIT);
225 if (env_var_str)
226 {
227 u32 sb;
228 if (sscanf (env_var_str, "%u", &sb) != 1)
229 {
Florin Coras294afe22019-01-07 17:49:17 -0800230 LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var "
231 LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str,
232 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700233 }
234 else if (sb < LDP_SID_BIT_MIN)
235 {
Florin Coras7baeb712019-01-04 17:05:43 -0800236 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN);
237 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500238
Florin Coras294afe22019-01-07 17:49:17 -0800239 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
240 LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN"
241 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
242 LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Florin Coras99368312018-08-02 10:45:44 -0700243 }
244 else if (sb > LDP_SID_BIT_MAX)
245 {
Florin Coras7baeb712019-01-04 17:05:43 -0800246 ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX);
247 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500248
Florin Coras294afe22019-01-07 17:49:17 -0800249 LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var "
250 LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX"
251 " (%d)! sid bit value %d (0x%x)", sb, env_var_str,
252 LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500253 }
254 else
255 {
Florin Coras7baeb712019-01-04 17:05:43 -0800256 ldp->vlsh_bit_val = (1 << sb);
257 ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1;
Florin Coras99368312018-08-02 10:45:44 -0700258
Florin Coras05ecfcc2018-12-12 18:19:39 -0800259 LDBG (0, "configured LDP sid bit (%u) from "
260 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras7baeb712019-01-04 17:05:43 -0800261 ldp->vlsh_bit_val, ldp->vlsh_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500262 }
Florin Coras294afe22019-01-07 17:49:17 -0800263
264 /* Make sure there are enough bits in the fd set for vcl sessions */
265 if (ldp->vlsh_bit_val > FD_SETSIZE / 2)
266 {
267 LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!",
268 ldp->vlsh_bit_val, FD_SETSIZE / 2);
269 ldp->init = 0;
270 return -1;
271 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500272 }
Yu Ping7b74b072019-05-08 00:40:24 +0800273 env_var_str = getenv (LDP_ENV_TLS_TRANS);
274 if (env_var_str)
275 {
276 ldp->transparent_tls = 1;
277 }
Florin Coras99368312018-08-02 10:45:44 -0700278
Florin Coras4dee8cd2019-01-29 21:28:16 -0800279 /* *INDENT-OFF* */
280 pool_foreach (ldpw, ldp->workers, ({
281 clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time));
282 }));
283 /* *INDENT-ON* */
284
Florin Coras05ecfcc2018-12-12 18:19:39 -0800285 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700286
287 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500288}
289
290int
291close (int fd)
292{
Florin Coras7baeb712019-01-04 17:05:43 -0800293 vls_handle_t vlsh;
294 int rv, epfd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500295
Dave Wallace2a865272018-02-07 21:00:42 -0500296 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500297 return -1;
298
Florin Coras7baeb712019-01-04 17:05:43 -0800299 vlsh = ldp_fd_to_vlsh (fd);
300 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500301 {
Florin Coras7baeb712019-01-04 17:05:43 -0800302 epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500303 if (epfd > 0)
304 {
Florin Coras7baeb712019-01-04 17:05:43 -0800305 LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500306
307 rv = libc_close (epfd);
308 if (rv < 0)
309 {
310 u32 size = sizeof (epfd);
311 epfd = 0;
312
Florin Coras7baeb712019-01-04 17:05:43 -0800313 (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500314 }
315 }
316 else if (PREDICT_FALSE (epfd < 0))
317 {
318 errno = -epfd;
319 rv = -1;
320 goto done;
321 }
322
Florin Coras7baeb712019-01-04 17:05:43 -0800323 LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500324
Florin Coras7baeb712019-01-04 17:05:43 -0800325 rv = vls_close (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500326 if (rv != VPPCOM_OK)
327 {
328 errno = -rv;
329 rv = -1;
330 }
331 }
332 else
333 {
Florin Coras7baeb712019-01-04 17:05:43 -0800334 LDBG (0, "fd %d: calling libc_close", fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500335 rv = libc_close (fd);
336 }
337
338done:
Dave Wallace048b1d62018-01-03 22:24:41 -0500339 return rv;
340}
341
342ssize_t
343read (int fd, void *buf, size_t nbytes)
344{
Florin Coras7baeb712019-01-04 17:05:43 -0800345 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500346 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -0500347
Dave Wallace2a865272018-02-07 21:00:42 -0500348 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500349 return -1;
350
Florin Coras7baeb712019-01-04 17:05:43 -0800351 vlsh = ldp_fd_to_vlsh (fd);
352 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500353 {
Florin Coras7baeb712019-01-04 17:05:43 -0800354 size = vls_read (vlsh, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500355 if (size < 0)
356 {
357 errno = -size;
358 size = -1;
359 }
360 }
361 else
362 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500363 size = libc_read (fd, buf, nbytes);
364 }
365
Dave Wallace048b1d62018-01-03 22:24:41 -0500366 return size;
367}
368
369ssize_t
370readv (int fd, const struct iovec * iov, int iovcnt)
371{
Dave Wallace8aaba562018-01-18 17:21:19 -0500372 int rv = 0, i, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800373 vls_handle_t vlsh;
374 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500375
Dave Wallace2a865272018-02-07 21:00:42 -0500376 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500377 return -1;
378
Florin Coras7baeb712019-01-04 17:05:43 -0800379 vlsh = ldp_fd_to_vlsh (fd);
380 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500381 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500382 do
383 {
384 for (i = 0; i < iovcnt; ++i)
385 {
Florin Coras7baeb712019-01-04 17:05:43 -0800386 rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len);
Dave Wallace048b1d62018-01-03 22:24:41 -0500387 if (rv < 0)
388 break;
389 else
390 {
391 total += rv;
392 if (rv < iov[i].iov_len)
Florin Coras7baeb712019-01-04 17:05:43 -0800393 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700394 }
395 }
396 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500397 while ((rv >= 0) && (total == 0));
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700398
Dave Wallace048b1d62018-01-03 22:24:41 -0500399 if (rv < 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700400 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500401 errno = -rv;
402 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700403 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500404 else
405 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700406 }
407 else
408 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500409 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700410 }
411
Dave Wallace048b1d62018-01-03 22:24:41 -0500412 return size;
413}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700414
Dave Wallace048b1d62018-01-03 22:24:41 -0500415ssize_t
416write (int fd, const void *buf, size_t nbytes)
417{
Florin Coras7baeb712019-01-04 17:05:43 -0800418 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500419 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500420
Dave Wallace2a865272018-02-07 21:00:42 -0500421 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500422 return -1;
423
Florin Coras7baeb712019-01-04 17:05:43 -0800424 vlsh = ldp_fd_to_vlsh (fd);
425 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500426 {
Florin Coras7baeb712019-01-04 17:05:43 -0800427 size = vls_write_msg (vlsh, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500428 if (size < 0)
429 {
430 errno = -size;
431 size = -1;
432 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700433 }
434 else
435 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500436 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700437 }
438
Dave Wallace048b1d62018-01-03 22:24:41 -0500439 return size;
440}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700441
Dave Wallace048b1d62018-01-03 22:24:41 -0500442ssize_t
443writev (int fd, const struct iovec * iov, int iovcnt)
444{
Dave Wallace048b1d62018-01-03 22:24:41 -0500445 ssize_t size = 0, total = 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800446 vls_handle_t vlsh;
Dave Wallace8aaba562018-01-18 17:21:19 -0500447 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500448
Dave Wallace2a865272018-02-07 21:00:42 -0500449 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500450 return -1;
451
Florin Coras7baeb712019-01-04 17:05:43 -0800452 vlsh = ldp_fd_to_vlsh (fd);
453 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -0500454 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500455 do
456 {
457 for (i = 0; i < iovcnt; ++i)
458 {
Florin Coras7baeb712019-01-04 17:05:43 -0800459 rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len);
Dave Wallace048b1d62018-01-03 22:24:41 -0500460 if (rv < 0)
461 break;
462 else
463 {
464 total += rv;
465 if (rv < iov[i].iov_len)
Florin Corasb0f662f2018-12-27 14:51:46 -0800466 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500467 }
468 }
469 }
470 while ((rv >= 0) && (total == 0));
471
472 if (rv < 0)
473 {
474 errno = -rv;
475 size = -1;
476 }
477 else
478 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700479 }
480 else
481 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500482 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700483 }
484
Dave Wallace048b1d62018-01-03 22:24:41 -0500485 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700486}
487
488int
Dave Wallace048b1d62018-01-03 22:24:41 -0500489fcntl (int fd, int cmd, ...)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700490{
Florin Coras7baeb712019-01-04 17:05:43 -0800491 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700492 int rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500493 va_list ap;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700494
Dave Wallace2a865272018-02-07 21:00:42 -0500495 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500496 return -1;
497
498 va_start (ap, cmd);
Florin Coras7baeb712019-01-04 17:05:43 -0800499
500 vlsh = ldp_fd_to_vlsh (fd);
501 LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd);
502 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700503 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500504 int flags = va_arg (ap, int);
505 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700506
Dave Wallace048b1d62018-01-03 22:24:41 -0500507 size = sizeof (flags);
508 rv = -EOPNOTSUPP;
509 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700510 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500511 case F_SETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800512 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500513 break;
514
515 case F_GETFL:
Florin Coras7baeb712019-01-04 17:05:43 -0800516 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500517 if (rv == VPPCOM_OK)
Florin Coras7baeb712019-01-04 17:05:43 -0800518 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700519 break;
Florin Coras173bae32018-11-16 18:56:28 -0800520 case F_SETFD:
521 /* TODO handle this */
522 LDBG (0, "F_SETFD ignored flags %u", flags);
523 rv = 0;
524 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700525 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500526 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700527 break;
528 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500529 if (rv < 0)
530 {
531 errno = -rv;
532 rv = -1;
533 }
534 }
535 else
536 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500537 rv = libc_vfcntl (fd, cmd, ap);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700538 }
539
Dave Wallace048b1d62018-01-03 22:24:41 -0500540 va_end (ap);
541
Dave Wallace048b1d62018-01-03 22:24:41 -0500542 return rv;
543}
544
545int
546ioctl (int fd, unsigned long int cmd, ...)
547{
Florin Coras7baeb712019-01-04 17:05:43 -0800548 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500549 va_list ap;
Florin Coras7baeb712019-01-04 17:05:43 -0800550 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -0500551
Dave Wallace2a865272018-02-07 21:00:42 -0500552 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500553 return -1;
554
555 va_start (ap, cmd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500556
Florin Coras7baeb712019-01-04 17:05:43 -0800557 vlsh = ldp_fd_to_vlsh (fd);
558 if (vlsh != VLS_INVALID_HANDLE)
559 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500560 switch (cmd)
561 {
562 case FIONREAD:
Florin Coras7baeb712019-01-04 17:05:43 -0800563 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500564 break;
565
566 case FIONBIO:
567 {
568 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
569 u32 size = sizeof (flags);
570
571 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
572 * non-blocking, the flags should be read here and merged
573 * with O_NONBLOCK.
574 */
Florin Coras7baeb712019-01-04 17:05:43 -0800575 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500576 }
577 break;
578
579 default:
580 rv = -EOPNOTSUPP;
581 break;
582 }
583 if (rv < 0)
584 {
585 errno = -rv;
586 rv = -1;
587 }
588 }
589 else
590 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500591 rv = libc_vioctl (fd, cmd, ap);
592 }
593
Dave Wallace048b1d62018-01-03 22:24:41 -0500594 va_end (ap);
595 return rv;
596}
597
Florin Coras294afe22019-01-07 17:49:17 -0800598always_inline void
599ldp_select_init_maps (fd_set * __restrict original,
600 clib_bitmap_t ** resultb, clib_bitmap_t ** libcb,
601 clib_bitmap_t ** vclb, int nfds, u32 minbits,
602 u32 n_bytes, uword * si_bits, uword * libc_bits)
603{
604 uword si_bits_set, libc_bits_set;
605 vls_handle_t vlsh;
606 int fd;
607
608 clib_bitmap_validate (*vclb, minbits);
609 clib_bitmap_validate (*libcb, minbits);
610 clib_bitmap_validate (*resultb, minbits);
611 clib_memcpy_fast (*resultb, original, n_bytes);
612 memset (original, 0, n_bytes);
613
614 /* *INDENT-OFF* */
615 clib_bitmap_foreach (fd, *resultb, ({
616 if (fd > nfds)
617 break;
618 vlsh = ldp_fd_to_vlsh (fd);
619 if (vlsh == VLS_INVALID_HANDLE)
620 clib_bitmap_set_no_check (*libcb, fd, 1);
621 else
622 clib_bitmap_set_no_check (*vclb, vlsh_to_session_index (vlsh), 1);
623 }));
624 /* *INDENT-ON* */
625
626 si_bits_set = clib_bitmap_last_set (*vclb) + 1;
627 *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits;
628
629 libc_bits_set = clib_bitmap_last_set (*libcb) + 1;
630 *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits;
631}
632
633always_inline int
634ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb)
635{
636 vls_handle_t vlsh;
637 uword si;
638 int fd;
639
640 if (!libcb)
641 return 0;
642
643 /* *INDENT-OFF* */
644 clib_bitmap_foreach (si, vclb, ({
645 vlsh = vls_session_index_to_vlsh (si);
646 fd = ldp_vlsh_to_fd (vlsh);
647 if (PREDICT_FALSE (fd < 0))
648 {
649 errno = EBADFD;
650 return -1;
651 }
652 FD_SET (fd, libcb);
653 }));
654 /* *INDENT-ON* */
655
656 return 0;
657}
658
659always_inline void
660ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb)
661{
662 uword fd;
663
Florin Coras78b5fa62019-02-21 20:04:15 -0800664 if (!libcb)
665 return;
666
Florin Coras294afe22019-01-07 17:49:17 -0800667 /* *INDENT-OFF* */
668 clib_bitmap_foreach (fd, result, ({
669 FD_SET ((int)fd, libcb);
670 }));
671 /* *INDENT-ON* */
672}
673
Dave Wallace048b1d62018-01-03 22:24:41 -0500674int
Dave Wallace2a865272018-02-07 21:00:42 -0500675ldp_pselect (int nfds, fd_set * __restrict readfds,
676 fd_set * __restrict writefds,
677 fd_set * __restrict exceptfds,
678 const struct timespec *__restrict timeout,
679 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500680{
Florin Coras294afe22019-01-07 17:49:17 -0800681 u32 minbits = clib_max (nfds, BITS (uword)), n_bytes;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800682 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras294afe22019-01-07 17:49:17 -0800683 struct timespec libc_tspec = { 0 };
684 f64 time_out, vcl_timeout = 0;
685 uword si_bits, libc_bits;
686 int rv, bits_set = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500687
688 if (nfds < 0)
689 {
690 errno = EINVAL;
691 return -1;
692 }
693
Florin Coras4dee8cd2019-01-29 21:28:16 -0800694 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
695 clib_time_init (&ldpw->clib_time);
696
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500697 if (timeout)
698 {
699 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
Florin Coras7baeb712019-01-04 17:05:43 -0800700 (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9;
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500701
702 /* select as fine grained sleep */
703 if (!nfds)
704 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800705 time_out += clib_time_now (&ldpw->clib_time);
706 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500707 ;
708 return 0;
709 }
710 }
711 else if (!nfds)
712 {
713 errno = EINVAL;
714 return -1;
715 }
716 else
717 time_out = -1;
718
Florin Coras7baeb712019-01-04 17:05:43 -0800719 if (nfds <= ldp->vlsh_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500720 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500721 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
722 timeout, sigmask);
723 goto done;
724 }
725
Florin Coras294afe22019-01-07 17:49:17 -0800726 si_bits = libc_bits = 0;
727 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Florin Coras7baeb712019-01-04 17:05:43 -0800728
Dave Wallace048b1d62018-01-03 22:24:41 -0500729 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800730 ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap,
731 &ldpw->si_rd_bitmap, nfds, minbits, n_bytes,
732 &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500733 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800734 ldp_select_init_maps (writefds, &ldpw->wr_bitmap,
735 &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds,
736 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500737 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800738 ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap,
739 &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds,
740 minbits, n_bytes, &si_bits, &libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500741
Florin Coras294afe22019-01-07 17:49:17 -0800742 if (PREDICT_FALSE (!si_bits && !libc_bits))
Dave Wallace048b1d62018-01-03 22:24:41 -0500743 {
744 errno = EINVAL;
745 rv = -1;
746 goto done;
747 }
748
Florin Coras78b5fa62019-02-21 20:04:15 -0800749 if (!si_bits)
750 libc_tspec = timeout ? *timeout : libc_tspec;
Florin Coras294afe22019-01-07 17:49:17 -0800751
Dave Wallace048b1d62018-01-03 22:24:41 -0500752 do
753 {
Florin Coras294afe22019-01-07 17:49:17 -0800754 if (si_bits)
Dave Wallace048b1d62018-01-03 22:24:41 -0500755 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500756 if (readfds)
Florin Coras294afe22019-01-07 17:49:17 -0800757 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap,
Florin Corasdfe4cf42018-11-28 22:13:45 -0800758 vec_len (ldpw->rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500759 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500760 if (writefds)
Florin Coras294afe22019-01-07 17:49:17 -0800761 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap,
Florin Corasdfe4cf42018-11-28 22:13:45 -0800762 vec_len (ldpw->wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500763 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500764 if (exceptfds)
Florin Coras294afe22019-01-07 17:49:17 -0800765 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap,
Florin Corasdfe4cf42018-11-28 22:13:45 -0800766 vec_len (ldpw->ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500767 sizeof (clib_bitmap_t));
Florin Coras294afe22019-01-07 17:49:17 -0800768
Florin Coras0ef8ef22019-01-18 08:37:13 -0800769 rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL,
770 writefds ? ldpw->wr_bitmap : NULL,
771 exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout);
Florin Coras294afe22019-01-07 17:49:17 -0800772 if (rv < 0)
773 {
774 errno = -rv;
775 rv = -1;
776 }
777 else if (rv > 0)
778 {
779 if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds))
780 {
781 rv = -1;
782 goto done;
783 }
784
785 if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds))
786 {
787 rv = -1;
788 goto done;
789 }
790
791 if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds))
792 {
793 rv = -1;
794 goto done;
795 }
796 bits_set = rv;
797 }
798 }
799 if (libc_bits)
800 {
801 if (readfds)
802 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap,
803 vec_len (ldpw->libc_rd_bitmap) *
804 sizeof (clib_bitmap_t));
805 if (writefds)
806 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap,
807 vec_len (ldpw->libc_wr_bitmap) *
808 sizeof (clib_bitmap_t));
809 if (exceptfds)
810 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap,
811 vec_len (ldpw->libc_ex_bitmap) *
812 sizeof (clib_bitmap_t));
813
Dave Wallace048b1d62018-01-03 22:24:41 -0500814 rv = libc_pselect (libc_bits,
Florin Coras294afe22019-01-07 17:49:17 -0800815 readfds ? (fd_set *) ldpw->rd_bitmap : NULL,
816 writefds ? (fd_set *) ldpw->wr_bitmap : NULL,
817 exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL,
818 &libc_tspec, sigmask);
819 if (rv > 0)
820 {
821 ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds);
822 ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds);
823 ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds);
824 bits_set += rv;
825 }
826 }
827
828 if (bits_set)
829 {
830 rv = bits_set;
831 goto done;
Dave Wallace048b1d62018-01-03 22:24:41 -0500832 }
833 }
Florin Corasdfe4cf42018-11-28 22:13:45 -0800834 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -0500835 rv = 0;
836
837done:
838 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800839 clib_bitmap_zero (ldpw->rd_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800840 clib_bitmap_zero (ldpw->si_rd_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800841 clib_bitmap_zero (ldpw->libc_rd_bitmap);
842 clib_bitmap_zero (ldpw->wr_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800843 clib_bitmap_zero (ldpw->si_wr_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800844 clib_bitmap_zero (ldpw->libc_wr_bitmap);
845 clib_bitmap_zero (ldpw->ex_bitmap);
Florin Coras294afe22019-01-07 17:49:17 -0800846 clib_bitmap_zero (ldpw->si_ex_bitmap);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800847 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -0500848
Dave Wallace048b1d62018-01-03 22:24:41 -0500849 return rv;
850}
851
852int
853select (int nfds, fd_set * __restrict readfds,
854 fd_set * __restrict writefds,
855 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
856{
857 struct timespec tspec;
858
859 if (timeout)
860 {
861 tspec.tv_sec = timeout->tv_sec;
862 tspec.tv_nsec = timeout->tv_usec * 1000;
863 }
Dave Wallace2a865272018-02-07 21:00:42 -0500864 return ldp_pselect (nfds, readfds, writefds, exceptfds,
865 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -0500866}
867
868#ifdef __USE_XOPEN2K
869int
870pselect (int nfds, fd_set * __restrict readfds,
871 fd_set * __restrict writefds,
872 fd_set * __restrict exceptfds,
873 const struct timespec *__restrict timeout,
874 const __sigset_t * __restrict sigmask)
875{
Dave Wallace2a865272018-02-07 21:00:42 -0500876 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500877}
878#endif
879
Yu Ping7b74b072019-05-08 00:40:24 +0800880/* If transparent TLS mode is turned on, then ldp will load key and cert.
881 */
882static int
883load_tls_cert (vls_handle_t vlsh)
884{
885 char *env_var_str = getenv (LDP_ENV_TLS_CERT);
886 char inbuf[4096];
887 char *tls_cert;
888 int cert_size;
889 FILE *fp;
890
891 if (env_var_str)
892 {
893 fp = fopen (env_var_str, "r");
894 if (fp == NULL)
895 {
896 LDBG (0, "ERROR: failed to open cert file %s \n", env_var_str);
897 return -1;
898 }
899 cert_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
900 tls_cert = inbuf;
901 vppcom_session_tls_add_cert (vlsh_to_session_index (vlsh), tls_cert,
902 cert_size);
903 fclose (fp);
904 }
905 else
906 {
907 LDBG (0, "ERROR: failed to read LDP environment %s\n",
908 LDP_ENV_TLS_CERT);
909 return -1;
910 }
911 return 0;
912}
913
914static int
915load_tls_key (vls_handle_t vlsh)
916{
917 char *env_var_str = getenv (LDP_ENV_TLS_KEY);
918 char inbuf[4096];
919 char *tls_key;
920 int key_size;
921 FILE *fp;
922
923 if (env_var_str)
924 {
925 fp = fopen (env_var_str, "r");
926 if (fp == NULL)
927 {
928 LDBG (0, "ERROR: failed to open key file %s \n", env_var_str);
929 return -1;
930 }
931 key_size = fread (inbuf, sizeof (char), sizeof (inbuf), fp);
932 tls_key = inbuf;
933 vppcom_session_tls_add_key (vlsh_to_session_index (vlsh), tls_key,
934 key_size);
935 fclose (fp);
936 }
937 else
938 {
939 LDBG (0, "ERROR: failed to read LDP environment %s\n", LDP_ENV_TLS_KEY);
940 return -1;
941 }
942 return 0;
943}
944
Dave Wallace048b1d62018-01-03 22:24:41 -0500945int
946socket (int domain, int type, int protocol)
947{
Florin Coras7baeb712019-01-04 17:05:43 -0800948 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -0500949 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
Florin Coras7baeb712019-01-04 17:05:43 -0800950 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500951
Dave Wallace2a865272018-02-07 21:00:42 -0500952 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500953 return -1;
954
955 if (((domain == AF_INET) || (domain == AF_INET6)) &&
956 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
957 {
Yu Ping7b74b072019-05-08 00:40:24 +0800958 u8 proto;
959 if (ldp->transparent_tls)
960 {
961 proto = VPPCOM_PROTO_TLS;
962 }
963 else
964 proto = ((sock_type == SOCK_DGRAM) ?
965 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
Dave Wallace048b1d62018-01-03 22:24:41 -0500966
Florin Coras7baeb712019-01-04 17:05:43 -0800967 LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u",
968 proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -0500969
Florin Coras7baeb712019-01-04 17:05:43 -0800970 vlsh = vls_create (proto, is_nonblocking);
971 if (vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500972 {
Florin Coras7baeb712019-01-04 17:05:43 -0800973 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -0500974 rv = -1;
975 }
976 else
977 {
Yu Ping7b74b072019-05-08 00:40:24 +0800978 if (ldp->transparent_tls)
979 {
980 if (load_tls_cert (vlsh) < 0 || load_tls_key (vlsh) < 0)
981 {
982 return -1;
983 }
984 }
Florin Coras7baeb712019-01-04 17:05:43 -0800985 rv = ldp_vlsh_to_fd (vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -0500986 }
987 }
988 else
989 {
Florin Coras7baeb712019-01-04 17:05:43 -0800990 LDBG (0, "calling libc_socket");
Dave Wallace048b1d62018-01-03 22:24:41 -0500991 rv = libc_socket (domain, type, protocol);
992 }
993
Dave Wallace048b1d62018-01-03 22:24:41 -0500994 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700995}
996
997/*
998 * Create two new sockets, of type TYPE in domain DOMAIN and using
999 * protocol PROTOCOL, which are connected to each other, and put file
1000 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1001 * one will be chosen automatically.
1002 * Returns 0 on success, -1 for errors.
1003 * */
1004int
Dave Wallace048b1d62018-01-03 22:24:41 -05001005socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001006{
Florin Coras7baeb712019-01-04 17:05:43 -08001007 int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
Dave Wallace048b1d62018-01-03 22:24:41 -05001008
Dave Wallace2a865272018-02-07 21:00:42 -05001009 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001010 return -1;
1011
1012 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1013 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001014 {
Florin Coras7baeb712019-01-04 17:05:43 -08001015 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001016 errno = ENOSYS;
1017 rv = -1;
1018 }
1019 else
1020 {
Florin Coras7baeb712019-01-04 17:05:43 -08001021 LDBG (1, "calling libc_socketpair");
Florin Coras173bae32018-11-16 18:56:28 -08001022 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001023 }
1024
Dave Wallace048b1d62018-01-03 22:24:41 -05001025 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001026}
1027
1028int
Dave Wallace048b1d62018-01-03 22:24:41 -05001029bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001030{
Florin Coras7baeb712019-01-04 17:05:43 -08001031 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001032 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001033
Dave Wallace2a865272018-02-07 21:00:42 -05001034 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001035 return -1;
1036
Florin Coras7baeb712019-01-04 17:05:43 -08001037 vlsh = ldp_fd_to_vlsh (fd);
1038 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001039 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001040 vppcom_endpt_t ep;
1041
Dave Wallace048b1d62018-01-03 22:24:41 -05001042 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001043 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001044 case AF_INET:
1045 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001046 {
Florin Coras7baeb712019-01-04 17:05:43 -08001047 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!",
1048 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001049 errno = EINVAL;
1050 rv = -1;
1051 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001052 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001053 ep.is_ip4 = VPPCOM_IS_IP4;
1054 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1055 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1056 break;
1057
1058 case AF_INET6:
1059 if (len != sizeof (struct sockaddr_in6))
1060 {
Florin Coras7baeb712019-01-04 17:05:43 -08001061 LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!",
1062 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001063 errno = EINVAL;
1064 rv = -1;
1065 goto done;
1066 }
1067 ep.is_ip4 = VPPCOM_IS_IP6;
1068 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1069 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001070 break;
1071
1072 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001073 LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!",
1074 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001075 errno = EAFNOSUPPORT;
1076 rv = -1;
1077 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001078 }
Florin Coras7baeb712019-01-04 17:05:43 -08001079 LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh,
1080 addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001081
Florin Coras7baeb712019-01-04 17:05:43 -08001082 rv = vls_bind (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001083 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001084 {
1085 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001086 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001087 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001088 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001089 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001090 {
Florin Coras7baeb712019-01-04 17:05:43 -08001091 LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001092 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001093 }
1094
Dave Wallace048b1d62018-01-03 22:24:41 -05001095done:
Florin Coras7baeb712019-01-04 17:05:43 -08001096 LDBG (1, "fd %d: returning %d", fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001097
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001098 return rv;
1099}
1100
1101static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001102ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1103 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001104{
Dave Wallace048b1d62018-01-03 22:24:41 -05001105 int rv = 0;
1106 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001107
Dave Wallace2a865272018-02-07 21:00:42 -05001108 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001109 return -1;
1110
1111 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001112 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001113 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1114 switch (addr->sa_family)
1115 {
1116 case AF_INET:
1117 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1118 if (*len > sizeof (struct sockaddr_in))
1119 *len = sizeof (struct sockaddr_in);
1120 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1121 copy_len = *len - sa_len;
1122 if (copy_len > 0)
1123 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1124 copy_len);
1125 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001126
Dave Wallace048b1d62018-01-03 22:24:41 -05001127 case AF_INET6:
1128 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1129 if (*len > sizeof (struct sockaddr_in6))
1130 *len = sizeof (struct sockaddr_in6);
1131 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1132 copy_len = *len - sa_len;
1133 if (copy_len > 0)
1134 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1135 __in6_u.__u6_addr8, ep->ip, copy_len);
1136 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001137
Dave Wallace048b1d62018-01-03 22:24:41 -05001138 default:
1139 /* Not possible */
1140 rv = -EAFNOSUPPORT;
1141 break;
1142 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001143 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001144 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001145}
1146
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001147int
Dave Wallace048b1d62018-01-03 22:24:41 -05001148getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001149{
Florin Coras7baeb712019-01-04 17:05:43 -08001150 vls_handle_t vlsh;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001151 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001152
Dave Wallace2a865272018-02-07 21:00:42 -05001153 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001154 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001155
Florin Coras7baeb712019-01-04 17:05:43 -08001156 vlsh = ldp_fd_to_vlsh (fd);
1157 if (vlsh != VLS_INVALID_HANDLE)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001158 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001159 vppcom_endpt_t ep;
1160 u8 addr_buf[sizeof (struct in6_addr)];
1161 u32 size = sizeof (ep);
1162
1163 ep.ip = addr_buf;
Dave Wallace048b1d62018-01-03 22:24:41 -05001164
Florin Coras7baeb712019-01-04 17:05:43 -08001165 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001166 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001167 {
1168 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001169 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001170 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001171 else
1172 {
Dave Wallace2a865272018-02-07 21:00:42 -05001173 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001174 if (rv != VPPCOM_OK)
1175 {
1176 errno = -rv;
1177 rv = -1;
1178 }
1179 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001180 }
1181 else
1182 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001183 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001184 }
1185
Dave Wallace048b1d62018-01-03 22:24:41 -05001186 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001187}
1188
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001189int
Dave Wallace048b1d62018-01-03 22:24:41 -05001190connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001191{
Florin Coras7baeb712019-01-04 17:05:43 -08001192 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001193 int rv;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001194
Dave Wallace2a865272018-02-07 21:00:42 -05001195 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001196 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001197
Dave Wallace048b1d62018-01-03 22:24:41 -05001198 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001199 {
Florin Coras7baeb712019-01-04 17:05:43 -08001200 LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001201 errno = EINVAL;
1202 rv = -1;
1203 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001204 }
1205
Florin Coras7baeb712019-01-04 17:05:43 -08001206 vlsh = ldp_fd_to_vlsh (fd);
1207 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001208 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001209 vppcom_endpt_t ep;
1210
Dave Wallace048b1d62018-01-03 22:24:41 -05001211 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001212 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001213 case AF_INET:
1214 if (len != sizeof (struct sockaddr_in))
1215 {
Florin Coras7baeb712019-01-04 17:05:43 -08001216 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!",
1217 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001218 errno = EINVAL;
1219 rv = -1;
1220 goto done;
1221 }
1222 ep.is_ip4 = VPPCOM_IS_IP4;
1223 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1224 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1225 break;
1226
1227 case AF_INET6:
1228 if (len != sizeof (struct sockaddr_in6))
1229 {
Florin Coras7baeb712019-01-04 17:05:43 -08001230 LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!",
1231 fd, vlsh, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001232 errno = EINVAL;
1233 rv = -1;
1234 goto done;
1235 }
1236 ep.is_ip4 = VPPCOM_IS_IP6;
1237 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1238 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1239 break;
1240
1241 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001242 LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!",
1243 fd, vlsh, addr->sa_family);
Dave Wallace048b1d62018-01-03 22:24:41 -05001244 errno = EAFNOSUPPORT;
1245 rv = -1;
1246 goto done;
1247 }
Florin Coras7baeb712019-01-04 17:05:43 -08001248 LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd,
1249 vlsh, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001250
Florin Coras7baeb712019-01-04 17:05:43 -08001251 rv = vls_connect (vlsh, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001252 if (rv != VPPCOM_OK)
1253 {
1254 errno = -rv;
1255 rv = -1;
1256 }
1257 }
1258 else
1259 {
Florin Coras7baeb712019-01-04 17:05:43 -08001260 LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u",
1261 fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001262
1263 rv = libc_connect (fd, addr, len);
1264 }
1265
1266done:
Florin Coras7baeb712019-01-04 17:05:43 -08001267 LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001268 return rv;
1269}
1270
1271int
1272getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1273{
Florin Coras7baeb712019-01-04 17:05:43 -08001274 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001275 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001276
Dave Wallace2a865272018-02-07 21:00:42 -05001277 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001278 return -1;
1279
Florin Coras7baeb712019-01-04 17:05:43 -08001280 vlsh = ldp_fd_to_vlsh (fd);
1281 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001282 {
1283 vppcom_endpt_t ep;
1284 u8 addr_buf[sizeof (struct in6_addr)];
1285 u32 size = sizeof (ep);
1286
1287 ep.ip = addr_buf;
Florin Coras7baeb712019-01-04 17:05:43 -08001288 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
Dave Wallace048b1d62018-01-03 22:24:41 -05001289 if (rv != VPPCOM_OK)
1290 {
1291 errno = -rv;
1292 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001293 }
1294 else
1295 {
Dave Wallace2a865272018-02-07 21:00:42 -05001296 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001297 if (rv != VPPCOM_OK)
1298 {
1299 errno = -rv;
1300 rv = -1;
1301 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001302 }
1303 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001304 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001305 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001306 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001307 }
1308
Dave Wallace048b1d62018-01-03 22:24:41 -05001309 return rv;
1310}
1311
1312ssize_t
1313send (int fd, const void *buf, size_t n, int flags)
1314{
Florin Coras7baeb712019-01-04 17:05:43 -08001315 vls_handle_t vlsh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001316 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001317
Dave Wallace2a865272018-02-07 21:00:42 -05001318 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001319 return -1;
1320
Florin Coras7baeb712019-01-04 17:05:43 -08001321 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001322 {
Florin Coras7baeb712019-01-04 17:05:43 -08001323 size = vls_sendto (vlsh, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001324 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 {
1326 errno = -size;
1327 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001328 }
1329 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001330 else
1331 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001332 size = libc_send (fd, buf, n, flags);
1333 }
1334
Dave Wallace048b1d62018-01-03 22:24:41 -05001335 return size;
1336}
1337
1338ssize_t
1339sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1340{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001341 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08001342 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001343 ssize_t size = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001344
Dave Wallace2a865272018-02-07 21:00:42 -05001345 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001346 return -1;
1347
Florin Coras7baeb712019-01-04 17:05:43 -08001348 vlsh = ldp_fd_to_vlsh (out_fd);
1349 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001350 {
1351 int rv;
1352 ssize_t results = 0;
1353 size_t n_bytes_left = len;
1354 size_t bytes_to_read;
1355 int nbytes;
Dave Wallace048b1d62018-01-03 22:24:41 -05001356 u8 eagain = 0;
1357 u32 flags, flags_len = sizeof (flags);
1358
Florin Coras7baeb712019-01-04 17:05:43 -08001359 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001360 if (PREDICT_FALSE (rv != VPPCOM_OK))
1361 {
Florin Coras7baeb712019-01-04 17:05:43 -08001362 LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!",
1363 out_fd, vlsh, rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05001364
Florin Corasdfe4cf42018-11-28 22:13:45 -08001365 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001366 errno = -rv;
1367 size = -1;
1368 goto done;
1369 }
1370
1371 if (offset)
1372 {
1373 off_t off = lseek (in_fd, *offset, SEEK_SET);
1374 if (PREDICT_FALSE (off == -1))
1375 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001376 size = -1;
1377 goto done;
1378 }
1379
1380 ASSERT (off == *offset);
1381 }
1382
1383 do
1384 {
Florin Coras7baeb712019-01-04 17:05:43 -08001385 size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001386 if (size < 0)
1387 {
Florin Coras7baeb712019-01-04 17:05:43 -08001388 LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %d (%s)!",
1389 out_fd, vlsh, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001390 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001391 errno = -size;
1392 size = -1;
1393 goto done;
1394 }
1395
1396 bytes_to_read = size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001397 if (bytes_to_read == 0)
1398 {
1399 if (flags & O_NONBLOCK)
1400 {
1401 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001402 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001403 goto update_offset;
1404 }
1405 else
1406 continue;
1407 }
1408 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001409 vec_validate (ldpw->io_buffer, bytes_to_read);
1410 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001411 if (nbytes < 0)
1412 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001413 if (results == 0)
1414 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001415 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001416 size = -1;
1417 goto done;
1418 }
1419 goto update_offset;
1420 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001421
Florin Coras7baeb712019-01-04 17:05:43 -08001422 size = vls_write (vlsh, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001423 if (size < 0)
1424 {
1425 if (size == VPPCOM_EAGAIN)
1426 {
1427 if (flags & O_NONBLOCK)
1428 {
1429 if (!results)
Florin Coras7baeb712019-01-04 17:05:43 -08001430 eagain = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001431 goto update_offset;
1432 }
1433 else
1434 continue;
1435 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001436 if (results == 0)
1437 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001438 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001439 errno = -size;
1440 size = -1;
1441 goto done;
1442 }
1443 goto update_offset;
1444 }
1445
1446 results += nbytes;
1447 ASSERT (n_bytes_left >= nbytes);
1448 n_bytes_left = n_bytes_left - nbytes;
1449 }
1450 while (n_bytes_left > 0);
1451
1452 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001453 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001454 if (offset)
1455 {
1456 off_t off = lseek (in_fd, *offset, SEEK_SET);
1457 if (PREDICT_FALSE (off == -1))
1458 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001459 size = -1;
1460 goto done;
1461 }
1462
1463 ASSERT (off == *offset);
1464 *offset += results + 1;
1465 }
1466 if (eagain)
1467 {
1468 errno = EAGAIN;
1469 size = -1;
1470 }
1471 else
1472 size = results;
1473 }
1474 else
1475 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001476 size = libc_sendfile (out_fd, in_fd, offset, len);
1477 }
1478
1479done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001480 return size;
1481}
1482
1483ssize_t
1484sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1485{
1486 return sendfile (out_fd, in_fd, offset, len);
1487}
1488
1489ssize_t
1490recv (int fd, void *buf, size_t n, int flags)
1491{
Florin Coras7baeb712019-01-04 17:05:43 -08001492 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001493 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001494
Dave Wallace2a865272018-02-07 21:00:42 -05001495 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001496 return -1;
1497
Florin Coras7baeb712019-01-04 17:05:43 -08001498 vlsh = ldp_fd_to_vlsh (fd);
1499 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001500 {
Florin Coras7baeb712019-01-04 17:05:43 -08001501 size = vls_recvfrom (vlsh, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001502 if (size < 0)
Florin Corasa7a1a222018-12-30 17:11:31 -08001503 errno = -size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001504 }
1505 else
1506 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001507 size = libc_recv (fd, buf, n, flags);
1508 }
1509
Dave Wallace048b1d62018-01-03 22:24:41 -05001510 return size;
1511}
1512
1513ssize_t
1514sendto (int fd, const void *buf, size_t n, int flags,
1515 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1516{
Florin Coras7baeb712019-01-04 17:05:43 -08001517 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001518 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001519
Dave Wallace2a865272018-02-07 21:00:42 -05001520 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001521 return -1;
1522
Florin Coras7baeb712019-01-04 17:05:43 -08001523 vlsh = ldp_fd_to_vlsh (fd);
1524 if (vlsh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001525 {
1526 vppcom_endpt_t *ep = 0;
1527 vppcom_endpt_t _ep;
1528
1529 if (addr)
1530 {
1531 ep = &_ep;
Dave Wallace048b1d62018-01-03 22:24:41 -05001532 switch (addr->sa_family)
1533 {
1534 case AF_INET:
1535 ep->is_ip4 = VPPCOM_IS_IP4;
1536 ep->ip =
1537 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1538 ep->port =
1539 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1540 break;
1541
1542 case AF_INET6:
1543 ep->is_ip4 = VPPCOM_IS_IP6;
1544 ep->ip =
1545 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1546 ep->port =
1547 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1548 break;
1549
1550 default:
1551 errno = EAFNOSUPPORT;
1552 size = -1;
1553 goto done;
1554 }
1555 }
1556
Florin Coras7baeb712019-01-04 17:05:43 -08001557 size = vls_sendto (vlsh, (void *) buf, n, flags, ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001558 if (size < 0)
1559 {
1560 errno = -size;
1561 size = -1;
1562 }
1563 }
1564 else
1565 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001566 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1567 }
1568
1569done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001570 return size;
1571}
1572
1573ssize_t
1574recvfrom (int fd, void *__restrict buf, size_t n, int flags,
1575 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
1576{
Florin Coras7baeb712019-01-04 17:05:43 -08001577 vls_handle_t sid;
Haggai Eran46ae2ef2019-02-19 15:04:42 +02001578 ssize_t size, rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001579
Dave Wallace2a865272018-02-07 21:00:42 -05001580 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001581 return -1;
1582
Florin Coras7baeb712019-01-04 17:05:43 -08001583 sid = ldp_fd_to_vlsh (fd);
1584 if (sid != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001585 {
1586 vppcom_endpt_t ep;
1587 u8 src_addr[sizeof (struct sockaddr_in6)];
1588
Dave Wallace048b1d62018-01-03 22:24:41 -05001589 if (addr)
1590 {
1591 ep.ip = src_addr;
Florin Coras7baeb712019-01-04 17:05:43 -08001592 size = vls_recvfrom (sid, buf, n, flags, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001593
1594 if (size > 0)
Haggai Eran46ae2ef2019-02-19 15:04:42 +02001595 {
1596 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
1597 if (rv < 0)
1598 size = rv;
1599 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001600 }
1601 else
Florin Coras7baeb712019-01-04 17:05:43 -08001602 size = vls_recvfrom (sid, buf, n, flags, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001603
1604 if (size < 0)
1605 {
1606 errno = -size;
1607 size = -1;
1608 }
1609 }
1610 else
1611 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001612 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
1613 }
1614
Dave Wallace048b1d62018-01-03 22:24:41 -05001615 return size;
1616}
1617
1618ssize_t
1619sendmsg (int fd, const struct msghdr * message, int flags)
1620{
Florin Coras7baeb712019-01-04 17:05:43 -08001621 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001622 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001623
Dave Wallace2a865272018-02-07 21:00:42 -05001624 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001625 return -1;
1626
Florin Coras7baeb712019-01-04 17:05:43 -08001627 vlsh = ldp_fd_to_vlsh (fd);
1628 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001629 {
Florin Coras7baeb712019-01-04 17:05:43 -08001630 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001631 errno = ENOSYS;
1632 size = -1;
1633 }
1634 else
1635 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001636 size = libc_sendmsg (fd, message, flags);
1637 }
1638
Dave Wallace048b1d62018-01-03 22:24:41 -05001639 return size;
1640}
1641
1642#ifdef USE_GNU
1643int
1644sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
1645{
1646 ssize_t size;
1647 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001648 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001649
Dave Wallace2a865272018-02-07 21:00:42 -05001650 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001651 return -1;
1652
Florin Coras7baeb712019-01-04 17:05:43 -08001653 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001654 {
1655 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1656 errno = ENOSYS;
1657 size = -1;
1658 }
1659 else
1660 {
1661 func_str = "libc_sendmmsg";
1662
Dave Wallace2a865272018-02-07 21:00:42 -05001663 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001664 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1665 "vmessages %p, vlen %u, flags 0x%x",
1666 getpid (), fd, fd, func_str, vmessages, vlen, flags);
1667
1668 size = libc_sendmmsg (fd, vmessages, vlen, flags);
1669 }
1670
Dave Wallace2a865272018-02-07 21:00:42 -05001671 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001672 {
1673 if (size < 0)
1674 {
1675 int errno_val = errno;
1676 perror (func_str);
1677 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1678 "rv %d, errno = %d", getpid (), fd, fd,
1679 func_str, size, errno_val);
1680 errno = errno_val;
1681 }
1682 else
1683 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1684 getpid (), fd, fd, size, size);
1685 }
1686 return size;
1687}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001688#endif
1689
Dave Wallace048b1d62018-01-03 22:24:41 -05001690ssize_t
1691recvmsg (int fd, struct msghdr * message, int flags)
1692{
Florin Coras7baeb712019-01-04 17:05:43 -08001693 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001694 ssize_t size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001695
Dave Wallace2a865272018-02-07 21:00:42 -05001696 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001697 return -1;
1698
Florin Coras7baeb712019-01-04 17:05:43 -08001699 vlsh = ldp_fd_to_vlsh (fd);
1700 if (vlsh != VLS_INVALID_HANDLE)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001701 {
Florin Coras7baeb712019-01-04 17:05:43 -08001702 LDBG (0, "LDP-TBD");
Dave Wallace048b1d62018-01-03 22:24:41 -05001703 errno = ENOSYS;
1704 size = -1;
1705 }
1706 else
1707 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001708 size = libc_recvmsg (fd, message, flags);
1709 }
1710
Dave Wallace048b1d62018-01-03 22:24:41 -05001711 return size;
1712}
1713
1714#ifdef USE_GNU
1715int
1716recvmmsg (int fd, struct mmsghdr *vmessages,
1717 unsigned int vlen, int flags, struct timespec *tmo)
1718{
1719 ssize_t size;
1720 const char *func_str;
Florin Coras7baeb712019-01-04 17:05:43 -08001721 u32 sh = ldp_fd_to_vlsh (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001722
Dave Wallace2a865272018-02-07 21:00:42 -05001723 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001724 return -1;
1725
Florin Coras7baeb712019-01-04 17:05:43 -08001726 if (sh != INVALID_SESSION_ID)
Dave Wallace048b1d62018-01-03 22:24:41 -05001727 {
1728 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1729 errno = ENOSYS;
1730 size = -1;
1731 }
1732 else
1733 {
1734 func_str = "libc_recvmmsg";
1735
Dave Wallace2a865272018-02-07 21:00:42 -05001736 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001737 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1738 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
1739 getpid (), fd, fd, func_str, vmessages, vlen,
1740 flags, tmo);
1741
1742 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
1743 }
1744
Dave Wallace2a865272018-02-07 21:00:42 -05001745 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001746 {
1747 if (size < 0)
1748 {
1749 int errno_val = errno;
1750 perror (func_str);
1751 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1752 "rv %d, errno = %d", getpid (), fd, fd,
1753 func_str, size, errno_val);
1754 errno = errno_val;
1755 }
1756 else
1757 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1758 getpid (), fd, fd, size, size);
1759 }
1760 return size;
1761}
1762#endif
1763
1764int
1765getsockopt (int fd, int level, int optname,
1766 void *__restrict optval, socklen_t * __restrict optlen)
1767{
Florin Coras7baeb712019-01-04 17:05:43 -08001768 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001769 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001770
Dave Wallace2a865272018-02-07 21:00:42 -05001771 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001772 return -1;
1773
Florin Coras7baeb712019-01-04 17:05:43 -08001774 vlsh = ldp_fd_to_vlsh (fd);
1775 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001776 {
1777 rv = -EOPNOTSUPP;
1778
1779 switch (level)
1780 {
1781 case SOL_TCP:
1782 switch (optname)
1783 {
1784 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001785 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY,
1786 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001787 break;
1788 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001789 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS,
1790 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001791 break;
1792 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001793 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
1794 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001795 break;
1796 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001797 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
1798 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001799 break;
1800 case TCP_INFO:
1801 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
1802 {
Florin Coras7baeb712019-01-04 17:05:43 -08001803 LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, "
1804 "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001805 memset (optval, 0, *optlen);
1806 rv = VPPCOM_OK;
1807 }
1808 else
1809 rv = -EFAULT;
1810 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001811 case TCP_CONGESTION:
1812 strcpy (optval, "cubic");
1813 *optlen = strlen ("cubic");
1814 rv = 0;
1815 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001816 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001817 LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, "
1818 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001819 break;
1820 }
1821 break;
1822 case SOL_IPV6:
1823 switch (optname)
1824 {
1825 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001826 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001827 break;
1828 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001829 LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u "
1830 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001831 break;
1832 }
1833 break;
1834 case SOL_SOCKET:
1835 switch (optname)
1836 {
1837 case SO_ACCEPTCONN:
Florin Coras7baeb712019-01-04 17:05:43 -08001838 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001839 break;
1840 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001841 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001842 break;
1843 case SO_PROTOCOL:
Florin Coras7baeb712019-01-04 17:05:43 -08001844 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001845 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
1846 break;
1847 case SO_SNDBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001848 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN,
1849 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001850 break;
1851 case SO_RCVBUF:
Florin Coras7baeb712019-01-04 17:05:43 -08001852 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN,
1853 optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001854 break;
1855 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001856 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001857 break;
1858 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001859 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001860 break;
1861 case SO_ERROR:
Florin Coras7baeb712019-01-04 17:05:43 -08001862 rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001863 break;
1864 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001865 LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u "
1866 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001867 break;
1868 }
1869 break;
1870 default:
1871 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001872 }
1873
Dave Wallace048b1d62018-01-03 22:24:41 -05001874 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001875 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001876 errno = -rv;
1877 rv = -1;
1878 }
1879 }
1880 else
1881 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001882 rv = libc_getsockopt (fd, level, optname, optval, optlen);
1883 }
1884
Dave Wallace048b1d62018-01-03 22:24:41 -05001885 return rv;
1886}
1887
1888int
1889setsockopt (int fd, int level, int optname,
1890 const void *optval, socklen_t optlen)
1891{
Florin Coras7baeb712019-01-04 17:05:43 -08001892 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001893 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001894
Dave Wallace2a865272018-02-07 21:00:42 -05001895 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001896 return -1;
1897
Florin Coras7baeb712019-01-04 17:05:43 -08001898 vlsh = ldp_fd_to_vlsh (fd);
1899 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001900 {
1901 rv = -EOPNOTSUPP;
1902
1903 switch (level)
1904 {
1905 case SOL_TCP:
1906 switch (optname)
1907 {
1908 case TCP_NODELAY:
Florin Coras7baeb712019-01-04 17:05:43 -08001909 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY,
1910 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001911 break;
1912 case TCP_MAXSEG:
Florin Coras7baeb712019-01-04 17:05:43 -08001913 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS,
1914 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001915 break;
1916 case TCP_KEEPIDLE:
Florin Coras7baeb712019-01-04 17:05:43 -08001917 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
1918 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001919 break;
1920 case TCP_KEEPINTVL:
Florin Coras7baeb712019-01-04 17:05:43 -08001921 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
1922 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001923 break;
Florin Coras0ed24e92019-01-21 09:03:10 -08001924 case TCP_CONGESTION:
Florin Coras8509aa22019-04-04 12:55:30 -07001925 case TCP_CORK:
Florin Coras0ed24e92019-01-21 09:03:10 -08001926 /* Ignore */
1927 rv = 0;
1928 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05001929 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001930 LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u"
1931 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001932 break;
1933 }
1934 break;
1935 case SOL_IPV6:
1936 switch (optname)
1937 {
1938 case IPV6_V6ONLY:
Florin Coras7baeb712019-01-04 17:05:43 -08001939 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY,
1940 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001941 break;
1942 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001943 LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u"
1944 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001945 break;
1946 }
1947 break;
1948 case SOL_SOCKET:
1949 switch (optname)
1950 {
1951 case SO_KEEPALIVE:
Florin Coras7baeb712019-01-04 17:05:43 -08001952 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE,
1953 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001954 break;
1955 case SO_REUSEADDR:
Florin Coras7baeb712019-01-04 17:05:43 -08001956 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR,
1957 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001958 break;
1959 case SO_BROADCAST:
Florin Coras7baeb712019-01-04 17:05:43 -08001960 rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST,
1961 (void *) optval, &optlen);
Dave Wallace048b1d62018-01-03 22:24:41 -05001962 break;
1963 default:
Florin Coras7baeb712019-01-04 17:05:43 -08001964 LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u "
1965 "optname %d unsupported!", fd, vlsh, optname);
Dave Wallace048b1d62018-01-03 22:24:41 -05001966 break;
1967 }
1968 break;
1969 default:
1970 break;
1971 }
1972
1973 if (rv != VPPCOM_OK)
1974 {
1975 errno = -rv;
1976 rv = -1;
1977 }
1978 }
1979 else
1980 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001981 rv = libc_setsockopt (fd, level, optname, optval, optlen);
1982 }
1983
Dave Wallace048b1d62018-01-03 22:24:41 -05001984 return rv;
1985}
1986
1987int
1988listen (int fd, int n)
1989{
Florin Coras7baeb712019-01-04 17:05:43 -08001990 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05001991 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001992
Dave Wallace2a865272018-02-07 21:00:42 -05001993 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001994 return -1;
1995
Florin Coras7baeb712019-01-04 17:05:43 -08001996 vlsh = ldp_fd_to_vlsh (fd);
1997 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05001998 {
Florin Coras7baeb712019-01-04 17:05:43 -08001999 LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002000
Florin Coras7baeb712019-01-04 17:05:43 -08002001 rv = vls_listen (vlsh, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002002 if (rv != VPPCOM_OK)
2003 {
2004 errno = -rv;
2005 rv = -1;
2006 }
2007 }
2008 else
2009 {
Florin Coras7baeb712019-01-04 17:05:43 -08002010 LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002011 rv = libc_listen (fd, n);
2012 }
2013
Florin Coras7baeb712019-01-04 17:05:43 -08002014 LDBG (1, "fd %d: returning %d", fd, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002015 return rv;
2016}
2017
2018static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002019ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2020 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002021{
Florin Coras7baeb712019-01-04 17:05:43 -08002022 vls_handle_t listen_vlsh, accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002023 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002024
Dave Wallace2a865272018-02-07 21:00:42 -05002025 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002026 return -1;
2027
Florin Coras7baeb712019-01-04 17:05:43 -08002028 listen_vlsh = ldp_fd_to_vlsh (listen_fd);
2029 if (listen_vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002030 {
2031 vppcom_endpt_t ep;
2032 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002033 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002034 ep.ip = src_addr;
2035
Florin Coras7baeb712019-01-04 17:05:43 -08002036 LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u,"
2037 " ep %p, flags 0x%x", listen_fd, listen_vlsh, ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002038
Florin Coras7baeb712019-01-04 17:05:43 -08002039 accept_vlsh = vls_accept (listen_vlsh, &ep, flags);
2040 if (accept_vlsh < 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002041 {
Florin Coras7baeb712019-01-04 17:05:43 -08002042 errno = -accept_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002043 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002044 }
2045 else
2046 {
Dave Wallace2a865272018-02-07 21:00:42 -05002047 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002048 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002049 {
Florin Coras7baeb712019-01-04 17:05:43 -08002050 (void) vls_close (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002051 errno = -rv;
2052 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002053 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002054 else
2055 {
Florin Coras7baeb712019-01-04 17:05:43 -08002056 rv = ldp_vlsh_to_fd (accept_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002058 }
2059 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002060 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002061 {
Florin Coras7baeb712019-01-04 17:05:43 -08002062 LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p,"
2063 " flags 0x%x", listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002064
Dave Wallace048b1d62018-01-03 22:24:41 -05002065 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002066 }
2067
Florin Coras7baeb712019-01-04 17:05:43 -08002068 LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002069
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002070 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002071}
2072
Dave Wallace048b1d62018-01-03 22:24:41 -05002073int
2074accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2075 int flags)
2076{
Dave Wallace2a865272018-02-07 21:00:42 -05002077 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002078}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002079
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002080int
Dave Wallace048b1d62018-01-03 22:24:41 -05002081accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002082{
Dave Wallace2a865272018-02-07 21:00:42 -05002083 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002084}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002085
Dave Wallace048b1d62018-01-03 22:24:41 -05002086int
2087shutdown (int fd, int how)
2088{
Florin Coras7baeb712019-01-04 17:05:43 -08002089 vls_handle_t vlsh;
2090 int rv = 0, flags;
2091 u32 flags_len = sizeof (flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002092
Dave Wallace2a865272018-02-07 21:00:42 -05002093 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002094 return -1;
2095
Florin Coras7baeb712019-01-04 17:05:43 -08002096 vlsh = ldp_fd_to_vlsh (fd);
2097 if (vlsh != VLS_INVALID_HANDLE)
Dave Wallace048b1d62018-01-03 22:24:41 -05002098 {
Florin Coras7baeb712019-01-04 17:05:43 -08002099 LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how);
Florin Corasa7a1a222018-12-30 17:11:31 -08002100
Florin Coras7baeb712019-01-04 17:05:43 -08002101 if (vls_attr (vlsh, VPPCOM_ATTR_SET_SHUT, &how, &flags_len))
Florin Corasa7a1a222018-12-30 17:11:31 -08002102 {
Florin Coras7baeb712019-01-04 17:05:43 -08002103 close (fd);
Florin Corasa7a1a222018-12-30 17:11:31 -08002104 return -1;
2105 }
2106
Florin Coras7baeb712019-01-04 17:05:43 -08002107 if (vls_attr (vlsh, VPPCOM_ATTR_GET_SHUT, &flags, &flags_len))
2108 {
2109 close (fd);
2110 return -1;
2111 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002112
Florin Coras7baeb712019-01-04 17:05:43 -08002113 if (flags == SHUT_RDWR)
Florin Corasa7a1a222018-12-30 17:11:31 -08002114 rv = close (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 }
2116 else
2117 {
Florin Coras7baeb712019-01-04 17:05:43 -08002118 LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002119 rv = libc_shutdown (fd, how);
2120 }
2121
Dave Wallace048b1d62018-01-03 22:24:41 -05002122 return rv;
2123}
2124
2125int
2126epoll_create1 (int flags)
2127{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002128 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08002129 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002130 int rv;
2131
Dave Wallace2a865272018-02-07 21:00:42 -05002132 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002133 return -1;
2134
Florin Coras99368312018-08-02 10:45:44 -07002135 if (ldp->vcl_needs_real_epoll)
2136 {
Florin Coras2d9b4272019-03-11 10:14:37 -07002137 /* Make sure workers have been allocated */
2138 if (!ldp->workers)
2139 {
2140 ldp_alloc_workers ();
2141 ldpw = ldp_worker_get_current ();
2142 }
Florin Coras99368312018-08-02 10:45:44 -07002143 rv = libc_epoll_create1 (flags);
2144 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002145 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002146 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002147 return rv;
2148 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002149
Florin Coras7baeb712019-01-04 17:05:43 -08002150 vlsh = vls_epoll_create ();
2151 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002152 {
Florin Coras7baeb712019-01-04 17:05:43 -08002153 errno = -vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002154 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002155 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002156 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002157 {
Florin Coras7baeb712019-01-04 17:05:43 -08002158 rv = ldp_vlsh_to_fd (vlsh);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002159 }
Florin Coras7baeb712019-01-04 17:05:43 -08002160 LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002161 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002162}
2163
2164int
Dave Wallace048b1d62018-01-03 22:24:41 -05002165epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002166{
Dave Wallace048b1d62018-01-03 22:24:41 -05002167 return epoll_create1 (0);
2168}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002169
Dave Wallace048b1d62018-01-03 22:24:41 -05002170int
2171epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2172{
Florin Coras7baeb712019-01-04 17:05:43 -08002173 vls_handle_t vep_vlsh, vlsh;
Florin Coras99368312018-08-02 10:45:44 -07002174 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002175
Dave Wallace2a865272018-02-07 21:00:42 -05002176 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002177 return -1;
2178
Florin Coras7baeb712019-01-04 17:05:43 -08002179 vep_vlsh = ldp_fd_to_vlsh (epfd);
2180 if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002181 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002182 /* The LDP epoll_create1 always creates VCL epfd's.
2183 * The app should never have a kernel base epoll fd unless it
2184 * was acquired outside of the LD_PRELOAD process context.
2185 * In any case, if we get one, punt it to libc_epoll_ctl.
2186 */
Florin Coras7baeb712019-01-04 17:05:43 -08002187 LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d"
2188 " event %p", epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002189
2190 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002191 goto done;
2192 }
2193
Florin Coras7baeb712019-01-04 17:05:43 -08002194 vlsh = ldp_fd_to_vlsh (fd);
Florin Coras99368312018-08-02 10:45:44 -07002195
Florin Coras7baeb712019-01-04 17:05:43 -08002196 LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd,
2197 vlsh, op);
Florin Coras99368312018-08-02 10:45:44 -07002198
Florin Coras7baeb712019-01-04 17:05:43 -08002199 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras99368312018-08-02 10:45:44 -07002200 {
Florin Coras7baeb712019-01-04 17:05:43 -08002201 LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u,"
2202 " event %p", epfd, vep_vlsh, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002203
Florin Coras7baeb712019-01-04 17:05:43 -08002204 rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event);
Florin Coras99368312018-08-02 10:45:44 -07002205 if (rv != VPPCOM_OK)
2206 {
2207 errno = -rv;
2208 rv = -1;
2209 }
2210 }
2211 else
2212 {
2213 int libc_epfd;
2214 u32 size = sizeof (epfd);
2215
Florin Coras7baeb712019-01-04 17:05:43 -08002216 libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Florin Coras99368312018-08-02 10:45:44 -07002217 if (!libc_epfd)
2218 {
Florin Coras7baeb712019-01-04 17:05:43 -08002219 LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: "
2220 "EPOLL_CLOEXEC", epfd, vep_vlsh);
Florin Coras99368312018-08-02 10:45:44 -07002221
2222 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2223 if (libc_epfd < 0)
2224 {
2225 rv = libc_epfd;
2226 goto done;
2227 }
2228
Florin Coras7baeb712019-01-04 17:05:43 -08002229 rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd,
2230 &size);
Florin Coras99368312018-08-02 10:45:44 -07002231 if (rv < 0)
2232 {
2233 errno = -rv;
2234 rv = -1;
2235 goto done;
2236 }
2237 }
2238 else if (PREDICT_FALSE (libc_epfd < 0))
2239 {
2240 errno = -epfd;
2241 rv = -1;
2242 goto done;
2243 }
2244
Florin Coras7baeb712019-01-04 17:05:43 -08002245 LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d,"
2246 " event %p", epfd, libc_epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002247
2248 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002249 }
2250
2251done:
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002252 return rv;
2253}
Dave Wallace048b1d62018-01-03 22:24:41 -05002254
2255static inline int
Florin Coras99368312018-08-02 10:45:44 -07002256ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2257 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002258{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002259 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras72f77822019-01-22 19:05:52 -08002260 double time_to_wait = (double) 0, max_time;
Florin Coras99368312018-08-02 10:45:44 -07002261 int libc_epfd, rv = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002262 vls_handle_t ep_vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002263
Dave Wallace2a865272018-02-07 21:00:42 -05002264 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002265 return -1;
2266
2267 if (PREDICT_FALSE (!events || (timeout < -1)))
2268 {
2269 errno = EFAULT;
2270 return -1;
2271 }
2272
Florin Corasdfe4cf42018-11-28 22:13:45 -08002273 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07002274 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
2275
Florin Coras7baeb712019-01-04 17:05:43 -08002276 ep_vlsh = ldp_fd_to_vlsh (epfd);
2277 if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE))
Dave Wallace048b1d62018-01-03 22:24:41 -05002278 {
Florin Coras7baeb712019-01-04 17:05:43 -08002279 LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh);
Dave Wallace048b1d62018-01-03 22:24:41 -05002280 errno = EBADFD;
2281 return -1;
2282 }
2283
Florin Coras4dee8cd2019-01-29 21:28:16 -08002284 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2285 clib_time_init (&ldpw->clib_time);
Florin Corasb0f662f2018-12-27 14:51:46 -08002286 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Coras72f77822019-01-22 19:05:52 -08002287 max_time = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05002288
Florin Coras7baeb712019-01-04 17:05:43 -08002289 libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002290 if (PREDICT_FALSE (libc_epfd < 0))
2291 {
2292 errno = -libc_epfd;
2293 rv = -1;
2294 goto done;
2295 }
2296
Florin Coras7baeb712019-01-04 17:05:43 -08002297 LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, "
2298 "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh,
Florin Coras72f77822019-01-22 19:05:52 -08002299 libc_epfd, events, maxevents, timeout, sigmask, time_to_wait);
Dave Wallace048b1d62018-01-03 22:24:41 -05002300 do
2301 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002302 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05002303 {
Florin Coras7baeb712019-01-04 17:05:43 -08002304 rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002305 if (rv > 0)
2306 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002307 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05002308 goto done;
2309 }
2310 else if (rv < 0)
2311 {
2312 errno = -rv;
2313 rv = -1;
2314 goto done;
2315 }
2316 }
2317 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08002318 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002319
2320 if (libc_epfd > 0)
2321 {
Florin Corasb0f662f2018-12-27 14:51:46 -08002322 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002323 if (rv != 0)
2324 goto done;
2325 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002326 }
Florin Coras72f77822019-01-22 19:05:52 -08002327 while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002328
2329done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002330 return rv;
2331}
2332
2333int
2334epoll_pwait (int epfd, struct epoll_event *events,
2335 int maxevents, int timeout, const sigset_t * sigmask)
2336{
Dave Wallace2a865272018-02-07 21:00:42 -05002337 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05002338}
2339
2340int
2341epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
2342{
Dave Wallace2a865272018-02-07 21:00:42 -05002343 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05002344}
2345
2346int
2347poll (struct pollfd *fds, nfds_t nfds, int timeout)
2348{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002349 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras6917b942018-11-13 22:44:54 -08002350 int rv, i, n_revents = 0;
Florin Coras7baeb712019-01-04 17:05:43 -08002351 vls_handle_t vlsh;
Dave Wallace048b1d62018-01-03 22:24:41 -05002352 vcl_poll_t *vp;
Florin Coras4dee8cd2019-01-29 21:28:16 -08002353 double max_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05002354
Florin Coras05ecfcc2018-12-12 18:19:39 -08002355 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05002356
Florin Coras4dee8cd2019-01-29 21:28:16 -08002357 if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0))
2358 clib_time_init (&ldpw->clib_time);
2359
2360 max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0;
2361 max_time += clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002362
Dave Wallace048b1d62018-01-03 22:24:41 -05002363 for (i = 0; i < nfds; i++)
2364 {
Florin Coras6917b942018-11-13 22:44:54 -08002365 if (fds[i].fd < 0)
2366 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05002367
Florin Coras7baeb712019-01-04 17:05:43 -08002368 vlsh = ldp_fd_to_vlsh (fds[i].fd);
2369 if (vlsh != VLS_INVALID_HANDLE)
Florin Coras6917b942018-11-13 22:44:54 -08002370 {
2371 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002372 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08002373 vp->fds_ndx = i;
Florin Coras7baeb712019-01-04 17:05:43 -08002374 vp->sh = vlsh_to_sh (vlsh);
Florin Coras6917b942018-11-13 22:44:54 -08002375 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05002376#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08002377 if (fds[i].events & POLLRDNORM)
2378 vp->events |= POLLIN;
2379 if (fds[i].events & POLLWRNORM)
2380 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05002381#endif
Florin Coras6917b942018-11-13 22:44:54 -08002382 vp->revents = fds[i].revents;
2383 }
2384 else
2385 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002386 vec_add1 (ldpw->libc_poll, fds[i]);
2387 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05002388 }
2389 }
2390
Dave Wallace048b1d62018-01-03 22:24:41 -05002391 do
2392 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002393 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002394 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002395 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002396 if (rv < 0)
2397 {
2398 errno = -rv;
2399 rv = -1;
2400 goto done;
2401 }
2402 else
2403 n_revents += rv;
2404 }
2405
Florin Corasdfe4cf42018-11-28 22:13:45 -08002406 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05002407 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002408 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002409 if (rv < 0)
2410 goto done;
2411 else
2412 n_revents += rv;
2413 }
2414
2415 if (n_revents)
2416 {
2417 rv = n_revents;
2418 goto done;
2419 }
2420 }
Florin Coras4dee8cd2019-01-29 21:28:16 -08002421 while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05002422 rv = 0;
2423
2424done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08002425 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05002426 {
2427 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08002428 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05002429#ifdef __USE_XOPEN2K
2430 if ((fds[vp->fds_ndx].revents & POLLIN) &&
2431 (fds[vp->fds_ndx].events & POLLRDNORM))
2432 fds[vp->fds_ndx].revents |= POLLRDNORM;
2433 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
2434 (fds[vp->fds_ndx].events & POLLWRNORM))
2435 fds[vp->fds_ndx].revents |= POLLWRNORM;
2436#endif
2437 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002438 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05002439
Florin Corasdfe4cf42018-11-28 22:13:45 -08002440 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08002441 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08002442 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08002443 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08002444 vec_reset_length (ldpw->libc_poll_idxs);
2445 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08002446
Dave Wallace048b1d62018-01-03 22:24:41 -05002447 return rv;
2448}
2449
2450#ifdef USE_GNU
2451int
2452ppoll (struct pollfd *fds, nfds_t nfds,
2453 const struct timespec *timeout, const sigset_t * sigmask)
2454{
Dave Wallace2a865272018-02-07 21:00:42 -05002455 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002456 return -1;
2457
2458 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2459 errno = ENOSYS;
2460
2461
2462 return -1;
2463}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002464#endif
2465
Dave Wallace2a865272018-02-07 21:00:42 -05002466void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002467
Dave Wallace2a865272018-02-07 21:00:42 -05002468void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002469
Dave Wallace048b1d62018-01-03 22:24:41 -05002470/*
2471 * This function is called when the library is loaded
2472 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002473void
Dave Wallace2a865272018-02-07 21:00:42 -05002474ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002475{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002476 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05002477 if (ldp_init () != 0)
Florin Corasd89411e2019-03-19 19:44:51 -07002478 {
2479 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
2480 getpid ());
2481 _exit (1);
2482 }
Dave Wallace69d01192018-02-22 16:22:09 -05002483 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002484 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002485}
2486
2487/*
2488 * This function is called when the library is unloaded
2489 */
2490void
Dave Wallace2a865272018-02-07 21:00:42 -05002491ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002492{
Florin Coras0ef8ef22019-01-18 08:37:13 -08002493 /*
2494 swrap_destructor ();
2495 if (ldp->init)
2496 ldp->init = 0;
2497 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002498
2499 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05002500 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05002501 */
Dave Wallace69d01192018-02-22 16:22:09 -05002502 if (LDP_DEBUG > 0)
Florin Coras0ef8ef22019-01-18 08:37:13 -08002503 fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n",
2504 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002505}
2506
2507
2508/*
2509 * fd.io coding-style-patch-verification: ON
2510 *
2511 * Local Variables:
2512 * eval: (c-set-style "gnu")
2513 * End:
2514 */