Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1 | /* |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021 Cisco and/or its affiliates. |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this |
| 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 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 16 | /** |
| 17 | * VCL Locked Sessions (VLS) is a wrapper that synchronizes access to VCL APIs |
| 18 | * which are, by construction, not thread safe. To this end, VLS uses |
| 19 | * configuration and heuristics to detect how applications use sessions in |
| 20 | * an attempt to optimize the locking strategy. The modes of operation |
| 21 | * currently supported are the following: |
| 22 | * |
| 23 | * 1) per-process workers |
| 24 | * |
| 25 | * +----------+ +----------+ |
| 26 | * | | | | |
| 27 | * | process0 | | process1 | |
| 28 | * | | | | |
| 29 | * +-----+----+ +-----+----+ |
| 30 | * | | |
| 31 | * | | |
| 32 | * +-----+----+ +-----+----+ |
| 33 | * | | | | |
| 34 | * | vls_wrk0 | | vls_wrk1 | |
| 35 | * | | | | |
| 36 | * +-----+----+ +-----+----+ |
| 37 | * | | |
| 38 | * | | |
| 39 | * +-----+----+ +-----+----+ |
| 40 | * | | | | |
| 41 | * | vcl_wrk0 | | vcl_wrk1 | |
| 42 | * | | | | |
| 43 | * +----------+ +----------+ |
| 44 | * |
| 45 | * 2) per-thread workers 3) single-worker multi-thread |
| 46 | * |
| 47 | * +---------+ +---------+ +---------+ +---------+ |
| 48 | * | | | | | | | | |
| 49 | * | thread0 | | thread1 | | thread0 | | thread1 | |
| 50 | * | | | | | | | | |
| 51 | * +--------++ +-+-------+ +--------++ +-+-------+ |
| 52 | * | | | | |
| 53 | * | | | | |
| 54 | * +-+------+-+ +-+------+-+ |
| 55 | * | | | | |
| 56 | * | vls_wrk0 | | vls_wrk0 | |
| 57 | * | | | | |
| 58 | * +-+------+-+ +----+-----+ |
| 59 | * | | | |
| 60 | * | | | |
| 61 | * +--------+-+ +-+--------+ +----+-----+ |
| 62 | * | | | | | | |
| 63 | * | vcl_wrk0 | | vcl_wrk1 | | vcl_wrk0 | |
| 64 | * | | | | | | |
| 65 | * +----------+ +----------+ +----------+ |
| 66 | * |
| 67 | * 1) per-process workers: intercept fork calls and assume all children |
| 68 | * processes are new workers that must be registered with vcl. VLS |
| 69 | * sessions are cloned and shared between workers. Only shared sessions |
| 70 | * are locked on use and thereby only one process can interact with |
| 71 | * them at a time (explicit sharing). |
| 72 | * |
| 73 | * 2) per-thread workers: each newly detected pthread is assumed to be a new |
| 74 | * worker and is registered with vcl. Enabled via configuration. |
| 75 | * When a thread tries to access a session it does not own, a clone and |
| 76 | * share rpc request is sent to the owning thread via vcl and vpp. |
| 77 | * Consequently, a vls session can map to multiple vcl sessions, one per |
| 78 | * vcl worker. VLS sessions are locked on use (implicit sharing). |
| 79 | * |
| 80 | * 3) single-worker multi-thread: vls does not make any assumptions about |
| 81 | * application threads and therefore implements an aggressive locking |
| 82 | * strategy that limits access to underlying vcl resources based on type |
| 83 | * of interaction and locks vls session on use (implicit sharing). |
| 84 | */ |
| 85 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 86 | #include <vcl/vcl_locked.h> |
| 87 | #include <vcl/vcl_private.h> |
| 88 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 89 | typedef struct vls_shared_data_ |
| 90 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 91 | clib_spinlock_t lock; /**< shared data lock */ |
| 92 | u32 owner_wrk_index; /**< vcl wrk that owns session */ |
| 93 | u32 *workers_subscribed; /**< vec of wrks subscribed to session */ |
| 94 | clib_bitmap_t *listeners; /**< bitmap of wrks actively listening */ |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 95 | } vls_shared_data_t; |
| 96 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 97 | typedef struct vcl_locked_session_ |
| 98 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 99 | clib_spinlock_t lock; /**< vls lock when in use */ |
| 100 | u32 session_index; /**< vcl session index */ |
| 101 | u32 vcl_wrk_index; /**< vcl worker index */ |
| 102 | u32 vls_index; /**< index in vls pool */ |
| 103 | u32 shared_data_index; /**< shared data index if any */ |
| 104 | u32 owner_vcl_wrk_index; /**< vcl wrk of the vls wrk at alloc */ |
| 105 | uword *vcl_wrk_index_to_session_index; /**< map vcl wrk to session */ |
Florin Coras | 978d48b | 2024-11-26 00:29:24 -0800 | [diff] [blame^] | 106 | int libc_epfd; /**< epoll fd for libc epoll */ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 107 | } vcl_locked_session_t; |
| 108 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 109 | typedef struct vls_worker_ |
| 110 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 111 | clib_rwlock_t sh_to_vlsh_table_lock; /**< ht rwlock with mt workers */ |
| 112 | vcl_locked_session_t *vls_pool; /**< pool of vls session */ |
| 113 | uword *sh_to_vlsh_table; /**< map from vcl sh to vls sh */ |
| 114 | u32 *pending_vcl_wrk_cleanup; /**< child vcl wrks to cleanup */ |
| 115 | u32 vcl_wrk_index; /**< if 1:1 map vls to vcl wrk */ |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 116 | } vls_worker_t; |
| 117 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 118 | typedef struct vls_local_ |
| 119 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 120 | int vls_wrk_index; /**< vls wrk index, 1 per process */ |
| 121 | volatile int vls_mt_n_threads; /**< number of threads detected */ |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 122 | clib_rwlock_t vls_pool_lock; /**< per process/wrk vls pool locks */ |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 123 | pthread_mutex_t vls_mt_mq_mlock; /**< vcl mq lock */ |
| 124 | pthread_mutex_t vls_mt_spool_mlock; /**< vcl select or pool lock */ |
| 125 | volatile u8 select_mp_check; /**< flag set if select checks done */ |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 126 | } vls_process_local_t; |
| 127 | |
| 128 | static vls_process_local_t vls_local; |
| 129 | static vls_process_local_t *vlsl = &vls_local; |
| 130 | |
| 131 | typedef struct vls_main_ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 132 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 133 | vls_worker_t *workers; /**< pool of vls workers */ |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 134 | vls_shared_data_t *shared_data_pool; /**< inter proc pool of shared data */ |
| 135 | clib_rwlock_t shared_data_lock; /**< shared data pool lock */ |
| 136 | clib_spinlock_t worker_rpc_lock; /**< lock for inter-worker rpcs */ |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 137 | } vls_main_t; |
| 138 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 139 | vls_main_t *vlsm; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 140 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 141 | typedef enum |
| 142 | { |
| 143 | VLS_RPC_STATE_INIT, |
| 144 | VLS_RPC_STATE_SUCCESS, |
| 145 | VLS_RPC_STATE_SESSION_NOT_EXIST, |
| 146 | } vls_rpc_state_e; |
| 147 | |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 148 | typedef enum vls_rpc_msg_type_ |
| 149 | { |
| 150 | VLS_RPC_CLONE_AND_SHARE, |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 151 | VLS_RPC_SESS_CLEANUP, |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 152 | } vls_rpc_msg_type_e; |
| 153 | |
| 154 | typedef struct vls_rpc_msg_ |
| 155 | { |
| 156 | u8 type; |
| 157 | u8 data[0]; |
| 158 | } vls_rpc_msg_t; |
| 159 | |
| 160 | typedef struct vls_clone_and_share_msg_ |
| 161 | { |
| 162 | u32 vls_index; /**< vls to be shared */ |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 163 | u32 session_index; /**< vcl session to be shared */ |
| 164 | u32 origin_vls_wrk; /**< vls worker that initiated the rpc */ |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 165 | u32 origin_vls_index; /**< vls session of the originator */ |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 166 | u32 origin_vcl_wrk; /**< vcl worker that initiated the rpc */ |
| 167 | u32 origin_session_index; /**< vcl session of the originator */ |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 168 | } vls_clone_and_share_msg_t; |
| 169 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 170 | typedef struct vls_sess_cleanup_msg_ |
| 171 | { |
| 172 | u32 session_index; /**< vcl session to be cleaned */ |
| 173 | u32 origin_vcl_wrk; /**< worker that initiated the rpc */ |
| 174 | } vls_sess_cleanup_msg_t; |
| 175 | |
| 176 | void vls_send_session_cleanup_rpc (vcl_worker_t * wrk, |
| 177 | u32 dst_wrk_index, u32 dst_session_index); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 178 | void vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index, |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 179 | u32 session_index, u32 vls_wrk_index, |
| 180 | u32 dst_wrk_index, u32 dst_vls_index, |
| 181 | u32 dst_session_index); |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 182 | static void vls_cleanup_forked_child (vcl_worker_t *wrk, |
| 183 | vcl_worker_t *child_wrk); |
| 184 | static void vls_handle_pending_wrk_cleanup (void); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 185 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 186 | static inline u32 |
| 187 | vls_get_worker_index (void) |
| 188 | { |
liuyacan | 603e1a4 | 2021-07-22 15:52:01 +0800 | [diff] [blame] | 189 | return vlsl->vls_wrk_index; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static u32 |
| 193 | vls_shared_data_alloc (void) |
| 194 | { |
| 195 | vls_shared_data_t *vls_shd; |
| 196 | u32 shd_index; |
| 197 | |
| 198 | clib_rwlock_writer_lock (&vlsm->shared_data_lock); |
| 199 | pool_get_zero (vlsm->shared_data_pool, vls_shd); |
| 200 | clib_spinlock_init (&vls_shd->lock); |
| 201 | shd_index = vls_shd - vlsm->shared_data_pool; |
| 202 | clib_rwlock_writer_unlock (&vlsm->shared_data_lock); |
| 203 | |
| 204 | return shd_index; |
| 205 | } |
| 206 | |
| 207 | static u32 |
| 208 | vls_shared_data_index (vls_shared_data_t * vls_shd) |
| 209 | { |
| 210 | return vls_shd - vlsm->shared_data_pool; |
| 211 | } |
| 212 | |
| 213 | vls_shared_data_t * |
| 214 | vls_shared_data_get (u32 shd_index) |
| 215 | { |
| 216 | if (pool_is_free_index (vlsm->shared_data_pool, shd_index)) |
| 217 | return 0; |
| 218 | return pool_elt_at_index (vlsm->shared_data_pool, shd_index); |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | vls_shared_data_free (u32 shd_index) |
| 223 | { |
| 224 | vls_shared_data_t *vls_shd; |
| 225 | |
| 226 | clib_rwlock_writer_lock (&vlsm->shared_data_lock); |
| 227 | vls_shd = vls_shared_data_get (shd_index); |
| 228 | clib_spinlock_free (&vls_shd->lock); |
| 229 | clib_bitmap_free (vls_shd->listeners); |
| 230 | vec_free (vls_shd->workers_subscribed); |
| 231 | pool_put (vlsm->shared_data_pool, vls_shd); |
| 232 | clib_rwlock_writer_unlock (&vlsm->shared_data_lock); |
| 233 | } |
| 234 | |
| 235 | static inline void |
| 236 | vls_shared_data_pool_rlock (void) |
| 237 | { |
| 238 | clib_rwlock_reader_lock (&vlsm->shared_data_lock); |
| 239 | } |
| 240 | |
| 241 | static inline void |
| 242 | vls_shared_data_pool_runlock (void) |
| 243 | { |
| 244 | clib_rwlock_reader_unlock (&vlsm->shared_data_lock); |
| 245 | } |
| 246 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 247 | static inline void |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 248 | vls_mt_pool_rlock (void) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 249 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 250 | if (vlsl->vls_mt_n_threads > 1) |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 251 | clib_rwlock_reader_lock (&vlsl->vls_pool_lock); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static inline void |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 255 | vls_mt_pool_runlock (void) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 256 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 257 | if (vlsl->vls_mt_n_threads > 1) |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 258 | clib_rwlock_reader_unlock (&vlsl->vls_pool_lock); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static inline void |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 262 | vls_mt_pool_wlock (void) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 263 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 264 | if (vlsl->vls_mt_n_threads > 1) |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 265 | clib_rwlock_writer_lock (&vlsl->vls_pool_lock); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static inline void |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 269 | vls_mt_pool_wunlock (void) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 270 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 271 | if (vlsl->vls_mt_n_threads > 1) |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 272 | clib_rwlock_writer_unlock (&vlsl->vls_pool_lock); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 275 | typedef enum |
| 276 | { |
| 277 | VLS_MT_OP_READ, |
| 278 | VLS_MT_OP_WRITE, |
| 279 | VLS_MT_OP_SPOOL, |
| 280 | VLS_MT_OP_XPOLL, |
| 281 | } vls_mt_ops_t; |
| 282 | |
| 283 | typedef enum |
| 284 | { |
| 285 | VLS_MT_LOCK_MQ = 1 << 0, |
| 286 | VLS_MT_LOCK_SPOOL = 1 << 1 |
| 287 | } vls_mt_lock_type_t; |
| 288 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 289 | static void |
| 290 | vls_mt_add (void) |
| 291 | { |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 292 | vlsl->vls_mt_n_threads += 1; |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 293 | |
| 294 | /* If multi-thread workers are supported, for each new thread register a new |
| 295 | * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so |
| 296 | * update the vcl worker's thread local worker index variable */ |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 297 | if (vls_mt_wrk_supported ()) |
wanghanlin | 492350e | 2020-09-11 17:19:32 +0800 | [diff] [blame] | 298 | { |
| 299 | if (vppcom_worker_register () != VPPCOM_OK) |
| 300 | VERR ("failed to register worker"); |
| 301 | } |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 302 | else |
| 303 | vcl_set_worker_index (vlsl->vls_wrk_index); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static inline void |
| 307 | vls_mt_mq_lock (void) |
| 308 | { |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 309 | pthread_mutex_lock (&vlsl->vls_mt_mq_mlock); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static inline void |
| 313 | vls_mt_mq_unlock (void) |
| 314 | { |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 315 | pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static inline void |
| 319 | vls_mt_spool_lock (void) |
| 320 | { |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 321 | pthread_mutex_lock (&vlsl->vls_mt_spool_mlock); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static inline void |
| 325 | vls_mt_create_unlock (void) |
| 326 | { |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 327 | pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock); |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | vls_mt_locks_init (void) |
| 332 | { |
| 333 | pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL); |
| 334 | pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 337 | u8 |
| 338 | vls_is_shared (vcl_locked_session_t * vls) |
| 339 | { |
| 340 | return (vls->shared_data_index != ~0); |
| 341 | } |
| 342 | |
| 343 | static inline void |
| 344 | vls_lock (vcl_locked_session_t * vls) |
| 345 | { |
| 346 | if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls)) |
| 347 | clib_spinlock_lock (&vls->lock); |
| 348 | } |
| 349 | |
| 350 | static inline void |
| 351 | vls_unlock (vcl_locked_session_t * vls) |
| 352 | { |
| 353 | if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls)) |
| 354 | clib_spinlock_unlock (&vls->lock); |
| 355 | } |
| 356 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 357 | static inline vcl_session_handle_t |
| 358 | vls_to_sh (vcl_locked_session_t * vls) |
| 359 | { |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 360 | return vcl_session_handle_from_index (vls->session_index); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static inline vcl_session_handle_t |
| 364 | vls_to_sh_tu (vcl_locked_session_t * vls) |
| 365 | { |
| 366 | vcl_session_handle_t sh; |
| 367 | sh = vls_to_sh (vls); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 368 | vls_mt_pool_runlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 369 | return sh; |
| 370 | } |
| 371 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 372 | static vls_worker_t * |
| 373 | vls_worker_get_current (void) |
| 374 | { |
| 375 | return pool_elt_at_index (vlsm->workers, vls_get_worker_index ()); |
| 376 | } |
| 377 | |
Mohamed Feroz | 88c836c | 2024-08-29 08:04:02 +0000 | [diff] [blame] | 378 | static inline u8 |
| 379 | vls_n_workers (void) |
| 380 | { |
| 381 | return pool_elts (vlsm->workers); |
| 382 | } |
| 383 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 384 | static void |
| 385 | vls_worker_alloc (void) |
| 386 | { |
| 387 | vls_worker_t *wrk; |
| 388 | |
| 389 | pool_get_zero (vlsm->workers, wrk); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 390 | if (vls_mt_wrk_supported ()) |
| 391 | clib_rwlock_init (&wrk->sh_to_vlsh_table_lock); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 392 | wrk->vcl_wrk_index = vcl_get_worker_index (); |
| 393 | vec_validate (wrk->pending_vcl_wrk_cleanup, 16); |
| 394 | vec_reset_length (wrk->pending_vcl_wrk_cleanup); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static void |
| 398 | vls_worker_free (vls_worker_t * wrk) |
| 399 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 400 | hash_free (wrk->sh_to_vlsh_table); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 401 | if (vls_mt_wrk_supported ()) |
| 402 | clib_rwlock_free (&wrk->sh_to_vlsh_table_lock); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 403 | pool_free (wrk->vls_pool); |
| 404 | pool_put (vlsm->workers, wrk); |
| 405 | } |
| 406 | |
| 407 | static vls_worker_t * |
| 408 | vls_worker_get (u32 wrk_index) |
| 409 | { |
| 410 | if (pool_is_free_index (vlsm->workers, wrk_index)) |
| 411 | return 0; |
| 412 | return pool_elt_at_index (vlsm->workers, wrk_index); |
| 413 | } |
| 414 | |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 415 | static void |
| 416 | vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh) |
| 417 | { |
| 418 | if (vls_mt_wrk_supported ()) |
| 419 | clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 420 | hash_set (wrk->sh_to_vlsh_table, sh, vlsh); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 421 | if (vls_mt_wrk_supported ()) |
| 422 | clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock); |
| 423 | } |
| 424 | |
| 425 | static void |
| 426 | vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh) |
| 427 | { |
| 428 | if (vls_mt_wrk_supported ()) |
| 429 | clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 430 | hash_unset (wrk->sh_to_vlsh_table, sh); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 431 | if (vls_mt_wrk_supported ()) |
| 432 | clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock); |
| 433 | } |
| 434 | |
| 435 | static uword * |
| 436 | vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh) |
| 437 | { |
| 438 | if (vls_mt_wrk_supported ()) |
| 439 | clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 440 | uword *vlshp = hash_get (wrk->sh_to_vlsh_table, sh); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 441 | if (vls_mt_wrk_supported ()) |
| 442 | clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock); |
| 443 | return vlshp; |
| 444 | } |
| 445 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 446 | static vls_handle_t |
| 447 | vls_alloc (vcl_session_handle_t sh) |
| 448 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 449 | vls_worker_t *wrk = vls_worker_get_current (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 450 | vcl_locked_session_t *vls; |
| 451 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 452 | vls_mt_pool_wlock (); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 453 | |
| 454 | pool_get_zero (wrk->vls_pool, vls); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 455 | vls->session_index = vppcom_session_index (sh); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 456 | vls->vcl_wrk_index = vppcom_session_worker (sh); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 457 | vls->vls_index = vls - wrk->vls_pool; |
| 458 | vls->shared_data_index = ~0; |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 459 | vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 460 | if (vls_mt_wrk_supported ()) |
| 461 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 462 | hash_set (vls->vcl_wrk_index_to_session_index, vls->vcl_wrk_index, |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 463 | vls->session_index); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 464 | vls->owner_vcl_wrk_index = vls->vcl_wrk_index; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 465 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 466 | clib_spinlock_init (&vls->lock); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 467 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 468 | vls_mt_pool_wunlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 469 | return vls->vls_index; |
| 470 | } |
| 471 | |
| 472 | static vcl_locked_session_t * |
| 473 | vls_get (vls_handle_t vlsh) |
| 474 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 475 | vls_worker_t *wrk = vls_worker_get_current (); |
| 476 | if (pool_is_free_index (wrk->vls_pool, vlsh)) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 477 | return 0; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 478 | return pool_elt_at_index (wrk->vls_pool, vlsh); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 482 | vls_free (vcl_locked_session_t * vls) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 483 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 484 | vls_worker_t *wrk = vls_worker_get_current (); |
| 485 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 486 | ASSERT (vls != 0); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 487 | vls_sh_to_vlsh_table_del ( |
| 488 | wrk, vcl_session_handle_from_index (vls->session_index)); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 489 | clib_spinlock_free (&vls->lock); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 490 | pool_put (wrk->vls_pool, vls); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static vcl_locked_session_t * |
| 494 | vls_get_and_lock (vls_handle_t vlsh) |
| 495 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 496 | vls_worker_t *wrk = vls_worker_get_current (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 497 | vcl_locked_session_t *vls; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 498 | if (pool_is_free_index (wrk->vls_pool, vlsh)) |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 499 | return 0; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 500 | vls = pool_elt_at_index (wrk->vls_pool, vlsh); |
| 501 | vls_lock (vls); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 502 | return vls; |
| 503 | } |
| 504 | |
| 505 | static vcl_locked_session_t * |
| 506 | vls_get_w_dlock (vls_handle_t vlsh) |
| 507 | { |
| 508 | vcl_locked_session_t *vls; |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 509 | vls_mt_pool_rlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 510 | vls = vls_get_and_lock (vlsh); |
| 511 | if (!vls) |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 512 | vls_mt_pool_runlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 513 | return vls; |
| 514 | } |
| 515 | |
| 516 | static inline void |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 517 | vls_get_and_unlock (vls_handle_t vlsh) |
| 518 | { |
| 519 | vcl_locked_session_t *vls; |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 520 | vls_mt_pool_rlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 521 | vls = vls_get (vlsh); |
| 522 | vls_unlock (vls); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 523 | vls_mt_pool_runlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static inline void |
| 527 | vls_dunlock (vcl_locked_session_t * vls) |
| 528 | { |
| 529 | vls_unlock (vls); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 530 | vls_mt_pool_runlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 533 | static vcl_locked_session_t * |
| 534 | vls_session_get (vls_worker_t * wrk, u32 vls_index) |
| 535 | { |
| 536 | if (pool_is_free_index (wrk->vls_pool, vls_index)) |
| 537 | return 0; |
| 538 | return pool_elt_at_index (wrk->vls_pool, vls_index); |
| 539 | } |
| 540 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 541 | vcl_session_handle_t |
| 542 | vlsh_to_sh (vls_handle_t vlsh) |
| 543 | { |
| 544 | vcl_locked_session_t *vls; |
| 545 | int rv; |
| 546 | |
| 547 | vls = vls_get_w_dlock (vlsh); |
| 548 | if (!vls) |
| 549 | return INVALID_SESSION_ID; |
| 550 | rv = vls_to_sh (vls); |
| 551 | vls_dunlock (vls); |
| 552 | return rv; |
| 553 | } |
| 554 | |
| 555 | vcl_session_handle_t |
| 556 | vlsh_to_session_index (vls_handle_t vlsh) |
| 557 | { |
| 558 | vcl_session_handle_t sh; |
| 559 | sh = vlsh_to_sh (vlsh); |
| 560 | return vppcom_session_index (sh); |
| 561 | } |
| 562 | |
Florin Coras | ce4635c | 2024-10-23 14:19:31 -0700 | [diff] [blame] | 563 | int |
| 564 | vlsh_to_worker_index (vls_handle_t vlsh) |
| 565 | { |
| 566 | vcl_locked_session_t *vls; |
| 567 | u32 wrk_index; |
| 568 | |
| 569 | vls = vls_get_w_dlock (vlsh); |
| 570 | if (!vls) |
| 571 | wrk_index = INVALID_SESSION_ID; |
| 572 | else |
| 573 | wrk_index = vls->vcl_wrk_index; |
| 574 | vls_dunlock (vls); |
| 575 | |
| 576 | return wrk_index; |
| 577 | } |
| 578 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 579 | vls_handle_t |
hanlin | f8e1363 | 2020-08-21 11:05:36 +0800 | [diff] [blame] | 580 | vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index) |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 581 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 582 | vls_worker_t *wrk = vls_worker_get_current (); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 583 | uword *vlshp = vls_sh_to_vlsh_table_get ( |
| 584 | wrk, |
| 585 | vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index)); |
| 586 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 587 | return vlshp ? *vlshp : VLS_INVALID_HANDLE; |
| 588 | } |
| 589 | |
| 590 | vls_handle_t |
| 591 | vls_session_index_to_vlsh (uint32_t session_index) |
| 592 | { |
| 593 | vls_handle_t vlsh; |
| 594 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 595 | vls_mt_pool_rlock (); |
hanlin | f8e1363 | 2020-08-21 11:05:36 +0800 | [diff] [blame] | 596 | vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ()); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 597 | vls_mt_pool_runlock (); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 598 | |
| 599 | return vlsh; |
| 600 | } |
| 601 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 602 | u8 |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 603 | vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index) |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 604 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 605 | vls_shared_data_t *vls_shd; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 606 | int i; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 607 | |
| 608 | if (vls->shared_data_index == ~0) |
| 609 | return 0; |
| 610 | |
| 611 | vls_shared_data_pool_rlock (); |
| 612 | |
| 613 | vls_shd = vls_shared_data_get (vls->shared_data_index); |
| 614 | clib_spinlock_lock (&vls_shd->lock); |
| 615 | |
| 616 | for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++) |
| 617 | if (vls_shd->workers_subscribed[i] == wrk_index) |
| 618 | { |
| 619 | clib_spinlock_unlock (&vls_shd->lock); |
| 620 | vls_shared_data_pool_runlock (); |
| 621 | return 1; |
| 622 | } |
| 623 | clib_spinlock_unlock (&vls_shd->lock); |
| 624 | |
| 625 | vls_shared_data_pool_runlock (); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 629 | static void |
| 630 | vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active) |
| 631 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 632 | vls_shared_data_t *vls_shd; |
| 633 | |
| 634 | if (vls->shared_data_index == ~0) |
| 635 | { |
| 636 | clib_warning ("not a shared session"); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | vls_shared_data_pool_rlock (); |
| 641 | |
| 642 | vls_shd = vls_shared_data_get (vls->shared_data_index); |
| 643 | |
| 644 | clib_spinlock_lock (&vls_shd->lock); |
Florin Coras | 674d565 | 2021-12-03 23:02:13 -0800 | [diff] [blame] | 645 | vls_shd->listeners = |
| 646 | clib_bitmap_set (vls_shd->listeners, wrk_index, is_active); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 647 | clib_spinlock_unlock (&vls_shd->lock); |
| 648 | |
| 649 | vls_shared_data_pool_runlock (); |
| 650 | } |
| 651 | |
| 652 | static u32 |
| 653 | vls_shared_get_owner (vcl_locked_session_t * vls) |
| 654 | { |
| 655 | vls_shared_data_t *vls_shd; |
| 656 | u32 owner_wrk; |
| 657 | |
| 658 | vls_shared_data_pool_rlock (); |
| 659 | |
| 660 | vls_shd = vls_shared_data_get (vls->shared_data_index); |
| 661 | owner_wrk = vls_shd->owner_wrk_index; |
| 662 | |
| 663 | vls_shared_data_pool_runlock (); |
| 664 | |
| 665 | return owner_wrk; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static u8 |
| 669 | vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index) |
| 670 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 671 | vls_shared_data_t *vls_shd; |
| 672 | u8 is_set; |
| 673 | |
| 674 | if (vls->shared_data_index == ~0) |
| 675 | { |
| 676 | clib_warning ("not a shared session"); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | vls_shared_data_pool_rlock (); |
| 681 | |
| 682 | vls_shd = vls_shared_data_get (vls->shared_data_index); |
| 683 | |
| 684 | clib_spinlock_lock (&vls_shd->lock); |
| 685 | is_set = clib_bitmap_get (vls_shd->listeners, wrk_index); |
| 686 | clib_spinlock_unlock (&vls_shd->lock); |
| 687 | |
| 688 | vls_shared_data_pool_runlock (); |
| 689 | |
| 690 | return (is_set == 1); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static void |
| 694 | vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index) |
| 695 | { |
Florin Coras | 3288193 | 2023-02-06 13:30:13 -0800 | [diff] [blame] | 696 | vcl_worker_t *wrk; |
| 697 | vcl_session_t *ls; |
| 698 | |
| 699 | wrk = vcl_worker_get (wrk_index); |
| 700 | ls = vcl_session_get (wrk, vls->session_index); |
| 701 | |
| 702 | /* Listen request already sent */ |
| 703 | if (ls->flags & VCL_SESSION_F_PENDING_LISTEN) |
| 704 | return; |
| 705 | |
| 706 | vcl_send_session_listen (wrk, ls); |
| 707 | |
| 708 | vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static void |
| 712 | vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index) |
| 713 | { |
| 714 | vcl_worker_t *wrk; |
| 715 | vcl_session_t *s; |
| 716 | |
| 717 | wrk = vcl_worker_get (wrk_index); |
| 718 | s = vcl_session_get (wrk, vls->session_index); |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 719 | if (s->session_state != VCL_STATE_LISTEN) |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 720 | return; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 721 | vcl_send_session_unlisten (wrk, s); |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 722 | s->session_state = VCL_STATE_LISTEN_NO_MQ; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 723 | vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ ); |
| 724 | } |
| 725 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 726 | static int |
| 727 | vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd, |
| 728 | u32 wrk_index) |
| 729 | { |
| 730 | int i; |
| 731 | |
| 732 | for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++) |
| 733 | { |
| 734 | if (vls_shd->workers_subscribed[i] == wrk_index) |
| 735 | return i; |
| 736 | } |
| 737 | return -1; |
| 738 | } |
| 739 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 740 | int |
| 741 | vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk) |
| 742 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 743 | vls_shared_data_t *vls_shd; |
Florin Coras | 311817f | 2020-03-07 17:45:47 +0000 | [diff] [blame] | 744 | int do_disconnect, pos; |
| 745 | u32 n_subscribers; |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 746 | vcl_session_t *s; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 747 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 748 | if (vls->shared_data_index == ~0) |
| 749 | return 0; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 750 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 751 | s = vcl_session_get (wrk, vls->session_index); |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 752 | if (s->session_state == VCL_STATE_LISTEN) |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 753 | vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ ); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 754 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 755 | vls_shared_data_pool_rlock (); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 756 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 757 | vls_shd = vls_shared_data_get (vls->shared_data_index); |
| 758 | clib_spinlock_lock (&vls_shd->lock); |
| 759 | |
| 760 | pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index); |
| 761 | if (pos < 0) |
| 762 | { |
| 763 | clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index, |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 764 | vls->vcl_wrk_index); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 765 | goto done; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Unsubscribe from share data and fifos |
| 770 | */ |
| 771 | if (s->rx_fifo) |
| 772 | { |
| 773 | svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index); |
| 774 | svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index); |
| 775 | } |
| 776 | vec_del1 (vls_shd->workers_subscribed, pos); |
| 777 | |
| 778 | /* |
| 779 | * Cleanup vcl state |
| 780 | */ |
| 781 | n_subscribers = vec_len (vls_shd->workers_subscribed); |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 782 | do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 783 | vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect); |
| 784 | |
| 785 | /* |
| 786 | * No subscriber left, cleanup shared data |
| 787 | */ |
| 788 | if (!n_subscribers) |
| 789 | { |
| 790 | u32 shd_index = vls_shared_data_index (vls_shd); |
| 791 | |
| 792 | clib_spinlock_unlock (&vls_shd->lock); |
| 793 | vls_shared_data_pool_runlock (); |
| 794 | |
| 795 | vls_shared_data_free (shd_index); |
| 796 | |
| 797 | /* All locks have been dropped */ |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 798 | return 0; |
| 799 | } |
| 800 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 801 | /* Return, if this is not the owning worker */ |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 802 | if (vls_shd->owner_wrk_index != wrk->wrk_index) |
| 803 | goto done; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 804 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 805 | ASSERT (vec_len (vls_shd->workers_subscribed)); |
| 806 | |
| 807 | /* |
| 808 | * Check if we can change owner or close |
| 809 | */ |
| 810 | vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0]; |
liuyacan | 603e1a4 | 2021-07-22 15:52:01 +0800 | [diff] [blame] | 811 | if (s->vpp_evt_q) |
liuyacan | 5cac2e8 | 2021-07-14 15:53:01 +0800 | [diff] [blame] | 812 | vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 813 | |
| 814 | /* XXX is this still needed? */ |
| 815 | if (vec_len (vls_shd->workers_subscribed) > 1) |
| 816 | clib_warning ("more workers need to be updated"); |
| 817 | |
| 818 | done: |
| 819 | |
| 820 | clib_spinlock_unlock (&vls_shd->lock); |
| 821 | vls_shared_data_pool_runlock (); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | void |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 827 | vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls) |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 828 | { |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 829 | vls_shared_data_t *vls_shd; |
| 830 | |
| 831 | u32 vls_shd_index = vls_shared_data_alloc (); |
| 832 | |
| 833 | vls_shared_data_pool_rlock (); |
| 834 | |
| 835 | vls_shd = vls_shared_data_get (vls_shd_index); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 836 | vls_shd->owner_wrk_index = vls_wrk->vcl_wrk_index; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 837 | vls->shared_data_index = vls_shd_index; |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 838 | vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index); |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 839 | |
| 840 | vls_shared_data_pool_runlock (); |
| 841 | } |
| 842 | |
| 843 | void |
| 844 | vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls) |
| 845 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 846 | vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->vcl_wrk_index); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 847 | vls_shared_data_t *vls_shd; |
| 848 | vcl_session_t *s; |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 849 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 850 | s = vcl_session_get (vcl_wrk, vls->session_index); |
| 851 | if (!s) |
| 852 | { |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 853 | clib_warning ("wrk %u session %u vls %u NOT AVAILABLE", |
| 854 | vcl_wrk->wrk_index, vls->session_index, vls->vls_index); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 855 | return; |
| 856 | } |
| 857 | |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 858 | ASSERT (vls->shared_data_index != ~0); |
| 859 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 860 | /* Reinit session lock */ |
| 861 | clib_spinlock_init (&vls->lock); |
| 862 | |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 863 | vls_shared_data_pool_rlock (); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 864 | |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 865 | vls_shd = vls_shared_data_get (vls->shared_data_index); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 866 | |
| 867 | clib_spinlock_lock (&vls_shd->lock); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 868 | vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 869 | clib_spinlock_unlock (&vls_shd->lock); |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 870 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 871 | vls_shared_data_pool_runlock (); |
| 872 | |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 873 | if (s->session_state == VCL_STATE_LISTEN) |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 874 | { |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 875 | s->session_state = VCL_STATE_LISTEN_NO_MQ; |
Florin Coras | 7428eaa | 2023-12-11 16:04:57 -0800 | [diff] [blame] | 876 | s->rx_fifo = s->tx_fifo = 0; |
| 877 | } |
| 878 | else if (s->rx_fifo) |
| 879 | { |
| 880 | vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 881 | } |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 882 | } |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 883 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 884 | static void |
| 885 | vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk) |
| 886 | { |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 887 | vcl_locked_session_t *vls, *parent_vls; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 888 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 889 | pool_foreach (vls, vls_wrk->vls_pool) { |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 890 | /* Initialize sharing on parent session */ |
| 891 | if (vls->shared_data_index == ~0) |
| 892 | { |
| 893 | parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index); |
| 894 | vls_init_share_session (vls_parent_wrk, parent_vls); |
| 895 | vls->shared_data_index = parent_vls->shared_data_index; |
| 896 | } |
| 897 | vls_share_session (vls_wrk, vls); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 898 | } |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 899 | } |
| 900 | |
wanghanlin | 5788a34 | 2021-06-22 17:34:08 +0800 | [diff] [blame] | 901 | static void |
| 902 | vls_validate_veps (vcl_worker_t *wrk) |
| 903 | { |
| 904 | vcl_session_t *s; |
| 905 | u32 session_index, wrk_index; |
| 906 | |
| 907 | pool_foreach (s, wrk->sessions) |
| 908 | { |
| 909 | if (s->vep.vep_sh != ~0) |
| 910 | { |
| 911 | vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index); |
| 912 | s->vep.vep_sh = vcl_session_handle_from_index (session_index); |
| 913 | } |
| 914 | if (s->vep.next_sh != ~0) |
| 915 | { |
| 916 | vcl_session_handle_parse (s->vep.next_sh, &wrk_index, |
| 917 | &session_index); |
| 918 | s->vep.next_sh = vcl_session_handle_from_index (session_index); |
| 919 | } |
| 920 | if (s->vep.prev_sh != ~0) |
| 921 | { |
| 922 | vcl_session_handle_parse (s->vep.prev_sh, &wrk_index, |
| 923 | &session_index); |
| 924 | s->vep.prev_sh = vcl_session_handle_from_index (session_index); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 929 | void |
| 930 | vls_worker_copy_on_fork (vcl_worker_t * parent_wrk) |
| 931 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 932 | vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk; |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 933 | vcl_worker_t *vcl_wrk = vcl_worker_get_current (); |
hanlin | f8e1363 | 2020-08-21 11:05:36 +0800 | [diff] [blame] | 934 | u32 vls_index, session_index, wrk_index; |
| 935 | vcl_session_handle_t sh; |
liuyacan | 603e1a4 | 2021-07-22 15:52:01 +0800 | [diff] [blame] | 936 | vcl_locked_session_t *vls; |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 937 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 938 | /* |
| 939 | * init vcl worker |
| 940 | */ |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 941 | vcl_wrk->sessions = pool_dup (parent_wrk->sessions); |
| 942 | vcl_wrk->session_index_by_vpp_handles = |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 943 | hash_dup (parent_wrk->session_index_by_vpp_handles); |
| 944 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 945 | /* |
| 946 | * init vls worker |
| 947 | */ |
| 948 | vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 949 | |
| 950 | /* clang-format off */ |
| 951 | hash_foreach (sh, vls_index, vls_parent_wrk->sh_to_vlsh_table, ({ |
| 952 | vcl_session_handle_parse (sh, &wrk_index, &session_index); |
| 953 | hash_set (vls_wrk->sh_to_vlsh_table, |
| 954 | vcl_session_handle_from_index (session_index), vls_index); |
| 955 | })); |
| 956 | /* clang-format on */ |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 957 | vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 958 | |
liuyacan | 603e1a4 | 2021-07-22 15:52:01 +0800 | [diff] [blame] | 959 | /* |
| 960 | * Detach vls from parent vcl worker and attach them to child. |
| 961 | */ |
| 962 | pool_foreach (vls, vls_wrk->vls_pool) |
| 963 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 964 | vls->vcl_wrk_index = vcl_wrk->wrk_index; |
liuyacan | 603e1a4 | 2021-07-22 15:52:01 +0800 | [diff] [blame] | 965 | } |
| 966 | |
wanghanlin | 5788a34 | 2021-06-22 17:34:08 +0800 | [diff] [blame] | 967 | /* Validate vep's handle */ |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 968 | vls_validate_veps (vcl_wrk); |
wanghanlin | 5788a34 | 2021-06-22 17:34:08 +0800 | [diff] [blame] | 969 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 970 | vls_share_sessions (vls_parent_wrk, vls_wrk); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 973 | static void |
| 974 | vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq) |
| 975 | { |
| 976 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 977 | vcl_session_t *s = 0; |
| 978 | int is_nonblk = 0; |
| 979 | |
| 980 | if (vls) |
| 981 | { |
| 982 | s = vcl_session_get (wrk, vls->session_index); |
| 983 | if (PREDICT_FALSE (!s)) |
| 984 | return; |
Florin Coras | ac422d6 | 2020-10-19 20:51:36 -0700 | [diff] [blame] | 985 | is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | switch (op) |
| 989 | { |
| 990 | case VLS_MT_OP_READ: |
| 991 | if (!is_nonblk) |
| 992 | is_nonblk = vcl_session_read_ready (s) != 0; |
| 993 | if (!is_nonblk) |
| 994 | { |
| 995 | vls_mt_mq_lock (); |
| 996 | *locks_acq |= VLS_MT_LOCK_MQ; |
| 997 | } |
| 998 | break; |
| 999 | case VLS_MT_OP_WRITE: |
Florin Coras | 78b5fa6 | 2019-02-21 20:04:15 -0800 | [diff] [blame] | 1000 | ASSERT (s); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1001 | if (!is_nonblk) |
| 1002 | is_nonblk = vcl_session_write_ready (s) != 0; |
| 1003 | if (!is_nonblk) |
| 1004 | { |
| 1005 | vls_mt_mq_lock (); |
| 1006 | *locks_acq |= VLS_MT_LOCK_MQ; |
| 1007 | } |
| 1008 | break; |
| 1009 | case VLS_MT_OP_XPOLL: |
| 1010 | vls_mt_mq_lock (); |
| 1011 | *locks_acq |= VLS_MT_LOCK_MQ; |
| 1012 | break; |
| 1013 | case VLS_MT_OP_SPOOL: |
| 1014 | vls_mt_spool_lock (); |
| 1015 | *locks_acq |= VLS_MT_LOCK_SPOOL; |
| 1016 | break; |
| 1017 | default: |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | static void |
| 1023 | vls_mt_rel_locks (int locks_acq) |
| 1024 | { |
| 1025 | if (locks_acq & VLS_MT_LOCK_MQ) |
| 1026 | vls_mt_mq_unlock (); |
| 1027 | if (locks_acq & VLS_MT_LOCK_SPOOL) |
| 1028 | vls_mt_create_unlock (); |
| 1029 | } |
| 1030 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1031 | static inline u8 |
| 1032 | vls_mt_session_should_migrate (vcl_locked_session_t * vls) |
| 1033 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1034 | return (vls_mt_wrk_supported () && |
| 1035 | vls->vcl_wrk_index != vcl_get_worker_index ()); |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1038 | static vcl_locked_session_t * |
| 1039 | vls_mt_session_migrate (vcl_locked_session_t *vls) |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1040 | { |
| 1041 | u32 wrk_index = vcl_get_worker_index (); |
| 1042 | vcl_worker_t *wrk; |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 1043 | vls_worker_t *vls_wrk = vls_worker_get_current (); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1044 | u32 src_sid, sid, vls_index, own_vcl_wrk_index; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1045 | vcl_session_t *session; |
| 1046 | uword *p; |
| 1047 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1048 | ASSERT (vls_mt_wrk_supported () && vls->vcl_wrk_index != wrk_index); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1049 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1050 | /* |
| 1051 | * VCL session on current vcl worker already allocated. Update current |
| 1052 | * owner worker and index and return |
| 1053 | */ |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1054 | if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index))) |
| 1055 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1056 | vls->vcl_wrk_index = wrk_index; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1057 | vls->session_index = (u32) p[0]; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1058 | return vls; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1061 | /* |
| 1062 | * Ask vcl worker that owns the original vcl session to clone it into |
| 1063 | * current vcl worker session pool |
| 1064 | */ |
| 1065 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1066 | if (!(p = hash_get (vls->vcl_wrk_index_to_session_index, |
| 1067 | vls->owner_vcl_wrk_index))) |
| 1068 | { |
| 1069 | VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index); |
| 1070 | ASSERT (0); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1071 | vls_unlock (vls); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1072 | vls_mt_pool_runlock (); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1073 | return 0; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | src_sid = (u32) p[0]; |
| 1077 | wrk = vcl_worker_get_current (); |
| 1078 | session = vcl_session_alloc (wrk); |
| 1079 | sid = session->session_index; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1080 | VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)", |
| 1081 | vls->owner_vcl_wrk_index, src_sid, wrk_index, sid); |
| 1082 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1083 | /* Drop lock to prevent dead lock when dst wrk trying to get lock. */ |
| 1084 | vls_index = vls->vls_index; |
| 1085 | own_vcl_wrk_index = vls->owner_vcl_wrk_index; |
| 1086 | vls_unlock (vls); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1087 | vls_mt_pool_runlock (); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1088 | vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (), |
| 1089 | own_vcl_wrk_index, vls_index, src_sid); |
| 1090 | |
| 1091 | if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST)) |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1092 | { |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1093 | VWRN ("session %u not exist", src_sid); |
| 1094 | goto err; |
| 1095 | } |
| 1096 | else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT)) |
| 1097 | { |
| 1098 | VWRN ("failed to wait rpc response"); |
| 1099 | goto err; |
| 1100 | } |
| 1101 | else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) && |
| 1102 | session->vep.next_sh != ~0)) |
| 1103 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1104 | VERR ("can't migrate nonempty epoll session"); |
| 1105 | ASSERT (0); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1106 | goto err; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1107 | } |
Florin Coras | 6c3b218 | 2020-10-19 18:36:48 -0700 | [diff] [blame] | 1108 | else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) && |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 1109 | session->session_state != VCL_STATE_CLOSED)) |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1110 | { |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1111 | VERR ("migrate NOT supported, session_status (%u)", |
| 1112 | session->session_state); |
| 1113 | ASSERT (0); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1114 | goto err; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1115 | } |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1116 | |
| 1117 | vls = vls_get_w_dlock (vls_index); |
| 1118 | if (PREDICT_FALSE (!vls)) |
| 1119 | { |
| 1120 | VWRN ("failed to get vls %u", vls_index); |
| 1121 | goto err; |
| 1122 | } |
| 1123 | |
| 1124 | session->session_index = sid; |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1125 | vls->vcl_wrk_index = wrk_index; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1126 | vls->session_index = sid; |
| 1127 | hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 1128 | vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session), |
| 1129 | vls->vls_index); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1130 | return vls; |
| 1131 | |
| 1132 | err: |
| 1133 | vcl_session_free (wrk, session); |
| 1134 | return 0; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1137 | static inline void |
| 1138 | vls_mt_detect (void) |
| 1139 | { |
| 1140 | if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) |
| 1141 | vls_mt_add (); |
| 1142 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1143 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1144 | #define vls_mt_guard(_vls, _op) \ |
| 1145 | int _locks_acq = 0; \ |
| 1146 | if (vls_mt_wrk_supported ()) \ |
| 1147 | { \ |
| 1148 | if (PREDICT_FALSE (_vls && \ |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1149 | ((vcl_locked_session_t *) _vls)->vcl_wrk_index != \ |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1150 | vcl_get_worker_index ())) \ |
| 1151 | { \ |
| 1152 | _vls = vls_mt_session_migrate (_vls); \ |
| 1153 | if (PREDICT_FALSE (!_vls)) \ |
| 1154 | return VPPCOM_EBADFD; \ |
| 1155 | } \ |
| 1156 | } \ |
| 1157 | else \ |
| 1158 | { \ |
| 1159 | if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \ |
| 1160 | vls_mt_acq_locks (_vls, _op, &_locks_acq); \ |
| 1161 | } |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1162 | |
| 1163 | #define vls_mt_unguard() \ |
| 1164 | if (PREDICT_FALSE (_locks_acq)) \ |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1165 | vls_mt_rel_locks (_locks_acq) |
| 1166 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1167 | int |
| 1168 | vls_write (vls_handle_t vlsh, void *buf, size_t nbytes) |
| 1169 | { |
| 1170 | vcl_locked_session_t *vls; |
| 1171 | int rv; |
| 1172 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1173 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1174 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1175 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1176 | |
| 1177 | vls_mt_guard (vls, VLS_MT_OP_WRITE); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1178 | rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1179 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1180 | vls_get_and_unlock (vlsh); |
| 1181 | return rv; |
| 1182 | } |
| 1183 | |
| 1184 | int |
| 1185 | vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes) |
| 1186 | { |
| 1187 | vcl_locked_session_t *vls; |
| 1188 | int rv; |
| 1189 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1190 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1191 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1192 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1193 | vls_mt_guard (vls, VLS_MT_OP_WRITE); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1194 | rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1195 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1196 | vls_get_and_unlock (vlsh); |
| 1197 | return rv; |
| 1198 | } |
| 1199 | |
| 1200 | int |
| 1201 | vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags, |
| 1202 | vppcom_endpt_t * ep) |
| 1203 | { |
| 1204 | vcl_locked_session_t *vls; |
| 1205 | int rv; |
| 1206 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1207 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1208 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1209 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1210 | vls_mt_guard (vls, VLS_MT_OP_WRITE); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1211 | rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1212 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1213 | vls_get_and_unlock (vlsh); |
| 1214 | return rv; |
| 1215 | } |
| 1216 | |
| 1217 | ssize_t |
| 1218 | vls_read (vls_handle_t vlsh, void *buf, size_t nbytes) |
| 1219 | { |
| 1220 | vcl_locked_session_t *vls; |
| 1221 | int rv; |
| 1222 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1223 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1224 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1225 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1226 | vls_mt_guard (vls, VLS_MT_OP_READ); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1227 | rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1228 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1229 | vls_get_and_unlock (vlsh); |
| 1230 | return rv; |
| 1231 | } |
| 1232 | |
| 1233 | ssize_t |
| 1234 | vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags, |
| 1235 | vppcom_endpt_t * ep) |
| 1236 | { |
| 1237 | vcl_locked_session_t *vls; |
| 1238 | int rv; |
| 1239 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1240 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1241 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1242 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1243 | vls_mt_guard (vls, VLS_MT_OP_READ); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1244 | rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags, |
| 1245 | ep); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1246 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1247 | vls_get_and_unlock (vlsh); |
| 1248 | return rv; |
| 1249 | } |
| 1250 | |
| 1251 | int |
| 1252 | vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen) |
| 1253 | { |
| 1254 | vcl_locked_session_t *vls; |
| 1255 | int rv; |
| 1256 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1257 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1258 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1259 | return VPPCOM_EBADFD; |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1260 | if (vls_mt_session_should_migrate (vls)) |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1261 | { |
| 1262 | vls = vls_mt_session_migrate (vls); |
| 1263 | if (PREDICT_FALSE (!vls)) |
| 1264 | return VPPCOM_EBADFD; |
| 1265 | } |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1266 | rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen); |
| 1267 | vls_get_and_unlock (vlsh); |
| 1268 | return rv; |
| 1269 | } |
| 1270 | |
| 1271 | int |
| 1272 | vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep) |
| 1273 | { |
| 1274 | vcl_locked_session_t *vls; |
| 1275 | int rv; |
| 1276 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1277 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1278 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1279 | return VPPCOM_EBADFD; |
| 1280 | rv = vppcom_session_bind (vls_to_sh_tu (vls), ep); |
| 1281 | vls_get_and_unlock (vlsh); |
| 1282 | return rv; |
| 1283 | } |
| 1284 | |
| 1285 | int |
| 1286 | vls_listen (vls_handle_t vlsh, int q_len) |
| 1287 | { |
| 1288 | vcl_locked_session_t *vls; |
| 1289 | int rv; |
| 1290 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1291 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1292 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1293 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1294 | vls_mt_guard (vls, VLS_MT_OP_XPOLL); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1295 | rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1296 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1297 | vls_get_and_unlock (vlsh); |
| 1298 | return rv; |
| 1299 | } |
| 1300 | |
| 1301 | int |
| 1302 | vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep) |
| 1303 | { |
| 1304 | vcl_locked_session_t *vls; |
| 1305 | int rv; |
| 1306 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1307 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1308 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1309 | return VPPCOM_EBADFD; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1310 | vls_mt_guard (vls, VLS_MT_OP_XPOLL); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1311 | rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1312 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1313 | vls_get_and_unlock (vlsh); |
| 1314 | return rv; |
| 1315 | } |
| 1316 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1317 | static inline void |
| 1318 | vls_mp_checks (vcl_locked_session_t * vls, int is_add) |
| 1319 | { |
| 1320 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 1321 | vcl_session_t *s; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1322 | u32 owner_wrk; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1323 | |
Mohamed Feroz | 88c836c | 2024-08-29 08:04:02 +0000 | [diff] [blame] | 1324 | if (vls_mt_wrk_supported () && vls_n_workers () <= 1) |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1325 | return; |
| 1326 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1327 | ASSERT (wrk->wrk_index == vls->vcl_wrk_index); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1328 | s = vcl_session_get (wrk, vls->session_index); |
| 1329 | switch (s->session_state) |
| 1330 | { |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 1331 | case VCL_STATE_LISTEN: |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1332 | if (is_add) |
| 1333 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1334 | vls_listener_wrk_set (vls, vls->vcl_wrk_index, 1 /* is_active */); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1335 | break; |
| 1336 | } |
Florin Coras | a41a0b5 | 2023-03-01 22:22:30 -0800 | [diff] [blame] | 1337 | /* Although removal from epoll means listener no longer accepts new |
| 1338 | * sessions, the accept queue built by vpp cannot be drained by stopping |
| 1339 | * the listener. Morover, some applications, e.g., nginx, might |
| 1340 | * constantly remove and add listeners to their epfds. Removing |
| 1341 | * listeners in such situations causes a lot of churn in vpp as segments |
| 1342 | * and segment managers need to be recreated. */ |
| 1343 | /* vls_listener_wrk_stop_listen (vls, vls->vcl_wrk_index); */ |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1344 | break; |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 1345 | case VCL_STATE_LISTEN_NO_MQ: |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1346 | if (!is_add) |
| 1347 | break; |
| 1348 | |
| 1349 | /* Register worker as listener */ |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1350 | vls_listener_wrk_start_listen (vls, vls->vcl_wrk_index); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1351 | |
| 1352 | /* If owner worker did not attempt to accept/xpoll on the session, |
| 1353 | * force a listen stop for it, since it may not be interested in |
| 1354 | * accepting new sessions. |
| 1355 | * This is pretty much a hack done to give app workers the illusion |
| 1356 | * that it is fine to listen and not accept new sessions for a |
| 1357 | * given listener. Without it, we would accumulate unhandled |
| 1358 | * accepts on the passive worker message queue. */ |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1359 | owner_wrk = vls_shared_get_owner (vls); |
| 1360 | if (!vls_listener_wrk_is_active (vls, owner_wrk)) |
| 1361 | vls_listener_wrk_stop_listen (vls, owner_wrk); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1362 | break; |
| 1363 | default: |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1368 | vls_handle_t |
| 1369 | vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags) |
| 1370 | { |
| 1371 | vls_handle_t accepted_vlsh; |
| 1372 | vcl_locked_session_t *vls; |
| 1373 | int sh; |
| 1374 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1375 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1376 | if (!(vls = vls_get_w_dlock (listener_vlsh))) |
| 1377 | return VPPCOM_EBADFD; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1378 | if (vcl_n_workers () > 1) |
| 1379 | vls_mp_checks (vls, 1 /* is_add */ ); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1380 | vls_mt_guard (vls, VLS_MT_OP_SPOOL); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1381 | sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1382 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1383 | vls_get_and_unlock (listener_vlsh); |
| 1384 | if (sh < 0) |
| 1385 | return sh; |
| 1386 | accepted_vlsh = vls_alloc (sh); |
| 1387 | if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE)) |
| 1388 | vppcom_session_close (sh); |
| 1389 | return accepted_vlsh; |
| 1390 | } |
| 1391 | |
| 1392 | vls_handle_t |
| 1393 | vls_create (uint8_t proto, uint8_t is_nonblocking) |
| 1394 | { |
| 1395 | vcl_session_handle_t sh; |
| 1396 | vls_handle_t vlsh; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1397 | vcl_locked_session_t *vls = NULL; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1398 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1399 | vls_mt_detect (); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1400 | vls_mt_guard (vls, VLS_MT_OP_SPOOL); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1401 | sh = vppcom_session_create (proto, is_nonblocking); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1402 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1403 | if (sh == INVALID_SESSION_ID) |
| 1404 | return VLS_INVALID_HANDLE; |
| 1405 | |
| 1406 | vlsh = vls_alloc (sh); |
| 1407 | if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE)) |
| 1408 | vppcom_session_close (sh); |
| 1409 | |
| 1410 | return vlsh; |
| 1411 | } |
| 1412 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1413 | static void |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1414 | vls_mt_session_cleanup (vcl_locked_session_t * vls) |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1415 | { |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1416 | u32 session_index, wrk_index, current_vcl_wrk; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1417 | vcl_worker_t *wrk = vcl_worker_get_current (); |
| 1418 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1419 | ASSERT (vls_mt_wrk_supported ()); |
| 1420 | |
| 1421 | current_vcl_wrk = vcl_get_worker_index (); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1422 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1423 | hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index, |
| 1424 | ({ |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1425 | if (current_vcl_wrk != wrk_index) |
| 1426 | vls_send_session_cleanup_rpc (wrk, wrk_index, session_index); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1427 | })); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1428 | hash_free (vls->vcl_wrk_index_to_session_index); |
| 1429 | } |
| 1430 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1431 | int |
| 1432 | vls_close (vls_handle_t vlsh) |
| 1433 | { |
| 1434 | vcl_locked_session_t *vls; |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1435 | int rv; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1436 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1437 | vls_mt_detect (); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1438 | vls_mt_pool_wlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1439 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1440 | vls = vls_get_and_lock (vlsh); |
| 1441 | if (!vls) |
| 1442 | { |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1443 | vls_mt_pool_wunlock (); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1444 | return VPPCOM_EBADFD; |
| 1445 | } |
| 1446 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1447 | vls_mt_guard (vls, VLS_MT_OP_SPOOL); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1448 | |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1449 | if (vls_is_shared (vls)) |
| 1450 | rv = vls_unshare_session (vls, vcl_worker_get_current ()); |
| 1451 | else |
| 1452 | rv = vppcom_session_close (vls_to_sh (vls)); |
| 1453 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1454 | if (vls_mt_wrk_supported ()) |
| 1455 | vls_mt_session_cleanup (vls); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1456 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1457 | vls_free (vls); |
| 1458 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1459 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1460 | vls_mt_pool_wunlock (); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1461 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1462 | return rv; |
| 1463 | } |
| 1464 | |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1465 | int |
liuyacan | 55c952e | 2021-06-13 14:54:55 +0800 | [diff] [blame] | 1466 | vls_shutdown (vls_handle_t vlsh, int how) |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1467 | { |
| 1468 | vcl_locked_session_t *vls; |
| 1469 | int rv; |
| 1470 | |
| 1471 | vls_mt_detect (); |
| 1472 | if (!(vls = vls_get_w_dlock (vlsh))) |
| 1473 | return VPPCOM_EBADFD; |
| 1474 | |
| 1475 | vls_mt_guard (vls, VLS_MT_OP_SPOOL); |
liuyacan | 6fc07b4 | 2021-07-24 22:48:36 +0800 | [diff] [blame] | 1476 | rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how); |
liuyacan | 534468e | 2021-05-09 03:50:40 +0000 | [diff] [blame] | 1477 | vls_mt_unguard (); |
| 1478 | vls_get_and_unlock (vlsh); |
| 1479 | |
| 1480 | return rv; |
| 1481 | } |
| 1482 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1483 | vls_handle_t |
| 1484 | vls_epoll_create (void) |
| 1485 | { |
| 1486 | vcl_session_handle_t sh; |
| 1487 | vls_handle_t vlsh; |
| 1488 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1489 | vls_mt_detect (); |
Florin Coras | 63d3ac6 | 2019-03-29 08:29:25 -0700 | [diff] [blame] | 1490 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1491 | sh = vppcom_epoll_create (); |
| 1492 | if (sh == INVALID_SESSION_ID) |
| 1493 | return VLS_INVALID_HANDLE; |
| 1494 | |
| 1495 | vlsh = vls_alloc (sh); |
| 1496 | if (vlsh == VLS_INVALID_HANDLE) |
| 1497 | vppcom_session_close (sh); |
| 1498 | |
| 1499 | return vlsh; |
| 1500 | } |
| 1501 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1502 | static void |
| 1503 | vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op) |
| 1504 | { |
nandfan | dc2632e | 2021-03-26 16:46:58 +0800 | [diff] [blame] | 1505 | if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD) |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1506 | return; |
| 1507 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1508 | vls_mp_checks (vls, op == EPOLL_CTL_ADD); |
| 1509 | } |
| 1510 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1511 | int |
| 1512 | vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh, |
| 1513 | struct epoll_event *event) |
| 1514 | { |
| 1515 | vcl_locked_session_t *ep_vls, *vls; |
| 1516 | vcl_session_handle_t ep_sh, sh; |
| 1517 | int rv; |
| 1518 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1519 | vls_mt_detect (); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1520 | vls_mt_pool_rlock (); |
Florin Coras | 54223ee | 2022-03-02 21:06:30 -0800 | [diff] [blame] | 1521 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1522 | ep_vls = vls_get_and_lock (ep_vlsh); |
Florin Coras | 54223ee | 2022-03-02 21:06:30 -0800 | [diff] [blame] | 1523 | if (PREDICT_FALSE (!ep_vls)) |
| 1524 | { |
| 1525 | vls_mt_pool_runlock (); |
| 1526 | return VPPCOM_EBADFD; |
| 1527 | } |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1528 | |
| 1529 | if (vls_mt_session_should_migrate (ep_vls)) |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1530 | { |
| 1531 | ep_vls = vls_mt_session_migrate (ep_vls); |
| 1532 | if (PREDICT_FALSE (!ep_vls)) |
Florin Coras | 54223ee | 2022-03-02 21:06:30 -0800 | [diff] [blame] | 1533 | { |
| 1534 | vls_mt_pool_runlock (); |
| 1535 | return VPPCOM_EBADFD; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | vls = vls_get_and_lock (vlsh); |
| 1540 | if (PREDICT_FALSE (!vls)) |
| 1541 | { |
| 1542 | vls_unlock (ep_vls); |
| 1543 | vls_mt_pool_runlock (); |
| 1544 | return VPPCOM_EBADFD; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1545 | } |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1546 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1547 | ep_sh = vls_to_sh (ep_vls); |
| 1548 | sh = vls_to_sh (vls); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1549 | |
nandfan | dc2632e | 2021-03-26 16:46:58 +0800 | [diff] [blame] | 1550 | vls_epoll_ctl_mp_checks (vls, op); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1551 | vls_mt_pool_runlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1552 | rv = vppcom_epoll_ctl (ep_sh, op, sh, event); |
| 1553 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1554 | vls_mt_pool_rlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1555 | ep_vls = vls_get (ep_vlsh); |
| 1556 | vls = vls_get (vlsh); |
| 1557 | vls_unlock (vls); |
| 1558 | vls_unlock (ep_vls); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1559 | vls_mt_pool_runlock (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1560 | return rv; |
| 1561 | } |
| 1562 | |
| 1563 | int |
| 1564 | vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events, |
| 1565 | int maxevents, double wait_for_time) |
| 1566 | { |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1567 | vcl_locked_session_t *vls, *vls_tmp = NULL; |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1568 | int rv; |
| 1569 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1570 | vls_mt_detect (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1571 | if (!(vls = vls_get_w_dlock (ep_vlsh))) |
| 1572 | return VPPCOM_EBADFD; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1573 | vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1574 | rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents, |
| 1575 | wait_for_time); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1576 | vls_mt_unguard (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1577 | vls_get_and_unlock (ep_vlsh); |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1578 | vls_handle_pending_wrk_cleanup (); |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1579 | return rv; |
| 1580 | } |
| 1581 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1582 | static void |
| 1583 | vls_select_mp_checks (vcl_si_set * read_map) |
| 1584 | { |
| 1585 | vcl_locked_session_t *vls; |
| 1586 | vcl_worker_t *wrk; |
| 1587 | vcl_session_t *s; |
| 1588 | u32 si; |
| 1589 | |
| 1590 | if (vcl_n_workers () <= 1) |
| 1591 | { |
| 1592 | vlsl->select_mp_check = 1; |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | if (!read_map) |
| 1597 | return; |
| 1598 | |
| 1599 | vlsl->select_mp_check = 1; |
| 1600 | wrk = vcl_worker_get_current (); |
| 1601 | |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 1602 | clib_bitmap_foreach (si, read_map) { |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1603 | s = vcl_session_get (wrk, si); |
Florin Coras | c127d5a | 2020-10-14 16:35:58 -0700 | [diff] [blame] | 1604 | if (s->session_state == VCL_STATE_LISTEN) |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1605 | { |
| 1606 | vls = vls_get (vls_session_index_to_vlsh (si)); |
| 1607 | vls_mp_checks (vls, 1 /* is_add */); |
| 1608 | } |
Damjan Marion | f0ca1e8 | 2020-12-13 23:26:56 +0100 | [diff] [blame] | 1609 | } |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1612 | int |
| 1613 | vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map, |
| 1614 | vcl_si_set * except_map, double wait_for_time) |
| 1615 | { |
| 1616 | int rv; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1617 | vcl_locked_session_t *vls = NULL; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1618 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1619 | vls_mt_detect (); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1620 | vls_mt_guard (vls, VLS_MT_OP_XPOLL); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1621 | if (PREDICT_FALSE (!vlsl->select_mp_check)) |
| 1622 | vls_select_mp_checks (read_map); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1623 | rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time); |
| 1624 | vls_mt_unguard (); |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1625 | vls_handle_pending_wrk_cleanup (); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1626 | return rv; |
| 1627 | } |
| 1628 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1629 | static void |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1630 | vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk) |
| 1631 | { |
| 1632 | u32 current_wrk, is_current; |
| 1633 | vcl_locked_session_t *vls; |
| 1634 | vcl_session_t *s; |
| 1635 | |
Florin Coras | 14ed6df | 2019-03-06 21:13:42 -0800 | [diff] [blame] | 1636 | if (pool_elts (vcm->workers) <= 1) |
| 1637 | return; |
| 1638 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1639 | current_wrk = vcl_get_worker_index (); |
| 1640 | is_current = current_wrk == wrk->wrk_index; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1641 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1642 | pool_foreach (s, wrk->sessions) { |
hanlin | f8e1363 | 2020-08-21 11:05:36 +0800 | [diff] [blame] | 1643 | vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index)); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1644 | if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk))) |
| 1645 | vls_unshare_session (vls, wrk); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1646 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | static void |
| 1650 | vls_cleanup_vcl_worker (vcl_worker_t * wrk) |
| 1651 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1652 | vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index); |
| 1653 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1654 | /* Unshare sessions and also cleanup worker since child may have |
| 1655 | * called _exit () and therefore vcl may not catch the event */ |
| 1656 | vls_unshare_vcl_worker_sessions (wrk); |
wanghanlin | b940fd4 | 2021-06-29 16:01:55 +0800 | [diff] [blame] | 1657 | |
| 1658 | /* Since child may have exited and thereforce fd of vpp_app_socket_api |
| 1659 | * may have been closed, so DONOT notify VPP. |
| 1660 | */ |
| 1661 | vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1662 | |
| 1663 | vls_worker_free (vls_wrk); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | static void |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1667 | vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk) |
| 1668 | { |
| 1669 | vcl_worker_t *sub_child; |
| 1670 | int tries = 0; |
| 1671 | |
| 1672 | if (child_wrk->forked_child != ~0) |
| 1673 | { |
| 1674 | sub_child = vcl_worker_get_if_valid (child_wrk->forked_child); |
| 1675 | if (sub_child) |
| 1676 | { |
| 1677 | /* Wait a bit, maybe the process is going away */ |
| 1678 | while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50) |
| 1679 | usleep (1e3); |
| 1680 | if (kill (sub_child->current_pid, 0) < 0) |
| 1681 | vls_cleanup_forked_child (child_wrk, sub_child); |
| 1682 | } |
| 1683 | } |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1684 | vls_cleanup_vcl_worker (child_wrk); |
| 1685 | VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1686 | wrk->forked_child = ~0; |
| 1687 | } |
| 1688 | |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1689 | static void |
| 1690 | vls_handle_pending_wrk_cleanup (void) |
| 1691 | { |
| 1692 | u32 *wip; |
| 1693 | vcl_worker_t *child_wrk, *wrk; |
| 1694 | vls_worker_t *vls_wrk = vls_worker_get_current (); |
| 1695 | |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1696 | if (PREDICT_TRUE (vec_len (vls_wrk->pending_vcl_wrk_cleanup) == 0)) |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1697 | return; |
| 1698 | |
| 1699 | wrk = vcl_worker_get_current (); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1700 | vec_foreach (wip, vls_wrk->pending_vcl_wrk_cleanup) |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1701 | { |
| 1702 | child_wrk = vcl_worker_get_if_valid (*wip); |
| 1703 | if (!child_wrk) |
| 1704 | continue; |
| 1705 | vls_cleanup_forked_child (wrk, child_wrk); |
| 1706 | } |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1707 | vec_reset_length (vls_wrk->pending_vcl_wrk_cleanup); |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1708 | } |
| 1709 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1710 | static struct sigaction old_sa; |
| 1711 | |
| 1712 | static void |
| 1713 | vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc) |
| 1714 | { |
| 1715 | vcl_worker_t *wrk, *child_wrk; |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1716 | vls_worker_t *vls_wrk; |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1717 | |
| 1718 | if (vcl_get_worker_index () == ~0) |
| 1719 | return; |
| 1720 | |
| 1721 | if (sigaction (SIGCHLD, &old_sa, 0)) |
| 1722 | { |
| 1723 | VERR ("couldn't restore sigchld"); |
| 1724 | exit (-1); |
| 1725 | } |
| 1726 | |
| 1727 | wrk = vcl_worker_get_current (); |
| 1728 | if (wrk->forked_child == ~0) |
| 1729 | return; |
| 1730 | |
| 1731 | child_wrk = vcl_worker_get_if_valid (wrk->forked_child); |
| 1732 | if (!child_wrk) |
| 1733 | goto done; |
| 1734 | |
| 1735 | if (si && si->si_pid != child_wrk->current_pid) |
| 1736 | { |
| 1737 | VDBG (0, "unexpected child pid %u", si->si_pid); |
| 1738 | goto done; |
| 1739 | } |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1740 | |
| 1741 | /* Parent process may enter sighandler with a lock, such as lock in localtime |
| 1742 | * or in mspace_free, and child wrk cleanup may try to get such locks and |
| 1743 | * cause deadlock. |
| 1744 | * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select. |
| 1745 | */ |
| 1746 | vls_wrk = vls_worker_get_current (); |
Florin Coras | 5e61843 | 2021-07-26 18:19:25 -0700 | [diff] [blame] | 1747 | vec_add1 (vls_wrk->pending_vcl_wrk_cleanup, child_wrk->wrk_index); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1748 | |
| 1749 | done: |
| 1750 | if (old_sa.sa_flags & SA_SIGINFO) |
| 1751 | { |
| 1752 | void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction; |
| 1753 | fn (signum, si, uc); |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | void (*fn) (int) = old_sa.sa_handler; |
| 1758 | if (fn) |
| 1759 | fn (signum); |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | static void |
| 1764 | vls_incercept_sigchld () |
| 1765 | { |
| 1766 | struct sigaction sa; |
nandfan | 5fdc47c | 2021-02-22 17:17:17 +0800 | [diff] [blame] | 1767 | if (old_sa.sa_sigaction) |
| 1768 | { |
| 1769 | VDBG (0, "have intercepted sigchld"); |
| 1770 | return; |
| 1771 | } |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1772 | clib_memset (&sa, 0, sizeof (sa)); |
| 1773 | sa.sa_sigaction = vls_intercept_sigchld_handler; |
| 1774 | sa.sa_flags = SA_SIGINFO; |
| 1775 | if (sigaction (SIGCHLD, &sa, &old_sa)) |
| 1776 | { |
| 1777 | VERR ("couldn't intercept sigchld"); |
| 1778 | exit (-1); |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | static void |
| 1783 | vls_app_pre_fork (void) |
| 1784 | { |
| 1785 | vls_incercept_sigchld (); |
| 1786 | vcl_flush_mq_events (); |
| 1787 | } |
| 1788 | |
| 1789 | static void |
| 1790 | vls_app_fork_child_handler (void) |
| 1791 | { |
| 1792 | vcl_worker_t *parent_wrk; |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 1793 | int parent_wrk_index; |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1794 | |
| 1795 | parent_wrk_index = vcl_get_worker_index (); |
| 1796 | VDBG (0, "initializing forked child %u with parent wrk %u", getpid (), |
| 1797 | parent_wrk_index); |
| 1798 | |
| 1799 | /* |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 1800 | * Clear old state |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1801 | */ |
| 1802 | vcl_set_worker_index (~0); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1803 | |
| 1804 | /* |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 1805 | * Allocate and register vcl worker with vpp |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1806 | */ |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 1807 | if (vppcom_worker_register ()) |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1808 | { |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 1809 | VERR ("couldn't register new worker!"); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1810 | return; |
| 1811 | } |
| 1812 | |
| 1813 | /* |
Florin Coras | b88de90 | 2020-09-08 16:47:57 -0700 | [diff] [blame] | 1814 | * Allocate/initialize vls worker and share sessions |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1815 | */ |
| 1816 | vls_worker_alloc (); |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1817 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1818 | /* Reset number of threads and set wrk index */ |
Florin Coras | ce4635c | 2024-10-23 14:19:31 -0700 | [diff] [blame] | 1819 | vlsl->vls_mt_n_threads = 1; |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1820 | vlsl->vls_wrk_index = vcl_get_worker_index (); |
| 1821 | vlsl->select_mp_check = 0; |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 1822 | clib_rwlock_init (&vlsl->vls_pool_lock); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1823 | vls_mt_locks_init (); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1824 | |
liuyacan | 603e1a4 | 2021-07-22 15:52:01 +0800 | [diff] [blame] | 1825 | parent_wrk = vcl_worker_get (parent_wrk_index); |
| 1826 | vls_worker_copy_on_fork (parent_wrk); |
| 1827 | parent_wrk->forked_child = vcl_get_worker_index (); |
| 1828 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1829 | VDBG (0, "forked child main worker initialized"); |
| 1830 | vcm->forking = 0; |
| 1831 | } |
| 1832 | |
| 1833 | static void |
| 1834 | vls_app_fork_parent_handler (void) |
| 1835 | { |
| 1836 | vcm->forking = 1; |
| 1837 | while (vcm->forking) |
| 1838 | ; |
| 1839 | } |
| 1840 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1841 | void |
| 1842 | vls_app_exit (void) |
| 1843 | { |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1844 | vls_worker_t *wrk = vls_worker_get_current (); |
| 1845 | |
wanghanlin | d72a034 | 2021-05-12 17:00:29 +0800 | [diff] [blame] | 1846 | /* Handle pending wrk cleanup */ |
| 1847 | vls_handle_pending_wrk_cleanup (); |
| 1848 | |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1849 | /* Unshare the sessions. VCL will clean up the worker */ |
| 1850 | vls_unshare_vcl_worker_sessions (vcl_worker_get_current ()); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1851 | vls_worker_free (wrk); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1852 | } |
| 1853 | |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1854 | static void |
| 1855 | vls_clone_and_share_rpc_handler (void *args) |
| 1856 | { |
| 1857 | vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args; |
| 1858 | vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk; |
| 1859 | vcl_locked_session_t *vls, *dst_vls; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1860 | vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1861 | vcl_session_t *s, *dst_s; |
| 1862 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1863 | VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)", |
| 1864 | vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk, |
| 1865 | msg->origin_session_index); |
| 1866 | |
| 1867 | /* VCL locked session can't been protected, so DONT touch it. |
| 1868 | * VCL session may been free, check it. |
| 1869 | */ |
| 1870 | dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk); |
| 1871 | s = vcl_session_get (vcl_wrk, msg->session_index); |
| 1872 | if (PREDICT_FALSE (!s)) |
| 1873 | { |
| 1874 | dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST; |
| 1875 | return; |
| 1876 | } |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1877 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1878 | if (!vls_mt_wrk_supported ()) |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1879 | { |
| 1880 | vls = vls_session_get (wrk, msg->vls_index); |
| 1881 | vls_init_share_session (wrk, vls); |
| 1882 | dst_wrk = vls_worker_get (msg->origin_vls_wrk); |
| 1883 | dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index); |
| 1884 | dst_vls->shared_data_index = vls->shared_data_index; |
| 1885 | } |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1886 | dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index); |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1887 | clib_memcpy (dst_s, s, sizeof (*s)); |
| 1888 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1889 | dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | static void |
| 1893 | vls_session_cleanup_rpc_handler (void *args) |
| 1894 | { |
| 1895 | vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args; |
| 1896 | vcl_worker_t *wrk = vcl_worker_get_current (); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 1897 | vls_worker_t *vls_wrk = vls_worker_get_current (); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1898 | vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1899 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1900 | VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()", |
| 1901 | wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1902 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1903 | vppcom_session_close (sh); |
wanghanlin | e8f848a | 2021-01-08 14:57:11 +0800 | [diff] [blame] | 1904 | vls_sh_to_vlsh_table_del (vls_wrk, sh); |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | static void |
| 1908 | vls_rpc_handler (void *args) |
| 1909 | { |
| 1910 | vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args; |
| 1911 | switch (msg->type) |
| 1912 | { |
| 1913 | case VLS_RPC_CLONE_AND_SHARE: |
| 1914 | vls_clone_and_share_rpc_handler (msg->data); |
| 1915 | break; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1916 | case VLS_RPC_SESS_CLEANUP: |
| 1917 | vls_session_cleanup_rpc_handler (msg->data); |
| 1918 | break; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1919 | default: |
| 1920 | break; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | void |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1925 | vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index, |
| 1926 | u32 session_index, u32 vls_wrk_index, |
| 1927 | u32 dst_wrk_index, u32 dst_vls_index, |
| 1928 | u32 dst_session_index) |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1929 | { |
| 1930 | u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)]; |
| 1931 | vls_clone_and_share_msg_t *msg; |
| 1932 | vls_rpc_msg_t *rpc; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1933 | int ret; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1934 | f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1935 | |
| 1936 | rpc = (vls_rpc_msg_t *) & data; |
| 1937 | rpc->type = VLS_RPC_CLONE_AND_SHARE; |
| 1938 | msg = (vls_clone_and_share_msg_t *) & rpc->data; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1939 | msg->origin_vls_wrk = vls_wrk_index; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1940 | msg->origin_vls_index = origin_vls_index; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1941 | msg->origin_vcl_wrk = wrk->wrk_index; |
| 1942 | msg->origin_session_index = session_index; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1943 | msg->vls_index = dst_vls_index; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1944 | msg->session_index = dst_session_index; |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1945 | |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1946 | /* Try lock and handle rpcs if two threads send each other |
| 1947 | * clone requests at the same time. |
| 1948 | */ |
| 1949 | wrk->rpc_done = VLS_RPC_STATE_INIT; |
| 1950 | while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock)) |
| 1951 | vcl_flush_mq_events (); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1952 | ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data)); |
| 1953 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1954 | VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d", |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1955 | dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, |
| 1956 | msg->origin_session_index, ret); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1957 | while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT && |
| 1958 | clib_time_now (&wrk->clib_time) < timeout) |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1959 | ; |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1960 | clib_spinlock_unlock (&vlsm->worker_rpc_lock); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | void |
| 1964 | vls_send_session_cleanup_rpc (vcl_worker_t * wrk, |
| 1965 | u32 dst_wrk_index, u32 dst_session_index) |
| 1966 | { |
| 1967 | u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)]; |
| 1968 | vls_sess_cleanup_msg_t *msg; |
| 1969 | vls_rpc_msg_t *rpc; |
| 1970 | int ret; |
| 1971 | |
| 1972 | rpc = (vls_rpc_msg_t *) & data; |
| 1973 | rpc->type = VLS_RPC_SESS_CLEANUP; |
| 1974 | msg = (vls_sess_cleanup_msg_t *) & rpc->data; |
| 1975 | msg->origin_vcl_wrk = wrk->wrk_index; |
| 1976 | msg->session_index = dst_session_index; |
| 1977 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1978 | ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data)); |
| 1979 | |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 1980 | VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d", |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 1981 | dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret); |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1984 | int |
| 1985 | vls_app_create (char *app_name) |
| 1986 | { |
| 1987 | int rv; |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 1988 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 1989 | if ((rv = vppcom_app_create (app_name))) |
| 1990 | return rv; |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1991 | |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 1992 | vlsm = clib_mem_alloc (sizeof (vls_main_t)); |
| 1993 | clib_memset (vlsm, 0, sizeof (*vlsm)); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1994 | clib_rwlock_init (&vlsm->shared_data_lock); |
wanghanlin | dcacdc4 | 2020-12-28 16:19:05 +0800 | [diff] [blame] | 1995 | clib_spinlock_init (&vlsm->worker_rpc_lock); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 1996 | pool_alloc (vlsm->workers, vcm->cfg.max_workers); |
| 1997 | |
Florin Coras | f9240dc | 2019-01-15 08:03:17 -0800 | [diff] [blame] | 1998 | pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler, |
| 1999 | vls_app_fork_child_handler); |
Florin Coras | 0ef8ef2 | 2019-01-18 08:37:13 -0800 | [diff] [blame] | 2000 | atexit (vls_app_exit); |
Florin Coras | 243edd5 | 2020-03-04 22:20:12 +0000 | [diff] [blame] | 2001 | vls_worker_alloc (); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 2002 | vlsl->vls_wrk_index = vcl_get_worker_index (); |
Florin Coras | ce4635c | 2024-10-23 14:19:31 -0700 | [diff] [blame] | 2003 | vlsl->vls_mt_n_threads = 1; |
Florin Coras | 2e2f9df | 2021-07-27 22:48:05 -0700 | [diff] [blame] | 2004 | clib_rwlock_init (&vlsl->vls_pool_lock); |
Florin Coras | 2d675d7 | 2019-01-28 15:54:27 -0800 | [diff] [blame] | 2005 | vls_mt_locks_init (); |
Florin Coras | 40c07ce | 2020-07-16 20:46:17 -0700 | [diff] [blame] | 2006 | vcm->wrk_rpc_fn = vls_rpc_handler; |
Florin Coras | ce4635c | 2024-10-23 14:19:31 -0700 | [diff] [blame] | 2007 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2008 | return VPPCOM_OK; |
| 2009 | } |
| 2010 | |
hanlin | 4266d4d | 2020-05-19 17:34:17 +0800 | [diff] [blame] | 2011 | unsigned char |
| 2012 | vls_use_eventfd (void) |
| 2013 | { |
| 2014 | return vcm->cfg.use_mq_eventfd; |
| 2015 | } |
| 2016 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2017 | unsigned char |
| 2018 | vls_mt_wrk_supported (void) |
| 2019 | { |
Florin Coras | ff40d8f | 2020-08-11 22:05:28 -0700 | [diff] [blame] | 2020 | return vcm->cfg.mt_wrk_supported; |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | int |
| 2024 | vls_use_real_epoll (void) |
| 2025 | { |
| 2026 | if (vcl_get_worker_index () == ~0) |
| 2027 | return 0; |
| 2028 | |
| 2029 | return vcl_worker_get_current ()->vcl_needs_real_epoll; |
| 2030 | } |
| 2031 | |
Florin Coras | 978d48b | 2024-11-26 00:29:24 -0800 | [diff] [blame^] | 2032 | int |
| 2033 | vls_set_libc_epfd (vls_handle_t ep_vlsh, int libc_epfd) |
| 2034 | { |
| 2035 | vcl_locked_session_t *vls; |
| 2036 | |
| 2037 | vls_mt_detect (); |
| 2038 | if (!(vls = vls_get_w_dlock (ep_vlsh))) |
| 2039 | return VPPCOM_EBADFD; |
| 2040 | if (vls_mt_session_should_migrate (vls)) |
| 2041 | { |
| 2042 | vls = vls_mt_session_migrate (vls); |
| 2043 | if (PREDICT_FALSE (!vls)) |
| 2044 | return VPPCOM_EBADFD; |
| 2045 | } |
| 2046 | vls->libc_epfd = libc_epfd; |
| 2047 | vls_unlock (vls); |
| 2048 | vls_mt_pool_runlock (); |
| 2049 | return 0; |
| 2050 | } |
| 2051 | |
| 2052 | int |
| 2053 | vls_get_libc_epfd (vls_handle_t ep_vlsh) |
| 2054 | { |
| 2055 | vcl_locked_session_t *vls; |
| 2056 | int rv; |
| 2057 | |
| 2058 | vls_mt_detect (); |
| 2059 | vls_mt_pool_rlock (); |
| 2060 | |
| 2061 | vls = vls_get (ep_vlsh); |
| 2062 | if (!vls) |
| 2063 | { |
| 2064 | vls_mt_pool_runlock (); |
| 2065 | return VPPCOM_EBADFD; |
| 2066 | } |
| 2067 | |
| 2068 | /* Avoid locking. In mt scenarios, one thread might be blocking waiting on |
| 2069 | * the fd while another might be closing it. While closing, this attribute |
| 2070 | * might be retrieved, so avoid deadlock */ |
| 2071 | rv = vls->libc_epfd; |
| 2072 | |
| 2073 | vls_mt_pool_runlock (); |
| 2074 | |
| 2075 | return rv; |
| 2076 | } |
| 2077 | |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2078 | void |
| 2079 | vls_register_vcl_worker (void) |
| 2080 | { |
wanghanlin | 492350e | 2020-09-11 17:19:32 +0800 | [diff] [blame] | 2081 | vls_mt_add (); |
hanlin | a3a4896 | 2020-07-13 11:09:15 +0800 | [diff] [blame] | 2082 | } |
| 2083 | |
Florin Coras | 7baeb71 | 2019-01-04 17:05:43 -0800 | [diff] [blame] | 2084 | /* |
| 2085 | * fd.io coding-style-patch-verification: ON |
| 2086 | * |
| 2087 | * Local Variables: |
| 2088 | * eval: (c-set-style "gnu") |
| 2089 | * End: |
| 2090 | */ |