blob: ee35396bc6324771e20c0b91bba98b823a874845 [file] [log] [blame]
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * 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
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040029#include <vcl/vppcom.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 Corasa7a1a222018-12-30 17:11:31 -080054#define LDP_F_SHUT_RD (1 << 0)
55#define LDP_F_SHUT_WR (1 << 1)
56
Florin Coras30e273b2018-11-27 00:04:59 -080057typedef struct ldp_fd_entry_
58{
Florin Corasdfe4cf42018-11-28 22:13:45 -080059 u32 session_index;
Florin Coras30e273b2018-11-27 00:04:59 -080060 u32 fd;
61 u32 fd_index;
Florin Corasa7a1a222018-12-30 17:11:31 -080062 u32 flags;
Florin Coras30e273b2018-11-27 00:04:59 -080063} ldp_fd_entry_t;
64
Florin Corasdfe4cf42018-11-28 22:13:45 -080065typedef struct ldp_worker_ctx_
Dave Wallace048b1d62018-01-03 22:24:41 -050066{
Dave Wallace048b1d62018-01-03 22:24:41 -050067 u8 *io_buffer;
68 clib_time_t clib_time;
Florin Corasdfe4cf42018-11-28 22:13:45 -080069
70 /*
71 * Select state
72 */
Dave Wallace048b1d62018-01-03 22:24:41 -050073 clib_bitmap_t *rd_bitmap;
74 clib_bitmap_t *wr_bitmap;
75 clib_bitmap_t *ex_bitmap;
76 clib_bitmap_t *sid_rd_bitmap;
77 clib_bitmap_t *sid_wr_bitmap;
78 clib_bitmap_t *sid_ex_bitmap;
79 clib_bitmap_t *libc_rd_bitmap;
80 clib_bitmap_t *libc_wr_bitmap;
81 clib_bitmap_t *libc_ex_bitmap;
Florin Corasdfe4cf42018-11-28 22:13:45 -080082 u8 select_vcl;
83
84 /*
85 * Poll state
86 */
Dave Wallace048b1d62018-01-03 22:24:41 -050087 vcl_poll_t *vcl_poll;
Florin Coras6917b942018-11-13 22:44:54 -080088 struct pollfd *libc_poll;
89 u16 *libc_poll_idxs;
Florin Corasdfe4cf42018-11-28 22:13:45 -080090
91 /*
92 * Epoll state
93 */
Dave Wallace048b1d62018-01-03 22:24:41 -050094 u8 epoll_wait_vcl;
Florin Coras99368312018-08-02 10:45:44 -070095 int vcl_mq_epfd;
Florin Corasdfe4cf42018-11-28 22:13:45 -080096
97} ldp_worker_ctx_t;
98
99typedef struct
100{
101 ldp_worker_ctx_t *workers;
102 int init;
103 char app_name[LDP_APP_NAME_MAX];
104 u32 sid_bit_val;
105 u32 sid_bit_mask;
106 u32 debug;
Florin Coras30e273b2018-11-27 00:04:59 -0800107 ldp_fd_entry_t *fd_pool;
108 clib_rwlock_t fd_table_lock;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800109 uword *session_index_to_fd_table;
110
111 /** vcl needs next epoll_create to go to libc_epoll */
112 u8 vcl_needs_real_epoll;
Dave Wallace2a865272018-02-07 21:00:42 -0500113} ldp_main_t;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800114
Dave Wallace2a865272018-02-07 21:00:42 -0500115#define LDP_DEBUG ldp->debug
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700116
Florin Coras99368312018-08-02 10:45:44 -0700117#define LDBG(_lvl, _fmt, _args...) \
118 if (ldp->debug > _lvl) \
Florin Coras05ecfcc2018-12-12 18:19:39 -0800119 clib_warning ("ldp<%d>: " _fmt, getpid(), ##_args)
Florin Coras99368312018-08-02 10:45:44 -0700120
Dave Wallace2a865272018-02-07 21:00:42 -0500121static ldp_main_t ldp_main = {
122 .sid_bit_val = (1 << LDP_SID_BIT_MIN),
123 .sid_bit_mask = (1 << LDP_SID_BIT_MIN) - 1,
124 .debug = LDP_DEBUG_INIT,
Dave Wallace048b1d62018-01-03 22:24:41 -0500125};
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700126
Dave Wallace2a865272018-02-07 21:00:42 -0500127static ldp_main_t *ldp = &ldp_main;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700128
Florin Corasdfe4cf42018-11-28 22:13:45 -0800129static inline ldp_worker_ctx_t *
130ldp_worker_get_current (void)
131{
132 return (ldp->workers + vppcom_worker_index ());
133}
134
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700135/*
136 * RETURN: 0 on success or -1 on error.
137 * */
Dave Wallace048b1d62018-01-03 22:24:41 -0500138static inline void
Dave Wallace2a865272018-02-07 21:00:42 -0500139ldp_set_app_name (char *app_name)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700140{
Dave Wallace2a865272018-02-07 21:00:42 -0500141 int rv = snprintf (ldp->app_name, LDP_APP_NAME_MAX,
142 "ldp-%d-%s", getpid (), app_name);
Dave Wallace048b1d62018-01-03 22:24:41 -0500143
Dave Wallace2a865272018-02-07 21:00:42 -0500144 if (rv >= LDP_APP_NAME_MAX)
145 app_name[LDP_APP_NAME_MAX - 1] = 0;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700146}
147
Dave Wallace048b1d62018-01-03 22:24:41 -0500148static inline char *
Dave Wallace2a865272018-02-07 21:00:42 -0500149ldp_get_app_name ()
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700150{
Dave Wallace2a865272018-02-07 21:00:42 -0500151 if (ldp->app_name[0] == '\0')
152 ldp_set_app_name ("app");
Dave Wallace048b1d62018-01-03 22:24:41 -0500153
Dave Wallace2a865272018-02-07 21:00:42 -0500154 return ldp->app_name;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700155}
156
Florin Coras30e273b2018-11-27 00:04:59 -0800157static int
158ldp_fd_alloc (u32 sid)
159{
160 ldp_fd_entry_t *fde;
161
162 clib_rwlock_writer_lock (&ldp->fd_table_lock);
163 if (pool_elts (ldp->fd_pool) >= (1ULL << 32) - ldp->sid_bit_val)
164 {
165 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
166 return -1;
167 }
168 pool_get (ldp->fd_pool, fde);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800169 fde->session_index = vppcom_session_index (sid);
Florin Coras30e273b2018-11-27 00:04:59 -0800170 fde->fd_index = fde - ldp->fd_pool;
171 fde->fd = fde->fd_index + ldp->sid_bit_val;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800172 hash_set (ldp->session_index_to_fd_table, fde->session_index, fde->fd);
Florin Coras30e273b2018-11-27 00:04:59 -0800173 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
174 return fde->fd;
175}
176
177static ldp_fd_entry_t *
178ldp_fd_entry_get_w_lock (u32 fd_index)
179{
180 clib_rwlock_reader_lock (&ldp->fd_table_lock);
181 if (pool_is_free_index (ldp->fd_pool, fd_index))
182 return 0;
183
184 return pool_elt_at_index (ldp->fd_pool, fd_index);
185}
186
Dave Wallace048b1d62018-01-03 22:24:41 -0500187static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500188ldp_fd_from_sid (u32 sid)
Dave Wallace048b1d62018-01-03 22:24:41 -0500189{
Florin Coras30e273b2018-11-27 00:04:59 -0800190 uword *fdp;
191 int fd;
192
193 clib_rwlock_reader_lock (&ldp->fd_table_lock);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800194 fdp = hash_get (ldp->session_index_to_fd_table, vppcom_session_index (sid));
Florin Coras30e273b2018-11-27 00:04:59 -0800195 fd = fdp ? *fdp : -EMFILE;
196 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
197
198 return fd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500199}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700200
Dave Wallace048b1d62018-01-03 22:24:41 -0500201static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500202ldp_fd_is_sid (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500203{
Florin Coras30e273b2018-11-27 00:04:59 -0800204 return fd >= ldp->sid_bit_val;
Dave Wallace048b1d62018-01-03 22:24:41 -0500205}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700206
Dave Wallace048b1d62018-01-03 22:24:41 -0500207static inline u32
Dave Wallace2a865272018-02-07 21:00:42 -0500208ldp_sid_from_fd (int fd)
Dave Wallace048b1d62018-01-03 22:24:41 -0500209{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800210 u32 fd_index, session_index;
Florin Coras30e273b2018-11-27 00:04:59 -0800211 ldp_fd_entry_t *fde;
Florin Coras30e273b2018-11-27 00:04:59 -0800212
213 if (!ldp_fd_is_sid (fd))
214 return INVALID_SESSION_ID;
215
216 fd_index = fd - ldp->sid_bit_val;
217 fde = ldp_fd_entry_get_w_lock (fd_index);
Florin Corasdfe4cf42018-11-28 22:13:45 -0800218 if (!fde)
219 {
220 LDBG (0, "unknown fd %d", fd);
221 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
222 return INVALID_SESSION_ID;
223 }
224 session_index = fde->session_index;
Florin Coras30e273b2018-11-27 00:04:59 -0800225 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
226
Florin Corasdfe4cf42018-11-28 22:13:45 -0800227 return vppcom_session_handle (session_index);
Florin Coras30e273b2018-11-27 00:04:59 -0800228}
229
230static void
231ldp_fd_free_w_sid (u32 sid)
232{
233 ldp_fd_entry_t *fde;
234 u32 fd_index;
235 int fd;
236
237 fd = ldp_fd_from_sid (sid);
238 if (!fd)
239 return;
240
241 fd_index = fd - ldp->sid_bit_val;
242 fde = ldp_fd_entry_get_w_lock (fd_index);
243 if (fde)
244 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800245 hash_unset (ldp->session_index_to_fd_table, fde->session_index);
Florin Coras30e273b2018-11-27 00:04:59 -0800246 pool_put (ldp->fd_pool, fde);
247 }
248 clib_rwlock_writer_unlock (&ldp->fd_table_lock);
Dave Wallace048b1d62018-01-03 22:24:41 -0500249}
250
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700251static inline int
Dave Wallace2a865272018-02-07 21:00:42 -0500252ldp_init (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700253{
Florin Corasdfe4cf42018-11-28 22:13:45 -0800254 ldp_worker_ctx_t *ldpw;
Florin Coras99368312018-08-02 10:45:44 -0700255 int rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700256
Florin Coras99368312018-08-02 10:45:44 -0700257 if (PREDICT_TRUE (ldp->init))
258 return 0;
259
260 ldp->init = 1;
261 ldp->vcl_needs_real_epoll = 1;
262 rv = vppcom_app_create (ldp_get_app_name ());
263 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700264 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800265 ldp->vcl_needs_real_epoll = 0;
266 if (rv == VPPCOM_EEXIST)
267 return 0;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800268 LDBG (2, "\nERROR: ldp_init: vppcom_app_create()"
269 " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv));
Florin Coras99368312018-08-02 10:45:44 -0700270 ldp->init = 0;
271 return rv;
272 }
273 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800274 pool_alloc (ldp->workers, LDP_MAX_NWORKERS);
275 ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700276
277 char *env_var_str = getenv (LDP_ENV_DEBUG);
278 if (env_var_str)
279 {
280 u32 tmp;
281 if (sscanf (env_var_str, "%u", &tmp) != 1)
282 clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in"
283 " the env var " LDP_ENV_DEBUG " (%s)!", getpid (),
284 env_var_str);
285 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700286 {
Florin Coras99368312018-08-02 10:45:44 -0700287 ldp->debug = tmp;
Florin Coras05ecfcc2018-12-12 18:19:39 -0800288 LDBG (0, "configured LDP debug level (%u) from env var "
289 LDP_ENV_DEBUG "!", ldp->debug);
Florin Coras99368312018-08-02 10:45:44 -0700290 }
291 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500292
Florin Coras99368312018-08-02 10:45:44 -0700293 env_var_str = getenv (LDP_ENV_APP_NAME);
294 if (env_var_str)
295 {
296 ldp_set_app_name (env_var_str);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800297 LDBG (0, "configured LDP app name (%s) from the env var "
298 LDP_ENV_APP_NAME "!", ldp->app_name);
Florin Coras99368312018-08-02 10:45:44 -0700299 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500300
Florin Coras99368312018-08-02 10:45:44 -0700301 env_var_str = getenv (LDP_ENV_SID_BIT);
302 if (env_var_str)
303 {
304 u32 sb;
305 if (sscanf (env_var_str, "%u", &sb) != 1)
306 {
307 clib_warning ("LDP<%d>: WARNING: Invalid LDP sid bit specified in"
308 " the env var " LDP_ENV_SID_BIT " (%s)! sid bit "
309 "value %d (0x%x)", getpid (), env_var_str,
310 ldp->sid_bit_val, ldp->sid_bit_val);
311 }
312 else if (sb < LDP_SID_BIT_MIN)
313 {
314 ldp->sid_bit_val = (1 << LDP_SID_BIT_MIN);
315 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500316
Florin Coras99368312018-08-02 10:45:44 -0700317 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
318 " env var " LDP_ENV_SID_BIT " (%s) is too small. "
319 "Using LDP_SID_BIT_MIN (%d)! sid bit value %d (0x%x)",
320 getpid (), sb, env_var_str, LDP_SID_BIT_MIN,
321 ldp->sid_bit_val, ldp->sid_bit_val);
322 }
323 else if (sb > LDP_SID_BIT_MAX)
324 {
325 ldp->sid_bit_val = (1 << LDP_SID_BIT_MAX);
326 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500327
Florin Coras99368312018-08-02 10:45:44 -0700328 clib_warning ("LDP<%d>: WARNING: LDP sid bit (%u) specified in the"
329 " env var " LDP_ENV_SID_BIT " (%s) is too big. Using"
330 " LDP_SID_BIT_MAX (%d)! sid bit value %d (0x%x)",
331 getpid (), sb, env_var_str, LDP_SID_BIT_MAX,
332 ldp->sid_bit_val, ldp->sid_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500333 }
334 else
335 {
Florin Coras99368312018-08-02 10:45:44 -0700336 ldp->sid_bit_val = (1 << sb);
337 ldp->sid_bit_mask = ldp->sid_bit_val - 1;
338
Florin Coras05ecfcc2018-12-12 18:19:39 -0800339 LDBG (0, "configured LDP sid bit (%u) from "
340 LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb,
Florin Coras99368312018-08-02 10:45:44 -0700341 ldp->sid_bit_val, ldp->sid_bit_val);
Dave Wallace048b1d62018-01-03 22:24:41 -0500342 }
343 }
Florin Coras99368312018-08-02 10:45:44 -0700344
Florin Corasdfe4cf42018-11-28 22:13:45 -0800345 clib_time_init (&ldpw->clib_time);
Florin Coras30e273b2018-11-27 00:04:59 -0800346 clib_rwlock_init (&ldp->fd_table_lock);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800347 LDBG (0, "LDP initialization: done!");
Florin Coras99368312018-08-02 10:45:44 -0700348
349 return 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500350}
351
352int
353close (int fd)
354{
Florin Coras47c40e22018-11-26 17:01:36 -0800355 int rv, refcnt;
Dave Wallace2a865272018-02-07 21:00:42 -0500356 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500357
Dave Wallace2a865272018-02-07 21:00:42 -0500358 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500359 return -1;
360
361 if (sid != INVALID_SESSION_ID)
362 {
363 int epfd;
364
Dave Wallace048b1d62018-01-03 22:24:41 -0500365 epfd = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
366 if (epfd > 0)
367 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800368 LDBG (0, "fd %d (0x%x): calling libc_close: epfd %u (0x%x)",
369 fd, fd, epfd, epfd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500370
371 rv = libc_close (epfd);
372 if (rv < 0)
373 {
374 u32 size = sizeof (epfd);
375 epfd = 0;
376
377 (void) vppcom_session_attr (sid, VPPCOM_ATTR_SET_LIBC_EPFD,
378 &epfd, &size);
379 }
380 }
381 else if (PREDICT_FALSE (epfd < 0))
382 {
383 errno = -epfd;
384 rv = -1;
385 goto done;
386 }
387
Florin Coras05ecfcc2018-12-12 18:19:39 -0800388 LDBG (0, "fd %d (0x%x): calling vppcom_session_close: sid %u (0x%x)",
389 fd, fd, sid, sid);
Dave Wallace048b1d62018-01-03 22:24:41 -0500390
Florin Coras47c40e22018-11-26 17:01:36 -0800391 refcnt = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REFCNT, 0, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500392 rv = vppcom_session_close (sid);
393 if (rv != VPPCOM_OK)
394 {
395 errno = -rv;
396 rv = -1;
397 }
Florin Corasb0f662f2018-12-27 14:51:46 -0800398 if (refcnt <= 1)
Florin Coras47c40e22018-11-26 17:01:36 -0800399 ldp_fd_free_w_sid (sid);
Dave Wallace048b1d62018-01-03 22:24:41 -0500400 }
401 else
402 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800403 LDBG (0, "fd %d (0x%x): calling libc_close", fd, fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500404 rv = libc_close (fd);
405 }
406
407done:
Florin Coras05ecfcc2018-12-12 18:19:39 -0800408
409 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -0500410 return rv;
411}
412
413ssize_t
414read (int fd, void *buf, size_t nbytes)
415{
416 ssize_t size;
Dave Wallace2a865272018-02-07 21:00:42 -0500417 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500418
Dave Wallace2a865272018-02-07 21:00:42 -0500419 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500420 return -1;
421
422 if (sid != INVALID_SESSION_ID)
423 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800424 LDBG (2, "fd %d (0x%x): calling vppcom_session_read(): sid %u (0x%x),"
425 " buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500426
427 size = vppcom_session_read (sid, buf, nbytes);
428 if (size < 0)
429 {
430 errno = -size;
431 size = -1;
432 }
433 }
434 else
435 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800436 LDBG (2, "fd %d (0x%x): calling libc_read(): buf %p, nbytes %u",
437 fd, fd, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500438
439 size = libc_read (fd, buf, nbytes);
440 }
441
Florin Coras05ecfcc2018-12-12 18:19:39 -0800442 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500443 return size;
444}
445
446ssize_t
447readv (int fd, const struct iovec * iov, int iovcnt)
448{
Dave Wallace048b1d62018-01-03 22:24:41 -0500449 ssize_t size = 0;
Dave Wallace2a865272018-02-07 21:00:42 -0500450 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace8aaba562018-01-18 17:21:19 -0500451 int rv = 0, i, total = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500452
Dave Wallace2a865272018-02-07 21:00:42 -0500453 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500454 return -1;
455
456 if (sid != INVALID_SESSION_ID)
457 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500458 do
459 {
460 for (i = 0; i < iovcnt; ++i)
461 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800462 LDBG (2, "fd %d (0x%x): calling vppcom_session_read() [%d]:"
463 " sid %u (0x%x), iov %p, iovcnt %d, total %d", fd, fd, i,
464 sid, sid, iov, iovcnt, total);
Dave Wallace048b1d62018-01-03 22:24:41 -0500465
466 rv = vppcom_session_read (sid, iov[i].iov_base, iov[i].iov_len);
467 if (rv < 0)
468 break;
469 else
470 {
471 total += rv;
472 if (rv < iov[i].iov_len)
473 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800474 LDBG (2, "fd %d (0x%x): rv (%d) < iov[%d].iov_len (%d)",
475 fd, fd, rv, i, iov[i].iov_len);
Dave Wallace048b1d62018-01-03 22:24:41 -0500476 break;
477 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700478 }
479 }
480 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500481 while ((rv >= 0) && (total == 0));
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700482
Dave Wallace048b1d62018-01-03 22:24:41 -0500483 if (rv < 0)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700484 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500485 errno = -rv;
486 size = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700487 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500488 else
489 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700490 }
491 else
492 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800493 LDBG (2, "fd %d (0x%x): calling libc_readv(): iov %p, iovcnt %d", fd,
494 fd, iov, iovcnt);
Dave Wallace048b1d62018-01-03 22:24:41 -0500495
496 size = libc_readv (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700497 }
498
Florin Coras05ecfcc2018-12-12 18:19:39 -0800499
500 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500501 return size;
502}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700503
Dave Wallace048b1d62018-01-03 22:24:41 -0500504ssize_t
505write (int fd, const void *buf, size_t nbytes)
506{
Dave Wallace048b1d62018-01-03 22:24:41 -0500507 ssize_t size = 0;
Dave Wallace2a865272018-02-07 21:00:42 -0500508 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500509
Dave Wallace2a865272018-02-07 21:00:42 -0500510 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500511 return -1;
512
513 if (sid != INVALID_SESSION_ID)
514 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800515 LDBG (2, "fd %d (0x%x): calling vppcom_session_write(): sid %u (0x%x), "
516 "buf %p, nbytes %u", fd, fd, sid, sid, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500517
Florin Corasb0f662f2018-12-27 14:51:46 -0800518 size = vppcom_session_write_msg (sid, (void *) buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500519 if (size < 0)
520 {
521 errno = -size;
522 size = -1;
523 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700524 }
525 else
526 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800527 LDBG (2, "fd %d (0x%x): calling libc_write(): buf %p, nbytes %u",
528 fd, fd, buf, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500529
530 size = libc_write (fd, buf, nbytes);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700531 }
532
Florin Coras05ecfcc2018-12-12 18:19:39 -0800533 LDBG (2, "fd %d (0x%x): returning %d (0x%x)", fd, fd, size, size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500534 return size;
535}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700536
Dave Wallace048b1d62018-01-03 22:24:41 -0500537ssize_t
538writev (int fd, const struct iovec * iov, int iovcnt)
539{
Dave Wallace048b1d62018-01-03 22:24:41 -0500540 ssize_t size = 0, total = 0;
Dave Wallace2a865272018-02-07 21:00:42 -0500541 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace8aaba562018-01-18 17:21:19 -0500542 int i, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500543
544 /*
545 * Use [f]printf() instead of clib_warning() to prevent recursion SIGSEGV.
546 */
547
Dave Wallace2a865272018-02-07 21:00:42 -0500548 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500549 return -1;
550
551 if (sid != INVALID_SESSION_ID)
552 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500553 do
554 {
555 for (i = 0; i < iovcnt; ++i)
556 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800557 rv = vppcom_session_write_msg (sid, iov[i].iov_base,
558 iov[i].iov_len);
Dave Wallace048b1d62018-01-03 22:24:41 -0500559 if (rv < 0)
560 break;
561 else
562 {
563 total += rv;
564 if (rv < iov[i].iov_len)
Florin Corasb0f662f2018-12-27 14:51:46 -0800565 break;
Dave Wallace048b1d62018-01-03 22:24:41 -0500566 }
567 }
568 }
569 while ((rv >= 0) && (total == 0));
570
571 if (rv < 0)
572 {
573 errno = -rv;
574 size = -1;
575 }
576 else
577 size = total;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700578 }
579 else
580 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500581 size = libc_writev (fd, iov, iovcnt);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700582 }
583
Dave Wallace048b1d62018-01-03 22:24:41 -0500584 return size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700585}
586
587int
Dave Wallace048b1d62018-01-03 22:24:41 -0500588fcntl (int fd, int cmd, ...)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700589{
Dave Wallace048b1d62018-01-03 22:24:41 -0500590 const char *func_str = __func__;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700591 int rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -0500592 va_list ap;
Dave Wallace2a865272018-02-07 21:00:42 -0500593 u32 sid = ldp_sid_from_fd (fd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700594
Dave Wallace2a865272018-02-07 21:00:42 -0500595 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500596 return -1;
597
598 va_start (ap, cmd);
599 if (sid != INVALID_SESSION_ID)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700600 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500601 int flags = va_arg (ap, int);
602 u32 size;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700603
Dave Wallace048b1d62018-01-03 22:24:41 -0500604 size = sizeof (flags);
605 rv = -EOPNOTSUPP;
606 switch (cmd)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700607 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500608 case F_SETFL:
609 func_str = "vppcom_session_attr[SET_FLAGS]";
Florin Coras05ecfcc2018-12-12 18:19:39 -0800610 LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x) "
611 "flags %d (0x%x), size %d", fd, fd, func_str, sid,
Florin Coras173bae32018-11-16 18:56:28 -0800612 sid, flags, flags, size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500613
Florin Coras173bae32018-11-16 18:56:28 -0800614 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
615 &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500616 break;
617
618 case F_GETFL:
619 func_str = "vppcom_session_attr[GET_FLAGS]";
Florin Coras05ecfcc2018-12-12 18:19:39 -0800620 LDBG (2, "fd %d (0x%x): calling %s(): sid %u (0x%x), "
621 "flags %d (0x%x), size %d", fd, fd, func_str, sid,
Florin Coras173bae32018-11-16 18:56:28 -0800622 sid, flags, flags, size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500623
Florin Coras173bae32018-11-16 18:56:28 -0800624 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
625 &size);
Dave Wallace048b1d62018-01-03 22:24:41 -0500626 if (rv == VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700627 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800628 LDBG (2, "fd %d (0x%x), cmd %d (F_GETFL): %s() "
629 "returned flags %d (0x%x)", fd, fd, cmd,
Florin Coras173bae32018-11-16 18:56:28 -0800630 func_str, flags, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -0500631 rv = flags;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700632 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700633 break;
Florin Coras173bae32018-11-16 18:56:28 -0800634 case F_SETFD:
635 /* TODO handle this */
636 LDBG (0, "F_SETFD ignored flags %u", flags);
637 rv = 0;
638 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700639 default:
Dave Wallace048b1d62018-01-03 22:24:41 -0500640 rv = -EOPNOTSUPP;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700641 break;
642 }
Dave Wallace048b1d62018-01-03 22:24:41 -0500643 if (rv < 0)
644 {
645 errno = -rv;
646 rv = -1;
647 }
648 }
649 else
650 {
651 func_str = "libc_vfcntl";
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700652
Florin Coras05ecfcc2018-12-12 18:19:39 -0800653 LDBG (2, "fd %d (0x%x): calling %s(): cmd %d", fd, fd, func_str, cmd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700654
Dave Wallace048b1d62018-01-03 22:24:41 -0500655 rv = libc_vfcntl (fd, cmd, ap);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -0700656 }
657
Dave Wallace048b1d62018-01-03 22:24:41 -0500658 va_end (ap);
659
Dave Wallace2a865272018-02-07 21:00:42 -0500660 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500661 {
662 if (rv < 0)
663 {
664 int errno_val = errno;
665 perror (func_str);
666 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
667 "rv %d, errno = %d", getpid (), fd, fd,
668 func_str, rv, errno_val);
669 errno = errno_val;
670 }
671 else
672 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
673 getpid (), fd, fd, rv, rv);
674 }
675 return rv;
676}
677
678int
679ioctl (int fd, unsigned long int cmd, ...)
680{
681 const char *func_str;
682 int rv;
683 va_list ap;
Dave Wallace2a865272018-02-07 21:00:42 -0500684 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -0500685
Dave Wallace2a865272018-02-07 21:00:42 -0500686 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -0500687 return -1;
688
689 va_start (ap, cmd);
690 if (sid != INVALID_SESSION_ID)
691 {
692 func_str = "vppcom_session_attr[GET_NREAD]";
693
694 switch (cmd)
695 {
696 case FIONREAD:
Dave Wallace2a865272018-02-07 21:00:42 -0500697 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500698 clib_warning
699 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x)",
700 getpid (), fd, fd, func_str, sid, sid);
701
702 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NREAD, 0, 0);
703 break;
704
705 case FIONBIO:
706 {
707 u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0;
708 u32 size = sizeof (flags);
709
710 /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than
711 * non-blocking, the flags should be read here and merged
712 * with O_NONBLOCK.
713 */
Dave Wallace2a865272018-02-07 21:00:42 -0500714 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500715 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
716 "sid %u (0x%x), flags %d (0x%x), size %d",
717 getpid (), fd, fd, func_str, sid, sid,
718 flags, flags, size);
719
720 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_FLAGS, &flags,
721 &size);
722 }
723 break;
724
725 default:
726 rv = -EOPNOTSUPP;
727 break;
728 }
729 if (rv < 0)
730 {
731 errno = -rv;
732 rv = -1;
733 }
734 }
735 else
736 {
737 func_str = "libc_vioctl";
738
Dave Wallace2a865272018-02-07 21:00:42 -0500739 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500740 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): cmd %d",
741 getpid (), fd, fd, func_str, cmd);
742
743 rv = libc_vioctl (fd, cmd, ap);
744 }
745
Dave Wallace2a865272018-02-07 21:00:42 -0500746 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -0500747 {
748 if (rv < 0)
749 {
750 int errno_val = errno;
751 perror (func_str);
752 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
753 "rv %d, errno = %d", getpid (), fd, fd,
754 func_str, rv, errno_val);
755 errno = errno_val;
756 }
757 else
758 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
759 getpid (), fd, fd, rv, rv);
760 }
761 va_end (ap);
762 return rv;
763}
764
765int
Dave Wallace2a865272018-02-07 21:00:42 -0500766ldp_pselect (int nfds, fd_set * __restrict readfds,
767 fd_set * __restrict writefds,
768 fd_set * __restrict exceptfds,
769 const struct timespec *__restrict timeout,
770 const __sigset_t * __restrict sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -0500771{
Florin Coras30e273b2018-11-27 00:04:59 -0800772 uword sid_bits, sid_bits_set, libc_bits, libc_bits_set;
Florin Corasdfe4cf42018-11-28 22:13:45 -0800773 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras30e273b2018-11-27 00:04:59 -0800774 u32 minbits = clib_max (nfds, BITS (uword)), sid;
Dave Wallace048b1d62018-01-03 22:24:41 -0500775 char *func_str = "##";
776 f64 time_out;
Florin Coras30e273b2018-11-27 00:04:59 -0800777 int rv, fd;
Dave Wallace048b1d62018-01-03 22:24:41 -0500778
779 if (nfds < 0)
780 {
781 errno = EINVAL;
782 return -1;
783 }
784
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500785 if (timeout)
786 {
787 time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ?
788 (f64) 0 : (f64) timeout->tv_sec +
789 (f64) timeout->tv_nsec / (f64) 1000000000;
790
791 /* select as fine grained sleep */
792 if (!nfds)
793 {
Florin Coras05ecfcc2018-12-12 18:19:39 -0800794 LDBG (3, "sleeping for %.02f seconds", time_out);
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500795
Florin Corasdfe4cf42018-11-28 22:13:45 -0800796 time_out += clib_time_now (&ldpw->clib_time);
797 while (clib_time_now (&ldpw->clib_time) < time_out)
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500798 ;
799 return 0;
800 }
801 }
802 else if (!nfds)
803 {
804 errno = EINVAL;
805 return -1;
806 }
807 else
808 time_out = -1;
809
810
Dave Wallace2a865272018-02-07 21:00:42 -0500811 if (nfds <= ldp->sid_bit_val)
Dave Wallace048b1d62018-01-03 22:24:41 -0500812 {
813 func_str = "libc_pselect";
814
Florin Coras05ecfcc2018-12-12 18:19:39 -0800815 LDBG (3, "calling %s(): nfds %d, readfds %p, writefds %p, "
816 "exceptfds %p, timeout %p, sigmask %p", func_str, nfds,
Florin Coras173bae32018-11-16 18:56:28 -0800817 readfds, writefds, exceptfds, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -0500818
819 rv = libc_pselect (nfds, readfds, writefds, exceptfds,
820 timeout, sigmask);
821 goto done;
822 }
823
Dave Wallace2a865272018-02-07 21:00:42 -0500824 if (PREDICT_FALSE (ldp->sid_bit_val > FD_SETSIZE / 2))
Dave Wallace048b1d62018-01-03 22:24:41 -0500825 {
Dave Wallace2a865272018-02-07 21:00:42 -0500826 clib_warning ("LDP<%d>: ERROR: LDP sid bit value %d (0x%x) > "
Dave Wallace048b1d62018-01-03 22:24:41 -0500827 "FD_SETSIZE/2 %d (0x%x)!", getpid (),
Dave Wallace2a865272018-02-07 21:00:42 -0500828 ldp->sid_bit_val, ldp->sid_bit_val,
Dave Wallace048b1d62018-01-03 22:24:41 -0500829 FD_SETSIZE / 2, FD_SETSIZE / 2);
830 errno = EOVERFLOW;
831 return -1;
832 }
833
Dave Wallace048b1d62018-01-03 22:24:41 -0500834 sid_bits = libc_bits = 0;
Florin Coras173bae32018-11-16 18:56:28 -0800835 u32 n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500836 if (readfds)
837 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800838 clib_bitmap_validate (ldpw->sid_rd_bitmap, minbits);
839 clib_bitmap_validate (ldpw->libc_rd_bitmap, minbits);
840 clib_bitmap_validate (ldpw->rd_bitmap, minbits);
841 clib_memcpy_fast (ldpw->rd_bitmap, readfds, n_bytes);
Florin Coras173bae32018-11-16 18:56:28 -0800842 memset (readfds, 0, n_bytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500843
844 /* *INDENT-OFF* */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800845 clib_bitmap_foreach (fd, ldpw->rd_bitmap, ({
Florin Coras173bae32018-11-16 18:56:28 -0800846 if (fd > nfds)
847 break;
848 sid = ldp_sid_from_fd (fd);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800849 LDBG (3, "readfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
Florin Coras173bae32018-11-16 18:56:28 -0800850 if (sid == INVALID_SESSION_ID)
Florin Corasdfe4cf42018-11-28 22:13:45 -0800851 clib_bitmap_set_no_check (ldpw->libc_rd_bitmap, fd, 1);
Florin Coras173bae32018-11-16 18:56:28 -0800852 else
Florin Corasdfe4cf42018-11-28 22:13:45 -0800853 clib_bitmap_set_no_check (ldpw->sid_rd_bitmap,
Florin Coras30e273b2018-11-27 00:04:59 -0800854 vppcom_session_index (sid), 1);
Florin Coras173bae32018-11-16 18:56:28 -0800855 }));
Dave Wallace048b1d62018-01-03 22:24:41 -0500856 /* *INDENT-ON* */
857
Florin Corasdfe4cf42018-11-28 22:13:45 -0800858 sid_bits_set = clib_bitmap_last_set (ldpw->sid_rd_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500859 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
860
Florin Corasdfe4cf42018-11-28 22:13:45 -0800861 libc_bits_set = clib_bitmap_last_set (ldpw->libc_rd_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500862 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
863
Florin Coras05ecfcc2018-12-12 18:19:39 -0800864 LDBG (3, "readfds: sid_bits_set %d, sid_bits %d, "
865 "libc_bits_set %d, libc_bits %d", sid_bits_set,
Florin Coras173bae32018-11-16 18:56:28 -0800866 sid_bits, libc_bits_set, libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500867 }
868 if (writefds)
869 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800870 clib_bitmap_validate (ldpw->sid_wr_bitmap, minbits);
871 clib_bitmap_validate (ldpw->libc_wr_bitmap, minbits);
872 clib_bitmap_validate (ldpw->wr_bitmap, minbits);
873 clib_memcpy_fast (ldpw->wr_bitmap, writefds, n_bytes);
Florin Coras173bae32018-11-16 18:56:28 -0800874 memset (writefds, 0, n_bytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500875
876 /* *INDENT-OFF* */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800877 clib_bitmap_foreach (fd, ldpw->wr_bitmap, ({
Florin Coras173bae32018-11-16 18:56:28 -0800878 if (fd > nfds)
879 break;
880 sid = ldp_sid_from_fd (fd);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800881 LDBG (3, "writefds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
Florin Coras173bae32018-11-16 18:56:28 -0800882 if (sid == INVALID_SESSION_ID)
Florin Corasdfe4cf42018-11-28 22:13:45 -0800883 clib_bitmap_set_no_check (ldpw->libc_wr_bitmap, fd, 1);
Florin Coras173bae32018-11-16 18:56:28 -0800884 else
Florin Corasdfe4cf42018-11-28 22:13:45 -0800885 clib_bitmap_set_no_check (ldpw->sid_wr_bitmap,
Florin Coras30e273b2018-11-27 00:04:59 -0800886 vppcom_session_index (sid), 1);
Florin Coras173bae32018-11-16 18:56:28 -0800887 }));
Dave Wallace048b1d62018-01-03 22:24:41 -0500888 /* *INDENT-ON* */
889
Florin Corasdfe4cf42018-11-28 22:13:45 -0800890 sid_bits_set = clib_bitmap_last_set (ldpw->sid_wr_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500891 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
892
Florin Corasdfe4cf42018-11-28 22:13:45 -0800893 libc_bits_set = clib_bitmap_last_set (ldpw->libc_wr_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500894 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
895
Florin Coras05ecfcc2018-12-12 18:19:39 -0800896 LDBG (3, "writefds: sid_bits_set %d, sid_bits %d, "
897 "libc_bits_set %d, libc_bits %d",
Florin Coras173bae32018-11-16 18:56:28 -0800898 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500899 }
900 if (exceptfds)
901 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800902 clib_bitmap_validate (ldpw->sid_ex_bitmap, minbits);
903 clib_bitmap_validate (ldpw->libc_ex_bitmap, minbits);
904 clib_bitmap_validate (ldpw->ex_bitmap, minbits);
905 clib_memcpy_fast (ldpw->ex_bitmap, exceptfds, n_bytes);
Florin Coras173bae32018-11-16 18:56:28 -0800906 memset (exceptfds, 0, n_bytes);
Dave Wallace048b1d62018-01-03 22:24:41 -0500907
908 /* *INDENT-OFF* */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800909 clib_bitmap_foreach (fd, ldpw->ex_bitmap, ({
Florin Coras173bae32018-11-16 18:56:28 -0800910 if (fd > nfds)
911 break;
912 sid = ldp_sid_from_fd (fd);
Florin Coras05ecfcc2018-12-12 18:19:39 -0800913 LDBG (3, "exceptfds: fd %d (0x%x), sid %u (0x%x)", fd, fd, sid, sid);
Florin Coras173bae32018-11-16 18:56:28 -0800914 if (sid == INVALID_SESSION_ID)
Florin Corasdfe4cf42018-11-28 22:13:45 -0800915 clib_bitmap_set_no_check (ldpw->libc_ex_bitmap, fd, 1);
Florin Coras173bae32018-11-16 18:56:28 -0800916 else
Florin Corasdfe4cf42018-11-28 22:13:45 -0800917 clib_bitmap_set_no_check (ldpw->sid_ex_bitmap,
Florin Coras30e273b2018-11-27 00:04:59 -0800918 vppcom_session_index (sid), 1);
Florin Coras173bae32018-11-16 18:56:28 -0800919 }));
Dave Wallace048b1d62018-01-03 22:24:41 -0500920 /* *INDENT-ON* */
921
Florin Corasdfe4cf42018-11-28 22:13:45 -0800922 sid_bits_set = clib_bitmap_last_set (ldpw->sid_ex_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500923 sid_bits = (sid_bits_set > sid_bits) ? sid_bits_set : sid_bits;
924
Florin Corasdfe4cf42018-11-28 22:13:45 -0800925 libc_bits_set = clib_bitmap_last_set (ldpw->libc_ex_bitmap) + 1;
Dave Wallace048b1d62018-01-03 22:24:41 -0500926 libc_bits = (libc_bits_set > libc_bits) ? libc_bits_set : libc_bits;
927
Florin Coras05ecfcc2018-12-12 18:19:39 -0800928 LDBG (3, "exceptfds: sid_bits_set %d, sid_bits %d, "
929 "libc_bits_set %d, libc_bits %d",
Florin Coras173bae32018-11-16 18:56:28 -0800930 sid_bits_set, sid_bits, libc_bits_set, libc_bits);
Dave Wallace048b1d62018-01-03 22:24:41 -0500931 }
932
933 if (PREDICT_FALSE (!sid_bits && !libc_bits))
934 {
935 errno = EINVAL;
936 rv = -1;
937 goto done;
938 }
939
940 do
941 {
942 if (sid_bits)
943 {
Florin Corasdfe4cf42018-11-28 22:13:45 -0800944 if (!ldpw->select_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -0500945 {
946 func_str = "vppcom_select";
947
948 if (readfds)
Florin Corasdfe4cf42018-11-28 22:13:45 -0800949 clib_memcpy_fast (ldpw->rd_bitmap, ldpw->sid_rd_bitmap,
950 vec_len (ldpw->rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500951 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500952 if (writefds)
Florin Corasdfe4cf42018-11-28 22:13:45 -0800953 clib_memcpy_fast (ldpw->wr_bitmap, ldpw->sid_wr_bitmap,
954 vec_len (ldpw->wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500955 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500956 if (exceptfds)
Florin Corasdfe4cf42018-11-28 22:13:45 -0800957 clib_memcpy_fast (ldpw->ex_bitmap, ldpw->sid_ex_bitmap,
958 vec_len (ldpw->ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -0500959 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -0500960
961 rv = vppcom_select (sid_bits,
David Johnsond9818dd2018-12-14 14:53:41 -0500962 readfds ? (unsigned long *) ldpw->rd_bitmap
963 : NULL,
964 writefds ? (unsigned long *) ldpw->wr_bitmap
965 : NULL,
966 exceptfds ? (unsigned long *)
967 ldpw->ex_bitmap : NULL, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500968 if (rv < 0)
969 {
970 errno = -rv;
971 rv = -1;
972 }
973 else if (rv > 0)
974 {
975 if (readfds)
976 {
977 /* *INDENT-OFF* */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800978 clib_bitmap_foreach (sid, ldpw->rd_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -0500979 ({
Florin Coras30e273b2018-11-27 00:04:59 -0800980 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
Dave Wallace048b1d62018-01-03 22:24:41 -0500981 if (PREDICT_FALSE (fd < 0))
982 {
983 errno = EBADFD;
984 rv = -1;
985 goto done;
986 }
987 FD_SET (fd, readfds);
988 }));
989 /* *INDENT-ON* */
990 }
991 if (writefds)
992 {
993 /* *INDENT-OFF* */
Florin Corasdfe4cf42018-11-28 22:13:45 -0800994 clib_bitmap_foreach (sid, ldpw->wr_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -0500995 ({
Florin Coras30e273b2018-11-27 00:04:59 -0800996 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
Dave Wallace048b1d62018-01-03 22:24:41 -0500997 if (PREDICT_FALSE (fd < 0))
998 {
999 errno = EBADFD;
1000 rv = -1;
1001 goto done;
1002 }
1003 FD_SET (fd, writefds);
1004 }));
1005 /* *INDENT-ON* */
1006 }
1007 if (exceptfds)
1008 {
1009 /* *INDENT-OFF* */
Florin Corasdfe4cf42018-11-28 22:13:45 -08001010 clib_bitmap_foreach (sid, ldpw->ex_bitmap,
Dave Wallace048b1d62018-01-03 22:24:41 -05001011 ({
Florin Coras30e273b2018-11-27 00:04:59 -08001012 fd = ldp_fd_from_sid (vppcom_session_handle (sid));
Dave Wallace048b1d62018-01-03 22:24:41 -05001013 if (PREDICT_FALSE (fd < 0))
1014 {
1015 errno = EBADFD;
1016 rv = -1;
1017 goto done;
1018 }
1019 FD_SET (fd, exceptfds);
1020 }));
1021 /* *INDENT-ON* */
1022 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08001023 ldpw->select_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05001024 goto done;
1025 }
1026 }
1027 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08001028 ldpw->select_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05001029 }
1030 if (libc_bits)
1031 {
1032 struct timespec tspec;
1033
1034 func_str = "libc_pselect";
1035
1036 if (readfds)
Florin Corasdfe4cf42018-11-28 22:13:45 -08001037 clib_memcpy_fast (readfds, ldpw->libc_rd_bitmap,
1038 vec_len (ldpw->rd_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -05001039 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001040 if (writefds)
Florin Corasdfe4cf42018-11-28 22:13:45 -08001041 clib_memcpy_fast (writefds, ldpw->libc_wr_bitmap,
1042 vec_len (ldpw->wr_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -05001043 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001044 if (exceptfds)
Florin Corasdfe4cf42018-11-28 22:13:45 -08001045 clib_memcpy_fast (exceptfds, ldpw->libc_ex_bitmap,
1046 vec_len (ldpw->ex_bitmap) *
Dave Barach178cf492018-11-13 16:34:13 -05001047 sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001048 tspec.tv_sec = tspec.tv_nsec = 0;
1049 rv = libc_pselect (libc_bits,
1050 readfds ? readfds : NULL,
1051 writefds ? writefds : NULL,
1052 exceptfds ? exceptfds : NULL, &tspec, sigmask);
1053 if (rv != 0)
1054 goto done;
1055 }
1056 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08001057 while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out));
Dave Wallace048b1d62018-01-03 22:24:41 -05001058 rv = 0;
1059
1060done:
1061 /* TBD: set timeout to amount of time left */
Florin Corasdfe4cf42018-11-28 22:13:45 -08001062 clib_bitmap_zero (ldpw->rd_bitmap);
1063 clib_bitmap_zero (ldpw->sid_rd_bitmap);
1064 clib_bitmap_zero (ldpw->libc_rd_bitmap);
1065 clib_bitmap_zero (ldpw->wr_bitmap);
1066 clib_bitmap_zero (ldpw->sid_wr_bitmap);
1067 clib_bitmap_zero (ldpw->libc_wr_bitmap);
1068 clib_bitmap_zero (ldpw->ex_bitmap);
1069 clib_bitmap_zero (ldpw->sid_ex_bitmap);
1070 clib_bitmap_zero (ldpw->libc_ex_bitmap);
Dave Wallace048b1d62018-01-03 22:24:41 -05001071
Dave Wallace2a865272018-02-07 21:00:42 -05001072 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05001073 {
1074 if (rv < 0)
1075 {
1076 int errno_val = errno;
1077 perror (func_str);
1078 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1079 "rv %d, errno = %d", getpid (),
1080 func_str, rv, errno_val);
1081 errno = errno_val;
1082 }
1083 else
1084 clib_warning ("LDP<%d>: returning %d (0x%x)", getpid (), rv, rv);
1085 }
1086 return rv;
1087}
1088
1089int
1090select (int nfds, fd_set * __restrict readfds,
1091 fd_set * __restrict writefds,
1092 fd_set * __restrict exceptfds, struct timeval *__restrict timeout)
1093{
1094 struct timespec tspec;
1095
1096 if (timeout)
1097 {
1098 tspec.tv_sec = timeout->tv_sec;
1099 tspec.tv_nsec = timeout->tv_usec * 1000;
1100 }
Dave Wallace2a865272018-02-07 21:00:42 -05001101 return ldp_pselect (nfds, readfds, writefds, exceptfds,
1102 timeout ? &tspec : NULL, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05001103}
1104
1105#ifdef __USE_XOPEN2K
1106int
1107pselect (int nfds, fd_set * __restrict readfds,
1108 fd_set * __restrict writefds,
1109 fd_set * __restrict exceptfds,
1110 const struct timespec *__restrict timeout,
1111 const __sigset_t * __restrict sigmask)
1112{
Dave Wallace2a865272018-02-07 21:00:42 -05001113 return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05001114}
1115#endif
1116
1117int
1118socket (int domain, int type, int protocol)
1119{
1120 const char *func_str;
1121 int rv;
1122 u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0;
1123 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1124
Dave Wallace2a865272018-02-07 21:00:42 -05001125 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001126 return -1;
1127
1128 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1129 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
1130 {
1131 int sid;
Dave Wallace048b1d62018-01-03 22:24:41 -05001132 u8 proto = ((sock_type == SOCK_DGRAM) ?
1133 VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP);
1134
1135 func_str = "vppcom_session_create";
1136
Florin Coras05ecfcc2018-12-12 18:19:39 -08001137 LDBG (0, "calling %s(): proto %u (%s), is_nonblocking %u",
1138 func_str, proto, vppcom_proto_str (proto), is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -05001139
Dave Wallacec04cbf12018-02-07 18:14:02 -05001140 sid = vppcom_session_create (proto, is_nonblocking);
Dave Wallace048b1d62018-01-03 22:24:41 -05001141 if (sid < 0)
1142 {
1143 errno = -sid;
1144 rv = -1;
1145 }
1146 else
1147 {
Dave Wallace2a865272018-02-07 21:00:42 -05001148 func_str = "ldp_fd_from_sid";
Florin Coras30e273b2018-11-27 00:04:59 -08001149 rv = ldp_fd_alloc (sid);
Dave Wallace048b1d62018-01-03 22:24:41 -05001150 if (rv < 0)
1151 {
1152 (void) vppcom_session_close (sid);
1153 errno = -rv;
1154 rv = -1;
1155 }
1156 }
1157 }
1158 else
1159 {
1160 func_str = "libc_socket";
1161
Florin Coras05ecfcc2018-12-12 18:19:39 -08001162 LDBG (0, "calling %s()", func_str);
Dave Wallace048b1d62018-01-03 22:24:41 -05001163
1164 rv = libc_socket (domain, type, protocol);
1165 }
1166
Dave Wallace2a865272018-02-07 21:00:42 -05001167 if (LDP_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001168 {
1169 if (rv < 0)
1170 {
1171 int errno_val = errno;
1172 perror (func_str);
1173 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1174 "rv %d, errno = %d",
1175 getpid (), func_str, rv, errno_val);
1176 errno = errno_val;
1177 }
1178 else
Florin Coras05ecfcc2018-12-12 18:19:39 -08001179 clib_warning ("returning fd %d (0x%x)", getpid (), rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001180 }
1181 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001182}
1183
1184/*
1185 * Create two new sockets, of type TYPE in domain DOMAIN and using
1186 * protocol PROTOCOL, which are connected to each other, and put file
1187 * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
1188 * one will be chosen automatically.
1189 * Returns 0 on success, -1 for errors.
1190 * */
1191int
Dave Wallace048b1d62018-01-03 22:24:41 -05001192socketpair (int domain, int type, int protocol, int fds[2])
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001193{
Dave Wallace048b1d62018-01-03 22:24:41 -05001194 const char *func_str;
1195 int rv;
1196 int sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
1197
Dave Wallace2a865272018-02-07 21:00:42 -05001198 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001199 return -1;
1200
1201 if (((domain == AF_INET) || (domain == AF_INET6)) &&
1202 ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM)))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001203 {
Dave Wallace8aaba562018-01-18 17:21:19 -05001204 func_str = __func__;
1205
Dave Wallace048b1d62018-01-03 22:24:41 -05001206 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
1207 errno = ENOSYS;
1208 rv = -1;
1209 }
1210 else
1211 {
1212 func_str = "libc_socket";
1213
Florin Coras05ecfcc2018-12-12 18:19:39 -08001214 LDBG (1, "calling %s()", func_str);
Dave Wallace048b1d62018-01-03 22:24:41 -05001215
Florin Coras173bae32018-11-16 18:56:28 -08001216 rv = libc_socketpair (domain, type, protocol, fds);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001217 }
1218
Dave Wallace2a865272018-02-07 21:00:42 -05001219 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001220 {
1221 if (rv < 0)
1222 {
1223 int errno_val = errno;
1224 perror (func_str);
1225 clib_warning ("LDP<%d>: ERROR: %s() failed! "
1226 "rv %d, errno = %d",
1227 getpid (), func_str, rv, errno_val);
1228 errno = errno_val;
1229 }
1230 else
1231 clib_warning ("LDP<%d>: : returning fd %d (0x%x)", getpid (), rv, rv);
1232 }
1233 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001234}
1235
1236int
Dave Wallace048b1d62018-01-03 22:24:41 -05001237bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001238{
1239 int rv;
Dave Wallace2a865272018-02-07 21:00:42 -05001240 u32 sid = ldp_sid_from_fd (fd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001241
Dave Wallace2a865272018-02-07 21:00:42 -05001242 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001243 return -1;
1244
1245 if (sid != INVALID_SESSION_ID)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001246 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001247 vppcom_endpt_t ep;
1248
Dave Wallace048b1d62018-01-03 22:24:41 -05001249 switch (addr->sa_family)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001250 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001251 case AF_INET:
1252 if (len != sizeof (struct sockaddr_in))
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001253 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001254 clib_warning
1255 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1256 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1257 errno = EINVAL;
1258 rv = -1;
1259 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001260 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001261 ep.is_ip4 = VPPCOM_IS_IP4;
1262 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1263 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1264 break;
1265
1266 case AF_INET6:
1267 if (len != sizeof (struct sockaddr_in6))
1268 {
1269 clib_warning
1270 ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): Invalid "
1271 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1272 errno = EINVAL;
1273 rv = -1;
1274 goto done;
1275 }
1276 ep.is_ip4 = VPPCOM_IS_IP6;
1277 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1278 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001279 break;
1280
1281 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05001282 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): sid %u (0x%x): "
1283 "Unsupported address family %u!",
1284 getpid (), fd, fd, sid, sid, addr->sa_family);
1285 errno = EAFNOSUPPORT;
1286 rv = -1;
1287 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001288 }
Florin Coras05ecfcc2018-12-12 18:19:39 -08001289 LDBG (0, "fd %d (0x%x): calling vppcom_session_bind(): "
1290 "sid %u (0x%x), addr %p, len %u", fd, fd, sid, sid, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001291
1292 rv = vppcom_session_bind (sid, &ep);
1293 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001294 {
1295 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001296 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001297 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001298 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001299 else
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001300 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001301 LDBG (0, "fd %d (0x%x): calling libc_bind(): addr %p, len %u",
1302 fd, fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001303
Dave Wallace048b1d62018-01-03 22:24:41 -05001304 rv = libc_bind (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001305 }
1306
Dave Wallace048b1d62018-01-03 22:24:41 -05001307done:
Florin Coras05ecfcc2018-12-12 18:19:39 -08001308 LDBG (1, "fd %d (0x%x): returning %d", fd, fd, rv);
1309
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001310 return rv;
1311}
1312
1313static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05001314ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len,
1315 vppcom_endpt_t * ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001316{
Dave Wallace048b1d62018-01-03 22:24:41 -05001317 int rv = 0;
1318 int sa_len, copy_len;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001319
Dave Wallace2a865272018-02-07 21:00:42 -05001320 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001321 return -1;
1322
1323 if (addr && len && ep)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001324 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001325 addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6;
1326 switch (addr->sa_family)
1327 {
1328 case AF_INET:
1329 ((struct sockaddr_in *) addr)->sin_port = ep->port;
1330 if (*len > sizeof (struct sockaddr_in))
1331 *len = sizeof (struct sockaddr_in);
1332 sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr);
1333 copy_len = *len - sa_len;
1334 if (copy_len > 0)
1335 memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip,
1336 copy_len);
1337 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001338
Dave Wallace048b1d62018-01-03 22:24:41 -05001339 case AF_INET6:
1340 ((struct sockaddr_in6 *) addr)->sin6_port = ep->port;
1341 if (*len > sizeof (struct sockaddr_in6))
1342 *len = sizeof (struct sockaddr_in6);
1343 sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr);
1344 copy_len = *len - sa_len;
1345 if (copy_len > 0)
1346 memcpy (((struct sockaddr_in6 *) addr)->sin6_addr.
1347 __in6_u.__u6_addr8, ep->ip, copy_len);
1348 break;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001349
Dave Wallace048b1d62018-01-03 22:24:41 -05001350 default:
1351 /* Not possible */
1352 rv = -EAFNOSUPPORT;
1353 break;
1354 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001355 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001356 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001357}
1358
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001359int
Dave Wallace048b1d62018-01-03 22:24:41 -05001360getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001361{
1362 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001363 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001364 u32 sid = ldp_sid_from_fd (fd);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001365
Dave Wallace2a865272018-02-07 21:00:42 -05001366 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001367 return -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001368
Dave Wallace048b1d62018-01-03 22:24:41 -05001369 if (sid != INVALID_SESSION_ID)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001370 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001371 vppcom_endpt_t ep;
1372 u8 addr_buf[sizeof (struct in6_addr)];
1373 u32 size = sizeof (ep);
1374
1375 ep.ip = addr_buf;
1376 func_str = "vppcom_session_attr[GET_LCL_ADDR]";
1377
Dave Wallace2a865272018-02-07 21:00:42 -05001378 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001379 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1380 "addr %p, len %u",
1381 getpid (), fd, fd, func_str, sid, sid, addr, len);
1382
1383 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size);
1384 if (rv != VPPCOM_OK)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001385 {
1386 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05001387 rv = -1;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001388 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001389 else
1390 {
Dave Wallace2a865272018-02-07 21:00:42 -05001391 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001392 if (rv != VPPCOM_OK)
1393 {
1394 errno = -rv;
1395 rv = -1;
1396 }
1397 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001398 }
1399 else
1400 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001401 func_str = "libc_getsockname";
1402
Dave Wallace2a865272018-02-07 21:00:42 -05001403 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001404 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1405 "addr %p, len %u",
1406 getpid (), fd, fd, func_str, addr, len);
1407
1408 rv = libc_getsockname (fd, addr, len);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001409 }
1410
Dave Wallace2a865272018-02-07 21:00:42 -05001411 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001412 {
1413 if (rv < 0)
1414 {
1415 int errno_val = errno;
1416 perror (func_str);
1417 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1418 "rv %d, errno = %d", getpid (), fd, fd,
1419 func_str, rv, errno_val);
1420 errno = errno_val;
1421 }
1422 else
1423 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1424 getpid (), fd, fd, rv, rv);
1425 }
1426 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001427}
1428
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001429int
Dave Wallace048b1d62018-01-03 22:24:41 -05001430connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001431{
Dave Wallace048b1d62018-01-03 22:24:41 -05001432 int rv;
Dave Wallace2a865272018-02-07 21:00:42 -05001433 u32 sid = ldp_sid_from_fd (fd);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001434
Dave Wallace2a865272018-02-07 21:00:42 -05001435 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001436 return -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001437
Dave Wallace048b1d62018-01-03 22:24:41 -05001438 if (!addr)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001439 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001440 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): NULL addr, len %u",
1441 getpid (), fd, fd, len);
1442 errno = EINVAL;
1443 rv = -1;
1444 goto done;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07001445 }
1446
Dave Wallace048b1d62018-01-03 22:24:41 -05001447 if (sid != INVALID_SESSION_ID)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001448 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001449 vppcom_endpt_t ep;
1450
Dave Wallace048b1d62018-01-03 22:24:41 -05001451 switch (addr->sa_family)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001452 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001453 case AF_INET:
1454 if (len != sizeof (struct sockaddr_in))
1455 {
1456 clib_warning
1457 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1458 "AF_INET addr len %u!", getpid (), fd, fd, sid, sid, len);
1459 errno = EINVAL;
1460 rv = -1;
1461 goto done;
1462 }
1463 ep.is_ip4 = VPPCOM_IS_IP4;
1464 ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr;
1465 ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port;
1466 break;
1467
1468 case AF_INET6:
1469 if (len != sizeof (struct sockaddr_in6))
1470 {
1471 clib_warning
1472 ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): Invalid "
1473 "AF_INET6 addr len %u!", getpid (), fd, fd, sid, sid, len);
1474 errno = EINVAL;
1475 rv = -1;
1476 goto done;
1477 }
1478 ep.is_ip4 = VPPCOM_IS_IP6;
1479 ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1480 ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port;
1481 break;
1482
1483 default:
1484 clib_warning ("LDP<%d>: fd %d (0x%x): ERROR sid %u (0x%x): "
1485 "Unsupported address family %u!",
1486 getpid (), fd, fd, sid, sid, addr->sa_family);
1487 errno = EAFNOSUPPORT;
1488 rv = -1;
1489 goto done;
1490 }
Florin Coras05ecfcc2018-12-12 18:19:39 -08001491 LDBG (0, "fd %d (0x%x): calling vppcom_session_connect(): sid %u (0x%x)"
1492 " addr %p len %u", fd, fd, sid, sid, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001493
1494 rv = vppcom_session_connect (sid, &ep);
1495 if (rv != VPPCOM_OK)
1496 {
1497 errno = -rv;
1498 rv = -1;
1499 }
1500 }
1501 else
1502 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001503 LDBG (0, "fd %d (0x%x): calling libc_connect(): addr %p, len %u",
1504 fd, fd, addr, len);
Dave Wallace048b1d62018-01-03 22:24:41 -05001505
1506 rv = libc_connect (fd, addr, len);
1507 }
1508
1509done:
Florin Coras05ecfcc2018-12-12 18:19:39 -08001510 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05001511 return rv;
1512}
1513
1514int
1515getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len)
1516{
1517 int rv;
1518 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001519 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001520
Dave Wallace2a865272018-02-07 21:00:42 -05001521 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001522 return -1;
1523
Dave Wallace048b1d62018-01-03 22:24:41 -05001524 if (sid != INVALID_SESSION_ID)
1525 {
1526 vppcom_endpt_t ep;
1527 u8 addr_buf[sizeof (struct in6_addr)];
1528 u32 size = sizeof (ep);
1529
1530 ep.ip = addr_buf;
1531 func_str = "vppcom_session_attr[GET_PEER_ADDR]";
1532
Dave Wallace2a865272018-02-07 21:00:42 -05001533 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001534 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1535 "addr %p, len %u",
1536 getpid (), fd, fd, func_str, sid, sid, addr, len);
1537
1538 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size);
1539 if (rv != VPPCOM_OK)
1540 {
1541 errno = -rv;
1542 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001543 }
1544 else
1545 {
Dave Wallace2a865272018-02-07 21:00:42 -05001546 rv = ldp_copy_ep_to_sockaddr (addr, len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05001547 if (rv != VPPCOM_OK)
1548 {
1549 errno = -rv;
1550 rv = -1;
1551 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001552 }
1553 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001554 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001555 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001556 func_str = "libc_getpeername";
1557
Dave Wallace2a865272018-02-07 21:00:42 -05001558 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001559 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1560 "addr %p, len %u",
1561 getpid (), fd, fd, func_str, addr, len);
1562
1563 rv = libc_getpeername (fd, addr, len);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001564 }
1565
Dave Wallace2a865272018-02-07 21:00:42 -05001566 if (LDP_DEBUG > 2)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001567 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001568 if (rv < 0)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001569 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001570 int errno_val = errno;
1571 perror (func_str);
1572 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1573 "rv %d, errno = %d", getpid (), fd, fd,
1574 func_str, rv, errno_val);
1575 errno = errno_val;
1576 }
1577 else
1578 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1579 getpid (), fd, fd, rv, rv);
1580 }
1581 return rv;
1582}
1583
1584ssize_t
1585send (int fd, const void *buf, size_t n, int flags)
1586{
1587 ssize_t size;
1588 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001589 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001590
Dave Wallace2a865272018-02-07 21:00:42 -05001591 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001592 return -1;
1593
1594 if (sid != INVALID_SESSION_ID)
1595 {
1596
1597 func_str = "vppcom_session_sendto";
1598
Dave Wallace2a865272018-02-07 21:00:42 -05001599 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001600 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1601 "buf %p, n %u, flags 0x%x",
1602 getpid (), fd, fd, func_str, sid, sid, buf, n, flags);
1603
1604 size = vppcom_session_sendto (sid, (void *) buf, n, flags, NULL);
qchangaa8f63c2018-05-30 11:44:18 -07001605 if (size < VPPCOM_OK)
Dave Wallace048b1d62018-01-03 22:24:41 -05001606 {
1607 errno = -size;
1608 size = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07001609 }
1610 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001611 else
1612 {
1613 func_str = "libc_send";
1614
Dave Wallace2a865272018-02-07 21:00:42 -05001615 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001616 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1617 "buf %p, n %u, flags 0x%x",
1618 getpid (), fd, fd, func_str, buf, n, flags);
1619
1620 size = libc_send (fd, buf, n, flags);
1621 }
1622
Dave Wallace2a865272018-02-07 21:00:42 -05001623 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001624 {
1625 if (size < 0)
1626 {
1627 int errno_val = errno;
1628 perror (func_str);
1629 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1630 "rv %d, errno = %d", getpid (), fd, fd,
1631 func_str, size, errno_val);
1632 errno = errno_val;
1633 }
1634 else
1635 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1636 getpid (), fd, fd, size, size);
1637 }
1638 return size;
1639}
1640
1641ssize_t
1642sendfile (int out_fd, int in_fd, off_t * offset, size_t len)
1643{
Florin Corasdfe4cf42018-11-28 22:13:45 -08001644 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001645 ssize_t size = 0;
1646 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05001647 u32 sid = ldp_sid_from_fd (out_fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001648
Dave Wallace2a865272018-02-07 21:00:42 -05001649 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001650 return -1;
1651
1652 if (sid != INVALID_SESSION_ID)
1653 {
1654 int rv;
1655 ssize_t results = 0;
1656 size_t n_bytes_left = len;
1657 size_t bytes_to_read;
1658 int nbytes;
1659 int errno_val;
1660 u8 eagain = 0;
1661 u32 flags, flags_len = sizeof (flags);
1662
1663 func_str = "vppcom_session_attr[GET_FLAGS]";
1664 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_FLAGS, &flags,
1665 &flags_len);
1666 if (PREDICT_FALSE (rv != VPPCOM_OK))
1667 {
1668 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1669 "sid %u (0x%x), returned %d (%s)!", getpid (),
1670 out_fd, out_fd, func_str, sid, sid, rv,
1671 vppcom_retval_str (rv));
1672
Florin Corasdfe4cf42018-11-28 22:13:45 -08001673 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001674 errno = -rv;
1675 size = -1;
1676 goto done;
1677 }
1678
1679 if (offset)
1680 {
1681 off_t off = lseek (in_fd, *offset, SEEK_SET);
1682 if (PREDICT_FALSE (off == -1))
1683 {
1684 func_str = "lseek";
1685 errno_val = errno;
1686 clib_warning ("LDP<%d>: ERROR: out fd %d (0x%x): %s(): "
1687 "SEEK_SET failed: in_fd %d, offset %p, "
1688 "*offset %ld, rv %ld, errno %d", getpid (),
1689 out_fd, out_fd, in_fd, offset, *offset, off,
1690 errno_val);
1691 errno = errno_val;
1692 size = -1;
1693 goto done;
1694 }
1695
1696 ASSERT (off == *offset);
1697 }
1698
1699 do
1700 {
1701 func_str = "vppcom_session_attr[GET_NWRITE]";
1702 size = vppcom_session_attr (sid, VPPCOM_ATTR_GET_NWRITE, 0, 0);
1703 if (size < 0)
1704 {
1705 clib_warning
1706 ("LDP<%d>: ERROR: fd %d (0x%x): %s(): sid %u (0x%x), "
1707 "returned %d (%s)!", getpid (), out_fd, out_fd, func_str,
1708 sid, sid, size, vppcom_retval_str (size));
Florin Corasdfe4cf42018-11-28 22:13:45 -08001709 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001710 errno = -size;
1711 size = -1;
1712 goto done;
1713 }
1714
1715 bytes_to_read = size;
Dave Wallace2a865272018-02-07 21:00:42 -05001716 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001717 clib_warning
1718 ("LDP<%d>: fd %d (0x%x): called %s(): sid %u (0x%x), "
1719 "results %ld, n_bytes_left %lu, bytes_to_read %lu", getpid (),
1720 out_fd, out_fd, func_str, sid, sid, results, n_bytes_left,
1721 bytes_to_read);
1722
1723 if (bytes_to_read == 0)
1724 {
1725 if (flags & O_NONBLOCK)
1726 {
1727 if (!results)
1728 {
Dave Wallace2a865272018-02-07 21:00:42 -05001729 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001730 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1731 "EAGAIN",
1732 getpid (), out_fd, out_fd, sid, sid);
1733 eagain = 1;
1734 }
1735 goto update_offset;
1736 }
1737 else
1738 continue;
1739 }
1740 bytes_to_read = clib_min (n_bytes_left, bytes_to_read);
Florin Corasdfe4cf42018-11-28 22:13:45 -08001741 vec_validate (ldpw->io_buffer, bytes_to_read);
1742 nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read);
Dave Wallace048b1d62018-01-03 22:24:41 -05001743 if (nbytes < 0)
1744 {
1745 func_str = "libc_read";
1746 errno_val = errno;
1747 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): in_fd (%d), "
1748 "io_buffer %p, bytes_to_read %lu, rv %d, "
1749 "errno %d", getpid (), out_fd, out_fd, func_str,
Florin Corasdfe4cf42018-11-28 22:13:45 -08001750 in_fd, ldpw->io_buffer, bytes_to_read, nbytes,
Dave Wallace048b1d62018-01-03 22:24:41 -05001751 errno_val);
1752 errno = errno_val;
1753
1754 if (results == 0)
1755 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001756 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001757 size = -1;
1758 goto done;
1759 }
1760 goto update_offset;
1761 }
1762 func_str = "vppcom_session_write";
Dave Wallace2a865272018-02-07 21:00:42 -05001763 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001764 clib_warning
1765 ("LDP<%d>: fd %d (0x%x): calling %s(): sid %u (0x%x), "
1766 "buf %p, nbytes %u: results %d, n_bytes_left %d", getpid (),
Florin Corasdfe4cf42018-11-28 22:13:45 -08001767 out_fd, out_fd, func_str, sid, sid, ldpw->io_buffer, nbytes,
Dave Wallace048b1d62018-01-03 22:24:41 -05001768 results, n_bytes_left);
1769
Florin Corasdfe4cf42018-11-28 22:13:45 -08001770 size = vppcom_session_write (sid, ldpw->io_buffer, nbytes);
Dave Wallace048b1d62018-01-03 22:24:41 -05001771 if (size < 0)
1772 {
1773 if (size == VPPCOM_EAGAIN)
1774 {
1775 if (flags & O_NONBLOCK)
1776 {
1777 if (!results)
1778 {
Dave Wallace2a865272018-02-07 21:00:42 -05001779 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001780 clib_warning
1781 ("LDP<%d>: fd %d (0x%x): sid %u (0x%x): "
1782 "EAGAIN", getpid (), out_fd, out_fd, sid, sid);
1783 eagain = 1;
1784 }
1785 goto update_offset;
1786 }
1787 else
1788 continue;
1789 }
1790 else
1791 {
1792 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s():"
1793 "sid %u, io_buffer %p, nbytes %u "
1794 "returned %d (%s)",
1795 getpid (), out_fd, out_fd, func_str,
Florin Corasdfe4cf42018-11-28 22:13:45 -08001796 sid, ldpw->io_buffer, nbytes,
Dave Wallace048b1d62018-01-03 22:24:41 -05001797 size, vppcom_retval_str (size));
1798 }
1799 if (results == 0)
1800 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08001801 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001802 errno = -size;
1803 size = -1;
1804 goto done;
1805 }
1806 goto update_offset;
1807 }
1808
1809 results += nbytes;
1810 ASSERT (n_bytes_left >= nbytes);
1811 n_bytes_left = n_bytes_left - nbytes;
1812 }
1813 while (n_bytes_left > 0);
1814
1815 update_offset:
Florin Corasdfe4cf42018-11-28 22:13:45 -08001816 vec_reset_length (ldpw->io_buffer);
Dave Wallace048b1d62018-01-03 22:24:41 -05001817 if (offset)
1818 {
1819 off_t off = lseek (in_fd, *offset, SEEK_SET);
1820 if (PREDICT_FALSE (off == -1))
1821 {
1822 func_str = "lseek";
1823 errno_val = errno;
1824 clib_warning ("LDP<%d>: ERROR: %s(): SEEK_SET failed: "
1825 "in_fd %d, offset %p, *offset %ld, "
1826 "rv %ld, errno %d", getpid (), in_fd,
1827 offset, *offset, off, errno_val);
1828 errno = errno_val;
1829 size = -1;
1830 goto done;
1831 }
1832
1833 ASSERT (off == *offset);
1834 *offset += results + 1;
1835 }
1836 if (eagain)
1837 {
1838 errno = EAGAIN;
1839 size = -1;
1840 }
1841 else
1842 size = results;
1843 }
1844 else
1845 {
1846 func_str = "libc_send";
1847
Dave Wallace2a865272018-02-07 21:00:42 -05001848 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001849 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1850 "in_fd %d, offset %p, len %u",
1851 getpid (), out_fd, out_fd, func_str,
1852 in_fd, offset, len);
1853
1854 size = libc_sendfile (out_fd, in_fd, offset, len);
1855 }
1856
1857done:
Dave Wallace2a865272018-02-07 21:00:42 -05001858 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001859 {
1860 if (size < 0)
1861 {
1862 int errno_val = errno;
1863 perror (func_str);
1864 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1865 "rv %d, errno = %d", getpid (), out_fd, out_fd,
1866 func_str, size, errno_val);
1867 errno = errno_val;
1868 }
1869 else
1870 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1871 getpid (), out_fd, out_fd, size, size);
1872 }
1873 return size;
1874}
1875
1876ssize_t
1877sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len)
1878{
1879 return sendfile (out_fd, in_fd, offset, len);
1880}
1881
1882ssize_t
1883recv (int fd, void *buf, size_t n, int flags)
1884{
1885 ssize_t size;
Florin Corasa7a1a222018-12-30 17:11:31 -08001886 u32 sid;
Dave Wallace048b1d62018-01-03 22:24:41 -05001887
Dave Wallace2a865272018-02-07 21:00:42 -05001888 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001889 return -1;
1890
Florin Corasa7a1a222018-12-30 17:11:31 -08001891 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001892 if (sid != INVALID_SESSION_ID)
1893 {
Florin Corasa7a1a222018-12-30 17:11:31 -08001894 LDBG (2, "fd %d (0x%x): calling vcl recvfrom: sid %u (0x%x), buf %p,"
1895 " n %u, flags 0x%x", fd, fd, sid, sid, buf, n, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001896
1897 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
1898 if (size < 0)
Florin Corasa7a1a222018-12-30 17:11:31 -08001899 errno = -size;
Dave Wallace048b1d62018-01-03 22:24:41 -05001900 }
1901 else
1902 {
Florin Corasa7a1a222018-12-30 17:11:31 -08001903 LDBG (2, "fd %d (0x%x): calling libc_recvfrom(): buf %p, n %u, "
1904 "flags 0x%x", fd, fd, buf, n, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05001905
1906 size = libc_recv (fd, buf, n, flags);
1907 }
1908
Dave Wallace048b1d62018-01-03 22:24:41 -05001909 return size;
1910}
1911
1912ssize_t
1913sendto (int fd, const void *buf, size_t n, int flags,
1914 __CONST_SOCKADDR_ARG addr, socklen_t addr_len)
1915{
1916 ssize_t size;
1917 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05001918 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05001919
Dave Wallace2a865272018-02-07 21:00:42 -05001920 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05001921 return -1;
1922
1923 if (sid != INVALID_SESSION_ID)
1924 {
1925 vppcom_endpt_t *ep = 0;
1926 vppcom_endpt_t _ep;
1927
1928 if (addr)
1929 {
1930 ep = &_ep;
Dave Wallace048b1d62018-01-03 22:24:41 -05001931 switch (addr->sa_family)
1932 {
1933 case AF_INET:
1934 ep->is_ip4 = VPPCOM_IS_IP4;
1935 ep->ip =
1936 (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr;
1937 ep->port =
1938 (uint16_t) ((const struct sockaddr_in *) addr)->sin_port;
1939 break;
1940
1941 case AF_INET6:
1942 ep->is_ip4 = VPPCOM_IS_IP6;
1943 ep->ip =
1944 (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr;
1945 ep->port =
1946 (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port;
1947 break;
1948
1949 default:
1950 errno = EAFNOSUPPORT;
1951 size = -1;
1952 goto done;
1953 }
1954 }
1955
1956 func_str = "vppcom_session_sendto";
1957
Dave Wallace2a865272018-02-07 21:00:42 -05001958 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001959 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1960 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
1961 getpid (), fd, fd, func_str, sid, sid, buf, n,
1962 flags, ep);
1963
1964 size = vppcom_session_sendto (sid, (void *) buf, n, flags, ep);
1965 if (size < 0)
1966 {
1967 errno = -size;
1968 size = -1;
1969 }
1970 }
1971 else
1972 {
1973 func_str = "libc_sendto";
1974
Dave Wallace2a865272018-02-07 21:00:42 -05001975 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001976 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
1977 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
1978 getpid (), fd, fd, func_str, buf, n, flags,
1979 addr, addr_len);
1980
1981 size = libc_sendto (fd, buf, n, flags, addr, addr_len);
1982 }
1983
1984done:
Dave Wallace2a865272018-02-07 21:00:42 -05001985 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05001986 {
1987 if (size < 0)
1988 {
1989 int errno_val = errno;
1990 perror (func_str);
1991 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
1992 "rv %d, errno = %d", getpid (), fd, fd,
1993 func_str, size, errno_val);
1994 errno = errno_val;
1995 }
1996 else
1997 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
1998 getpid (), fd, fd, size, size);
1999 }
2000 return size;
2001}
2002
2003ssize_t
2004recvfrom (int fd, void *__restrict buf, size_t n, int flags,
2005 __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
2006{
2007 ssize_t size;
2008 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002009 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002010
Dave Wallace2a865272018-02-07 21:00:42 -05002011 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002012 return -1;
2013
2014 if (sid != INVALID_SESSION_ID)
2015 {
2016 vppcom_endpt_t ep;
2017 u8 src_addr[sizeof (struct sockaddr_in6)];
2018
2019 func_str = "vppcom_session_recvfrom";
2020
Dave Wallace2a865272018-02-07 21:00:42 -05002021 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002022 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2023 "sid %u (0x%x), buf %p, n %u, flags 0x%x, ep %p",
2024 getpid (), fd, fd, func_str, sid, sid, buf, n,
2025 flags, &ep);
2026 if (addr)
2027 {
2028 ep.ip = src_addr;
2029 size = vppcom_session_recvfrom (sid, buf, n, flags, &ep);
2030
2031 if (size > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05002032 size = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002033 }
2034 else
2035 size = vppcom_session_recvfrom (sid, buf, n, flags, NULL);
2036
2037 if (size < 0)
2038 {
2039 errno = -size;
2040 size = -1;
2041 }
2042 }
2043 else
2044 {
2045 func_str = "libc_recvfrom";
2046
Dave Wallace2a865272018-02-07 21:00:42 -05002047 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002048 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2049 "buf %p, n %u, flags 0x%x, addr %p, addr_len %d",
2050 getpid (), fd, fd, func_str, buf, n, flags,
2051 addr, addr_len);
2052
2053 size = libc_recvfrom (fd, buf, n, flags, addr, addr_len);
2054 }
2055
Dave Wallace2a865272018-02-07 21:00:42 -05002056 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 {
2058 if (size < 0)
2059 {
2060 int errno_val = errno;
2061 perror (func_str);
2062 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2063 "rv %d, errno = %d", getpid (), fd, fd,
2064 func_str, size, errno_val);
2065 errno = errno_val;
2066 }
2067 else
2068 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2069 getpid (), fd, fd, size, size);
2070 }
2071 return size;
2072}
2073
2074ssize_t
2075sendmsg (int fd, const struct msghdr * message, int flags)
2076{
2077 ssize_t size;
2078 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002079 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002080
Dave Wallace2a865272018-02-07 21:00:42 -05002081 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002082 return -1;
2083
2084 if (sid != INVALID_SESSION_ID)
2085 {
Dave Wallace8aaba562018-01-18 17:21:19 -05002086 func_str = __func__;
2087
Dave Wallace048b1d62018-01-03 22:24:41 -05002088 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2089 errno = ENOSYS;
2090 size = -1;
2091 }
2092 else
2093 {
2094 func_str = "libc_sendmsg";
2095
Dave Wallace2a865272018-02-07 21:00:42 -05002096 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002097 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2098 "message %p, flags 0x%x",
2099 getpid (), fd, fd, func_str, message, flags);
2100
2101 size = libc_sendmsg (fd, message, flags);
2102 }
2103
Dave Wallace2a865272018-02-07 21:00:42 -05002104 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002105 {
2106 if (size < 0)
2107 {
2108 int errno_val = errno;
2109 perror (func_str);
2110 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2111 "rv %d, errno = %d", getpid (), fd, fd,
2112 func_str, size, errno_val);
2113 errno = errno_val;
2114 }
2115 else
2116 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2117 getpid (), fd, fd, size, size);
2118 }
2119 return size;
2120}
2121
2122#ifdef USE_GNU
2123int
2124sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags)
2125{
2126 ssize_t size;
2127 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002128 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002129
Dave Wallace2a865272018-02-07 21:00:42 -05002130 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002131 return -1;
2132
2133 if (sid != INVALID_SESSION_ID)
2134 {
2135 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2136 errno = ENOSYS;
2137 size = -1;
2138 }
2139 else
2140 {
2141 func_str = "libc_sendmmsg";
2142
Dave Wallace2a865272018-02-07 21:00:42 -05002143 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002144 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2145 "vmessages %p, vlen %u, flags 0x%x",
2146 getpid (), fd, fd, func_str, vmessages, vlen, flags);
2147
2148 size = libc_sendmmsg (fd, vmessages, vlen, flags);
2149 }
2150
Dave Wallace2a865272018-02-07 21:00:42 -05002151 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002152 {
2153 if (size < 0)
2154 {
2155 int errno_val = errno;
2156 perror (func_str);
2157 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2158 "rv %d, errno = %d", getpid (), fd, fd,
2159 func_str, size, errno_val);
2160 errno = errno_val;
2161 }
2162 else
2163 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2164 getpid (), fd, fd, size, size);
2165 }
2166 return size;
2167}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002168#endif
2169
Dave Wallace048b1d62018-01-03 22:24:41 -05002170ssize_t
2171recvmsg (int fd, struct msghdr * message, int flags)
2172{
2173 ssize_t size;
2174 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002175 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002176
Dave Wallace2a865272018-02-07 21:00:42 -05002177 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002178 return -1;
2179
2180 if (sid != INVALID_SESSION_ID)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002181 {
Dave Wallace8aaba562018-01-18 17:21:19 -05002182 func_str = __func__;
2183
Dave Wallace048b1d62018-01-03 22:24:41 -05002184 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2185 errno = ENOSYS;
2186 size = -1;
2187 }
2188 else
2189 {
2190 func_str = "libc_recvmsg";
2191
Dave Wallace2a865272018-02-07 21:00:42 -05002192 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002193 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2194 "message %p, flags 0x%x",
2195 getpid (), fd, fd, func_str, message, flags);
2196
2197 size = libc_recvmsg (fd, message, flags);
2198 }
2199
Dave Wallace2a865272018-02-07 21:00:42 -05002200 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002201 {
2202 if (size < 0)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002203 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002204 int errno_val = errno;
2205 perror (func_str);
2206 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2207 "rv %d, errno = %d", getpid (), fd, fd,
2208 func_str, size, errno_val);
2209 errno = errno_val;
2210 }
2211 else
2212 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2213 getpid (), fd, fd, size, size);
2214 }
2215 return size;
2216}
2217
2218#ifdef USE_GNU
2219int
2220recvmmsg (int fd, struct mmsghdr *vmessages,
2221 unsigned int vlen, int flags, struct timespec *tmo)
2222{
2223 ssize_t size;
2224 const char *func_str;
Dave Wallace2a865272018-02-07 21:00:42 -05002225 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002226
Dave Wallace2a865272018-02-07 21:00:42 -05002227 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002228 return -1;
2229
2230 if (sid != INVALID_SESSION_ID)
2231 {
2232 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
2233 errno = ENOSYS;
2234 size = -1;
2235 }
2236 else
2237 {
2238 func_str = "libc_recvmmsg";
2239
Dave Wallace2a865272018-02-07 21:00:42 -05002240 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002241 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2242 "vmessages %p, vlen %u, flags 0x%x, tmo %p",
2243 getpid (), fd, fd, func_str, vmessages, vlen,
2244 flags, tmo);
2245
2246 size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo);
2247 }
2248
Dave Wallace2a865272018-02-07 21:00:42 -05002249 if (LDP_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05002250 {
2251 if (size < 0)
2252 {
2253 int errno_val = errno;
2254 perror (func_str);
2255 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2256 "rv %d, errno = %d", getpid (), fd, fd,
2257 func_str, size, errno_val);
2258 errno = errno_val;
2259 }
2260 else
2261 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2262 getpid (), fd, fd, size, size);
2263 }
2264 return size;
2265}
2266#endif
2267
2268int
2269getsockopt (int fd, int level, int optname,
2270 void *__restrict optval, socklen_t * __restrict optlen)
2271{
2272 int rv;
2273 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05002274 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace8aaba562018-01-18 17:21:19 -05002275 u32 buflen = optlen ? (u32) * optlen : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05002276
Dave Wallace2a865272018-02-07 21:00:42 -05002277 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002278 return -1;
2279
2280 if (sid != INVALID_SESSION_ID)
2281 {
2282 rv = -EOPNOTSUPP;
2283
2284 switch (level)
2285 {
2286 case SOL_TCP:
2287 switch (optname)
2288 {
2289 case TCP_NODELAY:
2290 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_NODELAY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002291 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002292 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2293 "sid %u (0x%x)",
2294 getpid (), fd, fd, func_str, sid, sid);
2295 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_NODELAY,
2296 optval, optlen);
2297 break;
2298 case TCP_MAXSEG:
2299 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_USER_MSS]";
Dave Wallace2a865272018-02-07 21:00:42 -05002300 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002301 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2302 "sid %u (0x%x)",
2303 getpid (), fd, fd, func_str, sid, sid);
2304 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_USER_MSS,
2305 optval, optlen);
2306 break;
2307 case TCP_KEEPIDLE:
2308 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPIDLE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002309 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002310 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2311 "sid %u (0x%x)",
2312 getpid (), fd, fd, func_str, sid, sid);
2313 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPIDLE,
2314 optval, optlen);
2315 break;
2316 case TCP_KEEPINTVL:
2317 func_str = "vppcom_session_attr[SOL_TCP,GET_TCP_KEEPINTVL]";
Dave Wallace2a865272018-02-07 21:00:42 -05002318 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002319 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2320 "sid %u (0x%x), SOL_TCP",
2321 getpid (), fd, fd, func_str, sid, sid);
2322 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TCP_KEEPINTVL,
2323 optval, optlen);
2324 break;
2325 case TCP_INFO:
2326 if (optval && optlen && (*optlen == sizeof (struct tcp_info)))
2327 {
Dave Wallace2a865272018-02-07 21:00:42 -05002328 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002329 clib_warning ("LDP<%d>: fd %d (0x%x): sid %u (0x%x), "
2330 "SOL_TCP, TCP_INFO, optval %p, "
2331 "optlen %d: #LDP-NOP#",
2332 getpid (), fd, fd, sid, sid,
2333 optval, *optlen);
2334 memset (optval, 0, *optlen);
2335 rv = VPPCOM_OK;
2336 }
2337 else
2338 rv = -EFAULT;
2339 break;
2340 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002341 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002342 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2343 "sid %u (0x%x), SOL_TCP, "
2344 "optname %d unsupported!",
2345 getpid (), fd, fd, func_str, sid, sid, optname);
2346 break;
2347 }
2348 break;
2349 case SOL_IPV6:
2350 switch (optname)
2351 {
2352 case IPV6_V6ONLY:
2353 func_str = "vppcom_session_attr[SOL_IPV6,GET_V6ONLY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002354 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002355 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2356 "sid %u (0x%x)",
2357 getpid (), fd, fd, func_str, sid, sid);
2358 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_V6ONLY,
2359 optval, optlen);
2360 break;
2361 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002362 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002363 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2364 "sid %u (0x%x), SOL_IPV6, "
2365 "optname %d unsupported!",
2366 getpid (), fd, fd, func_str, sid, sid, optname);
2367 break;
2368 }
2369 break;
2370 case SOL_SOCKET:
2371 switch (optname)
2372 {
2373 case SO_ACCEPTCONN:
2374 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ACCEPTCONN]";
Dave Wallace2a865272018-02-07 21:00:42 -05002375 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002376 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2377 "sid %u (0x%x)",
2378 getpid (), fd, fd, func_str, sid, sid);
2379 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_LISTEN,
2380 optval, optlen);
2381 break;
2382 case SO_KEEPALIVE:
2383 func_str = "vppcom_session_attr[SOL_SOCKET,GET_KEEPALIVE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002384 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002385 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2386 "sid %u (0x%x)",
2387 getpid (), fd, fd, func_str, sid, sid);
2388 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_KEEPALIVE,
2389 optval, optlen);
2390 break;
2391 case SO_PROTOCOL:
2392 func_str = "vppcom_session_attr[SOL_SOCKET,GET_PROTOCOL]";
Dave Wallace2a865272018-02-07 21:00:42 -05002393 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002394 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2395 "sid %u (0x%x)",
2396 getpid (), fd, fd, func_str, sid, sid);
2397 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_PROTOCOL,
2398 optval, optlen);
2399 *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM;
2400 break;
2401 case SO_SNDBUF:
2402 func_str = "vppcom_session_attr[SOL_SOCKET,GET_TX_FIFO_LEN]";
Dave Wallace2a865272018-02-07 21:00:42 -05002403 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002404 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2405 "sid %u (0x%x), optlen %d",
2406 getpid (), fd, fd, func_str, sid, sid, buflen);
2407 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_TX_FIFO_LEN,
2408 optval, optlen);
2409 break;
2410 case SO_RCVBUF:
2411 func_str = "vppcom_session_attr[SOL_SOCKET,GET_RX_FIFO_LEN]";
Dave Wallace2a865272018-02-07 21:00:42 -05002412 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002413 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2414 "sid %u (0x%x), optlen %d",
Dave Wallaceb4cd4ff2018-01-19 12:17:08 -05002415 getpid (), fd, fd, func_str, sid, sid, buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002416 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_RX_FIFO_LEN,
2417 optval, optlen);
2418 break;
2419 case SO_REUSEADDR:
2420 func_str = "vppcom_session_attr[SOL_SOCKET,GET_REUSEADDR]";
Dave Wallace2a865272018-02-07 21:00:42 -05002421 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002422 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2423 "sid %u (0x%x)",
2424 getpid (), fd, fd, func_str, sid, sid);
2425 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_REUSEADDR,
2426 optval, optlen);
2427 break;
2428 case SO_BROADCAST:
2429 func_str = "vppcom_session_attr[SOL_SOCKET,GET_BROADCAST]";
Dave Wallace2a865272018-02-07 21:00:42 -05002430 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002431 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2432 "sid %u (0x%x)",
2433 getpid (), fd, fd, func_str, sid, sid);
2434 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_BROADCAST,
2435 optval, optlen);
2436 break;
2437 case SO_ERROR:
2438 func_str = "vppcom_session_attr[SOL_SOCKET,GET_ERROR]";
Dave Wallace2a865272018-02-07 21:00:42 -05002439 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002440 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2441 "sid %u (0x%x)",
2442 getpid (), fd, fd, func_str, sid, sid);
2443 rv = vppcom_session_attr (sid, VPPCOM_ATTR_GET_ERROR,
2444 optval, optlen);
2445 break;
2446 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002447 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002448 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2449 "sid %u (0x%x), SOL_SOCKET, "
2450 "optname %d unsupported!",
2451 getpid (), fd, fd, func_str, sid, sid, optname);
2452 break;
2453 }
2454 break;
2455 default:
2456 break;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002457 }
2458
Dave Wallace048b1d62018-01-03 22:24:41 -05002459 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002460 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002461 errno = -rv;
2462 rv = -1;
2463 }
2464 }
2465 else
2466 {
2467 func_str = "libc_getsockopt";
2468
Dave Wallace2a865272018-02-07 21:00:42 -05002469 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002470 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2471 "optname %d, optval %p, optlen %d",
2472 getpid (), fd, fd, func_str, level, optname,
2473 optval, optlen);
2474
2475 rv = libc_getsockopt (fd, level, optname, optval, optlen);
2476 }
2477
Dave Wallace2a865272018-02-07 21:00:42 -05002478 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002479 {
2480 if (rv < 0)
2481 {
2482 int errno_val = errno;
2483 perror (func_str);
2484 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2485 "rv %d, errno = %d", getpid (), fd, fd,
2486 func_str, rv, errno_val);
2487 errno = errno_val;
2488 }
2489 else
2490 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2491 getpid (), fd, fd, rv, rv);
2492 }
2493 return rv;
2494}
2495
2496int
2497setsockopt (int fd, int level, int optname,
2498 const void *optval, socklen_t optlen)
2499{
2500 int rv;
2501 const char *func_str = __func__;
Dave Wallace2a865272018-02-07 21:00:42 -05002502 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002503
Dave Wallace2a865272018-02-07 21:00:42 -05002504 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002505 return -1;
2506
2507 if (sid != INVALID_SESSION_ID)
2508 {
2509 rv = -EOPNOTSUPP;
2510
2511 switch (level)
2512 {
2513 case SOL_TCP:
2514 switch (optname)
2515 {
2516 case TCP_NODELAY:
2517 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_NODELAY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002518 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002519 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2520 "sid %u (0x%x)",
2521 getpid (), fd, fd, func_str, sid, sid);
2522 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_NODELAY,
2523 (void *) optval, &optlen);
2524 break;
2525 case TCP_MAXSEG:
2526 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_USER_MSS]";
Dave Wallace2a865272018-02-07 21:00:42 -05002527 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002528 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2529 "sid %u (0x%x)",
2530 getpid (), fd, fd, func_str, sid, sid);
2531 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_USER_MSS,
2532 (void *) optval, &optlen);
2533 break;
2534 case TCP_KEEPIDLE:
2535 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPIDLE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002536 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002537 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2538 "sid %u (0x%x)",
2539 getpid (), fd, fd, func_str, sid, sid);
2540 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPIDLE,
2541 (void *) optval, &optlen);
2542 break;
2543 case TCP_KEEPINTVL:
2544 func_str = "vppcom_session_attr[SOL_TCP,SET_TCP_KEEPINTVL]";
Dave Wallace2a865272018-02-07 21:00:42 -05002545 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002546 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2547 "sid %u (0x%x), SOL_TCP",
2548 getpid (), fd, fd, func_str, sid, sid);
2549 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_TCP_KEEPINTVL,
2550 (void *) optval, &optlen);
2551 break;
2552 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002553 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002554 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2555 "sid %u (0x%x), SOL_TCP, "
2556 "optname %d unsupported!",
2557 getpid (), fd, fd, func_str, sid, sid, optname);
2558 break;
2559 }
2560 break;
2561 case SOL_IPV6:
2562 switch (optname)
2563 {
2564 case IPV6_V6ONLY:
2565 func_str = "vppcom_session_attr[SOL_IPV6,SET_V6ONLY]";
Dave Wallace2a865272018-02-07 21:00:42 -05002566 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002567 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2568 "sid %u (0x%x)",
2569 getpid (), fd, fd, func_str, sid, sid);
2570 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_V6ONLY,
2571 (void *) optval, &optlen);
2572 break;
2573 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002574 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002575 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2576 "sid %u (0x%x), SOL_IPV6, "
2577 "optname %d unsupported!",
2578 getpid (), fd, fd, func_str, sid, sid, optname);
2579 break;
2580 }
2581 break;
2582 case SOL_SOCKET:
2583 switch (optname)
2584 {
2585 case SO_KEEPALIVE:
2586 func_str = "vppcom_session_attr[SOL_SOCKET,SET_KEEPALIVE]";
Dave Wallace2a865272018-02-07 21:00:42 -05002587 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002588 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2589 "sid %u (0x%x)",
2590 getpid (), fd, fd, func_str, sid, sid);
2591 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_KEEPALIVE,
2592 (void *) optval, &optlen);
2593 break;
2594 case SO_REUSEADDR:
2595 func_str = "vppcom_session_attr[SOL_SOCKET,SET_REUSEADDR]";
Dave Wallace2a865272018-02-07 21:00:42 -05002596 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002597 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2598 "sid %u (0x%x)",
2599 getpid (), fd, fd, func_str, sid, sid);
2600 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_REUSEADDR,
2601 (void *) optval, &optlen);
2602 break;
2603 case SO_BROADCAST:
2604 func_str = "vppcom_session_attr[SOL_SOCKET,SET_BROADCAST]";
Dave Wallace2a865272018-02-07 21:00:42 -05002605 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002606 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): "
2607 "sid %u (0x%x)",
2608 getpid (), fd, fd, func_str, sid, sid);
2609 rv = vppcom_session_attr (sid, VPPCOM_ATTR_SET_BROADCAST,
2610 (void *) optval, &optlen);
2611 break;
2612 default:
Dave Wallace2a865272018-02-07 21:00:42 -05002613 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002614 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s(): "
2615 "sid %u (0x%x), SOL_SOCKET, "
2616 "optname %d unsupported!",
2617 getpid (), fd, fd, func_str, sid, sid, optname);
2618 break;
2619 }
2620 break;
2621 default:
2622 break;
2623 }
2624
2625 if (rv != VPPCOM_OK)
2626 {
2627 errno = -rv;
2628 rv = -1;
2629 }
2630 }
2631 else
2632 {
2633 func_str = "libc_setsockopt";
2634
Dave Wallace2a865272018-02-07 21:00:42 -05002635 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002636 clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): level %d, "
2637 "optname %d, optval %p, optlen %d",
2638 getpid (), fd, fd, func_str, level, optname,
2639 optval, optlen);
2640
2641 rv = libc_setsockopt (fd, level, optname, optval, optlen);
2642 }
2643
Dave Wallace2a865272018-02-07 21:00:42 -05002644 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002645 {
2646 if (rv < 0)
2647 {
2648 int errno_val = errno;
2649 perror (func_str);
2650 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2651 "rv %d, errno = %d", getpid (), fd, fd,
2652 func_str, rv, errno_val);
2653 errno = errno_val;
2654 }
2655 else
2656 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2657 getpid (), fd, fd, rv, rv);
2658 }
2659 return rv;
2660}
2661
2662int
2663listen (int fd, int n)
2664{
2665 int rv;
Dave Wallace2a865272018-02-07 21:00:42 -05002666 u32 sid = ldp_sid_from_fd (fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002667
Dave Wallace2a865272018-02-07 21:00:42 -05002668 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002669 return -1;
2670
2671 if (sid != INVALID_SESSION_ID)
2672 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08002673 LDBG (0, "fd %d (0x%x): calling vppcom_session_listen():"
2674 " sid %u (0x%x), n %d", fd, fd, sid, sid, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002675
2676 rv = vppcom_session_listen (sid, n);
2677 if (rv != VPPCOM_OK)
2678 {
2679 errno = -rv;
2680 rv = -1;
2681 }
2682 }
2683 else
2684 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08002685 LDBG (0, "fd %d (0x%x): calling libc_listen(): n %d", fd, fd, n);
Dave Wallace048b1d62018-01-03 22:24:41 -05002686
2687 rv = libc_listen (fd, n);
2688 }
2689
Florin Coras05ecfcc2018-12-12 18:19:39 -08002690 LDBG (1, "fd %d (0x%x): returning %d (0x%x)", fd, fd, rv, rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002691 return rv;
2692}
2693
2694static inline int
Dave Wallace2a865272018-02-07 21:00:42 -05002695ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr,
2696 socklen_t * __restrict addr_len, int flags)
Dave Wallace048b1d62018-01-03 22:24:41 -05002697{
2698 int rv;
Dave Wallace2a865272018-02-07 21:00:42 -05002699 u32 listen_sid = ldp_sid_from_fd (listen_fd);
Dave Wallace048b1d62018-01-03 22:24:41 -05002700 int accept_sid;
2701
Dave Wallace2a865272018-02-07 21:00:42 -05002702 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002703 return -1;
2704
2705 if (listen_sid != INVALID_SESSION_ID)
2706 {
2707 vppcom_endpt_t ep;
2708 u8 src_addr[sizeof (struct sockaddr_in6)];
Dave Wallace8aaba562018-01-18 17:21:19 -05002709 memset (&ep, 0, sizeof (ep));
Dave Wallace048b1d62018-01-03 22:24:41 -05002710 ep.ip = src_addr;
2711
Florin Coras05ecfcc2018-12-12 18:19:39 -08002712 LDBG (0, "listen fd %d (0x%x): calling vppcom_session_accept:"
2713 " listen sid %u (0x%x), ep %p, flags 0x%x", listen_fd,
2714 listen_fd, listen_sid, listen_sid, ep, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002715
2716 accept_sid = vppcom_session_accept (listen_sid, &ep, flags);
2717 if (accept_sid < 0)
2718 {
2719 errno = -accept_sid;
2720 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002721 }
2722 else
2723 {
Dave Wallace2a865272018-02-07 21:00:42 -05002724 rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep);
Dave Wallace048b1d62018-01-03 22:24:41 -05002725 if (rv != VPPCOM_OK)
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002726 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002727 (void) vppcom_session_close ((u32) accept_sid);
2728 errno = -rv;
2729 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002730 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002731 else
2732 {
Florin Coras30e273b2018-11-27 00:04:59 -08002733 rv = ldp_fd_alloc ((u32) accept_sid);
Dave Wallace048b1d62018-01-03 22:24:41 -05002734 if (rv < 0)
2735 {
2736 (void) vppcom_session_close ((u32) accept_sid);
2737 errno = -rv;
2738 rv = -1;
2739 }
2740 }
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002741 }
2742 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002743 else
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002744 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08002745 LDBG (0, "listen fd %d (0x%x): calling libc_accept4(): "
2746 "addr %p, addr_len %p, flags 0x%x", listen_fd,
2747 listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002748
Dave Wallace048b1d62018-01-03 22:24:41 -05002749 rv = libc_accept4 (listen_fd, addr, addr_len, flags);
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002750 }
2751
Florin Coras05ecfcc2018-12-12 18:19:39 -08002752 LDBG (1, "listen fd %d (0x%x): returning %d (0x%x)", listen_fd, listen_fd,
2753 rv, rv);
2754
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002755 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002756}
2757
Dave Wallace048b1d62018-01-03 22:24:41 -05002758int
2759accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len,
2760 int flags)
2761{
Dave Wallace2a865272018-02-07 21:00:42 -05002762 return ldp_accept4 (fd, addr, addr_len, flags);
Dave Wallace048b1d62018-01-03 22:24:41 -05002763}
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002764
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002765int
Dave Wallace048b1d62018-01-03 22:24:41 -05002766accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002767{
Dave Wallace2a865272018-02-07 21:00:42 -05002768 return ldp_accept4 (fd, addr, addr_len, 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05002769}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002770
Dave Wallace048b1d62018-01-03 22:24:41 -05002771int
2772shutdown (int fd, int how)
2773{
Florin Corasa7a1a222018-12-30 17:11:31 -08002774 int rv = 0;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002775
Dave Wallace2a865272018-02-07 21:00:42 -05002776 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002777 return -1;
2778
Florin Corasa7a1a222018-12-30 17:11:31 -08002779 if (ldp_fd_is_sid (fd))
Dave Wallace048b1d62018-01-03 22:24:41 -05002780 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002781 u32 fd_index = fd - ldp->sid_bit_val;
2782 ldp_fd_entry_t *fde;
2783
2784 fde = ldp_fd_entry_get_w_lock (fd_index);
2785 if (!fde)
2786 {
2787 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
2788 errno = ENOTCONN;
2789 return -1;
2790 }
2791
2792 if (how == SHUT_RD)
2793 fde->flags |= LDP_F_SHUT_RD;
2794 else if (how == SHUT_WR)
2795 fde->flags |= LDP_F_SHUT_WR;
2796 else if (how == SHUT_RDWR)
2797 fde->flags |= (LDP_F_SHUT_RD | LDP_F_SHUT_WR);
2798
2799 if ((fde->flags & LDP_F_SHUT_RD) && (fde->flags & LDP_F_SHUT_WR))
2800 rv = close (fd);
2801
2802 clib_rwlock_reader_unlock (&ldp->fd_table_lock);
2803 LDBG (0, "fd %d (0x%x): calling vcl shutdown: how %d", fd, fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002804 }
2805 else
2806 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002807 LDBG (1, "fd %d (0x%x): calling libc_shutdown: how %d", fd, fd, how);
Dave Wallace048b1d62018-01-03 22:24:41 -05002808 rv = libc_shutdown (fd, how);
2809 }
2810
Dave Wallace048b1d62018-01-03 22:24:41 -05002811 return rv;
2812}
2813
2814int
2815epoll_create1 (int flags)
2816{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002817 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002818 const char *func_str;
2819 int rv;
2820
Dave Wallace2a865272018-02-07 21:00:42 -05002821 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002822 return -1;
2823
Florin Coras99368312018-08-02 10:45:44 -07002824 if (ldp->vcl_needs_real_epoll)
2825 {
2826 rv = libc_epoll_create1 (flags);
2827 ldp->vcl_needs_real_epoll = 0;
Florin Corasdfe4cf42018-11-28 22:13:45 -08002828 ldpw->vcl_mq_epfd = rv;
Florin Coras05ecfcc2018-12-12 18:19:39 -08002829 LDBG (0, "created vcl epfd %u", rv);
Florin Coras99368312018-08-02 10:45:44 -07002830 return rv;
2831 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002832 func_str = "vppcom_epoll_create";
2833
Florin Coras05ecfcc2018-12-12 18:19:39 -08002834 LDBG (1, "calling %s()", func_str);
Dave Wallace048b1d62018-01-03 22:24:41 -05002835
2836 rv = vppcom_epoll_create ();
2837
2838 if (PREDICT_FALSE (rv < 0))
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002839 {
2840 errno = -rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002841 rv = -1;
shrinivasan ganapathy1d359632017-10-15 15:46:09 -07002842 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002843 else
Florin Coras30e273b2018-11-27 00:04:59 -08002844 rv = ldp_fd_alloc ((u32) rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002845
Dave Wallace2a865272018-02-07 21:00:42 -05002846 if (LDP_DEBUG > 1)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002847 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002848 if (rv < 0)
2849 {
2850 int errno_val = errno;
2851 perror (func_str);
2852 clib_warning ("LDP<%d>: ERROR: %s() failed! "
2853 "rv %d, errno = %d",
2854 getpid (), func_str, rv, errno_val);
2855 errno = errno_val;
2856 }
2857 else
2858 clib_warning ("LDP<%d>: returning epfd %d (0x%x)", getpid (), rv, rv);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002859 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002860 return rv;
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002861}
2862
2863int
Dave Wallace048b1d62018-01-03 22:24:41 -05002864epoll_create (int size)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002865{
Dave Wallace048b1d62018-01-03 22:24:41 -05002866 return epoll_create1 (0);
2867}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002868
Dave Wallace048b1d62018-01-03 22:24:41 -05002869int
2870epoll_ctl (int epfd, int op, int fd, struct epoll_event *event)
2871{
Florin Coras99368312018-08-02 10:45:44 -07002872 u32 vep_idx = ldp_sid_from_fd (epfd), sid;
Dave Wallace048b1d62018-01-03 22:24:41 -05002873 const char *func_str;
Florin Coras99368312018-08-02 10:45:44 -07002874 int rv;
Dave Wallace048b1d62018-01-03 22:24:41 -05002875
Dave Wallace2a865272018-02-07 21:00:42 -05002876 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05002877 return -1;
2878
Florin Coras99368312018-08-02 10:45:44 -07002879 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
Dave Wallace048b1d62018-01-03 22:24:41 -05002880 {
Dave Wallace3ee1fe12018-02-23 01:09:11 -05002881 /* The LDP epoll_create1 always creates VCL epfd's.
2882 * The app should never have a kernel base epoll fd unless it
2883 * was acquired outside of the LD_PRELOAD process context.
2884 * In any case, if we get one, punt it to libc_epoll_ctl.
2885 */
Dave Wallace048b1d62018-01-03 22:24:41 -05002886 func_str = "libc_epoll_ctl";
2887
Florin Coras05ecfcc2018-12-12 18:19:39 -08002888 LDBG (1, "epfd %d (0x%x): calling %s(): op %d, fd %d (0x%x),"
2889 " event %p", epfd, epfd, func_str, op, fd, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002890
2891 rv = libc_epoll_ctl (epfd, op, fd, event);
Florin Coras99368312018-08-02 10:45:44 -07002892 goto done;
2893 }
2894
2895 sid = ldp_sid_from_fd (fd);
2896
Florin Coras05ecfcc2018-12-12 18:19:39 -08002897 LDBG (0, "epfd %d (0x%x), vep_idx %d (0x%x), sid %d (0x%x)",
2898 epfd, epfd, vep_idx, vep_idx, sid, sid);
Florin Coras99368312018-08-02 10:45:44 -07002899
2900 if (sid != INVALID_SESSION_ID)
2901 {
2902 func_str = "vppcom_epoll_ctl";
2903
Florin Coras05ecfcc2018-12-12 18:19:39 -08002904 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
2905 " op %d, sid %u (0x%x), event %p", epfd, epfd,
Florin Coras99368312018-08-02 10:45:44 -07002906 func_str, vep_idx, vep_idx, sid, sid, event);
2907
2908 rv = vppcom_epoll_ctl (vep_idx, op, sid, event);
2909 if (rv != VPPCOM_OK)
2910 {
2911 errno = -rv;
2912 rv = -1;
2913 }
2914 }
2915 else
2916 {
2917 int libc_epfd;
2918 u32 size = sizeof (epfd);
2919
2920 func_str = "vppcom_session_attr[GET_LIBC_EPFD]";
2921 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0,
2922 0);
Florin Coras05ecfcc2018-12-12 18:19:39 -08002923 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): %s() "
2924 "returned libc_epfd %d (0x%x)", epfd, epfd,
Florin Coras99368312018-08-02 10:45:44 -07002925 vep_idx, vep_idx, func_str, libc_epfd, libc_epfd);
2926
2927 if (!libc_epfd)
2928 {
2929 func_str = "libc_epoll_create1";
2930
Florin Coras05ecfcc2018-12-12 18:19:39 -08002931 LDBG (1, "epfd %d (0x%x), vep_idx %d (0x%x): "
2932 "calling %s(): EPOLL_CLOEXEC", epfd, epfd,
Florin Coras99368312018-08-02 10:45:44 -07002933 vep_idx, vep_idx, func_str);
2934
2935 libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC);
2936 if (libc_epfd < 0)
2937 {
2938 rv = libc_epfd;
2939 goto done;
2940 }
2941
2942 func_str = "vppcom_session_attr[SET_LIBC_EPFD]";
Florin Coras05ecfcc2018-12-12 18:19:39 -08002943 LDBG (1, "epfd %d (0x%x): calling %s(): vep_idx %d (0x%x),"
Florin Coras99368312018-08-02 10:45:44 -07002944 " VPPCOM_ATTR_SET_LIBC_EPFD, libc_epfd %d (0x%x), size %d",
Florin Coras05ecfcc2018-12-12 18:19:39 -08002945 epfd, epfd, func_str, vep_idx, vep_idx, libc_epfd,
Florin Coras99368312018-08-02 10:45:44 -07002946 libc_epfd, size);
2947
2948 rv = vppcom_session_attr (vep_idx, VPPCOM_ATTR_SET_LIBC_EPFD,
2949 &libc_epfd, &size);
2950 if (rv < 0)
2951 {
2952 errno = -rv;
2953 rv = -1;
2954 goto done;
2955 }
2956 }
2957 else if (PREDICT_FALSE (libc_epfd < 0))
2958 {
2959 errno = -epfd;
2960 rv = -1;
2961 goto done;
2962 }
2963
2964 func_str = "libc_epoll_ctl";
2965
Florin Coras05ecfcc2018-12-12 18:19:39 -08002966 LDBG (1, "epfd %d (0x%x): calling %s(): libc_epfd %d (0x%x), "
2967 "op %d, fd %d (0x%x), event %p", epfd, epfd, func_str,
Florin Coras99368312018-08-02 10:45:44 -07002968 libc_epfd, libc_epfd, op, fd, fd, event);
2969
2970 rv = libc_epoll_ctl (libc_epfd, op, fd, event);
Dave Wallace048b1d62018-01-03 22:24:41 -05002971 }
2972
2973done:
Dave Wallace2a865272018-02-07 21:00:42 -05002974 if (LDP_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002975 {
2976 if (rv < 0)
2977 {
2978 int errno_val = errno;
2979 perror (func_str);
2980 clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! "
2981 "rv %d, errno = %d", getpid (), fd, fd,
2982 func_str, rv, errno_val);
2983 errno = errno_val;
2984 }
2985 else
2986 clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)",
2987 getpid (), fd, fd, rv, rv);
2988 }
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07002989 return rv;
2990}
Dave Wallace048b1d62018-01-03 22:24:41 -05002991
2992static inline int
Florin Coras99368312018-08-02 10:45:44 -07002993ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents,
2994 int timeout, const sigset_t * sigmask)
Dave Wallace048b1d62018-01-03 22:24:41 -05002995{
Florin Corasdfe4cf42018-11-28 22:13:45 -08002996 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -07002997 double time_to_wait = (double) 0, time_out, now = 0;
Dave Wallace2a865272018-02-07 21:00:42 -05002998 u32 vep_idx = ldp_sid_from_fd (epfd);
Florin Coras99368312018-08-02 10:45:44 -07002999 int libc_epfd, rv = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003000
Dave Wallace2a865272018-02-07 21:00:42 -05003001 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05003002 return -1;
3003
3004 if (PREDICT_FALSE (!events || (timeout < -1)))
3005 {
3006 errno = EFAULT;
3007 return -1;
3008 }
3009
Florin Corasdfe4cf42018-11-28 22:13:45 -08003010 if (epfd == ldpw->vcl_mq_epfd)
Florin Coras99368312018-08-02 10:45:44 -07003011 return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
3012
Dave Wallace048b1d62018-01-03 22:24:41 -05003013 if (PREDICT_FALSE (vep_idx == INVALID_SESSION_ID))
3014 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003015 LDBG (0, "epfd %d (0x%x): bad vep_idx %d (0x%x)!", epfd, epfd, vep_idx,
3016 vep_idx);
Dave Wallace048b1d62018-01-03 22:24:41 -05003017 errno = EBADFD;
3018 return -1;
3019 }
3020
Florin Corasb0f662f2018-12-27 14:51:46 -08003021 time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0);
Florin Corasdfe4cf42018-11-28 22:13:45 -08003022 time_out = clib_time_now (&ldpw->clib_time) + time_to_wait;
Dave Wallace048b1d62018-01-03 22:24:41 -05003023
Dave Wallace048b1d62018-01-03 22:24:41 -05003024 libc_epfd = vppcom_session_attr (vep_idx, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0);
3025 if (PREDICT_FALSE (libc_epfd < 0))
3026 {
3027 errno = -libc_epfd;
3028 rv = -1;
3029 goto done;
3030 }
3031
Florin Coras05ecfcc2018-12-12 18:19:39 -08003032 LDBG (2, "epfd %d (0x%x): vep_idx %d (0x%x), libc_epfd %d (0x%x), "
Florin Coras99368312018-08-02 10:45:44 -07003033 "events %p, maxevents %d, timeout %d, sigmask %p: time_to_wait %.02f",
Florin Coras05ecfcc2018-12-12 18:19:39 -08003034 epfd, epfd, vep_idx, vep_idx, libc_epfd, libc_epfd, events,
Florin Coras99368312018-08-02 10:45:44 -07003035 maxevents, timeout, sigmask, time_to_wait, time_out);
Dave Wallace048b1d62018-01-03 22:24:41 -05003036 do
3037 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08003038 if (!ldpw->epoll_wait_vcl)
Dave Wallace048b1d62018-01-03 22:24:41 -05003039 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003040 LDBG (3, "epfd %d (0x%x): calling vcl_epoll_wait: vep_idx %d (0x%x)"
3041 " events %p, maxevents %d", epfd, epfd, vep_idx, vep_idx,
3042 events, maxevents);
Dave Wallace048b1d62018-01-03 22:24:41 -05003043
3044 rv = vppcom_epoll_wait (vep_idx, events, maxevents, 0);
3045 if (rv > 0)
3046 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08003047 ldpw->epoll_wait_vcl = 1;
Dave Wallace048b1d62018-01-03 22:24:41 -05003048 goto done;
3049 }
3050 else if (rv < 0)
3051 {
3052 errno = -rv;
3053 rv = -1;
3054 goto done;
3055 }
3056 }
3057 else
Florin Corasdfe4cf42018-11-28 22:13:45 -08003058 ldpw->epoll_wait_vcl = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003059
3060 if (libc_epfd > 0)
3061 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003062 LDBG (3, "epfd %d (0x%x): calling libc_epoll_wait: libc_epfd %d "
3063 "(0x%x), events %p, maxevents %d, sigmask %p", epfd, epfd,
3064 libc_epfd, libc_epfd, events, maxevents, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05003065
Florin Corasb0f662f2018-12-27 14:51:46 -08003066 rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05003067 if (rv != 0)
3068 goto done;
3069 }
3070
3071 if (timeout != -1)
Florin Corasdfe4cf42018-11-28 22:13:45 -08003072 now = clib_time_now (&ldpw->clib_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003073 }
3074 while (now < time_out);
3075
3076done:
Dave Wallace048b1d62018-01-03 22:24:41 -05003077 return rv;
3078}
3079
3080int
3081epoll_pwait (int epfd, struct epoll_event *events,
3082 int maxevents, int timeout, const sigset_t * sigmask)
3083{
Dave Wallace2a865272018-02-07 21:00:42 -05003084 return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask);
Dave Wallace048b1d62018-01-03 22:24:41 -05003085}
3086
3087int
3088epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout)
3089{
Dave Wallace2a865272018-02-07 21:00:42 -05003090 return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003091}
3092
3093int
3094poll (struct pollfd *fds, nfds_t nfds, int timeout)
3095{
Florin Corasdfe4cf42018-11-28 22:13:45 -08003096 ldp_worker_ctx_t *ldpw = ldp_worker_get_current ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003097 const char *func_str = __func__;
Florin Coras6917b942018-11-13 22:44:54 -08003098 int rv, i, n_revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003099 u32 sid;
3100 vcl_poll_t *vp;
3101 double wait_for_time;
3102
Florin Coras05ecfcc2018-12-12 18:19:39 -08003103 LDBG (3, "fds %p, nfds %d, timeout %d", fds, nfds, timeout);
Dave Wallace048b1d62018-01-03 22:24:41 -05003104
3105 if (timeout >= 0)
3106 wait_for_time = (f64) timeout / 1000;
3107 else
3108 wait_for_time = -1;
3109
Dave Wallace048b1d62018-01-03 22:24:41 -05003110 for (i = 0; i < nfds; i++)
3111 {
Florin Coras6917b942018-11-13 22:44:54 -08003112 if (fds[i].fd < 0)
3113 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05003114
Florin Coras05ecfcc2018-12-12 18:19:39 -08003115 LDBG (3, "fds[%d] fd %d (0x%0x) events = 0x%x revents = 0x%x",
3116 i, fds[i].fd, fds[i].fd, fds[i].events, fds[i].revents);
Florin Coras6917b942018-11-13 22:44:54 -08003117
3118 sid = ldp_sid_from_fd (fds[i].fd);
3119 if (sid != INVALID_SESSION_ID)
3120 {
3121 fds[i].fd = -fds[i].fd;
Florin Corasdfe4cf42018-11-28 22:13:45 -08003122 vec_add2 (ldpw->vcl_poll, vp, 1);
Florin Coras6917b942018-11-13 22:44:54 -08003123 vp->fds_ndx = i;
3124 vp->sid = sid;
3125 vp->events = fds[i].events;
Dave Wallace048b1d62018-01-03 22:24:41 -05003126#ifdef __USE_XOPEN2K
Florin Coras6917b942018-11-13 22:44:54 -08003127 if (fds[i].events & POLLRDNORM)
3128 vp->events |= POLLIN;
3129 if (fds[i].events & POLLWRNORM)
3130 vp->events |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003131#endif
Florin Coras6917b942018-11-13 22:44:54 -08003132 vp->revents = fds[i].revents;
3133 }
3134 else
3135 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08003136 vec_add1 (ldpw->libc_poll, fds[i]);
3137 vec_add1 (ldpw->libc_poll_idxs, i);
Dave Wallace048b1d62018-01-03 22:24:41 -05003138 }
3139 }
3140
Dave Wallace048b1d62018-01-03 22:24:41 -05003141 do
3142 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08003143 if (vec_len (ldpw->vcl_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05003144 {
3145 func_str = "vppcom_poll";
3146
Florin Coras05ecfcc2018-12-12 18:19:39 -08003147 LDBG (3, "calling %s(): vcl_poll %p, n_sids %u (0x%x): "
3148 "n_libc_fds %u", func_str, ldpw->vcl_poll,
Florin Corasdfe4cf42018-11-28 22:13:45 -08003149 vec_len (ldpw->vcl_poll), vec_len (ldpw->vcl_poll),
3150 vec_len (ldpw->libc_poll));
Dave Wallace048b1d62018-01-03 22:24:41 -05003151
Florin Corasdfe4cf42018-11-28 22:13:45 -08003152 rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05003153 if (rv < 0)
3154 {
3155 errno = -rv;
3156 rv = -1;
3157 goto done;
3158 }
3159 else
3160 n_revents += rv;
3161 }
3162
Florin Corasdfe4cf42018-11-28 22:13:45 -08003163 if (vec_len (ldpw->libc_poll))
Dave Wallace048b1d62018-01-03 22:24:41 -05003164 {
3165 func_str = "libc_poll";
3166
Florin Coras05ecfcc2018-12-12 18:19:39 -08003167 LDBG (3, "calling %s(): fds %p, nfds %u: n_sids %u",
3168 fds, nfds, vec_len (ldpw->vcl_poll));
Dave Wallace048b1d62018-01-03 22:24:41 -05003169
Florin Corasdfe4cf42018-11-28 22:13:45 -08003170 rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0);
Dave Wallace048b1d62018-01-03 22:24:41 -05003171 if (rv < 0)
3172 goto done;
3173 else
3174 n_revents += rv;
3175 }
3176
3177 if (n_revents)
3178 {
3179 rv = n_revents;
3180 goto done;
3181 }
3182 }
3183 while ((wait_for_time == -1) ||
Florin Corasdfe4cf42018-11-28 22:13:45 -08003184 (clib_time_now (&ldpw->clib_time) < wait_for_time));
Dave Wallace048b1d62018-01-03 22:24:41 -05003185 rv = 0;
3186
3187done:
Florin Corasdfe4cf42018-11-28 22:13:45 -08003188 vec_foreach (vp, ldpw->vcl_poll)
Dave Wallace048b1d62018-01-03 22:24:41 -05003189 {
3190 fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd;
Florin Coras6917b942018-11-13 22:44:54 -08003191 fds[vp->fds_ndx].revents = vp->revents;
Dave Wallace048b1d62018-01-03 22:24:41 -05003192#ifdef __USE_XOPEN2K
3193 if ((fds[vp->fds_ndx].revents & POLLIN) &&
3194 (fds[vp->fds_ndx].events & POLLRDNORM))
3195 fds[vp->fds_ndx].revents |= POLLRDNORM;
3196 if ((fds[vp->fds_ndx].revents & POLLOUT) &&
3197 (fds[vp->fds_ndx].events & POLLWRNORM))
3198 fds[vp->fds_ndx].revents |= POLLWRNORM;
3199#endif
3200 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08003201 vec_reset_length (ldpw->vcl_poll);
Dave Wallace048b1d62018-01-03 22:24:41 -05003202
Florin Corasdfe4cf42018-11-28 22:13:45 -08003203 for (i = 0; i < vec_len (ldpw->libc_poll); i++)
Florin Coras6917b942018-11-13 22:44:54 -08003204 {
Florin Corasdfe4cf42018-11-28 22:13:45 -08003205 fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents;
Florin Coras6917b942018-11-13 22:44:54 -08003206 }
Florin Corasdfe4cf42018-11-28 22:13:45 -08003207 vec_reset_length (ldpw->libc_poll_idxs);
3208 vec_reset_length (ldpw->libc_poll);
Florin Coras6917b942018-11-13 22:44:54 -08003209
Dave Wallace2a865272018-02-07 21:00:42 -05003210 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003211 {
3212 if (rv < 0)
3213 {
3214 int errno_val = errno;
3215 perror (func_str);
3216 clib_warning ("LDP<%d>: ERROR: %s() failed! "
3217 "rv %d, errno = %d", getpid (),
3218 func_str, rv, errno_val);
3219 errno = errno_val;
3220 }
3221 else
3222 {
3223 clib_warning ("LDP<%d>: returning %d (0x%x): n_sids %u, "
3224 "n_libc_fds %d", getpid (), rv, rv,
Florin Corasdfe4cf42018-11-28 22:13:45 -08003225 vec_len (ldpw->vcl_poll), vec_len (ldpw->libc_poll));
Dave Wallace048b1d62018-01-03 22:24:41 -05003226
3227 for (i = 0; i < nfds; i++)
3228 {
3229 if (fds[i].fd >= 0)
3230 {
Dave Wallace2a865272018-02-07 21:00:42 -05003231 if (LDP_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003232 clib_warning ("LDP<%d>: fds[%d].fd %d (0x%0x), "
3233 ".events = 0x%x, .revents = 0x%x",
3234 getpid (), i, fds[i].fd, fds[i].fd,
3235 fds[i].events, fds[i].revents);
3236 }
3237 }
3238 }
3239 }
3240
3241 return rv;
3242}
3243
3244#ifdef USE_GNU
3245int
3246ppoll (struct pollfd *fds, nfds_t nfds,
3247 const struct timespec *timeout, const sigset_t * sigmask)
3248{
Dave Wallace2a865272018-02-07 21:00:42 -05003249 if ((errno = -ldp_init ()))
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 return -1;
3251
3252 clib_warning ("LDP<%d>: LDP-TBD", getpid ());
3253 errno = ENOSYS;
3254
3255
3256 return -1;
3257}
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003258#endif
3259
Dave Wallace2a865272018-02-07 21:00:42 -05003260void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003261
Dave Wallace2a865272018-02-07 21:00:42 -05003262void DESTRUCTOR_ATTRIBUTE ldp_destructor (void);
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003263
Dave Wallace048b1d62018-01-03 22:24:41 -05003264/*
3265 * This function is called when the library is loaded
3266 */
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003267void
Dave Wallace2a865272018-02-07 21:00:42 -05003268ldp_constructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003269{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003270 swrap_constructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05003271 if (ldp_init () != 0)
3272 fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n",
Dave Wallace048b1d62018-01-03 22:24:41 -05003273 getpid ());
Dave Wallace69d01192018-02-22 16:22:09 -05003274 else if (LDP_DEBUG > 0)
Dave Wallace2a865272018-02-07 21:00:42 -05003275 clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003276}
3277
3278/*
3279 * This function is called when the library is unloaded
3280 */
3281void
Dave Wallace2a865272018-02-07 21:00:42 -05003282ldp_destructor (void)
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003283{
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003284 swrap_destructor ();
Dave Wallace2a865272018-02-07 21:00:42 -05003285 if (ldp->init)
Florin Coras940f78f2018-11-30 12:11:20 -08003286 ldp->init = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003287
3288 /* Don't use clib_warning() here because that calls writev()
Dave Wallace2a865272018-02-07 21:00:42 -05003289 * which will call ldp_init().
Dave Wallace048b1d62018-01-03 22:24:41 -05003290 */
Dave Wallace69d01192018-02-22 16:22:09 -05003291 if (LDP_DEBUG > 0)
3292 printf ("%s:%d: LDP<%d>: LDP destructor: done!\n",
3293 __func__, __LINE__, getpid ());
Keith Burns (alagalah)b327c2b2017-10-09 08:52:59 -07003294}
3295
3296
3297/*
3298 * fd.io coding-style-patch-verification: ON
3299 *
3300 * Local Variables:
3301 * eval: (c-set-style "gnu")
3302 * End:
3303 */