blob: 5011461596b52e7fdb3f43f55886521849209d15 [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{
41 vcl_locked_session_t *vls_pool;
hanlinf8e13632020-08-21 11:05:36 +080042 uword *session_handle_to_vlsh_table;
Florin Coras243edd52020-03-04 22:20:12 +000043 u32 wrk_index;
44} vls_worker_t;
45
Florin Coras2d675d72019-01-28 15:54:27 -080046typedef struct vls_local_
47{
48 int vls_wrk_index;
49 volatile int vls_mt_n_threads;
50 pthread_mutex_t vls_mt_mq_mlock;
51 pthread_mutex_t vls_mt_spool_mlock;
52 volatile u8 select_mp_check;
53 volatile u8 epoll_mp_check;
54} vls_process_local_t;
55
56static vls_process_local_t vls_local;
57static vls_process_local_t *vlsl = &vls_local;
58
59typedef struct vls_main_
Florin Coras7baeb712019-01-04 17:05:43 -080060{
Florin Coras243edd52020-03-04 22:20:12 +000061 vls_worker_t *workers;
Florin Coras7baeb712019-01-04 17:05:43 -080062 clib_rwlock_t vls_table_lock;
Florin Coras243edd52020-03-04 22:20:12 +000063 /** Pool of data shared by sessions owned by different workers */
64 vls_shared_data_t *shared_data_pool;
65 clib_rwlock_t shared_data_lock;
wanghanlindcacdc42020-12-28 16:19:05 +080066 /** Lock to protect rpc among workers */
67 clib_spinlock_t worker_rpc_lock;
Florin Coras7baeb712019-01-04 17:05:43 -080068} vls_main_t;
69
Florin Coras2d675d72019-01-28 15:54:27 -080070vls_main_t *vlsm;
Florin Coras7baeb712019-01-04 17:05:43 -080071
wanghanlindcacdc42020-12-28 16:19:05 +080072typedef enum
73{
74 VLS_RPC_STATE_INIT,
75 VLS_RPC_STATE_SUCCESS,
76 VLS_RPC_STATE_SESSION_NOT_EXIST,
77} vls_rpc_state_e;
78
Florin Coras40c07ce2020-07-16 20:46:17 -070079typedef enum vls_rpc_msg_type_
80{
81 VLS_RPC_CLONE_AND_SHARE,
hanlina3a48962020-07-13 11:09:15 +080082 VLS_RPC_SESS_CLEANUP,
Florin Coras40c07ce2020-07-16 20:46:17 -070083} vls_rpc_msg_type_e;
84
85typedef struct vls_rpc_msg_
86{
87 u8 type;
88 u8 data[0];
89} vls_rpc_msg_t;
90
91typedef struct vls_clone_and_share_msg_
92{
93 u32 vls_index; /**< vls to be shared */
hanlina3a48962020-07-13 11:09:15 +080094 u32 session_index; /**< vcl session to be shared */
95 u32 origin_vls_wrk; /**< vls worker that initiated the rpc */
Florin Coras40c07ce2020-07-16 20:46:17 -070096 u32 origin_vls_index; /**< vls session of the originator */
hanlina3a48962020-07-13 11:09:15 +080097 u32 origin_vcl_wrk; /**< vcl worker that initiated the rpc */
98 u32 origin_session_index; /**< vcl session of the originator */
Florin Coras40c07ce2020-07-16 20:46:17 -070099} vls_clone_and_share_msg_t;
100
hanlina3a48962020-07-13 11:09:15 +0800101typedef struct vls_sess_cleanup_msg_
102{
103 u32 session_index; /**< vcl session to be cleaned */
104 u32 origin_vcl_wrk; /**< worker that initiated the rpc */
105} vls_sess_cleanup_msg_t;
106
107void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
108 u32 dst_wrk_index, u32 dst_session_index);
wanghanlindcacdc42020-12-28 16:19:05 +0800109void vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
hanlina3a48962020-07-13 11:09:15 +0800110 u32 session_index, u32 vls_wrk_index,
111 u32 dst_wrk_index, u32 dst_vls_index,
112 u32 dst_session_index);
113
Florin Coras243edd52020-03-04 22:20:12 +0000114static inline u32
115vls_get_worker_index (void)
116{
hanlina3a48962020-07-13 11:09:15 +0800117 if (vls_mt_wrk_supported ())
118 return vlsl->vls_wrk_index;
119 else
120 return vcl_get_worker_index ();
Florin Coras243edd52020-03-04 22:20:12 +0000121}
122
123static u32
124vls_shared_data_alloc (void)
125{
126 vls_shared_data_t *vls_shd;
127 u32 shd_index;
128
129 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
130 pool_get_zero (vlsm->shared_data_pool, vls_shd);
131 clib_spinlock_init (&vls_shd->lock);
132 shd_index = vls_shd - vlsm->shared_data_pool;
133 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
134
135 return shd_index;
136}
137
138static u32
139vls_shared_data_index (vls_shared_data_t * vls_shd)
140{
141 return vls_shd - vlsm->shared_data_pool;
142}
143
144vls_shared_data_t *
145vls_shared_data_get (u32 shd_index)
146{
147 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
148 return 0;
149 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
150}
151
152static void
153vls_shared_data_free (u32 shd_index)
154{
155 vls_shared_data_t *vls_shd;
156
157 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
158 vls_shd = vls_shared_data_get (shd_index);
159 clib_spinlock_free (&vls_shd->lock);
160 clib_bitmap_free (vls_shd->listeners);
161 vec_free (vls_shd->workers_subscribed);
162 pool_put (vlsm->shared_data_pool, vls_shd);
163 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
164}
165
166static inline void
167vls_shared_data_pool_rlock (void)
168{
169 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
170}
171
172static inline void
173vls_shared_data_pool_runlock (void)
174{
175 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
176}
177
Florin Coras7baeb712019-01-04 17:05:43 -0800178static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700179vls_mt_table_rlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800180{
Florin Coras243edd52020-03-04 22:20:12 +0000181 if (vlsl->vls_mt_n_threads > 1)
182 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800183}
184
185static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700186vls_mt_table_runlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800187{
Florin Coras243edd52020-03-04 22:20:12 +0000188 if (vlsl->vls_mt_n_threads > 1)
189 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800190}
191
192static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700193vls_mt_table_wlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800194{
Florin Coras243edd52020-03-04 22:20:12 +0000195 if (vlsl->vls_mt_n_threads > 1)
196 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800197}
198
199static inline void
Florin Corasff40d8f2020-08-11 22:05:28 -0700200vls_mt_table_wunlock (void)
Florin Coras7baeb712019-01-04 17:05:43 -0800201{
Florin Coras243edd52020-03-04 22:20:12 +0000202 if (vlsl->vls_mt_n_threads > 1)
203 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
Florin Coras7baeb712019-01-04 17:05:43 -0800204}
205
Florin Coras0ef8ef22019-01-18 08:37:13 -0800206typedef enum
207{
208 VLS_MT_OP_READ,
209 VLS_MT_OP_WRITE,
210 VLS_MT_OP_SPOOL,
211 VLS_MT_OP_XPOLL,
212} vls_mt_ops_t;
213
214typedef enum
215{
216 VLS_MT_LOCK_MQ = 1 << 0,
217 VLS_MT_LOCK_SPOOL = 1 << 1
218} vls_mt_lock_type_t;
219
Florin Coras0ef8ef22019-01-18 08:37:13 -0800220static void
221vls_mt_add (void)
222{
Florin Coras2d675d72019-01-28 15:54:27 -0800223 vlsl->vls_mt_n_threads += 1;
Florin Corasff40d8f2020-08-11 22:05:28 -0700224
225 /* If multi-thread workers are supported, for each new thread register a new
226 * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
227 * update the vcl worker's thread local worker index variable */
hanlina3a48962020-07-13 11:09:15 +0800228 if (vls_mt_wrk_supported ())
wanghanlin492350e2020-09-11 17:19:32 +0800229 {
230 if (vppcom_worker_register () != VPPCOM_OK)
231 VERR ("failed to register worker");
232 }
hanlina3a48962020-07-13 11:09:15 +0800233 else
234 vcl_set_worker_index (vlsl->vls_wrk_index);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800235}
236
237static inline void
238vls_mt_mq_lock (void)
239{
Florin Coras2d675d72019-01-28 15:54:27 -0800240 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800241}
242
243static inline void
244vls_mt_mq_unlock (void)
245{
Florin Coras2d675d72019-01-28 15:54:27 -0800246 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800247}
248
249static inline void
250vls_mt_spool_lock (void)
251{
Florin Coras2d675d72019-01-28 15:54:27 -0800252 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800253}
254
255static inline void
256vls_mt_create_unlock (void)
257{
Florin Coras2d675d72019-01-28 15:54:27 -0800258 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
259}
260
261static void
262vls_mt_locks_init (void)
263{
264 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
265 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800266}
267
Florin Coras243edd52020-03-04 22:20:12 +0000268u8
269vls_is_shared (vcl_locked_session_t * vls)
270{
271 return (vls->shared_data_index != ~0);
272}
273
274static inline void
275vls_lock (vcl_locked_session_t * vls)
276{
277 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
278 clib_spinlock_lock (&vls->lock);
279}
280
281static inline void
282vls_unlock (vcl_locked_session_t * vls)
283{
284 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
285 clib_spinlock_unlock (&vls->lock);
286}
287
Florin Coras7baeb712019-01-04 17:05:43 -0800288static inline vcl_session_handle_t
289vls_to_sh (vcl_locked_session_t * vls)
290{
Florin Corasf9240dc2019-01-15 08:03:17 -0800291 return vcl_session_handle_from_index (vls->session_index);
Florin Coras7baeb712019-01-04 17:05:43 -0800292}
293
294static inline vcl_session_handle_t
295vls_to_sh_tu (vcl_locked_session_t * vls)
296{
297 vcl_session_handle_t sh;
298 sh = vls_to_sh (vls);
Florin Corasff40d8f2020-08-11 22:05:28 -0700299 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800300 return sh;
301}
302
Florin Coras243edd52020-03-04 22:20:12 +0000303static vls_worker_t *
304vls_worker_get_current (void)
305{
306 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
307}
308
309static void
310vls_worker_alloc (void)
311{
312 vls_worker_t *wrk;
313
314 pool_get_zero (vlsm->workers, wrk);
315 wrk->wrk_index = vcl_get_worker_index ();
316}
317
318static void
319vls_worker_free (vls_worker_t * wrk)
320{
hanlinf8e13632020-08-21 11:05:36 +0800321 hash_free (wrk->session_handle_to_vlsh_table);
Florin Coras243edd52020-03-04 22:20:12 +0000322 pool_free (wrk->vls_pool);
323 pool_put (vlsm->workers, wrk);
324}
325
326static vls_worker_t *
327vls_worker_get (u32 wrk_index)
328{
329 if (pool_is_free_index (vlsm->workers, wrk_index))
330 return 0;
331 return pool_elt_at_index (vlsm->workers, wrk_index);
332}
333
Florin Coras7baeb712019-01-04 17:05:43 -0800334static vls_handle_t
335vls_alloc (vcl_session_handle_t sh)
336{
Florin Coras243edd52020-03-04 22:20:12 +0000337 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800338 vcl_locked_session_t *vls;
339
Florin Corasff40d8f2020-08-11 22:05:28 -0700340 vls_mt_table_wlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000341
342 pool_get_zero (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800343 vls->session_index = vppcom_session_index (sh);
344 vls->worker_index = vppcom_session_worker (sh);
Florin Coras243edd52020-03-04 22:20:12 +0000345 vls->vls_index = vls - wrk->vls_pool;
346 vls->shared_data_index = ~0;
hanlinf8e13632020-08-21 11:05:36 +0800347 hash_set (wrk->session_handle_to_vlsh_table, sh, vls->vls_index);
hanlina3a48962020-07-13 11:09:15 +0800348 if (vls_mt_wrk_supported ())
349 {
350 hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
351 vls->session_index);
352 vls->owner_vcl_wrk_index = vls->worker_index;
353 }
Florin Coras7baeb712019-01-04 17:05:43 -0800354 clib_spinlock_init (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000355
Florin Corasff40d8f2020-08-11 22:05:28 -0700356 vls_mt_table_wunlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800357 return vls->vls_index;
358}
359
360static vcl_locked_session_t *
361vls_get (vls_handle_t vlsh)
362{
Florin Coras243edd52020-03-04 22:20:12 +0000363 vls_worker_t *wrk = vls_worker_get_current ();
364 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800365 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000366 return pool_elt_at_index (wrk->vls_pool, vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -0800367}
368
369static void
Florin Coras0ef8ef22019-01-18 08:37:13 -0800370vls_free (vcl_locked_session_t * vls)
Florin Coras7baeb712019-01-04 17:05:43 -0800371{
Florin Coras243edd52020-03-04 22:20:12 +0000372 vls_worker_t *wrk = vls_worker_get_current ();
373
Florin Coras0ef8ef22019-01-18 08:37:13 -0800374 ASSERT (vls != 0);
hanlinf8e13632020-08-21 11:05:36 +0800375 hash_unset (wrk->session_handle_to_vlsh_table,
376 vcl_session_handle_from_index (vls->session_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -0800377 clib_spinlock_free (&vls->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000378 pool_put (wrk->vls_pool, vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800379}
380
381static vcl_locked_session_t *
382vls_get_and_lock (vls_handle_t vlsh)
383{
Florin Coras243edd52020-03-04 22:20:12 +0000384 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -0800385 vcl_locked_session_t *vls;
Florin Coras243edd52020-03-04 22:20:12 +0000386 if (pool_is_free_index (wrk->vls_pool, vlsh))
Florin Coras7baeb712019-01-04 17:05:43 -0800387 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000388 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
389 vls_lock (vls);
Florin Coras7baeb712019-01-04 17:05:43 -0800390 return vls;
391}
392
393static vcl_locked_session_t *
394vls_get_w_dlock (vls_handle_t vlsh)
395{
396 vcl_locked_session_t *vls;
Florin Corasff40d8f2020-08-11 22:05:28 -0700397 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800398 vls = vls_get_and_lock (vlsh);
399 if (!vls)
Florin Corasff40d8f2020-08-11 22:05:28 -0700400 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800401 return vls;
402}
403
404static inline void
Florin Coras7baeb712019-01-04 17:05:43 -0800405vls_get_and_unlock (vls_handle_t vlsh)
406{
407 vcl_locked_session_t *vls;
Florin Corasff40d8f2020-08-11 22:05:28 -0700408 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800409 vls = vls_get (vlsh);
410 vls_unlock (vls);
Florin Corasff40d8f2020-08-11 22:05:28 -0700411 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800412}
413
414static inline void
415vls_dunlock (vcl_locked_session_t * vls)
416{
417 vls_unlock (vls);
Florin Corasff40d8f2020-08-11 22:05:28 -0700418 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -0800419}
420
Florin Coras243edd52020-03-04 22:20:12 +0000421static vcl_locked_session_t *
422vls_session_get (vls_worker_t * wrk, u32 vls_index)
423{
424 if (pool_is_free_index (wrk->vls_pool, vls_index))
425 return 0;
426 return pool_elt_at_index (wrk->vls_pool, vls_index);
427}
428
Florin Coras2d675d72019-01-28 15:54:27 -0800429vcl_session_handle_t
430vlsh_to_sh (vls_handle_t vlsh)
431{
432 vcl_locked_session_t *vls;
433 int rv;
434
435 vls = vls_get_w_dlock (vlsh);
436 if (!vls)
437 return INVALID_SESSION_ID;
438 rv = vls_to_sh (vls);
439 vls_dunlock (vls);
440 return rv;
441}
442
443vcl_session_handle_t
444vlsh_to_session_index (vls_handle_t vlsh)
445{
446 vcl_session_handle_t sh;
447 sh = vlsh_to_sh (vlsh);
448 return vppcom_session_index (sh);
449}
450
451vls_handle_t
hanlinf8e13632020-08-21 11:05:36 +0800452vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
Florin Coras2d675d72019-01-28 15:54:27 -0800453{
Florin Coras243edd52020-03-04 22:20:12 +0000454 vls_worker_t *wrk = vls_worker_get_current ();
Florin Coras2d675d72019-01-28 15:54:27 -0800455 uword *vlshp;
hanlinf8e13632020-08-21 11:05:36 +0800456 vlshp = hash_get (wrk->session_handle_to_vlsh_table,
457 vcl_session_handle_from_wrk_session_index (session_index,
458 vcl_wrk_index));
Florin Coras2d675d72019-01-28 15:54:27 -0800459 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
460}
461
462vls_handle_t
463vls_session_index_to_vlsh (uint32_t session_index)
464{
465 vls_handle_t vlsh;
466
Florin Corasff40d8f2020-08-11 22:05:28 -0700467 vls_mt_table_rlock ();
hanlinf8e13632020-08-21 11:05:36 +0800468 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
Florin Corasff40d8f2020-08-11 22:05:28 -0700469 vls_mt_table_runlock ();
Florin Coras2d675d72019-01-28 15:54:27 -0800470
471 return vlsh;
472}
473
Florin Corasf9240dc2019-01-15 08:03:17 -0800474u8
Florin Coras0ef8ef22019-01-18 08:37:13 -0800475vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
Florin Corasf9240dc2019-01-15 08:03:17 -0800476{
Florin Coras243edd52020-03-04 22:20:12 +0000477 vls_shared_data_t *vls_shd;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800478 int i;
Florin Coras243edd52020-03-04 22:20:12 +0000479
480 if (vls->shared_data_index == ~0)
481 return 0;
482
483 vls_shared_data_pool_rlock ();
484
485 vls_shd = vls_shared_data_get (vls->shared_data_index);
486 clib_spinlock_lock (&vls_shd->lock);
487
488 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
489 if (vls_shd->workers_subscribed[i] == wrk_index)
490 {
491 clib_spinlock_unlock (&vls_shd->lock);
492 vls_shared_data_pool_runlock ();
493 return 1;
494 }
495 clib_spinlock_unlock (&vls_shd->lock);
496
497 vls_shared_data_pool_runlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -0800498 return 0;
499}
500
Florin Coras2d675d72019-01-28 15:54:27 -0800501static void
502vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
503{
Florin Coras243edd52020-03-04 22:20:12 +0000504 vls_shared_data_t *vls_shd;
505
506 if (vls->shared_data_index == ~0)
507 {
508 clib_warning ("not a shared session");
509 return;
510 }
511
512 vls_shared_data_pool_rlock ();
513
514 vls_shd = vls_shared_data_get (vls->shared_data_index);
515
516 clib_spinlock_lock (&vls_shd->lock);
517 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
518 clib_spinlock_unlock (&vls_shd->lock);
519
520 vls_shared_data_pool_runlock ();
521}
522
523static u32
524vls_shared_get_owner (vcl_locked_session_t * vls)
525{
526 vls_shared_data_t *vls_shd;
527 u32 owner_wrk;
528
529 vls_shared_data_pool_rlock ();
530
531 vls_shd = vls_shared_data_get (vls->shared_data_index);
532 owner_wrk = vls_shd->owner_wrk_index;
533
534 vls_shared_data_pool_runlock ();
535
536 return owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -0800537}
538
539static u8
540vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
541{
Florin Coras243edd52020-03-04 22:20:12 +0000542 vls_shared_data_t *vls_shd;
543 u8 is_set;
544
545 if (vls->shared_data_index == ~0)
546 {
547 clib_warning ("not a shared session");
548 return 0;
549 }
550
551 vls_shared_data_pool_rlock ();
552
553 vls_shd = vls_shared_data_get (vls->shared_data_index);
554
555 clib_spinlock_lock (&vls_shd->lock);
556 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
557 clib_spinlock_unlock (&vls_shd->lock);
558
559 vls_shared_data_pool_runlock ();
560
561 return (is_set == 1);
Florin Coras2d675d72019-01-28 15:54:27 -0800562}
563
564static void
565vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
566{
567 vppcom_session_listen (vls_to_sh (vls), ~0);
568 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
569}
570
571static void
572vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
573{
574 vcl_worker_t *wrk;
575 vcl_session_t *s;
576
577 wrk = vcl_worker_get (wrk_index);
578 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700579 if (s->session_state != VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800580 return;
Florin Coras458089b2019-08-21 16:20:44 -0700581 vcl_send_session_unlisten (wrk, s);
Florin Corasc127d5a2020-10-14 16:35:58 -0700582 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800583 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
584}
585
Florin Coras243edd52020-03-04 22:20:12 +0000586static int
587vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
588 u32 wrk_index)
589{
590 int i;
591
592 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
593 {
594 if (vls_shd->workers_subscribed[i] == wrk_index)
595 return i;
596 }
597 return -1;
598}
599
Florin Coras0ef8ef22019-01-18 08:37:13 -0800600int
601vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
602{
Florin Coras243edd52020-03-04 22:20:12 +0000603 vls_shared_data_t *vls_shd;
Florin Coras311817f2020-03-07 17:45:47 +0000604 int do_disconnect, pos;
605 u32 n_subscribers;
Florin Corasf9240dc2019-01-15 08:03:17 -0800606 vcl_session_t *s;
Florin Coras2d675d72019-01-28 15:54:27 -0800607
hanlina3a48962020-07-13 11:09:15 +0800608 if (vls->shared_data_index == ~0)
609 return 0;
Florin Coras243edd52020-03-04 22:20:12 +0000610
Florin Coras2d675d72019-01-28 15:54:27 -0800611 s = vcl_session_get (wrk, vls->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700612 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800613 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
Florin Corasf9240dc2019-01-15 08:03:17 -0800614
Florin Coras243edd52020-03-04 22:20:12 +0000615 vls_shared_data_pool_rlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800616
Florin Coras243edd52020-03-04 22:20:12 +0000617 vls_shd = vls_shared_data_get (vls->shared_data_index);
618 clib_spinlock_lock (&vls_shd->lock);
619
620 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
621 if (pos < 0)
622 {
623 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
624 vls->worker_index);
625 goto done;
626 }
627
628 /*
629 * Unsubscribe from share data and fifos
630 */
631 if (s->rx_fifo)
632 {
633 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
634 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
635 }
636 vec_del1 (vls_shd->workers_subscribed, pos);
637
638 /*
639 * Cleanup vcl state
640 */
641 n_subscribers = vec_len (vls_shd->workers_subscribed);
Florin Corasc127d5a2020-10-14 16:35:58 -0700642 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
Florin Coras243edd52020-03-04 22:20:12 +0000643 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
644
645 /*
646 * No subscriber left, cleanup shared data
647 */
648 if (!n_subscribers)
649 {
650 u32 shd_index = vls_shared_data_index (vls_shd);
651
652 clib_spinlock_unlock (&vls_shd->lock);
653 vls_shared_data_pool_runlock ();
654
655 vls_shared_data_free (shd_index);
656
657 /* All locks have been dropped */
Florin Corasf9240dc2019-01-15 08:03:17 -0800658 return 0;
659 }
660
Florin Coras0ef8ef22019-01-18 08:37:13 -0800661 /* Return, if this is not the owning worker */
Florin Coras243edd52020-03-04 22:20:12 +0000662 if (vls_shd->owner_wrk_index != wrk->wrk_index)
663 goto done;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800664
Florin Coras243edd52020-03-04 22:20:12 +0000665 ASSERT (vec_len (vls_shd->workers_subscribed));
666
667 /*
668 * Check if we can change owner or close
669 */
670 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
671 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
672
673 /* XXX is this still needed? */
674 if (vec_len (vls_shd->workers_subscribed) > 1)
675 clib_warning ("more workers need to be updated");
676
677done:
678
679 clib_spinlock_unlock (&vls_shd->lock);
680 vls_shared_data_pool_runlock ();
Florin Corasf9240dc2019-01-15 08:03:17 -0800681
682 return 0;
683}
684
685void
Florin Coras40c07ce2020-07-16 20:46:17 -0700686vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
Florin Corasf9240dc2019-01-15 08:03:17 -0800687{
Florin Coras40c07ce2020-07-16 20:46:17 -0700688 vls_shared_data_t *vls_shd;
689
690 u32 vls_shd_index = vls_shared_data_alloc ();
691
692 vls_shared_data_pool_rlock ();
693
694 vls_shd = vls_shared_data_get (vls_shd_index);
695 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
696 vls->shared_data_index = vls_shd_index;
697 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
698
699 vls_shared_data_pool_runlock ();
700}
701
702void
703vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
704{
705 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000706 vls_shared_data_t *vls_shd;
707 vcl_session_t *s;
Florin Corasf9240dc2019-01-15 08:03:17 -0800708
Florin Coras243edd52020-03-04 22:20:12 +0000709 s = vcl_session_get (vcl_wrk, vls->session_index);
710 if (!s)
711 {
Florin Coras40c07ce2020-07-16 20:46:17 -0700712 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
713 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
Florin Coras243edd52020-03-04 22:20:12 +0000714 return;
715 }
716
Florin Coras40c07ce2020-07-16 20:46:17 -0700717 ASSERT (vls->shared_data_index != ~0);
718
Florin Coras243edd52020-03-04 22:20:12 +0000719 /* Reinit session lock */
720 clib_spinlock_init (&vls->lock);
721
Florin Coras40c07ce2020-07-16 20:46:17 -0700722 vls_shared_data_pool_rlock ();
Florin Coras243edd52020-03-04 22:20:12 +0000723
Florin Coras40c07ce2020-07-16 20:46:17 -0700724 vls_shd = vls_shared_data_get (vls->shared_data_index);
Florin Coras243edd52020-03-04 22:20:12 +0000725
726 clib_spinlock_lock (&vls_shd->lock);
Florin Coras243edd52020-03-04 22:20:12 +0000727 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
Florin Coras243edd52020-03-04 22:20:12 +0000728 clib_spinlock_unlock (&vls_shd->lock);
Florin Coras40c07ce2020-07-16 20:46:17 -0700729
Florin Coras243edd52020-03-04 22:20:12 +0000730 vls_shared_data_pool_runlock ();
731
Florin Corasf9240dc2019-01-15 08:03:17 -0800732 if (s->rx_fifo)
733 {
Florin Coras243edd52020-03-04 22:20:12 +0000734 svm_fifo_add_subscriber (s->rx_fifo, vcl_wrk->vpp_wrk_index);
735 svm_fifo_add_subscriber (s->tx_fifo, vcl_wrk->vpp_wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -0800736 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700737 else if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -0800738 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700739 s->session_state = VCL_STATE_LISTEN_NO_MQ;
Florin Coras2d675d72019-01-28 15:54:27 -0800740 }
Florin Coras243edd52020-03-04 22:20:12 +0000741}
Florin Coras2d675d72019-01-28 15:54:27 -0800742
Florin Coras243edd52020-03-04 22:20:12 +0000743static void
744vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
745{
Florin Coras40c07ce2020-07-16 20:46:17 -0700746 vcl_locked_session_t *vls, *parent_vls;
Florin Coras243edd52020-03-04 22:20:12 +0000747
748 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100749 pool_foreach (vls, vls_wrk->vls_pool) {
Florin Coras40c07ce2020-07-16 20:46:17 -0700750 /* Initialize sharing on parent session */
751 if (vls->shared_data_index == ~0)
752 {
753 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
754 vls_init_share_session (vls_parent_wrk, parent_vls);
755 vls->shared_data_index = parent_vls->shared_data_index;
756 }
757 vls_share_session (vls_wrk, vls);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100758 }
Florin Coras243edd52020-03-04 22:20:12 +0000759 /* *INDENT-ON* */
Florin Corasf9240dc2019-01-15 08:03:17 -0800760}
761
762void
763vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
764{
Florin Coras243edd52020-03-04 22:20:12 +0000765 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
Florin Corasf9240dc2019-01-15 08:03:17 -0800766 vcl_worker_t *wrk = vcl_worker_get_current ();
hanlinf8e13632020-08-21 11:05:36 +0800767 u32 vls_index, session_index, wrk_index;
768 vcl_session_handle_t sh;
Florin Corasf9240dc2019-01-15 08:03:17 -0800769
Florin Coras243edd52020-03-04 22:20:12 +0000770 /*
771 * init vcl worker
772 */
Florin Corasf9240dc2019-01-15 08:03:17 -0800773 wrk->sessions = pool_dup (parent_wrk->sessions);
774 wrk->session_index_by_vpp_handles =
775 hash_dup (parent_wrk->session_index_by_vpp_handles);
776
Florin Coras243edd52020-03-04 22:20:12 +0000777 /*
778 * init vls worker
779 */
780 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
hanlinf8e13632020-08-21 11:05:36 +0800781 /* *INDENT-OFF* */
782 hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
783 ({
784 vcl_session_handle_parse (sh, &wrk_index, &session_index);
785 hash_set (vls_wrk->session_handle_to_vlsh_table,
786 vcl_session_handle_from_index (session_index), vls_index);
787 }));
788 /* *INDENT-ON* */
Florin Coras243edd52020-03-04 22:20:12 +0000789 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
Florin Coras2d675d72019-01-28 15:54:27 -0800790
Florin Coras243edd52020-03-04 22:20:12 +0000791 vls_share_sessions (vls_parent_wrk, vls_wrk);
Florin Corasf9240dc2019-01-15 08:03:17 -0800792}
793
Florin Coras0ef8ef22019-01-18 08:37:13 -0800794static void
795vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
796{
797 vcl_worker_t *wrk = vcl_worker_get_current ();
798 vcl_session_t *s = 0;
799 int is_nonblk = 0;
800
801 if (vls)
802 {
803 s = vcl_session_get (wrk, vls->session_index);
804 if (PREDICT_FALSE (!s))
805 return;
Florin Corasac422d62020-10-19 20:51:36 -0700806 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800807 }
808
809 switch (op)
810 {
811 case VLS_MT_OP_READ:
812 if (!is_nonblk)
813 is_nonblk = vcl_session_read_ready (s) != 0;
814 if (!is_nonblk)
815 {
816 vls_mt_mq_lock ();
817 *locks_acq |= VLS_MT_LOCK_MQ;
818 }
819 break;
820 case VLS_MT_OP_WRITE:
Florin Coras78b5fa62019-02-21 20:04:15 -0800821 ASSERT (s);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800822 if (!is_nonblk)
823 is_nonblk = vcl_session_write_ready (s) != 0;
824 if (!is_nonblk)
825 {
826 vls_mt_mq_lock ();
827 *locks_acq |= VLS_MT_LOCK_MQ;
828 }
829 break;
830 case VLS_MT_OP_XPOLL:
831 vls_mt_mq_lock ();
832 *locks_acq |= VLS_MT_LOCK_MQ;
833 break;
834 case VLS_MT_OP_SPOOL:
835 vls_mt_spool_lock ();
836 *locks_acq |= VLS_MT_LOCK_SPOOL;
837 break;
838 default:
839 break;
840 }
841}
842
843static void
844vls_mt_rel_locks (int locks_acq)
845{
846 if (locks_acq & VLS_MT_LOCK_MQ)
847 vls_mt_mq_unlock ();
848 if (locks_acq & VLS_MT_LOCK_SPOOL)
849 vls_mt_create_unlock ();
850}
851
Florin Corasff40d8f2020-08-11 22:05:28 -0700852static inline u8
853vls_mt_session_should_migrate (vcl_locked_session_t * vls)
854{
855 return (vls_mt_wrk_supported ()
856 && vls->worker_index != vcl_get_worker_index ());
857}
858
wanghanlindcacdc42020-12-28 16:19:05 +0800859static vcl_locked_session_t *
860vls_mt_session_migrate (vcl_locked_session_t *vls)
hanlina3a48962020-07-13 11:09:15 +0800861{
862 u32 wrk_index = vcl_get_worker_index ();
863 vcl_worker_t *wrk;
wanghanlindcacdc42020-12-28 16:19:05 +0800864 u32 src_sid, sid, vls_index, own_vcl_wrk_index;
hanlina3a48962020-07-13 11:09:15 +0800865 vcl_session_t *session;
866 uword *p;
867
Florin Corasff40d8f2020-08-11 22:05:28 -0700868 ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
hanlina3a48962020-07-13 11:09:15 +0800869
Florin Corasff40d8f2020-08-11 22:05:28 -0700870 /*
871 * VCL session on current vcl worker already allocated. Update current
872 * owner worker and index and return
873 */
hanlina3a48962020-07-13 11:09:15 +0800874 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
875 {
876 vls->worker_index = wrk_index;
877 vls->session_index = (u32) p[0];
wanghanlindcacdc42020-12-28 16:19:05 +0800878 return vls;
hanlina3a48962020-07-13 11:09:15 +0800879 }
880
Florin Corasff40d8f2020-08-11 22:05:28 -0700881 /*
882 * Ask vcl worker that owns the original vcl session to clone it into
883 * current vcl worker session pool
884 */
885
hanlina3a48962020-07-13 11:09:15 +0800886 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
887 vls->owner_vcl_wrk_index)))
888 {
889 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
890 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +0800891 vls_unlock (vls);
892 vls_mt_table_runlock ();
893 return 0;
hanlina3a48962020-07-13 11:09:15 +0800894 }
895
896 src_sid = (u32) p[0];
897 wrk = vcl_worker_get_current ();
898 session = vcl_session_alloc (wrk);
899 sid = session->session_index;
hanlina3a48962020-07-13 11:09:15 +0800900 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
901 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
902
wanghanlindcacdc42020-12-28 16:19:05 +0800903 /* Drop lock to prevent dead lock when dst wrk trying to get lock. */
904 vls_index = vls->vls_index;
905 own_vcl_wrk_index = vls->owner_vcl_wrk_index;
906 vls_unlock (vls);
907 vls_mt_table_runlock ();
908 vls_send_clone_and_share_rpc (wrk, vls_index, sid, vls_get_worker_index (),
909 own_vcl_wrk_index, vls_index, src_sid);
910
911 if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_SESSION_NOT_EXIST))
hanlina3a48962020-07-13 11:09:15 +0800912 {
wanghanlindcacdc42020-12-28 16:19:05 +0800913 VWRN ("session %u not exist", src_sid);
914 goto err;
915 }
916 else if (PREDICT_FALSE (wrk->rpc_done == VLS_RPC_STATE_INIT))
917 {
918 VWRN ("failed to wait rpc response");
919 goto err;
920 }
921 else if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP) &&
922 session->vep.next_sh != ~0))
923 {
hanlina3a48962020-07-13 11:09:15 +0800924 VERR ("can't migrate nonempty epoll session");
925 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +0800926 goto err;
hanlina3a48962020-07-13 11:09:15 +0800927 }
Florin Coras6c3b2182020-10-19 18:36:48 -0700928 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
Florin Corasc127d5a2020-10-14 16:35:58 -0700929 session->session_state != VCL_STATE_CLOSED))
hanlina3a48962020-07-13 11:09:15 +0800930 {
hanlina3a48962020-07-13 11:09:15 +0800931 VERR ("migrate NOT supported, session_status (%u)",
932 session->session_state);
933 ASSERT (0);
wanghanlindcacdc42020-12-28 16:19:05 +0800934 goto err;
hanlina3a48962020-07-13 11:09:15 +0800935 }
wanghanlindcacdc42020-12-28 16:19:05 +0800936
937 vls = vls_get_w_dlock (vls_index);
938 if (PREDICT_FALSE (!vls))
939 {
940 VWRN ("failed to get vls %u", vls_index);
941 goto err;
942 }
943
944 session->session_index = sid;
945 vls->worker_index = wrk_index;
946 vls->session_index = sid;
947 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
948 return vls;
949
950err:
951 vcl_session_free (wrk, session);
952 return 0;
hanlina3a48962020-07-13 11:09:15 +0800953}
954
Florin Corasff40d8f2020-08-11 22:05:28 -0700955static inline void
956vls_mt_detect (void)
957{
958 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
959 vls_mt_add ();
960}
Florin Coras0ef8ef22019-01-18 08:37:13 -0800961
wanghanlindcacdc42020-12-28 16:19:05 +0800962#define vls_mt_guard(_vls, _op) \
963 int _locks_acq = 0; \
964 if (vls_mt_wrk_supported ()) \
965 { \
966 if (PREDICT_FALSE (_vls && \
967 ((vcl_locked_session_t *) _vls)->worker_index != \
968 vcl_get_worker_index ())) \
969 { \
970 _vls = vls_mt_session_migrate (_vls); \
971 if (PREDICT_FALSE (!_vls)) \
972 return VPPCOM_EBADFD; \
973 } \
974 } \
975 else \
976 { \
977 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
978 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
979 }
Florin Corasff40d8f2020-08-11 22:05:28 -0700980
981#define vls_mt_unguard() \
982 if (PREDICT_FALSE (_locks_acq)) \
Florin Coras0ef8ef22019-01-18 08:37:13 -0800983 vls_mt_rel_locks (_locks_acq)
984
Florin Coras7baeb712019-01-04 17:05:43 -0800985int
986vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
987{
988 vcl_locked_session_t *vls;
989 int rv;
990
Florin Corasff40d8f2020-08-11 22:05:28 -0700991 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -0800992 if (!(vls = vls_get_w_dlock (vlsh)))
993 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -0800994
995 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -0800996 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -0800997 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -0800998 vls_get_and_unlock (vlsh);
999 return rv;
1000}
1001
1002int
1003vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
1004{
1005 vcl_locked_session_t *vls;
1006 int rv;
1007
Florin Corasff40d8f2020-08-11 22:05:28 -07001008 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001009 if (!(vls = vls_get_w_dlock (vlsh)))
1010 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001011 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001012 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001013 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001014 vls_get_and_unlock (vlsh);
1015 return rv;
1016}
1017
1018int
1019vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
1020 vppcom_endpt_t * ep)
1021{
1022 vcl_locked_session_t *vls;
1023 int rv;
1024
Florin Corasff40d8f2020-08-11 22:05:28 -07001025 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001026 if (!(vls = vls_get_w_dlock (vlsh)))
1027 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001028 vls_mt_guard (vls, VLS_MT_OP_WRITE);
Florin Coras7baeb712019-01-04 17:05:43 -08001029 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001030 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001031 vls_get_and_unlock (vlsh);
1032 return rv;
1033}
1034
1035ssize_t
1036vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
1037{
1038 vcl_locked_session_t *vls;
1039 int rv;
1040
Florin Corasff40d8f2020-08-11 22:05:28 -07001041 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001042 if (!(vls = vls_get_w_dlock (vlsh)))
1043 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001044 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001045 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001046 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001047 vls_get_and_unlock (vlsh);
1048 return rv;
1049}
1050
1051ssize_t
1052vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1053 vppcom_endpt_t * ep)
1054{
1055 vcl_locked_session_t *vls;
1056 int rv;
1057
Florin Corasff40d8f2020-08-11 22:05:28 -07001058 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001059 if (!(vls = vls_get_w_dlock (vlsh)))
1060 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001061 vls_mt_guard (vls, VLS_MT_OP_READ);
Florin Coras7baeb712019-01-04 17:05:43 -08001062 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1063 ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001064 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001065 vls_get_and_unlock (vlsh);
1066 return rv;
1067}
1068
1069int
1070vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1071{
1072 vcl_locked_session_t *vls;
1073 int rv;
1074
Florin Corasff40d8f2020-08-11 22:05:28 -07001075 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001076 if (!(vls = vls_get_w_dlock (vlsh)))
1077 return VPPCOM_EBADFD;
Florin Corasff40d8f2020-08-11 22:05:28 -07001078 if (vls_mt_session_should_migrate (vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001079 {
1080 vls = vls_mt_session_migrate (vls);
1081 if (PREDICT_FALSE (!vls))
1082 return VPPCOM_EBADFD;
1083 }
Florin Coras7baeb712019-01-04 17:05:43 -08001084 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1085 vls_get_and_unlock (vlsh);
1086 return rv;
1087}
1088
1089int
1090vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1091{
1092 vcl_locked_session_t *vls;
1093 int rv;
1094
Florin Corasff40d8f2020-08-11 22:05:28 -07001095 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001096 if (!(vls = vls_get_w_dlock (vlsh)))
1097 return VPPCOM_EBADFD;
1098 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1099 vls_get_and_unlock (vlsh);
1100 return rv;
1101}
1102
1103int
1104vls_listen (vls_handle_t vlsh, int q_len)
1105{
1106 vcl_locked_session_t *vls;
1107 int rv;
1108
Florin Corasff40d8f2020-08-11 22:05:28 -07001109 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001110 if (!(vls = vls_get_w_dlock (vlsh)))
1111 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001112 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001113 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001114 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001115 vls_get_and_unlock (vlsh);
1116 return rv;
1117}
1118
1119int
1120vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1121{
1122 vcl_locked_session_t *vls;
1123 int rv;
1124
Florin Corasff40d8f2020-08-11 22:05:28 -07001125 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001126 if (!(vls = vls_get_w_dlock (vlsh)))
1127 return VPPCOM_EBADFD;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001128 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001129 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001130 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001131 vls_get_and_unlock (vlsh);
1132 return rv;
1133}
1134
Florin Coras2d675d72019-01-28 15:54:27 -08001135static inline void
1136vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1137{
1138 vcl_worker_t *wrk = vcl_worker_get_current ();
1139 vcl_session_t *s;
Florin Coras243edd52020-03-04 22:20:12 +00001140 u32 owner_wrk;
Florin Coras2d675d72019-01-28 15:54:27 -08001141
hanlina3a48962020-07-13 11:09:15 +08001142 if (vls_mt_wrk_supported ())
1143 return;
1144
Florin Coras2d675d72019-01-28 15:54:27 -08001145 s = vcl_session_get (wrk, vls->session_index);
1146 switch (s->session_state)
1147 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001148 case VCL_STATE_LISTEN:
Florin Coras2d675d72019-01-28 15:54:27 -08001149 if (is_add)
1150 {
Florin Coras243edd52020-03-04 22:20:12 +00001151 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
Florin Coras2d675d72019-01-28 15:54:27 -08001152 break;
1153 }
1154 vls_listener_wrk_stop_listen (vls, vls->worker_index);
1155 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001156 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -08001157 if (!is_add)
1158 break;
1159
1160 /* Register worker as listener */
1161 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
1162
1163 /* If owner worker did not attempt to accept/xpoll on the session,
1164 * force a listen stop for it, since it may not be interested in
1165 * accepting new sessions.
1166 * This is pretty much a hack done to give app workers the illusion
1167 * that it is fine to listen and not accept new sessions for a
1168 * given listener. Without it, we would accumulate unhandled
1169 * accepts on the passive worker message queue. */
Florin Coras243edd52020-03-04 22:20:12 +00001170 owner_wrk = vls_shared_get_owner (vls);
1171 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1172 vls_listener_wrk_stop_listen (vls, owner_wrk);
Florin Coras2d675d72019-01-28 15:54:27 -08001173 break;
1174 default:
1175 break;
1176 }
1177}
1178
Florin Coras7baeb712019-01-04 17:05:43 -08001179vls_handle_t
1180vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1181{
1182 vls_handle_t accepted_vlsh;
1183 vcl_locked_session_t *vls;
1184 int sh;
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 (listener_vlsh)))
1188 return VPPCOM_EBADFD;
Florin Coras2d675d72019-01-28 15:54:27 -08001189 if (vcl_n_workers () > 1)
1190 vls_mp_checks (vls, 1 /* is_add */ );
Florin Coras0ef8ef22019-01-18 08:37:13 -08001191 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001192 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001193 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001194 vls_get_and_unlock (listener_vlsh);
1195 if (sh < 0)
1196 return sh;
1197 accepted_vlsh = vls_alloc (sh);
1198 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1199 vppcom_session_close (sh);
1200 return accepted_vlsh;
1201}
1202
1203vls_handle_t
1204vls_create (uint8_t proto, uint8_t is_nonblocking)
1205{
1206 vcl_session_handle_t sh;
1207 vls_handle_t vlsh;
wanghanlindcacdc42020-12-28 16:19:05 +08001208 vcl_locked_session_t *vls = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001209
Florin Corasff40d8f2020-08-11 22:05:28 -07001210 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001211 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Coras7baeb712019-01-04 17:05:43 -08001212 sh = vppcom_session_create (proto, is_nonblocking);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001213 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001214 if (sh == INVALID_SESSION_ID)
1215 return VLS_INVALID_HANDLE;
1216
1217 vlsh = vls_alloc (sh);
1218 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1219 vppcom_session_close (sh);
1220
1221 return vlsh;
1222}
1223
hanlina3a48962020-07-13 11:09:15 +08001224static void
Florin Corasff40d8f2020-08-11 22:05:28 -07001225vls_mt_session_cleanup (vcl_locked_session_t * vls)
hanlina3a48962020-07-13 11:09:15 +08001226{
Florin Corasff40d8f2020-08-11 22:05:28 -07001227 u32 session_index, wrk_index, current_vcl_wrk;
hanlina3a48962020-07-13 11:09:15 +08001228 vcl_worker_t *wrk = vcl_worker_get_current ();
1229
Florin Corasff40d8f2020-08-11 22:05:28 -07001230 ASSERT (vls_mt_wrk_supported ());
1231
1232 current_vcl_wrk = vcl_get_worker_index ();
hanlina3a48962020-07-13 11:09:15 +08001233
1234 /* *INDENT-OFF* */
1235 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1236 ({
Florin Corasff40d8f2020-08-11 22:05:28 -07001237 if (current_vcl_wrk != wrk_index)
1238 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
hanlina3a48962020-07-13 11:09:15 +08001239 }));
1240 /* *INDENT-ON* */
1241 hash_free (vls->vcl_wrk_index_to_session_index);
1242}
1243
Florin Coras7baeb712019-01-04 17:05:43 -08001244int
1245vls_close (vls_handle_t vlsh)
1246{
1247 vcl_locked_session_t *vls;
Florin Corasf9240dc2019-01-15 08:03:17 -08001248 int rv;
Florin Coras7baeb712019-01-04 17:05:43 -08001249
Florin Corasff40d8f2020-08-11 22:05:28 -07001250 vls_mt_detect ();
1251 vls_mt_table_wlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001252
Florin Coras0ef8ef22019-01-18 08:37:13 -08001253 vls = vls_get_and_lock (vlsh);
1254 if (!vls)
1255 {
Florin Corasff40d8f2020-08-11 22:05:28 -07001256 vls_mt_table_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001257 return VPPCOM_EBADFD;
1258 }
1259
hanlina3a48962020-07-13 11:09:15 +08001260 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
Florin Corasf9240dc2019-01-15 08:03:17 -08001261
Florin Coras243edd52020-03-04 22:20:12 +00001262 if (vls_is_shared (vls))
1263 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1264 else
1265 rv = vppcom_session_close (vls_to_sh (vls));
1266
Florin Corasff40d8f2020-08-11 22:05:28 -07001267 if (vls_mt_wrk_supported ())
1268 vls_mt_session_cleanup (vls);
hanlina3a48962020-07-13 11:09:15 +08001269
Florin Coras0ef8ef22019-01-18 08:37:13 -08001270 vls_free (vls);
1271 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001272
Florin Corasff40d8f2020-08-11 22:05:28 -07001273 vls_mt_table_wunlock ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001274
Florin Coras7baeb712019-01-04 17:05:43 -08001275 return rv;
1276}
1277
1278vls_handle_t
1279vls_epoll_create (void)
1280{
1281 vcl_session_handle_t sh;
1282 vls_handle_t vlsh;
1283
Florin Corasff40d8f2020-08-11 22:05:28 -07001284 vls_mt_detect ();
Florin Coras63d3ac62019-03-29 08:29:25 -07001285
Florin Coras7baeb712019-01-04 17:05:43 -08001286 sh = vppcom_epoll_create ();
1287 if (sh == INVALID_SESSION_ID)
1288 return VLS_INVALID_HANDLE;
1289
1290 vlsh = vls_alloc (sh);
1291 if (vlsh == VLS_INVALID_HANDLE)
1292 vppcom_session_close (sh);
1293
1294 return vlsh;
1295}
1296
Florin Coras2d675d72019-01-28 15:54:27 -08001297static void
1298vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1299{
1300 if (vcl_n_workers () <= 1)
1301 {
1302 vlsl->epoll_mp_check = 1;
1303 return;
1304 }
1305
1306 if (op == EPOLL_CTL_MOD)
1307 return;
1308
1309 vlsl->epoll_mp_check = 1;
1310 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1311}
1312
Florin Coras7baeb712019-01-04 17:05:43 -08001313int
1314vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1315 struct epoll_event *event)
1316{
1317 vcl_locked_session_t *ep_vls, *vls;
1318 vcl_session_handle_t ep_sh, sh;
1319 int rv;
1320
Florin Corasff40d8f2020-08-11 22:05:28 -07001321 vls_mt_detect ();
1322 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001323 ep_vls = vls_get_and_lock (ep_vlsh);
Florin Corasff40d8f2020-08-11 22:05:28 -07001324
1325 if (vls_mt_session_should_migrate (ep_vls))
wanghanlindcacdc42020-12-28 16:19:05 +08001326 {
1327 ep_vls = vls_mt_session_migrate (ep_vls);
1328 if (PREDICT_FALSE (!ep_vls))
1329 return VPPCOM_EBADFD;
1330 }
Florin Corasff40d8f2020-08-11 22:05:28 -07001331
Florin Coras7baeb712019-01-04 17:05:43 -08001332 ep_sh = vls_to_sh (ep_vls);
wanghanlindcacdc42020-12-28 16:19:05 +08001333 vls = vls_get_and_lock (vlsh);
Florin Coras7baeb712019-01-04 17:05:43 -08001334 sh = vls_to_sh (vls);
Florin Coras2d675d72019-01-28 15:54:27 -08001335
1336 if (PREDICT_FALSE (!vlsl->epoll_mp_check))
1337 vls_epoll_ctl_mp_checks (vls, op);
1338
Florin Corasff40d8f2020-08-11 22:05:28 -07001339 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001340
1341 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1342
Florin Corasff40d8f2020-08-11 22:05:28 -07001343 vls_mt_table_rlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001344 ep_vls = vls_get (ep_vlsh);
1345 vls = vls_get (vlsh);
1346 vls_unlock (vls);
1347 vls_unlock (ep_vls);
Florin Corasff40d8f2020-08-11 22:05:28 -07001348 vls_mt_table_runlock ();
Florin Coras7baeb712019-01-04 17:05:43 -08001349 return rv;
1350}
1351
1352int
1353vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1354 int maxevents, double wait_for_time)
1355{
wanghanlindcacdc42020-12-28 16:19:05 +08001356 vcl_locked_session_t *vls, *vls_tmp = NULL;
Florin Coras7baeb712019-01-04 17:05:43 -08001357 int rv;
1358
Florin Corasff40d8f2020-08-11 22:05:28 -07001359 vls_mt_detect ();
Florin Coras7baeb712019-01-04 17:05:43 -08001360 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1361 return VPPCOM_EBADFD;
wanghanlindcacdc42020-12-28 16:19:05 +08001362 vls_mt_guard (vls_tmp, VLS_MT_OP_XPOLL);
Florin Coras7baeb712019-01-04 17:05:43 -08001363 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1364 wait_for_time);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001365 vls_mt_unguard ();
Florin Coras7baeb712019-01-04 17:05:43 -08001366 vls_get_and_unlock (ep_vlsh);
1367 return rv;
1368}
1369
Florin Coras2d675d72019-01-28 15:54:27 -08001370static void
1371vls_select_mp_checks (vcl_si_set * read_map)
1372{
1373 vcl_locked_session_t *vls;
1374 vcl_worker_t *wrk;
1375 vcl_session_t *s;
1376 u32 si;
1377
1378 if (vcl_n_workers () <= 1)
1379 {
1380 vlsl->select_mp_check = 1;
1381 return;
1382 }
1383
1384 if (!read_map)
1385 return;
1386
1387 vlsl->select_mp_check = 1;
1388 wrk = vcl_worker_get_current ();
1389
1390 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001391 clib_bitmap_foreach (si, read_map) {
Florin Coras2d675d72019-01-28 15:54:27 -08001392 s = vcl_session_get (wrk, si);
Florin Corasc127d5a2020-10-14 16:35:58 -07001393 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras2d675d72019-01-28 15:54:27 -08001394 {
1395 vls = vls_get (vls_session_index_to_vlsh (si));
1396 vls_mp_checks (vls, 1 /* is_add */);
1397 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001398 }
Florin Coras2d675d72019-01-28 15:54:27 -08001399 /* *INDENT-ON* */
1400}
1401
Florin Coras0ef8ef22019-01-18 08:37:13 -08001402int
1403vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1404 vcl_si_set * except_map, double wait_for_time)
1405{
1406 int rv;
wanghanlindcacdc42020-12-28 16:19:05 +08001407 vcl_locked_session_t *vls = NULL;
Florin Coras2d675d72019-01-28 15:54:27 -08001408
Florin Corasff40d8f2020-08-11 22:05:28 -07001409 vls_mt_detect ();
wanghanlindcacdc42020-12-28 16:19:05 +08001410 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
Florin Coras2d675d72019-01-28 15:54:27 -08001411 if (PREDICT_FALSE (!vlsl->select_mp_check))
1412 vls_select_mp_checks (read_map);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001413 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1414 vls_mt_unguard ();
1415 return rv;
1416}
1417
Florin Corasf9240dc2019-01-15 08:03:17 -08001418static void
Florin Coras0ef8ef22019-01-18 08:37:13 -08001419vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1420{
1421 u32 current_wrk, is_current;
1422 vcl_locked_session_t *vls;
1423 vcl_session_t *s;
1424
Florin Coras14ed6df2019-03-06 21:13:42 -08001425 if (pool_elts (vcm->workers) <= 1)
1426 return;
1427
Florin Coras0ef8ef22019-01-18 08:37:13 -08001428 current_wrk = vcl_get_worker_index ();
1429 is_current = current_wrk == wrk->wrk_index;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001430
1431 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001432 pool_foreach (s, wrk->sessions) {
hanlinf8e13632020-08-21 11:05:36 +08001433 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
Florin Coras0ef8ef22019-01-18 08:37:13 -08001434 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1435 vls_unshare_session (vls, wrk);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001436 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001437 /* *INDENT-ON* */
Florin Coras0ef8ef22019-01-18 08:37:13 -08001438}
1439
1440static void
1441vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1442{
Florin Coras243edd52020-03-04 22:20:12 +00001443 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1444
Florin Coras0ef8ef22019-01-18 08:37:13 -08001445 /* Unshare sessions and also cleanup worker since child may have
1446 * called _exit () and therefore vcl may not catch the event */
1447 vls_unshare_vcl_worker_sessions (wrk);
1448 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
Florin Coras243edd52020-03-04 22:20:12 +00001449
1450 vls_worker_free (vls_wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001451}
1452
1453static void
Florin Corasf9240dc2019-01-15 08:03:17 -08001454vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1455{
1456 vcl_worker_t *sub_child;
1457 int tries = 0;
1458
1459 if (child_wrk->forked_child != ~0)
1460 {
1461 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1462 if (sub_child)
1463 {
1464 /* Wait a bit, maybe the process is going away */
1465 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1466 usleep (1e3);
1467 if (kill (sub_child->current_pid, 0) < 0)
1468 vls_cleanup_forked_child (child_wrk, sub_child);
1469 }
1470 }
Florin Coras0ef8ef22019-01-18 08:37:13 -08001471 vls_cleanup_vcl_worker (child_wrk);
1472 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
Florin Corasf9240dc2019-01-15 08:03:17 -08001473 wrk->forked_child = ~0;
1474}
1475
1476static struct sigaction old_sa;
1477
1478static void
1479vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1480{
1481 vcl_worker_t *wrk, *child_wrk;
1482
1483 if (vcl_get_worker_index () == ~0)
1484 return;
1485
1486 if (sigaction (SIGCHLD, &old_sa, 0))
1487 {
1488 VERR ("couldn't restore sigchld");
1489 exit (-1);
1490 }
1491
1492 wrk = vcl_worker_get_current ();
1493 if (wrk->forked_child == ~0)
1494 return;
1495
1496 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1497 if (!child_wrk)
1498 goto done;
1499
1500 if (si && si->si_pid != child_wrk->current_pid)
1501 {
1502 VDBG (0, "unexpected child pid %u", si->si_pid);
1503 goto done;
1504 }
1505 vls_cleanup_forked_child (wrk, child_wrk);
1506
1507done:
1508 if (old_sa.sa_flags & SA_SIGINFO)
1509 {
1510 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1511 fn (signum, si, uc);
1512 }
1513 else
1514 {
1515 void (*fn) (int) = old_sa.sa_handler;
1516 if (fn)
1517 fn (signum);
1518 }
1519}
1520
1521static void
1522vls_incercept_sigchld ()
1523{
1524 struct sigaction sa;
1525 clib_memset (&sa, 0, sizeof (sa));
1526 sa.sa_sigaction = vls_intercept_sigchld_handler;
1527 sa.sa_flags = SA_SIGINFO;
1528 if (sigaction (SIGCHLD, &sa, &old_sa))
1529 {
1530 VERR ("couldn't intercept sigchld");
1531 exit (-1);
1532 }
1533}
1534
1535static void
1536vls_app_pre_fork (void)
1537{
1538 vls_incercept_sigchld ();
1539 vcl_flush_mq_events ();
1540}
1541
1542static void
1543vls_app_fork_child_handler (void)
1544{
1545 vcl_worker_t *parent_wrk;
Florin Corasb88de902020-09-08 16:47:57 -07001546 int parent_wrk_index;
Florin Corasf9240dc2019-01-15 08:03:17 -08001547
1548 parent_wrk_index = vcl_get_worker_index ();
1549 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1550 parent_wrk_index);
1551
1552 /*
Florin Corasb88de902020-09-08 16:47:57 -07001553 * Clear old state
Florin Corasf9240dc2019-01-15 08:03:17 -08001554 */
1555 vcl_set_worker_index (~0);
Florin Corasf9240dc2019-01-15 08:03:17 -08001556
1557 /*
Florin Corasb88de902020-09-08 16:47:57 -07001558 * Allocate and register vcl worker with vpp
Florin Corasf9240dc2019-01-15 08:03:17 -08001559 */
Florin Corasb88de902020-09-08 16:47:57 -07001560 if (vppcom_worker_register ())
Florin Corasf9240dc2019-01-15 08:03:17 -08001561 {
Florin Corasb88de902020-09-08 16:47:57 -07001562 VERR ("couldn't register new worker!");
Florin Corasf9240dc2019-01-15 08:03:17 -08001563 return;
1564 }
1565
1566 /*
Florin Corasb88de902020-09-08 16:47:57 -07001567 * Allocate/initialize vls worker and share sessions
Florin Coras243edd52020-03-04 22:20:12 +00001568 */
1569 vls_worker_alloc ();
Florin Corasf9240dc2019-01-15 08:03:17 -08001570 parent_wrk = vcl_worker_get (parent_wrk_index);
1571 vls_worker_copy_on_fork (parent_wrk);
1572 parent_wrk->forked_child = vcl_get_worker_index ();
1573
Florin Coras0ef8ef22019-01-18 08:37:13 -08001574 /* Reset number of threads and set wrk index */
Florin Coras2d675d72019-01-28 15:54:27 -08001575 vlsl->vls_mt_n_threads = 0;
1576 vlsl->vls_wrk_index = vcl_get_worker_index ();
1577 vlsl->select_mp_check = 0;
1578 vlsl->epoll_mp_check = 0;
1579 vls_mt_locks_init ();
Florin Coras0ef8ef22019-01-18 08:37:13 -08001580
Florin Corasf9240dc2019-01-15 08:03:17 -08001581 VDBG (0, "forked child main worker initialized");
1582 vcm->forking = 0;
1583}
1584
1585static void
1586vls_app_fork_parent_handler (void)
1587{
1588 vcm->forking = 1;
1589 while (vcm->forking)
1590 ;
1591}
1592
Florin Coras0ef8ef22019-01-18 08:37:13 -08001593void
1594vls_app_exit (void)
1595{
Florin Coras243edd52020-03-04 22:20:12 +00001596 vls_worker_t *wrk = vls_worker_get_current ();
1597
Florin Coras0ef8ef22019-01-18 08:37:13 -08001598 /* Unshare the sessions. VCL will clean up the worker */
1599 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
Florin Coras243edd52020-03-04 22:20:12 +00001600 vls_worker_free (wrk);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001601}
1602
Florin Coras40c07ce2020-07-16 20:46:17 -07001603static void
1604vls_clone_and_share_rpc_handler (void *args)
1605{
1606 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1607 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1608 vcl_locked_session_t *vls, *dst_vls;
hanlina3a48962020-07-13 11:09:15 +08001609 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
Florin Coras40c07ce2020-07-16 20:46:17 -07001610 vcl_session_t *s, *dst_s;
1611
wanghanlindcacdc42020-12-28 16:19:05 +08001612 VDBG (1, "process session clone of worker (session): %u (%u) -> %u (%u)",
1613 vcl_wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk,
1614 msg->origin_session_index);
1615
1616 /* VCL locked session can't been protected, so DONT touch it.
1617 * VCL session may been free, check it.
1618 */
1619 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1620 s = vcl_session_get (vcl_wrk, msg->session_index);
1621 if (PREDICT_FALSE (!s))
1622 {
1623 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SESSION_NOT_EXIST;
1624 return;
1625 }
Florin Coras40c07ce2020-07-16 20:46:17 -07001626
hanlina3a48962020-07-13 11:09:15 +08001627 if (!vls_mt_wrk_supported ())
wanghanlindcacdc42020-12-28 16:19:05 +08001628 {
1629 vls = vls_session_get (wrk, msg->vls_index);
1630 vls_init_share_session (wrk, vls);
1631 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1632 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1633 dst_vls->shared_data_index = vls->shared_data_index;
1634 }
hanlina3a48962020-07-13 11:09:15 +08001635 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
Florin Coras40c07ce2020-07-16 20:46:17 -07001636 clib_memcpy (dst_s, s, sizeof (*s));
1637
wanghanlindcacdc42020-12-28 16:19:05 +08001638 dst_vcl_wrk->rpc_done = VLS_RPC_STATE_SUCCESS;
hanlina3a48962020-07-13 11:09:15 +08001639}
1640
1641static void
1642vls_session_cleanup_rpc_handler (void *args)
1643{
1644 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1645 vcl_worker_t *wrk = vcl_worker_get_current ();
wanghanlindcacdc42020-12-28 16:19:05 +08001646 vcl_session_handle_t sh = vcl_session_handle_from_index (msg->session_index);
hanlina3a48962020-07-13 11:09:15 +08001647
wanghanlindcacdc42020-12-28 16:19:05 +08001648 VDBG (1, "process session cleanup of worker (session): %u (%u) from %u ()",
1649 wrk->wrk_index, msg->session_index, msg->origin_vcl_wrk);
hanlina3a48962020-07-13 11:09:15 +08001650
wanghanlindcacdc42020-12-28 16:19:05 +08001651 vppcom_session_close (sh);
Florin Coras40c07ce2020-07-16 20:46:17 -07001652}
1653
1654static void
1655vls_rpc_handler (void *args)
1656{
1657 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1658 switch (msg->type)
1659 {
1660 case VLS_RPC_CLONE_AND_SHARE:
1661 vls_clone_and_share_rpc_handler (msg->data);
1662 break;
hanlina3a48962020-07-13 11:09:15 +08001663 case VLS_RPC_SESS_CLEANUP:
1664 vls_session_cleanup_rpc_handler (msg->data);
1665 break;
Florin Coras40c07ce2020-07-16 20:46:17 -07001666 default:
1667 break;
1668 }
1669}
1670
1671void
wanghanlindcacdc42020-12-28 16:19:05 +08001672vls_send_clone_and_share_rpc (vcl_worker_t *wrk, u32 origin_vls_index,
1673 u32 session_index, u32 vls_wrk_index,
1674 u32 dst_wrk_index, u32 dst_vls_index,
1675 u32 dst_session_index)
Florin Coras40c07ce2020-07-16 20:46:17 -07001676{
1677 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1678 vls_clone_and_share_msg_t *msg;
1679 vls_rpc_msg_t *rpc;
hanlina3a48962020-07-13 11:09:15 +08001680 int ret;
wanghanlindcacdc42020-12-28 16:19:05 +08001681 f64 timeout = clib_time_now (&wrk->clib_time) + VLS_WORKER_RPC_TIMEOUT;
Florin Coras40c07ce2020-07-16 20:46:17 -07001682
1683 rpc = (vls_rpc_msg_t *) & data;
1684 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1685 msg = (vls_clone_and_share_msg_t *) & rpc->data;
hanlina3a48962020-07-13 11:09:15 +08001686 msg->origin_vls_wrk = vls_wrk_index;
wanghanlindcacdc42020-12-28 16:19:05 +08001687 msg->origin_vls_index = origin_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001688 msg->origin_vcl_wrk = wrk->wrk_index;
1689 msg->origin_session_index = session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001690 msg->vls_index = dst_vls_index;
hanlina3a48962020-07-13 11:09:15 +08001691 msg->session_index = dst_session_index;
Florin Coras40c07ce2020-07-16 20:46:17 -07001692
wanghanlindcacdc42020-12-28 16:19:05 +08001693 /* Try lock and handle rpcs if two threads send each other
1694 * clone requests at the same time.
1695 */
1696 wrk->rpc_done = VLS_RPC_STATE_INIT;
1697 while (!clib_spinlock_trylock (&vlsm->worker_rpc_lock))
1698 vcl_flush_mq_events ();
hanlina3a48962020-07-13 11:09:15 +08001699 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1700
Florin Corasff40d8f2020-08-11 22:05:28 -07001701 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001702 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1703 msg->origin_session_index, ret);
wanghanlindcacdc42020-12-28 16:19:05 +08001704 while (!ret && wrk->rpc_done == VLS_RPC_STATE_INIT &&
1705 clib_time_now (&wrk->clib_time) < timeout)
hanlina3a48962020-07-13 11:09:15 +08001706 ;
wanghanlindcacdc42020-12-28 16:19:05 +08001707 clib_spinlock_unlock (&vlsm->worker_rpc_lock);
hanlina3a48962020-07-13 11:09:15 +08001708}
1709
1710void
1711vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1712 u32 dst_wrk_index, u32 dst_session_index)
1713{
1714 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1715 vls_sess_cleanup_msg_t *msg;
1716 vls_rpc_msg_t *rpc;
1717 int ret;
1718
1719 rpc = (vls_rpc_msg_t *) & data;
1720 rpc->type = VLS_RPC_SESS_CLEANUP;
1721 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1722 msg->origin_vcl_wrk = wrk->wrk_index;
1723 msg->session_index = dst_session_index;
1724
hanlina3a48962020-07-13 11:09:15 +08001725 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1726
Florin Corasff40d8f2020-08-11 22:05:28 -07001727 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
hanlina3a48962020-07-13 11:09:15 +08001728 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
Florin Coras40c07ce2020-07-16 20:46:17 -07001729}
1730
Florin Coras7baeb712019-01-04 17:05:43 -08001731int
1732vls_app_create (char *app_name)
1733{
1734 int rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001735
Florin Coras7baeb712019-01-04 17:05:43 -08001736 if ((rv = vppcom_app_create (app_name)))
1737 return rv;
Florin Coras243edd52020-03-04 22:20:12 +00001738
Florin Coras2d675d72019-01-28 15:54:27 -08001739 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1740 clib_memset (vlsm, 0, sizeof (*vlsm));
Florin Coras7baeb712019-01-04 17:05:43 -08001741 clib_rwlock_init (&vlsm->vls_table_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001742 clib_rwlock_init (&vlsm->shared_data_lock);
wanghanlindcacdc42020-12-28 16:19:05 +08001743 clib_spinlock_init (&vlsm->worker_rpc_lock);
Florin Coras243edd52020-03-04 22:20:12 +00001744 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1745
Florin Corasf9240dc2019-01-15 08:03:17 -08001746 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1747 vls_app_fork_child_handler);
Florin Coras0ef8ef22019-01-18 08:37:13 -08001748 atexit (vls_app_exit);
Florin Coras243edd52020-03-04 22:20:12 +00001749 vls_worker_alloc ();
Florin Coras2d675d72019-01-28 15:54:27 -08001750 vlsl->vls_wrk_index = vcl_get_worker_index ();
1751 vls_mt_locks_init ();
Florin Coras40c07ce2020-07-16 20:46:17 -07001752 vcm->wrk_rpc_fn = vls_rpc_handler;
Florin Coras7baeb712019-01-04 17:05:43 -08001753 return VPPCOM_OK;
1754}
1755
hanlin4266d4d2020-05-19 17:34:17 +08001756unsigned char
1757vls_use_eventfd (void)
1758{
1759 return vcm->cfg.use_mq_eventfd;
1760}
1761
hanlina3a48962020-07-13 11:09:15 +08001762unsigned char
1763vls_mt_wrk_supported (void)
1764{
Florin Corasff40d8f2020-08-11 22:05:28 -07001765 return vcm->cfg.mt_wrk_supported;
hanlina3a48962020-07-13 11:09:15 +08001766}
1767
1768int
1769vls_use_real_epoll (void)
1770{
1771 if (vcl_get_worker_index () == ~0)
1772 return 0;
1773
1774 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1775}
1776
1777void
1778vls_register_vcl_worker (void)
1779{
wanghanlin492350e2020-09-11 17:19:32 +08001780 vls_mt_add ();
hanlina3a48962020-07-13 11:09:15 +08001781}
1782
Florin Coras7baeb712019-01-04 17:05:43 -08001783/*
1784 * fd.io coding-style-patch-verification: ON
1785 *
1786 * Local Variables:
1787 * eval: (c-set-style "gnu")
1788 * End:
1789 */