blob: 412db8def3bbf38442c9ae44e8fe308e58d8efd1 [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);
Florin Coras674d5652021-12-03 23:02:13 -0800622 vls_shd->listeners =
623 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
Florin Coras243edd52020-03-04 22:20:12 +0000624 clib_spinlock_unlock (&vls_shd->lock);
625
626 vls_shared_data_pool_runlock ();
627}
628
629static u32
630vls_shared_get_owner (vcl_locked_session_t * vls)
631{
632 vls_shared_data_t *vls_shd;
633 u32 owner_wrk;
634
635 vls_shared_data_pool_rlock ();
636
637 vls_shd = vls_shared_data_get (vls->shared_data_index);
638 owner_wrk = vls_shd->owner_wrk_index;
639
640 vls_shared_data_pool_runlock ();
641
642 return owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -0800643}
644
645static u8
646vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
647{
Florin Coras243edd52020-03-04 22:20:12 +0000648 vls_shared_data_t *vls_shd;
649 u8 is_set;
650
651 if (vls->shared_data_index == ~0)
652 {
653 clib_warning ("not a shared session");
654 return 0;
655 }
656
657 vls_shared_data_pool_rlock ();
658
659 vls_shd = vls_shared_data_get (vls->shared_data_index);
660
661 clib_spinlock_lock (&vls_shd->lock);
662 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
663 clib_spinlock_unlock (&vls_shd->lock);
664
665 vls_shared_data_pool_runlock ();
666
667 return (is_set == 1);
Florin Coras2d675d72019-01-28 15:54:27 -0800668}
669
670static void
671vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
672{
Florin Coras32881932023-02-06 13:30:13 -0800673 vcl_worker_t *wrk;
674 vcl_session_t *ls;
675
676 wrk = vcl_worker_get (wrk_index);
677 ls = vcl_session_get (wrk, vls->session_index);
678
679 /* Listen request already sent */
680 if (ls->flags & VCL_SESSION_F_PENDING_LISTEN)
681 return;
682
683 vcl_send_session_listen (wrk, ls);
684
685 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */);
Florin Coras2d675d72019-01-28 15:54:27 -0800686}
687
688static void
689vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
690{
691 vcl_worker_t *wrk;
692 vcl_session_t *s;
693
694 wrk = vcl_worker_get (wrk_index);
695 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700696 if (s->session_state != VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800697 return;
Florin Coras458089b2019-08-21 16:20:44 -0700698 vcl_send_session_unlisten (wrk, s);
Florin Corasc127d5a2020-10-14 16:35:58 -0700699 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800700 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
701}
702
Florin Coras243edd52020-03-04 22:20:12 +0000703static int
704vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
705 u32 wrk_index)
706{
707 int i;
708
709 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
710 {
711 if (vls_shd->workers_subscribed[i] == wrk_index)
712 return i;
713 }
714 return -1;
715}
716
Florin Coras0ef8ef22019-01-18 08:37:13 -0800717int
718vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
719{
Florin Coras243edd52020-03-04 22:20:12 +0000720 vls_shared_data_t *vls_shd;
Florin Coras311817f2020-03-07 17:45:47 +0000721 int do_disconnect, pos;
722 u32 n_subscribers;
Florin Corasf9240dc2019-01-15 08:03:17 -0800723 vcl_session_t *s;
Florin Coras2d675d72019-01-28 15:54:27 -0800724
hanlina3a48962020-07-13 11:09:15 +0800725 if (vls->shared_data_index == ~0)
726 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000727
Florin Coras2d675d72019-01-28 15:54:27 -0800728 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700729 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800730 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
Florin Corasf9240dc2019-01-15 08:03:17 -0800731
Florin Coras243edd52020-03-04 22:20:12 +0000732 vls_shared_data_pool_rlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800733
Florin Coras243edd52020-03-04 22:20:12 +0000734 vls_shd = vls_shared_data_get (vls->shared_data_index);
735 clib_spinlock_lock (&vls_shd->lock);
736
737 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
738 if (pos < 0)
739 {
740 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
Florin Coras5e618432021-07-26 18:19:25 -0700741 vls->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000742 goto done;
743 }
744
745 /*
746 * Unsubscribe from share data and fifos
747 */
748 if (s->rx_fifo)
749 {
750 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
751 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
752 }
753 vec_del1 (vls_shd->workers_subscribed, pos);
754
755 /*
756 * Cleanup vcl state
757 */
758 n_subscribers = vec_len (vls_shd->workers_subscribed);
Florin Corasc127d5a2020-10-14 16:35:58 -0700759 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
Florin Coras243edd52020-03-04 22:20:12 +0000760 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
761
762 /*
763 * No subscriber left, cleanup shared data
764 */
765 if (!n_subscribers)
766 {
767 u32 shd_index = vls_shared_data_index (vls_shd);
768
769 clib_spinlock_unlock (&vls_shd->lock);
770 vls_shared_data_pool_runlock ();
771
772 vls_shared_data_free (shd_index);
773
774 /* All locks have been dropped */
Florin Corasf9240dc2019-01-15 08:03:17 -0800775 return 0;
776 }
777
Florin Coras0ef8ef22019-01-18 08:37:13 -0800778 /* Return, if this is not the owning worker */
Florin Coras243edd52020-03-04 22:20:12 +0000779 if (vls_shd->owner_wrk_index != wrk->wrk_index)
780 goto done;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800781
Florin Coras243edd52020-03-04 22:20:12 +0000782 ASSERT (vec_len (vls_shd->workers_subscribed));
783
784 /*
785 * Check if we can change owner or close
786 */
787 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
liuyacan603e1a42021-07-22 15:52:01 +0800788 if (s->vpp_evt_q)
liuyacan5cac2e82021-07-14 15:53:01 +0800789 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000790
791 /* XXX is this still needed? */
792 if (vec_len (vls_shd->workers_subscribed) > 1)
793 clib_warning ("more workers need to be updated");
794
795done:
796
797 clib_spinlock_unlock (&vls_shd->lock);
798 vls_shared_data_pool_runlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800799
800 return 0;
801}
802
803void
Florin Coras40c07ce2020-07-16 20:46:17 -0700804vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
Florin Corasf9240dc2019-01-15 08:03:17 -0800805{
Florin Coras40c07ce2020-07-16 20:46:17 -0700806 vls_shared_data_t *vls_shd;
807
808 u32 vls_shd_index = vls_shared_data_alloc ();
809
810 vls_shared_data_pool_rlock ();
811
812 vls_shd = vls_shared_data_get (vls_shd_index);
Florin Coras5e618432021-07-26 18:19:25 -0700813 vls_shd->owner_wrk_index = vls_wrk->vcl_wrk_index;
Florin Coras40c07ce2020-07-16 20:46:17 -0700814 vls->shared_data_index = vls_shd_index;
Florin Coras5e618432021-07-26 18:19:25 -0700815 vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
Florin Coras40c07ce2020-07-16 20:46:17 -0700816
817 vls_shared_data_pool_runlock ();
818}
819
820void
821vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
822{
Florin Coras5e618432021-07-26 18:19:25 -0700823 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000824 vls_shared_data_t *vls_shd;
825 vcl_session_t *s;
Florin Corasf9240dc2019-01-15 08:03:17 -0800826
Florin Coras243edd52020-03-04 22:20:12 +0000827 s = vcl_session_get (vcl_wrk, vls->session_index);
828 if (!s)
829 {
Florin Coras40c07ce2020-07-16 20:46:17 -0700830 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
831 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
Florin Coras243edd52020-03-04 22:20:12 +0000832 return;
833 }
834
Florin Coras40c07ce2020-07-16 20:46:17 -0700835 ASSERT (vls->shared_data_index != ~0);
836
Florin Coras243edd52020-03-04 22:20:12 +0000837 /* Reinit session lock */
838 clib_spinlock_init (&vls->lock);
839
Florin Coras40c07ce2020-07-16 20:46:17 -0700840 vls_shared_data_pool_rlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000841
Florin Coras40c07ce2020-07-16 20:46:17 -0700842 vls_shd = vls_shared_data_get (vls->shared_data_index);
Florin Coras243edd52020-03-04 22:20:12 +0000843
844 clib_spinlock_lock (&vls_shd->lock);
Florin Coras5e618432021-07-26 18:19:25 -0700845 vec_add1 (vls_shd->workers_subscribed, vls_wrk->vcl_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000846 clib_spinlock_unlock (&vls_shd->lock);
Florin Coras40c07ce2020-07-16 20:46:17 -0700847
Florin Coras243edd52020-03-04 22:20:12 +0000848 vls_shared_data_pool_runlock ();
849
Florin Coras7428eaa2023-12-11 16:04:57 -0800850 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800851 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700852 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras7428eaa2023-12-11 16:04:57 -0800853 s->rx_fifo = s->tx_fifo = 0;
854 }
855 else if (s->rx_fifo)
856 {
857 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
Florin Coras2d675d72019-01-28 15:54:27 -0800858 }
Florin Coras243edd52020-03-04 22:20:12 +0000859}
Florin Coras2d675d72019-01-28 15:54:27 -0800860
Florin Coras243edd52020-03-04 22:20:12 +0000861static void
862vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
863{
Florin Coras40c07ce2020-07-16 20:46:17 -0700864 vcl_locked_session_t *vls, *parent_vls;
Florin Coras243edd52020-03-04 22:20:12 +0000865
866 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100867 pool_foreach (vls, vls_wrk->vls_pool) {
Florin Coras40c07ce2020-07-16 20:46:17 -0700868 /* Initialize sharing on parent session */
869 if (vls->shared_data_index == ~0)
870 {
871 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
872 vls_init_share_session (vls_parent_wrk, parent_vls);
873 vls->shared_data_index = parent_vls->shared_data_index;
874 }
875 vls_share_session (vls_wrk, vls);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100876 }
Florin Coras243edd52020-03-04 22:20:12 +0000877 /* *INDENT-ON* */
Florin Corasf9240dc2019-01-15 08:03:17 -0800878}
879
wanghanlin5788a342021-06-22 17:34:08 +0800880static void
881vls_validate_veps (vcl_worker_t *wrk)
882{
883 vcl_session_t *s;
884 u32 session_index, wrk_index;
885
886 pool_foreach (s, wrk->sessions)
887 {
888 if (s->vep.vep_sh != ~0)
889 {
890 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
891 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
892 }
893 if (s->vep.next_sh != ~0)
894 {
895 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
896 &session_index);
897 s->vep.next_sh = vcl_session_handle_from_index (session_index);
898 }
899 if (s->vep.prev_sh != ~0)
900 {
901 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
902 &session_index);
903 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
904 }
905 }
906}
907
Florin Corasf9240dc2019-01-15 08:03:17 -0800908void
909vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
910{
Florin Coras243edd52020-03-04 22:20:12 +0000911 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
Florin Coras5e618432021-07-26 18:19:25 -0700912 vcl_worker_t *vcl_wrk = vcl_worker_get_current ();
hanlinf8e13632020-08-21 11:05:36 +0800913 u32 vls_index, session_index, wrk_index;
914 vcl_session_handle_t sh;
liuyacan603e1a42021-07-22 15:52:01 +0800915 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -0800916
Florin Coras243edd52020-03-04 22:20:12 +0000917 /*
918 * init vcl worker
919 */
Florin Coras5e618432021-07-26 18:19:25 -0700920 vcl_wrk->sessions = pool_dup (parent_wrk->sessions);
921 vcl_wrk->session_index_by_vpp_handles =
Florin Corasf9240dc2019-01-15 08:03:17 -0800922 hash_dup (parent_wrk->session_index_by_vpp_handles);
923
Florin Coras243edd52020-03-04 22:20:12 +0000924 /*
925 * init vls worker
926 */
927 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
Florin Coras5e618432021-07-26 18:19:25 -0700928
929 /* clang-format off */
930 hash_foreach (sh, vls_index, vls_parent_wrk->sh_to_vlsh_table, ({
931 vcl_session_handle_parse (sh, &wrk_index, &session_index);
932 hash_set (vls_wrk->sh_to_vlsh_table,
933 vcl_session_handle_from_index (session_index), vls_index);
934 }));
935 /* clang-format on */
Florin Coras243edd52020-03-04 22:20:12 +0000936 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
Florin Coras2d675d72019-01-28 15:54:27 -0800937
liuyacan603e1a42021-07-22 15:52:01 +0800938 /*
939 * Detach vls from parent vcl worker and attach them to child.
940 */
941 pool_foreach (vls, vls_wrk->vls_pool)
942 {
Florin Coras5e618432021-07-26 18:19:25 -0700943 vls->vcl_wrk_index = vcl_wrk->wrk_index;
liuyacan603e1a42021-07-22 15:52:01 +0800944 }
945
wanghanlin5788a342021-06-22 17:34:08 +0800946 /* Validate vep's handle */
Florin Coras5e618432021-07-26 18:19:25 -0700947 vls_validate_veps (vcl_wrk);
wanghanlin5788a342021-06-22 17:34:08 +0800948
Florin Coras243edd52020-03-04 22:20:12 +0000949 vls_share_sessions (vls_parent_wrk, vls_wrk);
Florin Corasf9240dc2019-01-15 08:03:17 -0800950}
951
Florin Coras0ef8ef22019-01-18 08:37:13 -0800952static void
953vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
954{
955 vcl_worker_t *wrk = vcl_worker_get_current ();
956 vcl_session_t *s = 0;
957 int is_nonblk = 0;
958
959 if (vls)
960 {
961 s = vcl_session_get (wrk, vls->session_index);
962 if (PREDICT_FALSE (!s))
963 return;
Florin Corasac422d62020-10-19 20:51:36 -0700964 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800965 }
966
967 switch (op)
968 {
969 case VLS_MT_OP_READ:
970 if (!is_nonblk)
971 is_nonblk = vcl_session_read_ready (s) != 0;
972 if (!is_nonblk)
973 {
974 vls_mt_mq_lock ();
975 *locks_acq |= VLS_MT_LOCK_MQ;
976 }
977 break;
978 case VLS_MT_OP_WRITE:
Florin Coras78b5fa62019-02-21 20:04:15 -0800979 ASSERT (s);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800980 if (!is_nonblk)
981 is_nonblk = vcl_session_write_ready (s) != 0;
982 if (!is_nonblk)
983 {
984 vls_mt_mq_lock ();
985 *locks_acq |= VLS_MT_LOCK_MQ;
986 }
987 break;
988 case VLS_MT_OP_XPOLL:
989 vls_mt_mq_lock ();
990 *locks_acq |= VLS_MT_LOCK_MQ;
991 break;
992 case VLS_MT_OP_SPOOL:
993 vls_mt_spool_lock ();
994 *locks_acq |= VLS_MT_LOCK_SPOOL;
995 break;
996 default:
997 break;
998 }
999}
1000
1001static void
1002vls_mt_rel_locks (int locks_acq)
1003{
1004 if (locks_acq & VLS_MT_LOCK_MQ)
1005 vls_mt_mq_unlock ();
1006 if (locks_acq & VLS_MT_LOCK_SPOOL)
1007 vls_mt_create_unlock ();
1008}
1009
Florin Corasff40d8f2020-08-11 22:05:28 -07001010static inline u8
1011vls_mt_session_should_migrate (vcl_locked_session_t * vls)
1012{
Florin Coras5e618432021-07-26 18:19:25 -07001013 return (vls_mt_wrk_supported () &&
1014 vls->vcl_wrk_index != vcl_get_worker_index ());
Florin Corasff40d8f2020-08-11 22:05:28 -07001015}
1016
wanghanlindcacdc42020-12-28 16:19:05 +08001017static vcl_locked_session_t *
1018vls_mt_session_migrate (vcl_locked_session_t *vls)
hanlina3a48962020-07-13 11:09:15 +08001019{
1020 u32 wrk_index = vcl_get_worker_index ();
1021 vcl_worker_t *wrk;
wanghanline8f848a2021-01-08 14:57:11 +08001022 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001023 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +08001024 vcl_session_t *session;
1025 uword *p;
1026
Florin Coras5e618432021-07-26 18:19:25 -07001027 ASSERT (vls_mt_wrk_supported () && vls->vcl_wrk_index != wrk_index);
hanlina3a48962020-07-13 11:09:15 +08001028
Florin Corasff40d8f2020-08-11 22:05:28 -07001029 /*
1030 * VCL session on current vcl worker already allocated. Update current
1031 * owner worker and index and return
1032 */
hanlina3a48962020-07-13 11:09:15 +08001033 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
1034 {
Florin Coras5e618432021-07-26 18:19:25 -07001035 vls->vcl_wrk_index = wrk_index;
hanlina3a48962020-07-13 11:09:15 +08001036 vls->session_index = (u32) p[0];
wanghanlindcacdc42020-12-28 16:19:05 +08001037 return vls;
hanlina3a48962020-07-13 11:09:15 +08001038 }
1039
Florin Corasff40d8f2020-08-11 22:05:28 -07001040 /*
1041 * Ask vcl worker that owns the original vcl session to clone it into
1042 * current vcl worker session pool
1043 */
1044
hanlina3a48962020-07-13 11:09:15 +08001045 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
1046 vls->owner_vcl_wrk_index)))
1047 {
1048 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
1049 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001050 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -07001051 vls_mt_pool_runlock ();
wanghanlindcacdc42020-12-28 16:19:05 +08001052 return 0;
hanlina3a48962020-07-13 11:09:15 +08001053 }
1054
1055 src_sid = (u32) p[0];
1056 wrk = vcl_worker_get_current ();
1057 session = vcl_session_alloc (wrk);
1058 sid = session->session_index;
hanlina3a48962020-07-13 11:09:15 +08001059 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
1060 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
1061
wanghanlindcacdc42020-12-28 16:19:05 +08001062 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
1063 vls_index = vls->vls_index;
1064 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
1065 vls_unlock (vls);
Florin Coras5e618432021-07-26 18:19:25 -07001066 vls_mt_pool_runlock ();
wanghanlindcacdc42020-12-28 16:19:05 +08001067 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
1068 own_vcl_wrk_index, vls_index, src_sid);
1069
1070 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
hanlina3a48962020-07-13 11:09:15 +08001071 {
wanghanlindcacdc42020-12-28 16:19:05 +08001072 VWRN ("session %u not exist", src_sid);
1073 goto err;
1074 }
1075 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
1076 {
1077 VWRN ("failed to wait rpc response");
1078 goto err;
1079 }
1080 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
1081 session->vep.next_sh != ~0))
1082 {
hanlina3a48962020-07-13 11:09:15 +08001083 VERR ("can't migrate nonempty epoll session");
1084 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001085 goto err;
hanlina3a48962020-07-13 11:09:15 +08001086 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001087 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
Florin Corasc127d5a2020-10-14 16:35:58 -07001088 session->session_state != VCL_STATE_CLOSED))
hanlina3a48962020-07-13 11:09:15 +08001089 {
hanlina3a48962020-07-13 11:09:15 +08001090 VERR ("migrate NOT supported, session_status (%u)",
1091 session->session_state);
1092 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001093 goto err;
hanlina3a48962020-07-13 11:09:15 +08001094 }
wanghanlindcacdc42020-12-28 16:19:05 +08001095
1096 vls = vls_get_w_dlock (vls_index);
1097 if (PREDICT_FALSE (!vls))
1098 {
1099 VWRN ("failed to get vls %u", vls_index);
1100 goto err;
1101 }
1102
1103 session->session_index = sid;
Florin Coras5e618432021-07-26 18:19:25 -07001104 vls->vcl_wrk_index = wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001105 vls->session_index = sid;
1106 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
wanghanline8f848a2021-01-08 14:57:11 +08001107 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1108 vls->vls_index);
wanghanlindcacdc42020-12-28 16:19:05 +08001109 return vls;
1110
1111err:
1112 vcl_session_free (wrk, session);
1113 return 0;
hanlina3a48962020-07-13 11:09:15 +08001114}
1115
Florin Corasff40d8f2020-08-11 22:05:28 -07001116static inline void
1117vls_mt_detect (void)
1118{
1119 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1120 vls_mt_add ();
1121}
Florin Coras0ef8ef22019-01-18 08:37:13 -08001122
wanghanlindcacdc42020-12-28 16:19:05 +08001123#define vls_mt_guard(_vls, _op) \
1124 int _locks_acq = 0; \
1125 if (vls_mt_wrk_supported ()) \
1126 { \
1127 if (PREDICT_FALSE (_vls && \
Florin Coras5e618432021-07-26 18:19:25 -07001128 ((vcl_locked_session_t *) _vls)->vcl_wrk_index != \
wanghanlindcacdc42020-12-28 16:19:05 +08001129 vcl_get_worker_index ())) \
1130 { \
1131 _vls = vls_mt_session_migrate (_vls); \
1132 if (PREDICT_FALSE (!_vls)) \
1133 return VPPCOM_EBADFD; \
1134 } \
1135 } \
1136 else \
1137 { \
1138 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1139 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1140 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001141
1142#define vls_mt_unguard() \
1143 if (PREDICT_FALSE (_locks_acq)) \
Florin Coras0ef8ef22019-01-18 08:37:13 -08001144 vls_mt_rel_locks (_locks_acq)
1145
Florin Coras7baeb712019-01-04 17:05:43 -08001146int
1147vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1148{
1149 vcl_locked_session_t *vls;
1150 int rv;
1151
Florin Corasff40d8f2020-08-11 22:05:28 -07001152 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001153 if (!(vls = vls_get_w_dlock (vlsh)))
1154 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001155
1156 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001157 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001158 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001159 vls_get_and_unlock (vlsh);
1160 return rv;
1161}
1162
1163int
1164vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1165{
1166 vcl_locked_session_t *vls;
1167 int rv;
1168
Florin Corasff40d8f2020-08-11 22:05:28 -07001169 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001170 if (!(vls = vls_get_w_dlock (vlsh)))
1171 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001172 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001173 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001174 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001175 vls_get_and_unlock (vlsh);
1176 return rv;
1177}
1178
1179int
1180vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1181 vppcom_endpt_t * ep)
1182{
1183 vcl_locked_session_t *vls;
1184 int rv;
1185
Florin Corasff40d8f2020-08-11 22:05:28 -07001186 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001187 if (!(vls = vls_get_w_dlock (vlsh)))
1188 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001189 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001190 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001191 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001192 vls_get_and_unlock (vlsh);
1193 return rv;
1194}
1195
1196ssize_t
1197vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1198{
1199 vcl_locked_session_t *vls;
1200 int rv;
1201
Florin Corasff40d8f2020-08-11 22:05:28 -07001202 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001203 if (!(vls = vls_get_w_dlock (vlsh)))
1204 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001205 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001206 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001207 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001208 vls_get_and_unlock (vlsh);
1209 return rv;
1210}
1211
1212ssize_t
1213vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1214 vppcom_endpt_t * ep)
1215{
1216 vcl_locked_session_t *vls;
1217 int rv;
1218
Florin Corasff40d8f2020-08-11 22:05:28 -07001219 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001220 if (!(vls = vls_get_w_dlock (vlsh)))
1221 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001222 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001223 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1224 ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001225 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001226 vls_get_and_unlock (vlsh);
1227 return rv;
1228}
1229
1230int
1231vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1232{
1233 vcl_locked_session_t *vls;
1234 int rv;
1235
Florin Corasff40d8f2020-08-11 22:05:28 -07001236 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001237 if (!(vls = vls_get_w_dlock (vlsh)))
1238 return VPPCOM_EBADFD;
Florin Corasff40d8f2020-08-11 22:05:28 -07001239 if (vls_mt_session_should_migrate (vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001240 {
1241 vls = vls_mt_session_migrate (vls);
1242 if (PREDICT_FALSE (!vls))
1243 return VPPCOM_EBADFD;
1244 }
Florin Coras7baeb712019-01-04 17:05:43 -08001245 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1246 vls_get_and_unlock (vlsh);
1247 return rv;
1248}
1249
1250int
1251vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1252{
1253 vcl_locked_session_t *vls;
1254 int rv;
1255
Florin Corasff40d8f2020-08-11 22:05:28 -07001256 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001257 if (!(vls = vls_get_w_dlock (vlsh)))
1258 return VPPCOM_EBADFD;
1259 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1260 vls_get_and_unlock (vlsh);
1261 return rv;
1262}
1263
1264int
1265vls_listen (vls_handle_t vlsh, int q_len)
1266{
1267 vcl_locked_session_t *vls;
1268 int rv;
1269
Florin Corasff40d8f2020-08-11 22:05:28 -07001270 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001271 if (!(vls = vls_get_w_dlock (vlsh)))
1272 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001273 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001274 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001275 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001276 vls_get_and_unlock (vlsh);
1277 return rv;
1278}
1279
1280int
1281vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1282{
1283 vcl_locked_session_t *vls;
1284 int rv;
1285
Florin Corasff40d8f2020-08-11 22:05:28 -07001286 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001287 if (!(vls = vls_get_w_dlock (vlsh)))
1288 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001289 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001290 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001291 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001292 vls_get_and_unlock (vlsh);
1293 return rv;
1294}
1295
Florin Coras2d675d72019-01-28 15:54:27 -08001296static inline void
1297vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1298{
1299 vcl_worker_t *wrk = vcl_worker_get_current ();
1300 vcl_session_t *s;
Florin Coras243edd52020-03-04 22:20:12 +00001301 u32 owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -08001302
hanlina3a48962020-07-13 11:09:15 +08001303 if (vls_mt_wrk_supported ())
1304 return;
1305
Florin Coras5e618432021-07-26 18:19:25 -07001306 ASSERT (wrk->wrk_index == vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001307 s = vcl_session_get (wrk, vls->session_index);
1308 switch (s->session_state)
1309 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001310 case VCL_STATE_LISTEN:
Florin Coras2d675d72019-01-28 15:54:27 -08001311 if (is_add)
1312 {
Florin Coras5e618432021-07-26 18:19:25 -07001313 vls_listener_wrk_set (vls, vls->vcl_wrk_index, 1 /* is_active */);
Florin Coras2d675d72019-01-28 15:54:27 -08001314 break;
1315 }
Florin Corasa41a0b52023-03-01 22:22:30 -08001316 /* Although removal from epoll means listener no longer accepts new
1317 * sessions, the accept queue built by vpp cannot be drained by stopping
1318 * the listener. Morover, some applications, e.g., nginx, might
1319 * constantly remove and add listeners to their epfds. Removing
1320 * listeners in such situations causes a lot of churn in vpp as segments
1321 * and segment managers need to be recreated. */
1322 /* vls_listener_wrk_stop_listen (vls, vls->vcl_wrk_index); */
Florin Coras2d675d72019-01-28 15:54:27 -08001323 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001324 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -08001325 if (!is_add)
1326 break;
1327
1328 /* Register worker as listener */
Florin Coras5e618432021-07-26 18:19:25 -07001329 vls_listener_wrk_start_listen (vls, vls->vcl_wrk_index);
Florin Coras2d675d72019-01-28 15:54:27 -08001330
1331 /* If owner worker did not attempt to accept/xpoll on the session,
1332 * force a listen stop for it, since it may not be interested in
1333 * accepting new sessions.
1334 * This is pretty much a hack done to give app workers the illusion
1335 * that it is fine to listen and not accept new sessions for a
1336 * given listener. Without it, we would accumulate unhandled
1337 * accepts on the passive worker message queue. */
Florin Coras243edd52020-03-04 22:20:12 +00001338 owner_wrk = vls_shared_get_owner (vls);
1339 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1340 vls_listener_wrk_stop_listen (vls, owner_wrk);
Florin Coras2d675d72019-01-28 15:54:27 -08001341 break;
1342 default:
1343 break;
1344 }
1345}
1346
Florin Coras7baeb712019-01-04 17:05:43 -08001347vls_handle_t
1348vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1349{
1350 vls_handle_t accepted_vlsh;
1351 vcl_locked_session_t *vls;
1352 int sh;
1353
Florin Corasff40d8f2020-08-11 22:05:28 -07001354 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001355 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1356 return VPPCOM_EBADFD;
Florin Coras2d675d72019-01-28 15:54:27 -08001357 if (vcl_n_workers () > 1)
1358 vls_mp_checks (vls, 1 /* is_add */ );
Florin Coras0ef8ef22019-01-18 08:37:13 -08001359 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001360 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001361 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001362 vls_get_and_unlock (listener_vlsh);
1363 if (sh < 0)
1364 return sh;
1365 accepted_vlsh = vls_alloc (sh);
1366 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1367 vppcom_session_close (sh);
1368 return accepted_vlsh;
1369}
1370
1371vls_handle_t
1372vls_create (uint8_t proto, uint8_t is_nonblocking)
1373{
1374 vcl_session_handle_t sh;
1375 vls_handle_t vlsh;
wanghanlindcacdc42020-12-28 16:19:05 +08001376 vcl_locked_session_t *vls = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001377
Florin Corasff40d8f2020-08-11 22:05:28 -07001378 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001379 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001380 sh = vppcom_session_create (proto, is_nonblocking);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001381 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001382 if (sh == INVALID_SESSION_ID)
1383 return VLS_INVALID_HANDLE;
1384
1385 vlsh = vls_alloc (sh);
1386 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1387 vppcom_session_close (sh);
1388
1389 return vlsh;
1390}
1391
hanlina3a48962020-07-13 11:09:15 +08001392static void
Florin Corasff40d8f2020-08-11 22:05:28 -07001393vls_mt_session_cleanup (vcl_locked_session_t * vls)
hanlina3a48962020-07-13 11:09:15 +08001394{
Florin Corasff40d8f2020-08-11 22:05:28 -07001395 u32 session_index, wrk_index, current_vcl_wrk;
hanlina3a48962020-07-13 11:09:15 +08001396 vcl_worker_t *wrk = vcl_worker_get_current ();
1397
Florin Corasff40d8f2020-08-11 22:05:28 -07001398 ASSERT (vls_mt_wrk_supported ());
1399
1400 current_vcl_wrk = vcl_get_worker_index ();
hanlina3a48962020-07-13 11:09:15 +08001401
1402 /* *INDENT-OFF* */
1403 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1404 ({
Florin Corasff40d8f2020-08-11 22:05:28 -07001405 if (current_vcl_wrk != wrk_index)
1406 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
hanlina3a48962020-07-13 11:09:15 +08001407 }));
1408 /* *INDENT-ON* */
1409 hash_free (vls->vcl_wrk_index_to_session_index);
1410}
1411
Florin Coras7baeb712019-01-04 17:05:43 -08001412int
1413vls_close (vls_handle_t vlsh)
1414{
1415 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -08001416 int rv;
Florin Coras7baeb712019-01-04 17:05:43 -08001417
Florin Corasff40d8f2020-08-11 22:05:28 -07001418 vls_mt_detect ();
Florin Coras5e618432021-07-26 18:19:25 -07001419 vls_mt_pool_wlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001420
Florin Coras0ef8ef22019-01-18 08:37:13 -08001421 vls = vls_get_and_lock (vlsh);
1422 if (!vls)
1423 {
Florin Coras5e618432021-07-26 18:19:25 -07001424 vls_mt_pool_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001425 return VPPCOM_EBADFD;
1426 }
1427
hanlina3a48962020-07-13 11:09:15 +08001428 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Corasf9240dc2019-01-15 08:03:17 -08001429
Florin Coras243edd52020-03-04 22:20:12 +00001430 if (vls_is_shared (vls))
1431 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1432 else
1433 rv = vppcom_session_close (vls_to_sh (vls));
1434
Florin Corasff40d8f2020-08-11 22:05:28 -07001435 if (vls_mt_wrk_supported ())
1436 vls_mt_session_cleanup (vls);
hanlina3a48962020-07-13 11:09:15 +08001437
Florin Coras0ef8ef22019-01-18 08:37:13 -08001438 vls_free (vls);
1439 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001440
Florin Coras5e618432021-07-26 18:19:25 -07001441 vls_mt_pool_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001442
Florin Coras7baeb712019-01-04 17:05:43 -08001443 return rv;
1444}
1445
liuyacan534468e2021-05-09 03:50:40 +00001446int
liuyacan55c952e2021-06-13 14:54:55 +08001447vls_shutdown (vls_handle_t vlsh, int how)
liuyacan534468e2021-05-09 03:50:40 +00001448{
1449 vcl_locked_session_t *vls;
1450 int rv;
1451
1452 vls_mt_detect ();
1453 if (!(vls = vls_get_w_dlock (vlsh)))
1454 return VPPCOM_EBADFD;
1455
1456 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
liuyacan6fc07b42021-07-24 22:48:36 +08001457 rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
liuyacan534468e2021-05-09 03:50:40 +00001458 vls_mt_unguard ();
1459 vls_get_and_unlock (vlsh);
1460
1461 return rv;
1462}
1463
Florin Coras7baeb712019-01-04 17:05:43 -08001464vls_handle_t
1465vls_epoll_create (void)
1466{
1467 vcl_session_handle_t sh;
1468 vls_handle_t vlsh;
1469
Florin Corasff40d8f2020-08-11 22:05:28 -07001470 vls_mt_detect ();
Florin Coras63d3ac62019-03-29 08:29:25 -07001471
Florin Coras7baeb712019-01-04 17:05:43 -08001472 sh = vppcom_epoll_create ();
1473 if (sh == INVALID_SESSION_ID)
1474 return VLS_INVALID_HANDLE;
1475
1476 vlsh = vls_alloc (sh);
1477 if (vlsh == VLS_INVALID_HANDLE)
1478 vppcom_session_close (sh);
1479
1480 return vlsh;
1481}
1482
Florin Coras2d675d72019-01-28 15:54:27 -08001483static void
1484vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1485{
nandfandc2632e2021-03-26 16:46:58 +08001486 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
Florin Coras2d675d72019-01-28 15:54:27 -08001487 return;
1488
Florin Coras2d675d72019-01-28 15:54:27 -08001489 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1490}
1491
Florin Coras7baeb712019-01-04 17:05:43 -08001492int
1493vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1494 struct epoll_event *event)
1495{
1496 vcl_locked_session_t *ep_vls, *vls;
1497 vcl_session_handle_t ep_sh, sh;
1498 int rv;
1499
Florin Corasff40d8f2020-08-11 22:05:28 -07001500 vls_mt_detect ();
Florin Coras5e618432021-07-26 18:19:25 -07001501 vls_mt_pool_rlock ();
Florin Coras54223ee2022-03-02 21:06:30 -08001502
Florin Coras7baeb712019-01-04 17:05:43 -08001503 ep_vls = vls_get_and_lock (ep_vlsh);
Florin Coras54223ee2022-03-02 21:06:30 -08001504 if (PREDICT_FALSE (!ep_vls))
1505 {
1506 vls_mt_pool_runlock ();
1507 return VPPCOM_EBADFD;
1508 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001509
1510 if (vls_mt_session_should_migrate (ep_vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001511 {
1512 ep_vls = vls_mt_session_migrate (ep_vls);
1513 if (PREDICT_FALSE (!ep_vls))
Florin Coras54223ee2022-03-02 21:06:30 -08001514 {
1515 vls_mt_pool_runlock ();
1516 return VPPCOM_EBADFD;
1517 }
1518 }
1519
1520 vls = vls_get_and_lock (vlsh);
1521 if (PREDICT_FALSE (!vls))
1522 {
1523 vls_unlock (ep_vls);
1524 vls_mt_pool_runlock ();
1525 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001526 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001527
Florin Coras7baeb712019-01-04 17:05:43 -08001528 ep_sh = vls_to_sh (ep_vls);
1529 sh = vls_to_sh (vls);
Florin Coras2d675d72019-01-28 15:54:27 -08001530
nandfandc2632e2021-03-26 16:46:58 +08001531 vls_epoll_ctl_mp_checks (vls, op);
Florin Coras5e618432021-07-26 18:19:25 -07001532 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001533 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1534
Florin Coras5e618432021-07-26 18:19:25 -07001535 vls_mt_pool_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001536 ep_vls = vls_get (ep_vlsh);
1537 vls = vls_get (vlsh);
1538 vls_unlock (vls);
1539 vls_unlock (ep_vls);
Florin Coras5e618432021-07-26 18:19:25 -07001540 vls_mt_pool_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001541 return rv;
1542}
1543
1544int
1545vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1546 int maxevents, double wait_for_time)
1547{
wanghanlindcacdc42020-12-28 16:19:05 +08001548 vcl_locked_session_t *vls, *vls_tmp = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001549 int rv;
1550
Florin Corasff40d8f2020-08-11 22:05:28 -07001551 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001552 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1553 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001554 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001555 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1556 wait_for_time);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001557 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001558 vls_get_and_unlock (ep_vlsh);
wanghanlind72a0342021-05-12 17:00:29 +08001559 vls_handle_pending_wrk_cleanup ();
Florin Coras7baeb712019-01-04 17:05:43 -08001560 return rv;
1561}
1562
Florin Coras2d675d72019-01-28 15:54:27 -08001563static void
1564vls_select_mp_checks (vcl_si_set * read_map)
1565{
1566 vcl_locked_session_t *vls;
1567 vcl_worker_t *wrk;
1568 vcl_session_t *s;
1569 u32 si;
1570
1571 if (vcl_n_workers () <= 1)
1572 {
1573 vlsl->select_mp_check = 1;
1574 return;
1575 }
1576
1577 if (!read_map)
1578 return;
1579
1580 vlsl->select_mp_check = 1;
1581 wrk = vcl_worker_get_current ();
1582
1583 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001584 clib_bitmap_foreach (si, read_map) {
Florin Coras2d675d72019-01-28 15:54:27 -08001585 s = vcl_session_get (wrk, si);
Florin Corasc127d5a2020-10-14 16:35:58 -07001586 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -08001587 {
1588 vls = vls_get (vls_session_index_to_vlsh (si));
1589 vls_mp_checks (vls, 1 /* is_add */);
1590 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001591 }
Florin Coras2d675d72019-01-28 15:54:27 -08001592 /* *INDENT-ON* */
1593}
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
1625 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001626 pool_foreach (s, wrk->sessions) {
hanlinf8e13632020-08-21 11:05:36 +08001627 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -08001628 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1629 vls_unshare_session (vls, wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001630 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001631 /* *INDENT-ON* */
Florin Coras0ef8ef22019-01-18 08:37:13 -08001632}
1633
1634static void
1635vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1636{
Florin Coras243edd52020-03-04 22:20:12 +00001637 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1638
Florin Coras0ef8ef22019-01-18 08:37:13 -08001639 /* Unshare sessions and also cleanup worker since child may have
1640 * called _exit () and therefore vcl may not catch the event */
1641 vls_unshare_vcl_worker_sessions (wrk);
wanghanlinb940fd42021-06-29 16:01:55 +08001642
1643 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1644 * may have been closed, so DONOT notify VPP.
1645 */
1646 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
Florin Coras243edd52020-03-04 22:20:12 +00001647
1648 vls_worker_free (vls_wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001649}
1650
1651static void
Florin Corasf9240dc2019-01-15 08:03:17 -08001652vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1653{
1654 vcl_worker_t *sub_child;
1655 int tries = 0;
1656
1657 if (child_wrk->forked_child != ~0)
1658 {
1659 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1660 if (sub_child)
1661 {
1662 /* Wait a bit, maybe the process is going away */
1663 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1664 usleep (1e3);
1665 if (kill (sub_child->current_pid, 0) < 0)
1666 vls_cleanup_forked_child (child_wrk, sub_child);
1667 }
1668 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001669 vls_cleanup_vcl_worker (child_wrk);
1670 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001671 wrk->forked_child = ~0;
1672}
1673
wanghanlind72a0342021-05-12 17:00:29 +08001674static void
1675vls_handle_pending_wrk_cleanup (void)
1676{
1677 u32 *wip;
1678 vcl_worker_t *child_wrk, *wrk;
1679 vls_worker_t *vls_wrk = vls_worker_get_current ();
1680
Florin Coras5e618432021-07-26 18:19:25 -07001681 if (PREDICT_TRUE (vec_len (vls_wrk->pending_vcl_wrk_cleanup) == 0))
wanghanlind72a0342021-05-12 17:00:29 +08001682 return;
1683
1684 wrk = vcl_worker_get_current ();
Florin Coras5e618432021-07-26 18:19:25 -07001685 vec_foreach (wip, vls_wrk->pending_vcl_wrk_cleanup)
wanghanlind72a0342021-05-12 17:00:29 +08001686 {
1687 child_wrk = vcl_worker_get_if_valid (*wip);
1688 if (!child_wrk)
1689 continue;
1690 vls_cleanup_forked_child (wrk, child_wrk);
1691 }
Florin Coras5e618432021-07-26 18:19:25 -07001692 vec_reset_length (vls_wrk->pending_vcl_wrk_cleanup);
wanghanlind72a0342021-05-12 17:00:29 +08001693}
1694
Florin Corasf9240dc2019-01-15 08:03:17 -08001695static struct sigaction old_sa;
1696
1697static void
1698vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1699{
1700 vcl_worker_t *wrk, *child_wrk;
wanghanlind72a0342021-05-12 17:00:29 +08001701 vls_worker_t *vls_wrk;
Florin Corasf9240dc2019-01-15 08:03:17 -08001702
1703 if (vcl_get_worker_index () == ~0)
1704 return;
1705
1706 if (sigaction (SIGCHLD, &old_sa, 0))
1707 {
1708 VERR ("couldn't restore sigchld");
1709 exit (-1);
1710 }
1711
1712 wrk = vcl_worker_get_current ();
1713 if (wrk->forked_child == ~0)
1714 return;
1715
1716 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1717 if (!child_wrk)
1718 goto done;
1719
1720 if (si && si->si_pid != child_wrk->current_pid)
1721 {
1722 VDBG (0, "unexpected child pid %u", si->si_pid);
1723 goto done;
1724 }
wanghanlind72a0342021-05-12 17:00:29 +08001725
1726 /* Parent process may enter sighandler with a lock, such as lock in localtime
1727 * or in mspace_free, and child wrk cleanup may try to get such locks and
1728 * cause deadlock.
1729 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1730 */
1731 vls_wrk = vls_worker_get_current ();
Florin Coras5e618432021-07-26 18:19:25 -07001732 vec_add1 (vls_wrk->pending_vcl_wrk_cleanup, child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001733
1734done:
1735 if (old_sa.sa_flags & SA_SIGINFO)
1736 {
1737 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1738 fn (signum, si, uc);
1739 }
1740 else
1741 {
1742 void (*fn) (int) = old_sa.sa_handler;
1743 if (fn)
1744 fn (signum);
1745 }
1746}
1747
1748static void
1749vls_incercept_sigchld ()
1750{
1751 struct sigaction sa;
nandfan5fdc47c2021-02-22 17:17:17 +08001752 if (old_sa.sa_sigaction)
1753 {
1754 VDBG (0, "have intercepted sigchld");
1755 return;
1756 }
Florin Corasf9240dc2019-01-15 08:03:17 -08001757 clib_memset (&sa, 0, sizeof (sa));
1758 sa.sa_sigaction = vls_intercept_sigchld_handler;
1759 sa.sa_flags = SA_SIGINFO;
1760 if (sigaction (SIGCHLD, &sa, &old_sa))
1761 {
1762 VERR ("couldn't intercept sigchld");
1763 exit (-1);
1764 }
1765}
1766
1767static void
1768vls_app_pre_fork (void)
1769{
1770 vls_incercept_sigchld ();
1771 vcl_flush_mq_events ();
1772}
1773
1774static void
1775vls_app_fork_child_handler (void)
1776{
1777 vcl_worker_t *parent_wrk;
Florin Corasb88de902020-09-08 16:47:57 -07001778 int parent_wrk_index;
Florin Corasf9240dc2019-01-15 08:03:17 -08001779
1780 parent_wrk_index = vcl_get_worker_index ();
1781 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1782 parent_wrk_index);
1783
1784 /*
Florin Corasb88de902020-09-08 16:47:57 -07001785 * Clear old state
Florin Corasf9240dc2019-01-15 08:03:17 -08001786 */
1787 vcl_set_worker_index (~0);
Florin Corasf9240dc2019-01-15 08:03:17 -08001788
1789 /*
Florin Corasb88de902020-09-08 16:47:57 -07001790 * Allocate and register vcl worker with vpp
Florin Corasf9240dc2019-01-15 08:03:17 -08001791 */
Florin Corasb88de902020-09-08 16:47:57 -07001792 if (vppcom_worker_register ())
Florin Corasf9240dc2019-01-15 08:03:17 -08001793 {
Florin Corasb88de902020-09-08 16:47:57 -07001794 VERR ("couldn't register new worker!");
Florin Corasf9240dc2019-01-15 08:03:17 -08001795 return;
1796 }
1797
1798 /*
Florin Corasb88de902020-09-08 16:47:57 -07001799 * Allocate/initialize vls worker and share sessions
Florin Coras243edd52020-03-04 22:20:12 +00001800 */
1801 vls_worker_alloc ();
Florin Corasf9240dc2019-01-15 08:03:17 -08001802
Florin Coras0ef8ef22019-01-18 08:37:13 -08001803 /* Reset number of threads and set wrk index */
Florin Coras2d675d72019-01-28 15:54:27 -08001804 vlsl->vls_mt_n_threads = 0;
1805 vlsl->vls_wrk_index = vcl_get_worker_index ();
1806 vlsl->select_mp_check = 0;
Florin Coras2e2f9df2021-07-27 22:48:05 -07001807 clib_rwlock_init (&vlsl->vls_pool_lock);
Florin Coras2d675d72019-01-28 15:54:27 -08001808 vls_mt_locks_init ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001809
liuyacan603e1a42021-07-22 15:52:01 +08001810 parent_wrk = vcl_worker_get (parent_wrk_index);
1811 vls_worker_copy_on_fork (parent_wrk);
1812 parent_wrk->forked_child = vcl_get_worker_index ();
1813
Florin Corasf9240dc2019-01-15 08:03:17 -08001814 VDBG (0, "forked child main worker initialized");
1815 vcm->forking = 0;
1816}
1817
1818static void
1819vls_app_fork_parent_handler (void)
1820{
1821 vcm->forking = 1;
1822 while (vcm->forking)
1823 ;
1824}
1825
Florin Coras0ef8ef22019-01-18 08:37:13 -08001826void
1827vls_app_exit (void)
1828{
Florin Coras243edd52020-03-04 22:20:12 +00001829 vls_worker_t *wrk = vls_worker_get_current ();
1830
wanghanlind72a0342021-05-12 17:00:29 +08001831 /* Handle pending wrk cleanup */
1832 vls_handle_pending_wrk_cleanup ();
1833
Florin Coras0ef8ef22019-01-18 08:37:13 -08001834 /* Unshare the sessions. VCL will clean up the worker */
1835 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
Florin Coras243edd52020-03-04 22:20:12 +00001836 vls_worker_free (wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001837}
1838
Florin Coras40c07ce2020-07-16 20:46:17 -07001839static void
1840vls_clone_and_share_rpc_handler (void *args)
1841{
1842 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1843 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1844 vcl_locked_session_t *vls, *dst_vls;
hanlina3a48962020-07-13 11:09:15 +08001845 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
Florin Coras40c07ce2020-07-16 20:46:17 -07001846 vcl_session_t *s, *dst_s;
1847
wanghanlindcacdc42020-12-28 16:19:05 +08001848 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1849 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1850 msg->origin_session_index);
1851
1852 /* VCL locked session can't been protected, so DONT touch it.
1853 * VCL session may been free, check it.
1854 */
1855 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1856 s = vcl_session_get (vcl_wrk, msg->session_index);
1857 if (PREDICT_FALSE (!s))
1858 {
1859 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1860 return;
1861 }
Florin Coras40c07ce2020-07-16 20:46:17 -07001862
hanlina3a48962020-07-13 11:09:15 +08001863 if (!vls_mt_wrk_supported ())
wanghanlindcacdc42020-12-28 16:19:05 +08001864 {
1865 vls = vls_session_get (wrk, msg->vls_index);
1866 vls_init_share_session (wrk, vls);
1867 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1868 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1869 dst_vls->shared_data_index = vls->shared_data_index;
1870 }
hanlina3a48962020-07-13 11:09:15 +08001871 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
Florin Coras40c07ce2020-07-16 20:46:17 -07001872 clib_memcpy (dst_s, s, sizeof (*s));
1873
wanghanlindcacdc42020-12-28 16:19:05 +08001874 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
hanlina3a48962020-07-13 11:09:15 +08001875}
1876
1877static void
1878vls_session_cleanup_rpc_handler (void *args)
1879{
1880 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1881 vcl_worker_t *wrk = vcl_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +08001882 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001883 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
hanlina3a48962020-07-13 11:09:15 +08001884
wanghanlindcacdc42020-12-28 16:19:05 +08001885 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1886 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
hanlina3a48962020-07-13 11:09:15 +08001887
wanghanlindcacdc42020-12-28 16:19:05 +08001888 vppcom_session_close (sh);
wanghanline8f848a2021-01-08 14:57:11 +08001889 vls_sh_to_vlsh_table_del (vls_wrk, sh);
Florin Coras40c07ce2020-07-16 20:46:17 -07001890}
1891
1892static void
1893vls_rpc_handler (void *args)
1894{
1895 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1896 switch (msg->type)
1897 {
1898 case VLS_RPC_CLONE_AND_SHARE:
1899 vls_clone_and_share_rpc_handler (msg->data);
1900 break;
hanlina3a48962020-07-13 11:09:15 +08001901 case VLS_RPC_SESS_CLEANUP:
1902 vls_session_cleanup_rpc_handler (msg->data);
1903 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001904 default:
1905 break;
1906 }
1907}
1908
1909void
wanghanlindcacdc42020-12-28 16:19:05 +08001910vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1911 u32 session_index, u32 vls_wrk_index,
1912 u32 dst_wrk_index, u32 dst_vls_index,
1913 u32 dst_session_index)
Florin Coras40c07ce2020-07-16 20:46:17 -07001914{
1915 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1916 vls_clone_and_share_msg_t *msg;
1917 vls_rpc_msg_t *rpc;
hanlina3a48962020-07-13 11:09:15 +08001918 int ret;
wanghanlindcacdc42020-12-28 16:19:05 +08001919 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
Florin Coras40c07ce2020-07-16 20:46:17 -07001920
1921 rpc = (vls_rpc_msg_t *) & data;
1922 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1923 msg = (vls_clone_and_share_msg_t *) & rpc->data;
hanlina3a48962020-07-13 11:09:15 +08001924 msg->origin_vls_wrk = vls_wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001925 msg->origin_vls_index = origin_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001926 msg->origin_vcl_wrk = wrk->wrk_index;
1927 msg->origin_session_index = session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001928 msg->vls_index = dst_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001929 msg->session_index = dst_session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001930
wanghanlindcacdc42020-12-28 16:19:05 +08001931 /* Try lock and handle rpcs if two threads send each other
1932 * clone requests at the same time.
1933 */
1934 wrk->rpc_done = VLS_RPC_STATE_INIT;
1935 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1936 vcl_flush_mq_events ();
hanlina3a48962020-07-13 11:09:15 +08001937 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1938
Florin Corasff40d8f2020-08-11 22:05:28 -07001939 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001940 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1941 msg->origin_session_index, ret);
wanghanlindcacdc42020-12-28 16:19:05 +08001942 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1943 clib_time_now (&wrk->clib_time) < timeout)
hanlina3a48962020-07-13 11:09:15 +08001944 ;
wanghanlindcacdc42020-12-28 16:19:05 +08001945 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
hanlina3a48962020-07-13 11:09:15 +08001946}
1947
1948void
1949vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1950 u32 dst_wrk_index, u32 dst_session_index)
1951{
1952 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1953 vls_sess_cleanup_msg_t *msg;
1954 vls_rpc_msg_t *rpc;
1955 int ret;
1956
1957 rpc = (vls_rpc_msg_t *) & data;
1958 rpc->type = VLS_RPC_SESS_CLEANUP;
1959 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1960 msg->origin_vcl_wrk = wrk->wrk_index;
1961 msg->session_index = dst_session_index;
1962
hanlina3a48962020-07-13 11:09:15 +08001963 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1964
Florin Corasff40d8f2020-08-11 22:05:28 -07001965 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001966 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
Florin Coras40c07ce2020-07-16 20:46:17 -07001967}
1968
Florin Coras7baeb712019-01-04 17:05:43 -08001969int
1970vls_app_create (char *app_name)
1971{
1972 int rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001973
Florin Coras7baeb712019-01-04 17:05:43 -08001974 if ((rv = vppcom_app_create (app_name)))
1975 return rv;
Florin Coras243edd52020-03-04 22:20:12 +00001976
Florin Coras2d675d72019-01-28 15:54:27 -08001977 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1978 clib_memset (vlsm, 0, sizeof (*vlsm));
Florin Coras243edd52020-03-04 22:20:12 +00001979 clib_rwlock_init (&vlsm->shared_data_lock);
wanghanlindcacdc42020-12-28 16:19:05 +08001980 clib_spinlock_init (&vlsm->worker_rpc_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001981 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1982
Florin Corasf9240dc2019-01-15 08:03:17 -08001983 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1984 vls_app_fork_child_handler);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001985 atexit (vls_app_exit);
Florin Coras243edd52020-03-04 22:20:12 +00001986 vls_worker_alloc ();
Florin Coras2d675d72019-01-28 15:54:27 -08001987 vlsl->vls_wrk_index = vcl_get_worker_index ();
Florin Coras2e2f9df2021-07-27 22:48:05 -07001988 clib_rwlock_init (&vlsl->vls_pool_lock);
Florin Coras2d675d72019-01-28 15:54:27 -08001989 vls_mt_locks_init ();
Florin Coras40c07ce2020-07-16 20:46:17 -07001990 vcm->wrk_rpc_fn = vls_rpc_handler;
Florin Coras7baeb712019-01-04 17:05:43 -08001991 return VPPCOM_OK;
1992}
1993
hanlin4266d4d2020-05-19 17:34:17 +08001994unsigned char
1995vls_use_eventfd (void)
1996{
1997 return vcm->cfg.use_mq_eventfd;
1998}
1999
hanlina3a48962020-07-13 11:09:15 +08002000unsigned char
2001vls_mt_wrk_supported (void)
2002{
Florin Corasff40d8f2020-08-11 22:05:28 -07002003 return vcm->cfg.mt_wrk_supported;
hanlina3a48962020-07-13 11:09:15 +08002004}
2005
2006int
2007vls_use_real_epoll (void)
2008{
2009 if (vcl_get_worker_index () == ~0)
2010 return 0;
2011
2012 return vcl_worker_get_current ()->vcl_needs_real_epoll;
2013}
2014
2015void
2016vls_register_vcl_worker (void)
2017{
wanghanlin492350e2020-09-11 17:19:32 +08002018 vls_mt_add ();
hanlina3a48962020-07-13 11:09:15 +08002019}
2020
Florin Coras7baeb712019-01-04 17:05:43 -08002021/*
2022 * fd.io coding-style-patch-verification: ON
2023 *
2024 * Local Variables:
2025 * eval: (c-set-style "gnu")
2026 * End:
2027 */