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