Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 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 ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 22 | #include <sys/resource.h> |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 23 | #include <netinet/tcp.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 24 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 25 | #include <vcl/ldp_socket_wrapper.h> |
| 26 | #include <vcl/ldp.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 27 | #include <sys/time.h> |
| 28 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 29 | #include <vcl/vcl_locked.h> |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 30 | #include <vppinfra/time.h> |
| 31 | #include <vppinfra/bitmap.h> |
Florin Coras | 30e273b | 2018-11-27 00:04:59 -0800 | [diff] [blame] | 32 | #include <vppinfra/lock.h> |
| 33 | #include <vppinfra/pool.h> |
| 34 | #include <vppinfra/hash.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 35 | |
| 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 Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 52 | #define LDP_MAX_NWORKERS 32 |
| 53 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 54 | typedef struct ldp_worker_ctx_ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 55 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 56 | u8 *io_buffer; |
| 57 | clib_time_t clib_time; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Select state |
| 61 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 62 | clib_bitmap_t *rd_bitmap; |
| 63 | clib_bitmap_t *wr_bitmap; |
| 64 | clib_bitmap_t *ex_bitmap; |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 65 | clib_bitmap_t *si_rd_bitmap; |
| 66 | clib_bitmap_t *si_wr_bitmap; |
| 67 | clib_bitmap_t *si_ex_bitmap; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 68 | clib_bitmap_t *libc_rd_bitmap; |
| 69 | clib_bitmap_t *libc_wr_bitmap; |
| 70 | clib_bitmap_t *libc_ex_bitmap; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Poll state |
| 74 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 75 | vcl_poll_t *vcl_poll; |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 76 | struct pollfd *libc_poll; |
| 77 | u16 *libc_poll_idxs; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Epoll state |
| 81 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 82 | u8 epoll_wait_vcl; |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 83 | u8 mq_epfd_added; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 84 | int vcl_mq_epfd; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 85 | |
| 86 | } ldp_worker_ctx_t; |
| 87 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 88 | /* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure |
| 89 | * they are the same size */ |
| 90 | STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask), |
| 91 | "ldp bitmap size mismatch"); |
| 92 | STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask), |
| 93 | "ldp bitmap size mismatch"); |
| 94 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 95 | typedef struct |
| 96 | { |
| 97 | ldp_worker_ctx_t *workers; |
| 98 | int init; |
| 99 | char app_name[LDP_APP_NAME_MAX]; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 100 | u32 vlsh_bit_val; |
| 101 | u32 vlsh_bit_mask; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 102 | u32 debug; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 103 | |
| 104 | /** vcl needs next epoll_create to go to libc_epoll */ |
| 105 | u8 vcl_needs_real_epoll; |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * crypto state used only for testing |
| 109 | */ |
| 110 | u8 transparent_tls; |
| 111 | u32 ckpair_index; |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 112 | } ldp_main_t; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 113 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 114 | #define LDP_DEBUG ldp->debug |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 115 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 116 | #define LDBG(_lvl, _fmt, _args...) \ |
| 117 | if (ldp->debug > _lvl) \ |
hanlin | 9f3f18f | 2019-12-30 16:25:20 +0800 | [diff] [blame] | 118 | { \ |
| 119 | int errno_saved = errno; \ |
Florin Coras | 585c86a | 2020-10-16 17:57:36 -0700 | [diff] [blame] | 120 | fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \ |
hanlin | 9f3f18f | 2019-12-30 16:25:20 +0800 | [diff] [blame] | 121 | errno = errno_saved; \ |
| 122 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 123 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 124 | static ldp_main_t ldp_main = { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 125 | .vlsh_bit_val = (1 << LDP_SID_BIT_MIN), |
| 126 | .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1, |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 127 | .debug = LDP_DEBUG_INIT, |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 128 | .transparent_tls = 0, |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 129 | .ckpair_index = ~0, |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 130 | }; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 131 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 132 | static ldp_main_t *ldp = &ldp_main; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 133 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 134 | static inline ldp_worker_ctx_t * |
| 135 | ldp_worker_get_current (void) |
| 136 | { |
| 137 | return (ldp->workers + vppcom_worker_index ()); |
| 138 | } |
| 139 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 140 | /* |
| 141 | * RETURN: 0 on success or -1 on error. |
| 142 | * */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 143 | static inline void |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 144 | ldp_set_app_name (char *app_name) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 145 | { |
Benoît Ganne | 747b3d8 | 2019-08-21 18:27:23 +0200 | [diff] [blame] | 146 | snprintf (ldp->app_name, LDP_APP_NAME_MAX, |
| 147 | "ldp-%d-%s", getpid (), app_name); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 150 | static inline char * |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 151 | ldp_get_app_name () |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 152 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 153 | if (ldp->app_name[0] == '\0') |
| 154 | ldp_set_app_name ("app"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 155 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 156 | return ldp->app_name; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 159 | static inline int |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 160 | ldp_vlsh_to_fd (vls_handle_t vlsh) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 161 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 162 | return (vlsh + ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 163 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 164 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 165 | static inline vls_handle_t |
| 166 | ldp_fd_to_vlsh (int fd) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 167 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 168 | if (fd < ldp->vlsh_bit_val) |
| 169 | return VLS_INVALID_HANDLE; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 170 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 171 | return (fd - ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Florin Coras | 2d9b427 | 2019-03-11 10:14:37 -0700 | [diff] [blame] | 174 | static void |
| 175 | ldp_alloc_workers (void) |
| 176 | { |
| 177 | if (ldp->workers) |
| 178 | return; |
| 179 | pool_alloc (ldp->workers, LDP_MAX_NWORKERS); |
| 180 | } |
| 181 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 182 | static int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 183 | ldp_init (void) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 184 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 185 | ldp_worker_ctx_t *ldpw; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 186 | int rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 187 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 188 | ASSERT (!ldp->init); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 189 | |
| 190 | ldp->init = 1; |
| 191 | ldp->vcl_needs_real_epoll = 1; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 192 | rv = vls_app_create (ldp_get_app_name ()); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 193 | if (rv != VPPCOM_OK) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 194 | { |
Florin Coras | 955bfbb | 2018-12-04 13:43:45 -0800 | [diff] [blame] | 195 | ldp->vcl_needs_real_epoll = 0; |
| 196 | if (rv == VPPCOM_EEXIST) |
| 197 | return 0; |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 198 | LDBG (2, "\nERROR: ldp_init: vppcom_app_create()" |
| 199 | " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv)); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 200 | ldp->init = 0; |
| 201 | return rv; |
| 202 | } |
| 203 | ldp->vcl_needs_real_epoll = 0; |
Florin Coras | 2d9b427 | 2019-03-11 10:14:37 -0700 | [diff] [blame] | 204 | ldp_alloc_workers (); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 205 | ldpw = ldp_worker_get_current (); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 206 | |
| 207 | char *env_var_str = getenv (LDP_ENV_DEBUG); |
| 208 | if (env_var_str) |
| 209 | { |
| 210 | u32 tmp; |
| 211 | if (sscanf (env_var_str, "%u", &tmp) != 1) |
| 212 | clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in" |
| 213 | " the env var " LDP_ENV_DEBUG " (%s)!", getpid (), |
| 214 | env_var_str); |
| 215 | else |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 216 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 217 | ldp->debug = tmp; |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 218 | LDBG (0, "configured LDP debug level (%u) from env var " |
| 219 | LDP_ENV_DEBUG "!", ldp->debug); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 222 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 223 | env_var_str = getenv (LDP_ENV_APP_NAME); |
| 224 | if (env_var_str) |
| 225 | { |
| 226 | ldp_set_app_name (env_var_str); |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 227 | LDBG (0, "configured LDP app name (%s) from the env var " |
| 228 | LDP_ENV_APP_NAME "!", ldp->app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 229 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 230 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 231 | env_var_str = getenv (LDP_ENV_SID_BIT); |
| 232 | if (env_var_str) |
| 233 | { |
| 234 | u32 sb; |
| 235 | if (sscanf (env_var_str, "%u", &sb) != 1) |
| 236 | { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 237 | LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var " |
| 238 | LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str, |
| 239 | ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 240 | } |
| 241 | else if (sb < LDP_SID_BIT_MIN) |
| 242 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 243 | ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN); |
| 244 | ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 245 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 246 | LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var " |
| 247 | LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN" |
| 248 | " (%d)! sid bit value %d (0x%x)", sb, env_var_str, |
| 249 | LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 250 | } |
| 251 | else if (sb > LDP_SID_BIT_MAX) |
| 252 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 253 | ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX); |
| 254 | ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 255 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 256 | LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var " |
| 257 | LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX" |
| 258 | " (%d)! sid bit value %d (0x%x)", sb, env_var_str, |
| 259 | LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 260 | } |
| 261 | else |
| 262 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 263 | ldp->vlsh_bit_val = (1 << sb); |
| 264 | ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 265 | |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 266 | LDBG (0, "configured LDP sid bit (%u) from " |
| 267 | LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb, |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 268 | ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 269 | } |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 270 | |
| 271 | /* Make sure there are enough bits in the fd set for vcl sessions */ |
| 272 | if (ldp->vlsh_bit_val > FD_SETSIZE / 2) |
| 273 | { |
| 274 | LDBG (0, "ERROR: LDP vlsh bit value %d > FD_SETSIZE/2 %d!", |
| 275 | ldp->vlsh_bit_val, FD_SETSIZE / 2); |
| 276 | ldp->init = 0; |
| 277 | return -1; |
| 278 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 279 | } |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 280 | env_var_str = getenv (LDP_ENV_TLS_TRANS); |
| 281 | if (env_var_str) |
| 282 | { |
| 283 | ldp->transparent_tls = 1; |
| 284 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 285 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 286 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 287 | pool_foreach (ldpw, ldp->workers) { |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 288 | clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time)); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 289 | } |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 290 | /* *INDENT-ON* */ |
| 291 | |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 292 | LDBG (0, "LDP initialization: done!"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 293 | |
| 294 | return 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 297 | #define ldp_init_check() \ |
| 298 | if (PREDICT_FALSE (!ldp->init)) \ |
| 299 | { \ |
| 300 | if ((errno = -ldp_init ())) \ |
| 301 | return -1; \ |
| 302 | } |
| 303 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 304 | int |
| 305 | close (int fd) |
| 306 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 307 | vls_handle_t vlsh; |
| 308 | int rv, epfd; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 309 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 310 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 311 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 312 | vlsh = ldp_fd_to_vlsh (fd); |
| 313 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 314 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 315 | epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 316 | if (epfd > 0) |
| 317 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 318 | LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 319 | |
| 320 | rv = libc_close (epfd); |
| 321 | if (rv < 0) |
| 322 | { |
| 323 | u32 size = sizeof (epfd); |
| 324 | epfd = 0; |
| 325 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 326 | (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | else if (PREDICT_FALSE (epfd < 0)) |
| 330 | { |
| 331 | errno = -epfd; |
| 332 | rv = -1; |
| 333 | goto done; |
| 334 | } |
| 335 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 336 | LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 337 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 338 | rv = vls_close (vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 339 | if (rv != VPPCOM_OK) |
| 340 | { |
| 341 | errno = -rv; |
| 342 | rv = -1; |
| 343 | } |
| 344 | } |
| 345 | else |
| 346 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 347 | LDBG (0, "fd %d: calling libc_close", fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 348 | rv = libc_close (fd); |
| 349 | } |
| 350 | |
| 351 | done: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 352 | return rv; |
| 353 | } |
| 354 | |
| 355 | ssize_t |
| 356 | read (int fd, void *buf, size_t nbytes) |
| 357 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 358 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 359 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 360 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 361 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 362 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 363 | vlsh = ldp_fd_to_vlsh (fd); |
| 364 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 365 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 366 | size = vls_read (vlsh, buf, nbytes); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 367 | if (size < 0) |
| 368 | { |
| 369 | errno = -size; |
| 370 | size = -1; |
| 371 | } |
| 372 | } |
| 373 | else |
| 374 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 375 | size = libc_read (fd, buf, nbytes); |
| 376 | } |
| 377 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 378 | return size; |
| 379 | } |
| 380 | |
| 381 | ssize_t |
| 382 | readv (int fd, const struct iovec * iov, int iovcnt) |
| 383 | { |
Dave Wallace | 8aaba56 | 2018-01-18 17:21:19 -0500 | [diff] [blame] | 384 | int rv = 0, i, total = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 385 | vls_handle_t vlsh; |
| 386 | ssize_t size = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 387 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 388 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 389 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 390 | vlsh = ldp_fd_to_vlsh (fd); |
| 391 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 392 | { |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 393 | for (i = 0; i < iovcnt; ++i) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 394 | { |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 395 | rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len); |
| 396 | if (rv <= 0) |
| 397 | break; |
| 398 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 399 | { |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 400 | total += rv; |
| 401 | if (rv < iov[i].iov_len) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 402 | break; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 405 | if (rv < 0 && total == 0) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 406 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 407 | errno = -rv; |
| 408 | size = -1; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 409 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 410 | else |
| 411 | size = total; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 412 | } |
| 413 | else |
| 414 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 415 | size = libc_readv (fd, iov, iovcnt); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 418 | return size; |
| 419 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 420 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 421 | ssize_t |
| 422 | write (int fd, const void *buf, size_t nbytes) |
| 423 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 424 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 425 | ssize_t size = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 426 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 427 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 428 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 429 | vlsh = ldp_fd_to_vlsh (fd); |
| 430 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 431 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 432 | size = vls_write_msg (vlsh, (void *) buf, nbytes); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 433 | if (size < 0) |
| 434 | { |
| 435 | errno = -size; |
| 436 | size = -1; |
| 437 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 438 | } |
| 439 | else |
| 440 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 441 | size = libc_write (fd, buf, nbytes); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 444 | return size; |
| 445 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 446 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 447 | ssize_t |
| 448 | writev (int fd, const struct iovec * iov, int iovcnt) |
| 449 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 450 | ssize_t size = 0, total = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 451 | vls_handle_t vlsh; |
Dave Wallace | 8aaba56 | 2018-01-18 17:21:19 -0500 | [diff] [blame] | 452 | int i, rv = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 453 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 454 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 455 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 456 | vlsh = ldp_fd_to_vlsh (fd); |
| 457 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 458 | { |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 459 | for (i = 0; i < iovcnt; ++i) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 460 | { |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 461 | rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len); |
| 462 | if (rv < 0) |
| 463 | break; |
| 464 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 465 | { |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 466 | total += rv; |
| 467 | if (rv < iov[i].iov_len) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 468 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 469 | } |
| 470 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 471 | |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 472 | if (rv < 0 && total == 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 473 | { |
| 474 | errno = -rv; |
| 475 | size = -1; |
| 476 | } |
| 477 | else |
| 478 | size = total; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 482 | size = libc_writev (fd, iov, iovcnt); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 485 | return size; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 488 | static int |
| 489 | fcntl_internal (int fd, int cmd, va_list ap) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 490 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 491 | vls_handle_t vlsh; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 492 | int rv = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 493 | |
| 494 | vlsh = ldp_fd_to_vlsh (fd); |
| 495 | LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd); |
| 496 | if (vlsh != VLS_INVALID_HANDLE) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 497 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 498 | int flags = va_arg (ap, int); |
| 499 | u32 size; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 500 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 501 | size = sizeof (flags); |
| 502 | rv = -EOPNOTSUPP; |
| 503 | switch (cmd) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 504 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 505 | case F_SETFL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 506 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 507 | break; |
| 508 | |
| 509 | case F_GETFL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 510 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 511 | if (rv == VPPCOM_OK) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 512 | rv = flags; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 513 | break; |
Florin Coras | 173bae3 | 2018-11-16 18:56:28 -0800 | [diff] [blame] | 514 | case F_SETFD: |
| 515 | /* TODO handle this */ |
| 516 | LDBG (0, "F_SETFD ignored flags %u", flags); |
| 517 | rv = 0; |
| 518 | break; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 519 | default: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 520 | rv = -EOPNOTSUPP; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 521 | break; |
| 522 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 523 | if (rv < 0) |
| 524 | { |
| 525 | errno = -rv; |
| 526 | rv = -1; |
| 527 | } |
| 528 | } |
| 529 | else |
| 530 | { |
Carl Smith | e16707b | 2019-11-13 14:37:39 +1300 | [diff] [blame] | 531 | #ifdef HAVE_FCNTL64 |
| 532 | rv = libc_vfcntl64 (fd, cmd, ap); |
| 533 | #else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 534 | rv = libc_vfcntl (fd, cmd, ap); |
Carl Smith | e16707b | 2019-11-13 14:37:39 +1300 | [diff] [blame] | 535 | #endif |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 538 | return rv; |
| 539 | } |
| 540 | |
| 541 | int |
| 542 | fcntl (int fd, int cmd, ...) |
| 543 | { |
| 544 | va_list ap; |
| 545 | int rv; |
| 546 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 547 | ldp_init_check (); |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 548 | |
| 549 | va_start (ap, cmd); |
| 550 | rv = fcntl_internal (fd, cmd, ap); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 551 | va_end (ap); |
| 552 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 553 | return rv; |
| 554 | } |
| 555 | |
| 556 | int |
Florin Coras | d7586d5 | 2020-04-29 02:19:51 +0000 | [diff] [blame] | 557 | fcntl64 (int fd, int cmd, ...) |
| 558 | { |
| 559 | va_list ap; |
| 560 | int rv; |
| 561 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 562 | ldp_init_check (); |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 563 | |
Florin Coras | d7586d5 | 2020-04-29 02:19:51 +0000 | [diff] [blame] | 564 | va_start (ap, cmd); |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 565 | rv = fcntl_internal (fd, cmd, ap); |
Florin Coras | d7586d5 | 2020-04-29 02:19:51 +0000 | [diff] [blame] | 566 | va_end (ap); |
| 567 | return rv; |
| 568 | } |
| 569 | |
| 570 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 571 | ioctl (int fd, unsigned long int cmd, ...) |
| 572 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 573 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 574 | va_list ap; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 575 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 576 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 577 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 578 | |
| 579 | va_start (ap, cmd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 580 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 581 | vlsh = ldp_fd_to_vlsh (fd); |
| 582 | if (vlsh != VLS_INVALID_HANDLE) |
| 583 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 584 | switch (cmd) |
| 585 | { |
| 586 | case FIONREAD: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 587 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 588 | break; |
| 589 | |
| 590 | case FIONBIO: |
| 591 | { |
| 592 | u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0; |
| 593 | u32 size = sizeof (flags); |
| 594 | |
| 595 | /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than |
| 596 | * non-blocking, the flags should be read here and merged |
| 597 | * with O_NONBLOCK. |
| 598 | */ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 599 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 600 | } |
| 601 | break; |
| 602 | |
| 603 | default: |
| 604 | rv = -EOPNOTSUPP; |
| 605 | break; |
| 606 | } |
| 607 | if (rv < 0) |
| 608 | { |
| 609 | errno = -rv; |
| 610 | rv = -1; |
| 611 | } |
| 612 | } |
| 613 | else |
| 614 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 615 | rv = libc_vioctl (fd, cmd, ap); |
| 616 | } |
| 617 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 618 | va_end (ap); |
| 619 | return rv; |
| 620 | } |
| 621 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 622 | always_inline void |
| 623 | ldp_select_init_maps (fd_set * __restrict original, |
| 624 | clib_bitmap_t ** resultb, clib_bitmap_t ** libcb, |
| 625 | clib_bitmap_t ** vclb, int nfds, u32 minbits, |
| 626 | u32 n_bytes, uword * si_bits, uword * libc_bits) |
| 627 | { |
| 628 | uword si_bits_set, libc_bits_set; |
| 629 | vls_handle_t vlsh; |
| 630 | int fd; |
| 631 | |
| 632 | clib_bitmap_validate (*vclb, minbits); |
| 633 | clib_bitmap_validate (*libcb, minbits); |
| 634 | clib_bitmap_validate (*resultb, minbits); |
| 635 | clib_memcpy_fast (*resultb, original, n_bytes); |
| 636 | memset (original, 0, n_bytes); |
| 637 | |
| 638 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 639 | clib_bitmap_foreach (fd, *resultb) { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 640 | if (fd > nfds) |
| 641 | break; |
| 642 | vlsh = ldp_fd_to_vlsh (fd); |
| 643 | if (vlsh == VLS_INVALID_HANDLE) |
| 644 | clib_bitmap_set_no_check (*libcb, fd, 1); |
| 645 | else |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 646 | *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1); |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 647 | } |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 648 | /* *INDENT-ON* */ |
| 649 | |
| 650 | si_bits_set = clib_bitmap_last_set (*vclb) + 1; |
| 651 | *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits; |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 652 | clib_bitmap_validate (*resultb, *si_bits); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 653 | |
| 654 | libc_bits_set = clib_bitmap_last_set (*libcb) + 1; |
| 655 | *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits; |
| 656 | } |
| 657 | |
| 658 | always_inline int |
| 659 | ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb) |
| 660 | { |
| 661 | vls_handle_t vlsh; |
| 662 | uword si; |
| 663 | int fd; |
| 664 | |
| 665 | if (!libcb) |
| 666 | return 0; |
| 667 | |
| 668 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 669 | clib_bitmap_foreach (si, vclb) { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 670 | vlsh = vls_session_index_to_vlsh (si); |
Florin Coras | 5414062 | 2020-02-04 19:04:34 +0000 | [diff] [blame] | 671 | ASSERT (vlsh != VLS_INVALID_HANDLE); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 672 | fd = ldp_vlsh_to_fd (vlsh); |
| 673 | if (PREDICT_FALSE (fd < 0)) |
| 674 | { |
| 675 | errno = EBADFD; |
| 676 | return -1; |
| 677 | } |
| 678 | FD_SET (fd, libcb); |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 679 | } |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 680 | /* *INDENT-ON* */ |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | always_inline void |
| 686 | ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb) |
| 687 | { |
| 688 | uword fd; |
| 689 | |
Florin Coras | 78b5fa6 | 2019-02-21 20:04:15 -0800 | [diff] [blame] | 690 | if (!libcb) |
| 691 | return; |
| 692 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 693 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 694 | clib_bitmap_foreach (fd, result) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 695 | FD_SET ((int)fd, libcb); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 696 | /* *INDENT-ON* */ |
| 697 | } |
| 698 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 699 | int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 700 | ldp_pselect (int nfds, fd_set * __restrict readfds, |
| 701 | fd_set * __restrict writefds, |
| 702 | fd_set * __restrict exceptfds, |
| 703 | const struct timespec *__restrict timeout, |
| 704 | const __sigset_t * __restrict sigmask) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 705 | { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 706 | u32 minbits = clib_max (nfds, BITS (uword)), n_bytes; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 707 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 708 | struct timespec libc_tspec = { 0 }; |
| 709 | f64 time_out, vcl_timeout = 0; |
| 710 | uword si_bits, libc_bits; |
| 711 | int rv, bits_set = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 712 | |
| 713 | if (nfds < 0) |
| 714 | { |
| 715 | errno = EINVAL; |
| 716 | return -1; |
| 717 | } |
| 718 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 719 | if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) |
| 720 | clib_time_init (&ldpw->clib_time); |
| 721 | |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 722 | if (timeout) |
| 723 | { |
| 724 | time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ? |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 725 | (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9; |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 726 | |
liuyacan | f71796e | 2021-08-02 10:01:39 +0800 | [diff] [blame] | 727 | time_out += clib_time_now (&ldpw->clib_time); |
| 728 | |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 729 | /* select as fine grained sleep */ |
| 730 | if (!nfds) |
| 731 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 732 | while (clib_time_now (&ldpw->clib_time) < time_out) |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 733 | ; |
| 734 | return 0; |
| 735 | } |
| 736 | } |
| 737 | else if (!nfds) |
| 738 | { |
| 739 | errno = EINVAL; |
| 740 | return -1; |
| 741 | } |
| 742 | else |
| 743 | time_out = -1; |
| 744 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 745 | if (nfds <= ldp->vlsh_bit_val) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 746 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 747 | rv = libc_pselect (nfds, readfds, writefds, exceptfds, |
| 748 | timeout, sigmask); |
| 749 | goto done; |
| 750 | } |
| 751 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 752 | si_bits = libc_bits = 0; |
| 753 | n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 754 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 755 | if (readfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 756 | ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap, |
| 757 | &ldpw->si_rd_bitmap, nfds, minbits, n_bytes, |
| 758 | &si_bits, &libc_bits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 759 | if (writefds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 760 | ldp_select_init_maps (writefds, &ldpw->wr_bitmap, |
| 761 | &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds, |
| 762 | minbits, n_bytes, &si_bits, &libc_bits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 763 | if (exceptfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 764 | ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap, |
| 765 | &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds, |
| 766 | minbits, n_bytes, &si_bits, &libc_bits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 767 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 768 | if (PREDICT_FALSE (!si_bits && !libc_bits)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 769 | { |
| 770 | errno = EINVAL; |
| 771 | rv = -1; |
| 772 | goto done; |
| 773 | } |
| 774 | |
Florin Coras | 78b5fa6 | 2019-02-21 20:04:15 -0800 | [diff] [blame] | 775 | if (!si_bits) |
| 776 | libc_tspec = timeout ? *timeout : libc_tspec; |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 777 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 778 | do |
| 779 | { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 780 | if (si_bits) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 781 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 782 | if (readfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 783 | clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap, |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 784 | vec_len (ldpw->si_rd_bitmap) * |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 785 | sizeof (clib_bitmap_t)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 786 | if (writefds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 787 | clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap, |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 788 | vec_len (ldpw->si_wr_bitmap) * |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 789 | sizeof (clib_bitmap_t)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 790 | if (exceptfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 791 | clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap, |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 792 | vec_len (ldpw->si_ex_bitmap) * |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 793 | sizeof (clib_bitmap_t)); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 794 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 795 | rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL, |
| 796 | writefds ? ldpw->wr_bitmap : NULL, |
| 797 | exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 798 | if (rv < 0) |
| 799 | { |
| 800 | errno = -rv; |
| 801 | rv = -1; |
Florin Coras | 5e6222a | 2020-04-24 17:09:25 +0000 | [diff] [blame] | 802 | goto done; |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 803 | } |
| 804 | else if (rv > 0) |
| 805 | { |
| 806 | if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds)) |
| 807 | { |
| 808 | rv = -1; |
| 809 | goto done; |
| 810 | } |
| 811 | |
| 812 | if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds)) |
| 813 | { |
| 814 | rv = -1; |
| 815 | goto done; |
| 816 | } |
| 817 | |
| 818 | if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds)) |
| 819 | { |
| 820 | rv = -1; |
| 821 | goto done; |
| 822 | } |
| 823 | bits_set = rv; |
| 824 | } |
| 825 | } |
| 826 | if (libc_bits) |
| 827 | { |
| 828 | if (readfds) |
| 829 | clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap, |
| 830 | vec_len (ldpw->libc_rd_bitmap) * |
| 831 | sizeof (clib_bitmap_t)); |
| 832 | if (writefds) |
| 833 | clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap, |
| 834 | vec_len (ldpw->libc_wr_bitmap) * |
| 835 | sizeof (clib_bitmap_t)); |
| 836 | if (exceptfds) |
| 837 | clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap, |
| 838 | vec_len (ldpw->libc_ex_bitmap) * |
| 839 | sizeof (clib_bitmap_t)); |
| 840 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 841 | rv = libc_pselect (libc_bits, |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 842 | readfds ? (fd_set *) ldpw->rd_bitmap : NULL, |
| 843 | writefds ? (fd_set *) ldpw->wr_bitmap : NULL, |
| 844 | exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL, |
| 845 | &libc_tspec, sigmask); |
| 846 | if (rv > 0) |
| 847 | { |
| 848 | ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds); |
| 849 | ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds); |
| 850 | ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds); |
| 851 | bits_set += rv; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if (bits_set) |
| 856 | { |
| 857 | rv = bits_set; |
| 858 | goto done; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 859 | } |
| 860 | } |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 861 | while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 862 | rv = 0; |
| 863 | |
| 864 | done: |
| 865 | /* TBD: set timeout to amount of time left */ |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 866 | clib_bitmap_zero (ldpw->rd_bitmap); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 867 | clib_bitmap_zero (ldpw->si_rd_bitmap); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 868 | clib_bitmap_zero (ldpw->libc_rd_bitmap); |
| 869 | clib_bitmap_zero (ldpw->wr_bitmap); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 870 | clib_bitmap_zero (ldpw->si_wr_bitmap); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 871 | clib_bitmap_zero (ldpw->libc_wr_bitmap); |
| 872 | clib_bitmap_zero (ldpw->ex_bitmap); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 873 | clib_bitmap_zero (ldpw->si_ex_bitmap); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 874 | clib_bitmap_zero (ldpw->libc_ex_bitmap); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 875 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 876 | return rv; |
| 877 | } |
| 878 | |
| 879 | int |
| 880 | select (int nfds, fd_set * __restrict readfds, |
| 881 | fd_set * __restrict writefds, |
| 882 | fd_set * __restrict exceptfds, struct timeval *__restrict timeout) |
| 883 | { |
| 884 | struct timespec tspec; |
| 885 | |
| 886 | if (timeout) |
| 887 | { |
| 888 | tspec.tv_sec = timeout->tv_sec; |
| 889 | tspec.tv_nsec = timeout->tv_usec * 1000; |
| 890 | } |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 891 | return ldp_pselect (nfds, readfds, writefds, exceptfds, |
| 892 | timeout ? &tspec : NULL, NULL); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | #ifdef __USE_XOPEN2K |
| 896 | int |
| 897 | pselect (int nfds, fd_set * __restrict readfds, |
| 898 | fd_set * __restrict writefds, |
| 899 | fd_set * __restrict exceptfds, |
| 900 | const struct timespec *__restrict timeout, |
| 901 | const __sigset_t * __restrict sigmask) |
| 902 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 903 | return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 904 | } |
| 905 | #endif |
| 906 | |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 907 | /* If transparent TLS mode is turned on, then ldp will load key and cert. |
| 908 | */ |
| 909 | static int |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 910 | load_cert_key_pair (void) |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 911 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 912 | char *cert_str = getenv (LDP_ENV_TLS_CERT); |
| 913 | char *key_str = getenv (LDP_ENV_TLS_KEY); |
| 914 | char cert_buf[4096], key_buf[4096]; |
| 915 | int cert_size, key_size; |
| 916 | vppcom_cert_key_pair_t crypto; |
| 917 | int ckp_index; |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 918 | FILE *fp; |
| 919 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 920 | if (!cert_str || !key_str) |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 921 | { |
| 922 | LDBG (0, "ERROR: failed to read LDP environment %s\n", |
| 923 | LDP_ENV_TLS_CERT); |
| 924 | return -1; |
| 925 | } |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 926 | |
| 927 | fp = fopen (cert_str, "r"); |
| 928 | if (fp == NULL) |
| 929 | { |
| 930 | LDBG (0, "ERROR: failed to open cert file %s \n", cert_str); |
| 931 | return -1; |
| 932 | } |
| 933 | cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp); |
| 934 | fclose (fp); |
| 935 | |
| 936 | fp = fopen (key_str, "r"); |
| 937 | if (fp == NULL) |
| 938 | { |
| 939 | LDBG (0, "ERROR: failed to open key file %s \n", key_str); |
| 940 | return -1; |
| 941 | } |
| 942 | key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp); |
| 943 | fclose (fp); |
| 944 | |
| 945 | crypto.cert = cert_buf; |
| 946 | crypto.key = key_buf; |
| 947 | crypto.cert_len = cert_size; |
| 948 | crypto.key_len = key_size; |
| 949 | ckp_index = vppcom_add_cert_key_pair (&crypto); |
| 950 | if (ckp_index < 0) |
| 951 | { |
| 952 | LDBG (0, "ERROR: failed to add cert key pair\n"); |
| 953 | return -1; |
| 954 | } |
| 955 | |
| 956 | ldp->ckpair_index = ckp_index; |
| 957 | |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static int |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 962 | assign_cert_key_pair (vls_handle_t vlsh) |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 963 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 964 | uint32_t ckp_len; |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 965 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 966 | if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0) |
| 967 | return -1; |
| 968 | |
| 969 | ckp_len = sizeof (ldp->ckpair_index); |
| 970 | return vppcom_session_attr (vlsh_to_session_index (vlsh), |
| 971 | VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, |
| 972 | &ckp_len); |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 973 | } |
| 974 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 975 | int |
| 976 | socket (int domain, int type, int protocol) |
| 977 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 978 | int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 979 | u8 is_nonblocking = type & SOCK_NONBLOCK ? 1 : 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 980 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 981 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 982 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 983 | |
| 984 | if (((domain == AF_INET) || (domain == AF_INET6)) && |
| 985 | ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM))) |
| 986 | { |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 987 | u8 proto; |
| 988 | if (ldp->transparent_tls) |
| 989 | { |
| 990 | proto = VPPCOM_PROTO_TLS; |
| 991 | } |
| 992 | else |
| 993 | proto = ((sock_type == SOCK_DGRAM) ? |
| 994 | VPPCOM_PROTO_UDP : VPPCOM_PROTO_TCP); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 995 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 996 | LDBG (0, "calling vls_create: proto %u (%s), is_nonblocking %u", |
| 997 | proto, vppcom_proto_str (proto), is_nonblocking); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 998 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 999 | vlsh = vls_create (proto, is_nonblocking); |
| 1000 | if (vlsh < 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1001 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1002 | errno = -vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1003 | rv = -1; |
| 1004 | } |
| 1005 | else |
| 1006 | { |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 1007 | if (ldp->transparent_tls) |
| 1008 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 1009 | if (assign_cert_key_pair (vlsh) < 0) |
| 1010 | return -1; |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 1011 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1012 | rv = ldp_vlsh_to_fd (vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | else |
| 1016 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1017 | LDBG (0, "calling libc_socket"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1018 | rv = libc_socket (domain, type, protocol); |
| 1019 | } |
| 1020 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1021 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Create two new sockets, of type TYPE in domain DOMAIN and using |
| 1026 | * protocol PROTOCOL, which are connected to each other, and put file |
| 1027 | * descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero, |
| 1028 | * one will be chosen automatically. |
| 1029 | * Returns 0 on success, -1 for errors. |
| 1030 | * */ |
| 1031 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1032 | socketpair (int domain, int type, int protocol, int fds[2]) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1033 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1034 | int rv, sock_type = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1035 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1036 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1037 | |
| 1038 | if (((domain == AF_INET) || (domain == AF_INET6)) && |
| 1039 | ((sock_type == SOCK_STREAM) || (sock_type == SOCK_DGRAM))) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1040 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1041 | LDBG (0, "LDP-TBD"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1042 | errno = ENOSYS; |
| 1043 | rv = -1; |
| 1044 | } |
| 1045 | else |
| 1046 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1047 | LDBG (1, "calling libc_socketpair"); |
Florin Coras | 173bae3 | 2018-11-16 18:56:28 -0800 | [diff] [blame] | 1048 | rv = libc_socketpair (domain, type, protocol, fds); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1051 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1055 | bind (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1056 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1057 | vls_handle_t vlsh; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1058 | int rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1059 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1060 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1061 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1062 | vlsh = ldp_fd_to_vlsh (fd); |
| 1063 | if (vlsh != VLS_INVALID_HANDLE) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1064 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1065 | vppcom_endpt_t ep; |
| 1066 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1067 | switch (addr->sa_family) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1068 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1069 | case AF_INET: |
| 1070 | if (len != sizeof (struct sockaddr_in)) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1071 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1072 | LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET addr len %u!", |
| 1073 | fd, vlsh, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1074 | errno = EINVAL; |
| 1075 | rv = -1; |
| 1076 | goto done; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1077 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1078 | ep.is_ip4 = VPPCOM_IS_IP4; |
| 1079 | ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr; |
| 1080 | ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port; |
| 1081 | break; |
| 1082 | |
| 1083 | case AF_INET6: |
| 1084 | if (len != sizeof (struct sockaddr_in6)) |
| 1085 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1086 | LDBG (0, "ERROR: fd %d: vlsh %u: Invalid AF_INET6 addr len %u!", |
| 1087 | fd, vlsh, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1088 | errno = EINVAL; |
| 1089 | rv = -1; |
| 1090 | goto done; |
| 1091 | } |
| 1092 | ep.is_ip4 = VPPCOM_IS_IP6; |
| 1093 | ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr; |
| 1094 | ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1095 | break; |
| 1096 | |
| 1097 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1098 | LDBG (0, "ERROR: fd %d: vlsh %u: Unsupported address family %u!", |
| 1099 | fd, vlsh, addr->sa_family); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1100 | errno = EAFNOSUPPORT; |
| 1101 | rv = -1; |
| 1102 | goto done; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1103 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1104 | LDBG (0, "fd %d: calling vls_bind: vlsh %u, addr %p, len %u", fd, vlsh, |
| 1105 | addr, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1106 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1107 | rv = vls_bind (vlsh, &ep); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1108 | if (rv != VPPCOM_OK) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1109 | { |
| 1110 | errno = -rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1111 | rv = -1; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1112 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1113 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1114 | else |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1115 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1116 | LDBG (0, "fd %d: calling libc_bind: addr %p, len %u", fd, addr, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1117 | rv = libc_bind (fd, addr, len); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1120 | done: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1121 | LDBG (1, "fd %d: returning %d", fd, rv); |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 1122 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1123 | return rv; |
| 1124 | } |
| 1125 | |
| 1126 | static inline int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1127 | ldp_copy_ep_to_sockaddr (__SOCKADDR_ARG addr, socklen_t * __restrict len, |
| 1128 | vppcom_endpt_t * ep) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1129 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1130 | int rv = 0; |
| 1131 | int sa_len, copy_len; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1132 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1133 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1134 | |
| 1135 | if (addr && len && ep) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1136 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1137 | addr->sa_family = (ep->is_ip4 == VPPCOM_IS_IP4) ? AF_INET : AF_INET6; |
| 1138 | switch (addr->sa_family) |
| 1139 | { |
| 1140 | case AF_INET: |
| 1141 | ((struct sockaddr_in *) addr)->sin_port = ep->port; |
| 1142 | if (*len > sizeof (struct sockaddr_in)) |
| 1143 | *len = sizeof (struct sockaddr_in); |
| 1144 | sa_len = sizeof (struct sockaddr_in) - sizeof (struct in_addr); |
| 1145 | copy_len = *len - sa_len; |
| 1146 | if (copy_len > 0) |
| 1147 | memcpy (&((struct sockaddr_in *) addr)->sin_addr, ep->ip, |
| 1148 | copy_len); |
| 1149 | break; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1150 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1151 | case AF_INET6: |
| 1152 | ((struct sockaddr_in6 *) addr)->sin6_port = ep->port; |
| 1153 | if (*len > sizeof (struct sockaddr_in6)) |
| 1154 | *len = sizeof (struct sockaddr_in6); |
| 1155 | sa_len = sizeof (struct sockaddr_in6) - sizeof (struct in6_addr); |
| 1156 | copy_len = *len - sa_len; |
| 1157 | if (copy_len > 0) |
| 1158 | memcpy (((struct sockaddr_in6 *) addr)->sin6_addr. |
| 1159 | __in6_u.__u6_addr8, ep->ip, copy_len); |
| 1160 | break; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1161 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1162 | default: |
| 1163 | /* Not possible */ |
| 1164 | rv = -EAFNOSUPPORT; |
| 1165 | break; |
| 1166 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1167 | } |
Dave Wallace | e695cb4 | 2017-11-02 22:04:42 -0400 | [diff] [blame] | 1168 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1171 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1172 | getsockname (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1173 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1174 | vls_handle_t vlsh; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1175 | int rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1176 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1177 | ldp_init_check (); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1178 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1179 | vlsh = ldp_fd_to_vlsh (fd); |
| 1180 | if (vlsh != VLS_INVALID_HANDLE) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1181 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1182 | vppcom_endpt_t ep; |
| 1183 | u8 addr_buf[sizeof (struct in6_addr)]; |
| 1184 | u32 size = sizeof (ep); |
| 1185 | |
| 1186 | ep.ip = addr_buf; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1187 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1188 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LCL_ADDR, &ep, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1189 | if (rv != VPPCOM_OK) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1190 | { |
| 1191 | errno = -rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1192 | rv = -1; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1193 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1194 | else |
| 1195 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1196 | rv = ldp_copy_ep_to_sockaddr (addr, len, &ep); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1197 | if (rv != VPPCOM_OK) |
| 1198 | { |
| 1199 | errno = -rv; |
| 1200 | rv = -1; |
| 1201 | } |
| 1202 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1203 | } |
| 1204 | else |
| 1205 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1206 | rv = libc_getsockname (fd, addr, len); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1209 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1212 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1213 | connect (int fd, __CONST_SOCKADDR_ARG addr, socklen_t len) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1214 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1215 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1216 | int rv; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1217 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1218 | ldp_init_check (); |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1219 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1220 | if (!addr) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1221 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1222 | LDBG (0, "ERROR: fd %d: NULL addr, len %u", fd, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1223 | errno = EINVAL; |
| 1224 | rv = -1; |
| 1225 | goto done; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1228 | vlsh = ldp_fd_to_vlsh (fd); |
| 1229 | if (vlsh != VLS_INVALID_HANDLE) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1230 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1231 | vppcom_endpt_t ep; |
| 1232 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1233 | switch (addr->sa_family) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1234 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1235 | case AF_INET: |
| 1236 | if (len != sizeof (struct sockaddr_in)) |
| 1237 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1238 | LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET addr len %u!", |
| 1239 | fd, vlsh, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1240 | errno = EINVAL; |
| 1241 | rv = -1; |
| 1242 | goto done; |
| 1243 | } |
| 1244 | ep.is_ip4 = VPPCOM_IS_IP4; |
| 1245 | ep.ip = (u8 *) & ((const struct sockaddr_in *) addr)->sin_addr; |
| 1246 | ep.port = (u16) ((const struct sockaddr_in *) addr)->sin_port; |
| 1247 | break; |
| 1248 | |
| 1249 | case AF_INET6: |
| 1250 | if (len != sizeof (struct sockaddr_in6)) |
| 1251 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1252 | LDBG (0, "fd %d: ERROR vlsh %u: Invalid AF_INET6 addr len %u!", |
| 1253 | fd, vlsh, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1254 | errno = EINVAL; |
| 1255 | rv = -1; |
| 1256 | goto done; |
| 1257 | } |
| 1258 | ep.is_ip4 = VPPCOM_IS_IP6; |
| 1259 | ep.ip = (u8 *) & ((const struct sockaddr_in6 *) addr)->sin6_addr; |
| 1260 | ep.port = (u16) ((const struct sockaddr_in6 *) addr)->sin6_port; |
| 1261 | break; |
| 1262 | |
| 1263 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1264 | LDBG (0, "fd %d: ERROR vlsh %u: Unsupported address family %u!", |
| 1265 | fd, vlsh, addr->sa_family); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1266 | errno = EAFNOSUPPORT; |
| 1267 | rv = -1; |
| 1268 | goto done; |
| 1269 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1270 | LDBG (0, "fd %d: calling vls_connect(): vlsh %u addr %p len %u", fd, |
| 1271 | vlsh, addr, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1272 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1273 | rv = vls_connect (vlsh, &ep); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1274 | if (rv != VPPCOM_OK) |
| 1275 | { |
| 1276 | errno = -rv; |
| 1277 | rv = -1; |
| 1278 | } |
| 1279 | } |
| 1280 | else |
| 1281 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1282 | LDBG (0, "fd %d: calling libc_connect(): addr %p, len %u", |
| 1283 | fd, addr, len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1284 | |
| 1285 | rv = libc_connect (fd, addr, len); |
| 1286 | } |
| 1287 | |
| 1288 | done: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1289 | LDBG (1, "fd %d: returning %d (0x%x)", fd, rv, rv); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1290 | return rv; |
| 1291 | } |
| 1292 | |
| 1293 | int |
| 1294 | getpeername (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict len) |
| 1295 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1296 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1297 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1298 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1299 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1300 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1301 | vlsh = ldp_fd_to_vlsh (fd); |
| 1302 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1303 | { |
| 1304 | vppcom_endpt_t ep; |
| 1305 | u8 addr_buf[sizeof (struct in6_addr)]; |
| 1306 | u32 size = sizeof (ep); |
| 1307 | |
| 1308 | ep.ip = addr_buf; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1309 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PEER_ADDR, &ep, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1310 | if (rv != VPPCOM_OK) |
| 1311 | { |
| 1312 | errno = -rv; |
| 1313 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1314 | } |
| 1315 | else |
| 1316 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1317 | rv = ldp_copy_ep_to_sockaddr (addr, len, &ep); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1318 | if (rv != VPPCOM_OK) |
| 1319 | { |
| 1320 | errno = -rv; |
| 1321 | rv = -1; |
| 1322 | } |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1325 | else |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1326 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1327 | rv = libc_getpeername (fd, addr, len); |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1330 | return rv; |
| 1331 | } |
| 1332 | |
| 1333 | ssize_t |
| 1334 | send (int fd, const void *buf, size_t n, int flags) |
| 1335 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1336 | vls_handle_t vlsh = ldp_fd_to_vlsh (fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1337 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1338 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1339 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1340 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1341 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1342 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1343 | size = vls_sendto (vlsh, (void *) buf, n, flags, NULL); |
qchang | aa8f63c | 2018-05-30 11:44:18 -0700 | [diff] [blame] | 1344 | if (size < VPPCOM_OK) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1345 | { |
| 1346 | errno = -size; |
| 1347 | size = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1348 | } |
| 1349 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1350 | else |
| 1351 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1352 | size = libc_send (fd, buf, n, flags); |
| 1353 | } |
| 1354 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1355 | return size; |
| 1356 | } |
| 1357 | |
| 1358 | ssize_t |
| 1359 | sendfile (int out_fd, int in_fd, off_t * offset, size_t len) |
| 1360 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1361 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1362 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1363 | ssize_t size = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1364 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1365 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1366 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1367 | vlsh = ldp_fd_to_vlsh (out_fd); |
| 1368 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1369 | { |
| 1370 | int rv; |
| 1371 | ssize_t results = 0; |
| 1372 | size_t n_bytes_left = len; |
| 1373 | size_t bytes_to_read; |
| 1374 | int nbytes; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1375 | u8 eagain = 0; |
| 1376 | u32 flags, flags_len = sizeof (flags); |
| 1377 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1378 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &flags_len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1379 | if (PREDICT_FALSE (rv != VPPCOM_OK)) |
| 1380 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1381 | LDBG (0, "ERROR: out fd %d: vls_attr: vlsh %u, returned %d (%s)!", |
| 1382 | out_fd, vlsh, rv, vppcom_retval_str (rv)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1383 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1384 | vec_reset_length (ldpw->io_buffer); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1385 | errno = -rv; |
| 1386 | size = -1; |
| 1387 | goto done; |
| 1388 | } |
| 1389 | |
| 1390 | if (offset) |
| 1391 | { |
| 1392 | off_t off = lseek (in_fd, *offset, SEEK_SET); |
| 1393 | if (PREDICT_FALSE (off == -1)) |
| 1394 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1395 | size = -1; |
| 1396 | goto done; |
| 1397 | } |
| 1398 | |
| 1399 | ASSERT (off == *offset); |
| 1400 | } |
| 1401 | |
| 1402 | do |
| 1403 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1404 | size = vls_attr (vlsh, VPPCOM_ATTR_GET_NWRITE, 0, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1405 | if (size < 0) |
| 1406 | { |
Florin Coras | eb801d0 | 2020-09-16 17:44:58 -0700 | [diff] [blame] | 1407 | LDBG (0, "ERROR: fd %d: vls_attr: vlsh %u returned %ld (%s)!", |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1408 | out_fd, vlsh, size, vppcom_retval_str (size)); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1409 | vec_reset_length (ldpw->io_buffer); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1410 | errno = -size; |
| 1411 | size = -1; |
| 1412 | goto done; |
| 1413 | } |
| 1414 | |
| 1415 | bytes_to_read = size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1416 | if (bytes_to_read == 0) |
| 1417 | { |
| 1418 | if (flags & O_NONBLOCK) |
| 1419 | { |
| 1420 | if (!results) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1421 | eagain = 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1422 | goto update_offset; |
| 1423 | } |
| 1424 | else |
| 1425 | continue; |
| 1426 | } |
| 1427 | bytes_to_read = clib_min (n_bytes_left, bytes_to_read); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1428 | vec_validate (ldpw->io_buffer, bytes_to_read); |
| 1429 | nbytes = libc_read (in_fd, ldpw->io_buffer, bytes_to_read); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1430 | if (nbytes < 0) |
| 1431 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1432 | if (results == 0) |
| 1433 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1434 | vec_reset_length (ldpw->io_buffer); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1435 | size = -1; |
| 1436 | goto done; |
| 1437 | } |
| 1438 | goto update_offset; |
| 1439 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1440 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1441 | size = vls_write (vlsh, ldpw->io_buffer, nbytes); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1442 | if (size < 0) |
| 1443 | { |
| 1444 | if (size == VPPCOM_EAGAIN) |
| 1445 | { |
| 1446 | if (flags & O_NONBLOCK) |
| 1447 | { |
| 1448 | if (!results) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1449 | eagain = 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1450 | goto update_offset; |
| 1451 | } |
| 1452 | else |
| 1453 | continue; |
| 1454 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1455 | if (results == 0) |
| 1456 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1457 | vec_reset_length (ldpw->io_buffer); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1458 | errno = -size; |
| 1459 | size = -1; |
| 1460 | goto done; |
| 1461 | } |
| 1462 | goto update_offset; |
| 1463 | } |
| 1464 | |
| 1465 | results += nbytes; |
| 1466 | ASSERT (n_bytes_left >= nbytes); |
| 1467 | n_bytes_left = n_bytes_left - nbytes; |
| 1468 | } |
| 1469 | while (n_bytes_left > 0); |
| 1470 | |
| 1471 | update_offset: |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 1472 | vec_reset_length (ldpw->io_buffer); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1473 | if (offset) |
| 1474 | { |
| 1475 | off_t off = lseek (in_fd, *offset, SEEK_SET); |
| 1476 | if (PREDICT_FALSE (off == -1)) |
| 1477 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1478 | size = -1; |
| 1479 | goto done; |
| 1480 | } |
| 1481 | |
| 1482 | ASSERT (off == *offset); |
| 1483 | *offset += results + 1; |
| 1484 | } |
| 1485 | if (eagain) |
| 1486 | { |
| 1487 | errno = EAGAIN; |
| 1488 | size = -1; |
| 1489 | } |
| 1490 | else |
| 1491 | size = results; |
| 1492 | } |
| 1493 | else |
| 1494 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1495 | size = libc_sendfile (out_fd, in_fd, offset, len); |
| 1496 | } |
| 1497 | |
| 1498 | done: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1499 | return size; |
| 1500 | } |
| 1501 | |
| 1502 | ssize_t |
| 1503 | sendfile64 (int out_fd, int in_fd, off_t * offset, size_t len) |
| 1504 | { |
| 1505 | return sendfile (out_fd, in_fd, offset, len); |
| 1506 | } |
| 1507 | |
| 1508 | ssize_t |
| 1509 | recv (int fd, void *buf, size_t n, int flags) |
| 1510 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1511 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1512 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1513 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1514 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1515 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1516 | vlsh = ldp_fd_to_vlsh (fd); |
| 1517 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1518 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1519 | size = vls_recvfrom (vlsh, buf, n, flags, NULL); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1520 | if (size < 0) |
Florin Coras | 2a6642e | 2020-03-24 15:24:29 +0000 | [diff] [blame] | 1521 | { |
| 1522 | errno = -size; |
| 1523 | size = -1; |
| 1524 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1525 | } |
| 1526 | else |
| 1527 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1528 | size = libc_recv (fd, buf, n, flags); |
| 1529 | } |
| 1530 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1531 | return size; |
| 1532 | } |
| 1533 | |
Sivaprasad Tummala | fdcbd38 | 2021-07-31 21:38:19 +0530 | [diff] [blame] | 1534 | ssize_t |
| 1535 | __recv_chk (int fd, void *buf, size_t n, size_t buflen, int flags) |
| 1536 | { |
| 1537 | if (n > buflen) |
| 1538 | return -1; |
| 1539 | |
| 1540 | return recv (fd, buf, n, flags); |
| 1541 | } |
| 1542 | |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1543 | static int |
| 1544 | ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, int flags, |
| 1545 | __CONST_SOCKADDR_ARG addr, socklen_t addr_len) |
| 1546 | { |
| 1547 | vppcom_endpt_t *ep = 0; |
| 1548 | vppcom_endpt_t _ep; |
| 1549 | |
| 1550 | if (addr) |
| 1551 | { |
| 1552 | ep = &_ep; |
| 1553 | switch (addr->sa_family) |
| 1554 | { |
| 1555 | case AF_INET: |
| 1556 | ep->is_ip4 = VPPCOM_IS_IP4; |
| 1557 | ep->ip = |
| 1558 | (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr; |
| 1559 | ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port; |
| 1560 | break; |
| 1561 | |
| 1562 | case AF_INET6: |
| 1563 | ep->is_ip4 = VPPCOM_IS_IP6; |
| 1564 | ep->ip = |
| 1565 | (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr; |
| 1566 | ep->port = |
| 1567 | (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port; |
| 1568 | break; |
| 1569 | |
| 1570 | default: |
| 1571 | return EAFNOSUPPORT; |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | return vls_sendto (vlsh, (void *) buf, n, flags, ep); |
| 1576 | } |
| 1577 | |
| 1578 | static int |
| 1579 | ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n, |
| 1580 | int flags, __SOCKADDR_ARG addr, |
| 1581 | socklen_t * __restrict addr_len) |
| 1582 | { |
| 1583 | u8 src_addr[sizeof (struct sockaddr_in6)]; |
| 1584 | vppcom_endpt_t ep; |
| 1585 | ssize_t size; |
| 1586 | int rv; |
| 1587 | |
| 1588 | if (addr) |
| 1589 | { |
| 1590 | ep.ip = src_addr; |
| 1591 | size = vls_recvfrom (vlsh, buf, n, flags, &ep); |
| 1592 | |
| 1593 | if (size > 0) |
| 1594 | { |
| 1595 | rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep); |
| 1596 | if (rv < 0) |
| 1597 | size = rv; |
| 1598 | } |
| 1599 | } |
| 1600 | else |
| 1601 | size = vls_recvfrom (vlsh, buf, n, flags, NULL); |
| 1602 | |
| 1603 | return size; |
| 1604 | } |
| 1605 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1606 | ssize_t |
| 1607 | sendto (int fd, const void *buf, size_t n, int flags, |
| 1608 | __CONST_SOCKADDR_ARG addr, socklen_t addr_len) |
| 1609 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1610 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1611 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1612 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1613 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1614 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1615 | vlsh = ldp_fd_to_vlsh (fd); |
wanghanlin | 97c6e0d | 2021-07-06 15:01:48 +0800 | [diff] [blame] | 1616 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1617 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1618 | size = ldp_vls_sendo (vlsh, buf, n, flags, addr, addr_len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1619 | if (size < 0) |
| 1620 | { |
| 1621 | errno = -size; |
| 1622 | size = -1; |
| 1623 | } |
| 1624 | } |
| 1625 | else |
| 1626 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1627 | size = libc_sendto (fd, buf, n, flags, addr, addr_len); |
| 1628 | } |
| 1629 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1630 | return size; |
| 1631 | } |
| 1632 | |
| 1633 | ssize_t |
| 1634 | recvfrom (int fd, void *__restrict buf, size_t n, int flags, |
| 1635 | __SOCKADDR_ARG addr, socklen_t * __restrict addr_len) |
| 1636 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1637 | vls_handle_t vlsh; |
| 1638 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1639 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1640 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1641 | |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1642 | vlsh = ldp_fd_to_vlsh (fd); |
| 1643 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1644 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1645 | size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1646 | if (size < 0) |
| 1647 | { |
| 1648 | errno = -size; |
| 1649 | size = -1; |
| 1650 | } |
| 1651 | } |
| 1652 | else |
| 1653 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1654 | size = libc_recvfrom (fd, buf, n, flags, addr, addr_len); |
| 1655 | } |
| 1656 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1657 | return size; |
| 1658 | } |
| 1659 | |
| 1660 | ssize_t |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1661 | sendmsg (int fd, const struct msghdr * msg, int flags) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1662 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1663 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1664 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1665 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1666 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1667 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1668 | vlsh = ldp_fd_to_vlsh (fd); |
| 1669 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1670 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1671 | struct iovec *iov = msg->msg_iov; |
| 1672 | ssize_t total = 0; |
| 1673 | int i, rv; |
| 1674 | |
| 1675 | for (i = 0; i < msg->msg_iovlen; ++i) |
| 1676 | { |
| 1677 | rv = ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, flags, |
| 1678 | msg->msg_name, msg->msg_namelen); |
| 1679 | if (rv < 0) |
| 1680 | break; |
| 1681 | else |
| 1682 | { |
| 1683 | total += rv; |
| 1684 | if (rv < iov[i].iov_len) |
| 1685 | break; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | if (rv < 0 && total == 0) |
| 1690 | { |
| 1691 | errno = -rv; |
| 1692 | size = -1; |
| 1693 | } |
| 1694 | else |
| 1695 | size = total; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1696 | } |
| 1697 | else |
| 1698 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1699 | size = libc_sendmsg (fd, msg, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1700 | } |
| 1701 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1702 | return size; |
| 1703 | } |
| 1704 | |
| 1705 | #ifdef USE_GNU |
| 1706 | int |
| 1707 | sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags) |
| 1708 | { |
| 1709 | ssize_t size; |
| 1710 | const char *func_str; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1711 | u32 sh = ldp_fd_to_vlsh (fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1712 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1713 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1714 | |
wanghanlin | 97c6e0d | 2021-07-06 15:01:48 +0800 | [diff] [blame] | 1715 | if (sh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1716 | { |
| 1717 | clib_warning ("LDP<%d>: LDP-TBD", getpid ()); |
| 1718 | errno = ENOSYS; |
| 1719 | size = -1; |
| 1720 | } |
| 1721 | else |
| 1722 | { |
| 1723 | func_str = "libc_sendmmsg"; |
| 1724 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1725 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1726 | clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " |
| 1727 | "vmessages %p, vlen %u, flags 0x%x", |
| 1728 | getpid (), fd, fd, func_str, vmessages, vlen, flags); |
| 1729 | |
| 1730 | size = libc_sendmmsg (fd, vmessages, vlen, flags); |
| 1731 | } |
| 1732 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1733 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1734 | { |
| 1735 | if (size < 0) |
| 1736 | { |
| 1737 | int errno_val = errno; |
| 1738 | perror (func_str); |
| 1739 | clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " |
| 1740 | "rv %d, errno = %d", getpid (), fd, fd, |
| 1741 | func_str, size, errno_val); |
| 1742 | errno = errno_val; |
| 1743 | } |
| 1744 | else |
| 1745 | clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", |
| 1746 | getpid (), fd, fd, size, size); |
| 1747 | } |
| 1748 | return size; |
| 1749 | } |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1750 | #endif |
| 1751 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1752 | ssize_t |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1753 | recvmsg (int fd, struct msghdr * msg, int flags) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1754 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1755 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1756 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1757 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1758 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1759 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1760 | vlsh = ldp_fd_to_vlsh (fd); |
| 1761 | if (vlsh != VLS_INVALID_HANDLE) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1762 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1763 | struct iovec *iov = msg->msg_iov; |
| 1764 | ssize_t max_deq, total = 0; |
| 1765 | int i, rv; |
| 1766 | |
| 1767 | max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0); |
| 1768 | if (!max_deq) |
| 1769 | return 0; |
| 1770 | |
| 1771 | for (i = 0; i < msg->msg_iovlen; i++) |
| 1772 | { |
| 1773 | rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags, |
| 1774 | (i == 0 ? msg->msg_name : NULL), |
| 1775 | (i == 0 ? &msg->msg_namelen : NULL)); |
| 1776 | if (rv <= 0) |
| 1777 | break; |
| 1778 | else |
| 1779 | { |
| 1780 | total += rv; |
| 1781 | if (rv < iov[i].iov_len) |
| 1782 | break; |
| 1783 | } |
| 1784 | if (total >= max_deq) |
| 1785 | break; |
| 1786 | } |
| 1787 | |
| 1788 | if (rv < 0 && total == 0) |
| 1789 | { |
| 1790 | errno = -rv; |
| 1791 | size = -1; |
| 1792 | } |
| 1793 | else |
| 1794 | size = total; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1795 | } |
| 1796 | else |
| 1797 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1798 | size = libc_recvmsg (fd, msg, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1799 | } |
| 1800 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1801 | return size; |
| 1802 | } |
| 1803 | |
| 1804 | #ifdef USE_GNU |
| 1805 | int |
| 1806 | recvmmsg (int fd, struct mmsghdr *vmessages, |
| 1807 | unsigned int vlen, int flags, struct timespec *tmo) |
| 1808 | { |
| 1809 | ssize_t size; |
| 1810 | const char *func_str; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1811 | u32 sh = ldp_fd_to_vlsh (fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1812 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1813 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1814 | |
wanghanlin | 97c6e0d | 2021-07-06 15:01:48 +0800 | [diff] [blame] | 1815 | if (sh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1816 | { |
| 1817 | clib_warning ("LDP<%d>: LDP-TBD", getpid ()); |
| 1818 | errno = ENOSYS; |
| 1819 | size = -1; |
| 1820 | } |
| 1821 | else |
| 1822 | { |
| 1823 | func_str = "libc_recvmmsg"; |
| 1824 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1825 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1826 | clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " |
| 1827 | "vmessages %p, vlen %u, flags 0x%x, tmo %p", |
| 1828 | getpid (), fd, fd, func_str, vmessages, vlen, |
| 1829 | flags, tmo); |
| 1830 | |
| 1831 | size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo); |
| 1832 | } |
| 1833 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1834 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1835 | { |
| 1836 | if (size < 0) |
| 1837 | { |
| 1838 | int errno_val = errno; |
| 1839 | perror (func_str); |
| 1840 | clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " |
| 1841 | "rv %d, errno = %d", getpid (), fd, fd, |
| 1842 | func_str, size, errno_val); |
| 1843 | errno = errno_val; |
| 1844 | } |
| 1845 | else |
| 1846 | clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", |
| 1847 | getpid (), fd, fd, size, size); |
| 1848 | } |
| 1849 | return size; |
| 1850 | } |
| 1851 | #endif |
| 1852 | |
| 1853 | int |
| 1854 | getsockopt (int fd, int level, int optname, |
| 1855 | void *__restrict optval, socklen_t * __restrict optlen) |
| 1856 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1857 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1858 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1859 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1860 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1861 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1862 | vlsh = ldp_fd_to_vlsh (fd); |
| 1863 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1864 | { |
| 1865 | rv = -EOPNOTSUPP; |
| 1866 | |
| 1867 | switch (level) |
| 1868 | { |
| 1869 | case SOL_TCP: |
| 1870 | switch (optname) |
| 1871 | { |
| 1872 | case TCP_NODELAY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1873 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY, |
| 1874 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1875 | break; |
| 1876 | case TCP_MAXSEG: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1877 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS, |
| 1878 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1879 | break; |
| 1880 | case TCP_KEEPIDLE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1881 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE, |
| 1882 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1883 | break; |
| 1884 | case TCP_KEEPINTVL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1885 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL, |
| 1886 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1887 | break; |
| 1888 | case TCP_INFO: |
| 1889 | if (optval && optlen && (*optlen == sizeof (struct tcp_info))) |
| 1890 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1891 | LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, " |
| 1892 | "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1893 | memset (optval, 0, *optlen); |
| 1894 | rv = VPPCOM_OK; |
| 1895 | } |
| 1896 | else |
| 1897 | rv = -EFAULT; |
| 1898 | break; |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 1899 | case TCP_CONGESTION: |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 1900 | *optlen = strlen ("cubic"); |
Dave Barach | 0250090 | 2020-04-04 18:34:41 -0400 | [diff] [blame] | 1901 | strncpy (optval, "cubic", *optlen + 1); |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 1902 | rv = 0; |
| 1903 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1904 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1905 | LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, " |
| 1906 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1907 | break; |
| 1908 | } |
| 1909 | break; |
| 1910 | case SOL_IPV6: |
| 1911 | switch (optname) |
| 1912 | { |
| 1913 | case IPV6_V6ONLY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1914 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1915 | break; |
| 1916 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1917 | LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u " |
| 1918 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1919 | break; |
| 1920 | } |
| 1921 | break; |
| 1922 | case SOL_SOCKET: |
| 1923 | switch (optname) |
| 1924 | { |
| 1925 | case SO_ACCEPTCONN: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1926 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1927 | break; |
| 1928 | case SO_KEEPALIVE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1929 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1930 | break; |
| 1931 | case SO_PROTOCOL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1932 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1933 | *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM; |
| 1934 | break; |
| 1935 | case SO_SNDBUF: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1936 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN, |
| 1937 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1938 | break; |
| 1939 | case SO_RCVBUF: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1940 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN, |
| 1941 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1942 | break; |
| 1943 | case SO_REUSEADDR: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1944 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1945 | break; |
wanghanlin | 0674f85 | 2021-02-22 10:38:36 +0800 | [diff] [blame] | 1946 | case SO_REUSEPORT: |
| 1947 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen); |
| 1948 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1949 | case SO_BROADCAST: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1950 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1951 | break; |
wanghanlin | 0674f85 | 2021-02-22 10:38:36 +0800 | [diff] [blame] | 1952 | case SO_DOMAIN: |
| 1953 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen); |
| 1954 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1955 | case SO_ERROR: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1956 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1957 | break; |
Florin Coras | 8eb8d50 | 2021-06-16 14:46:57 -0700 | [diff] [blame] | 1958 | case SO_BINDTODEVICE: |
| 1959 | rv = 0; |
| 1960 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1961 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1962 | LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u " |
| 1963 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1964 | break; |
| 1965 | } |
| 1966 | break; |
| 1967 | default: |
| 1968 | break; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1971 | if (rv != VPPCOM_OK) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1972 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1973 | errno = -rv; |
| 1974 | rv = -1; |
| 1975 | } |
| 1976 | } |
| 1977 | else |
| 1978 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1979 | rv = libc_getsockopt (fd, level, optname, optval, optlen); |
| 1980 | } |
| 1981 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1982 | return rv; |
| 1983 | } |
| 1984 | |
| 1985 | int |
| 1986 | setsockopt (int fd, int level, int optname, |
| 1987 | const void *optval, socklen_t optlen) |
| 1988 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1989 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1990 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1991 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1992 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1993 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1994 | vlsh = ldp_fd_to_vlsh (fd); |
| 1995 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1996 | { |
| 1997 | rv = -EOPNOTSUPP; |
| 1998 | |
| 1999 | switch (level) |
| 2000 | { |
| 2001 | case SOL_TCP: |
| 2002 | switch (optname) |
| 2003 | { |
| 2004 | case TCP_NODELAY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2005 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY, |
| 2006 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2007 | break; |
| 2008 | case TCP_MAXSEG: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2009 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS, |
| 2010 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2011 | break; |
| 2012 | case TCP_KEEPIDLE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2013 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE, |
| 2014 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2015 | break; |
| 2016 | case TCP_KEEPINTVL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2017 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL, |
| 2018 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2019 | break; |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 2020 | case TCP_CONGESTION: |
Florin Coras | 8509aa2 | 2019-04-04 12:55:30 -0700 | [diff] [blame] | 2021 | case TCP_CORK: |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 2022 | /* Ignore */ |
| 2023 | rv = 0; |
| 2024 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2025 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2026 | LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u" |
| 2027 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2028 | break; |
| 2029 | } |
| 2030 | break; |
| 2031 | case SOL_IPV6: |
| 2032 | switch (optname) |
| 2033 | { |
| 2034 | case IPV6_V6ONLY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2035 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY, |
| 2036 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2037 | break; |
| 2038 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2039 | LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u" |
| 2040 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2041 | break; |
| 2042 | } |
| 2043 | break; |
| 2044 | case SOL_SOCKET: |
| 2045 | switch (optname) |
| 2046 | { |
| 2047 | case SO_KEEPALIVE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2048 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE, |
| 2049 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2050 | break; |
| 2051 | case SO_REUSEADDR: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2052 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR, |
| 2053 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2054 | break; |
wanghanlin | 0674f85 | 2021-02-22 10:38:36 +0800 | [diff] [blame] | 2055 | case SO_REUSEPORT: |
| 2056 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval, |
| 2057 | &optlen); |
| 2058 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2059 | case SO_BROADCAST: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2060 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST, |
| 2061 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2062 | break; |
Florin Coras | 2f64790 | 2021-06-02 08:23:50 -0700 | [diff] [blame] | 2063 | case SO_LINGER: |
| 2064 | rv = 0; |
| 2065 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2066 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2067 | LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u " |
| 2068 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2069 | break; |
| 2070 | } |
| 2071 | break; |
| 2072 | default: |
| 2073 | break; |
| 2074 | } |
| 2075 | |
| 2076 | if (rv != VPPCOM_OK) |
| 2077 | { |
| 2078 | errno = -rv; |
| 2079 | rv = -1; |
| 2080 | } |
| 2081 | } |
| 2082 | else |
| 2083 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2084 | rv = libc_setsockopt (fd, level, optname, optval, optlen); |
| 2085 | } |
| 2086 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2087 | return rv; |
| 2088 | } |
| 2089 | |
| 2090 | int |
| 2091 | listen (int fd, int n) |
| 2092 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2093 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2094 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2095 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2096 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2097 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2098 | vlsh = ldp_fd_to_vlsh (fd); |
| 2099 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2100 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2101 | LDBG (0, "fd %d: calling vls_listen: vlsh %u, n %d", fd, vlsh, n); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2102 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2103 | rv = vls_listen (vlsh, n); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2104 | if (rv != VPPCOM_OK) |
| 2105 | { |
| 2106 | errno = -rv; |
| 2107 | rv = -1; |
| 2108 | } |
| 2109 | } |
| 2110 | else |
| 2111 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2112 | LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2113 | rv = libc_listen (fd, n); |
| 2114 | } |
| 2115 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2116 | LDBG (1, "fd %d: returning %d", fd, rv); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2117 | return rv; |
| 2118 | } |
| 2119 | |
| 2120 | static inline int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2121 | ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr, |
| 2122 | socklen_t * __restrict addr_len, int flags) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2123 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2124 | vls_handle_t listen_vlsh, accept_vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2125 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2126 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2127 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2128 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2129 | listen_vlsh = ldp_fd_to_vlsh (listen_fd); |
| 2130 | if (listen_vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2131 | { |
| 2132 | vppcom_endpt_t ep; |
| 2133 | u8 src_addr[sizeof (struct sockaddr_in6)]; |
Dave Wallace | 8aaba56 | 2018-01-18 17:21:19 -0500 | [diff] [blame] | 2134 | memset (&ep, 0, sizeof (ep)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2135 | ep.ip = src_addr; |
| 2136 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2137 | LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u," |
Florin Coras | eb801d0 | 2020-09-16 17:44:58 -0700 | [diff] [blame] | 2138 | " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2139 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2140 | accept_vlsh = vls_accept (listen_vlsh, &ep, flags); |
| 2141 | if (accept_vlsh < 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2142 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2143 | errno = -accept_vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2144 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2145 | } |
| 2146 | else |
| 2147 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2148 | rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2149 | if (rv != VPPCOM_OK) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2150 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2151 | (void) vls_close (accept_vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2152 | errno = -rv; |
| 2153 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2154 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2155 | else |
| 2156 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2157 | rv = ldp_vlsh_to_fd (accept_vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2158 | } |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2159 | } |
| 2160 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2161 | else |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2162 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2163 | LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p," |
| 2164 | " flags 0x%x", listen_fd, addr, addr_len, flags); |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2165 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2166 | rv = libc_accept4 (listen_fd, addr, addr_len, flags); |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2169 | LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv); |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 2170 | |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2171 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2174 | int |
| 2175 | accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len, |
| 2176 | int flags) |
| 2177 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2178 | return ldp_accept4 (fd, addr, addr_len, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2179 | } |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2180 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2181 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2182 | accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2183 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2184 | return ldp_accept4 (fd, addr, addr_len, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2185 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2186 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2187 | int |
| 2188 | shutdown (int fd, int how) |
| 2189 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2190 | vls_handle_t vlsh; |
liuyacan | 55c952e | 2021-06-13 14:54:55 +0800 | [diff] [blame] | 2191 | int rv = 0; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2192 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2193 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2194 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2195 | vlsh = ldp_fd_to_vlsh (fd); |
| 2196 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2197 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2198 | LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how); |
liuyacan | 55c952e | 2021-06-13 14:54:55 +0800 | [diff] [blame] | 2199 | rv = vls_shutdown (vlsh, how); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2200 | } |
| 2201 | else |
| 2202 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2203 | LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2204 | rv = libc_shutdown (fd, how); |
| 2205 | } |
| 2206 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2207 | return rv; |
| 2208 | } |
| 2209 | |
| 2210 | int |
| 2211 | epoll_create1 (int flags) |
| 2212 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2213 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2214 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2215 | int rv; |
| 2216 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2217 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2218 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2219 | if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ()) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2220 | { |
Florin Coras | 2d9b427 | 2019-03-11 10:14:37 -0700 | [diff] [blame] | 2221 | /* Make sure workers have been allocated */ |
| 2222 | if (!ldp->workers) |
| 2223 | { |
| 2224 | ldp_alloc_workers (); |
| 2225 | ldpw = ldp_worker_get_current (); |
| 2226 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2227 | rv = libc_epoll_create1 (flags); |
| 2228 | ldp->vcl_needs_real_epoll = 0; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2229 | ldpw->vcl_mq_epfd = rv; |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 2230 | LDBG (0, "created vcl epfd %u", rv); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2231 | return rv; |
| 2232 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2233 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2234 | vlsh = vls_epoll_create (); |
| 2235 | if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE)) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2236 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2237 | errno = -vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2238 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2239 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2240 | else |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2241 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2242 | rv = ldp_vlsh_to_fd (vlsh); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2243 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2244 | LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2245 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2249 | epoll_create (int size) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2250 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2251 | return epoll_create1 (0); |
| 2252 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2253 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2254 | int |
| 2255 | epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) |
| 2256 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2257 | vls_handle_t vep_vlsh, vlsh; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2258 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2259 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2260 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2261 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2262 | vep_vlsh = ldp_fd_to_vlsh (epfd); |
| 2263 | if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2264 | { |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 2265 | /* The LDP epoll_create1 always creates VCL epfd's. |
| 2266 | * The app should never have a kernel base epoll fd unless it |
| 2267 | * was acquired outside of the LD_PRELOAD process context. |
| 2268 | * In any case, if we get one, punt it to libc_epoll_ctl. |
| 2269 | */ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2270 | LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d" |
| 2271 | " event %p", epfd, op, fd, event); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2272 | |
| 2273 | rv = libc_epoll_ctl (epfd, op, fd, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2274 | goto done; |
| 2275 | } |
| 2276 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2277 | vlsh = ldp_fd_to_vlsh (fd); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2278 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2279 | LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd, |
| 2280 | vlsh, op); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2281 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2282 | if (vlsh != VLS_INVALID_HANDLE) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2283 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2284 | LDBG (1, "epfd %d: calling vls_epoll_ctl: ep_vlsh %d op %d, vlsh %u," |
Florin Coras | eb801d0 | 2020-09-16 17:44:58 -0700 | [diff] [blame] | 2285 | " event %p", epfd, vep_vlsh, op, vlsh, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2286 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2287 | rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2288 | if (rv != VPPCOM_OK) |
| 2289 | { |
| 2290 | errno = -rv; |
| 2291 | rv = -1; |
| 2292 | } |
| 2293 | } |
| 2294 | else |
| 2295 | { |
| 2296 | int libc_epfd; |
| 2297 | u32 size = sizeof (epfd); |
| 2298 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2299 | libc_epfd = vls_attr (vep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2300 | if (!libc_epfd) |
| 2301 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2302 | LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: " |
| 2303 | "EPOLL_CLOEXEC", epfd, vep_vlsh); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2304 | |
| 2305 | libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC); |
| 2306 | if (libc_epfd < 0) |
| 2307 | { |
| 2308 | rv = libc_epfd; |
| 2309 | goto done; |
| 2310 | } |
| 2311 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2312 | rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, |
| 2313 | &size); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2314 | if (rv < 0) |
| 2315 | { |
| 2316 | errno = -rv; |
| 2317 | rv = -1; |
| 2318 | goto done; |
| 2319 | } |
| 2320 | } |
| 2321 | else if (PREDICT_FALSE (libc_epfd < 0)) |
| 2322 | { |
| 2323 | errno = -epfd; |
| 2324 | rv = -1; |
| 2325 | goto done; |
| 2326 | } |
| 2327 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2328 | LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d," |
| 2329 | " event %p", epfd, libc_epfd, op, fd, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2330 | |
| 2331 | rv = libc_epoll_ctl (libc_epfd, op, fd, event); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | done: |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2335 | return rv; |
| 2336 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2337 | |
| 2338 | static inline int |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2339 | ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, |
| 2340 | int timeout, const sigset_t * sigmask) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2341 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2342 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2343 | double time_to_wait = (double) 0, max_time; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2344 | int libc_epfd, rv = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2345 | vls_handle_t ep_vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2346 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2347 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2348 | |
| 2349 | if (PREDICT_FALSE (!events || (timeout < -1))) |
| 2350 | { |
| 2351 | errno = EFAULT; |
| 2352 | return -1; |
| 2353 | } |
| 2354 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2355 | if (epfd == ldpw->vcl_mq_epfd) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2356 | return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask); |
| 2357 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2358 | ep_vlsh = ldp_fd_to_vlsh (epfd); |
| 2359 | if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2360 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2361 | LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2362 | errno = EBADFD; |
| 2363 | return -1; |
| 2364 | } |
| 2365 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2366 | if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) |
| 2367 | clib_time_init (&ldpw->clib_time); |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 2368 | time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0); |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2369 | max_time = clib_time_now (&ldpw->clib_time) + time_to_wait; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2370 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2371 | libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2372 | if (PREDICT_FALSE (libc_epfd < 0)) |
| 2373 | { |
| 2374 | errno = -libc_epfd; |
| 2375 | rv = -1; |
| 2376 | goto done; |
| 2377 | } |
| 2378 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2379 | LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, " |
| 2380 | "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh, |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2381 | libc_epfd, events, maxevents, timeout, sigmask, time_to_wait); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2382 | do |
| 2383 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2384 | if (!ldpw->epoll_wait_vcl) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2385 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2386 | rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2387 | if (rv > 0) |
| 2388 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2389 | ldpw->epoll_wait_vcl = 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2390 | goto done; |
| 2391 | } |
| 2392 | else if (rv < 0) |
| 2393 | { |
| 2394 | errno = -rv; |
| 2395 | rv = -1; |
| 2396 | goto done; |
| 2397 | } |
| 2398 | } |
| 2399 | else |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2400 | ldpw->epoll_wait_vcl = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2401 | |
| 2402 | if (libc_epfd > 0) |
| 2403 | { |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 2404 | rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2405 | if (rv != 0) |
| 2406 | goto done; |
| 2407 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2408 | } |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2409 | while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2410 | |
| 2411 | done: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2412 | return rv; |
| 2413 | } |
| 2414 | |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2415 | static inline int |
| 2416 | ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events, |
| 2417 | int maxevents, int timeout, const sigset_t * sigmask) |
| 2418 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2419 | ldp_worker_ctx_t *ldpw; |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2420 | int libc_epfd, rv = 0, num_ev; |
| 2421 | vls_handle_t ep_vlsh; |
| 2422 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2423 | ldp_init_check (); |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2424 | |
| 2425 | if (PREDICT_FALSE (!events || (timeout < -1))) |
| 2426 | { |
| 2427 | errno = EFAULT; |
| 2428 | return -1; |
| 2429 | } |
| 2430 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 2431 | /* Make sure the vcl worker is valid. Could be that epoll fd was created on |
| 2432 | * one thread but it is now used on another */ |
| 2433 | if (PREDICT_FALSE (vppcom_worker_index () == ~0)) |
| 2434 | vls_register_vcl_worker (); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2435 | |
| 2436 | ldpw = ldp_worker_get_current (); |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2437 | if (epfd == ldpw->vcl_mq_epfd) |
| 2438 | return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask); |
| 2439 | |
| 2440 | ep_vlsh = ldp_fd_to_vlsh (epfd); |
| 2441 | if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE)) |
| 2442 | { |
| 2443 | LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh); |
| 2444 | errno = EBADFD; |
| 2445 | return -1; |
| 2446 | } |
| 2447 | |
| 2448 | libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); |
| 2449 | if (PREDICT_FALSE (!libc_epfd)) |
| 2450 | { |
| 2451 | u32 size = sizeof (epfd); |
| 2452 | |
| 2453 | LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: " |
| 2454 | "EPOLL_CLOEXEC", epfd, ep_vlsh); |
| 2455 | libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC); |
| 2456 | if (libc_epfd < 0) |
| 2457 | { |
| 2458 | rv = libc_epfd; |
| 2459 | goto done; |
| 2460 | } |
| 2461 | |
| 2462 | rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size); |
| 2463 | if (rv < 0) |
| 2464 | { |
| 2465 | errno = -rv; |
| 2466 | rv = -1; |
| 2467 | goto done; |
| 2468 | } |
| 2469 | } |
| 2470 | if (PREDICT_FALSE (libc_epfd <= 0)) |
| 2471 | { |
| 2472 | errno = -libc_epfd; |
| 2473 | rv = -1; |
| 2474 | goto done; |
| 2475 | } |
| 2476 | |
| 2477 | if (PREDICT_FALSE (!ldpw->mq_epfd_added)) |
| 2478 | { |
| 2479 | struct epoll_event e = { 0 }; |
| 2480 | e.events = EPOLLIN; |
| 2481 | e.data.fd = ldpw->vcl_mq_epfd; |
| 2482 | if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) < |
| 2483 | 0) |
| 2484 | { |
| 2485 | LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d", |
| 2486 | epfd, ldpw->vcl_mq_epfd, libc_epfd); |
| 2487 | rv = -1; |
| 2488 | goto done; |
| 2489 | } |
| 2490 | ldpw->mq_epfd_added = 1; |
| 2491 | } |
| 2492 | |
wanghanlin | 8919fec | 2021-03-18 20:00:41 +0800 | [diff] [blame] | 2493 | /* Request to only drain unhandled to prevent libc_epoll_wait starved */ |
| 2494 | rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2); |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2495 | if (rv > 0) |
| 2496 | goto done; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2497 | else if (PREDICT_FALSE (rv < 0)) |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2498 | { |
| 2499 | errno = -rv; |
| 2500 | rv = -1; |
| 2501 | goto done; |
| 2502 | } |
| 2503 | |
| 2504 | rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask); |
| 2505 | if (rv <= 0) |
| 2506 | goto done; |
| 2507 | for (int i = 0; i < rv; i++) |
| 2508 | { |
| 2509 | if (events[i].data.fd == ldpw->vcl_mq_epfd) |
| 2510 | { |
| 2511 | /* We should remove mq epoll fd from events. */ |
| 2512 | rv--; |
| 2513 | if (i != rv) |
| 2514 | { |
| 2515 | events[i].events = events[rv].events; |
| 2516 | events[i].data.u64 = events[rv].data.u64; |
| 2517 | } |
| 2518 | num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0); |
| 2519 | if (PREDICT_TRUE (num_ev > 0)) |
| 2520 | rv += num_ev; |
| 2521 | break; |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | done: |
| 2526 | return rv; |
| 2527 | } |
| 2528 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2529 | int |
| 2530 | epoll_pwait (int epfd, struct epoll_event *events, |
| 2531 | int maxevents, int timeout, const sigset_t * sigmask) |
| 2532 | { |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2533 | if (vls_use_eventfd ()) |
| 2534 | return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, |
| 2535 | sigmask); |
| 2536 | else |
| 2537 | return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | int |
| 2541 | epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout) |
| 2542 | { |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2543 | if (vls_use_eventfd ()) |
| 2544 | return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL); |
| 2545 | else |
| 2546 | return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | int |
| 2550 | poll (struct pollfd *fds, nfds_t nfds, int timeout) |
| 2551 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2552 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2553 | int rv, i, n_revents = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2554 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2555 | vcl_poll_t *vp; |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2556 | double max_time; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2557 | |
Florin Coras | eb801d0 | 2020-09-16 17:44:58 -0700 | [diff] [blame] | 2558 | LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2559 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2560 | if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) |
| 2561 | clib_time_init (&ldpw->clib_time); |
| 2562 | |
| 2563 | max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0; |
| 2564 | max_time += clib_time_now (&ldpw->clib_time); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2565 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2566 | for (i = 0; i < nfds; i++) |
| 2567 | { |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2568 | if (fds[i].fd < 0) |
| 2569 | continue; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2570 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2571 | vlsh = ldp_fd_to_vlsh (fds[i].fd); |
| 2572 | if (vlsh != VLS_INVALID_HANDLE) |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2573 | { |
| 2574 | fds[i].fd = -fds[i].fd; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2575 | vec_add2 (ldpw->vcl_poll, vp, 1); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2576 | vp->fds_ndx = i; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2577 | vp->sh = vlsh_to_sh (vlsh); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2578 | vp->events = fds[i].events; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2579 | #ifdef __USE_XOPEN2K |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2580 | if (fds[i].events & POLLRDNORM) |
| 2581 | vp->events |= POLLIN; |
| 2582 | if (fds[i].events & POLLWRNORM) |
| 2583 | vp->events |= POLLOUT; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2584 | #endif |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2585 | vp->revents = fds[i].revents; |
| 2586 | } |
| 2587 | else |
| 2588 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2589 | vec_add1 (ldpw->libc_poll, fds[i]); |
| 2590 | vec_add1 (ldpw->libc_poll_idxs, i); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2591 | } |
| 2592 | } |
| 2593 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2594 | do |
| 2595 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2596 | if (vec_len (ldpw->vcl_poll)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2597 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2598 | rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2599 | if (rv < 0) |
| 2600 | { |
| 2601 | errno = -rv; |
| 2602 | rv = -1; |
| 2603 | goto done; |
| 2604 | } |
| 2605 | else |
| 2606 | n_revents += rv; |
| 2607 | } |
| 2608 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2609 | if (vec_len (ldpw->libc_poll)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2610 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2611 | rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2612 | if (rv < 0) |
| 2613 | goto done; |
| 2614 | else |
| 2615 | n_revents += rv; |
| 2616 | } |
| 2617 | |
| 2618 | if (n_revents) |
| 2619 | { |
| 2620 | rv = n_revents; |
| 2621 | goto done; |
| 2622 | } |
| 2623 | } |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2624 | while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2625 | rv = 0; |
| 2626 | |
| 2627 | done: |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2628 | vec_foreach (vp, ldpw->vcl_poll) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2629 | { |
| 2630 | fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd; |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2631 | fds[vp->fds_ndx].revents = vp->revents; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2632 | #ifdef __USE_XOPEN2K |
| 2633 | if ((fds[vp->fds_ndx].revents & POLLIN) && |
| 2634 | (fds[vp->fds_ndx].events & POLLRDNORM)) |
| 2635 | fds[vp->fds_ndx].revents |= POLLRDNORM; |
| 2636 | if ((fds[vp->fds_ndx].revents & POLLOUT) && |
| 2637 | (fds[vp->fds_ndx].events & POLLWRNORM)) |
| 2638 | fds[vp->fds_ndx].revents |= POLLWRNORM; |
| 2639 | #endif |
| 2640 | } |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2641 | vec_reset_length (ldpw->vcl_poll); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2642 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2643 | for (i = 0; i < vec_len (ldpw->libc_poll); i++) |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2644 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2645 | fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents; |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2646 | } |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2647 | vec_reset_length (ldpw->libc_poll_idxs); |
| 2648 | vec_reset_length (ldpw->libc_poll); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2649 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2650 | return rv; |
| 2651 | } |
| 2652 | |
| 2653 | #ifdef USE_GNU |
| 2654 | int |
| 2655 | ppoll (struct pollfd *fds, nfds_t nfds, |
| 2656 | const struct timespec *timeout, const sigset_t * sigmask) |
| 2657 | { |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2658 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2659 | |
| 2660 | clib_warning ("LDP<%d>: LDP-TBD", getpid ()); |
| 2661 | errno = ENOSYS; |
| 2662 | |
| 2663 | |
| 2664 | return -1; |
| 2665 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2666 | #endif |
| 2667 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2668 | void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2669 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2670 | void DESTRUCTOR_ATTRIBUTE ldp_destructor (void); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2671 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2672 | /* |
| 2673 | * This function is called when the library is loaded |
| 2674 | */ |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2675 | void |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2676 | ldp_constructor (void) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2677 | { |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2678 | swrap_constructor (); |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2679 | if (ldp_init () != 0) |
Florin Coras | d89411e | 2019-03-19 19:44:51 -0700 | [diff] [blame] | 2680 | { |
| 2681 | fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n", |
| 2682 | getpid ()); |
| 2683 | _exit (1); |
| 2684 | } |
Dave Wallace | 69d0119 | 2018-02-22 16:22:09 -0500 | [diff] [blame] | 2685 | else if (LDP_DEBUG > 0) |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2686 | clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ()); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | /* |
| 2690 | * This function is called when the library is unloaded |
| 2691 | */ |
| 2692 | void |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2693 | ldp_destructor (void) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2694 | { |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 2695 | /* |
| 2696 | swrap_destructor (); |
| 2697 | if (ldp->init) |
| 2698 | ldp->init = 0; |
| 2699 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2700 | |
| 2701 | /* Don't use clib_warning() here because that calls writev() |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2702 | * which will call ldp_init(). |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2703 | */ |
Dave Wallace | 69d0119 | 2018-02-22 16:22:09 -0500 | [diff] [blame] | 2704 | if (LDP_DEBUG > 0) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 2705 | fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n", |
| 2706 | __func__, __LINE__, getpid ()); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | |
| 2710 | /* |
| 2711 | * fd.io coding-style-patch-verification: ON |
| 2712 | * |
| 2713 | * Local Variables: |
| 2714 | * eval: (c-set-style "gnu") |
| 2715 | * End: |
| 2716 | */ |