blob: 4330d4179c643dfc04e492a44088397bf348dc22 [file] [log] [blame]
Florin Coras7baeb712019-01-04 17:05:43 -08001/*
2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vcl/vcl_locked.h>
17#include <vcl/vcl_private.h>
18
Florin Coras243edd52020-03-04 22:20:12 +000019typedef struct vls_shared_data_
20{
21 clib_spinlock_t lock;
22 u32 owner_wrk_index;
23 u32 *workers_subscribed;
24 clib_bitmap_t *listeners;
25} vls_shared_data_t;
26
Florin Coras7baeb712019-01-04 17:05:43 -080027typedef struct vcl_locked_session_
28{
Florin Corasf9240dc2019-01-15 08:03:17 -080029 clib_spinlock_t lock;
Florin Coras7baeb712019-01-04 17:05:43 -080030 u32 session_index;
31 u32 worker_index;
32 u32 vls_index;
Florin Coras243edd52020-03-04 22:20:12 +000033 u32 shared_data_index;
hanlina3a48962020-07-13 11:09:15 +080034 /** VCL session owned by different workers because of migration */
35 u32 owner_vcl_wrk_index;
36 uword *vcl_wrk_index_to_session_index;
Florin Coras7baeb712019-01-04 17:05:43 -080037} vcl_locked_session_t;
38
Florin Coras243edd52020-03-04 22:20:12 +000039typedef struct vls_worker_
40{
wanghanline8f848a2021-01-08 14:57:11 +080041 clib_rwlock_t sh_to_vlsh_table_lock; /** valid for multithread workers */
Florin Coras243edd52020-03-04 22:20:12 +000042 vcl_locked_session_t *vls_pool;
hanlinf8e13632020-08-21 11:05:36 +080043 uword *session_handle_to_vlsh_table;
Florin Coras243edd52020-03-04 22:20:12 +000044 u32 wrk_index;
wanghanlind72a0342021-05-12 17:00:29 +080045 /** Vector of child wrk to cleanup */
46 u32 *pending_wrk_cleanup;
Florin Coras243edd52020-03-04 22:20:12 +000047} vls_worker_t;
48
Florin Coras2d675d72019-01-28 15:54:27 -080049typedef struct vls_local_
50{
51 int vls_wrk_index;
52 volatile int vls_mt_n_threads;
53 pthread_mutex_t vls_mt_mq_mlock;
54 pthread_mutex_t vls_mt_spool_mlock;
55 volatile u8 select_mp_check;
Florin Coras2d675d72019-01-28 15:54:27 -080056} vls_process_local_t;
57
58static vls_process_local_t vls_local;
59static vls_process_local_t *vlsl = &vls_local;
60
61typedef struct vls_main_
Florin Coras7baeb712019-01-04 17:05:43 -080062{
Florin Coras243edd52020-03-04 22:20:12 +000063 vls_worker_t *workers;
Florin Coras7baeb712019-01-04 17:05:43 -080064 clib_rwlock_t vls_table_lock;
Florin Coras243edd52020-03-04 22:20:12 +000065 /** Pool of data shared by sessions owned by different workers */
66 vls_shared_data_t *shared_data_pool;
67 clib_rwlock_t shared_data_lock;
wanghanlindcacdc42020-12-28 16:19:05 +080068 /** Lock to protect rpc among workers */
69 clib_spinlock_t worker_rpc_lock;
Florin Coras7baeb712019-01-04 17:05:43 -080070} vls_main_t;
71
Florin Coras2d675d72019-01-28 15:54:27 -080072vls_main_t *vlsm;
Florin Coras7baeb712019-01-04 17:05:43 -080073
wanghanlindcacdc42020-12-28 16:19:05 +080074typedef enum
75{
76 VLS_RPC_STATE_INIT,
77 VLS_RPC_STATE_SUCCESS,
78 VLS_RPC_STATE_SESSION_NOT_EXIST,
79} vls_rpc_state_e;
80
Florin Coras40c07ce2020-07-16 20:46:17 -070081typedef enum vls_rpc_msg_type_
82{
83 VLS_RPC_CLONE_AND_SHARE,
hanlina3a48962020-07-13 11:09:15 +080084 VLS_RPC_SESS_CLEANUP,
Florin Coras40c07ce2020-07-16 20:46:17 -070085} vls_rpc_msg_type_e;
86
87typedef struct vls_rpc_msg_
88{
89 u8 type;
90 u8 data[0];
91} vls_rpc_msg_t;
92
93typedef struct vls_clone_and_share_msg_
94{
95 u32 vls_index; /**< vls to be shared */
hanlina3a48962020-07-13 11:09:15 +080096 u32 session_index; /**< vcl session to be shared */
97 u32 origin_vls_wrk; /**< vls worker that initiated the rpc */
Florin Coras40c07ce2020-07-16 20:46:17 -070098 u32 origin_vls_index; /**< vls session of the originator */
hanlina3a48962020-07-13 11:09:15 +080099 u32 origin_vcl_wrk; /**< vcl worker that initiated the rpc */
100 u32 origin_session_index; /**< vcl session of the originator */
Florin Coras40c07ce2020-07-16 20:46:17 -0700101} vls_clone_and_share_msg_t;
102
hanlina3a48962020-07-13 11:09:15 +0800103typedef struct vls_sess_cleanup_msg_
104{
105 u32 session_index; /**< vcl session to be cleaned */
106 u32 origin_vcl_wrk; /**< worker that initiated the rpc */
107} vls_sess_cleanup_msg_t;
108
109void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
110 u32 dst_wrk_index, u32 dst_session_index);
wanghanlindcacdc42020-12-28 16:19:05 +0800111void vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
hanlina3a48962020-07-13 11:09:15 +0800112 u32 session_index, u32 vls_wrk_index,
113 u32 dst_wrk_index, u32 dst_vls_index,
114 u32 dst_session_index);
wanghanlind72a0342021-05-12 17:00:29 +0800115static void vls_cleanup_forked_child (vcl_worker_t *wrk,
116 vcl_worker_t *child_wrk);
117static void vls_handle_pending_wrk_cleanup (void);
hanlina3a48962020-07-13 11:09:15 +0800118
Florin Coras243edd52020-03-04 22:20:12 +0000119static inline u32
120vls_get_worker_index (void)
121{
hanlina3a48962020-07-13 11:09:15 +0800122 if (vls_mt_wrk_supported ())
123 return vlsl->vls_wrk_index;
124 else
125 return vcl_get_worker_index ();
Florin Coras243edd52020-03-04 22:20:12 +0000126}
127
128static u32
129vls_shared_data_alloc (void)
130{
131 vls_shared_data_t *vls_shd;
132 u32 shd_index;
133
134 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
135 pool_get_zero (vlsm->shared_data_pool, vls_shd);
136 clib_spinlock_init (&vls_shd->lock);
137 shd_index = vls_shd - vlsm->shared_data_pool;
138 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
139
140 return shd_index;
141}
142
143static u32
144vls_shared_data_index (vls_shared_data_t * vls_shd)
145{
146 return vls_shd - vlsm->shared_data_pool;
147}
148
149vls_shared_data_t *
150vls_shared_data_get (u32 shd_index)
151{
152 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
153 return 0;
154 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
155}
156
157static void
158vls_shared_data_free (u32 shd_index)
159{
160 vls_shared_data_t *vls_shd;
161
162 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
163 vls_shd = vls_shared_data_get (shd_index);
164 clib_spinlock_free (&vls_shd->lock);
165 clib_bitmap_free (vls_shd->listeners);
166 vec_free (vls_shd->workers_subscribed);
167 pool_put (vlsm->shared_data_pool, vls_shd);
168 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
169}
170
171static inline void
172vls_shared_data_pool_rlock (void)
173{
174 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
175}
176
177static inline void
178vls_shared_data_pool_runlock (void)
179{
180 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
181}
182
Florin Coras7baeb712019-01-04 17:05:43 -0800183static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700184vls_mt_table_rlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800185{
Florin Coras243edd52020-03-04 22:20:12 +0000186 if (vlsl->vls_mt_n_threads > 1)
187 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800188}
189
190static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700191vls_mt_table_runlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800192{
Florin Coras243edd52020-03-04 22:20:12 +0000193 if (vlsl->vls_mt_n_threads > 1)
194 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800195}
196
197static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700198vls_mt_table_wlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800199{
Florin Coras243edd52020-03-04 22:20:12 +0000200 if (vlsl->vls_mt_n_threads > 1)
201 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800202}
203
204static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700205vls_mt_table_wunlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800206{
Florin Coras243edd52020-03-04 22:20:12 +0000207 if (vlsl->vls_mt_n_threads > 1)
208 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800209}
210
Florin Coras0ef8ef22019-01-18 08:37:13 -0800211typedef enum
212{
213 VLS_MT_OP_READ,
214 VLS_MT_OP_WRITE,
215 VLS_MT_OP_SPOOL,
216 VLS_MT_OP_XPOLL,
217} vls_mt_ops_t;
218
219typedef enum
220{
221 VLS_MT_LOCK_MQ = 1 << 0,
222 VLS_MT_LOCK_SPOOL = 1 << 1
223} vls_mt_lock_type_t;
224
Florin Coras0ef8ef22019-01-18 08:37:13 -0800225static void
226vls_mt_add (void)
227{
Florin Coras2d675d72019-01-28 15:54:27 -0800228 vlsl->vls_mt_n_threads += 1;
Florin Corasff40d8f2020-08-11 22:05:28 -0700229
230 /* If multi-thread workers are supported, for each new thread register a new
231 * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
232 * update the vcl worker's thread local worker index variable */
hanlina3a48962020-07-13 11:09:15 +0800233 if (vls_mt_wrk_supported ())
wanghanlin492350e2020-09-11 17:19:32 +0800234 {
235 if (vppcom_worker_register () != VPPCOM_OK)
236 VERR ("failed to register worker");
237 }
hanlina3a48962020-07-13 11:09:15 +0800238 else
239 vcl_set_worker_index (vlsl->vls_wrk_index);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800240}
241
242static inline void
243vls_mt_mq_lock (void)
244{
Florin Coras2d675d72019-01-28 15:54:27 -0800245 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800246}
247
248static inline void
249vls_mt_mq_unlock (void)
250{
Florin Coras2d675d72019-01-28 15:54:27 -0800251 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800252}
253
254static inline void
255vls_mt_spool_lock (void)
256{
Florin Coras2d675d72019-01-28 15:54:27 -0800257 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800258}
259
260static inline void
261vls_mt_create_unlock (void)
262{
Florin Coras2d675d72019-01-28 15:54:27 -0800263 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
264}
265
266static void
267vls_mt_locks_init (void)
268{
269 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
270 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800271}
272
Florin Coras243edd52020-03-04 22:20:12 +0000273u8
274vls_is_shared (vcl_locked_session_t * vls)
275{
276 return (vls->shared_data_index != ~0);
277}
278
279static inline void
280vls_lock (vcl_locked_session_t * vls)
281{
282 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
283 clib_spinlock_lock (&vls->lock);
284}
285
286static inline void
287vls_unlock (vcl_locked_session_t * vls)
288{
289 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
290 clib_spinlock_unlock (&vls->lock);
291}
292
Florin Coras7baeb712019-01-04 17:05:43 -0800293static inline vcl_session_handle_t
294vls_to_sh (vcl_locked_session_t * vls)
295{
Florin Corasf9240dc2019-01-15 08:03:17 -0800296 return vcl_session_handle_from_index (vls->session_index);
Florin Coras7baeb712019-01-04 17:05:43 -0800297}
298
299static inline vcl_session_handle_t
300vls_to_sh_tu (vcl_locked_session_t * vls)
301{
302 vcl_session_handle_t sh;
303 sh = vls_to_sh (vls);
Florin Corasff40d8f2020-08-11 22:05:28 -0700304 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800305 return sh;
306}
307
Florin Coras243edd52020-03-04 22:20:12 +0000308static vls_worker_t *
309vls_worker_get_current (void)
310{
311 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
312}
313
314static void
315vls_worker_alloc (void)
316{
317 vls_worker_t *wrk;
318
319 pool_get_zero (vlsm->workers, wrk);
wanghanline8f848a2021-01-08 14:57:11 +0800320 if (vls_mt_wrk_supported ())
321 clib_rwlock_init (&wrk->sh_to_vlsh_table_lock);
Florin Coras243edd52020-03-04 22:20:12 +0000322 wrk->wrk_index = vcl_get_worker_index ();
wanghanlind72a0342021-05-12 17:00:29 +0800323 vec_validate (wrk->pending_wrk_cleanup, 16);
324 vec_reset_length (wrk->pending_wrk_cleanup);
Florin Coras243edd52020-03-04 22:20:12 +0000325}
326
327static void
328vls_worker_free (vls_worker_t * wrk)
329{
hanlinf8e13632020-08-21 11:05:36 +0800330 hash_free (wrk->session_handle_to_vlsh_table);
wanghanline8f848a2021-01-08 14:57:11 +0800331 if (vls_mt_wrk_supported ())
332 clib_rwlock_free (&wrk->sh_to_vlsh_table_lock);
Florin Coras243edd52020-03-04 22:20:12 +0000333 pool_free (wrk->vls_pool);
334 pool_put (vlsm->workers, wrk);
335}
336
337static vls_worker_t *
338vls_worker_get (u32 wrk_index)
339{
340 if (pool_is_free_index (vlsm->workers, wrk_index))
341 return 0;
342 return pool_elt_at_index (vlsm->workers, wrk_index);
343}
344
wanghanline8f848a2021-01-08 14:57:11 +0800345static void
346vls_sh_to_vlsh_table_add (vls_worker_t *wrk, vcl_session_handle_t sh, u32 vlsh)
347{
348 if (vls_mt_wrk_supported ())
349 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
350 hash_set (wrk->session_handle_to_vlsh_table, sh, vlsh);
351 if (vls_mt_wrk_supported ())
352 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
353}
354
355static void
356vls_sh_to_vlsh_table_del (vls_worker_t *wrk, vcl_session_handle_t sh)
357{
358 if (vls_mt_wrk_supported ())
359 clib_rwlock_writer_lock (&wrk->sh_to_vlsh_table_lock);
360 hash_unset (wrk->session_handle_to_vlsh_table, sh);
361 if (vls_mt_wrk_supported ())
362 clib_rwlock_writer_unlock (&wrk->sh_to_vlsh_table_lock);
363}
364
365static uword *
366vls_sh_to_vlsh_table_get (vls_worker_t *wrk, vcl_session_handle_t sh)
367{
368 if (vls_mt_wrk_supported ())
369 clib_rwlock_reader_lock (&wrk->sh_to_vlsh_table_lock);
370 uword *vlshp = hash_get (wrk->session_handle_to_vlsh_table, sh);
371 if (vls_mt_wrk_supported ())
372 clib_rwlock_reader_unlock (&wrk->sh_to_vlsh_table_lock);
373 return vlshp;
374}
375
Florin Coras7baeb712019-01-04 17:05:43 -0800376static vls_handle_t
377vls_alloc (vcl_session_handle_t sh)
378{
Florin Coras243edd52020-03-04 22:20:12 +0000379 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800380 vcl_locked_session_t *vls;
381
Florin Corasff40d8f2020-08-11 22:05:28 -0700382 vls_mt_table_wlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000383
384 pool_get_zero (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800385 vls->session_index = vppcom_session_index (sh);
386 vls->worker_index = vppcom_session_worker (sh);
Florin Coras243edd52020-03-04 22:20:12 +0000387 vls->vls_index = vls - wrk->vls_pool;
388 vls->shared_data_index = ~0;
wanghanline8f848a2021-01-08 14:57:11 +0800389 vls_sh_to_vlsh_table_add (wrk, sh, vls->vls_index);
hanlina3a48962020-07-13 11:09:15 +0800390 if (vls_mt_wrk_supported ())
391 {
392 hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
393 vls->session_index);
394 vls->owner_vcl_wrk_index = vls->worker_index;
395 }
Florin Coras7baeb712019-01-04 17:05:43 -0800396 clib_spinlock_init (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000397
Florin Corasff40d8f2020-08-11 22:05:28 -0700398 vls_mt_table_wunlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800399 return vls->vls_index;
400}
401
402static vcl_locked_session_t *
403vls_get (vls_handle_t vlsh)
404{
Florin Coras243edd52020-03-04 22:20:12 +0000405 vls_worker_t *wrk = vls_worker_get_current ();
406 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800407 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000408 return pool_elt_at_index (wrk->vls_pool, vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -0800409}
410
411static void
Florin Coras0ef8ef22019-01-18 08:37:13 -0800412vls_free (vcl_locked_session_t * vls)
Florin Coras7baeb712019-01-04 17:05:43 -0800413{
Florin Coras243edd52020-03-04 22:20:12 +0000414 vls_worker_t *wrk = vls_worker_get_current ();
415
Florin Coras0ef8ef22019-01-18 08:37:13 -0800416 ASSERT (vls != 0);
wanghanline8f848a2021-01-08 14:57:11 +0800417 vls_sh_to_vlsh_table_del (
418 wrk, vcl_session_handle_from_index (vls->session_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -0800419 clib_spinlock_free (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000420 pool_put (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800421}
422
423static vcl_locked_session_t *
424vls_get_and_lock (vls_handle_t vlsh)
425{
Florin Coras243edd52020-03-04 22:20:12 +0000426 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800427 vcl_locked_session_t *vls;
Florin Coras243edd52020-03-04 22:20:12 +0000428 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800429 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000430 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
431 vls_lock (vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800432 return vls;
433}
434
435static vcl_locked_session_t *
436vls_get_w_dlock (vls_handle_t vlsh)
437{
438 vcl_locked_session_t *vls;
Florin Corasff40d8f2020-08-11 22:05:28 -0700439 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800440 vls = vls_get_and_lock (vlsh);
441 if (!vls)
Florin Corasff40d8f2020-08-11 22:05:28 -0700442 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800443 return vls;
444}
445
446static inline void
Florin Coras7baeb712019-01-04 17:05:43 -0800447vls_get_and_unlock (vls_handle_t vlsh)
448{
449 vcl_locked_session_t *vls;
Florin Corasff40d8f2020-08-11 22:05:28 -0700450 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800451 vls = vls_get (vlsh);
452 vls_unlock (vls);
Florin Corasff40d8f2020-08-11 22:05:28 -0700453 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800454}
455
456static inline void
457vls_dunlock (vcl_locked_session_t * vls)
458{
459 vls_unlock (vls);
Florin Corasff40d8f2020-08-11 22:05:28 -0700460 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800461}
462
Florin Coras243edd52020-03-04 22:20:12 +0000463static vcl_locked_session_t *
464vls_session_get (vls_worker_t * wrk, u32 vls_index)
465{
466 if (pool_is_free_index (wrk->vls_pool, vls_index))
467 return 0;
468 return pool_elt_at_index (wrk->vls_pool, vls_index);
469}
470
Florin Coras2d675d72019-01-28 15:54:27 -0800471vcl_session_handle_t
472vlsh_to_sh (vls_handle_t vlsh)
473{
474 vcl_locked_session_t *vls;
475 int rv;
476
477 vls = vls_get_w_dlock (vlsh);
478 if (!vls)
479 return INVALID_SESSION_ID;
480 rv = vls_to_sh (vls);
481 vls_dunlock (vls);
482 return rv;
483}
484
485vcl_session_handle_t
486vlsh_to_session_index (vls_handle_t vlsh)
487{
488 vcl_session_handle_t sh;
489 sh = vlsh_to_sh (vlsh);
490 return vppcom_session_index (sh);
491}
492
493vls_handle_t
hanlinf8e13632020-08-21 11:05:36 +0800494vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
Florin Coras2d675d72019-01-28 15:54:27 -0800495{
Florin Coras243edd52020-03-04 22:20:12 +0000496 vls_worker_t *wrk = vls_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +0800497 uword *vlshp = vls_sh_to_vlsh_table_get (
498 wrk,
499 vcl_session_handle_from_wrk_session_index (session_index, vcl_wrk_index));
500
Florin Coras2d675d72019-01-28 15:54:27 -0800501 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
502}
503
504vls_handle_t
505vls_session_index_to_vlsh (uint32_t session_index)
506{
507 vls_handle_t vlsh;
508
Florin Corasff40d8f2020-08-11 22:05:28 -0700509 vls_mt_table_rlock ();
hanlinf8e13632020-08-21 11:05:36 +0800510 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
Florin Corasff40d8f2020-08-11 22:05:28 -0700511 vls_mt_table_runlock ();
Florin Coras2d675d72019-01-28 15:54:27 -0800512
513 return vlsh;
514}
515
Florin Corasf9240dc2019-01-15 08:03:17 -0800516u8
Florin Coras0ef8ef22019-01-18 08:37:13 -0800517vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
Florin Corasf9240dc2019-01-15 08:03:17 -0800518{
Florin Coras243edd52020-03-04 22:20:12 +0000519 vls_shared_data_t *vls_shd;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800520 int i;
Florin Coras243edd52020-03-04 22:20:12 +0000521
522 if (vls->shared_data_index == ~0)
523 return 0;
524
525 vls_shared_data_pool_rlock ();
526
527 vls_shd = vls_shared_data_get (vls->shared_data_index);
528 clib_spinlock_lock (&vls_shd->lock);
529
530 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
531 if (vls_shd->workers_subscribed[i] == wrk_index)
532 {
533 clib_spinlock_unlock (&vls_shd->lock);
534 vls_shared_data_pool_runlock ();
535 return 1;
536 }
537 clib_spinlock_unlock (&vls_shd->lock);
538
539 vls_shared_data_pool_runlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -0800540 return 0;
541}
542
Florin Coras2d675d72019-01-28 15:54:27 -0800543static void
544vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
545{
Florin Coras243edd52020-03-04 22:20:12 +0000546 vls_shared_data_t *vls_shd;
547
548 if (vls->shared_data_index == ~0)
549 {
550 clib_warning ("not a shared session");
551 return;
552 }
553
554 vls_shared_data_pool_rlock ();
555
556 vls_shd = vls_shared_data_get (vls->shared_data_index);
557
558 clib_spinlock_lock (&vls_shd->lock);
559 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
560 clib_spinlock_unlock (&vls_shd->lock);
561
562 vls_shared_data_pool_runlock ();
563}
564
565static u32
566vls_shared_get_owner (vcl_locked_session_t * vls)
567{
568 vls_shared_data_t *vls_shd;
569 u32 owner_wrk;
570
571 vls_shared_data_pool_rlock ();
572
573 vls_shd = vls_shared_data_get (vls->shared_data_index);
574 owner_wrk = vls_shd->owner_wrk_index;
575
576 vls_shared_data_pool_runlock ();
577
578 return owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -0800579}
580
581static u8
582vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
583{
Florin Coras243edd52020-03-04 22:20:12 +0000584 vls_shared_data_t *vls_shd;
585 u8 is_set;
586
587 if (vls->shared_data_index == ~0)
588 {
589 clib_warning ("not a shared session");
590 return 0;
591 }
592
593 vls_shared_data_pool_rlock ();
594
595 vls_shd = vls_shared_data_get (vls->shared_data_index);
596
597 clib_spinlock_lock (&vls_shd->lock);
598 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
599 clib_spinlock_unlock (&vls_shd->lock);
600
601 vls_shared_data_pool_runlock ();
602
603 return (is_set == 1);
Florin Coras2d675d72019-01-28 15:54:27 -0800604}
605
606static void
607vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
608{
609 vppcom_session_listen (vls_to_sh (vls), ~0);
610 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
611}
612
613static void
614vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
615{
616 vcl_worker_t *wrk;
617 vcl_session_t *s;
618
619 wrk = vcl_worker_get (wrk_index);
620 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700621 if (s->session_state != VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800622 return;
Florin Coras458089b2019-08-21 16:20:44 -0700623 vcl_send_session_unlisten (wrk, s);
Florin Corasc127d5a2020-10-14 16:35:58 -0700624 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800625 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
626}
627
Florin Coras243edd52020-03-04 22:20:12 +0000628static int
629vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
630 u32 wrk_index)
631{
632 int i;
633
634 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
635 {
636 if (vls_shd->workers_subscribed[i] == wrk_index)
637 return i;
638 }
639 return -1;
640}
641
Florin Coras0ef8ef22019-01-18 08:37:13 -0800642int
643vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
644{
Florin Coras243edd52020-03-04 22:20:12 +0000645 vls_shared_data_t *vls_shd;
Florin Coras311817f2020-03-07 17:45:47 +0000646 int do_disconnect, pos;
647 u32 n_subscribers;
Florin Corasf9240dc2019-01-15 08:03:17 -0800648 vcl_session_t *s;
Florin Coras2d675d72019-01-28 15:54:27 -0800649
hanlina3a48962020-07-13 11:09:15 +0800650 if (vls->shared_data_index == ~0)
651 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000652
Florin Coras2d675d72019-01-28 15:54:27 -0800653 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700654 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800655 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
Florin Corasf9240dc2019-01-15 08:03:17 -0800656
Florin Coras243edd52020-03-04 22:20:12 +0000657 vls_shared_data_pool_rlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800658
Florin Coras243edd52020-03-04 22:20:12 +0000659 vls_shd = vls_shared_data_get (vls->shared_data_index);
660 clib_spinlock_lock (&vls_shd->lock);
661
662 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
663 if (pos < 0)
664 {
665 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
666 vls->worker_index);
667 goto done;
668 }
669
670 /*
671 * Unsubscribe from share data and fifos
672 */
673 if (s->rx_fifo)
674 {
675 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
676 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
677 }
678 vec_del1 (vls_shd->workers_subscribed, pos);
679
680 /*
681 * Cleanup vcl state
682 */
683 n_subscribers = vec_len (vls_shd->workers_subscribed);
Florin Corasc127d5a2020-10-14 16:35:58 -0700684 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
Florin Coras243edd52020-03-04 22:20:12 +0000685 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
686
687 /*
688 * No subscriber left, cleanup shared data
689 */
690 if (!n_subscribers)
691 {
692 u32 shd_index = vls_shared_data_index (vls_shd);
693
694 clib_spinlock_unlock (&vls_shd->lock);
695 vls_shared_data_pool_runlock ();
696
697 vls_shared_data_free (shd_index);
698
699 /* All locks have been dropped */
Florin Corasf9240dc2019-01-15 08:03:17 -0800700 return 0;
701 }
702
Florin Coras0ef8ef22019-01-18 08:37:13 -0800703 /* Return, if this is not the owning worker */
Florin Coras243edd52020-03-04 22:20:12 +0000704 if (vls_shd->owner_wrk_index != wrk->wrk_index)
705 goto done;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800706
Florin Coras243edd52020-03-04 22:20:12 +0000707 ASSERT (vec_len (vls_shd->workers_subscribed));
708
709 /*
710 * Check if we can change owner or close
711 */
712 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
liuyacan5cac2e82021-07-14 15:53:01 +0800713 if (s->session_state != VCL_STATE_LISTEN_NO_MQ)
714 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000715
716 /* XXX is this still needed? */
717 if (vec_len (vls_shd->workers_subscribed) > 1)
718 clib_warning ("more workers need to be updated");
719
720done:
721
722 clib_spinlock_unlock (&vls_shd->lock);
723 vls_shared_data_pool_runlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800724
725 return 0;
726}
727
728void
Florin Coras40c07ce2020-07-16 20:46:17 -0700729vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
Florin Corasf9240dc2019-01-15 08:03:17 -0800730{
Florin Coras40c07ce2020-07-16 20:46:17 -0700731 vls_shared_data_t *vls_shd;
732
733 u32 vls_shd_index = vls_shared_data_alloc ();
734
735 vls_shared_data_pool_rlock ();
736
737 vls_shd = vls_shared_data_get (vls_shd_index);
738 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
739 vls->shared_data_index = vls_shd_index;
740 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
741
742 vls_shared_data_pool_runlock ();
743}
744
745void
746vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
747{
748 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000749 vls_shared_data_t *vls_shd;
750 vcl_session_t *s;
Florin Corasf9240dc2019-01-15 08:03:17 -0800751
Florin Coras243edd52020-03-04 22:20:12 +0000752 s = vcl_session_get (vcl_wrk, vls->session_index);
753 if (!s)
754 {
Florin Coras40c07ce2020-07-16 20:46:17 -0700755 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
756 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
Florin Coras243edd52020-03-04 22:20:12 +0000757 return;
758 }
759
Florin Coras40c07ce2020-07-16 20:46:17 -0700760 ASSERT (vls->shared_data_index != ~0);
761
Florin Coras243edd52020-03-04 22:20:12 +0000762 /* Reinit session lock */
763 clib_spinlock_init (&vls->lock);
764
Florin Coras40c07ce2020-07-16 20:46:17 -0700765 vls_shared_data_pool_rlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000766
Florin Coras40c07ce2020-07-16 20:46:17 -0700767 vls_shd = vls_shared_data_get (vls->shared_data_index);
Florin Coras243edd52020-03-04 22:20:12 +0000768
769 clib_spinlock_lock (&vls_shd->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000770 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000771 clib_spinlock_unlock (&vls_shd->lock);
Florin Coras40c07ce2020-07-16 20:46:17 -0700772
Florin Coras243edd52020-03-04 22:20:12 +0000773 vls_shared_data_pool_runlock ();
774
Florin Corasf9240dc2019-01-15 08:03:17 -0800775 if (s->rx_fifo)
776 {
Florin Coras8eb8d502021-06-16 14:46:57 -0700777 vcl_session_share_fifos (s, s->rx_fifo, s->tx_fifo);
Florin Corasf9240dc2019-01-15 08:03:17 -0800778 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700779 else if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800780 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700781 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800782 }
Florin Coras243edd52020-03-04 22:20:12 +0000783}
Florin Coras2d675d72019-01-28 15:54:27 -0800784
Florin Coras243edd52020-03-04 22:20:12 +0000785static void
786vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
787{
Florin Coras40c07ce2020-07-16 20:46:17 -0700788 vcl_locked_session_t *vls, *parent_vls;
Florin Coras243edd52020-03-04 22:20:12 +0000789
790 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100791 pool_foreach (vls, vls_wrk->vls_pool) {
Florin Coras40c07ce2020-07-16 20:46:17 -0700792 /* Initialize sharing on parent session */
793 if (vls->shared_data_index == ~0)
794 {
795 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
796 vls_init_share_session (vls_parent_wrk, parent_vls);
797 vls->shared_data_index = parent_vls->shared_data_index;
798 }
799 vls_share_session (vls_wrk, vls);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100800 }
Florin Coras243edd52020-03-04 22:20:12 +0000801 /* *INDENT-ON* */
Florin Corasf9240dc2019-01-15 08:03:17 -0800802}
803
wanghanlin5788a342021-06-22 17:34:08 +0800804static void
805vls_validate_veps (vcl_worker_t *wrk)
806{
807 vcl_session_t *s;
808 u32 session_index, wrk_index;
809
810 pool_foreach (s, wrk->sessions)
811 {
812 if (s->vep.vep_sh != ~0)
813 {
814 vcl_session_handle_parse (s->vep.vep_sh, &wrk_index, &session_index);
815 s->vep.vep_sh = vcl_session_handle_from_index (session_index);
816 }
817 if (s->vep.next_sh != ~0)
818 {
819 vcl_session_handle_parse (s->vep.next_sh, &wrk_index,
820 &session_index);
821 s->vep.next_sh = vcl_session_handle_from_index (session_index);
822 }
823 if (s->vep.prev_sh != ~0)
824 {
825 vcl_session_handle_parse (s->vep.prev_sh, &wrk_index,
826 &session_index);
827 s->vep.prev_sh = vcl_session_handle_from_index (session_index);
828 }
829 }
830}
831
Florin Corasf9240dc2019-01-15 08:03:17 -0800832void
833vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
834{
Florin Coras243edd52020-03-04 22:20:12 +0000835 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
Florin Corasf9240dc2019-01-15 08:03:17 -0800836 vcl_worker_t *wrk = vcl_worker_get_current ();
hanlinf8e13632020-08-21 11:05:36 +0800837 u32 vls_index, session_index, wrk_index;
838 vcl_session_handle_t sh;
Florin Corasf9240dc2019-01-15 08:03:17 -0800839
Florin Coras243edd52020-03-04 22:20:12 +0000840 /*
841 * init vcl worker
842 */
Florin Corasf9240dc2019-01-15 08:03:17 -0800843 wrk->sessions = pool_dup (parent_wrk->sessions);
844 wrk->session_index_by_vpp_handles =
845 hash_dup (parent_wrk->session_index_by_vpp_handles);
846
Florin Coras243edd52020-03-04 22:20:12 +0000847 /*
848 * init vls worker
849 */
850 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
hanlinf8e13632020-08-21 11:05:36 +0800851 /* *INDENT-OFF* */
852 hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
853 ({
854 vcl_session_handle_parse (sh, &wrk_index, &session_index);
855 hash_set (vls_wrk->session_handle_to_vlsh_table,
856 vcl_session_handle_from_index (session_index), vls_index);
857 }));
858 /* *INDENT-ON* */
Florin Coras243edd52020-03-04 22:20:12 +0000859 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
Florin Coras2d675d72019-01-28 15:54:27 -0800860
wanghanlin5788a342021-06-22 17:34:08 +0800861 /* Validate vep's handle */
862 vls_validate_veps (wrk);
863
Florin Coras243edd52020-03-04 22:20:12 +0000864 vls_share_sessions (vls_parent_wrk, vls_wrk);
Florin Corasf9240dc2019-01-15 08:03:17 -0800865}
866
Florin Coras0ef8ef22019-01-18 08:37:13 -0800867static void
868vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
869{
870 vcl_worker_t *wrk = vcl_worker_get_current ();
871 vcl_session_t *s = 0;
872 int is_nonblk = 0;
873
874 if (vls)
875 {
876 s = vcl_session_get (wrk, vls->session_index);
877 if (PREDICT_FALSE (!s))
878 return;
Florin Corasac422d62020-10-19 20:51:36 -0700879 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800880 }
881
882 switch (op)
883 {
884 case VLS_MT_OP_READ:
885 if (!is_nonblk)
886 is_nonblk = vcl_session_read_ready (s) != 0;
887 if (!is_nonblk)
888 {
889 vls_mt_mq_lock ();
890 *locks_acq |= VLS_MT_LOCK_MQ;
891 }
892 break;
893 case VLS_MT_OP_WRITE:
Florin Coras78b5fa62019-02-21 20:04:15 -0800894 ASSERT (s);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800895 if (!is_nonblk)
896 is_nonblk = vcl_session_write_ready (s) != 0;
897 if (!is_nonblk)
898 {
899 vls_mt_mq_lock ();
900 *locks_acq |= VLS_MT_LOCK_MQ;
901 }
902 break;
903 case VLS_MT_OP_XPOLL:
904 vls_mt_mq_lock ();
905 *locks_acq |= VLS_MT_LOCK_MQ;
906 break;
907 case VLS_MT_OP_SPOOL:
908 vls_mt_spool_lock ();
909 *locks_acq |= VLS_MT_LOCK_SPOOL;
910 break;
911 default:
912 break;
913 }
914}
915
916static void
917vls_mt_rel_locks (int locks_acq)
918{
919 if (locks_acq & VLS_MT_LOCK_MQ)
920 vls_mt_mq_unlock ();
921 if (locks_acq & VLS_MT_LOCK_SPOOL)
922 vls_mt_create_unlock ();
923}
924
Florin Corasff40d8f2020-08-11 22:05:28 -0700925static inline u8
926vls_mt_session_should_migrate (vcl_locked_session_t * vls)
927{
928 return (vls_mt_wrk_supported ()
929 && vls->worker_index != vcl_get_worker_index ());
930}
931
wanghanlindcacdc42020-12-28 16:19:05 +0800932static vcl_locked_session_t *
933vls_mt_session_migrate (vcl_locked_session_t *vls)
hanlina3a48962020-07-13 11:09:15 +0800934{
935 u32 wrk_index = vcl_get_worker_index ();
936 vcl_worker_t *wrk;
wanghanline8f848a2021-01-08 14:57:11 +0800937 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +0800938 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +0800939 vcl_session_t *session;
940 uword *p;
941
Florin Corasff40d8f2020-08-11 22:05:28 -0700942 ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
hanlina3a48962020-07-13 11:09:15 +0800943
Florin Corasff40d8f2020-08-11 22:05:28 -0700944 /*
945 * VCL session on current vcl worker already allocated. Update current
946 * owner worker and index and return
947 */
hanlina3a48962020-07-13 11:09:15 +0800948 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
949 {
950 vls->worker_index = wrk_index;
951 vls->session_index = (u32) p[0];
wanghanlindcacdc42020-12-28 16:19:05 +0800952 return vls;
hanlina3a48962020-07-13 11:09:15 +0800953 }
954
Florin Corasff40d8f2020-08-11 22:05:28 -0700955 /*
956 * Ask vcl worker that owns the original vcl session to clone it into
957 * current vcl worker session pool
958 */
959
hanlina3a48962020-07-13 11:09:15 +0800960 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
961 vls->owner_vcl_wrk_index)))
962 {
963 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
964 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +0800965 vls_unlock (vls);
966 vls_mt_table_runlock ();
967 return 0;
hanlina3a48962020-07-13 11:09:15 +0800968 }
969
970 src_sid = (u32) p[0];
971 wrk = vcl_worker_get_current ();
972 session = vcl_session_alloc (wrk);
973 sid = session->session_index;
hanlina3a48962020-07-13 11:09:15 +0800974 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
975 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
976
wanghanlindcacdc42020-12-28 16:19:05 +0800977 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
978 vls_index = vls->vls_index;
979 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
980 vls_unlock (vls);
981 vls_mt_table_runlock ();
982 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
983 own_vcl_wrk_index, vls_index, src_sid);
984
985 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
hanlina3a48962020-07-13 11:09:15 +0800986 {
wanghanlindcacdc42020-12-28 16:19:05 +0800987 VWRN ("session %u not exist", src_sid);
988 goto err;
989 }
990 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
991 {
992 VWRN ("failed to wait rpc response");
993 goto err;
994 }
995 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
996 session->vep.next_sh != ~0))
997 {
hanlina3a48962020-07-13 11:09:15 +0800998 VERR ("can't migrate nonempty epoll session");
999 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001000 goto err;
hanlina3a48962020-07-13 11:09:15 +08001001 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001002 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
Florin Corasc127d5a2020-10-14 16:35:58 -07001003 session->session_state != VCL_STATE_CLOSED))
hanlina3a48962020-07-13 11:09:15 +08001004 {
hanlina3a48962020-07-13 11:09:15 +08001005 VERR ("migrate NOT supported, session_status (%u)",
1006 session->session_state);
1007 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +08001008 goto err;
hanlina3a48962020-07-13 11:09:15 +08001009 }
wanghanlindcacdc42020-12-28 16:19:05 +08001010
1011 vls = vls_get_w_dlock (vls_index);
1012 if (PREDICT_FALSE (!vls))
1013 {
1014 VWRN ("failed to get vls %u", vls_index);
1015 goto err;
1016 }
1017
1018 session->session_index = sid;
1019 vls->worker_index = wrk_index;
1020 vls->session_index = sid;
1021 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
wanghanline8f848a2021-01-08 14:57:11 +08001022 vls_sh_to_vlsh_table_add (vls_wrk, vcl_session_handle (session),
1023 vls->vls_index);
wanghanlindcacdc42020-12-28 16:19:05 +08001024 return vls;
1025
1026err:
1027 vcl_session_free (wrk, session);
1028 return 0;
hanlina3a48962020-07-13 11:09:15 +08001029}
1030
Florin Corasff40d8f2020-08-11 22:05:28 -07001031static inline void
1032vls_mt_detect (void)
1033{
1034 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
1035 vls_mt_add ();
1036}
Florin Coras0ef8ef22019-01-18 08:37:13 -08001037
wanghanlindcacdc42020-12-28 16:19:05 +08001038#define vls_mt_guard(_vls, _op) \
1039 int _locks_acq = 0; \
1040 if (vls_mt_wrk_supported ()) \
1041 { \
1042 if (PREDICT_FALSE (_vls && \
1043 ((vcl_locked_session_t *) _vls)->worker_index != \
1044 vcl_get_worker_index ())) \
1045 { \
1046 _vls = vls_mt_session_migrate (_vls); \
1047 if (PREDICT_FALSE (!_vls)) \
1048 return VPPCOM_EBADFD; \
1049 } \
1050 } \
1051 else \
1052 { \
1053 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
1054 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
1055 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001056
1057#define vls_mt_unguard() \
1058 if (PREDICT_FALSE (_locks_acq)) \
Florin Coras0ef8ef22019-01-18 08:37:13 -08001059 vls_mt_rel_locks (_locks_acq)
1060
Florin Coras7baeb712019-01-04 17:05:43 -08001061int
1062vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
1063{
1064 vcl_locked_session_t *vls;
1065 int rv;
1066
Florin Corasff40d8f2020-08-11 22:05:28 -07001067 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001068 if (!(vls = vls_get_w_dlock (vlsh)))
1069 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001070
1071 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001072 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001073 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001074 vls_get_and_unlock (vlsh);
1075 return rv;
1076}
1077
1078int
1079vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1080{
1081 vcl_locked_session_t *vls;
1082 int rv;
1083
Florin Corasff40d8f2020-08-11 22:05:28 -07001084 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001085 if (!(vls = vls_get_w_dlock (vlsh)))
1086 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001087 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001088 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001089 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001090 vls_get_and_unlock (vlsh);
1091 return rv;
1092}
1093
1094int
1095vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1096 vppcom_endpt_t * ep)
1097{
1098 vcl_locked_session_t *vls;
1099 int rv;
1100
Florin Corasff40d8f2020-08-11 22:05:28 -07001101 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001102 if (!(vls = vls_get_w_dlock (vlsh)))
1103 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001104 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001105 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001106 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001107 vls_get_and_unlock (vlsh);
1108 return rv;
1109}
1110
1111ssize_t
1112vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1113{
1114 vcl_locked_session_t *vls;
1115 int rv;
1116
Florin Corasff40d8f2020-08-11 22:05:28 -07001117 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001118 if (!(vls = vls_get_w_dlock (vlsh)))
1119 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001120 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001121 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001122 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001123 vls_get_and_unlock (vlsh);
1124 return rv;
1125}
1126
1127ssize_t
1128vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1129 vppcom_endpt_t * ep)
1130{
1131 vcl_locked_session_t *vls;
1132 int rv;
1133
Florin Corasff40d8f2020-08-11 22:05:28 -07001134 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001135 if (!(vls = vls_get_w_dlock (vlsh)))
1136 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001137 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001138 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1139 ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001140 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001141 vls_get_and_unlock (vlsh);
1142 return rv;
1143}
1144
1145int
1146vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1147{
1148 vcl_locked_session_t *vls;
1149 int rv;
1150
Florin Corasff40d8f2020-08-11 22:05:28 -07001151 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001152 if (!(vls = vls_get_w_dlock (vlsh)))
1153 return VPPCOM_EBADFD;
Florin Corasff40d8f2020-08-11 22:05:28 -07001154 if (vls_mt_session_should_migrate (vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001155 {
1156 vls = vls_mt_session_migrate (vls);
1157 if (PREDICT_FALSE (!vls))
1158 return VPPCOM_EBADFD;
1159 }
Florin Coras7baeb712019-01-04 17:05:43 -08001160 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1161 vls_get_and_unlock (vlsh);
1162 return rv;
1163}
1164
1165int
1166vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1167{
1168 vcl_locked_session_t *vls;
1169 int rv;
1170
Florin Corasff40d8f2020-08-11 22:05:28 -07001171 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001172 if (!(vls = vls_get_w_dlock (vlsh)))
1173 return VPPCOM_EBADFD;
1174 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1175 vls_get_and_unlock (vlsh);
1176 return rv;
1177}
1178
1179int
1180vls_listen (vls_handle_t vlsh, int q_len)
1181{
1182 vcl_locked_session_t *vls;
1183 int rv;
1184
Florin Corasff40d8f2020-08-11 22:05:28 -07001185 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001186 if (!(vls = vls_get_w_dlock (vlsh)))
1187 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001188 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001189 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001190 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001191 vls_get_and_unlock (vlsh);
1192 return rv;
1193}
1194
1195int
1196vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1197{
1198 vcl_locked_session_t *vls;
1199 int rv;
1200
Florin Corasff40d8f2020-08-11 22:05:28 -07001201 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001202 if (!(vls = vls_get_w_dlock (vlsh)))
1203 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001204 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001205 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001206 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001207 vls_get_and_unlock (vlsh);
1208 return rv;
1209}
1210
Florin Coras2d675d72019-01-28 15:54:27 -08001211static inline void
1212vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1213{
1214 vcl_worker_t *wrk = vcl_worker_get_current ();
1215 vcl_session_t *s;
Florin Coras243edd52020-03-04 22:20:12 +00001216 u32 owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -08001217
hanlina3a48962020-07-13 11:09:15 +08001218 if (vls_mt_wrk_supported ())
1219 return;
1220
Florin Coras2d675d72019-01-28 15:54:27 -08001221 s = vcl_session_get (wrk, vls->session_index);
1222 switch (s->session_state)
1223 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001224 case VCL_STATE_LISTEN:
Florin Coras2d675d72019-01-28 15:54:27 -08001225 if (is_add)
1226 {
Florin Coras243edd52020-03-04 22:20:12 +00001227 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
Florin Coras2d675d72019-01-28 15:54:27 -08001228 break;
1229 }
1230 vls_listener_wrk_stop_listen (vls, vls->worker_index);
1231 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001232 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -08001233 if (!is_add)
1234 break;
1235
1236 /* Register worker as listener */
1237 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
1238
1239 /* If owner worker did not attempt to accept/xpoll on the session,
1240 * force a listen stop for it, since it may not be interested in
1241 * accepting new sessions.
1242 * This is pretty much a hack done to give app workers the illusion
1243 * that it is fine to listen and not accept new sessions for a
1244 * given listener. Without it, we would accumulate unhandled
1245 * accepts on the passive worker message queue. */
Florin Coras243edd52020-03-04 22:20:12 +00001246 owner_wrk = vls_shared_get_owner (vls);
1247 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1248 vls_listener_wrk_stop_listen (vls, owner_wrk);
Florin Coras2d675d72019-01-28 15:54:27 -08001249 break;
1250 default:
1251 break;
1252 }
1253}
1254
Florin Coras7baeb712019-01-04 17:05:43 -08001255vls_handle_t
1256vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1257{
1258 vls_handle_t accepted_vlsh;
1259 vcl_locked_session_t *vls;
1260 int sh;
1261
Florin Corasff40d8f2020-08-11 22:05:28 -07001262 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001263 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1264 return VPPCOM_EBADFD;
Florin Coras2d675d72019-01-28 15:54:27 -08001265 if (vcl_n_workers () > 1)
1266 vls_mp_checks (vls, 1 /* is_add */ );
Florin Coras0ef8ef22019-01-18 08:37:13 -08001267 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001268 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001269 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001270 vls_get_and_unlock (listener_vlsh);
1271 if (sh < 0)
1272 return sh;
1273 accepted_vlsh = vls_alloc (sh);
1274 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1275 vppcom_session_close (sh);
1276 return accepted_vlsh;
1277}
1278
1279vls_handle_t
1280vls_create (uint8_t proto, uint8_t is_nonblocking)
1281{
1282 vcl_session_handle_t sh;
1283 vls_handle_t vlsh;
wanghanlindcacdc42020-12-28 16:19:05 +08001284 vcl_locked_session_t *vls = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001285
Florin Corasff40d8f2020-08-11 22:05:28 -07001286 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001287 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001288 sh = vppcom_session_create (proto, is_nonblocking);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001289 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001290 if (sh == INVALID_SESSION_ID)
1291 return VLS_INVALID_HANDLE;
1292
1293 vlsh = vls_alloc (sh);
1294 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1295 vppcom_session_close (sh);
1296
1297 return vlsh;
1298}
1299
hanlina3a48962020-07-13 11:09:15 +08001300static void
Florin Corasff40d8f2020-08-11 22:05:28 -07001301vls_mt_session_cleanup (vcl_locked_session_t * vls)
hanlina3a48962020-07-13 11:09:15 +08001302{
Florin Corasff40d8f2020-08-11 22:05:28 -07001303 u32 session_index, wrk_index, current_vcl_wrk;
hanlina3a48962020-07-13 11:09:15 +08001304 vcl_worker_t *wrk = vcl_worker_get_current ();
1305
Florin Corasff40d8f2020-08-11 22:05:28 -07001306 ASSERT (vls_mt_wrk_supported ());
1307
1308 current_vcl_wrk = vcl_get_worker_index ();
hanlina3a48962020-07-13 11:09:15 +08001309
1310 /* *INDENT-OFF* */
1311 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1312 ({
Florin Corasff40d8f2020-08-11 22:05:28 -07001313 if (current_vcl_wrk != wrk_index)
1314 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
hanlina3a48962020-07-13 11:09:15 +08001315 }));
1316 /* *INDENT-ON* */
1317 hash_free (vls->vcl_wrk_index_to_session_index);
1318}
1319
Florin Coras7baeb712019-01-04 17:05:43 -08001320int
1321vls_close (vls_handle_t vlsh)
1322{
1323 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -08001324 int rv;
Florin Coras7baeb712019-01-04 17:05:43 -08001325
Florin Corasff40d8f2020-08-11 22:05:28 -07001326 vls_mt_detect ();
1327 vls_mt_table_wlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001328
Florin Coras0ef8ef22019-01-18 08:37:13 -08001329 vls = vls_get_and_lock (vlsh);
1330 if (!vls)
1331 {
Florin Corasff40d8f2020-08-11 22:05:28 -07001332 vls_mt_table_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001333 return VPPCOM_EBADFD;
1334 }
1335
hanlina3a48962020-07-13 11:09:15 +08001336 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Corasf9240dc2019-01-15 08:03:17 -08001337
Florin Coras243edd52020-03-04 22:20:12 +00001338 if (vls_is_shared (vls))
1339 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1340 else
1341 rv = vppcom_session_close (vls_to_sh (vls));
1342
Florin Corasff40d8f2020-08-11 22:05:28 -07001343 if (vls_mt_wrk_supported ())
1344 vls_mt_session_cleanup (vls);
hanlina3a48962020-07-13 11:09:15 +08001345
Florin Coras0ef8ef22019-01-18 08:37:13 -08001346 vls_free (vls);
1347 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001348
Florin Corasff40d8f2020-08-11 22:05:28 -07001349 vls_mt_table_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001350
Florin Coras7baeb712019-01-04 17:05:43 -08001351 return rv;
1352}
1353
liuyacan534468e2021-05-09 03:50:40 +00001354int
liuyacan55c952e2021-06-13 14:54:55 +08001355vls_shutdown (vls_handle_t vlsh, int how)
liuyacan534468e2021-05-09 03:50:40 +00001356{
1357 vcl_locked_session_t *vls;
1358 int rv;
1359
1360 vls_mt_detect ();
1361 if (!(vls = vls_get_w_dlock (vlsh)))
1362 return VPPCOM_EBADFD;
1363
1364 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
liuyacan6fc07b42021-07-24 22:48:36 +08001365 rv = vppcom_session_shutdown (vls_to_sh_tu (vls), how);
liuyacan534468e2021-05-09 03:50:40 +00001366 vls_mt_unguard ();
1367 vls_get_and_unlock (vlsh);
1368
1369 return rv;
1370}
1371
Florin Coras7baeb712019-01-04 17:05:43 -08001372vls_handle_t
1373vls_epoll_create (void)
1374{
1375 vcl_session_handle_t sh;
1376 vls_handle_t vlsh;
1377
Florin Corasff40d8f2020-08-11 22:05:28 -07001378 vls_mt_detect ();
Florin Coras63d3ac62019-03-29 08:29:25 -07001379
Florin Coras7baeb712019-01-04 17:05:43 -08001380 sh = vppcom_epoll_create ();
1381 if (sh == INVALID_SESSION_ID)
1382 return VLS_INVALID_HANDLE;
1383
1384 vlsh = vls_alloc (sh);
1385 if (vlsh == VLS_INVALID_HANDLE)
1386 vppcom_session_close (sh);
1387
1388 return vlsh;
1389}
1390
Florin Coras2d675d72019-01-28 15:54:27 -08001391static void
1392vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1393{
nandfandc2632e2021-03-26 16:46:58 +08001394 if (vcl_n_workers () <= 1 || op == EPOLL_CTL_MOD)
Florin Coras2d675d72019-01-28 15:54:27 -08001395 return;
1396
Florin Coras2d675d72019-01-28 15:54:27 -08001397 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1398}
1399
Florin Coras7baeb712019-01-04 17:05:43 -08001400int
1401vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1402 struct epoll_event *event)
1403{
1404 vcl_locked_session_t *ep_vls, *vls;
1405 vcl_session_handle_t ep_sh, sh;
1406 int rv;
1407
Florin Corasff40d8f2020-08-11 22:05:28 -07001408 vls_mt_detect ();
1409 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001410 ep_vls = vls_get_and_lock (ep_vlsh);
Florin Corasff40d8f2020-08-11 22:05:28 -07001411
1412 if (vls_mt_session_should_migrate (ep_vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001413 {
1414 ep_vls = vls_mt_session_migrate (ep_vls);
1415 if (PREDICT_FALSE (!ep_vls))
1416 return VPPCOM_EBADFD;
1417 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001418
Florin Coras7baeb712019-01-04 17:05:43 -08001419 ep_sh = vls_to_sh (ep_vls);
wanghanlindcacdc42020-12-28 16:19:05 +08001420 vls = vls_get_and_lock (vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -08001421 sh = vls_to_sh (vls);
Florin Coras2d675d72019-01-28 15:54:27 -08001422
nandfandc2632e2021-03-26 16:46:58 +08001423 vls_epoll_ctl_mp_checks (vls, op);
Florin Corasff40d8f2020-08-11 22:05:28 -07001424 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001425 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1426
Florin Corasff40d8f2020-08-11 22:05:28 -07001427 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001428 ep_vls = vls_get (ep_vlsh);
1429 vls = vls_get (vlsh);
1430 vls_unlock (vls);
1431 vls_unlock (ep_vls);
Florin Corasff40d8f2020-08-11 22:05:28 -07001432 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001433 return rv;
1434}
1435
1436int
1437vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1438 int maxevents, double wait_for_time)
1439{
wanghanlindcacdc42020-12-28 16:19:05 +08001440 vcl_locked_session_t *vls, *vls_tmp = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001441 int rv;
1442
Florin Corasff40d8f2020-08-11 22:05:28 -07001443 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001444 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1445 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001446 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001447 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1448 wait_for_time);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001449 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001450 vls_get_and_unlock (ep_vlsh);
wanghanlind72a0342021-05-12 17:00:29 +08001451 vls_handle_pending_wrk_cleanup ();
Florin Coras7baeb712019-01-04 17:05:43 -08001452 return rv;
1453}
1454
Florin Coras2d675d72019-01-28 15:54:27 -08001455static void
1456vls_select_mp_checks (vcl_si_set * read_map)
1457{
1458 vcl_locked_session_t *vls;
1459 vcl_worker_t *wrk;
1460 vcl_session_t *s;
1461 u32 si;
1462
1463 if (vcl_n_workers () <= 1)
1464 {
1465 vlsl->select_mp_check = 1;
1466 return;
1467 }
1468
1469 if (!read_map)
1470 return;
1471
1472 vlsl->select_mp_check = 1;
1473 wrk = vcl_worker_get_current ();
1474
1475 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001476 clib_bitmap_foreach (si, read_map) {
Florin Coras2d675d72019-01-28 15:54:27 -08001477 s = vcl_session_get (wrk, si);
Florin Corasc127d5a2020-10-14 16:35:58 -07001478 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -08001479 {
1480 vls = vls_get (vls_session_index_to_vlsh (si));
1481 vls_mp_checks (vls, 1 /* is_add */);
1482 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001483 }
Florin Coras2d675d72019-01-28 15:54:27 -08001484 /* *INDENT-ON* */
1485}
1486
Florin Coras0ef8ef22019-01-18 08:37:13 -08001487int
1488vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1489 vcl_si_set * except_map, double wait_for_time)
1490{
1491 int rv;
wanghanlindcacdc42020-12-28 16:19:05 +08001492 vcl_locked_session_t *vls = NULL;
Florin Coras2d675d72019-01-28 15:54:27 -08001493
Florin Corasff40d8f2020-08-11 22:05:28 -07001494 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001495 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras2d675d72019-01-28 15:54:27 -08001496 if (PREDICT_FALSE (!vlsl->select_mp_check))
1497 vls_select_mp_checks (read_map);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001498 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1499 vls_mt_unguard ();
wanghanlind72a0342021-05-12 17:00:29 +08001500 vls_handle_pending_wrk_cleanup ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001501 return rv;
1502}
1503
Florin Corasf9240dc2019-01-15 08:03:17 -08001504static void
Florin Coras0ef8ef22019-01-18 08:37:13 -08001505vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1506{
1507 u32 current_wrk, is_current;
1508 vcl_locked_session_t *vls;
1509 vcl_session_t *s;
1510
Florin Coras14ed6df2019-03-06 21:13:42 -08001511 if (pool_elts (vcm->workers) <= 1)
1512 return;
1513
Florin Coras0ef8ef22019-01-18 08:37:13 -08001514 current_wrk = vcl_get_worker_index ();
1515 is_current = current_wrk == wrk->wrk_index;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001516
1517 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001518 pool_foreach (s, wrk->sessions) {
hanlinf8e13632020-08-21 11:05:36 +08001519 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -08001520 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1521 vls_unshare_session (vls, wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001522 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001523 /* *INDENT-ON* */
Florin Coras0ef8ef22019-01-18 08:37:13 -08001524}
1525
1526static void
1527vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1528{
Florin Coras243edd52020-03-04 22:20:12 +00001529 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1530
Florin Coras0ef8ef22019-01-18 08:37:13 -08001531 /* Unshare sessions and also cleanup worker since child may have
1532 * called _exit () and therefore vcl may not catch the event */
1533 vls_unshare_vcl_worker_sessions (wrk);
wanghanlinb940fd42021-06-29 16:01:55 +08001534
1535 /* Since child may have exited and thereforce fd of vpp_app_socket_api
1536 * may have been closed, so DONOT notify VPP.
1537 */
1538 vcl_worker_cleanup (wrk, vcm->cfg.vpp_app_socket_api ? 0 : 1);
Florin Coras243edd52020-03-04 22:20:12 +00001539
1540 vls_worker_free (vls_wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001541}
1542
1543static void
Florin Corasf9240dc2019-01-15 08:03:17 -08001544vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1545{
1546 vcl_worker_t *sub_child;
1547 int tries = 0;
1548
1549 if (child_wrk->forked_child != ~0)
1550 {
1551 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1552 if (sub_child)
1553 {
1554 /* Wait a bit, maybe the process is going away */
1555 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1556 usleep (1e3);
1557 if (kill (sub_child->current_pid, 0) < 0)
1558 vls_cleanup_forked_child (child_wrk, sub_child);
1559 }
1560 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001561 vls_cleanup_vcl_worker (child_wrk);
1562 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001563 wrk->forked_child = ~0;
1564}
1565
wanghanlind72a0342021-05-12 17:00:29 +08001566static void
1567vls_handle_pending_wrk_cleanup (void)
1568{
1569 u32 *wip;
1570 vcl_worker_t *child_wrk, *wrk;
1571 vls_worker_t *vls_wrk = vls_worker_get_current ();
1572
1573 if (PREDICT_TRUE (vec_len (vls_wrk->pending_wrk_cleanup) == 0))
1574 return;
1575
1576 wrk = vcl_worker_get_current ();
1577 vec_foreach (wip, vls_wrk->pending_wrk_cleanup)
1578 {
1579 child_wrk = vcl_worker_get_if_valid (*wip);
1580 if (!child_wrk)
1581 continue;
1582 vls_cleanup_forked_child (wrk, child_wrk);
1583 }
1584 vec_reset_length (vls_wrk->pending_wrk_cleanup);
1585}
1586
Florin Corasf9240dc2019-01-15 08:03:17 -08001587static struct sigaction old_sa;
1588
1589static void
1590vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1591{
1592 vcl_worker_t *wrk, *child_wrk;
wanghanlind72a0342021-05-12 17:00:29 +08001593 vls_worker_t *vls_wrk;
Florin Corasf9240dc2019-01-15 08:03:17 -08001594
1595 if (vcl_get_worker_index () == ~0)
1596 return;
1597
1598 if (sigaction (SIGCHLD, &old_sa, 0))
1599 {
1600 VERR ("couldn't restore sigchld");
1601 exit (-1);
1602 }
1603
1604 wrk = vcl_worker_get_current ();
1605 if (wrk->forked_child == ~0)
1606 return;
1607
1608 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1609 if (!child_wrk)
1610 goto done;
1611
1612 if (si && si->si_pid != child_wrk->current_pid)
1613 {
1614 VDBG (0, "unexpected child pid %u", si->si_pid);
1615 goto done;
1616 }
wanghanlind72a0342021-05-12 17:00:29 +08001617
1618 /* Parent process may enter sighandler with a lock, such as lock in localtime
1619 * or in mspace_free, and child wrk cleanup may try to get such locks and
1620 * cause deadlock.
1621 * So move child wrk cleanup from sighandler to vls_epoll_wait/vls_select.
1622 */
1623 vls_wrk = vls_worker_get_current ();
1624 vec_add1 (vls_wrk->pending_wrk_cleanup, child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001625
1626done:
1627 if (old_sa.sa_flags & SA_SIGINFO)
1628 {
1629 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1630 fn (signum, si, uc);
1631 }
1632 else
1633 {
1634 void (*fn) (int) = old_sa.sa_handler;
1635 if (fn)
1636 fn (signum);
1637 }
1638}
1639
1640static void
1641vls_incercept_sigchld ()
1642{
1643 struct sigaction sa;
nandfan5fdc47c2021-02-22 17:17:17 +08001644 if (old_sa.sa_sigaction)
1645 {
1646 VDBG (0, "have intercepted sigchld");
1647 return;
1648 }
Florin Corasf9240dc2019-01-15 08:03:17 -08001649 clib_memset (&sa, 0, sizeof (sa));
1650 sa.sa_sigaction = vls_intercept_sigchld_handler;
1651 sa.sa_flags = SA_SIGINFO;
1652 if (sigaction (SIGCHLD, &sa, &old_sa))
1653 {
1654 VERR ("couldn't intercept sigchld");
1655 exit (-1);
1656 }
1657}
1658
1659static void
1660vls_app_pre_fork (void)
1661{
1662 vls_incercept_sigchld ();
1663 vcl_flush_mq_events ();
1664}
1665
1666static void
1667vls_app_fork_child_handler (void)
1668{
1669 vcl_worker_t *parent_wrk;
Florin Corasb88de902020-09-08 16:47:57 -07001670 int parent_wrk_index;
Florin Corasf9240dc2019-01-15 08:03:17 -08001671
1672 parent_wrk_index = vcl_get_worker_index ();
1673 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1674 parent_wrk_index);
1675
1676 /*
Florin Corasb88de902020-09-08 16:47:57 -07001677 * Clear old state
Florin Corasf9240dc2019-01-15 08:03:17 -08001678 */
1679 vcl_set_worker_index (~0);
Florin Corasf9240dc2019-01-15 08:03:17 -08001680
1681 /*
Florin Corasb88de902020-09-08 16:47:57 -07001682 * Allocate and register vcl worker with vpp
Florin Corasf9240dc2019-01-15 08:03:17 -08001683 */
Florin Corasb88de902020-09-08 16:47:57 -07001684 if (vppcom_worker_register ())
Florin Corasf9240dc2019-01-15 08:03:17 -08001685 {
Florin Corasb88de902020-09-08 16:47:57 -07001686 VERR ("couldn't register new worker!");
Florin Corasf9240dc2019-01-15 08:03:17 -08001687 return;
1688 }
1689
1690 /*
Florin Corasb88de902020-09-08 16:47:57 -07001691 * Allocate/initialize vls worker and share sessions
Florin Coras243edd52020-03-04 22:20:12 +00001692 */
1693 vls_worker_alloc ();
Florin Corasf9240dc2019-01-15 08:03:17 -08001694 parent_wrk = vcl_worker_get (parent_wrk_index);
1695 vls_worker_copy_on_fork (parent_wrk);
1696 parent_wrk->forked_child = vcl_get_worker_index ();
1697
Florin Coras0ef8ef22019-01-18 08:37:13 -08001698 /* Reset number of threads and set wrk index */
Florin Coras2d675d72019-01-28 15:54:27 -08001699 vlsl->vls_mt_n_threads = 0;
1700 vlsl->vls_wrk_index = vcl_get_worker_index ();
1701 vlsl->select_mp_check = 0;
Florin Coras2d675d72019-01-28 15:54:27 -08001702 vls_mt_locks_init ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001703
Florin Corasf9240dc2019-01-15 08:03:17 -08001704 VDBG (0, "forked child main worker initialized");
1705 vcm->forking = 0;
1706}
1707
1708static void
1709vls_app_fork_parent_handler (void)
1710{
1711 vcm->forking = 1;
1712 while (vcm->forking)
1713 ;
1714}
1715
Florin Coras0ef8ef22019-01-18 08:37:13 -08001716void
1717vls_app_exit (void)
1718{
Florin Coras243edd52020-03-04 22:20:12 +00001719 vls_worker_t *wrk = vls_worker_get_current ();
1720
wanghanlind72a0342021-05-12 17:00:29 +08001721 /* Handle pending wrk cleanup */
1722 vls_handle_pending_wrk_cleanup ();
1723
Florin Coras0ef8ef22019-01-18 08:37:13 -08001724 /* Unshare the sessions. VCL will clean up the worker */
1725 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
Florin Coras243edd52020-03-04 22:20:12 +00001726 vls_worker_free (wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001727}
1728
Florin Coras40c07ce2020-07-16 20:46:17 -07001729static void
1730vls_clone_and_share_rpc_handler (void *args)
1731{
1732 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1733 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1734 vcl_locked_session_t *vls, *dst_vls;
hanlina3a48962020-07-13 11:09:15 +08001735 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
Florin Coras40c07ce2020-07-16 20:46:17 -07001736 vcl_session_t *s, *dst_s;
1737
wanghanlindcacdc42020-12-28 16:19:05 +08001738 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1739 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1740 msg->origin_session_index);
1741
1742 /* VCL locked session can't been protected, so DONT touch it.
1743 * VCL session may been free, check it.
1744 */
1745 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1746 s = vcl_session_get (vcl_wrk, msg->session_index);
1747 if (PREDICT_FALSE (!s))
1748 {
1749 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1750 return;
1751 }
Florin Coras40c07ce2020-07-16 20:46:17 -07001752
hanlina3a48962020-07-13 11:09:15 +08001753 if (!vls_mt_wrk_supported ())
wanghanlindcacdc42020-12-28 16:19:05 +08001754 {
1755 vls = vls_session_get (wrk, msg->vls_index);
1756 vls_init_share_session (wrk, vls);
1757 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1758 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1759 dst_vls->shared_data_index = vls->shared_data_index;
1760 }
hanlina3a48962020-07-13 11:09:15 +08001761 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
Florin Coras40c07ce2020-07-16 20:46:17 -07001762 clib_memcpy (dst_s, s, sizeof (*s));
1763
wanghanlindcacdc42020-12-28 16:19:05 +08001764 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
hanlina3a48962020-07-13 11:09:15 +08001765}
1766
1767static void
1768vls_session_cleanup_rpc_handler (void *args)
1769{
1770 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1771 vcl_worker_t *wrk = vcl_worker_get_current ();
wanghanline8f848a2021-01-08 14:57:11 +08001772 vls_worker_t *vls_wrk = vls_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001773 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
hanlina3a48962020-07-13 11:09:15 +08001774
wanghanlindcacdc42020-12-28 16:19:05 +08001775 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1776 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
hanlina3a48962020-07-13 11:09:15 +08001777
wanghanlindcacdc42020-12-28 16:19:05 +08001778 vppcom_session_close (sh);
wanghanline8f848a2021-01-08 14:57:11 +08001779 vls_sh_to_vlsh_table_del (vls_wrk, sh);
Florin Coras40c07ce2020-07-16 20:46:17 -07001780}
1781
1782static void
1783vls_rpc_handler (void *args)
1784{
1785 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1786 switch (msg->type)
1787 {
1788 case VLS_RPC_CLONE_AND_SHARE:
1789 vls_clone_and_share_rpc_handler (msg->data);
1790 break;
hanlina3a48962020-07-13 11:09:15 +08001791 case VLS_RPC_SESS_CLEANUP:
1792 vls_session_cleanup_rpc_handler (msg->data);
1793 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001794 default:
1795 break;
1796 }
1797}
1798
1799void
wanghanlindcacdc42020-12-28 16:19:05 +08001800vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1801 u32 session_index, u32 vls_wrk_index,
1802 u32 dst_wrk_index, u32 dst_vls_index,
1803 u32 dst_session_index)
Florin Coras40c07ce2020-07-16 20:46:17 -07001804{
1805 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1806 vls_clone_and_share_msg_t *msg;
1807 vls_rpc_msg_t *rpc;
hanlina3a48962020-07-13 11:09:15 +08001808 int ret;
wanghanlindcacdc42020-12-28 16:19:05 +08001809 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
Florin Coras40c07ce2020-07-16 20:46:17 -07001810
1811 rpc = (vls_rpc_msg_t *) & data;
1812 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1813 msg = (vls_clone_and_share_msg_t *) & rpc->data;
hanlina3a48962020-07-13 11:09:15 +08001814 msg->origin_vls_wrk = vls_wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001815 msg->origin_vls_index = origin_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001816 msg->origin_vcl_wrk = wrk->wrk_index;
1817 msg->origin_session_index = session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001818 msg->vls_index = dst_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001819 msg->session_index = dst_session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001820
wanghanlindcacdc42020-12-28 16:19:05 +08001821 /* Try lock and handle rpcs if two threads send each other
1822 * clone requests at the same time.
1823 */
1824 wrk->rpc_done = VLS_RPC_STATE_INIT;
1825 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1826 vcl_flush_mq_events ();
hanlina3a48962020-07-13 11:09:15 +08001827 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1828
Florin Corasff40d8f2020-08-11 22:05:28 -07001829 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001830 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1831 msg->origin_session_index, ret);
wanghanlindcacdc42020-12-28 16:19:05 +08001832 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1833 clib_time_now (&wrk->clib_time) < timeout)
hanlina3a48962020-07-13 11:09:15 +08001834 ;
wanghanlindcacdc42020-12-28 16:19:05 +08001835 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
hanlina3a48962020-07-13 11:09:15 +08001836}
1837
1838void
1839vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1840 u32 dst_wrk_index, u32 dst_session_index)
1841{
1842 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1843 vls_sess_cleanup_msg_t *msg;
1844 vls_rpc_msg_t *rpc;
1845 int ret;
1846
1847 rpc = (vls_rpc_msg_t *) & data;
1848 rpc->type = VLS_RPC_SESS_CLEANUP;
1849 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1850 msg->origin_vcl_wrk = wrk->wrk_index;
1851 msg->session_index = dst_session_index;
1852
hanlina3a48962020-07-13 11:09:15 +08001853 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1854
Florin Corasff40d8f2020-08-11 22:05:28 -07001855 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001856 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
Florin Coras40c07ce2020-07-16 20:46:17 -07001857}
1858
Florin Coras7baeb712019-01-04 17:05:43 -08001859int
1860vls_app_create (char *app_name)
1861{
1862 int rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001863
Florin Coras7baeb712019-01-04 17:05:43 -08001864 if ((rv = vppcom_app_create (app_name)))
1865 return rv;
Florin Coras243edd52020-03-04 22:20:12 +00001866
Florin Coras2d675d72019-01-28 15:54:27 -08001867 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1868 clib_memset (vlsm, 0, sizeof (*vlsm));
Florin Coras7baeb712019-01-04 17:05:43 -08001869 clib_rwlock_init (&vlsm->vls_table_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001870 clib_rwlock_init (&vlsm->shared_data_lock);
wanghanlindcacdc42020-12-28 16:19:05 +08001871 clib_spinlock_init (&vlsm->worker_rpc_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001872 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1873
Florin Corasf9240dc2019-01-15 08:03:17 -08001874 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1875 vls_app_fork_child_handler);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001876 atexit (vls_app_exit);
Florin Coras243edd52020-03-04 22:20:12 +00001877 vls_worker_alloc ();
Florin Coras2d675d72019-01-28 15:54:27 -08001878 vlsl->vls_wrk_index = vcl_get_worker_index ();
1879 vls_mt_locks_init ();
Florin Coras40c07ce2020-07-16 20:46:17 -07001880 vcm->wrk_rpc_fn = vls_rpc_handler;
Florin Coras7baeb712019-01-04 17:05:43 -08001881 return VPPCOM_OK;
1882}
1883
hanlin4266d4d2020-05-19 17:34:17 +08001884unsigned char
1885vls_use_eventfd (void)
1886{
1887 return vcm->cfg.use_mq_eventfd;
1888}
1889
hanlina3a48962020-07-13 11:09:15 +08001890unsigned char
1891vls_mt_wrk_supported (void)
1892{
Florin Corasff40d8f2020-08-11 22:05:28 -07001893 return vcm->cfg.mt_wrk_supported;
hanlina3a48962020-07-13 11:09:15 +08001894}
1895
1896int
1897vls_use_real_epoll (void)
1898{
1899 if (vcl_get_worker_index () == ~0)
1900 return 0;
1901
1902 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1903}
1904
1905void
1906vls_register_vcl_worker (void)
1907{
wanghanlin492350e2020-09-11 17:19:32 +08001908 vls_mt_add ();
hanlina3a48962020-07-13 11:09:15 +08001909}
1910
Florin Coras7baeb712019-01-04 17:05:43 -08001911/*
1912 * fd.io coding-style-patch-verification: ON
1913 *
1914 * Local Variables:
1915 * eval: (c-set-style "gnu")
1916 * End:
1917 */