blob: 93ece0027ff55c0045bccdffdf9087b6a9d7d15d [file] [log] [blame]
Florin Coras7baeb712019-01-04 17:05:43 -08001/*
Florin Coras5e618432021-07-26 18:19:25 -07002 * Copyright (c) 2021 Cisco and/or its affiliates.
Florin Coras7baeb712019-01-04 17:05:43 -08003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras5e618432021-07-26 18:19:25 -070016/**
17 * VCL Locked Sessions (VLS) is a wrapper that synchronizes access to VCL APIs
18 * which are, by construction, not thread safe. To this end, VLS uses
19 * configuration and heuristics to detect how applications use sessions in
20 * an attempt to optimize the locking strategy. The modes of operation
21 * currently supported are the following:
22 *
23 * 1) per-process workers
24 *
25 * +----------+ +----------+
26 * | | | |
27 * | process0 | | process1 |
28 * | | | |
29 * +-----+----+ +-----+----+
30 * | |
31 * | |
32 * +-----+----+ +-----+----+
33 * | | | |
34 * | vls_wrk0 | | vls_wrk1 |
35 * | | | |
36 * +-----+----+ +-----+----+
37 * | |
38 * | |
39 * +-----+----+ +-----+----+
40 * | | | |
41 * | vcl_wrk0 | | vcl_wrk1 |
42 * | | | |
43 * +----------+ +----------+
44 *
45 * 2) per-thread workers 3) single-worker multi-thread
46 *
47 * +---------+ +---------+ +---------+ +---------+
48 * | | | | | | | |
49 * | thread0 | | thread1 | | thread0 | | thread1 |
50 * | | | | | | | |
51 * +--------++ +-+-------+ +--------++ +-+-------+
52 * | | | |
53 * | | | |
54 * +-+------+-+ +-+------+-+
55 * | | | |
56 * | vls_wrk0 | | vls_wrk0 |
57 * | | | |
58 * +-+------+-+ +----+-----+
59 * | | |
60 * | | |
61 * +--------+-+ +-+--------+ +----+-----+
62 * | | | | | |
63 * | vcl_wrk0 | | vcl_wrk1 | | vcl_wrk0 |
64 * | | | | | |
65 * +----------+ +----------+ +----------+
66 *
67 * 1) per-process workers: intercept fork calls and assume all children
68 * processes are new workers that must be registered with vcl. VLS
69 * sessions are cloned and shared between workers. Only shared sessions
70 * are locked on use and thereby only one process can interact with
71 * them at a time (explicit sharing).
72 *
73 * 2) per-thread workers: each newly detected pthread is assumed to be a new
74 * worker and is registered with vcl. Enabled via configuration.
75 * When a thread tries to access a session it does not own, a clone and
76 * share rpc request is sent to the owning thread via vcl and vpp.
77 * Consequently, a vls session can map to multiple vcl sessions, one per
78 * vcl worker. VLS sessions are locked on use (implicit sharing).
79 *
80 * 3) single-worker multi-thread: vls does not make any assumptions about
81 * application threads and therefore implements an aggressive locking
82 * strategy that limits access to underlying vcl resources based on type
83 * of interaction and locks vls session on use (implicit sharing).
84 */
85
Florin Coras7baeb712019-01-04 17:05:43 -080086#include <vcl/vcl_locked.h>
87#include <vcl/vcl_private.h>
88
Florin Coras243edd52020-03-04 22:20:12 +000089typedef struct vls_shared_data_
90{
Florin Coras5e618432021-07-26 18:19:25 -070091 clib_spinlock_t lock; /**< shared data lock */
92 u32 owner_wrk_index; /**< vcl wrk that owns session */
93 u32 *workers_subscribed; /**< vec of wrks subscribed to session */
94 clib_bitmap_t *listeners; /**< bitmap of wrks actively listening */
Florin Coras243edd52020-03-04 22:20:12 +000095} vls_shared_data_t;
96
Florin Coras7baeb712019-01-04 17:05:43 -080097typedef struct vcl_locked_session_
98{
Florin Coras5e618432021-07-26 18:19:25 -070099 clib_spinlock_t lock; /**< vls lock when in use */
100 u32 session_index; /**< vcl session index */
101 u32 vcl_wrk_index; /**< vcl worker index */
102 u32 vls_index; /**< index in vls pool */
103 u32 shared_data_index; /**< shared data index if any */
104 u32 owner_vcl_wrk_index; /**< vcl wrk of the vls wrk at alloc */
105 uword *vcl_wrk_index_to_session_index; /**< map vcl wrk to session */
Florin Coras7baeb712019-01-04 17:05:43 -0800106} vcl_locked_session_t;
107
Florin Coras243edd52020-03-04 22:20:12 +0000108typedef struct vls_worker_
109{
Florin Coras5e618432021-07-26 18:19:25 -0700110 clib_rwlock_t sh_to_vlsh_table_lock; /**< ht rwlock with mt workers */
111 vcl_locked_session_t *vls_pool; /**< pool of vls session */
112 uword *sh_to_vlsh_table; /**< map from vcl sh to vls sh */
113 u32 *pending_vcl_wrk_cleanup; /**< child vcl wrks to cleanup */
114 u32 vcl_wrk_index; /**< if 1:1 map vls to vcl wrk */
Florin Coras243edd52020-03-04 22:20:12 +0000115} vls_worker_t;
116
Florin Coras2d675d72019-01-28 15:54:27 -0800117typedef struct vls_local_
118{
Florin Coras5e618432021-07-26 18:19:25 -0700119 int vls_wrk_index; /**< vls wrk index, 1 per process */
120 volatile int vls_mt_n_threads; /**< number of threads detected */
Florin Coras2e2f9df2021-07-27 22:48:05 -0700121 clib_rwlock_t vls_pool_lock; /**< per process/wrk vls pool locks */
Florin Coras5e618432021-07-26 18:19:25 -0700122 pthread_mutex_t vls_mt_mq_mlock; /**< vcl mq lock */
123 pthread_mutex_t vls_mt_spool_mlock; /**< vcl select or pool lock */
124 volatile u8 select_mp_check; /**< flag set if select checks done */
Florin Coras2d675d72019-01-28 15:54:27 -0800125} vls_process_local_t;
126
127static vls_process_local_t vls_local;
128static vls_process_local_t *vlsl = &vls_local;
129
130typedef struct vls_main_
Florin Coras7baeb712019-01-04 17:05:43 -0800131{
Florin Coras5e618432021-07-26 18:19:25 -0700132 vls_worker_t *workers; /**< pool of vls workers */
Florin Coras5e618432021-07-26 18:19:25 -0700133 vls_shared_data_t *shared_data_pool; /**< inter proc pool of shared data */
134 clib_rwlock_t shared_data_lock; /**< shared data pool lock */
135 clib_spinlock_t worker_rpc_lock; /**< lock for inter-worker rpcs */
Florin Coras7baeb712019-01-04 17:05:43 -0800136} vls_main_t;
137
Florin Coras2d675d72019-01-28 15:54:27 -0800138vls_main_t *vlsm;
Florin Coras7baeb712019-01-04 17:05:43 -0800139
wanghanlindcacdc42020-12-28 16:19:05 +0800140typedef enum
141{
142 VLS_RPC_STATE_INIT,
143 VLS_RPC_STATE_SUCCESS,
144 VLS_RPC_STATE_SESSION_NOT_EXIST,
145} vls_rpc_state_e;
146
Florin Coras40c07ce2020-07-16 20:46:17 -0700147typedef enum vls_rpc_msg_type_
148{
149 VLS_RPC_CLONE_AND_SHARE,
hanlina3a48962020-07-13 11:09:15 +0800150 VLS_RPC_SESS_CLEANUP,
Florin Coras40c07ce2020-07-16 20:46:17 -0700151} vls_rpc_msg_type_e;
152
153typedef struct vls_rpc_msg_
154{
155 u8 type;
156 u8 data[0];
157} vls_rpc_msg_t;
158
159typedef struct vls_clone_and_share_msg_
160{
161 u32 vls_index; /**< vls to be shared */
hanlina3a48962020-07-13 11:09:15 +0800162 u32 session_index; /**< vcl session to be shared */
163 u32 origin_vls_wrk; /**< vls worker that initiated the rpc */
Florin Coras40c07ce2020-07-16 20:46:17 -0700164 u32 origin_vls_index; /**< vls session of the originator */
hanlina3a48962020-07-13 11:09:15 +0800165 u32 origin_vcl_wrk; /**< vcl worker that initiated the rpc */
166 u32 origin_session_index; /**< vcl session of the originator */
Florin Coras40c07ce2020-07-16 20:46:17 -0700167} vls_clone_and_share_msg_t;
168
hanlina3a48962020-07-13 11:09:15 +0800169typedef struct vls_sess_cleanup_msg_
170{
171 u32 session_index; /**< vcl session to be cleaned */
172 u32 origin_vcl_wrk; /**< worker that initiated the rpc */
173} vls_sess_cleanup_msg_t;
174
175void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
176 u32 dst_wrk_index, u32 dst_session_index);
wanghanlindcacdc42020-12-28 16:19:05 +0800177void vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
hanlina3a48962020-07-13 11:09:15 +0800178 u32 session_index, u32 vls_wrk_index,
179 u32 dst_wrk_index, u32 dst_vls_index,
180 u32 dst_session_index);
wanghanlind72a0342021-05-12 17:00:29 +0800181static void vls_cleanup_forked_child (vcl_worker_t *wrk,
182 vcl_worker_t *child_wrk);
183static void vls_handle_pending_wrk_cleanup (void);
hanlina3a48962020-07-13 11:09:15 +0800184
Florin Coras243edd52020-03-04 22:20:12 +0000185static inline u32
186vls_get_worker_index (void)
187{
liuyacan603e1a42021-07-22 15:52:01 +0800188 return vlsl->vls_wrk_index;
Florin Coras243edd52020-03-04 22:20:12 +0000189}
190
191static u32
192vls_shared_data_alloc (void)
193{
194 vls_shared_data_t *vls_shd;
195 u32 shd_index;
196
197 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
198 pool_get_zero (vlsm->shared_data_pool, vls_shd);
199 clib_spinlock_init (&vls_shd->lock);
200 shd_index = vls_shd - vlsm->shared_data_pool;
201 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
202
203 return shd_index;
204}
205
206static u32
207vls_shared_data_index (vls_shared_data_t * vls_shd)
208{
209 return vls_shd - vlsm->shared_data_pool;
210}
211
212vls_shared_data_t *
213vls_shared_data_get (u32 shd_index)
214{
215 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
216 return 0;
217 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
218}
219
220static void
221vls_shared_data_free (u32 shd_index)
222{
223 vls_shared_data_t *vls_shd;
224
225 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
226 vls_shd = vls_shared_data_get (shd_index);
227 clib_spinlock_free (&vls_shd->lock);
228 clib_bitmap_free (vls_shd->listeners);
229 vec_free (vls_shd->workers_subscribed);
230 pool_put (vlsm->shared_data_pool, vls_shd);
231 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
232}
233
234static inline void
235vls_shared_data_pool_rlock (void)
236{
237 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
238}
239
240static inline void
241vls_shared_data_pool_runlock (void)
242{
243 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
244}
245
Florin Coras7baeb712019-01-04 17:05:43 -0800246static inline void
Florin Coras5e618432021-07-26 18:19:25 -0700247vls_mt_pool_rlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800248{
Florin Coras243edd52020-03-04 22:20:12 +0000249 if (vlsl->vls_mt_n_threads > 1)
Florin Coras2e2f9df2021-07-27 22:48:05 -0700250 clib_rwlock_reader_lock (&vlsl->vls_pool_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800251}
252
253static inline void
Florin Coras5e618432021-07-26 18:19:25 -0700254vls_mt_pool_runlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800255{
Florin Coras243edd52020-03-04 22:20:12 +0000256 if (vlsl->vls_mt_n_threads > 1)
Florin Coras2e2f9df2021-07-27 22:48:05 -0700257 clib_rwlock_reader_unlock (&vlsl->vls_pool_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800258}
259
260static inline void
Florin Coras5e618432021-07-26 18:19:25 -0700261vls_mt_pool_wlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800262{
Florin Coras243edd52020-03-04 22:20:12 +0000263 if (vlsl->vls_mt_n_threads > 1)
Florin Coras2e2f9df2021-07-27 22:48:05 -0700264 clib_rwlock_writer_lock (&vlsl->vls_pool_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800265}
266
267static inline void
Florin Coras5e618432021-07-26 18:19:25 -0700268vls_mt_pool_wunlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800269{
Florin Coras243edd52020-03-04 22:20:12 +0000270 if (vlsl->vls_mt_n_threads > 1)
Florin Coras2e2f9df2021-07-27 22:48:05 -0700271 clib_rwlock_writer_unlock (&vlsl->vls_pool_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800272}
273
Florin Coras0ef8ef22019-01-18 08:37:13 -0800274typedef enum
275{
276 VLS_MT_OP_READ,
277 VLS_MT_OP_WRITE,
278 VLS_MT_OP_SPOOL,
279 VLS_MT_OP_XPOLL,
280} vls_mt_ops_t;
281
282typedef enum
283{
284 VLS_MT_LOCK_MQ = 1 << 0,
285 VLS_MT_LOCK_SPOOL = 1 << 1
286} vls_mt_lock_type_t;
287
Florin Coras0ef8ef22019-01-18 08:37:13 -0800288static void
289vls_mt_add (void)
290{
Florin Coras2d675d72019-01-28 15:54:27 -0800291 vlsl->vls_mt_n_threads += 1;
Florin Corasff40d8f2020-08-11 22:05:28 -0700292
293 /* If multi-thread workers are supported, for each new thread register a new
294 * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
295 * update the vcl worker's thread local worker index variable */
hanlina3a48962020-07-13 11:09:15 +0800296 if (vls_mt_wrk_supported ())
wanghanlin492350e2020-09-11 17:19:32 +0800297 {
298 if (vppcom_worker_register () != VPPCOM_OK)
299 VERR ("failed to register worker");
300 }
hanlina3a48962020-07-13 11:09:15 +0800301 else
302 vcl_set_worker_index (vlsl->vls_wrk_index);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800303}
304
305static inline void
306vls_mt_mq_lock (void)
307{
Florin Coras2d675d72019-01-28 15:54:27 -0800308 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800309}
310
311static inline void
312vls_mt_mq_unlock (void)
313{
Florin Coras2d675d72019-01-28 15:54:27 -0800314 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800315}
316
317static inline void
318vls_mt_spool_lock (void)
319{
Florin Coras2d675d72019-01-28 15:54:27 -0800320 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800321}
322
323static inline void
324vls_mt_create_unlock (void)
325{
Florin Coras2d675d72019-01-28 15:54:27 -0800326 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
327}
328
329static void
330vls_mt_locks_init (void)
331{
332 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
333 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800334}
335
Florin Coras243edd52020-03-04 22:20:12 +0000336u8
337vls_is_shared (vcl_locked_session_t * vls)
338{
339 return (vls->shared_data_index != ~0);
340}
341
342static inline void
343vls_lock (vcl_locked_session_t * vls)
344{
345 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
346 clib_spinlock_lock (&vls->lock);
347}
348
349static inline void
350vls_unlock (vcl_locked_session_t * vls)
351{
352 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
353 clib_spinlock_unlock (&vls->lock);
354}
355
Florin Coras7baeb712019-01-04 17:05:43 -0800356static inline vcl_session_handle_t
357vls_to_sh (vcl_locked_session_t * vls)
358{
Florin Corasf9240dc2019-01-15 08:03:17 -0800359 return vcl_session_handle_from_index (vls->session_index);
Florin Coras7baeb712019-01-04 17:05:43 -0800360}
361
362static inline vcl_session_handle_t
363vls_to_sh_tu (vcl_locked_session_t * vls)
364{
365 vcl_session_handle_t sh;
366 sh = vls_to_sh (vls);
Florin Coras5e618432021-07-26 18:19:25 -0700367 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800368 return sh;
369}
370
Florin Coras243edd52020-03-04 22:20:12 +0000371static vls_worker_t *
372vls_worker_get_current (void)
373{
374 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
375}
376
Mohamed Feroz88c836c2024-08-29 08:04:02 +0000377static inline u8
378vls_n_workers (void)
379{
380 return pool_elts (vlsm->workers);
381}
382
Florin Coras243edd52020-03-04 22:20:12 +0000383static void
384vls_worker_alloc (void)
385{
386 vls_worker_t *wrk;
387
388 pool_get_zero (vlsm->workers, wrk);
wanghanline8f848a2021-01-08 14:57:11 +0800389 if (vls_mt_wrk_supported ())
390 clib_rwlock_init (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700391 wrk->vcl_wrk_index = vcl_get_worker_index ();
392 vec_validate (wrk->pending_vcl_wrk_cleanup, 16);
393 vec_reset_length (wrk->pending_vcl_wrk_cleanup);
Florin Coras243edd52020-03-04 22:20:12 +0000394}
395
396static void
397vls_worker_free (vls_worker_t * wrk)
398{
Florin Coras5e618432021-07-26 18:19:25 -0700399 hash_free (wrk->sh_to_vlsh_table);
wanghanline8f848a2021-01-08 14:57:11 +0800400 if (vls_mt_wrk_supported ())
401 clib_rwlock_free (&wrk->sh_to_vlsh_table_lock);
Florin Coras243edd52020-03-04 22:20:12 +0000402 pool_free (wrk->vls_pool);
403 pool_put (vlsm->workers, wrk);
404}
405
406static vls_worker_t *
407vls_worker_get (u32 wrk_index)
408{
409 if (pool_is_free_index (vlsm->workers, wrk_index))
410 return 0;
411 return pool_elt_at_index (vlsm->workers, wrk_index);
412}
413
wanghanline8f848a2021-01-08 14:57:11 +0800414static void
415vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
416{
417 if (vls_mt_wrk_supported ())
418 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700419 hash_set (wrk->sh_to_vlsh_table, sh, vlsh);
wanghanline8f848a2021-01-08 14:57:11 +0800420 if (vls_mt_wrk_supported ())
421 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
422}
423
424static void
425vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
426{
427 if (vls_mt_wrk_supported ())
428 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700429 hash_unset (wrk->sh_to_vlsh_table, sh);
wanghanline8f848a2021-01-08 14:57:11 +0800430 if (vls_mt_wrk_supported ())
431 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
432}
433
434static uword *
435vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
436{
437 if (vls_mt_wrk_supported ())
438 clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700439 uword *vlshp = hash_get (wrk->sh_to_vlsh_table, sh);
wanghanline8f848a2021-01-08 14:57:11 +0800440 if (vls_mt_wrk_supported ())
441 clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock);
442 return vlshp;
443}
444
Florin Coras7baeb712019-01-04 17:05:43 -0800445static vls_handle_t
446vls_alloc (vcl_session_handle_t sh)
447{
Florin Coras243edd52020-03-04 22:20:12 +0000448 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800449 vcl_locked_session_t *vls;
450
Florin Coras5e618432021-07-26 18:19:25 -0700451 vls_mt_pool_wlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000452
453 pool_get_zero (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800454 vls->session_index = vppcom_session_index (sh);
Florin Coras5e618432021-07-26 18:19:25 -0700455 vls->vcl_wrk_index = vppcom_session_worker (sh);
Florin Coras243edd52020-03-04 22:20:12 +0000456 vls->vls_index = vls - wrk->vls_pool;
457 vls->shared_data_index = ~0;
wanghanline8f848a2021-01-08 14:57:11 +0800458 vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index);
hanlina3a48962020-07-13 11:09:15 +0800459 if (vls_mt_wrk_supported ())
460 {
Florin Coras5e618432021-07-26 18:19:25 -0700461 hash_set (vls->vcl_wrk_index_to_session_index, vls->vcl_wrk_index,
hanlina3a48962020-07-13 11:09:15 +0800462 vls->session_index);
Florin Coras5e618432021-07-26 18:19:25 -0700463 vls->owner_vcl_wrk_index = vls->vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +0800464 }
Florin Coras7baeb712019-01-04 17:05:43 -0800465 clib_spinlock_init (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000466
Florin Coras5e618432021-07-26 18:19:25 -0700467 vls_mt_pool_wunlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800468 return vls->vls_index;
469}
470
471static vcl_locked_session_t *
472vls_get (vls_handle_t vlsh)
473{
Florin Coras243edd52020-03-04 22:20:12 +0000474 vls_worker_t *wrk = vls_worker_get_current ();
475 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800476 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000477 return pool_elt_at_index (wrk->vls_pool, vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -0800478}
479
480static void
Florin Coras0ef8ef22019-01-18 08:37:13 -0800481vls_free (vcl_locked_session_t * vls)
Florin Coras7baeb712019-01-04 17:05:43 -0800482{
Florin Coras243edd52020-03-04 22:20:12 +0000483 vls_worker_t *wrk = vls_worker_get_current ();
484
Florin Coras0ef8ef22019-01-18 08:37:13 -0800485 ASSERT (vls != 0);
wanghanline8f848a2021-01-08 14:57:11 +0800486 vls_sh_to_vlsh_table_del (
487 wrk, vcl_session_handle_from_index (vls->session_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -0800488 clib_spinlock_free (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000489 pool_put (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800490}
491
492static vcl_locked_session_t *
493vls_get_and_lock (vls_handle_t vlsh)
494{
Florin Coras243edd52020-03-04 22:20:12 +0000495 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800496 vcl_locked_session_t *vls;
Florin Coras243edd52020-03-04 22:20:12 +0000497 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800498 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000499 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
500 vls_lock (vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800501 return vls;
502}
503
504static vcl_locked_session_t *
505vls_get_w_dlock (vls_handle_t vlsh)
506{
507 vcl_locked_session_t *vls;
Florin Coras5e618432021-07-26 18:19:25 -0700508 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800509 vls = vls_get_and_lock (vlsh);
510 if (!vls)
Florin Coras5e618432021-07-26 18:19:25 -0700511 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800512 return vls;
513}
514
515static inline void
Florin Coras7baeb712019-01-04 17:05:43 -0800516vls_get_and_unlock (vls_handle_t vlsh)
517{
518 vcl_locked_session_t *vls;
Florin Coras5e618432021-07-26 18:19:25 -0700519 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800520 vls = vls_get (vlsh);
521 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -0700522 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800523}
524
525static inline void
526vls_dunlock (vcl_locked_session_t * vls)
527{
528 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -0700529 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800530}
531
Florin Coras243edd52020-03-04 22:20:12 +0000532static vcl_locked_session_t *
533vls_session_get (vls_worker_t * wrk, u32 vls_index)
534{
535 if (pool_is_free_index (wrk->vls_pool, vls_index))
536 return 0;
537 return pool_elt_at_index (wrk->vls_pool, vls_index);
538}
539
Florin Coras2d675d72019-01-28 15:54:27 -0800540vcl_session_handle_t
541vlsh_to_sh (vls_handle_t vlsh)
542{
543 vcl_locked_session_t *vls;
544 int rv;
545
546 vls = vls_get_w_dlock (vlsh);
547 if (!vls)
548 return INVALID_SESSION_ID;
549 rv = vls_to_sh (vls);
550 vls_dunlock (vls);
551 return rv;
552}
553
554vcl_session_handle_t
555vlsh_to_session_index (vls_handle_t vlsh)
556{
557 vcl_session_handle_t sh;
558 sh = vlsh_to_sh (vlsh);
559 return vppcom_session_index (sh);
560}
561
562vls_handle_t
hanlinf8e13632020-08-21 11:05:36 +0800563vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
Florin Coras2d675d72019-01-28 15:54:27 -0800564{
Florin Coras243edd52020-03-04 22:20:12 +0000565 vls_worker_t *wrk = vls_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +0800566 uword *vlshp = vls_sh_to_vlsh_table_get (
567 wrk,
568 vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
569
Florin Coras2d675d72019-01-28 15:54:27 -0800570 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
571}
572
573vls_handle_t
574vls_session_index_to_vlsh (uint32_t session_index)
575{
576 vls_handle_t vlsh;
577
Florin Coras5e618432021-07-26 18:19:25 -0700578 vls_mt_pool_rlock ();
hanlinf8e13632020-08-21 11:05:36 +0800579 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
Florin Coras5e618432021-07-26 18:19:25 -0700580 vls_mt_pool_runlock ();
Florin Coras2d675d72019-01-28 15:54:27 -0800581
582 return vlsh;
583}
584
Florin Corasf9240dc2019-01-15 08:03:17 -0800585u8
Florin Coras0ef8ef22019-01-18 08:37:13 -0800586vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
Florin Corasf9240dc2019-01-15 08:03:17 -0800587{
Florin Coras243edd52020-03-04 22:20:12 +0000588 vls_shared_data_t *vls_shd;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800589 int i;
Florin Coras243edd52020-03-04 22:20:12 +0000590
591 if (vls->shared_data_index == ~0)
592 return 0;
593
594 vls_shared_data_pool_rlock ();
595
596 vls_shd = vls_shared_data_get (vls->shared_data_index);
597 clib_spinlock_lock (&vls_shd->lock);
598
599 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
600 if (vls_shd->workers_subscribed[i] == wrk_index)
601 {
602 clib_spinlock_unlock (&vls_shd->lock);
603 vls_shared_data_pool_runlock ();
604 return 1;
605 }
606 clib_spinlock_unlock (&vls_shd->lock);
607
608 vls_shared_data_pool_runlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -0800609 return 0;
610}
611
Florin Coras2d675d72019-01-28 15:54:27 -0800612static void
613vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
614{
Florin Coras243edd52020-03-04 22:20:12 +0000615 vls_shared_data_t *vls_shd;
616
617 if (vls->shared_data_index == ~0)
618 {
619 clib_warning ("not a shared session");
620 return;
621 }
622
623 vls_shared_data_pool_rlock ();
624
625 vls_shd = vls_shared_data_get (vls->shared_data_index);
626
627 clib_spinlock_lock (&vls_shd->lock);
Florin Coras674d5652021-12-03 23:02:13 -0800628 vls_shd->listeners =
629 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
Florin Coras243edd52020-03-04 22:20:12 +0000630 clib_spinlock_unlock (&vls_shd->lock);
631
632 vls_shared_data_pool_runlock ();
633}
634
635static u32
636vls_shared_get_owner (vcl_locked_session_t * vls)
637{
638 vls_shared_data_t *vls_shd;
639 u32 owner_wrk;
640
641 vls_shared_data_pool_rlock ();
642
643 vls_shd = vls_shared_data_get (vls->shared_data_index);
644 owner_wrk = vls_shd->owner_wrk_index;
645
646 vls_shared_data_pool_runlock ();
647
648 return owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -0800649}
650
651static u8
652vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
653{
Florin Coras243edd52020-03-04 22:20:12 +0000654 vls_shared_data_t *vls_shd;
655 u8 is_set;
656
657 if (vls->shared_data_index == ~0)
658 {
659 clib_warning ("not a shared session");
660 return 0;
661 }
662
663 vls_shared_data_pool_rlock ();
664
665 vls_shd = vls_shared_data_get (vls->shared_data_index);
666
667 clib_spinlock_lock (&vls_shd->lock);
668 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
669 clib_spinlock_unlock (&vls_shd->lock);
670
671 vls_shared_data_pool_runlock ();
672
673 return (is_set == 1);
Florin Coras2d675d72019-01-28 15:54:27 -0800674}
675
676static void
677vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
678{
Florin Coras32881932023-02-06 13:30:13 -0800679 vcl_worker_t *wrk;
680 vcl_session_t *ls;
681
682 wrk = vcl_worker_get (wrk_index);
683 ls = vcl_session_get (wrk, vls->session_index);
684
685 /* Listen request already sent */
686 if (ls->flags & VCL_SESSION_F_PENDING_LISTEN)
687 return;
688
689 vcl_send_session_listen (wrk, ls);
690
691 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */);
Florin Coras2d675d72019-01-28 15:54:27 -0800692}
693
694static void
695vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
696{
697 vcl_worker_t *wrk;
698 vcl_session_t *s;
699
700 wrk = vcl_worker_get (wrk_index);
701 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700702 if (s->session_state != VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800703 return;
Florin Coras458089b2019-08-21 16:20:44 -0700704 vcl_send_session_unlisten (wrk, s);
Florin Corasc127d5a2020-10-14 16:35:58 -0700705 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800706 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
707}
708
Florin Coras243edd52020-03-04 22:20:12 +0000709static int
710vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
711 u32 wrk_index)
712{
713 int i;
714
715 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
716 {
717 if (vls_shd->workers_subscribed[i] == wrk_index)
718 return i;
719 }
720 return -1;
721}
722
Florin Coras0ef8ef22019-01-18 08:37:13 -0800723int
724vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
725{
Florin Coras243edd52020-03-04 22:20:12 +0000726 vls_shared_data_t *vls_shd;
Florin Coras311817f2020-03-07 17:45:47 +0000727 int do_disconnect, pos;
728 u32 n_subscribers;
Florin Corasf9240dc2019-01-15 08:03:17 -0800729 vcl_session_t *s;
Florin Coras2d675d72019-01-28 15:54:27 -0800730
hanlina3a48962020-07-13 11:09:15 +0800731 if (vls->shared_data_index == ~0)
732 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000733
Florin Coras2d675d72019-01-28 15:54:27 -0800734 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700735 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800736 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
Florin Corasf9240dc2019-01-15 08:03:17 -0800737
Florin Coras243edd52020-03-04 22:20:12 +0000738 vls_shared_data_pool_rlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800739
Florin Coras243edd52020-03-04 22:20:12 +0000740 vls_shd = vls_shared_data_get (vls->shared_data_index);
741 clib_spinlock_lock (&vls_shd->lock);
742
743 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
744 if (pos < 0)
745 {
746 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
Florin Coras5e618432021-07-26 18:19:25 -0700747 vls->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000748 goto done;
749 }
750
751 /*
752 * Unsubscribe from share data and fifos
753 */
754 if (s->rx_fifo)
755 {
756 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
757 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
758 }
759 vec_del1 (vls_shd->workers_subscribed, pos);
760
761 /*
762 * Cleanup vcl state
763 */
764 n_subscribers = vec_len (vls_shd->workers_subscribed);
Florin Corasc127d5a2020-10-14 16:35:58 -0700765 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
Florin Coras243edd52020-03-04 22:20:12 +0000766 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
767
768 /*
769 * No subscriber left, cleanup shared data
770 */
771 if (!n_subscribers)
772 {
773 u32 shd_index = vls_shared_data_index (vls_shd);
774
775 clib_spinlock_unlock (&vls_shd->lock);
776 vls_shared_data_pool_runlock ();
777
778 vls_shared_data_free (shd_index);
779
780 /* All locks have been dropped */
Florin Corasf9240dc2019-01-15 08:03:17 -0800781 return 0;
782 }
783
Florin Coras0ef8ef22019-01-18 08:37:13 -0800784 /* Return, if this is not the owning worker */
Florin Coras243edd52020-03-04 22:20:12 +0000785 if (vls_shd->owner_wrk_index != wrk->wrk_index)
786 goto done;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800787
Florin Coras243edd52020-03-04 22:20:12 +0000788 ASSERT (vec_len (vls_shd->workers_subscribed));
789
790 /*
791 * Check if we can change owner or close
792 */
793 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
liuyacan603e1a42021-07-22 15:52:01 +0800794 if (s->vpp_evt_q)
liuyacan5cac2e82021-07-14 15:53:01 +0800795 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000796
797 /* XXX is this still needed? */
798 if (vec_len (vls_shd->workers_subscribed) > 1)
799 clib_warning ("more workers need to be updated");
800
801done:
802
803 clib_spinlock_unlock (&vls_shd->lock);
804 vls_shared_data_pool_runlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800805
806 return 0;
807}
808
809void
Florin Coras40c07ce2020-07-16 20:46:17 -0700810vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
Florin Corasf9240dc2019-01-15 08:03:17 -0800811{
Florin Coras40c07ce2020-07-16 20:46:17 -0700812 vls_shared_data_t *vls_shd;
813
814 u32 vls_shd_index = vls_shared_data_alloc ();
815
816 vls_shared_data_pool_rlock ();
817
818 vls_shd = vls_shared_data_get (vls_shd_index);
Florin Coras5e618432021-07-26 18:19:25 -0700819 vls_shd->owner_wrk_index = vls_wrk->vcl_wrk_index;
Florin Coras40c07ce2020-07-16 20:46:17 -0700820 vls->shared_data_index = vls_shd_index;
Florin Coras5e618432021-07-26 18:19:25 -0700821 vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
Florin Coras40c07ce2020-07-16 20:46:17 -0700822
823 vls_shared_data_pool_runlock ();
824}
825
826void
827vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
828{
Florin Coras5e618432021-07-26 18:19:25 -0700829 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000830 vls_shared_data_t *vls_shd;
831 vcl_session_t *s;
Florin Corasf9240dc2019-01-15 08:03:17 -0800832
Florin Coras243edd52020-03-04 22:20:12 +0000833 s = vcl_session_get (vcl_wrk, vls->session_index);
834 if (!s)
835 {
Florin Coras40c07ce2020-07-16 20:46:17 -0700836 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
837 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
Florin Coras243edd52020-03-04 22:20:12 +0000838 return;
839 }
840
Florin Coras40c07ce2020-07-16 20:46:17 -0700841 ASSERT (vls->shared_data_index != ~0);
842
Florin Coras243edd52020-03-04 22:20:12 +0000843 /* Reinit session lock */
844 clib_spinlock_init (&vls->lock);
845
Florin Coras40c07ce2020-07-16 20:46:17 -0700846 vls_shared_data_pool_rlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000847
Florin Coras40c07ce2020-07-16 20:46:17 -0700848 vls_shd = vls_shared_data_get (vls->shared_data_index);
Florin Coras243edd52020-03-04 22:20:12 +0000849
850 clib_spinlock_lock (&vls_shd->lock);
Florin Coras5e618432021-07-26 18:19:25 -0700851 vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000852 clib_spinlock_unlock (&vls_shd->lock);
Florin Coras40c07ce2020-07-16 20:46:17 -0700853
Florin Coras243edd52020-03-04 22:20:12 +0000854 vls_shared_data_pool_runlock ();
855
Florin Coras7428eaa2023-12-11 16:04:57 -0800856 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800857 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700858 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras7428eaa2023-12-11 16:04:57 -0800859 s->rx_fifo = s->tx_fifo = 0;
860 }
861 else if (s->rx_fifo)
862 {
863 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
Florin Coras2d675d72019-01-28 15:54:27 -0800864 }
Florin Coras243edd52020-03-04 22:20:12 +0000865}
Florin Coras2d675d72019-01-28 15:54:27 -0800866
Florin Coras243edd52020-03-04 22:20:12 +0000867static void
868vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
869{
Florin Coras40c07ce2020-07-16 20:46:17 -0700870 vcl_locked_session_t *vls, *parent_vls;
Florin Coras243edd52020-03-04 22:20:12 +0000871
Damjan Marionb2c31b62020-12-13 21:47:40 +0100872 pool_foreach (vls, vls_wrk->vls_pool) {
Florin Coras40c07ce2020-07-16 20:46:17 -0700873 /* Initialize sharing on parent session */
874 if (vls->shared_data_index == ~0)
875 {
876 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
877 vls_init_share_session (vls_parent_wrk, parent_vls);
878 vls->shared_data_index = parent_vls->shared_data_index;
879 }
880 vls_share_session (vls_wrk, vls);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100881 }
Florin Corasf9240dc2019-01-15 08:03:17 -0800882}
883
wanghanlin5788a342021-06-22 17:34:08 +0800884static void
885vls_validate_veps (vcl_worker_t *wrk)
886{
887 vcl_session_t *s;
888 u32 session_index, wrk_index;
889
890 pool_foreach (s, wrk->sessions)
891 {
892 if (s->vep.vep_sh != ~0)
893 {
894 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
895 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
896 }
897 if (s->vep.next_sh != ~0)
898 {
899 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
900 &session_index);
901 s->vep.next_sh = vcl_session_handle_from_index (session_index);
902 }
903 if (s->vep.prev_sh != ~0)
904 {
905 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
906 &session_index);
907 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
908 }
909 }
910}
911
Florin Corasf9240dc2019-01-15 08:03:17 -0800912void
913vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
914{
Florin Coras243edd52020-03-04 22:20:12 +0000915 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
Florin Coras5e618432021-07-26 18:19:25 -0700916 vcl_worker_t *vcl_wrk = vcl_worker_get_current ();
hanlinf8e13632020-08-21 11:05:36 +0800917 u32 vls_index, session_index, wrk_index;
918 vcl_session_handle_t sh;
liuyacan603e1a42021-07-22 15:52:01 +0800919 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -0800920
Florin Coras243edd52020-03-04 22:20:12 +0000921 /*
922 * init vcl worker
923 */
Florin Coras5e618432021-07-26 18:19:25 -0700924 vcl_wrk->sessions = pool_dup (parent_wrk->sessions);
925 vcl_wrk->session_index_by_vpp_handles =
Florin Corasf9240dc2019-01-15 08:03:17 -0800926 hash_dup (parent_wrk->session_index_by_vpp_handles);
927
Florin Coras243edd52020-03-04 22:20:12 +0000928 /*
929 * init vls worker
930 */
931 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
Florin Coras5e618432021-07-26 18:19:25 -0700932
933 /* clang-format off */
934 hash_foreach (sh, vls_index, vls_parent_wrk->sh_to_vlsh_table, ({
935 vcl_session_handle_parse (sh, &wrk_index, &session_index);
936 hash_set (vls_wrk->sh_to_vlsh_table,
937 vcl_session_handle_from_index (session_index), vls_index);
938 }));
939 /* clang-format on */
Florin Coras243edd52020-03-04 22:20:12 +0000940 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
Florin Coras2d675d72019-01-28 15:54:27 -0800941
liuyacan603e1a42021-07-22 15:52:01 +0800942 /*
943 * Detach vls from parent vcl worker and attach them to child.
944 */
945 pool_foreach (vls, vls_wrk->vls_pool)
946 {
Florin Coras5e618432021-07-26 18:19:25 -0700947 vls->vcl_wrk_index = vcl_wrk->wrk_index;
liuyacan603e1a42021-07-22 15:52:01 +0800948 }
949
wanghanlin5788a342021-06-22 17:34:08 +0800950 /* Validate vep's handle */
Florin Coras5e618432021-07-26 18:19:25 -0700951 vls_validate_veps (vcl_wrk);
wanghanlin5788a342021-06-22 17:34:08 +0800952
Florin Coras243edd52020-03-04 22:20:12 +0000953 vls_share_sessions (vls_parent_wrk, vls_wrk);
Florin Corasf9240dc2019-01-15 08:03:17 -0800954}
955
Florin Coras0ef8ef22019-01-18 08:37:13 -0800956static void
957vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
958{
959 vcl_worker_t *wrk = vcl_worker_get_current ();
960 vcl_session_t *s = 0;
961 int is_nonblk = 0;
962
963 if (vls)
964 {
965 s = vcl_session_get (wrk, vls->session_index);
966 if (PREDICT_FALSE (!s))
967 return;
Florin Corasac422d62020-10-19 20:51:36 -0700968 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800969 }
970
971 switch (op)
972 {
973 case VLS_MT_OP_READ:
974 if (!is_nonblk)
975 is_nonblk = vcl_session_read_ready (s) != 0;
976 if (!is_nonblk)
977 {
978 vls_mt_mq_lock ();
979 *locks_acq |= VLS_MT_LOCK_MQ;
980 }
981 break;
982 case VLS_MT_OP_WRITE:
Florin Coras78b5fa62019-02-21 20:04:15 -0800983 ASSERT (s);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800984 if (!is_nonblk)
985 is_nonblk = vcl_session_write_ready (s) != 0;
986 if (!is_nonblk)
987 {
988 vls_mt_mq_lock ();
989 *locks_acq |= VLS_MT_LOCK_MQ;
990 }
991 break;
992 case VLS_MT_OP_XPOLL:
993 vls_mt_mq_lock ();
994 *locks_acq |= VLS_MT_LOCK_MQ;
995 break;
996 case VLS_MT_OP_SPOOL:
997 vls_mt_spool_lock ();
998 *locks_acq |= VLS_MT_LOCK_SPOOL;
999 break;
1000 default:
1001 break;
1002 }
1003}
1004
1005static void
1006vls_mt_rel_locks (int locks_acq)
1007{
1008 if (locks_acq & VLS_MT_LOCK_MQ)
1009 vls_mt_mq_unlock ();
1010 if (locks_acq & VLS_MT_LOCK_SPOOL)
1011 vls_mt_create_unlock ();
1012}
1013
Florin Corasff40d8f2020-08-11 22:05:28 -07001014static inline u8
1015vls_mt_session_should_migrate (vcl_locked_session_t * vls)
1016{
Florin Coras5e618432021-07-26 18:19:25 -07001017 return (vls_mt_wrk_supported () &&
1018 vls->vcl_wrk_index != vcl_get_worker_index ());
Florin Corasff40d8f2020-08-11 22:05:28 -07001019}
1020
wanghanlindcacdc42020-12-28 16:19:05 +08001021static vcl_locked_session_t *
1022vls_mt_session_migrate (vcl_locked_session_t *vls)
hanlina3a48962020-07-13 11:09:15 +08001023{
1024 u32 wrk_index = vcl_get_worker_index ();
1025 vcl_worker_t *wrk;
wanghanline8f848a2021-01-08 14:57:11 +08001026 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001027 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +08001028 vcl_session_t *session;
1029 uword *p;
1030
Florin Coras5e618432021-07-26 18:19:25 -07001031 ASSERT (vls_mt_wrk_supported () && vls->vcl_wrk_index != wrk_index);
hanlina3a48962020-07-13 11:09:15 +08001032
Florin Corasff40d8f2020-08-11 22:05:28 -07001033 /*
1034 * VCL session on current vcl worker already allocated. Update current
1035 * owner worker and index and return
1036 */
hanlina3a48962020-07-13 11:09:15 +08001037 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
1038 {
Florin Coras5e618432021-07-26 18:19:25 -07001039 vls->vcl_wrk_index = wrk_index;
hanlina3a48962020-07-13 11:09:15 +08001040 vls->session_index = (u32) p[0];
wanghanlindcacdc42020-12-28 16:19:05 +08001041 return vls;
hanlina3a48962020-07-13 11:09:15 +08001042 }
1043
Florin Corasff40d8f2020-08-11 22:05:28 -07001044 /*
1045 * Ask vcl worker that owns the original vcl session to clone it into
1046 * current vcl worker session pool
1047 */
1048
hanlina3a48962020-07-13 11:09:15 +08001049 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
1050 vls->owner_vcl_wrk_index)))
1051 {
1052 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
1053 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001054 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -07001055 vls_mt_pool_runlock ();
wanghanlindcacdc42020-12-28 16:19:05 +08001056 return 0;
hanlina3a48962020-07-13 11:09:15 +08001057 }
1058
1059 src_sid = (u32) p[0];
1060 wrk = vcl_worker_get_current ();
1061 session = vcl_session_alloc (wrk);
1062 sid = session->session_index;
hanlina3a48962020-07-13 11:09:15 +08001063 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
1064 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
1065
wanghanlindcacdc42020-12-28 16:19:05 +08001066 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
1067 vls_index = vls->vls_index;
1068 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
1069 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -07001070 vls_mt_pool_runlock ();
wanghanlindcacdc42020-12-28 16:19:05 +08001071 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
1072 own_vcl_wrk_index, vls_index, src_sid);
1073
1074 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
hanlina3a48962020-07-13 11:09:15 +08001075 {
wanghanlindcacdc42020-12-28 16:19:05 +08001076 VWRN ("session %u not exist", src_sid);
1077 goto err;
1078 }
1079 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
1080 {
1081 VWRN ("failed to wait rpc response");
1082 goto err;
1083 }
1084 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
1085 session->vep.next_sh != ~0))
1086 {
hanlina3a48962020-07-13 11:09:15 +08001087 VERR ("can't migrate nonempty epoll session");
1088 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001089 goto err;
hanlina3a48962020-07-13 11:09:15 +08001090 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001091 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
Florin Corasc127d5a2020-10-14 16:35:58 -07001092 session->session_state != VCL_STATE_CLOSED))
hanlina3a48962020-07-13 11:09:15 +08001093 {
hanlina3a48962020-07-13 11:09:15 +08001094 VERR ("migrate NOT supported, session_status (%u)",
1095 session->session_state);
1096 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001097 goto err;
hanlina3a48962020-07-13 11:09:15 +08001098 }
wanghanlindcacdc42020-12-28 16:19:05 +08001099
1100 vls = vls_get_w_dlock (vls_index);
1101 if (PREDICT_FALSE (!vls))
1102 {
1103 VWRN ("failed to get vls %u", vls_index);
1104 goto err;
1105 }
1106
1107 session->session_index = sid;
Florin Coras5e618432021-07-26 18:19:25 -07001108 vls->vcl_wrk_index = wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001109 vls->session_index = sid;
1110 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
wanghanline8f848a2021-01-08 14:57:11 +08001111 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1112 vls->vls_index);
wanghanlindcacdc42020-12-28 16:19:05 +08001113 return vls;
1114
1115err:
1116 vcl_session_free (wrk, session);
1117 return 0;
hanlina3a48962020-07-13 11:09:15 +08001118}
1119
Florin Corasff40d8f2020-08-11 22:05:28 -07001120static inline void
1121vls_mt_detect (void)
1122{
1123 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1124 vls_mt_add ();
1125}
Florin Coras0ef8ef22019-01-18 08:37:13 -08001126
wanghanlindcacdc42020-12-28 16:19:05 +08001127#define vls_mt_guard(_vls, _op) \
1128 int _locks_acq = 0; \
1129 if (vls_mt_wrk_supported ()) \
1130 { \
1131 if (PREDICT_FALSE (_vls && \
Florin Coras5e618432021-07-26 18:19:25 -07001132 ((vcl_locked_session_t *) _vls)->vcl_wrk_index != \
wanghanlindcacdc42020-12-28 16:19:05 +08001133 vcl_get_worker_index ())) \
1134 { \
1135 _vls = vls_mt_session_migrate (_vls); \
1136 if (PREDICT_FALSE (!_vls)) \
1137 return VPPCOM_EBADFD; \
1138 } \
1139 } \
1140 else \
1141 { \
1142 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1143 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1144 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001145
1146#define vls_mt_unguard() \
1147 if (PREDICT_FALSE (_locks_acq)) \
Florin Coras0ef8ef22019-01-18 08:37:13 -08001148 vls_mt_rel_locks (_locks_acq)
1149
Florin Coras7baeb712019-01-04 17:05:43 -08001150int
1151vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1152{
1153 vcl_locked_session_t *vls;
1154 int rv;
1155
Florin Corasff40d8f2020-08-11 22:05:28 -07001156 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001157 if (!(vls = vls_get_w_dlock (vlsh)))
1158 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001159
1160 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001161 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001162 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001163 vls_get_and_unlock (vlsh);
1164 return rv;
1165}
1166
1167int
1168vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1169{
1170 vcl_locked_session_t *vls;
1171 int rv;
1172
Florin Corasff40d8f2020-08-11 22:05:28 -07001173 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001174 if (!(vls = vls_get_w_dlock (vlsh)))
1175 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001176 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001177 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001178 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001179 vls_get_and_unlock (vlsh);
1180 return rv;
1181}
1182
1183int
1184vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1185 vppcom_endpt_t * ep)
1186{
1187 vcl_locked_session_t *vls;
1188 int rv;
1189
Florin Corasff40d8f2020-08-11 22:05:28 -07001190 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001191 if (!(vls = vls_get_w_dlock (vlsh)))
1192 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001193 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001194 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001195 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001196 vls_get_and_unlock (vlsh);
1197 return rv;
1198}
1199
1200ssize_t
1201vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1202{
1203 vcl_locked_session_t *vls;
1204 int rv;
1205
Florin Corasff40d8f2020-08-11 22:05:28 -07001206 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001207 if (!(vls = vls_get_w_dlock (vlsh)))
1208 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001209 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001210 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001211 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001212 vls_get_and_unlock (vlsh);
1213 return rv;
1214}
1215
1216ssize_t
1217vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1218 vppcom_endpt_t * ep)
1219{
1220 vcl_locked_session_t *vls;
1221 int rv;
1222
Florin Corasff40d8f2020-08-11 22:05:28 -07001223 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001224 if (!(vls = vls_get_w_dlock (vlsh)))
1225 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001226 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001227 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1228 ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001229 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001230 vls_get_and_unlock (vlsh);
1231 return rv;
1232}
1233
1234int
1235vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1236{
1237 vcl_locked_session_t *vls;
1238 int rv;
1239
Florin Corasff40d8f2020-08-11 22:05:28 -07001240 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001241 if (!(vls = vls_get_w_dlock (vlsh)))
1242 return VPPCOM_EBADFD;
Florin Corasff40d8f2020-08-11 22:05:28 -07001243 if (vls_mt_session_should_migrate (vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001244 {
1245 vls = vls_mt_session_migrate (vls);
1246 if (PREDICT_FALSE (!vls))
1247 return VPPCOM_EBADFD;
1248 }
Florin Coras7baeb712019-01-04 17:05:43 -08001249 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1250 vls_get_and_unlock (vlsh);
1251 return rv;
1252}
1253
1254int
1255vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1256{
1257 vcl_locked_session_t *vls;
1258 int rv;
1259
Florin Corasff40d8f2020-08-11 22:05:28 -07001260 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001261 if (!(vls = vls_get_w_dlock (vlsh)))
1262 return VPPCOM_EBADFD;
1263 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1264 vls_get_and_unlock (vlsh);
1265 return rv;
1266}
1267
1268int
1269vls_listen (vls_handle_t vlsh, int q_len)
1270{
1271 vcl_locked_session_t *vls;
1272 int rv;
1273
Florin Corasff40d8f2020-08-11 22:05:28 -07001274 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001275 if (!(vls = vls_get_w_dlock (vlsh)))
1276 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001277 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001278 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001279 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001280 vls_get_and_unlock (vlsh);
1281 return rv;
1282}
1283
1284int
1285vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1286{
1287 vcl_locked_session_t *vls;
1288 int rv;
1289
Florin Corasff40d8f2020-08-11 22:05:28 -07001290 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001291 if (!(vls = vls_get_w_dlock (vlsh)))
1292 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001293 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001294 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001295 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001296 vls_get_and_unlock (vlsh);
1297 return rv;
1298}
1299
Florin Coras2d675d72019-01-28 15:54:27 -08001300static inline void
1301vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1302{
1303 vcl_worker_t *wrk = vcl_worker_get_current ();
1304 vcl_session_t *s;
Florin Coras243edd52020-03-04 22:20:12 +00001305 u32 owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -08001306
Mohamed Feroz88c836c2024-08-29 08:04:02 +00001307 if (vls_mt_wrk_supported () && vls_n_workers () <= 1)
hanlina3a48962020-07-13 11:09:15 +08001308 return;
1309
Florin Coras5e618432021-07-26 18:19:25 -07001310 ASSERT (wrk->wrk_index == vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001311 s = vcl_session_get (wrk, vls->session_index);
1312 switch (s->session_state)
1313 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001314 case VCL_STATE_LISTEN:
Florin Coras2d675d72019-01-28 15:54:27 -08001315 if (is_add)
1316 {
Florin Coras5e618432021-07-26 18:19:25 -07001317 vls_listener_wrk_set (vls, vls->vcl_wrk_index, 1 /* is_active */);
Florin Coras2d675d72019-01-28 15:54:27 -08001318 break;
1319 }
Florin Corasa41a0b52023-03-01 22:22:30 -08001320 /* Although removal from epoll means listener no longer accepts new
1321 * sessions, the accept queue built by vpp cannot be drained by stopping
1322 * the listener. Morover, some applications, e.g., nginx, might
1323 * constantly remove and add listeners to their epfds. Removing
1324 * listeners in such situations causes a lot of churn in vpp as segments
1325 * and segment managers need to be recreated. */
1326 /* vls_listener_wrk_stop_listen (vls, vls->vcl_wrk_index); */
Florin Coras2d675d72019-01-28 15:54:27 -08001327 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001328 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -08001329 if (!is_add)
1330 break;
1331
1332 /* Register worker as listener */
Florin Coras5e618432021-07-26 18:19:25 -07001333 vls_listener_wrk_start_listen (vls, vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001334
1335 /* If owner worker did not attempt to accept/xpoll on the session,
1336 * force a listen stop for it, since it may not be interested in
1337 * accepting new sessions.
1338 * This is pretty much a hack done to give app workers the illusion
1339 * that it is fine to listen and not accept new sessions for a
1340 * given listener. Without it, we would accumulate unhandled
1341 * accepts on the passive worker message queue. */
Florin Coras243edd52020-03-04 22:20:12 +00001342 owner_wrk = vls_shared_get_owner (vls);
1343 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1344 vls_listener_wrk_stop_listen (vls, owner_wrk);
Florin Coras2d675d72019-01-28 15:54:27 -08001345 break;
1346 default:
1347 break;
1348 }
1349}
1350
Florin Coras7baeb712019-01-04 17:05:43 -08001351vls_handle_t
1352vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1353{
1354 vls_handle_t accepted_vlsh;
1355 vcl_locked_session_t *vls;
1356 int sh;
1357
Florin Corasff40d8f2020-08-11 22:05:28 -07001358 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001359 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1360 return VPPCOM_EBADFD;
Florin Coras2d675d72019-01-28 15:54:27 -08001361 if (vcl_n_workers () > 1)
1362 vls_mp_checks (vls, 1 /* is_add */ );
Florin Coras0ef8ef22019-01-18 08:37:13 -08001363 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001364 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001365 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001366 vls_get_and_unlock (listener_vlsh);
1367 if (sh < 0)
1368 return sh;
1369 accepted_vlsh = vls_alloc (sh);
1370 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1371 vppcom_session_close (sh);
1372 return accepted_vlsh;
1373}
1374
1375vls_handle_t
1376vls_create (uint8_t proto, uint8_t is_nonblocking)
1377{
1378 vcl_session_handle_t sh;
1379 vls_handle_t vlsh;
wanghanlindcacdc42020-12-28 16:19:05 +08001380 vcl_locked_session_t *vls = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001381
Florin Corasff40d8f2020-08-11 22:05:28 -07001382 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001383 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001384 sh = vppcom_session_create (proto, is_nonblocking);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001385 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001386 if (sh == INVALID_SESSION_ID)
1387 return VLS_INVALID_HANDLE;
1388
1389 vlsh = vls_alloc (sh);
1390 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1391 vppcom_session_close (sh);
1392
1393 return vlsh;
1394}
1395
hanlina3a48962020-07-13 11:09:15 +08001396static void
Florin Corasff40d8f2020-08-11 22:05:28 -07001397vls_mt_session_cleanup (vcl_locked_session_t * vls)
hanlina3a48962020-07-13 11:09:15 +08001398{
Florin Corasff40d8f2020-08-11 22:05:28 -07001399 u32 session_index, wrk_index, current_vcl_wrk;
hanlina3a48962020-07-13 11:09:15 +08001400 vcl_worker_t *wrk = vcl_worker_get_current ();
1401
Florin Corasff40d8f2020-08-11 22:05:28 -07001402 ASSERT (vls_mt_wrk_supported ());
1403
1404 current_vcl_wrk = vcl_get_worker_index ();
hanlina3a48962020-07-13 11:09:15 +08001405
hanlina3a48962020-07-13 11:09:15 +08001406 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1407 ({
Florin Corasff40d8f2020-08-11 22:05:28 -07001408 if (current_vcl_wrk != wrk_index)
1409 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
hanlina3a48962020-07-13 11:09:15 +08001410 }));
hanlina3a48962020-07-13 11:09:15 +08001411 hash_free (vls->vcl_wrk_index_to_session_index);
1412}
1413
Florin Coras7baeb712019-01-04 17:05:43 -08001414int
1415vls_close (vls_handle_t vlsh)
1416{
1417 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -08001418 int rv;
Florin Coras7baeb712019-01-04 17:05:43 -08001419
Florin Corasff40d8f2020-08-11 22:05:28 -07001420 vls_mt_detect ();
Florin Coras5e618432021-07-26 18:19:25 -07001421 vls_mt_pool_wlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001422
Florin Coras0ef8ef22019-01-18 08:37:13 -08001423 vls = vls_get_and_lock (vlsh);
1424 if (!vls)
1425 {
Florin Coras5e618432021-07-26 18:19:25 -07001426 vls_mt_pool_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001427 return VPPCOM_EBADFD;
1428 }
1429
hanlina3a48962020-07-13 11:09:15 +08001430 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Corasf9240dc2019-01-15 08:03:17 -08001431
Florin Coras243edd52020-03-04 22:20:12 +00001432 if (vls_is_shared (vls))
1433 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1434 else
1435 rv = vppcom_session_close (vls_to_sh (vls));
1436
Florin Corasff40d8f2020-08-11 22:05:28 -07001437 if (vls_mt_wrk_supported ())
1438 vls_mt_session_cleanup (vls);
hanlina3a48962020-07-13 11:09:15 +08001439
Florin Coras0ef8ef22019-01-18 08:37:13 -08001440 vls_free (vls);
1441 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001442
Florin Coras5e618432021-07-26 18:19:25 -07001443 vls_mt_pool_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001444
Florin Coras7baeb712019-01-04 17:05:43 -08001445 return rv;
1446}
1447
liuyacan534468e2021-05-09 03:50:40 +00001448int
liuyacan55c952e2021-06-13 14:54:55 +08001449vls_shutdown (vls_handle_t vlsh, int how)
liuyacan534468e2021-05-09 03:50:40 +00001450{
1451 vcl_locked_session_t *vls;
1452 int rv;
1453
1454 vls_mt_detect ();
1455 if (!(vls = vls_get_w_dlock (vlsh)))
1456 return VPPCOM_EBADFD;
1457
1458 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
liuyacan6fc07b42021-07-24 22:48:36 +08001459 rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
liuyacan534468e2021-05-09 03:50:40 +00001460 vls_mt_unguard ();
1461 vls_get_and_unlock (vlsh);
1462
1463 return rv;
1464}
1465
Florin Coras7baeb712019-01-04 17:05:43 -08001466vls_handle_t
1467vls_epoll_create (void)
1468{
1469 vcl_session_handle_t sh;
1470 vls_handle_t vlsh;
1471
Florin Corasff40d8f2020-08-11 22:05:28 -07001472 vls_mt_detect ();
Florin Coras63d3ac62019-03-29 08:29:25 -07001473
Florin Coras7baeb712019-01-04 17:05:43 -08001474 sh = vppcom_epoll_create ();
1475 if (sh == INVALID_SESSION_ID)
1476 return VLS_INVALID_HANDLE;
1477
1478 vlsh = vls_alloc (sh);
1479 if (vlsh == VLS_INVALID_HANDLE)
1480 vppcom_session_close (sh);
1481
1482 return vlsh;
1483}
1484
Florin Coras2d675d72019-01-28 15:54:27 -08001485static void
1486vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1487{
nandfandc2632e2021-03-26 16:46:58 +08001488 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
Florin Coras2d675d72019-01-28 15:54:27 -08001489 return;
1490
Florin Coras2d675d72019-01-28 15:54:27 -08001491 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1492}
1493
Florin Coras7baeb712019-01-04 17:05:43 -08001494int
1495vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1496 struct epoll_event *event)
1497{
1498 vcl_locked_session_t *ep_vls, *vls;
1499 vcl_session_handle_t ep_sh, sh;
1500 int rv;
1501
Florin Corasff40d8f2020-08-11 22:05:28 -07001502 vls_mt_detect ();
Florin Coras5e618432021-07-26 18:19:25 -07001503 vls_mt_pool_rlock ();
Florin Coras54223ee2022-03-02 21:06:30 -08001504
Florin Coras7baeb712019-01-04 17:05:43 -08001505 ep_vls = vls_get_and_lock (ep_vlsh);
Florin Coras54223ee2022-03-02 21:06:30 -08001506 if (PREDICT_FALSE (!ep_vls))
1507 {
1508 vls_mt_pool_runlock ();
1509 return VPPCOM_EBADFD;
1510 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001511
1512 if (vls_mt_session_should_migrate (ep_vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001513 {
1514 ep_vls = vls_mt_session_migrate (ep_vls);
1515 if (PREDICT_FALSE (!ep_vls))
Florin Coras54223ee2022-03-02 21:06:30 -08001516 {
1517 vls_mt_pool_runlock ();
1518 return VPPCOM_EBADFD;
1519 }
1520 }
1521
1522 vls = vls_get_and_lock (vlsh);
1523 if (PREDICT_FALSE (!vls))
1524 {
1525 vls_unlock (ep_vls);
1526 vls_mt_pool_runlock ();
1527 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001528 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001529
Florin Coras7baeb712019-01-04 17:05:43 -08001530 ep_sh = vls_to_sh (ep_vls);
1531 sh = vls_to_sh (vls);
Florin Coras2d675d72019-01-28 15:54:27 -08001532
nandfandc2632e2021-03-26 16:46:58 +08001533 vls_epoll_ctl_mp_checks (vls, op);
Florin Coras5e618432021-07-26 18:19:25 -07001534 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001535 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1536
Florin Coras5e618432021-07-26 18:19:25 -07001537 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001538 ep_vls = vls_get (ep_vlsh);
1539 vls = vls_get (vlsh);
1540 vls_unlock (vls);
1541 vls_unlock (ep_vls);
Florin Coras5e618432021-07-26 18:19:25 -07001542 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001543 return rv;
1544}
1545
1546int
1547vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1548 int maxevents, double wait_for_time)
1549{
wanghanlindcacdc42020-12-28 16:19:05 +08001550 vcl_locked_session_t *vls, *vls_tmp = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001551 int rv;
1552
Florin Corasff40d8f2020-08-11 22:05:28 -07001553 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001554 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1555 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001556 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001557 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1558 wait_for_time);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001559 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001560 vls_get_and_unlock (ep_vlsh);
wanghanlind72a0342021-05-12 17:00:29 +08001561 vls_handle_pending_wrk_cleanup ();
Florin Coras7baeb712019-01-04 17:05:43 -08001562 return rv;
1563}
1564
Florin Coras2d675d72019-01-28 15:54:27 -08001565static void
1566vls_select_mp_checks (vcl_si_set * read_map)
1567{
1568 vcl_locked_session_t *vls;
1569 vcl_worker_t *wrk;
1570 vcl_session_t *s;
1571 u32 si;
1572
1573 if (vcl_n_workers () <= 1)
1574 {
1575 vlsl->select_mp_check = 1;
1576 return;
1577 }
1578
1579 if (!read_map)
1580 return;
1581
1582 vlsl->select_mp_check = 1;
1583 wrk = vcl_worker_get_current ();
1584
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001585 clib_bitmap_foreach (si, read_map) {
Florin Coras2d675d72019-01-28 15:54:27 -08001586 s = vcl_session_get (wrk, si);
Florin Corasc127d5a2020-10-14 16:35:58 -07001587 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -08001588 {
1589 vls = vls_get (vls_session_index_to_vlsh (si));
1590 vls_mp_checks (vls, 1 /* is_add */);
1591 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001592 }
Florin Coras2d675d72019-01-28 15:54:27 -08001593}
1594
Florin Coras0ef8ef22019-01-18 08:37:13 -08001595int
1596vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1597 vcl_si_set * except_map, double wait_for_time)
1598{
1599 int rv;
wanghanlindcacdc42020-12-28 16:19:05 +08001600 vcl_locked_session_t *vls = NULL;
Florin Coras2d675d72019-01-28 15:54:27 -08001601
Florin Corasff40d8f2020-08-11 22:05:28 -07001602 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001603 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras2d675d72019-01-28 15:54:27 -08001604 if (PREDICT_FALSE (!vlsl->select_mp_check))
1605 vls_select_mp_checks (read_map);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001606 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1607 vls_mt_unguard ();
wanghanlind72a0342021-05-12 17:00:29 +08001608 vls_handle_pending_wrk_cleanup ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001609 return rv;
1610}
1611
Florin Corasf9240dc2019-01-15 08:03:17 -08001612static void
Florin Coras0ef8ef22019-01-18 08:37:13 -08001613vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1614{
1615 u32 current_wrk, is_current;
1616 vcl_locked_session_t *vls;
1617 vcl_session_t *s;
1618
Florin Coras14ed6df2019-03-06 21:13:42 -08001619 if (pool_elts (vcm->workers) <= 1)
1620 return;
1621
Florin Coras0ef8ef22019-01-18 08:37:13 -08001622 current_wrk = vcl_get_worker_index ();
1623 is_current = current_wrk == wrk->wrk_index;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001624
Damjan Marionb2c31b62020-12-13 21:47:40 +01001625 pool_foreach (s, wrk->sessions) {
hanlinf8e13632020-08-21 11:05:36 +08001626 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -08001627 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1628 vls_unshare_session (vls, wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001629 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001630}
1631
1632static void
1633vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1634{
Florin Coras243edd52020-03-04 22:20:12 +00001635 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1636
Florin Coras0ef8ef22019-01-18 08:37:13 -08001637 /* Unshare sessions and also cleanup worker since child may have
1638 * called _exit () and therefore vcl may not catch the event */
1639 vls_unshare_vcl_worker_sessions (wrk);
wanghanlinb940fd42021-06-29 16:01:55 +08001640
1641 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1642 * may have been closed, so DONOT notify VPP.
1643 */
1644 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
Florin Coras243edd52020-03-04 22:20:12 +00001645
1646 vls_worker_free (vls_wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001647}
1648
1649static void
Florin Corasf9240dc2019-01-15 08:03:17 -08001650vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1651{
1652 vcl_worker_t *sub_child;
1653 int tries = 0;
1654
1655 if (child_wrk->forked_child != ~0)
1656 {
1657 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1658 if (sub_child)
1659 {
1660 /* Wait a bit, maybe the process is going away */
1661 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1662 usleep (1e3);
1663 if (kill (sub_child->current_pid, 0) < 0)
1664 vls_cleanup_forked_child (child_wrk, sub_child);
1665 }
1666 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001667 vls_cleanup_vcl_worker (child_wrk);
1668 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001669 wrk->forked_child = ~0;
1670}
1671
wanghanlind72a0342021-05-12 17:00:29 +08001672static void
1673vls_handle_pending_wrk_cleanup (void)
1674{
1675 u32 *wip;
1676 vcl_worker_t *child_wrk, *wrk;
1677 vls_worker_t *vls_wrk = vls_worker_get_current ();
1678
Florin Coras5e618432021-07-26 18:19:25 -07001679 if (PREDICT_TRUE (vec_len (vls_wrk->pending_vcl_wrk_cleanup) == 0))
wanghanlind72a0342021-05-12 17:00:29 +08001680 return;
1681
1682 wrk = vcl_worker_get_current ();
Florin Coras5e618432021-07-26 18:19:25 -07001683 vec_foreach (wip, vls_wrk->pending_vcl_wrk_cleanup)
wanghanlind72a0342021-05-12 17:00:29 +08001684 {
1685 child_wrk = vcl_worker_get_if_valid (*wip);
1686 if (!child_wrk)
1687 continue;
1688 vls_cleanup_forked_child (wrk, child_wrk);
1689 }
Florin Coras5e618432021-07-26 18:19:25 -07001690 vec_reset_length (vls_wrk->pending_vcl_wrk_cleanup);
wanghanlind72a0342021-05-12 17:00:29 +08001691}
1692
Florin Corasf9240dc2019-01-15 08:03:17 -08001693static struct sigaction old_sa;
1694
1695static void
1696vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1697{
1698 vcl_worker_t *wrk, *child_wrk;
wanghanlind72a0342021-05-12 17:00:29 +08001699 vls_worker_t *vls_wrk;
Florin Corasf9240dc2019-01-15 08:03:17 -08001700
1701 if (vcl_get_worker_index () == ~0)
1702 return;
1703
1704 if (sigaction (SIGCHLD, &old_sa, 0))
1705 {
1706 VERR ("couldn't restore sigchld");
1707 exit (-1);
1708 }
1709
1710 wrk = vcl_worker_get_current ();
1711 if (wrk->forked_child == ~0)
1712 return;
1713
1714 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1715 if (!child_wrk)
1716 goto done;
1717
1718 if (si && si->si_pid != child_wrk->current_pid)
1719 {
1720 VDBG (0, "unexpected child pid %u", si->si_pid);
1721 goto done;
1722 }
wanghanlind72a0342021-05-12 17:00:29 +08001723
1724 /* Parent process may enter sighandler with a lock, such as lock in localtime
1725 * or in mspace_free, and child wrk cleanup may try to get such locks and
1726 * cause deadlock.
1727 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1728 */
1729 vls_wrk = vls_worker_get_current ();
Florin Coras5e618432021-07-26 18:19:25 -07001730 vec_add1 (vls_wrk->pending_vcl_wrk_cleanup, child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001731
1732done:
1733 if (old_sa.sa_flags & SA_SIGINFO)
1734 {
1735 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1736 fn (signum, si, uc);
1737 }
1738 else
1739 {
1740 void (*fn) (int) = old_sa.sa_handler;
1741 if (fn)
1742 fn (signum);
1743 }
1744}
1745
1746static void
1747vls_incercept_sigchld ()
1748{
1749 struct sigaction sa;
nandfan5fdc47c2021-02-22 17:17:17 +08001750 if (old_sa.sa_sigaction)
1751 {
1752 VDBG (0, "have intercepted sigchld");
1753 return;
1754 }
Florin Corasf9240dc2019-01-15 08:03:17 -08001755 clib_memset (&sa, 0, sizeof (sa));
1756 sa.sa_sigaction = vls_intercept_sigchld_handler;
1757 sa.sa_flags = SA_SIGINFO;
1758 if (sigaction (SIGCHLD, &sa, &old_sa))
1759 {
1760 VERR ("couldn't intercept sigchld");
1761 exit (-1);
1762 }
1763}
1764
1765static void
1766vls_app_pre_fork (void)
1767{
1768 vls_incercept_sigchld ();
1769 vcl_flush_mq_events ();
1770}
1771
1772static void
1773vls_app_fork_child_handler (void)
1774{
1775 vcl_worker_t *parent_wrk;
Florin Corasb88de902020-09-08 16:47:57 -07001776 int parent_wrk_index;
Florin Corasf9240dc2019-01-15 08:03:17 -08001777
1778 parent_wrk_index = vcl_get_worker_index ();
1779 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1780 parent_wrk_index);
1781
1782 /*
Florin Corasb88de902020-09-08 16:47:57 -07001783 * Clear old state
Florin Corasf9240dc2019-01-15 08:03:17 -08001784 */
1785 vcl_set_worker_index (~0);
Florin Corasf9240dc2019-01-15 08:03:17 -08001786
1787 /*
Florin Corasb88de902020-09-08 16:47:57 -07001788 * Allocate and register vcl worker with vpp
Florin Corasf9240dc2019-01-15 08:03:17 -08001789 */
Florin Corasb88de902020-09-08 16:47:57 -07001790 if (vppcom_worker_register ())
Florin Corasf9240dc2019-01-15 08:03:17 -08001791 {
Florin Corasb88de902020-09-08 16:47:57 -07001792 VERR ("couldn't register new worker!");
Florin Corasf9240dc2019-01-15 08:03:17 -08001793 return;
1794 }
1795
1796 /*
Florin Corasb88de902020-09-08 16:47:57 -07001797 * Allocate/initialize vls worker and share sessions
Florin Coras243edd52020-03-04 22:20:12 +00001798 */
1799 vls_worker_alloc ();
Florin Corasf9240dc2019-01-15 08:03:17 -08001800
Florin Coras0ef8ef22019-01-18 08:37:13 -08001801 /* Reset number of threads and set wrk index */
Florin Coras2d675d72019-01-28 15:54:27 -08001802 vlsl->vls_mt_n_threads = 0;
1803 vlsl->vls_wrk_index = vcl_get_worker_index ();
1804 vlsl->select_mp_check = 0;
Florin Coras2e2f9df2021-07-27 22:48:05 -07001805 clib_rwlock_init (&vlsl->vls_pool_lock);
Florin Coras2d675d72019-01-28 15:54:27 -08001806 vls_mt_locks_init ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001807
liuyacan603e1a42021-07-22 15:52:01 +08001808 parent_wrk = vcl_worker_get (parent_wrk_index);
1809 vls_worker_copy_on_fork (parent_wrk);
1810 parent_wrk->forked_child = vcl_get_worker_index ();
1811
Florin Corasf9240dc2019-01-15 08:03:17 -08001812 VDBG (0, "forked child main worker initialized");
1813 vcm->forking = 0;
1814}
1815
1816static void
1817vls_app_fork_parent_handler (void)
1818{
1819 vcm->forking = 1;
1820 while (vcm->forking)
1821 ;
1822}
1823
Florin Coras0ef8ef22019-01-18 08:37:13 -08001824void
1825vls_app_exit (void)
1826{
Florin Coras243edd52020-03-04 22:20:12 +00001827 vls_worker_t *wrk = vls_worker_get_current ();
1828
wanghanlind72a0342021-05-12 17:00:29 +08001829 /* Handle pending wrk cleanup */
1830 vls_handle_pending_wrk_cleanup ();
1831
Florin Coras0ef8ef22019-01-18 08:37:13 -08001832 /* Unshare the sessions. VCL will clean up the worker */
1833 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
Florin Coras243edd52020-03-04 22:20:12 +00001834 vls_worker_free (wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001835}
1836
Florin Coras40c07ce2020-07-16 20:46:17 -07001837static void
1838vls_clone_and_share_rpc_handler (void *args)
1839{
1840 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1841 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1842 vcl_locked_session_t *vls, *dst_vls;
hanlina3a48962020-07-13 11:09:15 +08001843 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
Florin Coras40c07ce2020-07-16 20:46:17 -07001844 vcl_session_t *s, *dst_s;
1845
wanghanlindcacdc42020-12-28 16:19:05 +08001846 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1847 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1848 msg->origin_session_index);
1849
1850 /* VCL locked session can't been protected, so DONT touch it.
1851 * VCL session may been free, check it.
1852 */
1853 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1854 s = vcl_session_get (vcl_wrk, msg->session_index);
1855 if (PREDICT_FALSE (!s))
1856 {
1857 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1858 return;
1859 }
Florin Coras40c07ce2020-07-16 20:46:17 -07001860
hanlina3a48962020-07-13 11:09:15 +08001861 if (!vls_mt_wrk_supported ())
wanghanlindcacdc42020-12-28 16:19:05 +08001862 {
1863 vls = vls_session_get (wrk, msg->vls_index);
1864 vls_init_share_session (wrk, vls);
1865 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1866 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1867 dst_vls->shared_data_index = vls->shared_data_index;
1868 }
hanlina3a48962020-07-13 11:09:15 +08001869 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
Florin Coras40c07ce2020-07-16 20:46:17 -07001870 clib_memcpy (dst_s, s, sizeof (*s));
1871
wanghanlindcacdc42020-12-28 16:19:05 +08001872 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
hanlina3a48962020-07-13 11:09:15 +08001873}
1874
1875static void
1876vls_session_cleanup_rpc_handler (void *args)
1877{
1878 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1879 vcl_worker_t *wrk = vcl_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +08001880 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001881 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
hanlina3a48962020-07-13 11:09:15 +08001882
wanghanlindcacdc42020-12-28 16:19:05 +08001883 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1884 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
hanlina3a48962020-07-13 11:09:15 +08001885
wanghanlindcacdc42020-12-28 16:19:05 +08001886 vppcom_session_close (sh);
wanghanline8f848a2021-01-08 14:57:11 +08001887 vls_sh_to_vlsh_table_del (vls_wrk, sh);
Florin Coras40c07ce2020-07-16 20:46:17 -07001888}
1889
1890static void
1891vls_rpc_handler (void *args)
1892{
1893 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1894 switch (msg->type)
1895 {
1896 case VLS_RPC_CLONE_AND_SHARE:
1897 vls_clone_and_share_rpc_handler (msg->data);
1898 break;
hanlina3a48962020-07-13 11:09:15 +08001899 case VLS_RPC_SESS_CLEANUP:
1900 vls_session_cleanup_rpc_handler (msg->data);
1901 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001902 default:
1903 break;
1904 }
1905}
1906
1907void
wanghanlindcacdc42020-12-28 16:19:05 +08001908vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1909 u32 session_index, u32 vls_wrk_index,
1910 u32 dst_wrk_index, u32 dst_vls_index,
1911 u32 dst_session_index)
Florin Coras40c07ce2020-07-16 20:46:17 -07001912{
1913 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1914 vls_clone_and_share_msg_t *msg;
1915 vls_rpc_msg_t *rpc;
hanlina3a48962020-07-13 11:09:15 +08001916 int ret;
wanghanlindcacdc42020-12-28 16:19:05 +08001917 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
Florin Coras40c07ce2020-07-16 20:46:17 -07001918
1919 rpc = (vls_rpc_msg_t *) & data;
1920 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1921 msg = (vls_clone_and_share_msg_t *) & rpc->data;
hanlina3a48962020-07-13 11:09:15 +08001922 msg->origin_vls_wrk = vls_wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001923 msg->origin_vls_index = origin_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001924 msg->origin_vcl_wrk = wrk->wrk_index;
1925 msg->origin_session_index = session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001926 msg->vls_index = dst_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001927 msg->session_index = dst_session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001928
wanghanlindcacdc42020-12-28 16:19:05 +08001929 /* Try lock and handle rpcs if two threads send each other
1930 * clone requests at the same time.
1931 */
1932 wrk->rpc_done = VLS_RPC_STATE_INIT;
1933 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1934 vcl_flush_mq_events ();
hanlina3a48962020-07-13 11:09:15 +08001935 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1936
Florin Corasff40d8f2020-08-11 22:05:28 -07001937 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001938 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1939 msg->origin_session_index, ret);
wanghanlindcacdc42020-12-28 16:19:05 +08001940 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1941 clib_time_now (&wrk->clib_time) < timeout)
hanlina3a48962020-07-13 11:09:15 +08001942 ;
wanghanlindcacdc42020-12-28 16:19:05 +08001943 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
hanlina3a48962020-07-13 11:09:15 +08001944}
1945
1946void
1947vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1948 u32 dst_wrk_index, u32 dst_session_index)
1949{
1950 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1951 vls_sess_cleanup_msg_t *msg;
1952 vls_rpc_msg_t *rpc;
1953 int ret;
1954
1955 rpc = (vls_rpc_msg_t *) & data;
1956 rpc->type = VLS_RPC_SESS_CLEANUP;
1957 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1958 msg->origin_vcl_wrk = wrk->wrk_index;
1959 msg->session_index = dst_session_index;
1960
hanlina3a48962020-07-13 11:09:15 +08001961 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1962
Florin Corasff40d8f2020-08-11 22:05:28 -07001963 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001964 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
Florin Coras40c07ce2020-07-16 20:46:17 -07001965}
1966
Florin Coras7baeb712019-01-04 17:05:43 -08001967int
1968vls_app_create (char *app_name)
1969{
1970 int rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001971
Florin Coras7baeb712019-01-04 17:05:43 -08001972 if ((rv = vppcom_app_create (app_name)))
1973 return rv;
Florin Coras243edd52020-03-04 22:20:12 +00001974
Florin Coras2d675d72019-01-28 15:54:27 -08001975 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1976 clib_memset (vlsm, 0, sizeof (*vlsm));
Florin Coras243edd52020-03-04 22:20:12 +00001977 clib_rwlock_init (&vlsm->shared_data_lock);
wanghanlindcacdc42020-12-28 16:19:05 +08001978 clib_spinlock_init (&vlsm->worker_rpc_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001979 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1980
Florin Corasf9240dc2019-01-15 08:03:17 -08001981 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1982 vls_app_fork_child_handler);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001983 atexit (vls_app_exit);
Florin Coras243edd52020-03-04 22:20:12 +00001984 vls_worker_alloc ();
Florin Coras2d675d72019-01-28 15:54:27 -08001985 vlsl->vls_wrk_index = vcl_get_worker_index ();
Florin Coras2e2f9df2021-07-27 22:48:05 -07001986 clib_rwlock_init (&vlsl->vls_pool_lock);
Florin Coras2d675d72019-01-28 15:54:27 -08001987 vls_mt_locks_init ();
Florin Coras40c07ce2020-07-16 20:46:17 -07001988 vcm->wrk_rpc_fn = vls_rpc_handler;
Florin Coras7baeb712019-01-04 17:05:43 -08001989 return VPPCOM_OK;
1990}
1991
hanlin4266d4d2020-05-19 17:34:17 +08001992unsigned char
1993vls_use_eventfd (void)
1994{
1995 return vcm->cfg.use_mq_eventfd;
1996}
1997
hanlina3a48962020-07-13 11:09:15 +08001998unsigned char
1999vls_mt_wrk_supported (void)
2000{
Florin Corasff40d8f2020-08-11 22:05:28 -07002001 return vcm->cfg.mt_wrk_supported;
hanlina3a48962020-07-13 11:09:15 +08002002}
2003
2004int
2005vls_use_real_epoll (void)
2006{
2007 if (vcl_get_worker_index () == ~0)
2008 return 0;
2009
2010 return vcl_worker_get_current ()->vcl_needs_real_epoll;
2011}
2012
2013void
2014vls_register_vcl_worker (void)
2015{
wanghanlin492350e2020-09-11 17:19:32 +08002016 vls_mt_add ();
hanlina3a48962020-07-13 11:09:15 +08002017}
2018
Florin Coras7baeb712019-01-04 17:05:43 -08002019/*
2020 * fd.io coding-style-patch-verification: ON
2021 *
2022 * Local Variables:
2023 * eval: (c-set-style "gnu")
2024 * End:
2025 */