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> |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 24 | #include <linux/udp.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 25 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 26 | #include <vcl/ldp_socket_wrapper.h> |
| 27 | #include <vcl/ldp.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 28 | #include <sys/time.h> |
| 29 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 30 | #include <vcl/vcl_locked.h> |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 31 | #include <vppinfra/time.h> |
| 32 | #include <vppinfra/bitmap.h> |
Florin Coras | 30e273b | 2018-11-27 00:04:59 -0800 | [diff] [blame] | 33 | #include <vppinfra/lock.h> |
| 34 | #include <vppinfra/pool.h> |
| 35 | #include <vppinfra/hash.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 36 | |
| 37 | #define HAVE_CONSTRUCTOR_ATTRIBUTE |
| 38 | #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE |
| 39 | #define CONSTRUCTOR_ATTRIBUTE \ |
| 40 | __attribute__ ((constructor)) |
| 41 | #else |
| 42 | #define CONSTRUCTOR_ATTRIBUTE |
| 43 | #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */ |
| 44 | |
| 45 | #define HAVE_DESTRUCTOR_ATTRIBUTE |
| 46 | #ifdef HAVE_DESTRUCTOR_ATTRIBUTE |
| 47 | #define DESTRUCTOR_ATTRIBUTE \ |
| 48 | __attribute__ ((destructor)) |
| 49 | #else |
| 50 | #define DESTRUCTOR_ATTRIBUTE |
| 51 | #endif |
| 52 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 53 | #define LDP_MAX_NWORKERS 32 |
| 54 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 55 | typedef struct ldp_worker_ctx_ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 56 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 57 | u8 *io_buffer; |
| 58 | clib_time_t clib_time; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Select state |
| 62 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 63 | clib_bitmap_t *rd_bitmap; |
| 64 | clib_bitmap_t *wr_bitmap; |
| 65 | clib_bitmap_t *ex_bitmap; |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 66 | clib_bitmap_t *si_rd_bitmap; |
| 67 | clib_bitmap_t *si_wr_bitmap; |
| 68 | clib_bitmap_t *si_ex_bitmap; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 69 | clib_bitmap_t *libc_rd_bitmap; |
| 70 | clib_bitmap_t *libc_wr_bitmap; |
| 71 | clib_bitmap_t *libc_ex_bitmap; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Poll state |
| 75 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 76 | vcl_poll_t *vcl_poll; |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 77 | struct pollfd *libc_poll; |
| 78 | u16 *libc_poll_idxs; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Epoll state |
| 82 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 83 | u8 epoll_wait_vcl; |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 84 | u8 mq_epfd_added; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 85 | int vcl_mq_epfd; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 86 | |
| 87 | } ldp_worker_ctx_t; |
| 88 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 89 | /* clib_bitmap_t, fd_mask and vcl_si_set are used interchangeably. Make sure |
| 90 | * they are the same size */ |
| 91 | STATIC_ASSERT (sizeof (clib_bitmap_t) == sizeof (fd_mask), |
| 92 | "ldp bitmap size mismatch"); |
| 93 | STATIC_ASSERT (sizeof (vcl_si_set) == sizeof (fd_mask), |
| 94 | "ldp bitmap size mismatch"); |
| 95 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 96 | typedef struct |
| 97 | { |
| 98 | ldp_worker_ctx_t *workers; |
| 99 | int init; |
| 100 | char app_name[LDP_APP_NAME_MAX]; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 101 | u32 vlsh_bit_val; |
| 102 | u32 vlsh_bit_mask; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 103 | u32 debug; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 104 | |
| 105 | /** vcl needs next epoll_create to go to libc_epoll */ |
| 106 | u8 vcl_needs_real_epoll; |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * crypto state used only for testing |
| 110 | */ |
| 111 | u8 transparent_tls; |
| 112 | u32 ckpair_index; |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 113 | } ldp_main_t; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 114 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 115 | #define LDP_DEBUG ldp->debug |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 116 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 117 | #define LDBG(_lvl, _fmt, _args...) \ |
| 118 | if (ldp->debug > _lvl) \ |
hanlin | 9f3f18f | 2019-12-30 16:25:20 +0800 | [diff] [blame] | 119 | { \ |
| 120 | int errno_saved = errno; \ |
Florin Coras | 585c86a | 2020-10-16 17:57:36 -0700 | [diff] [blame] | 121 | fprintf (stderr, "ldp<%d>: " _fmt "\n", getpid(), ##_args); \ |
hanlin | 9f3f18f | 2019-12-30 16:25:20 +0800 | [diff] [blame] | 122 | errno = errno_saved; \ |
| 123 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 124 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 125 | static ldp_main_t ldp_main = { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 126 | .vlsh_bit_val = (1 << LDP_SID_BIT_MIN), |
| 127 | .vlsh_bit_mask = (1 << LDP_SID_BIT_MIN) - 1, |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 128 | .debug = LDP_DEBUG_INIT, |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 129 | .transparent_tls = 0, |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 130 | .ckpair_index = ~0, |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 131 | }; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 132 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 133 | static ldp_main_t *ldp = &ldp_main; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 134 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 135 | static inline ldp_worker_ctx_t * |
| 136 | ldp_worker_get_current (void) |
| 137 | { |
| 138 | return (ldp->workers + vppcom_worker_index ()); |
| 139 | } |
| 140 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 141 | /* |
| 142 | * RETURN: 0 on success or -1 on error. |
| 143 | * */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 144 | static inline void |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 145 | ldp_set_app_name (char *app_name) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 146 | { |
Benoît Ganne | 747b3d8 | 2019-08-21 18:27:23 +0200 | [diff] [blame] | 147 | snprintf (ldp->app_name, LDP_APP_NAME_MAX, |
| 148 | "ldp-%d-%s", getpid (), app_name); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 151 | static inline char * |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 152 | ldp_get_app_name () |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 153 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 154 | if (ldp->app_name[0] == '\0') |
| 155 | ldp_set_app_name ("app"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 156 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 157 | return ldp->app_name; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 160 | static inline int |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 161 | ldp_vlsh_to_fd (vls_handle_t vlsh) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 162 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 163 | return (vlsh + ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 164 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 165 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 166 | static inline vls_handle_t |
| 167 | ldp_fd_to_vlsh (int fd) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 168 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 169 | if (fd < ldp->vlsh_bit_val) |
| 170 | return VLS_INVALID_HANDLE; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 171 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 172 | return (fd - ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Florin Coras | 2d9b427 | 2019-03-11 10:14:37 -0700 | [diff] [blame] | 175 | static void |
| 176 | ldp_alloc_workers (void) |
| 177 | { |
| 178 | if (ldp->workers) |
| 179 | return; |
| 180 | pool_alloc (ldp->workers, LDP_MAX_NWORKERS); |
| 181 | } |
| 182 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 183 | static int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 184 | ldp_init (void) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 185 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 186 | ldp_worker_ctx_t *ldpw; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 187 | int rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 188 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 189 | ASSERT (!ldp->init); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 190 | |
| 191 | ldp->init = 1; |
| 192 | ldp->vcl_needs_real_epoll = 1; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 193 | rv = vls_app_create (ldp_get_app_name ()); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 194 | if (rv != VPPCOM_OK) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 195 | { |
Florin Coras | 955bfbb | 2018-12-04 13:43:45 -0800 | [diff] [blame] | 196 | ldp->vcl_needs_real_epoll = 0; |
| 197 | if (rv == VPPCOM_EEXIST) |
| 198 | return 0; |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 199 | LDBG (2, "\nERROR: ldp_init: vppcom_app_create()" |
| 200 | " failed! rv = %d (%s)\n", rv, vppcom_retval_str (rv)); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 201 | ldp->init = 0; |
| 202 | return rv; |
| 203 | } |
| 204 | ldp->vcl_needs_real_epoll = 0; |
Florin Coras | 2d9b427 | 2019-03-11 10:14:37 -0700 | [diff] [blame] | 205 | ldp_alloc_workers (); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 206 | ldpw = ldp_worker_get_current (); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 207 | |
| 208 | char *env_var_str = getenv (LDP_ENV_DEBUG); |
| 209 | if (env_var_str) |
| 210 | { |
| 211 | u32 tmp; |
| 212 | if (sscanf (env_var_str, "%u", &tmp) != 1) |
| 213 | clib_warning ("LDP<%d>: WARNING: Invalid LDP debug level specified in" |
| 214 | " the env var " LDP_ENV_DEBUG " (%s)!", getpid (), |
| 215 | env_var_str); |
| 216 | else |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 217 | { |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 218 | ldp->debug = tmp; |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 219 | LDBG (0, "configured LDP debug level (%u) from env var " |
| 220 | LDP_ENV_DEBUG "!", ldp->debug); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 223 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 224 | env_var_str = getenv (LDP_ENV_APP_NAME); |
| 225 | if (env_var_str) |
| 226 | { |
| 227 | ldp_set_app_name (env_var_str); |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 228 | LDBG (0, "configured LDP app name (%s) from the env var " |
| 229 | LDP_ENV_APP_NAME "!", ldp->app_name); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 230 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 231 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 232 | env_var_str = getenv (LDP_ENV_SID_BIT); |
| 233 | if (env_var_str) |
| 234 | { |
| 235 | u32 sb; |
| 236 | if (sscanf (env_var_str, "%u", &sb) != 1) |
| 237 | { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 238 | LDBG (0, "WARNING: Invalid LDP sid bit specified in the env var " |
| 239 | LDP_ENV_SID_BIT " (%s)! sid bit value %d (0x%x)", env_var_str, |
| 240 | ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 241 | } |
| 242 | else if (sb < LDP_SID_BIT_MIN) |
| 243 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 244 | ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MIN); |
| 245 | ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 246 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 247 | LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var " |
| 248 | LDP_ENV_SID_BIT " (%s) is too small. Using LDP_SID_BIT_MIN" |
| 249 | " (%d)! sid bit value %d (0x%x)", sb, env_var_str, |
| 250 | LDP_SID_BIT_MIN, ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 251 | } |
| 252 | else if (sb > LDP_SID_BIT_MAX) |
| 253 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 254 | ldp->vlsh_bit_val = (1 << LDP_SID_BIT_MAX); |
| 255 | ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 256 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 257 | LDBG (0, "WARNING: LDP sid bit (%u) specified in the env var " |
| 258 | LDP_ENV_SID_BIT " (%s) is too big. Using LDP_SID_BIT_MAX" |
| 259 | " (%d)! sid bit value %d (0x%x)", sb, env_var_str, |
| 260 | LDP_SID_BIT_MAX, ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 261 | } |
| 262 | else |
| 263 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 264 | ldp->vlsh_bit_val = (1 << sb); |
| 265 | ldp->vlsh_bit_mask = ldp->vlsh_bit_val - 1; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 266 | |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 267 | LDBG (0, "configured LDP sid bit (%u) from " |
| 268 | LDP_ENV_SID_BIT "! sid bit value %d (0x%x)", sb, |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 269 | ldp->vlsh_bit_val, ldp->vlsh_bit_val); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 270 | } |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 271 | |
| 272 | /* Make sure there are enough bits in the fd set for vcl sessions */ |
| 273 | if (ldp->vlsh_bit_val > FD_SETSIZE / 2) |
| 274 | { |
wanghanlin | 1eb8fea | 2021-10-14 11:10:26 +0800 | [diff] [blame] | 275 | /* Only valid for select/pselect, so just WARNING and not exit */ |
| 276 | LDBG (0, |
| 277 | "WARNING: LDP vlsh bit value %d > FD_SETSIZE/2 %d, " |
| 278 | "select/pselect not supported now!", |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 279 | ldp->vlsh_bit_val, FD_SETSIZE / 2); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 280 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 281 | } |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 282 | env_var_str = getenv (LDP_ENV_TLS_TRANS); |
| 283 | if (env_var_str) |
| 284 | { |
| 285 | ldp->transparent_tls = 1; |
| 286 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 287 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 288 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 289 | pool_foreach (ldpw, ldp->workers) { |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 290 | clib_memset (&ldpw->clib_time, 0, sizeof (ldpw->clib_time)); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 291 | } |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 292 | /* *INDENT-ON* */ |
| 293 | |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 294 | LDBG (0, "LDP initialization: done!"); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 295 | |
| 296 | return 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 299 | #define ldp_init_check() \ |
| 300 | if (PREDICT_FALSE (!ldp->init)) \ |
| 301 | { \ |
| 302 | if ((errno = -ldp_init ())) \ |
| 303 | return -1; \ |
| 304 | } |
| 305 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 306 | int |
| 307 | close (int fd) |
| 308 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 309 | vls_handle_t vlsh; |
| 310 | int rv, epfd; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 311 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 312 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 313 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 314 | vlsh = ldp_fd_to_vlsh (fd); |
| 315 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 316 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 317 | epfd = vls_attr (vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 318 | if (epfd > 0) |
| 319 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 320 | LDBG (0, "fd %d: calling libc_close: epfd %u", fd, epfd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 321 | |
| 322 | rv = libc_close (epfd); |
| 323 | if (rv < 0) |
| 324 | { |
| 325 | u32 size = sizeof (epfd); |
| 326 | epfd = 0; |
| 327 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 328 | (void) vls_attr (vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &epfd, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | else if (PREDICT_FALSE (epfd < 0)) |
| 332 | { |
| 333 | errno = -epfd; |
| 334 | rv = -1; |
| 335 | goto done; |
| 336 | } |
| 337 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 338 | LDBG (0, "fd %d: calling vls_close: vlsh %u", fd, vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 339 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 340 | rv = vls_close (vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 341 | if (rv != VPPCOM_OK) |
| 342 | { |
| 343 | errno = -rv; |
| 344 | rv = -1; |
| 345 | } |
| 346 | } |
| 347 | else |
| 348 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 349 | LDBG (0, "fd %d: calling libc_close", fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 350 | rv = libc_close (fd); |
| 351 | } |
| 352 | |
| 353 | done: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 354 | return rv; |
| 355 | } |
| 356 | |
| 357 | ssize_t |
| 358 | read (int fd, void *buf, size_t nbytes) |
| 359 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 360 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 361 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 362 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 363 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 364 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 365 | vlsh = ldp_fd_to_vlsh (fd); |
| 366 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 367 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 368 | size = vls_read (vlsh, buf, nbytes); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 369 | if (size < 0) |
| 370 | { |
| 371 | errno = -size; |
| 372 | size = -1; |
| 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 377 | size = libc_read (fd, buf, nbytes); |
| 378 | } |
| 379 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 380 | return size; |
| 381 | } |
| 382 | |
| 383 | ssize_t |
| 384 | readv (int fd, const struct iovec * iov, int iovcnt) |
| 385 | { |
Dave Wallace | 8aaba56 | 2018-01-18 17:21:19 -0500 | [diff] [blame] | 386 | int rv = 0, i, total = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 387 | vls_handle_t vlsh; |
| 388 | ssize_t size = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 389 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 390 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 391 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 392 | vlsh = ldp_fd_to_vlsh (fd); |
| 393 | if (vlsh != VLS_INVALID_HANDLE) |
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 | for (i = 0; i < iovcnt; ++i) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 396 | { |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 397 | rv = vls_read (vlsh, iov[i].iov_base, iov[i].iov_len); |
| 398 | if (rv <= 0) |
| 399 | break; |
| 400 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 401 | { |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 402 | total += rv; |
| 403 | if (rv < iov[i].iov_len) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 404 | break; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
Florin Coras | 067f954 | 2020-02-14 05:33:46 +0000 | [diff] [blame] | 407 | if (rv < 0 && total == 0) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 408 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 409 | errno = -rv; |
| 410 | size = -1; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 411 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 412 | else |
| 413 | size = total; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 414 | } |
| 415 | else |
| 416 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 417 | size = libc_readv (fd, iov, iovcnt); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 420 | return size; |
| 421 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 422 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 423 | ssize_t |
| 424 | write (int fd, const void *buf, size_t nbytes) |
| 425 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 426 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 427 | ssize_t size = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 428 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 429 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 430 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 431 | vlsh = ldp_fd_to_vlsh (fd); |
| 432 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 433 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 434 | size = vls_write_msg (vlsh, (void *) buf, nbytes); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 435 | if (size < 0) |
| 436 | { |
| 437 | errno = -size; |
| 438 | size = -1; |
| 439 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 440 | } |
| 441 | else |
| 442 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 443 | size = libc_write (fd, buf, nbytes); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 446 | return size; |
| 447 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 448 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 449 | ssize_t |
| 450 | writev (int fd, const struct iovec * iov, int iovcnt) |
| 451 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 452 | ssize_t size = 0, total = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 453 | vls_handle_t vlsh; |
Dave Wallace | 8aaba56 | 2018-01-18 17:21:19 -0500 | [diff] [blame] | 454 | int i, rv = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 455 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 456 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 457 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 458 | vlsh = ldp_fd_to_vlsh (fd); |
| 459 | if (vlsh != VLS_INVALID_HANDLE) |
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 | for (i = 0; i < iovcnt; ++i) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 462 | { |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 463 | rv = vls_write_msg (vlsh, iov[i].iov_base, iov[i].iov_len); |
| 464 | if (rv < 0) |
| 465 | break; |
| 466 | else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 467 | { |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 468 | total += rv; |
| 469 | if (rv < iov[i].iov_len) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 470 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 471 | } |
| 472 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 473 | |
Florin Coras | eda1b8c | 2020-03-23 16:00:35 +0000 | [diff] [blame] | 474 | if (rv < 0 && total == 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 475 | { |
| 476 | errno = -rv; |
| 477 | size = -1; |
| 478 | } |
| 479 | else |
| 480 | size = total; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 481 | } |
| 482 | else |
| 483 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 484 | size = libc_writev (fd, iov, iovcnt); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 487 | return size; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 490 | static int |
| 491 | fcntl_internal (int fd, int cmd, va_list ap) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 492 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 493 | vls_handle_t vlsh; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 494 | int rv = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 495 | |
| 496 | vlsh = ldp_fd_to_vlsh (fd); |
| 497 | LDBG (0, "fd %u vlsh %d, cmd %u", fd, vlsh, cmd); |
| 498 | if (vlsh != VLS_INVALID_HANDLE) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 499 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 500 | int flags = va_arg (ap, int); |
| 501 | u32 size; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 502 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 503 | size = sizeof (flags); |
| 504 | rv = -EOPNOTSUPP; |
| 505 | switch (cmd) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 506 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 507 | case F_SETFL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 508 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 509 | break; |
| 510 | |
| 511 | case F_GETFL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 512 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_FLAGS, &flags, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 513 | if (rv == VPPCOM_OK) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 514 | rv = flags; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 515 | break; |
Florin Coras | 173bae3 | 2018-11-16 18:56:28 -0800 | [diff] [blame] | 516 | case F_SETFD: |
| 517 | /* TODO handle this */ |
| 518 | LDBG (0, "F_SETFD ignored flags %u", flags); |
| 519 | rv = 0; |
| 520 | break; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 521 | default: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 522 | rv = -EOPNOTSUPP; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 523 | break; |
| 524 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 525 | if (rv < 0) |
| 526 | { |
| 527 | errno = -rv; |
| 528 | rv = -1; |
| 529 | } |
| 530 | } |
| 531 | else |
| 532 | { |
Carl Smith | e16707b | 2019-11-13 14:37:39 +1300 | [diff] [blame] | 533 | #ifdef HAVE_FCNTL64 |
| 534 | rv = libc_vfcntl64 (fd, cmd, ap); |
| 535 | #else |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 536 | rv = libc_vfcntl (fd, cmd, ap); |
Carl Smith | e16707b | 2019-11-13 14:37:39 +1300 | [diff] [blame] | 537 | #endif |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 540 | return rv; |
| 541 | } |
| 542 | |
| 543 | int |
| 544 | fcntl (int fd, int cmd, ...) |
| 545 | { |
| 546 | va_list ap; |
| 547 | int rv; |
| 548 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 549 | ldp_init_check (); |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 550 | |
| 551 | va_start (ap, cmd); |
| 552 | rv = fcntl_internal (fd, cmd, ap); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 553 | va_end (ap); |
| 554 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 555 | return rv; |
| 556 | } |
| 557 | |
| 558 | int |
Florin Coras | d7586d5 | 2020-04-29 02:19:51 +0000 | [diff] [blame] | 559 | fcntl64 (int fd, int cmd, ...) |
| 560 | { |
| 561 | va_list ap; |
| 562 | int rv; |
| 563 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 564 | ldp_init_check (); |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 565 | |
Florin Coras | d7586d5 | 2020-04-29 02:19:51 +0000 | [diff] [blame] | 566 | va_start (ap, cmd); |
Florin Coras | 0ab36f5 | 2020-05-26 19:45:45 +0000 | [diff] [blame] | 567 | rv = fcntl_internal (fd, cmd, ap); |
Florin Coras | d7586d5 | 2020-04-29 02:19:51 +0000 | [diff] [blame] | 568 | va_end (ap); |
| 569 | return rv; |
| 570 | } |
| 571 | |
| 572 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 573 | ioctl (int fd, unsigned long int cmd, ...) |
| 574 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 575 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 576 | va_list ap; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 577 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 578 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 579 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 580 | |
| 581 | va_start (ap, cmd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 582 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 583 | vlsh = ldp_fd_to_vlsh (fd); |
| 584 | if (vlsh != VLS_INVALID_HANDLE) |
| 585 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 586 | switch (cmd) |
| 587 | { |
| 588 | case FIONREAD: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 589 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 590 | break; |
| 591 | |
| 592 | case FIONBIO: |
| 593 | { |
| 594 | u32 flags = va_arg (ap, int) ? O_NONBLOCK : 0; |
| 595 | u32 size = sizeof (flags); |
| 596 | |
| 597 | /* TBD: When VPPCOM_ATTR_[GS]ET_FLAGS supports flags other than |
| 598 | * non-blocking, the flags should be read here and merged |
| 599 | * with O_NONBLOCK. |
| 600 | */ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 601 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_FLAGS, &flags, &size); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 602 | } |
| 603 | break; |
| 604 | |
| 605 | default: |
| 606 | rv = -EOPNOTSUPP; |
| 607 | break; |
| 608 | } |
| 609 | if (rv < 0) |
| 610 | { |
| 611 | errno = -rv; |
| 612 | rv = -1; |
| 613 | } |
| 614 | } |
| 615 | else |
| 616 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 617 | rv = libc_vioctl (fd, cmd, ap); |
| 618 | } |
| 619 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 620 | va_end (ap); |
| 621 | return rv; |
| 622 | } |
| 623 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 624 | always_inline void |
| 625 | ldp_select_init_maps (fd_set * __restrict original, |
| 626 | clib_bitmap_t ** resultb, clib_bitmap_t ** libcb, |
| 627 | clib_bitmap_t ** vclb, int nfds, u32 minbits, |
| 628 | u32 n_bytes, uword * si_bits, uword * libc_bits) |
| 629 | { |
| 630 | uword si_bits_set, libc_bits_set; |
| 631 | vls_handle_t vlsh; |
| 632 | int fd; |
| 633 | |
| 634 | clib_bitmap_validate (*vclb, minbits); |
| 635 | clib_bitmap_validate (*libcb, minbits); |
| 636 | clib_bitmap_validate (*resultb, minbits); |
| 637 | clib_memcpy_fast (*resultb, original, n_bytes); |
| 638 | memset (original, 0, n_bytes); |
| 639 | |
| 640 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 641 | clib_bitmap_foreach (fd, *resultb) { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 642 | if (fd > nfds) |
| 643 | break; |
| 644 | vlsh = ldp_fd_to_vlsh (fd); |
| 645 | if (vlsh == VLS_INVALID_HANDLE) |
| 646 | clib_bitmap_set_no_check (*libcb, fd, 1); |
| 647 | else |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 648 | *vclb = clib_bitmap_set (*vclb, vlsh_to_session_index (vlsh), 1); |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 649 | } |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 650 | /* *INDENT-ON* */ |
| 651 | |
| 652 | si_bits_set = clib_bitmap_last_set (*vclb) + 1; |
| 653 | *si_bits = (si_bits_set > *si_bits) ? si_bits_set : *si_bits; |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 654 | clib_bitmap_validate (*resultb, *si_bits); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 655 | |
| 656 | libc_bits_set = clib_bitmap_last_set (*libcb) + 1; |
| 657 | *libc_bits = (libc_bits_set > *libc_bits) ? libc_bits_set : *libc_bits; |
| 658 | } |
| 659 | |
| 660 | always_inline int |
| 661 | ldp_select_vcl_map_to_libc (clib_bitmap_t * vclb, fd_set * __restrict libcb) |
| 662 | { |
| 663 | vls_handle_t vlsh; |
| 664 | uword si; |
| 665 | int fd; |
| 666 | |
| 667 | if (!libcb) |
| 668 | return 0; |
| 669 | |
| 670 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 671 | clib_bitmap_foreach (si, vclb) { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 672 | vlsh = vls_session_index_to_vlsh (si); |
Florin Coras | 5414062 | 2020-02-04 19:04:34 +0000 | [diff] [blame] | 673 | ASSERT (vlsh != VLS_INVALID_HANDLE); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 674 | fd = ldp_vlsh_to_fd (vlsh); |
| 675 | if (PREDICT_FALSE (fd < 0)) |
| 676 | { |
| 677 | errno = EBADFD; |
| 678 | return -1; |
| 679 | } |
| 680 | FD_SET (fd, libcb); |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 681 | } |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 682 | /* *INDENT-ON* */ |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | always_inline void |
| 688 | ldp_select_libc_map_merge (clib_bitmap_t * result, fd_set * __restrict libcb) |
| 689 | { |
| 690 | uword fd; |
| 691 | |
Florin Coras | 78b5fa6 | 2019-02-21 20:04:15 -0800 | [diff] [blame] | 692 | if (!libcb) |
| 693 | return; |
| 694 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 695 | /* *INDENT-OFF* */ |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 696 | clib_bitmap_foreach (fd, result) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 697 | FD_SET ((int)fd, libcb); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 698 | /* *INDENT-ON* */ |
| 699 | } |
| 700 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 701 | int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 702 | ldp_pselect (int nfds, fd_set * __restrict readfds, |
| 703 | fd_set * __restrict writefds, |
| 704 | fd_set * __restrict exceptfds, |
| 705 | const struct timespec *__restrict timeout, |
| 706 | const __sigset_t * __restrict sigmask) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 707 | { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 708 | u32 minbits = clib_max (nfds, BITS (uword)), n_bytes; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 709 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 710 | struct timespec libc_tspec = { 0 }; |
| 711 | f64 time_out, vcl_timeout = 0; |
| 712 | uword si_bits, libc_bits; |
| 713 | int rv, bits_set = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 714 | |
| 715 | if (nfds < 0) |
| 716 | { |
| 717 | errno = EINVAL; |
| 718 | return -1; |
| 719 | } |
| 720 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 721 | if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) |
| 722 | clib_time_init (&ldpw->clib_time); |
| 723 | |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 724 | if (timeout) |
| 725 | { |
| 726 | time_out = (timeout->tv_sec == 0 && timeout->tv_nsec == 0) ? |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 727 | (f64) 0 : (f64) timeout->tv_sec + (f64) timeout->tv_nsec / (f64) 1e9; |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 728 | |
liuyacan | f71796e | 2021-08-02 10:01:39 +0800 | [diff] [blame] | 729 | time_out += clib_time_now (&ldpw->clib_time); |
| 730 | |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 731 | /* select as fine grained sleep */ |
| 732 | if (!nfds) |
| 733 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 734 | while (clib_time_now (&ldpw->clib_time) < time_out) |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 735 | ; |
| 736 | return 0; |
| 737 | } |
| 738 | } |
| 739 | else if (!nfds) |
| 740 | { |
| 741 | errno = EINVAL; |
| 742 | return -1; |
| 743 | } |
| 744 | else |
| 745 | time_out = -1; |
| 746 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 747 | if (nfds <= ldp->vlsh_bit_val) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 748 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 749 | rv = libc_pselect (nfds, readfds, writefds, exceptfds, |
| 750 | timeout, sigmask); |
| 751 | goto done; |
| 752 | } |
| 753 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 754 | si_bits = libc_bits = 0; |
| 755 | n_bytes = nfds / 8 + ((nfds % 8) ? 1 : 0); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 756 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 757 | if (readfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 758 | ldp_select_init_maps (readfds, &ldpw->rd_bitmap, &ldpw->libc_rd_bitmap, |
| 759 | &ldpw->si_rd_bitmap, nfds, minbits, n_bytes, |
| 760 | &si_bits, &libc_bits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 761 | if (writefds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 762 | ldp_select_init_maps (writefds, &ldpw->wr_bitmap, |
| 763 | &ldpw->libc_wr_bitmap, &ldpw->si_wr_bitmap, nfds, |
| 764 | minbits, n_bytes, &si_bits, &libc_bits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 765 | if (exceptfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 766 | ldp_select_init_maps (exceptfds, &ldpw->ex_bitmap, |
| 767 | &ldpw->libc_ex_bitmap, &ldpw->si_ex_bitmap, nfds, |
| 768 | minbits, n_bytes, &si_bits, &libc_bits); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 769 | |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 770 | if (PREDICT_FALSE (!si_bits && !libc_bits)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 771 | { |
| 772 | errno = EINVAL; |
| 773 | rv = -1; |
| 774 | goto done; |
| 775 | } |
| 776 | |
Florin Coras | 78b5fa6 | 2019-02-21 20:04:15 -0800 | [diff] [blame] | 777 | if (!si_bits) |
| 778 | libc_tspec = timeout ? *timeout : libc_tspec; |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 779 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 780 | do |
| 781 | { |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 782 | if (si_bits) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 783 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 784 | if (readfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 785 | clib_memcpy_fast (ldpw->rd_bitmap, ldpw->si_rd_bitmap, |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 786 | vec_len (ldpw->si_rd_bitmap) * |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 787 | sizeof (clib_bitmap_t)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 788 | if (writefds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 789 | clib_memcpy_fast (ldpw->wr_bitmap, ldpw->si_wr_bitmap, |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 790 | vec_len (ldpw->si_wr_bitmap) * |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 791 | sizeof (clib_bitmap_t)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 792 | if (exceptfds) |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 793 | clib_memcpy_fast (ldpw->ex_bitmap, ldpw->si_ex_bitmap, |
Florin Coras | cbce80a | 2020-04-20 01:32:38 +0000 | [diff] [blame] | 794 | vec_len (ldpw->si_ex_bitmap) * |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 795 | sizeof (clib_bitmap_t)); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 796 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 797 | rv = vls_select (si_bits, readfds ? ldpw->rd_bitmap : NULL, |
| 798 | writefds ? ldpw->wr_bitmap : NULL, |
| 799 | exceptfds ? ldpw->ex_bitmap : NULL, vcl_timeout); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 800 | if (rv < 0) |
| 801 | { |
| 802 | errno = -rv; |
| 803 | rv = -1; |
Florin Coras | 5e6222a | 2020-04-24 17:09:25 +0000 | [diff] [blame] | 804 | goto done; |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 805 | } |
| 806 | else if (rv > 0) |
| 807 | { |
| 808 | if (ldp_select_vcl_map_to_libc (ldpw->rd_bitmap, readfds)) |
| 809 | { |
| 810 | rv = -1; |
| 811 | goto done; |
| 812 | } |
| 813 | |
| 814 | if (ldp_select_vcl_map_to_libc (ldpw->wr_bitmap, writefds)) |
| 815 | { |
| 816 | rv = -1; |
| 817 | goto done; |
| 818 | } |
| 819 | |
| 820 | if (ldp_select_vcl_map_to_libc (ldpw->ex_bitmap, exceptfds)) |
| 821 | { |
| 822 | rv = -1; |
| 823 | goto done; |
| 824 | } |
| 825 | bits_set = rv; |
| 826 | } |
| 827 | } |
| 828 | if (libc_bits) |
| 829 | { |
| 830 | if (readfds) |
| 831 | clib_memcpy_fast (ldpw->rd_bitmap, ldpw->libc_rd_bitmap, |
| 832 | vec_len (ldpw->libc_rd_bitmap) * |
| 833 | sizeof (clib_bitmap_t)); |
| 834 | if (writefds) |
| 835 | clib_memcpy_fast (ldpw->wr_bitmap, ldpw->libc_wr_bitmap, |
| 836 | vec_len (ldpw->libc_wr_bitmap) * |
| 837 | sizeof (clib_bitmap_t)); |
| 838 | if (exceptfds) |
| 839 | clib_memcpy_fast (ldpw->ex_bitmap, ldpw->libc_ex_bitmap, |
| 840 | vec_len (ldpw->libc_ex_bitmap) * |
| 841 | sizeof (clib_bitmap_t)); |
| 842 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 843 | rv = libc_pselect (libc_bits, |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 844 | readfds ? (fd_set *) ldpw->rd_bitmap : NULL, |
| 845 | writefds ? (fd_set *) ldpw->wr_bitmap : NULL, |
| 846 | exceptfds ? (fd_set *) ldpw->ex_bitmap : NULL, |
| 847 | &libc_tspec, sigmask); |
| 848 | if (rv > 0) |
| 849 | { |
| 850 | ldp_select_libc_map_merge (ldpw->rd_bitmap, readfds); |
| 851 | ldp_select_libc_map_merge (ldpw->wr_bitmap, writefds); |
| 852 | ldp_select_libc_map_merge (ldpw->ex_bitmap, exceptfds); |
| 853 | bits_set += rv; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | if (bits_set) |
| 858 | { |
| 859 | rv = bits_set; |
| 860 | goto done; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 861 | } |
| 862 | } |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 863 | while ((time_out == -1) || (clib_time_now (&ldpw->clib_time) < time_out)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 864 | rv = 0; |
| 865 | |
| 866 | done: |
| 867 | /* TBD: set timeout to amount of time left */ |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 868 | clib_bitmap_zero (ldpw->rd_bitmap); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 869 | clib_bitmap_zero (ldpw->si_rd_bitmap); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 870 | clib_bitmap_zero (ldpw->libc_rd_bitmap); |
| 871 | clib_bitmap_zero (ldpw->wr_bitmap); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 872 | clib_bitmap_zero (ldpw->si_wr_bitmap); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 873 | clib_bitmap_zero (ldpw->libc_wr_bitmap); |
| 874 | clib_bitmap_zero (ldpw->ex_bitmap); |
Florin Coras | 294afe2 | 2019-01-07 17:49:17 -0800 | [diff] [blame] | 875 | clib_bitmap_zero (ldpw->si_ex_bitmap); |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 876 | clib_bitmap_zero (ldpw->libc_ex_bitmap); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 877 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 878 | return rv; |
| 879 | } |
| 880 | |
| 881 | int |
| 882 | select (int nfds, fd_set * __restrict readfds, |
| 883 | fd_set * __restrict writefds, |
| 884 | fd_set * __restrict exceptfds, struct timeval *__restrict timeout) |
| 885 | { |
| 886 | struct timespec tspec; |
| 887 | |
| 888 | if (timeout) |
| 889 | { |
| 890 | tspec.tv_sec = timeout->tv_sec; |
| 891 | tspec.tv_nsec = timeout->tv_usec * 1000; |
| 892 | } |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 893 | return ldp_pselect (nfds, readfds, writefds, exceptfds, |
| 894 | timeout ? &tspec : NULL, NULL); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | #ifdef __USE_XOPEN2K |
| 898 | int |
| 899 | pselect (int nfds, fd_set * __restrict readfds, |
| 900 | fd_set * __restrict writefds, |
| 901 | fd_set * __restrict exceptfds, |
| 902 | const struct timespec *__restrict timeout, |
| 903 | const __sigset_t * __restrict sigmask) |
| 904 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 905 | return ldp_pselect (nfds, readfds, writefds, exceptfds, timeout, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 906 | } |
| 907 | #endif |
| 908 | |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 909 | /* If transparent TLS mode is turned on, then ldp will load key and cert. |
| 910 | */ |
| 911 | static int |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 912 | load_cert_key_pair (void) |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 913 | { |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 914 | char *cert_str = getenv (LDP_ENV_TLS_CERT); |
| 915 | char *key_str = getenv (LDP_ENV_TLS_KEY); |
| 916 | char cert_buf[4096], key_buf[4096]; |
| 917 | int cert_size, key_size; |
| 918 | vppcom_cert_key_pair_t crypto; |
| 919 | int ckp_index; |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 920 | FILE *fp; |
| 921 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 922 | if (!cert_str || !key_str) |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 923 | { |
| 924 | LDBG (0, "ERROR: failed to read LDP environment %s\n", |
| 925 | LDP_ENV_TLS_CERT); |
| 926 | return -1; |
| 927 | } |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 928 | |
| 929 | fp = fopen (cert_str, "r"); |
| 930 | if (fp == NULL) |
| 931 | { |
| 932 | LDBG (0, "ERROR: failed to open cert file %s \n", cert_str); |
| 933 | return -1; |
| 934 | } |
| 935 | cert_size = fread (cert_buf, sizeof (char), sizeof (cert_buf), fp); |
| 936 | fclose (fp); |
| 937 | |
| 938 | fp = fopen (key_str, "r"); |
| 939 | if (fp == NULL) |
| 940 | { |
| 941 | LDBG (0, "ERROR: failed to open key file %s \n", key_str); |
| 942 | return -1; |
| 943 | } |
| 944 | key_size = fread (key_buf, sizeof (char), sizeof (key_buf), fp); |
| 945 | fclose (fp); |
| 946 | |
| 947 | crypto.cert = cert_buf; |
| 948 | crypto.key = key_buf; |
| 949 | crypto.cert_len = cert_size; |
| 950 | crypto.key_len = key_size; |
| 951 | ckp_index = vppcom_add_cert_key_pair (&crypto); |
| 952 | if (ckp_index < 0) |
| 953 | { |
| 954 | LDBG (0, "ERROR: failed to add cert key pair\n"); |
| 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | ldp->ckpair_index = ckp_index; |
| 959 | |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static int |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 964 | assign_cert_key_pair (vls_handle_t vlsh) |
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 | uint32_t ckp_len; |
Yu Ping | 7b74b07 | 2019-05-08 00:40:24 +0800 | [diff] [blame] | 967 | |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 968 | if (ldp->ckpair_index == ~0 && load_cert_key_pair () < 0) |
| 969 | return -1; |
| 970 | |
| 971 | ckp_len = sizeof (ldp->ckpair_index); |
liuyacan | 276675e | 2021-08-30 13:24:53 +0800 | [diff] [blame] | 972 | return vls_attr (vlsh, VPPCOM_ATTR_SET_CKPAIR, &ldp->ckpair_index, &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 | |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 1543 | static inline int |
| 1544 | ldp_vls_sendo (vls_handle_t vlsh, const void *buf, size_t n, |
| 1545 | vppcom_endpt_tlv_t *ep_tlv, int flags, |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1546 | __CONST_SOCKADDR_ARG addr, socklen_t addr_len) |
| 1547 | { |
| 1548 | vppcom_endpt_t *ep = 0; |
| 1549 | vppcom_endpt_t _ep; |
| 1550 | |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 1551 | if (ep_tlv) |
| 1552 | { |
| 1553 | _ep.app_data = *ep_tlv; |
| 1554 | } |
| 1555 | |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1556 | if (addr) |
| 1557 | { |
| 1558 | ep = &_ep; |
| 1559 | switch (addr->sa_family) |
| 1560 | { |
| 1561 | case AF_INET: |
| 1562 | ep->is_ip4 = VPPCOM_IS_IP4; |
| 1563 | ep->ip = |
| 1564 | (uint8_t *) & ((const struct sockaddr_in *) addr)->sin_addr; |
| 1565 | ep->port = (uint16_t) ((const struct sockaddr_in *) addr)->sin_port; |
| 1566 | break; |
| 1567 | |
| 1568 | case AF_INET6: |
| 1569 | ep->is_ip4 = VPPCOM_IS_IP6; |
| 1570 | ep->ip = |
| 1571 | (uint8_t *) & ((const struct sockaddr_in6 *) addr)->sin6_addr; |
| 1572 | ep->port = |
| 1573 | (uint16_t) ((const struct sockaddr_in6 *) addr)->sin6_port; |
| 1574 | break; |
| 1575 | |
| 1576 | default: |
| 1577 | return EAFNOSUPPORT; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | return vls_sendto (vlsh, (void *) buf, n, flags, ep); |
| 1582 | } |
| 1583 | |
| 1584 | static int |
| 1585 | ldp_vls_recvfrom (vls_handle_t vlsh, void *__restrict buf, size_t n, |
| 1586 | int flags, __SOCKADDR_ARG addr, |
| 1587 | socklen_t * __restrict addr_len) |
| 1588 | { |
| 1589 | u8 src_addr[sizeof (struct sockaddr_in6)]; |
| 1590 | vppcom_endpt_t ep; |
| 1591 | ssize_t size; |
| 1592 | int rv; |
| 1593 | |
| 1594 | if (addr) |
| 1595 | { |
| 1596 | ep.ip = src_addr; |
| 1597 | size = vls_recvfrom (vlsh, buf, n, flags, &ep); |
| 1598 | |
| 1599 | if (size > 0) |
| 1600 | { |
| 1601 | rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep); |
| 1602 | if (rv < 0) |
| 1603 | size = rv; |
| 1604 | } |
| 1605 | } |
| 1606 | else |
| 1607 | size = vls_recvfrom (vlsh, buf, n, flags, NULL); |
| 1608 | |
| 1609 | return size; |
| 1610 | } |
| 1611 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1612 | ssize_t |
| 1613 | sendto (int fd, const void *buf, size_t n, int flags, |
| 1614 | __CONST_SOCKADDR_ARG addr, socklen_t addr_len) |
| 1615 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1616 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1617 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1618 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1619 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1620 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1621 | vlsh = ldp_fd_to_vlsh (fd); |
wanghanlin | 97c6e0d | 2021-07-06 15:01:48 +0800 | [diff] [blame] | 1622 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1623 | { |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 1624 | size = ldp_vls_sendo (vlsh, buf, n, NULL, flags, addr, addr_len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1625 | if (size < 0) |
| 1626 | { |
| 1627 | errno = -size; |
| 1628 | size = -1; |
| 1629 | } |
| 1630 | } |
| 1631 | else |
| 1632 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1633 | size = libc_sendto (fd, buf, n, flags, addr, addr_len); |
| 1634 | } |
| 1635 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1636 | return size; |
| 1637 | } |
| 1638 | |
| 1639 | ssize_t |
| 1640 | recvfrom (int fd, void *__restrict buf, size_t n, int flags, |
| 1641 | __SOCKADDR_ARG addr, socklen_t * __restrict addr_len) |
| 1642 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1643 | vls_handle_t vlsh; |
| 1644 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1645 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1646 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1647 | |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1648 | vlsh = ldp_fd_to_vlsh (fd); |
| 1649 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1650 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1651 | size = ldp_vls_recvfrom (vlsh, buf, n, flags, addr, addr_len); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1652 | if (size < 0) |
| 1653 | { |
| 1654 | errno = -size; |
| 1655 | size = -1; |
| 1656 | } |
| 1657 | } |
| 1658 | else |
| 1659 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1660 | size = libc_recvfrom (fd, buf, n, flags, addr, addr_len); |
| 1661 | } |
| 1662 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1663 | return size; |
| 1664 | } |
| 1665 | |
| 1666 | ssize_t |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1667 | sendmsg (int fd, const struct msghdr * msg, int flags) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1668 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1669 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1670 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1671 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1672 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1673 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1674 | vlsh = ldp_fd_to_vlsh (fd); |
| 1675 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1676 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1677 | struct iovec *iov = msg->msg_iov; |
| 1678 | ssize_t total = 0; |
Florin Coras | cba1c22 | 2021-11-29 08:12:27 -0800 | [diff] [blame] | 1679 | int i, rv = 0; |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 1680 | struct cmsghdr *cmsg; |
| 1681 | uint16_t *valp; |
| 1682 | vppcom_endpt_tlv_t _app_data; |
| 1683 | vppcom_endpt_tlv_t *p_app_data = NULL; |
| 1684 | |
| 1685 | cmsg = CMSG_FIRSTHDR (msg); |
| 1686 | if (cmsg && cmsg->cmsg_type == UDP_SEGMENT) |
| 1687 | { |
| 1688 | p_app_data = &_app_data; |
| 1689 | valp = (void *) CMSG_DATA (cmsg); |
| 1690 | p_app_data->data_type = VCL_UDP_SEGMENT; |
| 1691 | p_app_data->data_len = sizeof (*valp); |
| 1692 | p_app_data->value = *valp; |
| 1693 | } |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1694 | |
| 1695 | for (i = 0; i < msg->msg_iovlen; ++i) |
| 1696 | { |
Dou Chao | 243a043 | 2022-11-29 19:41:34 +0800 | [diff] [blame] | 1697 | rv = |
| 1698 | ldp_vls_sendo (vlsh, iov[i].iov_base, iov[i].iov_len, p_app_data, |
| 1699 | flags, msg->msg_name, msg->msg_namelen); |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1700 | if (rv < 0) |
| 1701 | break; |
| 1702 | else |
| 1703 | { |
| 1704 | total += rv; |
| 1705 | if (rv < iov[i].iov_len) |
| 1706 | break; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | if (rv < 0 && total == 0) |
| 1711 | { |
| 1712 | errno = -rv; |
| 1713 | size = -1; |
| 1714 | } |
| 1715 | else |
| 1716 | size = total; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1717 | } |
| 1718 | else |
| 1719 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1720 | size = libc_sendmsg (fd, msg, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1721 | } |
| 1722 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1723 | return size; |
| 1724 | } |
| 1725 | |
| 1726 | #ifdef USE_GNU |
| 1727 | int |
| 1728 | sendmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags) |
| 1729 | { |
| 1730 | ssize_t size; |
| 1731 | const char *func_str; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1732 | u32 sh = ldp_fd_to_vlsh (fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1733 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1734 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1735 | |
wanghanlin | 97c6e0d | 2021-07-06 15:01:48 +0800 | [diff] [blame] | 1736 | if (sh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1737 | { |
| 1738 | clib_warning ("LDP<%d>: LDP-TBD", getpid ()); |
| 1739 | errno = ENOSYS; |
| 1740 | size = -1; |
| 1741 | } |
| 1742 | else |
| 1743 | { |
| 1744 | func_str = "libc_sendmmsg"; |
| 1745 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1746 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1747 | clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " |
| 1748 | "vmessages %p, vlen %u, flags 0x%x", |
| 1749 | getpid (), fd, fd, func_str, vmessages, vlen, flags); |
| 1750 | |
| 1751 | size = libc_sendmmsg (fd, vmessages, vlen, flags); |
| 1752 | } |
| 1753 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1754 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1755 | { |
| 1756 | if (size < 0) |
| 1757 | { |
| 1758 | int errno_val = errno; |
| 1759 | perror (func_str); |
| 1760 | clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " |
| 1761 | "rv %d, errno = %d", getpid (), fd, fd, |
| 1762 | func_str, size, errno_val); |
| 1763 | errno = errno_val; |
| 1764 | } |
| 1765 | else |
| 1766 | clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", |
| 1767 | getpid (), fd, fd, size, size); |
| 1768 | } |
| 1769 | return size; |
| 1770 | } |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1771 | #endif |
| 1772 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1773 | ssize_t |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1774 | recvmsg (int fd, struct msghdr * msg, int flags) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1775 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1776 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1777 | ssize_t size; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1778 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1779 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1780 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1781 | vlsh = ldp_fd_to_vlsh (fd); |
| 1782 | if (vlsh != VLS_INVALID_HANDLE) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1783 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1784 | struct iovec *iov = msg->msg_iov; |
| 1785 | ssize_t max_deq, total = 0; |
| 1786 | int i, rv; |
| 1787 | |
| 1788 | max_deq = vls_attr (vlsh, VPPCOM_ATTR_GET_NREAD, 0, 0); |
| 1789 | if (!max_deq) |
| 1790 | return 0; |
| 1791 | |
| 1792 | for (i = 0; i < msg->msg_iovlen; i++) |
| 1793 | { |
| 1794 | rv = ldp_vls_recvfrom (vlsh, iov[i].iov_base, iov[i].iov_len, flags, |
| 1795 | (i == 0 ? msg->msg_name : NULL), |
| 1796 | (i == 0 ? &msg->msg_namelen : NULL)); |
| 1797 | if (rv <= 0) |
| 1798 | break; |
| 1799 | else |
| 1800 | { |
| 1801 | total += rv; |
| 1802 | if (rv < iov[i].iov_len) |
| 1803 | break; |
| 1804 | } |
| 1805 | if (total >= max_deq) |
| 1806 | break; |
| 1807 | } |
| 1808 | |
| 1809 | if (rv < 0 && total == 0) |
| 1810 | { |
| 1811 | errno = -rv; |
| 1812 | size = -1; |
| 1813 | } |
| 1814 | else |
| 1815 | size = total; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1816 | } |
| 1817 | else |
| 1818 | { |
Florin Coras | ce17f46 | 2020-05-22 20:36:29 +0000 | [diff] [blame] | 1819 | size = libc_recvmsg (fd, msg, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1820 | } |
| 1821 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1822 | return size; |
| 1823 | } |
| 1824 | |
| 1825 | #ifdef USE_GNU |
| 1826 | int |
| 1827 | recvmmsg (int fd, struct mmsghdr *vmessages, |
| 1828 | unsigned int vlen, int flags, struct timespec *tmo) |
| 1829 | { |
| 1830 | ssize_t size; |
| 1831 | const char *func_str; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1832 | u32 sh = ldp_fd_to_vlsh (fd); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1833 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1834 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1835 | |
wanghanlin | 97c6e0d | 2021-07-06 15:01:48 +0800 | [diff] [blame] | 1836 | if (sh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1837 | { |
| 1838 | clib_warning ("LDP<%d>: LDP-TBD", getpid ()); |
| 1839 | errno = ENOSYS; |
| 1840 | size = -1; |
| 1841 | } |
| 1842 | else |
| 1843 | { |
| 1844 | func_str = "libc_recvmmsg"; |
| 1845 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1846 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1847 | clib_warning ("LDP<%d>: fd %d (0x%x): calling %s(): " |
| 1848 | "vmessages %p, vlen %u, flags 0x%x, tmo %p", |
| 1849 | getpid (), fd, fd, func_str, vmessages, vlen, |
| 1850 | flags, tmo); |
| 1851 | |
| 1852 | size = libc_recvmmsg (fd, vmessages, vlen, flags, tmo); |
| 1853 | } |
| 1854 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 1855 | if (LDP_DEBUG > 2) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1856 | { |
| 1857 | if (size < 0) |
| 1858 | { |
| 1859 | int errno_val = errno; |
| 1860 | perror (func_str); |
| 1861 | clib_warning ("LDP<%d>: ERROR: fd %d (0x%x): %s() failed! " |
| 1862 | "rv %d, errno = %d", getpid (), fd, fd, |
| 1863 | func_str, size, errno_val); |
| 1864 | errno = errno_val; |
| 1865 | } |
| 1866 | else |
| 1867 | clib_warning ("LDP<%d>: fd %d (0x%x): returning %d (0x%x)", |
| 1868 | getpid (), fd, fd, size, size); |
| 1869 | } |
| 1870 | return size; |
| 1871 | } |
| 1872 | #endif |
| 1873 | |
| 1874 | int |
| 1875 | getsockopt (int fd, int level, int optname, |
| 1876 | void *__restrict optval, socklen_t * __restrict optlen) |
| 1877 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1878 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1879 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1880 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 1881 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1882 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1883 | vlsh = ldp_fd_to_vlsh (fd); |
| 1884 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1885 | { |
| 1886 | rv = -EOPNOTSUPP; |
| 1887 | |
| 1888 | switch (level) |
| 1889 | { |
| 1890 | case SOL_TCP: |
| 1891 | switch (optname) |
| 1892 | { |
| 1893 | case TCP_NODELAY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1894 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_NODELAY, |
| 1895 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1896 | break; |
| 1897 | case TCP_MAXSEG: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1898 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_USER_MSS, |
| 1899 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1900 | break; |
| 1901 | case TCP_KEEPIDLE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1902 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPIDLE, |
| 1903 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1904 | break; |
| 1905 | case TCP_KEEPINTVL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1906 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TCP_KEEPINTVL, |
| 1907 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1908 | break; |
| 1909 | case TCP_INFO: |
| 1910 | if (optval && optlen && (*optlen == sizeof (struct tcp_info))) |
| 1911 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1912 | LDBG (1, "fd %d: vlsh %u SOL_TCP, TCP_INFO, optval %p, " |
| 1913 | "optlen %d: #LDP-NOP#", fd, vlsh, optval, *optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1914 | memset (optval, 0, *optlen); |
| 1915 | rv = VPPCOM_OK; |
| 1916 | } |
| 1917 | else |
| 1918 | rv = -EFAULT; |
| 1919 | break; |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 1920 | case TCP_CONGESTION: |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 1921 | *optlen = strlen ("cubic"); |
Dave Barach | 0250090 | 2020-04-04 18:34:41 -0400 | [diff] [blame] | 1922 | strncpy (optval, "cubic", *optlen + 1); |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 1923 | rv = 0; |
| 1924 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1925 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1926 | LDBG (0, "ERROR: fd %d: getsockopt SOL_TCP: sid %u, " |
| 1927 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1928 | break; |
| 1929 | } |
| 1930 | break; |
| 1931 | case SOL_IPV6: |
| 1932 | switch (optname) |
| 1933 | { |
| 1934 | case IPV6_V6ONLY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1935 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_V6ONLY, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1936 | break; |
| 1937 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1938 | LDBG (0, "ERROR: fd %d: getsockopt SOL_IPV6: vlsh %u " |
| 1939 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1940 | break; |
| 1941 | } |
| 1942 | break; |
| 1943 | case SOL_SOCKET: |
| 1944 | switch (optname) |
| 1945 | { |
| 1946 | case SO_ACCEPTCONN: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1947 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_LISTEN, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1948 | break; |
| 1949 | case SO_KEEPALIVE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1950 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_KEEPALIVE, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1951 | break; |
| 1952 | case SO_PROTOCOL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1953 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_PROTOCOL, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1954 | *(int *) optval = *(int *) optval ? SOCK_DGRAM : SOCK_STREAM; |
| 1955 | break; |
| 1956 | case SO_SNDBUF: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1957 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_TX_FIFO_LEN, |
| 1958 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1959 | break; |
| 1960 | case SO_RCVBUF: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1961 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_RX_FIFO_LEN, |
| 1962 | optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1963 | break; |
| 1964 | case SO_REUSEADDR: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1965 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEADDR, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1966 | break; |
wanghanlin | 0674f85 | 2021-02-22 10:38:36 +0800 | [diff] [blame] | 1967 | case SO_REUSEPORT: |
| 1968 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_REUSEPORT, optval, optlen); |
| 1969 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1970 | case SO_BROADCAST: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1971 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_BROADCAST, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1972 | break; |
wanghanlin | 0674f85 | 2021-02-22 10:38:36 +0800 | [diff] [blame] | 1973 | case SO_DOMAIN: |
| 1974 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_DOMAIN, optval, optlen); |
| 1975 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1976 | case SO_ERROR: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1977 | rv = vls_attr (vlsh, VPPCOM_ATTR_GET_ERROR, optval, optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1978 | break; |
Florin Coras | 8eb8d50 | 2021-06-16 14:46:57 -0700 | [diff] [blame] | 1979 | case SO_BINDTODEVICE: |
| 1980 | rv = 0; |
| 1981 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1982 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1983 | LDBG (0, "ERROR: fd %d: getsockopt SOL_SOCKET: vlsh %u " |
| 1984 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1985 | break; |
| 1986 | } |
| 1987 | break; |
| 1988 | default: |
| 1989 | break; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1992 | if (rv != VPPCOM_OK) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 1993 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 1994 | errno = -rv; |
| 1995 | rv = -1; |
| 1996 | } |
| 1997 | } |
| 1998 | else |
| 1999 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2000 | rv = libc_getsockopt (fd, level, optname, optval, optlen); |
| 2001 | } |
| 2002 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2003 | return rv; |
| 2004 | } |
| 2005 | |
| 2006 | int |
| 2007 | setsockopt (int fd, int level, int optname, |
| 2008 | const void *optval, socklen_t optlen) |
| 2009 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2010 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2011 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2012 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2013 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2014 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2015 | vlsh = ldp_fd_to_vlsh (fd); |
| 2016 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2017 | { |
| 2018 | rv = -EOPNOTSUPP; |
| 2019 | |
| 2020 | switch (level) |
| 2021 | { |
| 2022 | case SOL_TCP: |
| 2023 | switch (optname) |
| 2024 | { |
| 2025 | case TCP_NODELAY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2026 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_NODELAY, |
| 2027 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2028 | break; |
| 2029 | case TCP_MAXSEG: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2030 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_USER_MSS, |
| 2031 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2032 | break; |
| 2033 | case TCP_KEEPIDLE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2034 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPIDLE, |
| 2035 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2036 | break; |
| 2037 | case TCP_KEEPINTVL: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2038 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_TCP_KEEPINTVL, |
| 2039 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2040 | break; |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 2041 | case TCP_CONGESTION: |
Florin Coras | 8509aa2 | 2019-04-04 12:55:30 -0700 | [diff] [blame] | 2042 | case TCP_CORK: |
Florin Coras | 0ed24e9 | 2019-01-21 09:03:10 -0800 | [diff] [blame] | 2043 | /* Ignore */ |
| 2044 | rv = 0; |
| 2045 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2046 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2047 | LDBG (0, "ERROR: fd %d: setsockopt() SOL_TCP: vlsh %u" |
| 2048 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2049 | break; |
| 2050 | } |
| 2051 | break; |
| 2052 | case SOL_IPV6: |
| 2053 | switch (optname) |
| 2054 | { |
| 2055 | case IPV6_V6ONLY: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2056 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_V6ONLY, |
| 2057 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2058 | break; |
| 2059 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2060 | LDBG (0, "ERROR: fd %d: setsockopt SOL_IPV6: vlsh %u" |
| 2061 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2062 | break; |
| 2063 | } |
| 2064 | break; |
| 2065 | case SOL_SOCKET: |
| 2066 | switch (optname) |
| 2067 | { |
| 2068 | case SO_KEEPALIVE: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2069 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_KEEPALIVE, |
| 2070 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2071 | break; |
| 2072 | case SO_REUSEADDR: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2073 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEADDR, |
| 2074 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2075 | break; |
wanghanlin | 0674f85 | 2021-02-22 10:38:36 +0800 | [diff] [blame] | 2076 | case SO_REUSEPORT: |
| 2077 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_REUSEPORT, (void *) optval, |
| 2078 | &optlen); |
| 2079 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2080 | case SO_BROADCAST: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2081 | rv = vls_attr (vlsh, VPPCOM_ATTR_SET_BROADCAST, |
| 2082 | (void *) optval, &optlen); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2083 | break; |
Florin Coras | 2f64790 | 2021-06-02 08:23:50 -0700 | [diff] [blame] | 2084 | case SO_LINGER: |
| 2085 | rv = 0; |
| 2086 | break; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2087 | default: |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2088 | LDBG (0, "ERROR: fd %d: setsockopt SOL_SOCKET: vlsh %u " |
| 2089 | "optname %d unsupported!", fd, vlsh, optname); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2090 | break; |
| 2091 | } |
| 2092 | break; |
| 2093 | default: |
| 2094 | break; |
| 2095 | } |
| 2096 | |
| 2097 | if (rv != VPPCOM_OK) |
| 2098 | { |
| 2099 | errno = -rv; |
| 2100 | rv = -1; |
| 2101 | } |
| 2102 | } |
| 2103 | else |
| 2104 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2105 | rv = libc_setsockopt (fd, level, optname, optval, optlen); |
| 2106 | } |
| 2107 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2108 | return rv; |
| 2109 | } |
| 2110 | |
| 2111 | int |
| 2112 | listen (int fd, int n) |
| 2113 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2114 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2115 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2116 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2117 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2118 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2119 | vlsh = ldp_fd_to_vlsh (fd); |
| 2120 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2121 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2122 | 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] | 2123 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2124 | rv = vls_listen (vlsh, n); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2125 | if (rv != VPPCOM_OK) |
| 2126 | { |
| 2127 | errno = -rv; |
| 2128 | rv = -1; |
| 2129 | } |
| 2130 | } |
| 2131 | else |
| 2132 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2133 | LDBG (0, "fd %d: calling libc_listen(): n %d", fd, n); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2134 | rv = libc_listen (fd, n); |
| 2135 | } |
| 2136 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2137 | LDBG (1, "fd %d: returning %d", fd, rv); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2138 | return rv; |
| 2139 | } |
| 2140 | |
| 2141 | static inline int |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2142 | ldp_accept4 (int listen_fd, __SOCKADDR_ARG addr, |
| 2143 | socklen_t * __restrict addr_len, int flags) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2144 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2145 | vls_handle_t listen_vlsh, accept_vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2146 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2147 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2148 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2149 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2150 | listen_vlsh = ldp_fd_to_vlsh (listen_fd); |
| 2151 | if (listen_vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2152 | { |
| 2153 | vppcom_endpt_t ep; |
| 2154 | u8 src_addr[sizeof (struct sockaddr_in6)]; |
Dave Wallace | 8aaba56 | 2018-01-18 17:21:19 -0500 | [diff] [blame] | 2155 | memset (&ep, 0, sizeof (ep)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2156 | ep.ip = src_addr; |
| 2157 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2158 | LDBG (0, "listen fd %d: calling vppcom_session_accept: listen sid %u," |
Florin Coras | eb801d0 | 2020-09-16 17:44:58 -0700 | [diff] [blame] | 2159 | " ep %p, flags 0x%x", listen_fd, listen_vlsh, &ep, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2160 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2161 | accept_vlsh = vls_accept (listen_vlsh, &ep, flags); |
| 2162 | if (accept_vlsh < 0) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2163 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2164 | errno = -accept_vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2165 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2166 | } |
| 2167 | else |
| 2168 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2169 | rv = ldp_copy_ep_to_sockaddr (addr, addr_len, &ep); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2170 | if (rv != VPPCOM_OK) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2171 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2172 | (void) vls_close (accept_vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2173 | errno = -rv; |
| 2174 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2175 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2176 | else |
| 2177 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2178 | rv = ldp_vlsh_to_fd (accept_vlsh); |
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 | } |
| 2181 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2182 | else |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2183 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2184 | LDBG (0, "listen fd %d: calling libc_accept4(): addr %p, addr_len %p," |
| 2185 | " flags 0x%x", listen_fd, addr, addr_len, flags); |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2186 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2187 | rv = libc_accept4 (listen_fd, addr, addr_len, flags); |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2190 | LDBG (1, "listen fd %d: accept returning %d", listen_fd, rv); |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 2191 | |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2192 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2195 | int |
| 2196 | accept4 (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len, |
| 2197 | int flags) |
| 2198 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2199 | return ldp_accept4 (fd, addr, addr_len, flags); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2200 | } |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2201 | |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2202 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2203 | accept (int fd, __SOCKADDR_ARG addr, socklen_t * __restrict addr_len) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2204 | { |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2205 | return ldp_accept4 (fd, addr, addr_len, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2206 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2207 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2208 | int |
| 2209 | shutdown (int fd, int how) |
| 2210 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2211 | vls_handle_t vlsh; |
liuyacan | 55c952e | 2021-06-13 14:54:55 +0800 | [diff] [blame] | 2212 | int rv = 0; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2213 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2214 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2215 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2216 | vlsh = ldp_fd_to_vlsh (fd); |
| 2217 | if (vlsh != VLS_INVALID_HANDLE) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2218 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2219 | LDBG (0, "called shutdown: fd %u vlsh %u how %d", fd, vlsh, how); |
liuyacan | 55c952e | 2021-06-13 14:54:55 +0800 | [diff] [blame] | 2220 | rv = vls_shutdown (vlsh, how); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2221 | } |
| 2222 | else |
| 2223 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2224 | LDBG (0, "fd %d: calling libc_shutdown: how %d", fd, how); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2225 | rv = libc_shutdown (fd, how); |
| 2226 | } |
| 2227 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2228 | return rv; |
| 2229 | } |
| 2230 | |
| 2231 | int |
| 2232 | epoll_create1 (int flags) |
| 2233 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2234 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2235 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2236 | int rv; |
| 2237 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2238 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2239 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2240 | if (ldp->vcl_needs_real_epoll || vls_use_real_epoll ()) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2241 | { |
Florin Coras | 2d9b427 | 2019-03-11 10:14:37 -0700 | [diff] [blame] | 2242 | /* Make sure workers have been allocated */ |
| 2243 | if (!ldp->workers) |
| 2244 | { |
| 2245 | ldp_alloc_workers (); |
| 2246 | ldpw = ldp_worker_get_current (); |
| 2247 | } |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2248 | rv = libc_epoll_create1 (flags); |
| 2249 | ldp->vcl_needs_real_epoll = 0; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2250 | ldpw->vcl_mq_epfd = rv; |
Florin Coras | 05ecfcc | 2018-12-12 18:19:39 -0800 | [diff] [blame] | 2251 | LDBG (0, "created vcl epfd %u", rv); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2252 | return rv; |
| 2253 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2254 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2255 | vlsh = vls_epoll_create (); |
| 2256 | if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE)) |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2257 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2258 | errno = -vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2259 | rv = -1; |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 2260 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2261 | else |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2262 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2263 | rv = ldp_vlsh_to_fd (vlsh); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2264 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2265 | LDBG (0, "epoll_create epfd %u vlsh %u", rv, vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2266 | return rv; |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | int |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2270 | epoll_create (int size) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2271 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2272 | return epoll_create1 (0); |
| 2273 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2274 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2275 | int |
| 2276 | epoll_ctl (int epfd, int op, int fd, struct epoll_event *event) |
| 2277 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2278 | vls_handle_t vep_vlsh, vlsh; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2279 | int rv; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2280 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2281 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2282 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2283 | vep_vlsh = ldp_fd_to_vlsh (epfd); |
| 2284 | if (PREDICT_FALSE (vep_vlsh == VLS_INVALID_HANDLE)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2285 | { |
Dave Wallace | 3ee1fe1 | 2018-02-23 01:09:11 -0500 | [diff] [blame] | 2286 | /* The LDP epoll_create1 always creates VCL epfd's. |
| 2287 | * The app should never have a kernel base epoll fd unless it |
| 2288 | * was acquired outside of the LD_PRELOAD process context. |
| 2289 | * In any case, if we get one, punt it to libc_epoll_ctl. |
| 2290 | */ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2291 | LDBG (1, "epfd %d: calling libc_epoll_ctl: op %d, fd %d" |
| 2292 | " event %p", epfd, op, fd, event); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2293 | |
| 2294 | rv = libc_epoll_ctl (epfd, op, fd, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2295 | goto done; |
| 2296 | } |
| 2297 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2298 | vlsh = ldp_fd_to_vlsh (fd); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2299 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2300 | LDBG (0, "epfd %d ep_vlsh %d, fd %u vlsh %d, op %u", epfd, vep_vlsh, fd, |
| 2301 | vlsh, op); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2302 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2303 | if (vlsh != VLS_INVALID_HANDLE) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2304 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2305 | 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] | 2306 | " event %p", epfd, vep_vlsh, op, vlsh, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2307 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2308 | rv = vls_epoll_ctl (vep_vlsh, op, vlsh, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2309 | if (rv != VPPCOM_OK) |
| 2310 | { |
| 2311 | errno = -rv; |
| 2312 | rv = -1; |
| 2313 | } |
| 2314 | } |
| 2315 | else |
| 2316 | { |
| 2317 | int libc_epfd; |
| 2318 | u32 size = sizeof (epfd); |
| 2319 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2320 | 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] | 2321 | if (!libc_epfd) |
| 2322 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2323 | LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: " |
| 2324 | "EPOLL_CLOEXEC", epfd, vep_vlsh); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2325 | |
| 2326 | libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC); |
| 2327 | if (libc_epfd < 0) |
| 2328 | { |
| 2329 | rv = libc_epfd; |
| 2330 | goto done; |
| 2331 | } |
| 2332 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2333 | rv = vls_attr (vep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, |
| 2334 | &size); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2335 | if (rv < 0) |
| 2336 | { |
| 2337 | errno = -rv; |
| 2338 | rv = -1; |
| 2339 | goto done; |
| 2340 | } |
| 2341 | } |
| 2342 | else if (PREDICT_FALSE (libc_epfd < 0)) |
| 2343 | { |
| 2344 | errno = -epfd; |
| 2345 | rv = -1; |
| 2346 | goto done; |
| 2347 | } |
| 2348 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2349 | LDBG (1, "epfd %d: calling libc_epoll_ctl: libc_epfd %d, op %d, fd %d," |
| 2350 | " event %p", epfd, libc_epfd, op, fd, event); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2351 | |
| 2352 | rv = libc_epoll_ctl (libc_epfd, op, fd, event); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | done: |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2356 | return rv; |
| 2357 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2358 | |
| 2359 | static inline int |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2360 | ldp_epoll_pwait (int epfd, struct epoll_event *events, int maxevents, |
| 2361 | int timeout, const sigset_t * sigmask) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2362 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2363 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2364 | double time_to_wait = (double) 0, max_time; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2365 | int libc_epfd, rv = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2366 | vls_handle_t ep_vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2367 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2368 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2369 | |
| 2370 | if (PREDICT_FALSE (!events || (timeout < -1))) |
| 2371 | { |
| 2372 | errno = EFAULT; |
| 2373 | return -1; |
| 2374 | } |
| 2375 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2376 | if (epfd == ldpw->vcl_mq_epfd) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 2377 | return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask); |
| 2378 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2379 | ep_vlsh = ldp_fd_to_vlsh (epfd); |
| 2380 | if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2381 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2382 | LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2383 | errno = EBADFD; |
| 2384 | return -1; |
| 2385 | } |
| 2386 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2387 | if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) |
| 2388 | clib_time_init (&ldpw->clib_time); |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 2389 | time_to_wait = ((timeout >= 0) ? (double) timeout / 1000 : 0); |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2390 | max_time = clib_time_now (&ldpw->clib_time) + time_to_wait; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2391 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2392 | 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] | 2393 | if (PREDICT_FALSE (libc_epfd < 0)) |
| 2394 | { |
| 2395 | errno = -libc_epfd; |
| 2396 | rv = -1; |
| 2397 | goto done; |
| 2398 | } |
| 2399 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2400 | LDBG (2, "epfd %d: vep_idx %d, libc_epfd %d, events %p, maxevents %d, " |
| 2401 | "timeout %d, sigmask %p: time_to_wait %.02f", epfd, ep_vlsh, |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2402 | libc_epfd, events, maxevents, timeout, sigmask, time_to_wait); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2403 | do |
| 2404 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2405 | if (!ldpw->epoll_wait_vcl) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2406 | { |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2407 | rv = vls_epoll_wait (ep_vlsh, events, maxevents, 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2408 | if (rv > 0) |
| 2409 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2410 | ldpw->epoll_wait_vcl = 1; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2411 | goto done; |
| 2412 | } |
| 2413 | else if (rv < 0) |
| 2414 | { |
| 2415 | errno = -rv; |
| 2416 | rv = -1; |
| 2417 | goto done; |
| 2418 | } |
| 2419 | } |
| 2420 | else |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2421 | ldpw->epoll_wait_vcl = 0; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2422 | |
| 2423 | if (libc_epfd > 0) |
| 2424 | { |
Florin Coras | b0f662f | 2018-12-27 14:51:46 -0800 | [diff] [blame] | 2425 | rv = libc_epoll_pwait (libc_epfd, events, maxevents, 0, sigmask); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2426 | if (rv != 0) |
| 2427 | goto done; |
| 2428 | } |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2429 | } |
Florin Coras | 72f7782 | 2019-01-22 19:05:52 -0800 | [diff] [blame] | 2430 | while ((timeout == -1) || (clib_time_now (&ldpw->clib_time) < max_time)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2431 | |
| 2432 | done: |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2433 | return rv; |
| 2434 | } |
| 2435 | |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2436 | static inline int |
| 2437 | ldp_epoll_pwait_eventfd (int epfd, struct epoll_event *events, |
| 2438 | int maxevents, int timeout, const sigset_t * sigmask) |
| 2439 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2440 | ldp_worker_ctx_t *ldpw; |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2441 | int libc_epfd, rv = 0, num_ev; |
| 2442 | vls_handle_t ep_vlsh; |
| 2443 | |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2444 | ldp_init_check (); |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2445 | |
| 2446 | if (PREDICT_FALSE (!events || (timeout < -1))) |
| 2447 | { |
| 2448 | errno = EFAULT; |
| 2449 | return -1; |
| 2450 | } |
| 2451 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 2452 | /* Make sure the vcl worker is valid. Could be that epoll fd was created on |
| 2453 | * one thread but it is now used on another */ |
| 2454 | if (PREDICT_FALSE (vppcom_worker_index () == ~0)) |
| 2455 | vls_register_vcl_worker (); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2456 | |
| 2457 | ldpw = ldp_worker_get_current (); |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2458 | if (epfd == ldpw->vcl_mq_epfd) |
| 2459 | return libc_epoll_pwait (epfd, events, maxevents, timeout, sigmask); |
| 2460 | |
| 2461 | ep_vlsh = ldp_fd_to_vlsh (epfd); |
| 2462 | if (PREDICT_FALSE (ep_vlsh == VLS_INVALID_HANDLE)) |
| 2463 | { |
| 2464 | LDBG (0, "epfd %d: bad ep_vlsh %d!", epfd, ep_vlsh); |
| 2465 | errno = EBADFD; |
| 2466 | return -1; |
| 2467 | } |
| 2468 | |
| 2469 | libc_epfd = vls_attr (ep_vlsh, VPPCOM_ATTR_GET_LIBC_EPFD, 0, 0); |
| 2470 | if (PREDICT_FALSE (!libc_epfd)) |
| 2471 | { |
| 2472 | u32 size = sizeof (epfd); |
| 2473 | |
| 2474 | LDBG (1, "epfd %d, vep_vlsh %d calling libc_epoll_create1: " |
| 2475 | "EPOLL_CLOEXEC", epfd, ep_vlsh); |
| 2476 | libc_epfd = libc_epoll_create1 (EPOLL_CLOEXEC); |
| 2477 | if (libc_epfd < 0) |
| 2478 | { |
| 2479 | rv = libc_epfd; |
| 2480 | goto done; |
| 2481 | } |
| 2482 | |
| 2483 | rv = vls_attr (ep_vlsh, VPPCOM_ATTR_SET_LIBC_EPFD, &libc_epfd, &size); |
| 2484 | if (rv < 0) |
| 2485 | { |
| 2486 | errno = -rv; |
| 2487 | rv = -1; |
| 2488 | goto done; |
| 2489 | } |
| 2490 | } |
| 2491 | if (PREDICT_FALSE (libc_epfd <= 0)) |
| 2492 | { |
| 2493 | errno = -libc_epfd; |
| 2494 | rv = -1; |
| 2495 | goto done; |
| 2496 | } |
| 2497 | |
| 2498 | if (PREDICT_FALSE (!ldpw->mq_epfd_added)) |
| 2499 | { |
| 2500 | struct epoll_event e = { 0 }; |
| 2501 | e.events = EPOLLIN; |
| 2502 | e.data.fd = ldpw->vcl_mq_epfd; |
| 2503 | if (libc_epoll_ctl (libc_epfd, EPOLL_CTL_ADD, ldpw->vcl_mq_epfd, &e) < |
| 2504 | 0) |
| 2505 | { |
| 2506 | LDBG (0, "epfd %d, add libc mq epoll fd %d to libc epoll fd %d", |
| 2507 | epfd, ldpw->vcl_mq_epfd, libc_epfd); |
| 2508 | rv = -1; |
| 2509 | goto done; |
| 2510 | } |
| 2511 | ldpw->mq_epfd_added = 1; |
| 2512 | } |
| 2513 | |
wanghanlin | 8919fec | 2021-03-18 20:00:41 +0800 | [diff] [blame] | 2514 | /* Request to only drain unhandled to prevent libc_epoll_wait starved */ |
| 2515 | rv = vls_epoll_wait (ep_vlsh, events, maxevents, -2); |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2516 | if (rv > 0) |
| 2517 | goto done; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2518 | else if (PREDICT_FALSE (rv < 0)) |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2519 | { |
| 2520 | errno = -rv; |
| 2521 | rv = -1; |
| 2522 | goto done; |
| 2523 | } |
| 2524 | |
| 2525 | rv = libc_epoll_pwait (libc_epfd, events, maxevents, timeout, sigmask); |
| 2526 | if (rv <= 0) |
| 2527 | goto done; |
| 2528 | for (int i = 0; i < rv; i++) |
| 2529 | { |
| 2530 | if (events[i].data.fd == ldpw->vcl_mq_epfd) |
| 2531 | { |
| 2532 | /* We should remove mq epoll fd from events. */ |
| 2533 | rv--; |
| 2534 | if (i != rv) |
| 2535 | { |
| 2536 | events[i].events = events[rv].events; |
| 2537 | events[i].data.u64 = events[rv].data.u64; |
| 2538 | } |
| 2539 | num_ev = vls_epoll_wait (ep_vlsh, &events[rv], maxevents - rv, 0); |
| 2540 | if (PREDICT_TRUE (num_ev > 0)) |
| 2541 | rv += num_ev; |
| 2542 | break; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | done: |
| 2547 | return rv; |
| 2548 | } |
| 2549 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2550 | int |
| 2551 | epoll_pwait (int epfd, struct epoll_event *events, |
| 2552 | int maxevents, int timeout, const sigset_t * sigmask) |
| 2553 | { |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2554 | if (vls_use_eventfd ()) |
| 2555 | return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, |
| 2556 | sigmask); |
| 2557 | else |
| 2558 | return ldp_epoll_pwait (epfd, events, maxevents, timeout, sigmask); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | int |
| 2562 | epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout) |
| 2563 | { |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2564 | if (vls_use_eventfd ()) |
| 2565 | return ldp_epoll_pwait_eventfd (epfd, events, maxevents, timeout, NULL); |
| 2566 | else |
| 2567 | return ldp_epoll_pwait (epfd, events, maxevents, timeout, NULL); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | int |
| 2571 | poll (struct pollfd *fds, nfds_t nfds, int timeout) |
| 2572 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2573 | ldp_worker_ctx_t *ldpw = ldp_worker_get_current (); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2574 | int rv, i, n_revents = 0; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2575 | vls_handle_t vlsh; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2576 | vcl_poll_t *vp; |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2577 | double max_time; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2578 | |
Florin Coras | eb801d0 | 2020-09-16 17:44:58 -0700 | [diff] [blame] | 2579 | LDBG (3, "fds %p, nfds %ld, timeout %d", fds, nfds, timeout); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2580 | |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2581 | if (PREDICT_FALSE (ldpw->clib_time.init_cpu_time == 0)) |
| 2582 | clib_time_init (&ldpw->clib_time); |
| 2583 | |
| 2584 | max_time = (timeout >= 0) ? (f64) timeout / 1000 : 0; |
| 2585 | max_time += clib_time_now (&ldpw->clib_time); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2586 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2587 | for (i = 0; i < nfds; i++) |
| 2588 | { |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2589 | if (fds[i].fd < 0) |
| 2590 | continue; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2591 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2592 | vlsh = ldp_fd_to_vlsh (fds[i].fd); |
| 2593 | if (vlsh != VLS_INVALID_HANDLE) |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2594 | { |
| 2595 | fds[i].fd = -fds[i].fd; |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2596 | vec_add2 (ldpw->vcl_poll, vp, 1); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2597 | vp->fds_ndx = i; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2598 | vp->sh = vlsh_to_sh (vlsh); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2599 | vp->events = fds[i].events; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2600 | #ifdef __USE_XOPEN2K |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2601 | if (fds[i].events & POLLRDNORM) |
| 2602 | vp->events |= POLLIN; |
| 2603 | if (fds[i].events & POLLWRNORM) |
| 2604 | vp->events |= POLLOUT; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2605 | #endif |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2606 | vp->revents = fds[i].revents; |
| 2607 | } |
| 2608 | else |
| 2609 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2610 | vec_add1 (ldpw->libc_poll, fds[i]); |
| 2611 | vec_add1 (ldpw->libc_poll_idxs, i); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2612 | } |
| 2613 | } |
| 2614 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2615 | do |
| 2616 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2617 | if (vec_len (ldpw->vcl_poll)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2618 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2619 | rv = vppcom_poll (ldpw->vcl_poll, vec_len (ldpw->vcl_poll), 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2620 | if (rv < 0) |
| 2621 | { |
| 2622 | errno = -rv; |
| 2623 | rv = -1; |
| 2624 | goto done; |
| 2625 | } |
| 2626 | else |
| 2627 | n_revents += rv; |
| 2628 | } |
| 2629 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2630 | if (vec_len (ldpw->libc_poll)) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2631 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2632 | rv = libc_poll (ldpw->libc_poll, vec_len (ldpw->libc_poll), 0); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2633 | if (rv < 0) |
| 2634 | goto done; |
| 2635 | else |
| 2636 | n_revents += rv; |
| 2637 | } |
| 2638 | |
| 2639 | if (n_revents) |
| 2640 | { |
| 2641 | rv = n_revents; |
| 2642 | goto done; |
| 2643 | } |
| 2644 | } |
Florin Coras | 4dee8cd | 2019-01-29 21:28:16 -0800 | [diff] [blame] | 2645 | while ((timeout < 0) || (clib_time_now (&ldpw->clib_time) < max_time)); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2646 | rv = 0; |
| 2647 | |
| 2648 | done: |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2649 | vec_foreach (vp, ldpw->vcl_poll) |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2650 | { |
| 2651 | fds[vp->fds_ndx].fd = -fds[vp->fds_ndx].fd; |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2652 | fds[vp->fds_ndx].revents = vp->revents; |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2653 | #ifdef __USE_XOPEN2K |
| 2654 | if ((fds[vp->fds_ndx].revents & POLLIN) && |
| 2655 | (fds[vp->fds_ndx].events & POLLRDNORM)) |
| 2656 | fds[vp->fds_ndx].revents |= POLLRDNORM; |
| 2657 | if ((fds[vp->fds_ndx].revents & POLLOUT) && |
| 2658 | (fds[vp->fds_ndx].events & POLLWRNORM)) |
| 2659 | fds[vp->fds_ndx].revents |= POLLWRNORM; |
| 2660 | #endif |
| 2661 | } |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2662 | vec_reset_length (ldpw->vcl_poll); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2663 | |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2664 | for (i = 0; i < vec_len (ldpw->libc_poll); i++) |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2665 | { |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2666 | fds[ldpw->libc_poll_idxs[i]].revents = ldpw->libc_poll[i].revents; |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2667 | } |
Florin Coras | dfe4cf4 | 2018-11-28 22:13:45 -0800 | [diff] [blame] | 2668 | vec_reset_length (ldpw->libc_poll_idxs); |
| 2669 | vec_reset_length (ldpw->libc_poll); |
Florin Coras | 6917b94 | 2018-11-13 22:44:54 -0800 | [diff] [blame] | 2670 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2671 | return rv; |
| 2672 | } |
| 2673 | |
| 2674 | #ifdef USE_GNU |
| 2675 | int |
| 2676 | ppoll (struct pollfd *fds, nfds_t nfds, |
| 2677 | const struct timespec *timeout, const sigset_t * sigmask) |
| 2678 | { |
Florin Coras | 5f33d0d | 2021-06-02 21:22:21 -0700 | [diff] [blame] | 2679 | ldp_init_check (); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2680 | |
| 2681 | clib_warning ("LDP<%d>: LDP-TBD", getpid ()); |
| 2682 | errno = ENOSYS; |
| 2683 | |
| 2684 | |
| 2685 | return -1; |
| 2686 | } |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2687 | #endif |
| 2688 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2689 | void CONSTRUCTOR_ATTRIBUTE ldp_constructor (void); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2690 | |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2691 | void DESTRUCTOR_ATTRIBUTE ldp_destructor (void); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2692 | |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2693 | /* |
| 2694 | * This function is called when the library is loaded |
| 2695 | */ |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2696 | void |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2697 | ldp_constructor (void) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2698 | { |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2699 | swrap_constructor (); |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2700 | if (ldp_init () != 0) |
Florin Coras | d89411e | 2019-03-19 19:44:51 -0700 | [diff] [blame] | 2701 | { |
| 2702 | fprintf (stderr, "\nLDP<%d>: ERROR: ldp_constructor: failed!\n", |
| 2703 | getpid ()); |
| 2704 | _exit (1); |
| 2705 | } |
Dave Wallace | 69d0119 | 2018-02-22 16:22:09 -0500 | [diff] [blame] | 2706 | else if (LDP_DEBUG > 0) |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2707 | clib_warning ("LDP<%d>: LDP constructor: done!\n", getpid ()); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | /* |
| 2711 | * This function is called when the library is unloaded |
| 2712 | */ |
| 2713 | void |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2714 | ldp_destructor (void) |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2715 | { |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 2716 | /* |
| 2717 | swrap_destructor (); |
| 2718 | if (ldp->init) |
| 2719 | ldp->init = 0; |
| 2720 | */ |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2721 | |
| 2722 | /* Don't use clib_warning() here because that calls writev() |
Dave Wallace | 2a86527 | 2018-02-07 21:00:42 -0500 | [diff] [blame] | 2723 | * which will call ldp_init(). |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 2724 | */ |
Dave Wallace | 69d0119 | 2018-02-22 16:22:09 -0500 | [diff] [blame] | 2725 | if (LDP_DEBUG > 0) |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 2726 | fprintf (stderr, "%s:%d: LDP<%d>: LDP destructor: done!\n", |
| 2727 | __func__, __LINE__, getpid ()); |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | |
| 2731 | /* |
| 2732 | * fd.io coding-style-patch-verification: ON |
| 2733 | * |
| 2734 | * Local Variables: |
| 2735 | * eval: (c-set-style "gnu") |
| 2736 | * End: |
| 2737 | */ |