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