blob: 6027fd19c15952eddeb484bc82b5fcb8684b635e [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
377static void
378vls_worker_alloc (void)
379{
380 vls_worker_t *wrk;
381
382 pool_get_zero (vlsm->workers, wrk);
wanghanline8f848a2021-01-08 14:57:11 +0800383 if (vls_mt_wrk_supported ())
384 clib_rwlock_init (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700385 wrk->vcl_wrk_index = vcl_get_worker_index ();
386 vec_validate (wrk->pending_vcl_wrk_cleanup, 16);
387 vec_reset_length (wrk->pending_vcl_wrk_cleanup);
Florin Coras243edd52020-03-04 22:20:12 +0000388}
389
390static void
391vls_worker_free (vls_worker_t * wrk)
392{
Florin Coras5e618432021-07-26 18:19:25 -0700393 hash_free (wrk->sh_to_vlsh_table);
wanghanline8f848a2021-01-08 14:57:11 +0800394 if (vls_mt_wrk_supported ())
395 clib_rwlock_free (&wrk->sh_to_vlsh_table_lock);
Florin Coras243edd52020-03-04 22:20:12 +0000396 pool_free (wrk->vls_pool);
397 pool_put (vlsm->workers, wrk);
398}
399
400static vls_worker_t *
401vls_worker_get (u32 wrk_index)
402{
403 if (pool_is_free_index (vlsm->workers, wrk_index))
404 return 0;
405 return pool_elt_at_index (vlsm->workers, wrk_index);
406}
407
wanghanline8f848a2021-01-08 14:57:11 +0800408static void
409vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
410{
411 if (vls_mt_wrk_supported ())
412 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700413 hash_set (wrk->sh_to_vlsh_table, sh, vlsh);
wanghanline8f848a2021-01-08 14:57:11 +0800414 if (vls_mt_wrk_supported ())
415 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
416}
417
418static void
419vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
420{
421 if (vls_mt_wrk_supported ())
422 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700423 hash_unset (wrk->sh_to_vlsh_table, sh);
wanghanline8f848a2021-01-08 14:57:11 +0800424 if (vls_mt_wrk_supported ())
425 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
426}
427
428static uword *
429vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
430{
431 if (vls_mt_wrk_supported ())
432 clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock);
Florin Coras5e618432021-07-26 18:19:25 -0700433 uword *vlshp = hash_get (wrk->sh_to_vlsh_table, sh);
wanghanline8f848a2021-01-08 14:57:11 +0800434 if (vls_mt_wrk_supported ())
435 clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock);
436 return vlshp;
437}
438
Florin Coras7baeb712019-01-04 17:05:43 -0800439static vls_handle_t
440vls_alloc (vcl_session_handle_t sh)
441{
Florin Coras243edd52020-03-04 22:20:12 +0000442 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800443 vcl_locked_session_t *vls;
444
Florin Coras5e618432021-07-26 18:19:25 -0700445 vls_mt_pool_wlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000446
447 pool_get_zero (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800448 vls->session_index = vppcom_session_index (sh);
Florin Coras5e618432021-07-26 18:19:25 -0700449 vls->vcl_wrk_index = vppcom_session_worker (sh);
Florin Coras243edd52020-03-04 22:20:12 +0000450 vls->vls_index = vls - wrk->vls_pool;
451 vls->shared_data_index = ~0;
wanghanline8f848a2021-01-08 14:57:11 +0800452 vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index);
hanlina3a48962020-07-13 11:09:15 +0800453 if (vls_mt_wrk_supported ())
454 {
Florin Coras5e618432021-07-26 18:19:25 -0700455 hash_set (vls->vcl_wrk_index_to_session_index, vls->vcl_wrk_index,
hanlina3a48962020-07-13 11:09:15 +0800456 vls->session_index);
Florin Coras5e618432021-07-26 18:19:25 -0700457 vls->owner_vcl_wrk_index = vls->vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +0800458 }
Florin Coras7baeb712019-01-04 17:05:43 -0800459 clib_spinlock_init (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000460
Florin Coras5e618432021-07-26 18:19:25 -0700461 vls_mt_pool_wunlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800462 return vls->vls_index;
463}
464
465static vcl_locked_session_t *
466vls_get (vls_handle_t vlsh)
467{
Florin Coras243edd52020-03-04 22:20:12 +0000468 vls_worker_t *wrk = vls_worker_get_current ();
469 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800470 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000471 return pool_elt_at_index (wrk->vls_pool, vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -0800472}
473
474static void
Florin Coras0ef8ef22019-01-18 08:37:13 -0800475vls_free (vcl_locked_session_t * vls)
Florin Coras7baeb712019-01-04 17:05:43 -0800476{
Florin Coras243edd52020-03-04 22:20:12 +0000477 vls_worker_t *wrk = vls_worker_get_current ();
478
Florin Coras0ef8ef22019-01-18 08:37:13 -0800479 ASSERT (vls != 0);
wanghanline8f848a2021-01-08 14:57:11 +0800480 vls_sh_to_vlsh_table_del (
481 wrk, vcl_session_handle_from_index (vls->session_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -0800482 clib_spinlock_free (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000483 pool_put (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800484}
485
486static vcl_locked_session_t *
487vls_get_and_lock (vls_handle_t vlsh)
488{
Florin Coras243edd52020-03-04 22:20:12 +0000489 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800490 vcl_locked_session_t *vls;
Florin Coras243edd52020-03-04 22:20:12 +0000491 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800492 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000493 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
494 vls_lock (vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800495 return vls;
496}
497
498static vcl_locked_session_t *
499vls_get_w_dlock (vls_handle_t vlsh)
500{
501 vcl_locked_session_t *vls;
Florin Coras5e618432021-07-26 18:19:25 -0700502 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800503 vls = vls_get_and_lock (vlsh);
504 if (!vls)
Florin Coras5e618432021-07-26 18:19:25 -0700505 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800506 return vls;
507}
508
509static inline void
Florin Coras7baeb712019-01-04 17:05:43 -0800510vls_get_and_unlock (vls_handle_t vlsh)
511{
512 vcl_locked_session_t *vls;
Florin Coras5e618432021-07-26 18:19:25 -0700513 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800514 vls = vls_get (vlsh);
515 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -0700516 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800517}
518
519static inline void
520vls_dunlock (vcl_locked_session_t * vls)
521{
522 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -0700523 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800524}
525
Florin Coras243edd52020-03-04 22:20:12 +0000526static vcl_locked_session_t *
527vls_session_get (vls_worker_t * wrk, u32 vls_index)
528{
529 if (pool_is_free_index (wrk->vls_pool, vls_index))
530 return 0;
531 return pool_elt_at_index (wrk->vls_pool, vls_index);
532}
533
Florin Coras2d675d72019-01-28 15:54:27 -0800534vcl_session_handle_t
535vlsh_to_sh (vls_handle_t vlsh)
536{
537 vcl_locked_session_t *vls;
538 int rv;
539
540 vls = vls_get_w_dlock (vlsh);
541 if (!vls)
542 return INVALID_SESSION_ID;
543 rv = vls_to_sh (vls);
544 vls_dunlock (vls);
545 return rv;
546}
547
548vcl_session_handle_t
549vlsh_to_session_index (vls_handle_t vlsh)
550{
551 vcl_session_handle_t sh;
552 sh = vlsh_to_sh (vlsh);
553 return vppcom_session_index (sh);
554}
555
556vls_handle_t
hanlinf8e13632020-08-21 11:05:36 +0800557vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
Florin Coras2d675d72019-01-28 15:54:27 -0800558{
Florin Coras243edd52020-03-04 22:20:12 +0000559 vls_worker_t *wrk = vls_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +0800560 uword *vlshp = vls_sh_to_vlsh_table_get (
561 wrk,
562 vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
563
Florin Coras2d675d72019-01-28 15:54:27 -0800564 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
565}
566
567vls_handle_t
568vls_session_index_to_vlsh (uint32_t session_index)
569{
570 vls_handle_t vlsh;
571
Florin Coras5e618432021-07-26 18:19:25 -0700572 vls_mt_pool_rlock ();
hanlinf8e13632020-08-21 11:05:36 +0800573 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
Florin Coras5e618432021-07-26 18:19:25 -0700574 vls_mt_pool_runlock ();
Florin Coras2d675d72019-01-28 15:54:27 -0800575
576 return vlsh;
577}
578
Florin Corasf9240dc2019-01-15 08:03:17 -0800579u8
Florin Coras0ef8ef22019-01-18 08:37:13 -0800580vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
Florin Corasf9240dc2019-01-15 08:03:17 -0800581{
Florin Coras243edd52020-03-04 22:20:12 +0000582 vls_shared_data_t *vls_shd;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800583 int i;
Florin Coras243edd52020-03-04 22:20:12 +0000584
585 if (vls->shared_data_index == ~0)
586 return 0;
587
588 vls_shared_data_pool_rlock ();
589
590 vls_shd = vls_shared_data_get (vls->shared_data_index);
591 clib_spinlock_lock (&vls_shd->lock);
592
593 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
594 if (vls_shd->workers_subscribed[i] == wrk_index)
595 {
596 clib_spinlock_unlock (&vls_shd->lock);
597 vls_shared_data_pool_runlock ();
598 return 1;
599 }
600 clib_spinlock_unlock (&vls_shd->lock);
601
602 vls_shared_data_pool_runlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -0800603 return 0;
604}
605
Florin Coras2d675d72019-01-28 15:54:27 -0800606static void
607vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
608{
Florin Coras243edd52020-03-04 22:20:12 +0000609 vls_shared_data_t *vls_shd;
610
611 if (vls->shared_data_index == ~0)
612 {
613 clib_warning ("not a shared session");
614 return;
615 }
616
617 vls_shared_data_pool_rlock ();
618
619 vls_shd = vls_shared_data_get (vls->shared_data_index);
620
621 clib_spinlock_lock (&vls_shd->lock);
622 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
623 clib_spinlock_unlock (&vls_shd->lock);
624
625 vls_shared_data_pool_runlock ();
626}
627
628static u32
629vls_shared_get_owner (vcl_locked_session_t * vls)
630{
631 vls_shared_data_t *vls_shd;
632 u32 owner_wrk;
633
634 vls_shared_data_pool_rlock ();
635
636 vls_shd = vls_shared_data_get (vls->shared_data_index);
637 owner_wrk = vls_shd->owner_wrk_index;
638
639 vls_shared_data_pool_runlock ();
640
641 return owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -0800642}
643
644static u8
645vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
646{
Florin Coras243edd52020-03-04 22:20:12 +0000647 vls_shared_data_t *vls_shd;
648 u8 is_set;
649
650 if (vls->shared_data_index == ~0)
651 {
652 clib_warning ("not a shared session");
653 return 0;
654 }
655
656 vls_shared_data_pool_rlock ();
657
658 vls_shd = vls_shared_data_get (vls->shared_data_index);
659
660 clib_spinlock_lock (&vls_shd->lock);
661 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
662 clib_spinlock_unlock (&vls_shd->lock);
663
664 vls_shared_data_pool_runlock ();
665
666 return (is_set == 1);
Florin Coras2d675d72019-01-28 15:54:27 -0800667}
668
669static void
670vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
671{
672 vppcom_session_listen (vls_to_sh (vls), ~0);
673 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
674}
675
676static void
677vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
678{
679 vcl_worker_t *wrk;
680 vcl_session_t *s;
681
682 wrk = vcl_worker_get (wrk_index);
683 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700684 if (s->session_state != VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800685 return;
Florin Coras458089b2019-08-21 16:20:44 -0700686 vcl_send_session_unlisten (wrk, s);
Florin Corasc127d5a2020-10-14 16:35:58 -0700687 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800688 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
689}
690
Florin Coras243edd52020-03-04 22:20:12 +0000691static int
692vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
693 u32 wrk_index)
694{
695 int i;
696
697 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
698 {
699 if (vls_shd->workers_subscribed[i] == wrk_index)
700 return i;
701 }
702 return -1;
703}
704
Florin Coras0ef8ef22019-01-18 08:37:13 -0800705int
706vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
707{
Florin Coras243edd52020-03-04 22:20:12 +0000708 vls_shared_data_t *vls_shd;
Florin Coras311817f2020-03-07 17:45:47 +0000709 int do_disconnect, pos;
710 u32 n_subscribers;
Florin Corasf9240dc2019-01-15 08:03:17 -0800711 vcl_session_t *s;
Florin Coras2d675d72019-01-28 15:54:27 -0800712
hanlina3a48962020-07-13 11:09:15 +0800713 if (vls->shared_data_index == ~0)
714 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000715
Florin Coras2d675d72019-01-28 15:54:27 -0800716 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700717 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800718 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
Florin Corasf9240dc2019-01-15 08:03:17 -0800719
Florin Coras243edd52020-03-04 22:20:12 +0000720 vls_shared_data_pool_rlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800721
Florin Coras243edd52020-03-04 22:20:12 +0000722 vls_shd = vls_shared_data_get (vls->shared_data_index);
723 clib_spinlock_lock (&vls_shd->lock);
724
725 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
726 if (pos < 0)
727 {
728 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
Florin Coras5e618432021-07-26 18:19:25 -0700729 vls->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000730 goto done;
731 }
732
733 /*
734 * Unsubscribe from share data and fifos
735 */
736 if (s->rx_fifo)
737 {
738 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
739 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
740 }
741 vec_del1 (vls_shd->workers_subscribed, pos);
742
743 /*
744 * Cleanup vcl state
745 */
746 n_subscribers = vec_len (vls_shd->workers_subscribed);
Florin Corasc127d5a2020-10-14 16:35:58 -0700747 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
Florin Coras243edd52020-03-04 22:20:12 +0000748 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
749
750 /*
751 * No subscriber left, cleanup shared data
752 */
753 if (!n_subscribers)
754 {
755 u32 shd_index = vls_shared_data_index (vls_shd);
756
757 clib_spinlock_unlock (&vls_shd->lock);
758 vls_shared_data_pool_runlock ();
759
760 vls_shared_data_free (shd_index);
761
762 /* All locks have been dropped */
Florin Corasf9240dc2019-01-15 08:03:17 -0800763 return 0;
764 }
765
Florin Coras0ef8ef22019-01-18 08:37:13 -0800766 /* Return, if this is not the owning worker */
Florin Coras243edd52020-03-04 22:20:12 +0000767 if (vls_shd->owner_wrk_index != wrk->wrk_index)
768 goto done;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800769
Florin Coras243edd52020-03-04 22:20:12 +0000770 ASSERT (vec_len (vls_shd->workers_subscribed));
771
772 /*
773 * Check if we can change owner or close
774 */
775 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
liuyacan603e1a42021-07-22 15:52:01 +0800776 if (s->vpp_evt_q)
liuyacan5cac2e82021-07-14 15:53:01 +0800777 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000778
779 /* XXX is this still needed? */
780 if (vec_len (vls_shd->workers_subscribed) > 1)
781 clib_warning ("more workers need to be updated");
782
783done:
784
785 clib_spinlock_unlock (&vls_shd->lock);
786 vls_shared_data_pool_runlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800787
788 return 0;
789}
790
791void
Florin Coras40c07ce2020-07-16 20:46:17 -0700792vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
Florin Corasf9240dc2019-01-15 08:03:17 -0800793{
Florin Coras40c07ce2020-07-16 20:46:17 -0700794 vls_shared_data_t *vls_shd;
795
796 u32 vls_shd_index = vls_shared_data_alloc ();
797
798 vls_shared_data_pool_rlock ();
799
800 vls_shd = vls_shared_data_get (vls_shd_index);
Florin Coras5e618432021-07-26 18:19:25 -0700801 vls_shd->owner_wrk_index = vls_wrk->vcl_wrk_index;
Florin Coras40c07ce2020-07-16 20:46:17 -0700802 vls->shared_data_index = vls_shd_index;
Florin Coras5e618432021-07-26 18:19:25 -0700803 vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
Florin Coras40c07ce2020-07-16 20:46:17 -0700804
805 vls_shared_data_pool_runlock ();
806}
807
808void
809vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
810{
Florin Coras5e618432021-07-26 18:19:25 -0700811 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000812 vls_shared_data_t *vls_shd;
813 vcl_session_t *s;
Florin Corasf9240dc2019-01-15 08:03:17 -0800814
Florin Coras243edd52020-03-04 22:20:12 +0000815 s = vcl_session_get (vcl_wrk, vls->session_index);
816 if (!s)
817 {
Florin Coras40c07ce2020-07-16 20:46:17 -0700818 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
819 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
Florin Coras243edd52020-03-04 22:20:12 +0000820 return;
821 }
822
Florin Coras40c07ce2020-07-16 20:46:17 -0700823 ASSERT (vls->shared_data_index != ~0);
824
Florin Coras243edd52020-03-04 22:20:12 +0000825 /* Reinit session lock */
826 clib_spinlock_init (&vls->lock);
827
Florin Coras40c07ce2020-07-16 20:46:17 -0700828 vls_shared_data_pool_rlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000829
Florin Coras40c07ce2020-07-16 20:46:17 -0700830 vls_shd = vls_shared_data_get (vls->shared_data_index);
Florin Coras243edd52020-03-04 22:20:12 +0000831
832 clib_spinlock_lock (&vls_shd->lock);
Florin Coras5e618432021-07-26 18:19:25 -0700833 vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000834 clib_spinlock_unlock (&vls_shd->lock);
Florin Coras40c07ce2020-07-16 20:46:17 -0700835
Florin Coras243edd52020-03-04 22:20:12 +0000836 vls_shared_data_pool_runlock ();
837
Florin Corasf9240dc2019-01-15 08:03:17 -0800838 if (s->rx_fifo)
839 {
Florin Coras8eb8d502021-06-16 14:46:57 -0700840 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
Florin Corasf9240dc2019-01-15 08:03:17 -0800841 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700842 else if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800843 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700844 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800845 }
Florin Coras243edd52020-03-04 22:20:12 +0000846}
Florin Coras2d675d72019-01-28 15:54:27 -0800847
Florin Coras243edd52020-03-04 22:20:12 +0000848static void
849vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
850{
Florin Coras40c07ce2020-07-16 20:46:17 -0700851 vcl_locked_session_t *vls, *parent_vls;
Florin Coras243edd52020-03-04 22:20:12 +0000852
853 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100854 pool_foreach (vls, vls_wrk->vls_pool) {
Florin Coras40c07ce2020-07-16 20:46:17 -0700855 /* Initialize sharing on parent session */
856 if (vls->shared_data_index == ~0)
857 {
858 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
859 vls_init_share_session (vls_parent_wrk, parent_vls);
860 vls->shared_data_index = parent_vls->shared_data_index;
861 }
862 vls_share_session (vls_wrk, vls);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100863 }
Florin Coras243edd52020-03-04 22:20:12 +0000864 /* *INDENT-ON* */
Florin Corasf9240dc2019-01-15 08:03:17 -0800865}
866
wanghanlin5788a342021-06-22 17:34:08 +0800867static void
868vls_validate_veps (vcl_worker_t *wrk)
869{
870 vcl_session_t *s;
871 u32 session_index, wrk_index;
872
873 pool_foreach (s, wrk->sessions)
874 {
875 if (s->vep.vep_sh != ~0)
876 {
877 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
878 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
879 }
880 if (s->vep.next_sh != ~0)
881 {
882 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
883 &session_index);
884 s->vep.next_sh = vcl_session_handle_from_index (session_index);
885 }
886 if (s->vep.prev_sh != ~0)
887 {
888 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
889 &session_index);
890 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
891 }
892 }
893}
894
Florin Corasf9240dc2019-01-15 08:03:17 -0800895void
896vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
897{
Florin Coras243edd52020-03-04 22:20:12 +0000898 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
Florin Coras5e618432021-07-26 18:19:25 -0700899 vcl_worker_t *vcl_wrk = vcl_worker_get_current ();
hanlinf8e13632020-08-21 11:05:36 +0800900 u32 vls_index, session_index, wrk_index;
901 vcl_session_handle_t sh;
liuyacan603e1a42021-07-22 15:52:01 +0800902 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -0800903
Florin Coras243edd52020-03-04 22:20:12 +0000904 /*
905 * init vcl worker
906 */
Florin Coras5e618432021-07-26 18:19:25 -0700907 vcl_wrk->sessions = pool_dup (parent_wrk->sessions);
908 vcl_wrk->session_index_by_vpp_handles =
Florin Corasf9240dc2019-01-15 08:03:17 -0800909 hash_dup (parent_wrk->session_index_by_vpp_handles);
910
Florin Coras243edd52020-03-04 22:20:12 +0000911 /*
912 * init vls worker
913 */
914 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
Florin Coras5e618432021-07-26 18:19:25 -0700915
916 /* clang-format off */
917 hash_foreach (sh, vls_index, vls_parent_wrk->sh_to_vlsh_table, ({
918 vcl_session_handle_parse (sh, &wrk_index, &session_index);
919 hash_set (vls_wrk->sh_to_vlsh_table,
920 vcl_session_handle_from_index (session_index), vls_index);
921 }));
922 /* clang-format on */
Florin Coras243edd52020-03-04 22:20:12 +0000923 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
Florin Coras2d675d72019-01-28 15:54:27 -0800924
liuyacan603e1a42021-07-22 15:52:01 +0800925 /*
926 * Detach vls from parent vcl worker and attach them to child.
927 */
928 pool_foreach (vls, vls_wrk->vls_pool)
929 {
Florin Coras5e618432021-07-26 18:19:25 -0700930 vls->vcl_wrk_index = vcl_wrk->wrk_index;
liuyacan603e1a42021-07-22 15:52:01 +0800931 }
932
wanghanlin5788a342021-06-22 17:34:08 +0800933 /* Validate vep's handle */
Florin Coras5e618432021-07-26 18:19:25 -0700934 vls_validate_veps (vcl_wrk);
wanghanlin5788a342021-06-22 17:34:08 +0800935
Florin Coras243edd52020-03-04 22:20:12 +0000936 vls_share_sessions (vls_parent_wrk, vls_wrk);
Florin Corasf9240dc2019-01-15 08:03:17 -0800937}
938
Florin Coras0ef8ef22019-01-18 08:37:13 -0800939static void
940vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
941{
942 vcl_worker_t *wrk = vcl_worker_get_current ();
943 vcl_session_t *s = 0;
944 int is_nonblk = 0;
945
946 if (vls)
947 {
948 s = vcl_session_get (wrk, vls->session_index);
949 if (PREDICT_FALSE (!s))
950 return;
Florin Corasac422d62020-10-19 20:51:36 -0700951 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800952 }
953
954 switch (op)
955 {
956 case VLS_MT_OP_READ:
957 if (!is_nonblk)
958 is_nonblk = vcl_session_read_ready (s) != 0;
959 if (!is_nonblk)
960 {
961 vls_mt_mq_lock ();
962 *locks_acq |= VLS_MT_LOCK_MQ;
963 }
964 break;
965 case VLS_MT_OP_WRITE:
Florin Coras78b5fa62019-02-21 20:04:15 -0800966 ASSERT (s);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800967 if (!is_nonblk)
968 is_nonblk = vcl_session_write_ready (s) != 0;
969 if (!is_nonblk)
970 {
971 vls_mt_mq_lock ();
972 *locks_acq |= VLS_MT_LOCK_MQ;
973 }
974 break;
975 case VLS_MT_OP_XPOLL:
976 vls_mt_mq_lock ();
977 *locks_acq |= VLS_MT_LOCK_MQ;
978 break;
979 case VLS_MT_OP_SPOOL:
980 vls_mt_spool_lock ();
981 *locks_acq |= VLS_MT_LOCK_SPOOL;
982 break;
983 default:
984 break;
985 }
986}
987
988static void
989vls_mt_rel_locks (int locks_acq)
990{
991 if (locks_acq & VLS_MT_LOCK_MQ)
992 vls_mt_mq_unlock ();
993 if (locks_acq & VLS_MT_LOCK_SPOOL)
994 vls_mt_create_unlock ();
995}
996
Florin Corasff40d8f2020-08-11 22:05:28 -0700997static inline u8
998vls_mt_session_should_migrate (vcl_locked_session_t * vls)
999{
Florin Coras5e618432021-07-26 18:19:25 -07001000 return (vls_mt_wrk_supported () &&
1001 vls->vcl_wrk_index != vcl_get_worker_index ());
Florin Corasff40d8f2020-08-11 22:05:28 -07001002}
1003
wanghanlindcacdc42020-12-28 16:19:05 +08001004static vcl_locked_session_t *
1005vls_mt_session_migrate (vcl_locked_session_t *vls)
hanlina3a48962020-07-13 11:09:15 +08001006{
1007 u32 wrk_index = vcl_get_worker_index ();
1008 vcl_worker_t *wrk;
wanghanline8f848a2021-01-08 14:57:11 +08001009 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001010 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +08001011 vcl_session_t *session;
1012 uword *p;
1013
Florin Coras5e618432021-07-26 18:19:25 -07001014 ASSERT (vls_mt_wrk_supported () && vls->vcl_wrk_index != wrk_index);
hanlina3a48962020-07-13 11:09:15 +08001015
Florin Corasff40d8f2020-08-11 22:05:28 -07001016 /*
1017 * VCL session on current vcl worker already allocated. Update current
1018 * owner worker and index and return
1019 */
hanlina3a48962020-07-13 11:09:15 +08001020 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
1021 {
Florin Coras5e618432021-07-26 18:19:25 -07001022 vls->vcl_wrk_index = wrk_index;
hanlina3a48962020-07-13 11:09:15 +08001023 vls->session_index = (u32) p[0];
wanghanlindcacdc42020-12-28 16:19:05 +08001024 return vls;
hanlina3a48962020-07-13 11:09:15 +08001025 }
1026
Florin Corasff40d8f2020-08-11 22:05:28 -07001027 /*
1028 * Ask vcl worker that owns the original vcl session to clone it into
1029 * current vcl worker session pool
1030 */
1031
hanlina3a48962020-07-13 11:09:15 +08001032 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
1033 vls->owner_vcl_wrk_index)))
1034 {
1035 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
1036 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001037 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -07001038 vls_mt_pool_runlock ();
wanghanlindcacdc42020-12-28 16:19:05 +08001039 return 0;
hanlina3a48962020-07-13 11:09:15 +08001040 }
1041
1042 src_sid = (u32) p[0];
1043 wrk = vcl_worker_get_current ();
1044 session = vcl_session_alloc (wrk);
1045 sid = session->session_index;
hanlina3a48962020-07-13 11:09:15 +08001046 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
1047 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
1048
wanghanlindcacdc42020-12-28 16:19:05 +08001049 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
1050 vls_index = vls->vls_index;
1051 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
1052 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -07001053 vls_mt_pool_runlock ();
wanghanlindcacdc42020-12-28 16:19:05 +08001054 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
1055 own_vcl_wrk_index, vls_index, src_sid);
1056
1057 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
hanlina3a48962020-07-13 11:09:15 +08001058 {
wanghanlindcacdc42020-12-28 16:19:05 +08001059 VWRN ("session %u not exist", src_sid);
1060 goto err;
1061 }
1062 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
1063 {
1064 VWRN ("failed to wait rpc response");
1065 goto err;
1066 }
1067 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
1068 session->vep.next_sh != ~0))
1069 {
hanlina3a48962020-07-13 11:09:15 +08001070 VERR ("can't migrate nonempty epoll session");
1071 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001072 goto err;
hanlina3a48962020-07-13 11:09:15 +08001073 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001074 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
Florin Corasc127d5a2020-10-14 16:35:58 -07001075 session->session_state != VCL_STATE_CLOSED))
hanlina3a48962020-07-13 11:09:15 +08001076 {
hanlina3a48962020-07-13 11:09:15 +08001077 VERR ("migrate NOT supported, session_status (%u)",
1078 session->session_state);
1079 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001080 goto err;
hanlina3a48962020-07-13 11:09:15 +08001081 }
wanghanlindcacdc42020-12-28 16:19:05 +08001082
1083 vls = vls_get_w_dlock (vls_index);
1084 if (PREDICT_FALSE (!vls))
1085 {
1086 VWRN ("failed to get vls %u", vls_index);
1087 goto err;
1088 }
1089
1090 session->session_index = sid;
Florin Coras5e618432021-07-26 18:19:25 -07001091 vls->vcl_wrk_index = wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001092 vls->session_index = sid;
1093 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
wanghanline8f848a2021-01-08 14:57:11 +08001094 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1095 vls->vls_index);
wanghanlindcacdc42020-12-28 16:19:05 +08001096 return vls;
1097
1098err:
1099 vcl_session_free (wrk, session);
1100 return 0;
hanlina3a48962020-07-13 11:09:15 +08001101}
1102
Florin Corasff40d8f2020-08-11 22:05:28 -07001103static inline void
1104vls_mt_detect (void)
1105{
1106 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1107 vls_mt_add ();
1108}
Florin Coras0ef8ef22019-01-18 08:37:13 -08001109
wanghanlindcacdc42020-12-28 16:19:05 +08001110#define vls_mt_guard(_vls, _op) \
1111 int _locks_acq = 0; \
1112 if (vls_mt_wrk_supported ()) \
1113 { \
1114 if (PREDICT_FALSE (_vls && \
Florin Coras5e618432021-07-26 18:19:25 -07001115 ((vcl_locked_session_t *) _vls)->vcl_wrk_index != \
wanghanlindcacdc42020-12-28 16:19:05 +08001116 vcl_get_worker_index ())) \
1117 { \
1118 _vls = vls_mt_session_migrate (_vls); \
1119 if (PREDICT_FALSE (!_vls)) \
1120 return VPPCOM_EBADFD; \
1121 } \
1122 } \
1123 else \
1124 { \
1125 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1126 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1127 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001128
1129#define vls_mt_unguard() \
1130 if (PREDICT_FALSE (_locks_acq)) \
Florin Coras0ef8ef22019-01-18 08:37:13 -08001131 vls_mt_rel_locks (_locks_acq)
1132
Florin Coras7baeb712019-01-04 17:05:43 -08001133int
1134vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1135{
1136 vcl_locked_session_t *vls;
1137 int rv;
1138
Florin Corasff40d8f2020-08-11 22:05:28 -07001139 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001140 if (!(vls = vls_get_w_dlock (vlsh)))
1141 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001142
1143 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001144 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001145 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001146 vls_get_and_unlock (vlsh);
1147 return rv;
1148}
1149
1150int
1151vls_write_msg (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 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001160 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001161 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001162 vls_get_and_unlock (vlsh);
1163 return rv;
1164}
1165
1166int
1167vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1168 vppcom_endpt_t * ep)
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_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
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
1183ssize_t
1184vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1185{
1186 vcl_locked_session_t *vls;
1187 int rv;
1188
Florin Corasff40d8f2020-08-11 22:05:28 -07001189 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001190 if (!(vls = vls_get_w_dlock (vlsh)))
1191 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001192 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001193 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001194 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001195 vls_get_and_unlock (vlsh);
1196 return rv;
1197}
1198
1199ssize_t
1200vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1201 vppcom_endpt_t * ep)
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_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1211 ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001212 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001213 vls_get_and_unlock (vlsh);
1214 return rv;
1215}
1216
1217int
1218vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
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 Corasff40d8f2020-08-11 22:05:28 -07001226 if (vls_mt_session_should_migrate (vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001227 {
1228 vls = vls_mt_session_migrate (vls);
1229 if (PREDICT_FALSE (!vls))
1230 return VPPCOM_EBADFD;
1231 }
Florin Coras7baeb712019-01-04 17:05:43 -08001232 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1233 vls_get_and_unlock (vlsh);
1234 return rv;
1235}
1236
1237int
1238vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1239{
1240 vcl_locked_session_t *vls;
1241 int rv;
1242
Florin Corasff40d8f2020-08-11 22:05:28 -07001243 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001244 if (!(vls = vls_get_w_dlock (vlsh)))
1245 return VPPCOM_EBADFD;
1246 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1247 vls_get_and_unlock (vlsh);
1248 return rv;
1249}
1250
1251int
1252vls_listen (vls_handle_t vlsh, int q_len)
1253{
1254 vcl_locked_session_t *vls;
1255 int rv;
1256
Florin Corasff40d8f2020-08-11 22:05:28 -07001257 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001258 if (!(vls = vls_get_w_dlock (vlsh)))
1259 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001260 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001261 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001262 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001263 vls_get_and_unlock (vlsh);
1264 return rv;
1265}
1266
1267int
1268vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1269{
1270 vcl_locked_session_t *vls;
1271 int rv;
1272
Florin Corasff40d8f2020-08-11 22:05:28 -07001273 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001274 if (!(vls = vls_get_w_dlock (vlsh)))
1275 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001276 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001277 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001278 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001279 vls_get_and_unlock (vlsh);
1280 return rv;
1281}
1282
Florin Coras2d675d72019-01-28 15:54:27 -08001283static inline void
1284vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1285{
1286 vcl_worker_t *wrk = vcl_worker_get_current ();
1287 vcl_session_t *s;
Florin Coras243edd52020-03-04 22:20:12 +00001288 u32 owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -08001289
hanlina3a48962020-07-13 11:09:15 +08001290 if (vls_mt_wrk_supported ())
1291 return;
1292
Florin Coras5e618432021-07-26 18:19:25 -07001293 ASSERT (wrk->wrk_index == vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001294 s = vcl_session_get (wrk, vls->session_index);
1295 switch (s->session_state)
1296 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001297 case VCL_STATE_LISTEN:
Florin Coras2d675d72019-01-28 15:54:27 -08001298 if (is_add)
1299 {
Florin Coras5e618432021-07-26 18:19:25 -07001300 vls_listener_wrk_set (vls, vls->vcl_wrk_index, 1 /* is_active */);
Florin Coras2d675d72019-01-28 15:54:27 -08001301 break;
1302 }
Florin Coras5e618432021-07-26 18:19:25 -07001303 vls_listener_wrk_stop_listen (vls, vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001304 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001305 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -08001306 if (!is_add)
1307 break;
1308
1309 /* Register worker as listener */
Florin Coras5e618432021-07-26 18:19:25 -07001310 vls_listener_wrk_start_listen (vls, vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001311
1312 /* If owner worker did not attempt to accept/xpoll on the session,
1313 * force a listen stop for it, since it may not be interested in
1314 * accepting new sessions.
1315 * This is pretty much a hack done to give app workers the illusion
1316 * that it is fine to listen and not accept new sessions for a
1317 * given listener. Without it, we would accumulate unhandled
1318 * accepts on the passive worker message queue. */
Florin Coras243edd52020-03-04 22:20:12 +00001319 owner_wrk = vls_shared_get_owner (vls);
1320 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1321 vls_listener_wrk_stop_listen (vls, owner_wrk);
Florin Coras2d675d72019-01-28 15:54:27 -08001322 break;
1323 default:
1324 break;
1325 }
1326}
1327
Florin Coras7baeb712019-01-04 17:05:43 -08001328vls_handle_t
1329vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1330{
1331 vls_handle_t accepted_vlsh;
1332 vcl_locked_session_t *vls;
1333 int sh;
1334
Florin Corasff40d8f2020-08-11 22:05:28 -07001335 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001336 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1337 return VPPCOM_EBADFD;
Florin Coras2d675d72019-01-28 15:54:27 -08001338 if (vcl_n_workers () > 1)
1339 vls_mp_checks (vls, 1 /* is_add */ );
Florin Coras0ef8ef22019-01-18 08:37:13 -08001340 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001341 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001342 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001343 vls_get_and_unlock (listener_vlsh);
1344 if (sh < 0)
1345 return sh;
1346 accepted_vlsh = vls_alloc (sh);
1347 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1348 vppcom_session_close (sh);
1349 return accepted_vlsh;
1350}
1351
1352vls_handle_t
1353vls_create (uint8_t proto, uint8_t is_nonblocking)
1354{
1355 vcl_session_handle_t sh;
1356 vls_handle_t vlsh;
wanghanlindcacdc42020-12-28 16:19:05 +08001357 vcl_locked_session_t *vls = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001358
Florin Corasff40d8f2020-08-11 22:05:28 -07001359 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001360 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001361 sh = vppcom_session_create (proto, is_nonblocking);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001362 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001363 if (sh == INVALID_SESSION_ID)
1364 return VLS_INVALID_HANDLE;
1365
1366 vlsh = vls_alloc (sh);
1367 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1368 vppcom_session_close (sh);
1369
1370 return vlsh;
1371}
1372
hanlina3a48962020-07-13 11:09:15 +08001373static void
Florin Corasff40d8f2020-08-11 22:05:28 -07001374vls_mt_session_cleanup (vcl_locked_session_t * vls)
hanlina3a48962020-07-13 11:09:15 +08001375{
Florin Corasff40d8f2020-08-11 22:05:28 -07001376 u32 session_index, wrk_index, current_vcl_wrk;
hanlina3a48962020-07-13 11:09:15 +08001377 vcl_worker_t *wrk = vcl_worker_get_current ();
1378
Florin Corasff40d8f2020-08-11 22:05:28 -07001379 ASSERT (vls_mt_wrk_supported ());
1380
1381 current_vcl_wrk = vcl_get_worker_index ();
hanlina3a48962020-07-13 11:09:15 +08001382
1383 /* *INDENT-OFF* */
1384 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1385 ({
Florin Corasff40d8f2020-08-11 22:05:28 -07001386 if (current_vcl_wrk != wrk_index)
1387 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
hanlina3a48962020-07-13 11:09:15 +08001388 }));
1389 /* *INDENT-ON* */
1390 hash_free (vls->vcl_wrk_index_to_session_index);
1391}
1392
Florin Coras7baeb712019-01-04 17:05:43 -08001393int
1394vls_close (vls_handle_t vlsh)
1395{
1396 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -08001397 int rv;
Florin Coras7baeb712019-01-04 17:05:43 -08001398
Florin Corasff40d8f2020-08-11 22:05:28 -07001399 vls_mt_detect ();
Florin Coras5e618432021-07-26 18:19:25 -07001400 vls_mt_pool_wlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001401
Florin Coras0ef8ef22019-01-18 08:37:13 -08001402 vls = vls_get_and_lock (vlsh);
1403 if (!vls)
1404 {
Florin Coras5e618432021-07-26 18:19:25 -07001405 vls_mt_pool_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001406 return VPPCOM_EBADFD;
1407 }
1408
hanlina3a48962020-07-13 11:09:15 +08001409 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Corasf9240dc2019-01-15 08:03:17 -08001410
Florin Coras243edd52020-03-04 22:20:12 +00001411 if (vls_is_shared (vls))
1412 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1413 else
1414 rv = vppcom_session_close (vls_to_sh (vls));
1415
Florin Corasff40d8f2020-08-11 22:05:28 -07001416 if (vls_mt_wrk_supported ())
1417 vls_mt_session_cleanup (vls);
hanlina3a48962020-07-13 11:09:15 +08001418
Florin Coras0ef8ef22019-01-18 08:37:13 -08001419 vls_free (vls);
1420 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001421
Florin Coras5e618432021-07-26 18:19:25 -07001422 vls_mt_pool_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001423
Florin Coras7baeb712019-01-04 17:05:43 -08001424 return rv;
1425}
1426
liuyacan534468e2021-05-09 03:50:40 +00001427int
liuyacan55c952e2021-06-13 14:54:55 +08001428vls_shutdown (vls_handle_t vlsh, int how)
liuyacan534468e2021-05-09 03:50:40 +00001429{
1430 vcl_locked_session_t *vls;
1431 int rv;
1432
1433 vls_mt_detect ();
1434 if (!(vls = vls_get_w_dlock (vlsh)))
1435 return VPPCOM_EBADFD;
1436
1437 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
liuyacan6fc07b42021-07-24 22:48:36 +08001438 rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
liuyacan534468e2021-05-09 03:50:40 +00001439 vls_mt_unguard ();
1440 vls_get_and_unlock (vlsh);
1441
1442 return rv;
1443}
1444
Florin Coras7baeb712019-01-04 17:05:43 -08001445vls_handle_t
1446vls_epoll_create (void)
1447{
1448 vcl_session_handle_t sh;
1449 vls_handle_t vlsh;
1450
Florin Corasff40d8f2020-08-11 22:05:28 -07001451 vls_mt_detect ();
Florin Coras63d3ac62019-03-29 08:29:25 -07001452
Florin Coras7baeb712019-01-04 17:05:43 -08001453 sh = vppcom_epoll_create ();
1454 if (sh == INVALID_SESSION_ID)
1455 return VLS_INVALID_HANDLE;
1456
1457 vlsh = vls_alloc (sh);
1458 if (vlsh == VLS_INVALID_HANDLE)
1459 vppcom_session_close (sh);
1460
1461 return vlsh;
1462}
1463
Florin Coras2d675d72019-01-28 15:54:27 -08001464static void
1465vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1466{
nandfandc2632e2021-03-26 16:46:58 +08001467 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
Florin Coras2d675d72019-01-28 15:54:27 -08001468 return;
1469
Florin Coras2d675d72019-01-28 15:54:27 -08001470 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1471}
1472
Florin Coras7baeb712019-01-04 17:05:43 -08001473int
1474vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1475 struct epoll_event *event)
1476{
1477 vcl_locked_session_t *ep_vls, *vls;
1478 vcl_session_handle_t ep_sh, sh;
1479 int rv;
1480
Florin Corasff40d8f2020-08-11 22:05:28 -07001481 vls_mt_detect ();
Florin Coras5e618432021-07-26 18:19:25 -07001482 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001483 ep_vls = vls_get_and_lock (ep_vlsh);
Florin Corasff40d8f2020-08-11 22:05:28 -07001484
1485 if (vls_mt_session_should_migrate (ep_vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001486 {
1487 ep_vls = vls_mt_session_migrate (ep_vls);
1488 if (PREDICT_FALSE (!ep_vls))
1489 return VPPCOM_EBADFD;
1490 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001491
Florin Coras7baeb712019-01-04 17:05:43 -08001492 ep_sh = vls_to_sh (ep_vls);
wanghanlindcacdc42020-12-28 16:19:05 +08001493 vls = vls_get_and_lock (vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -08001494 sh = vls_to_sh (vls);
Florin Coras2d675d72019-01-28 15:54:27 -08001495
nandfandc2632e2021-03-26 16:46:58 +08001496 vls_epoll_ctl_mp_checks (vls, op);
Florin Coras5e618432021-07-26 18:19:25 -07001497 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001498 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1499
Florin Coras5e618432021-07-26 18:19:25 -07001500 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001501 ep_vls = vls_get (ep_vlsh);
1502 vls = vls_get (vlsh);
1503 vls_unlock (vls);
1504 vls_unlock (ep_vls);
Florin Coras5e618432021-07-26 18:19:25 -07001505 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001506 return rv;
1507}
1508
1509int
1510vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1511 int maxevents, double wait_for_time)
1512{
wanghanlindcacdc42020-12-28 16:19:05 +08001513 vcl_locked_session_t *vls, *vls_tmp = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001514 int rv;
1515
Florin Corasff40d8f2020-08-11 22:05:28 -07001516 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001517 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1518 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001519 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001520 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1521 wait_for_time);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001522 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001523 vls_get_and_unlock (ep_vlsh);
wanghanlind72a0342021-05-12 17:00:29 +08001524 vls_handle_pending_wrk_cleanup ();
Florin Coras7baeb712019-01-04 17:05:43 -08001525 return rv;
1526}
1527
Florin Coras2d675d72019-01-28 15:54:27 -08001528static void
1529vls_select_mp_checks (vcl_si_set * read_map)
1530{
1531 vcl_locked_session_t *vls;
1532 vcl_worker_t *wrk;
1533 vcl_session_t *s;
1534 u32 si;
1535
1536 if (vcl_n_workers () <= 1)
1537 {
1538 vlsl->select_mp_check = 1;
1539 return;
1540 }
1541
1542 if (!read_map)
1543 return;
1544
1545 vlsl->select_mp_check = 1;
1546 wrk = vcl_worker_get_current ();
1547
1548 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001549 clib_bitmap_foreach (si, read_map) {
Florin Coras2d675d72019-01-28 15:54:27 -08001550 s = vcl_session_get (wrk, si);
Florin Corasc127d5a2020-10-14 16:35:58 -07001551 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -08001552 {
1553 vls = vls_get (vls_session_index_to_vlsh (si));
1554 vls_mp_checks (vls, 1 /* is_add */);
1555 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001556 }
Florin Coras2d675d72019-01-28 15:54:27 -08001557 /* *INDENT-ON* */
1558}
1559
Florin Coras0ef8ef22019-01-18 08:37:13 -08001560int
1561vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1562 vcl_si_set * except_map, double wait_for_time)
1563{
1564 int rv;
wanghanlindcacdc42020-12-28 16:19:05 +08001565 vcl_locked_session_t *vls = NULL;
Florin Coras2d675d72019-01-28 15:54:27 -08001566
Florin Corasff40d8f2020-08-11 22:05:28 -07001567 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001568 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras2d675d72019-01-28 15:54:27 -08001569 if (PREDICT_FALSE (!vlsl->select_mp_check))
1570 vls_select_mp_checks (read_map);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001571 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1572 vls_mt_unguard ();
wanghanlind72a0342021-05-12 17:00:29 +08001573 vls_handle_pending_wrk_cleanup ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001574 return rv;
1575}
1576
Florin Corasf9240dc2019-01-15 08:03:17 -08001577static void
Florin Coras0ef8ef22019-01-18 08:37:13 -08001578vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1579{
1580 u32 current_wrk, is_current;
1581 vcl_locked_session_t *vls;
1582 vcl_session_t *s;
1583
Florin Coras14ed6df2019-03-06 21:13:42 -08001584 if (pool_elts (vcm->workers) <= 1)
1585 return;
1586
Florin Coras0ef8ef22019-01-18 08:37:13 -08001587 current_wrk = vcl_get_worker_index ();
1588 is_current = current_wrk == wrk->wrk_index;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001589
1590 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001591 pool_foreach (s, wrk->sessions) {
hanlinf8e13632020-08-21 11:05:36 +08001592 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -08001593 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1594 vls_unshare_session (vls, wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001595 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001596 /* *INDENT-ON* */
Florin Coras0ef8ef22019-01-18 08:37:13 -08001597}
1598
1599static void
1600vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1601{
Florin Coras243edd52020-03-04 22:20:12 +00001602 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1603
Florin Coras0ef8ef22019-01-18 08:37:13 -08001604 /* Unshare sessions and also cleanup worker since child may have
1605 * called _exit () and therefore vcl may not catch the event */
1606 vls_unshare_vcl_worker_sessions (wrk);
wanghanlinb940fd42021-06-29 16:01:55 +08001607
1608 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1609 * may have been closed, so DONOT notify VPP.
1610 */
1611 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
Florin Coras243edd52020-03-04 22:20:12 +00001612
1613 vls_worker_free (vls_wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001614}
1615
1616static void
Florin Corasf9240dc2019-01-15 08:03:17 -08001617vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1618{
1619 vcl_worker_t *sub_child;
1620 int tries = 0;
1621
1622 if (child_wrk->forked_child != ~0)
1623 {
1624 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1625 if (sub_child)
1626 {
1627 /* Wait a bit, maybe the process is going away */
1628 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1629 usleep (1e3);
1630 if (kill (sub_child->current_pid, 0) < 0)
1631 vls_cleanup_forked_child (child_wrk, sub_child);
1632 }
1633 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001634 vls_cleanup_vcl_worker (child_wrk);
1635 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001636 wrk->forked_child = ~0;
1637}
1638
wanghanlind72a0342021-05-12 17:00:29 +08001639static void
1640vls_handle_pending_wrk_cleanup (void)
1641{
1642 u32 *wip;
1643 vcl_worker_t *child_wrk, *wrk;
1644 vls_worker_t *vls_wrk = vls_worker_get_current ();
1645
Florin Coras5e618432021-07-26 18:19:25 -07001646 if (PREDICT_TRUE (vec_len (vls_wrk->pending_vcl_wrk_cleanup) == 0))
wanghanlind72a0342021-05-12 17:00:29 +08001647 return;
1648
1649 wrk = vcl_worker_get_current ();
Florin Coras5e618432021-07-26 18:19:25 -07001650 vec_foreach (wip, vls_wrk->pending_vcl_wrk_cleanup)
wanghanlind72a0342021-05-12 17:00:29 +08001651 {
1652 child_wrk = vcl_worker_get_if_valid (*wip);
1653 if (!child_wrk)
1654 continue;
1655 vls_cleanup_forked_child (wrk, child_wrk);
1656 }
Florin Coras5e618432021-07-26 18:19:25 -07001657 vec_reset_length (vls_wrk->pending_vcl_wrk_cleanup);
wanghanlind72a0342021-05-12 17:00:29 +08001658}
1659
Florin Corasf9240dc2019-01-15 08:03:17 -08001660static struct sigaction old_sa;
1661
1662static void
1663vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1664{
1665 vcl_worker_t *wrk, *child_wrk;
wanghanlind72a0342021-05-12 17:00:29 +08001666 vls_worker_t *vls_wrk;
Florin Corasf9240dc2019-01-15 08:03:17 -08001667
1668 if (vcl_get_worker_index () == ~0)
1669 return;
1670
1671 if (sigaction (SIGCHLD, &old_sa, 0))
1672 {
1673 VERR ("couldn't restore sigchld");
1674 exit (-1);
1675 }
1676
1677 wrk = vcl_worker_get_current ();
1678 if (wrk->forked_child == ~0)
1679 return;
1680
1681 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1682 if (!child_wrk)
1683 goto done;
1684
1685 if (si && si->si_pid != child_wrk->current_pid)
1686 {
1687 VDBG (0, "unexpected child pid %u", si->si_pid);
1688 goto done;
1689 }
wanghanlind72a0342021-05-12 17:00:29 +08001690
1691 /* Parent process may enter sighandler with a lock, such as lock in localtime
1692 * or in mspace_free, and child wrk cleanup may try to get such locks and
1693 * cause deadlock.
1694 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1695 */
1696 vls_wrk = vls_worker_get_current ();
Florin Coras5e618432021-07-26 18:19:25 -07001697 vec_add1 (vls_wrk->pending_vcl_wrk_cleanup, child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001698
1699done:
1700 if (old_sa.sa_flags & SA_SIGINFO)
1701 {
1702 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1703 fn (signum, si, uc);
1704 }
1705 else
1706 {
1707 void (*fn) (int) = old_sa.sa_handler;
1708 if (fn)
1709 fn (signum);
1710 }
1711}
1712
1713static void
1714vls_incercept_sigchld ()
1715{
1716 struct sigaction sa;
nandfan5fdc47c2021-02-22 17:17:17 +08001717 if (old_sa.sa_sigaction)
1718 {
1719 VDBG (0, "have intercepted sigchld");
1720 return;
1721 }
Florin Corasf9240dc2019-01-15 08:03:17 -08001722 clib_memset (&sa, 0, sizeof (sa));
1723 sa.sa_sigaction = vls_intercept_sigchld_handler;
1724 sa.sa_flags = SA_SIGINFO;
1725 if (sigaction (SIGCHLD, &sa, &old_sa))
1726 {
1727 VERR ("couldn't intercept sigchld");
1728 exit (-1);
1729 }
1730}
1731
1732static void
1733vls_app_pre_fork (void)
1734{
1735 vls_incercept_sigchld ();
1736 vcl_flush_mq_events ();
1737}
1738
1739static void
1740vls_app_fork_child_handler (void)
1741{
1742 vcl_worker_t *parent_wrk;
Florin Corasb88de902020-09-08 16:47:57 -07001743 int parent_wrk_index;
Florin Corasf9240dc2019-01-15 08:03:17 -08001744
1745 parent_wrk_index = vcl_get_worker_index ();
1746 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1747 parent_wrk_index);
1748
1749 /*
Florin Corasb88de902020-09-08 16:47:57 -07001750 * Clear old state
Florin Corasf9240dc2019-01-15 08:03:17 -08001751 */
1752 vcl_set_worker_index (~0);
Florin Corasf9240dc2019-01-15 08:03:17 -08001753
1754 /*
Florin Corasb88de902020-09-08 16:47:57 -07001755 * Allocate and register vcl worker with vpp
Florin Corasf9240dc2019-01-15 08:03:17 -08001756 */
Florin Corasb88de902020-09-08 16:47:57 -07001757 if (vppcom_worker_register ())
Florin Corasf9240dc2019-01-15 08:03:17 -08001758 {
Florin Corasb88de902020-09-08 16:47:57 -07001759 VERR ("couldn't register new worker!");
Florin Corasf9240dc2019-01-15 08:03:17 -08001760 return;
1761 }
1762
1763 /*
Florin Corasb88de902020-09-08 16:47:57 -07001764 * Allocate/initialize vls worker and share sessions
Florin Coras243edd52020-03-04 22:20:12 +00001765 */
1766 vls_worker_alloc ();
Florin Corasf9240dc2019-01-15 08:03:17 -08001767
Florin Coras0ef8ef22019-01-18 08:37:13 -08001768 /* Reset number of threads and set wrk index */
Florin Coras2d675d72019-01-28 15:54:27 -08001769 vlsl->vls_mt_n_threads = 0;
1770 vlsl->vls_wrk_index = vcl_get_worker_index ();
1771 vlsl->select_mp_check = 0;
Florin Coras2e2f9df2021-07-27 22:48:05 -07001772 clib_rwlock_init (&vlsl->vls_pool_lock);
Florin Coras2d675d72019-01-28 15:54:27 -08001773 vls_mt_locks_init ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001774
liuyacan603e1a42021-07-22 15:52:01 +08001775 parent_wrk = vcl_worker_get (parent_wrk_index);
1776 vls_worker_copy_on_fork (parent_wrk);
1777 parent_wrk->forked_child = vcl_get_worker_index ();
1778
Florin Corasf9240dc2019-01-15 08:03:17 -08001779 VDBG (0, "forked child main worker initialized");
1780 vcm->forking = 0;
1781}
1782
1783static void
1784vls_app_fork_parent_handler (void)
1785{
1786 vcm->forking = 1;
1787 while (vcm->forking)
1788 ;
1789}
1790
Florin Coras0ef8ef22019-01-18 08:37:13 -08001791void
1792vls_app_exit (void)
1793{
Florin Coras243edd52020-03-04 22:20:12 +00001794 vls_worker_t *wrk = vls_worker_get_current ();
1795
wanghanlind72a0342021-05-12 17:00:29 +08001796 /* Handle pending wrk cleanup */
1797 vls_handle_pending_wrk_cleanup ();
1798
Florin Coras0ef8ef22019-01-18 08:37:13 -08001799 /* Unshare the sessions. VCL will clean up the worker */
1800 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
Florin Coras243edd52020-03-04 22:20:12 +00001801 vls_worker_free (wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001802}
1803
Florin Coras40c07ce2020-07-16 20:46:17 -07001804static void
1805vls_clone_and_share_rpc_handler (void *args)
1806{
1807 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1808 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1809 vcl_locked_session_t *vls, *dst_vls;
hanlina3a48962020-07-13 11:09:15 +08001810 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
Florin Coras40c07ce2020-07-16 20:46:17 -07001811 vcl_session_t *s, *dst_s;
1812
wanghanlindcacdc42020-12-28 16:19:05 +08001813 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1814 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1815 msg->origin_session_index);
1816
1817 /* VCL locked session can't been protected, so DONT touch it.
1818 * VCL session may been free, check it.
1819 */
1820 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1821 s = vcl_session_get (vcl_wrk, msg->session_index);
1822 if (PREDICT_FALSE (!s))
1823 {
1824 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1825 return;
1826 }
Florin Coras40c07ce2020-07-16 20:46:17 -07001827
hanlina3a48962020-07-13 11:09:15 +08001828 if (!vls_mt_wrk_supported ())
wanghanlindcacdc42020-12-28 16:19:05 +08001829 {
1830 vls = vls_session_get (wrk, msg->vls_index);
1831 vls_init_share_session (wrk, vls);
1832 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1833 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1834 dst_vls->shared_data_index = vls->shared_data_index;
1835 }
hanlina3a48962020-07-13 11:09:15 +08001836 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
Florin Coras40c07ce2020-07-16 20:46:17 -07001837 clib_memcpy (dst_s, s, sizeof (*s));
1838
wanghanlindcacdc42020-12-28 16:19:05 +08001839 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
hanlina3a48962020-07-13 11:09:15 +08001840}
1841
1842static void
1843vls_session_cleanup_rpc_handler (void *args)
1844{
1845 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1846 vcl_worker_t *wrk = vcl_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +08001847 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001848 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
hanlina3a48962020-07-13 11:09:15 +08001849
wanghanlindcacdc42020-12-28 16:19:05 +08001850 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1851 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
hanlina3a48962020-07-13 11:09:15 +08001852
wanghanlindcacdc42020-12-28 16:19:05 +08001853 vppcom_session_close (sh);
wanghanline8f848a2021-01-08 14:57:11 +08001854 vls_sh_to_vlsh_table_del (vls_wrk, sh);
Florin Coras40c07ce2020-07-16 20:46:17 -07001855}
1856
1857static void
1858vls_rpc_handler (void *args)
1859{
1860 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1861 switch (msg->type)
1862 {
1863 case VLS_RPC_CLONE_AND_SHARE:
1864 vls_clone_and_share_rpc_handler (msg->data);
1865 break;
hanlina3a48962020-07-13 11:09:15 +08001866 case VLS_RPC_SESS_CLEANUP:
1867 vls_session_cleanup_rpc_handler (msg->data);
1868 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001869 default:
1870 break;
1871 }
1872}
1873
1874void
wanghanlindcacdc42020-12-28 16:19:05 +08001875vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1876 u32 session_index, u32 vls_wrk_index,
1877 u32 dst_wrk_index, u32 dst_vls_index,
1878 u32 dst_session_index)
Florin Coras40c07ce2020-07-16 20:46:17 -07001879{
1880 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1881 vls_clone_and_share_msg_t *msg;
1882 vls_rpc_msg_t *rpc;
hanlina3a48962020-07-13 11:09:15 +08001883 int ret;
wanghanlindcacdc42020-12-28 16:19:05 +08001884 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
Florin Coras40c07ce2020-07-16 20:46:17 -07001885
1886 rpc = (vls_rpc_msg_t *) & data;
1887 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1888 msg = (vls_clone_and_share_msg_t *) & rpc->data;
hanlina3a48962020-07-13 11:09:15 +08001889 msg->origin_vls_wrk = vls_wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001890 msg->origin_vls_index = origin_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001891 msg->origin_vcl_wrk = wrk->wrk_index;
1892 msg->origin_session_index = session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001893 msg->vls_index = dst_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001894 msg->session_index = dst_session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001895
wanghanlindcacdc42020-12-28 16:19:05 +08001896 /* Try lock and handle rpcs if two threads send each other
1897 * clone requests at the same time.
1898 */
1899 wrk->rpc_done = VLS_RPC_STATE_INIT;
1900 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1901 vcl_flush_mq_events ();
hanlina3a48962020-07-13 11:09:15 +08001902 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1903
Florin Corasff40d8f2020-08-11 22:05:28 -07001904 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001905 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1906 msg->origin_session_index, ret);
wanghanlindcacdc42020-12-28 16:19:05 +08001907 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1908 clib_time_now (&wrk->clib_time) < timeout)
hanlina3a48962020-07-13 11:09:15 +08001909 ;
wanghanlindcacdc42020-12-28 16:19:05 +08001910 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
hanlina3a48962020-07-13 11:09:15 +08001911}
1912
1913void
1914vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1915 u32 dst_wrk_index, u32 dst_session_index)
1916{
1917 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1918 vls_sess_cleanup_msg_t *msg;
1919 vls_rpc_msg_t *rpc;
1920 int ret;
1921
1922 rpc = (vls_rpc_msg_t *) & data;
1923 rpc->type = VLS_RPC_SESS_CLEANUP;
1924 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1925 msg->origin_vcl_wrk = wrk->wrk_index;
1926 msg->session_index = dst_session_index;
1927
hanlina3a48962020-07-13 11:09:15 +08001928 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1929
Florin Corasff40d8f2020-08-11 22:05:28 -07001930 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001931 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
Florin Coras40c07ce2020-07-16 20:46:17 -07001932}
1933
Florin Coras7baeb712019-01-04 17:05:43 -08001934int
1935vls_app_create (char *app_name)
1936{
1937 int rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001938
Florin Coras7baeb712019-01-04 17:05:43 -08001939 if ((rv = vppcom_app_create (app_name)))
1940 return rv;
Florin Coras243edd52020-03-04 22:20:12 +00001941
Florin Coras2d675d72019-01-28 15:54:27 -08001942 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1943 clib_memset (vlsm, 0, sizeof (*vlsm));
Florin Coras243edd52020-03-04 22:20:12 +00001944 clib_rwlock_init (&vlsm->shared_data_lock);
wanghanlindcacdc42020-12-28 16:19:05 +08001945 clib_spinlock_init (&vlsm->worker_rpc_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001946 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1947
Florin Corasf9240dc2019-01-15 08:03:17 -08001948 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1949 vls_app_fork_child_handler);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001950 atexit (vls_app_exit);
Florin Coras243edd52020-03-04 22:20:12 +00001951 vls_worker_alloc ();
Florin Coras2d675d72019-01-28 15:54:27 -08001952 vlsl->vls_wrk_index = vcl_get_worker_index ();
Florin Coras2e2f9df2021-07-27 22:48:05 -07001953 clib_rwlock_init (&vlsl->vls_pool_lock);
Florin Coras2d675d72019-01-28 15:54:27 -08001954 vls_mt_locks_init ();
Florin Coras40c07ce2020-07-16 20:46:17 -07001955 vcm->wrk_rpc_fn = vls_rpc_handler;
Florin Coras7baeb712019-01-04 17:05:43 -08001956 return VPPCOM_OK;
1957}
1958
hanlin4266d4d2020-05-19 17:34:17 +08001959unsigned char
1960vls_use_eventfd (void)
1961{
1962 return vcm->cfg.use_mq_eventfd;
1963}
1964
hanlina3a48962020-07-13 11:09:15 +08001965unsigned char
1966vls_mt_wrk_supported (void)
1967{
Florin Corasff40d8f2020-08-11 22:05:28 -07001968 return vcm->cfg.mt_wrk_supported;
hanlina3a48962020-07-13 11:09:15 +08001969}
1970
1971int
1972vls_use_real_epoll (void)
1973{
1974 if (vcl_get_worker_index () == ~0)
1975 return 0;
1976
1977 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1978}
1979
1980void
1981vls_register_vcl_worker (void)
1982{
wanghanlin492350e2020-09-11 17:19:32 +08001983 vls_mt_add ();
hanlina3a48962020-07-13 11:09:15 +08001984}
1985
Florin Coras7baeb712019-01-04 17:05:43 -08001986/*
1987 * fd.io coding-style-patch-verification: ON
1988 *
1989 * Local Variables:
1990 * eval: (c-set-style "gnu")
1991 * End:
1992 */