2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vcl/vcl_locked.h>
17 #include <vcl/vcl_private.h>
19 typedef struct vls_shared_data_
23 u32 *workers_subscribed;
24 clib_bitmap_t *listeners;
27 typedef struct vcl_locked_session_
33 u32 shared_data_index;
34 /** VCL session owned by different workers because of migration */
35 u32 owner_vcl_wrk_index;
36 uword *vcl_wrk_index_to_session_index;
37 } vcl_locked_session_t;
39 typedef struct vls_worker_
41 vcl_locked_session_t *vls_pool;
42 uword *session_handle_to_vlsh_table;
46 typedef struct vls_local_
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;
56 static vls_process_local_t vls_local;
57 static vls_process_local_t *vlsl = &vls_local;
59 typedef struct vls_main_
61 vls_worker_t *workers;
62 clib_rwlock_t vls_table_lock;
63 /** 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;
70 typedef enum vls_rpc_msg_type_
72 VLS_RPC_CLONE_AND_SHARE,
76 typedef struct vls_rpc_msg_
82 typedef struct vls_clone_and_share_msg_
84 u32 vls_index; /**< vls to be shared */
85 u32 session_index; /**< vcl session to be shared */
86 u32 origin_vls_wrk; /**< vls worker that initiated the rpc */
87 u32 origin_vls_index; /**< vls session of the originator */
88 u32 origin_vcl_wrk; /**< vcl worker that initiated the rpc */
89 u32 origin_session_index; /**< vcl session of the originator */
90 } vls_clone_and_share_msg_t;
92 typedef struct vls_sess_cleanup_msg_
94 u32 session_index; /**< vcl session to be cleaned */
95 u32 origin_vcl_wrk; /**< worker that initiated the rpc */
96 } vls_sess_cleanup_msg_t;
98 void vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
99 u32 dst_wrk_index, u32 dst_session_index);
100 void vls_send_clone_and_share_rpc (vcl_worker_t * wrk,
101 vcl_locked_session_t * vls,
102 u32 session_index, u32 vls_wrk_index,
103 u32 dst_wrk_index, u32 dst_vls_index,
104 u32 dst_session_index);
108 vls_get_worker_index (void)
110 if (vls_mt_wrk_supported ())
111 return vlsl->vls_wrk_index;
113 return vcl_get_worker_index ();
117 vls_shared_data_alloc (void)
119 vls_shared_data_t *vls_shd;
122 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
123 pool_get_zero (vlsm->shared_data_pool, vls_shd);
124 clib_spinlock_init (&vls_shd->lock);
125 shd_index = vls_shd - vlsm->shared_data_pool;
126 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
132 vls_shared_data_index (vls_shared_data_t * vls_shd)
134 return vls_shd - vlsm->shared_data_pool;
138 vls_shared_data_get (u32 shd_index)
140 if (pool_is_free_index (vlsm->shared_data_pool, shd_index))
142 return pool_elt_at_index (vlsm->shared_data_pool, shd_index);
146 vls_shared_data_free (u32 shd_index)
148 vls_shared_data_t *vls_shd;
150 clib_rwlock_writer_lock (&vlsm->shared_data_lock);
151 vls_shd = vls_shared_data_get (shd_index);
152 clib_spinlock_free (&vls_shd->lock);
153 clib_bitmap_free (vls_shd->listeners);
154 vec_free (vls_shd->workers_subscribed);
155 pool_put (vlsm->shared_data_pool, vls_shd);
156 clib_rwlock_writer_unlock (&vlsm->shared_data_lock);
160 vls_shared_data_pool_rlock (void)
162 clib_rwlock_reader_lock (&vlsm->shared_data_lock);
166 vls_shared_data_pool_runlock (void)
168 clib_rwlock_reader_unlock (&vlsm->shared_data_lock);
172 vls_mt_table_rlock (void)
174 if (vlsl->vls_mt_n_threads > 1)
175 clib_rwlock_reader_lock (&vlsm->vls_table_lock);
179 vls_mt_table_runlock (void)
181 if (vlsl->vls_mt_n_threads > 1)
182 clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
186 vls_mt_table_wlock (void)
188 if (vlsl->vls_mt_n_threads > 1)
189 clib_rwlock_writer_lock (&vlsm->vls_table_lock);
193 vls_mt_table_wunlock (void)
195 if (vlsl->vls_mt_n_threads > 1)
196 clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
209 VLS_MT_LOCK_MQ = 1 << 0,
210 VLS_MT_LOCK_SPOOL = 1 << 1
211 } vls_mt_lock_type_t;
216 vlsl->vls_mt_n_threads += 1;
218 /* If multi-thread workers are supported, for each new thread register a new
219 * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
220 * update the vcl worker's thread local worker index variable */
221 if (vls_mt_wrk_supported ())
222 vls_register_vcl_worker ();
224 vcl_set_worker_index (vlsl->vls_wrk_index);
228 vls_mt_mq_lock (void)
230 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
234 vls_mt_mq_unlock (void)
236 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
240 vls_mt_spool_lock (void)
242 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
246 vls_mt_create_unlock (void)
248 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
252 vls_mt_locks_init (void)
254 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
255 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
259 vls_is_shared (vcl_locked_session_t * vls)
261 return (vls->shared_data_index != ~0);
265 vls_lock (vcl_locked_session_t * vls)
267 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
268 clib_spinlock_lock (&vls->lock);
272 vls_unlock (vcl_locked_session_t * vls)
274 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
275 clib_spinlock_unlock (&vls->lock);
278 static inline vcl_session_handle_t
279 vls_to_sh (vcl_locked_session_t * vls)
281 return vcl_session_handle_from_index (vls->session_index);
284 static inline vcl_session_handle_t
285 vls_to_sh_tu (vcl_locked_session_t * vls)
287 vcl_session_handle_t sh;
288 sh = vls_to_sh (vls);
289 vls_mt_table_runlock ();
293 static vls_worker_t *
294 vls_worker_get_current (void)
296 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
300 vls_worker_alloc (void)
304 pool_get_zero (vlsm->workers, wrk);
305 wrk->wrk_index = vcl_get_worker_index ();
309 vls_worker_free (vls_worker_t * wrk)
311 hash_free (wrk->session_handle_to_vlsh_table);
312 pool_free (wrk->vls_pool);
313 pool_put (vlsm->workers, wrk);
316 static vls_worker_t *
317 vls_worker_get (u32 wrk_index)
319 if (pool_is_free_index (vlsm->workers, wrk_index))
321 return pool_elt_at_index (vlsm->workers, wrk_index);
325 vls_alloc (vcl_session_handle_t sh)
327 vls_worker_t *wrk = vls_worker_get_current ();
328 vcl_locked_session_t *vls;
330 vls_mt_table_wlock ();
332 pool_get_zero (wrk->vls_pool, vls);
333 vls->session_index = vppcom_session_index (sh);
334 vls->worker_index = vppcom_session_worker (sh);
335 vls->vls_index = vls - wrk->vls_pool;
336 vls->shared_data_index = ~0;
337 hash_set (wrk->session_handle_to_vlsh_table, sh, vls->vls_index);
338 if (vls_mt_wrk_supported ())
340 hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
342 vls->owner_vcl_wrk_index = vls->worker_index;
344 clib_spinlock_init (&vls->lock);
346 vls_mt_table_wunlock ();
347 return vls->vls_index;
350 static vcl_locked_session_t *
351 vls_get (vls_handle_t vlsh)
353 vls_worker_t *wrk = vls_worker_get_current ();
354 if (pool_is_free_index (wrk->vls_pool, vlsh))
356 return pool_elt_at_index (wrk->vls_pool, vlsh);
360 vls_free (vcl_locked_session_t * vls)
362 vls_worker_t *wrk = vls_worker_get_current ();
365 hash_unset (wrk->session_handle_to_vlsh_table,
366 vcl_session_handle_from_index (vls->session_index));
367 clib_spinlock_free (&vls->lock);
368 pool_put (wrk->vls_pool, vls);
371 static vcl_locked_session_t *
372 vls_get_and_lock (vls_handle_t vlsh)
374 vls_worker_t *wrk = vls_worker_get_current ();
375 vcl_locked_session_t *vls;
376 if (pool_is_free_index (wrk->vls_pool, vlsh))
378 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
383 static vcl_locked_session_t *
384 vls_get_w_dlock (vls_handle_t vlsh)
386 vcl_locked_session_t *vls;
387 vls_mt_table_rlock ();
388 vls = vls_get_and_lock (vlsh);
390 vls_mt_table_runlock ();
395 vls_get_and_unlock (vls_handle_t vlsh)
397 vcl_locked_session_t *vls;
398 vls_mt_table_rlock ();
399 vls = vls_get (vlsh);
401 vls_mt_table_runlock ();
405 vls_dunlock (vcl_locked_session_t * vls)
408 vls_mt_table_runlock ();
411 static vcl_locked_session_t *
412 vls_session_get (vls_worker_t * wrk, u32 vls_index)
414 if (pool_is_free_index (wrk->vls_pool, vls_index))
416 return pool_elt_at_index (wrk->vls_pool, vls_index);
420 vlsh_to_sh (vls_handle_t vlsh)
422 vcl_locked_session_t *vls;
425 vls = vls_get_w_dlock (vlsh);
427 return INVALID_SESSION_ID;
428 rv = vls_to_sh (vls);
434 vlsh_to_session_index (vls_handle_t vlsh)
436 vcl_session_handle_t sh;
437 sh = vlsh_to_sh (vlsh);
438 return vppcom_session_index (sh);
442 vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
444 vls_worker_t *wrk = vls_worker_get_current ();
446 vlshp = hash_get (wrk->session_handle_to_vlsh_table,
447 vcl_session_handle_from_wrk_session_index (session_index,
449 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
453 vls_session_index_to_vlsh (uint32_t session_index)
457 vls_mt_table_rlock ();
458 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
459 vls_mt_table_runlock ();
465 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
467 vls_shared_data_t *vls_shd;
470 if (vls->shared_data_index == ~0)
473 vls_shared_data_pool_rlock ();
475 vls_shd = vls_shared_data_get (vls->shared_data_index);
476 clib_spinlock_lock (&vls_shd->lock);
478 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
479 if (vls_shd->workers_subscribed[i] == wrk_index)
481 clib_spinlock_unlock (&vls_shd->lock);
482 vls_shared_data_pool_runlock ();
485 clib_spinlock_unlock (&vls_shd->lock);
487 vls_shared_data_pool_runlock ();
492 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
494 vls_shared_data_t *vls_shd;
496 if (vls->shared_data_index == ~0)
498 clib_warning ("not a shared session");
502 vls_shared_data_pool_rlock ();
504 vls_shd = vls_shared_data_get (vls->shared_data_index);
506 clib_spinlock_lock (&vls_shd->lock);
507 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
508 clib_spinlock_unlock (&vls_shd->lock);
510 vls_shared_data_pool_runlock ();
514 vls_shared_get_owner (vcl_locked_session_t * vls)
516 vls_shared_data_t *vls_shd;
519 vls_shared_data_pool_rlock ();
521 vls_shd = vls_shared_data_get (vls->shared_data_index);
522 owner_wrk = vls_shd->owner_wrk_index;
524 vls_shared_data_pool_runlock ();
530 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
532 vls_shared_data_t *vls_shd;
535 if (vls->shared_data_index == ~0)
537 clib_warning ("not a shared session");
541 vls_shared_data_pool_rlock ();
543 vls_shd = vls_shared_data_get (vls->shared_data_index);
545 clib_spinlock_lock (&vls_shd->lock);
546 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
547 clib_spinlock_unlock (&vls_shd->lock);
549 vls_shared_data_pool_runlock ();
551 return (is_set == 1);
555 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
557 vppcom_session_listen (vls_to_sh (vls), ~0);
558 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
562 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
567 wrk = vcl_worker_get (wrk_index);
568 s = vcl_session_get (wrk, vls->session_index);
569 if (s->session_state != STATE_LISTEN)
571 vcl_send_session_unlisten (wrk, s);
572 s->session_state = STATE_LISTEN_NO_MQ;
573 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
577 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
582 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
584 if (vls_shd->workers_subscribed[i] == wrk_index)
591 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
593 vls_shared_data_t *vls_shd;
594 int do_disconnect, pos;
598 if (vls->shared_data_index == ~0)
601 s = vcl_session_get (wrk, vls->session_index);
602 if (s->session_state == STATE_LISTEN)
603 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
605 vls_shared_data_pool_rlock ();
607 vls_shd = vls_shared_data_get (vls->shared_data_index);
608 clib_spinlock_lock (&vls_shd->lock);
610 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
613 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
619 * Unsubscribe from share data and fifos
623 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
624 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
626 vec_del1 (vls_shd->workers_subscribed, pos);
631 n_subscribers = vec_len (vls_shd->workers_subscribed);
632 do_disconnect = s->session_state == STATE_LISTEN || !n_subscribers;
633 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
636 * No subscriber left, cleanup shared data
640 u32 shd_index = vls_shared_data_index (vls_shd);
642 clib_spinlock_unlock (&vls_shd->lock);
643 vls_shared_data_pool_runlock ();
645 vls_shared_data_free (shd_index);
647 /* All locks have been dropped */
651 /* Return, if this is not the owning worker */
652 if (vls_shd->owner_wrk_index != wrk->wrk_index)
655 ASSERT (vec_len (vls_shd->workers_subscribed));
658 * Check if we can change owner or close
660 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
661 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
663 /* XXX is this still needed? */
664 if (vec_len (vls_shd->workers_subscribed) > 1)
665 clib_warning ("more workers need to be updated");
669 clib_spinlock_unlock (&vls_shd->lock);
670 vls_shared_data_pool_runlock ();
676 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
678 vls_shared_data_t *vls_shd;
680 u32 vls_shd_index = vls_shared_data_alloc ();
682 vls_shared_data_pool_rlock ();
684 vls_shd = vls_shared_data_get (vls_shd_index);
685 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
686 vls->shared_data_index = vls_shd_index;
687 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
689 vls_shared_data_pool_runlock ();
693 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
695 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
696 vls_shared_data_t *vls_shd;
699 s = vcl_session_get (vcl_wrk, vls->session_index);
702 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
703 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
707 ASSERT (vls->shared_data_index != ~0);
709 /* Reinit session lock */
710 clib_spinlock_init (&vls->lock);
712 vls_shared_data_pool_rlock ();
714 vls_shd = vls_shared_data_get (vls->shared_data_index);
716 clib_spinlock_lock (&vls_shd->lock);
717 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
718 clib_spinlock_unlock (&vls_shd->lock);
720 vls_shared_data_pool_runlock ();
724 svm_fifo_add_subscriber (s->rx_fifo, vcl_wrk->vpp_wrk_index);
725 svm_fifo_add_subscriber (s->tx_fifo, vcl_wrk->vpp_wrk_index);
727 else if (s->session_state == STATE_LISTEN)
729 s->session_state = STATE_LISTEN_NO_MQ;
734 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
736 vcl_locked_session_t *vls, *parent_vls;
739 pool_foreach (vls, vls_wrk->vls_pool, ({
740 /* Initialize sharing on parent session */
741 if (vls->shared_data_index == ~0)
743 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
744 vls_init_share_session (vls_parent_wrk, parent_vls);
745 vls->shared_data_index = parent_vls->shared_data_index;
747 vls_share_session (vls_wrk, vls);
753 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
755 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
756 vcl_worker_t *wrk = vcl_worker_get_current ();
757 u32 vls_index, session_index, wrk_index;
758 vcl_session_handle_t sh;
763 wrk->vpp_event_queues = vec_dup (parent_wrk->vpp_event_queues);
764 wrk->sessions = pool_dup (parent_wrk->sessions);
765 wrk->session_index_by_vpp_handles =
766 hash_dup (parent_wrk->session_index_by_vpp_handles);
771 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
773 hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
775 vcl_session_handle_parse (sh, &wrk_index, &session_index);
776 hash_set (vls_wrk->session_handle_to_vlsh_table,
777 vcl_session_handle_from_index (session_index), vls_index);
780 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
782 vls_share_sessions (vls_parent_wrk, vls_wrk);
786 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
788 vcl_worker_t *wrk = vcl_worker_get_current ();
789 vcl_session_t *s = 0;
794 s = vcl_session_get (wrk, vls->session_index);
795 if (PREDICT_FALSE (!s))
797 is_nonblk = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
804 is_nonblk = vcl_session_read_ready (s) != 0;
808 *locks_acq |= VLS_MT_LOCK_MQ;
811 case VLS_MT_OP_WRITE:
814 is_nonblk = vcl_session_write_ready (s) != 0;
818 *locks_acq |= VLS_MT_LOCK_MQ;
821 case VLS_MT_OP_XPOLL:
823 *locks_acq |= VLS_MT_LOCK_MQ;
825 case VLS_MT_OP_SPOOL:
826 vls_mt_spool_lock ();
827 *locks_acq |= VLS_MT_LOCK_SPOOL;
835 vls_mt_rel_locks (int locks_acq)
837 if (locks_acq & VLS_MT_LOCK_MQ)
839 if (locks_acq & VLS_MT_LOCK_SPOOL)
840 vls_mt_create_unlock ();
844 vls_mt_session_should_migrate (vcl_locked_session_t * vls)
846 return (vls_mt_wrk_supported ()
847 && vls->worker_index != vcl_get_worker_index ());
851 vls_mt_session_migrate (vcl_locked_session_t * vls)
853 u32 wrk_index = vcl_get_worker_index ();
856 vcl_session_t *session;
859 ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
862 * VCL session on current vcl worker already allocated. Update current
863 * owner worker and index and return
865 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
867 vls->worker_index = wrk_index;
868 vls->session_index = (u32) p[0];
873 * Ask vcl worker that owns the original vcl session to clone it into
874 * current vcl worker session pool
877 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
878 vls->owner_vcl_wrk_index)))
880 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
885 src_sid = (u32) p[0];
886 wrk = vcl_worker_get_current ();
887 session = vcl_session_alloc (wrk);
888 sid = session->session_index;
889 vls_send_clone_and_share_rpc (wrk, vls, sid, vls_get_worker_index (),
890 vls->owner_vcl_wrk_index, vls->vls_index,
892 session->session_index = sid;
893 vls->worker_index = wrk_index;
894 vls->session_index = sid;
895 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
896 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
897 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
899 if (PREDICT_FALSE (session->is_vep && session->vep.next_sh != ~0))
901 /* TODO: rollback? */
902 VERR ("can't migrate nonempty epoll session");
906 else if (PREDICT_FALSE (!session->is_vep &&
907 session->session_state != STATE_CLOSED))
909 /* TODO: rollback? */
910 VERR ("migrate NOT supported, session_status (%u)",
911 session->session_state);
920 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
924 #define vls_mt_guard(_vls, _op) \
925 int _locks_acq = 0; \
926 if (vls_mt_wrk_supported ()) \
928 if (PREDICT_FALSE (_vls \
929 && ((vcl_locked_session_t *)_vls)->worker_index != \
930 vcl_get_worker_index ())) \
931 vls_mt_session_migrate (_vls); \
935 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
936 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
939 #define vls_mt_unguard() \
940 if (PREDICT_FALSE (_locks_acq)) \
941 vls_mt_rel_locks (_locks_acq)
944 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
946 vcl_locked_session_t *vls;
950 if (!(vls = vls_get_w_dlock (vlsh)))
951 return VPPCOM_EBADFD;
953 vls_mt_guard (vls, VLS_MT_OP_WRITE);
954 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
956 vls_get_and_unlock (vlsh);
961 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
963 vcl_locked_session_t *vls;
967 if (!(vls = vls_get_w_dlock (vlsh)))
968 return VPPCOM_EBADFD;
969 vls_mt_guard (vls, VLS_MT_OP_WRITE);
970 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
972 vls_get_and_unlock (vlsh);
977 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
980 vcl_locked_session_t *vls;
984 if (!(vls = vls_get_w_dlock (vlsh)))
985 return VPPCOM_EBADFD;
986 vls_mt_guard (vls, VLS_MT_OP_WRITE);
987 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
989 vls_get_and_unlock (vlsh);
994 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
996 vcl_locked_session_t *vls;
1000 if (!(vls = vls_get_w_dlock (vlsh)))
1001 return VPPCOM_EBADFD;
1002 vls_mt_guard (vls, VLS_MT_OP_READ);
1003 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
1005 vls_get_and_unlock (vlsh);
1010 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1011 vppcom_endpt_t * ep)
1013 vcl_locked_session_t *vls;
1017 if (!(vls = vls_get_w_dlock (vlsh)))
1018 return VPPCOM_EBADFD;
1019 vls_mt_guard (vls, VLS_MT_OP_READ);
1020 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1023 vls_get_and_unlock (vlsh);
1028 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1030 vcl_locked_session_t *vls;
1034 if (!(vls = vls_get_w_dlock (vlsh)))
1035 return VPPCOM_EBADFD;
1036 if (vls_mt_session_should_migrate (vls))
1037 vls_mt_session_migrate (vls);
1038 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1039 vls_get_and_unlock (vlsh);
1044 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1046 vcl_locked_session_t *vls;
1050 if (!(vls = vls_get_w_dlock (vlsh)))
1051 return VPPCOM_EBADFD;
1052 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1053 vls_get_and_unlock (vlsh);
1058 vls_listen (vls_handle_t vlsh, int q_len)
1060 vcl_locked_session_t *vls;
1064 if (!(vls = vls_get_w_dlock (vlsh)))
1065 return VPPCOM_EBADFD;
1066 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1067 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
1069 vls_get_and_unlock (vlsh);
1074 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1076 vcl_locked_session_t *vls;
1080 if (!(vls = vls_get_w_dlock (vlsh)))
1081 return VPPCOM_EBADFD;
1082 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1083 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
1085 vls_get_and_unlock (vlsh);
1090 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1092 vcl_worker_t *wrk = vcl_worker_get_current ();
1096 if (vls_mt_wrk_supported ())
1099 s = vcl_session_get (wrk, vls->session_index);
1100 switch (s->session_state)
1105 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
1108 vls_listener_wrk_stop_listen (vls, vls->worker_index);
1110 case STATE_LISTEN_NO_MQ:
1114 /* Register worker as listener */
1115 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
1117 /* If owner worker did not attempt to accept/xpoll on the session,
1118 * force a listen stop for it, since it may not be interested in
1119 * accepting new sessions.
1120 * This is pretty much a hack done to give app workers the illusion
1121 * that it is fine to listen and not accept new sessions for a
1122 * given listener. Without it, we would accumulate unhandled
1123 * accepts on the passive worker message queue. */
1124 owner_wrk = vls_shared_get_owner (vls);
1125 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1126 vls_listener_wrk_stop_listen (vls, owner_wrk);
1134 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1136 vls_handle_t accepted_vlsh;
1137 vcl_locked_session_t *vls;
1141 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1142 return VPPCOM_EBADFD;
1143 if (vcl_n_workers () > 1)
1144 vls_mp_checks (vls, 1 /* is_add */ );
1145 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1146 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
1148 vls_get_and_unlock (listener_vlsh);
1151 accepted_vlsh = vls_alloc (sh);
1152 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1153 vppcom_session_close (sh);
1154 return accepted_vlsh;
1158 vls_create (uint8_t proto, uint8_t is_nonblocking)
1160 vcl_session_handle_t sh;
1164 vls_mt_guard (0, VLS_MT_OP_SPOOL);
1165 sh = vppcom_session_create (proto, is_nonblocking);
1167 if (sh == INVALID_SESSION_ID)
1168 return VLS_INVALID_HANDLE;
1170 vlsh = vls_alloc (sh);
1171 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1172 vppcom_session_close (sh);
1178 vls_mt_session_cleanup (vcl_locked_session_t * vls)
1180 u32 session_index, wrk_index, current_vcl_wrk;
1181 vcl_worker_t *wrk = vcl_worker_get_current ();
1183 ASSERT (vls_mt_wrk_supported ());
1185 current_vcl_wrk = vcl_get_worker_index ();
1188 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1190 if (current_vcl_wrk != wrk_index)
1191 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
1194 hash_free (vls->vcl_wrk_index_to_session_index);
1198 vls_close (vls_handle_t vlsh)
1200 vcl_locked_session_t *vls;
1204 vls_mt_table_wlock ();
1206 vls = vls_get_and_lock (vlsh);
1209 vls_mt_table_wunlock ();
1210 return VPPCOM_EBADFD;
1213 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1215 if (vls_is_shared (vls))
1216 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1218 rv = vppcom_session_close (vls_to_sh (vls));
1220 if (vls_mt_wrk_supported ())
1221 vls_mt_session_cleanup (vls);
1226 vls_mt_table_wunlock ();
1232 vls_epoll_create (void)
1234 vcl_session_handle_t sh;
1239 sh = vppcom_epoll_create ();
1240 if (sh == INVALID_SESSION_ID)
1241 return VLS_INVALID_HANDLE;
1243 vlsh = vls_alloc (sh);
1244 if (vlsh == VLS_INVALID_HANDLE)
1245 vppcom_session_close (sh);
1251 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1253 if (vcl_n_workers () <= 1)
1255 vlsl->epoll_mp_check = 1;
1259 if (op == EPOLL_CTL_MOD)
1262 vlsl->epoll_mp_check = 1;
1263 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1267 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1268 struct epoll_event *event)
1270 vcl_locked_session_t *ep_vls, *vls;
1271 vcl_session_handle_t ep_sh, sh;
1275 vls_mt_table_rlock ();
1276 ep_vls = vls_get_and_lock (ep_vlsh);
1277 vls = vls_get_and_lock (vlsh);
1279 if (vls_mt_session_should_migrate (ep_vls))
1280 vls_mt_session_migrate (ep_vls);
1282 ep_sh = vls_to_sh (ep_vls);
1283 sh = vls_to_sh (vls);
1285 if (PREDICT_FALSE (!vlsl->epoll_mp_check))
1286 vls_epoll_ctl_mp_checks (vls, op);
1288 vls_mt_table_runlock ();
1290 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1292 vls_mt_table_rlock ();
1293 ep_vls = vls_get (ep_vlsh);
1294 vls = vls_get (vlsh);
1296 vls_unlock (ep_vls);
1297 vls_mt_table_runlock ();
1302 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1303 int maxevents, double wait_for_time)
1305 vcl_locked_session_t *vls;
1309 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1310 return VPPCOM_EBADFD;
1311 vls_mt_guard (0, VLS_MT_OP_XPOLL);
1312 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1315 vls_get_and_unlock (ep_vlsh);
1320 vls_select_mp_checks (vcl_si_set * read_map)
1322 vcl_locked_session_t *vls;
1327 if (vcl_n_workers () <= 1)
1329 vlsl->select_mp_check = 1;
1336 vlsl->select_mp_check = 1;
1337 wrk = vcl_worker_get_current ();
1340 clib_bitmap_foreach (si, read_map, ({
1341 s = vcl_session_get (wrk, si);
1342 if (s->session_state == STATE_LISTEN)
1344 vls = vls_get (vls_session_index_to_vlsh (si));
1345 vls_mp_checks (vls, 1 /* is_add */);
1352 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1353 vcl_si_set * except_map, double wait_for_time)
1358 vls_mt_guard (0, VLS_MT_OP_XPOLL);
1359 if (PREDICT_FALSE (!vlsl->select_mp_check))
1360 vls_select_mp_checks (read_map);
1361 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1367 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1369 u32 current_wrk, is_current;
1370 vcl_locked_session_t *vls;
1373 if (pool_elts (vcm->workers) <= 1)
1376 current_wrk = vcl_get_worker_index ();
1377 is_current = current_wrk == wrk->wrk_index;
1380 pool_foreach (s, wrk->sessions, ({
1381 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
1382 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1383 vls_unshare_session (vls, wrk);
1389 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1391 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1393 /* Unshare sessions and also cleanup worker since child may have
1394 * called _exit () and therefore vcl may not catch the event */
1395 vls_unshare_vcl_worker_sessions (wrk);
1396 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
1398 vls_worker_free (vls_wrk);
1402 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1404 vcl_worker_t *sub_child;
1407 if (child_wrk->forked_child != ~0)
1409 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1412 /* Wait a bit, maybe the process is going away */
1413 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1415 if (kill (sub_child->current_pid, 0) < 0)
1416 vls_cleanup_forked_child (child_wrk, sub_child);
1419 vls_cleanup_vcl_worker (child_wrk);
1420 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1421 wrk->forked_child = ~0;
1424 static struct sigaction old_sa;
1427 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1429 vcl_worker_t *wrk, *child_wrk;
1431 if (vcl_get_worker_index () == ~0)
1434 if (sigaction (SIGCHLD, &old_sa, 0))
1436 VERR ("couldn't restore sigchld");
1440 wrk = vcl_worker_get_current ();
1441 if (wrk->forked_child == ~0)
1444 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1448 if (si && si->si_pid != child_wrk->current_pid)
1450 VDBG (0, "unexpected child pid %u", si->si_pid);
1453 vls_cleanup_forked_child (wrk, child_wrk);
1456 if (old_sa.sa_flags & SA_SIGINFO)
1458 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1459 fn (signum, si, uc);
1463 void (*fn) (int) = old_sa.sa_handler;
1470 vls_incercept_sigchld ()
1472 struct sigaction sa;
1473 clib_memset (&sa, 0, sizeof (sa));
1474 sa.sa_sigaction = vls_intercept_sigchld_handler;
1475 sa.sa_flags = SA_SIGINFO;
1476 if (sigaction (SIGCHLD, &sa, &old_sa))
1478 VERR ("couldn't intercept sigchld");
1484 vls_app_pre_fork (void)
1486 vls_incercept_sigchld ();
1487 vcl_flush_mq_events ();
1491 vls_app_fork_child_handler (void)
1493 vcl_worker_t *parent_wrk;
1494 int parent_wrk_index;
1496 parent_wrk_index = vcl_get_worker_index ();
1497 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1503 vcl_set_worker_index (~0);
1506 * Allocate and register vcl worker with vpp
1508 if (vppcom_worker_register ())
1510 VERR ("couldn't register new worker!");
1515 * Allocate/initialize vls worker and share sessions
1517 vls_worker_alloc ();
1518 parent_wrk = vcl_worker_get (parent_wrk_index);
1519 vls_worker_copy_on_fork (parent_wrk);
1520 parent_wrk->forked_child = vcl_get_worker_index ();
1522 /* Reset number of threads and set wrk index */
1523 vlsl->vls_mt_n_threads = 0;
1524 vlsl->vls_wrk_index = vcl_get_worker_index ();
1525 vlsl->select_mp_check = 0;
1526 vlsl->epoll_mp_check = 0;
1527 vls_mt_locks_init ();
1529 VDBG (0, "forked child main worker initialized");
1534 vls_app_fork_parent_handler (void)
1537 while (vcm->forking)
1544 vls_worker_t *wrk = vls_worker_get_current ();
1546 /* Unshare the sessions. VCL will clean up the worker */
1547 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1548 vls_worker_free (wrk);
1552 vls_clone_and_share_rpc_handler (void *args)
1554 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1555 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1556 vcl_locked_session_t *vls, *dst_vls;
1557 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
1558 vcl_session_t *s, *dst_s;
1560 vls = vls_session_get (wrk, msg->vls_index);
1562 if (!vls_mt_wrk_supported ())
1563 vls_init_share_session (wrk, vls);
1565 s = vcl_session_get (vcl_wrk, msg->session_index);
1566 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1567 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1568 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1569 dst_vls->shared_data_index = vls->shared_data_index;
1570 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
1571 clib_memcpy (dst_s, s, sizeof (*s));
1573 dst_vcl_wrk->rpc_done = 1;
1575 VDBG (1, "proces session clone of worker (session): %u (%u) -> %u (%u)",
1576 vcl_wrk->wrk_index, msg->session_index, dst_vcl_wrk->wrk_index,
1577 msg->origin_session_index);
1581 vls_session_cleanup_rpc_handler (void *args)
1583 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1584 vcl_worker_t *wrk = vcl_worker_get_current ();
1585 vcl_worker_t *dst_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1587 vppcom_session_close (vcl_session_handle_from_index (msg->session_index));
1589 dst_wrk->rpc_done = 1;
1591 VDBG (1, "proces session cleanup of worker (session): %u (%u) from %u ()",
1592 wrk->wrk_index, msg->session_index, dst_wrk->wrk_index);
1596 vls_rpc_handler (void *args)
1598 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1601 case VLS_RPC_CLONE_AND_SHARE:
1602 vls_clone_and_share_rpc_handler (msg->data);
1604 case VLS_RPC_SESS_CLEANUP:
1605 vls_session_cleanup_rpc_handler (msg->data);
1613 vls_send_clone_and_share_rpc (vcl_worker_t * wrk,
1614 vcl_locked_session_t * vls, u32 session_index,
1615 u32 vls_wrk_index, u32 dst_wrk_index,
1616 u32 dst_vls_index, u32 dst_session_index)
1618 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1619 vls_clone_and_share_msg_t *msg;
1623 rpc = (vls_rpc_msg_t *) & data;
1624 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1625 msg = (vls_clone_and_share_msg_t *) & rpc->data;
1626 msg->origin_vls_wrk = vls_wrk_index;
1627 msg->origin_vls_index = vls->vls_index;
1628 msg->origin_vcl_wrk = wrk->wrk_index;
1629 msg->origin_session_index = session_index;
1630 msg->vls_index = dst_vls_index;
1631 msg->session_index = dst_session_index;
1634 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1636 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
1637 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1638 msg->origin_session_index, ret);
1639 while (!ret && !wrk->rpc_done)
1644 vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1645 u32 dst_wrk_index, u32 dst_session_index)
1647 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1648 vls_sess_cleanup_msg_t *msg;
1652 rpc = (vls_rpc_msg_t *) & data;
1653 rpc->type = VLS_RPC_SESS_CLEANUP;
1654 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1655 msg->origin_vcl_wrk = wrk->wrk_index;
1656 msg->session_index = dst_session_index;
1659 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1661 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
1662 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
1663 while (!ret && !wrk->rpc_done)
1668 vls_app_create (char *app_name)
1672 if ((rv = vppcom_app_create (app_name)))
1675 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1676 clib_memset (vlsm, 0, sizeof (*vlsm));
1677 clib_rwlock_init (&vlsm->vls_table_lock);
1678 clib_rwlock_init (&vlsm->shared_data_lock);
1679 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1681 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1682 vls_app_fork_child_handler);
1683 atexit (vls_app_exit);
1684 vls_worker_alloc ();
1685 vlsl->vls_wrk_index = vcl_get_worker_index ();
1686 vls_mt_locks_init ();
1687 vcm->wrk_rpc_fn = vls_rpc_handler;
1692 vls_use_eventfd (void)
1694 return vcm->cfg.use_mq_eventfd;
1698 vls_mt_wrk_supported (void)
1700 return vcm->cfg.mt_wrk_supported;
1704 vls_use_real_epoll (void)
1706 if (vcl_get_worker_index () == ~0)
1709 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1713 vls_register_vcl_worker (void)
1715 if (vppcom_worker_register () != VPPCOM_OK)
1717 VERR ("failed to register worker");
1723 * fd.io coding-style-patch-verification: ON
1726 * eval: (c-set-style "gnu")