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 ())
223 if (vppcom_worker_register () != VPPCOM_OK)
224 VERR ("failed to register worker");
227 vcl_set_worker_index (vlsl->vls_wrk_index);
231 vls_mt_mq_lock (void)
233 pthread_mutex_lock (&vlsl->vls_mt_mq_mlock);
237 vls_mt_mq_unlock (void)
239 pthread_mutex_unlock (&vlsl->vls_mt_mq_mlock);
243 vls_mt_spool_lock (void)
245 pthread_mutex_lock (&vlsl->vls_mt_spool_mlock);
249 vls_mt_create_unlock (void)
251 pthread_mutex_unlock (&vlsl->vls_mt_spool_mlock);
255 vls_mt_locks_init (void)
257 pthread_mutex_init (&vlsl->vls_mt_mq_mlock, NULL);
258 pthread_mutex_init (&vlsl->vls_mt_spool_mlock, NULL);
262 vls_is_shared (vcl_locked_session_t * vls)
264 return (vls->shared_data_index != ~0);
268 vls_lock (vcl_locked_session_t * vls)
270 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
271 clib_spinlock_lock (&vls->lock);
275 vls_unlock (vcl_locked_session_t * vls)
277 if ((vlsl->vls_mt_n_threads > 1) || vls_is_shared (vls))
278 clib_spinlock_unlock (&vls->lock);
281 static inline vcl_session_handle_t
282 vls_to_sh (vcl_locked_session_t * vls)
284 return vcl_session_handle_from_index (vls->session_index);
287 static inline vcl_session_handle_t
288 vls_to_sh_tu (vcl_locked_session_t * vls)
290 vcl_session_handle_t sh;
291 sh = vls_to_sh (vls);
292 vls_mt_table_runlock ();
296 static vls_worker_t *
297 vls_worker_get_current (void)
299 return pool_elt_at_index (vlsm->workers, vls_get_worker_index ());
303 vls_worker_alloc (void)
307 pool_get_zero (vlsm->workers, wrk);
308 wrk->wrk_index = vcl_get_worker_index ();
312 vls_worker_free (vls_worker_t * wrk)
314 hash_free (wrk->session_handle_to_vlsh_table);
315 pool_free (wrk->vls_pool);
316 pool_put (vlsm->workers, wrk);
319 static vls_worker_t *
320 vls_worker_get (u32 wrk_index)
322 if (pool_is_free_index (vlsm->workers, wrk_index))
324 return pool_elt_at_index (vlsm->workers, wrk_index);
328 vls_alloc (vcl_session_handle_t sh)
330 vls_worker_t *wrk = vls_worker_get_current ();
331 vcl_locked_session_t *vls;
333 vls_mt_table_wlock ();
335 pool_get_zero (wrk->vls_pool, vls);
336 vls->session_index = vppcom_session_index (sh);
337 vls->worker_index = vppcom_session_worker (sh);
338 vls->vls_index = vls - wrk->vls_pool;
339 vls->shared_data_index = ~0;
340 hash_set (wrk->session_handle_to_vlsh_table, sh, vls->vls_index);
341 if (vls_mt_wrk_supported ())
343 hash_set (vls->vcl_wrk_index_to_session_index, vls->worker_index,
345 vls->owner_vcl_wrk_index = vls->worker_index;
347 clib_spinlock_init (&vls->lock);
349 vls_mt_table_wunlock ();
350 return vls->vls_index;
353 static vcl_locked_session_t *
354 vls_get (vls_handle_t vlsh)
356 vls_worker_t *wrk = vls_worker_get_current ();
357 if (pool_is_free_index (wrk->vls_pool, vlsh))
359 return pool_elt_at_index (wrk->vls_pool, vlsh);
363 vls_free (vcl_locked_session_t * vls)
365 vls_worker_t *wrk = vls_worker_get_current ();
368 hash_unset (wrk->session_handle_to_vlsh_table,
369 vcl_session_handle_from_index (vls->session_index));
370 clib_spinlock_free (&vls->lock);
371 pool_put (wrk->vls_pool, vls);
374 static vcl_locked_session_t *
375 vls_get_and_lock (vls_handle_t vlsh)
377 vls_worker_t *wrk = vls_worker_get_current ();
378 vcl_locked_session_t *vls;
379 if (pool_is_free_index (wrk->vls_pool, vlsh))
381 vls = pool_elt_at_index (wrk->vls_pool, vlsh);
386 static vcl_locked_session_t *
387 vls_get_w_dlock (vls_handle_t vlsh)
389 vcl_locked_session_t *vls;
390 vls_mt_table_rlock ();
391 vls = vls_get_and_lock (vlsh);
393 vls_mt_table_runlock ();
398 vls_get_and_unlock (vls_handle_t vlsh)
400 vcl_locked_session_t *vls;
401 vls_mt_table_rlock ();
402 vls = vls_get (vlsh);
404 vls_mt_table_runlock ();
408 vls_dunlock (vcl_locked_session_t * vls)
411 vls_mt_table_runlock ();
414 static vcl_locked_session_t *
415 vls_session_get (vls_worker_t * wrk, u32 vls_index)
417 if (pool_is_free_index (wrk->vls_pool, vls_index))
419 return pool_elt_at_index (wrk->vls_pool, vls_index);
423 vlsh_to_sh (vls_handle_t vlsh)
425 vcl_locked_session_t *vls;
428 vls = vls_get_w_dlock (vlsh);
430 return INVALID_SESSION_ID;
431 rv = vls_to_sh (vls);
437 vlsh_to_session_index (vls_handle_t vlsh)
439 vcl_session_handle_t sh;
440 sh = vlsh_to_sh (vlsh);
441 return vppcom_session_index (sh);
445 vls_si_wi_to_vlsh (u32 session_index, u32 vcl_wrk_index)
447 vls_worker_t *wrk = vls_worker_get_current ();
449 vlshp = hash_get (wrk->session_handle_to_vlsh_table,
450 vcl_session_handle_from_wrk_session_index (session_index,
452 return vlshp ? *vlshp : VLS_INVALID_HANDLE;
456 vls_session_index_to_vlsh (uint32_t session_index)
460 vls_mt_table_rlock ();
461 vlsh = vls_si_wi_to_vlsh (session_index, vcl_get_worker_index ());
462 vls_mt_table_runlock ();
468 vls_is_shared_by_wrk (vcl_locked_session_t * vls, u32 wrk_index)
470 vls_shared_data_t *vls_shd;
473 if (vls->shared_data_index == ~0)
476 vls_shared_data_pool_rlock ();
478 vls_shd = vls_shared_data_get (vls->shared_data_index);
479 clib_spinlock_lock (&vls_shd->lock);
481 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
482 if (vls_shd->workers_subscribed[i] == wrk_index)
484 clib_spinlock_unlock (&vls_shd->lock);
485 vls_shared_data_pool_runlock ();
488 clib_spinlock_unlock (&vls_shd->lock);
490 vls_shared_data_pool_runlock ();
495 vls_listener_wrk_set (vcl_locked_session_t * vls, u32 wrk_index, u8 is_active)
497 vls_shared_data_t *vls_shd;
499 if (vls->shared_data_index == ~0)
501 clib_warning ("not a shared session");
505 vls_shared_data_pool_rlock ();
507 vls_shd = vls_shared_data_get (vls->shared_data_index);
509 clib_spinlock_lock (&vls_shd->lock);
510 clib_bitmap_set (vls_shd->listeners, wrk_index, is_active);
511 clib_spinlock_unlock (&vls_shd->lock);
513 vls_shared_data_pool_runlock ();
517 vls_shared_get_owner (vcl_locked_session_t * vls)
519 vls_shared_data_t *vls_shd;
522 vls_shared_data_pool_rlock ();
524 vls_shd = vls_shared_data_get (vls->shared_data_index);
525 owner_wrk = vls_shd->owner_wrk_index;
527 vls_shared_data_pool_runlock ();
533 vls_listener_wrk_is_active (vcl_locked_session_t * vls, u32 wrk_index)
535 vls_shared_data_t *vls_shd;
538 if (vls->shared_data_index == ~0)
540 clib_warning ("not a shared session");
544 vls_shared_data_pool_rlock ();
546 vls_shd = vls_shared_data_get (vls->shared_data_index);
548 clib_spinlock_lock (&vls_shd->lock);
549 is_set = clib_bitmap_get (vls_shd->listeners, wrk_index);
550 clib_spinlock_unlock (&vls_shd->lock);
552 vls_shared_data_pool_runlock ();
554 return (is_set == 1);
558 vls_listener_wrk_start_listen (vcl_locked_session_t * vls, u32 wrk_index)
560 vppcom_session_listen (vls_to_sh (vls), ~0);
561 vls_listener_wrk_set (vls, wrk_index, 1 /* is_active */ );
565 vls_listener_wrk_stop_listen (vcl_locked_session_t * vls, u32 wrk_index)
570 wrk = vcl_worker_get (wrk_index);
571 s = vcl_session_get (wrk, vls->session_index);
572 if (s->session_state != VCL_STATE_LISTEN)
574 vcl_send_session_unlisten (wrk, s);
575 s->session_state = VCL_STATE_LISTEN_NO_MQ;
576 vls_listener_wrk_set (vls, wrk_index, 0 /* is_active */ );
580 vls_shared_data_subscriber_position (vls_shared_data_t * vls_shd,
585 for (i = 0; i < vec_len (vls_shd->workers_subscribed); i++)
587 if (vls_shd->workers_subscribed[i] == wrk_index)
594 vls_unshare_session (vcl_locked_session_t * vls, vcl_worker_t * wrk)
596 vls_shared_data_t *vls_shd;
597 int do_disconnect, pos;
601 if (vls->shared_data_index == ~0)
604 s = vcl_session_get (wrk, vls->session_index);
605 if (s->session_state == VCL_STATE_LISTEN)
606 vls_listener_wrk_set (vls, wrk->wrk_index, 0 /* is_active */ );
608 vls_shared_data_pool_rlock ();
610 vls_shd = vls_shared_data_get (vls->shared_data_index);
611 clib_spinlock_lock (&vls_shd->lock);
613 pos = vls_shared_data_subscriber_position (vls_shd, wrk->wrk_index);
616 clib_warning ("worker %u not subscribed for vls %u", wrk->wrk_index,
622 * Unsubscribe from share data and fifos
626 svm_fifo_del_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
627 svm_fifo_del_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
629 vec_del1 (vls_shd->workers_subscribed, pos);
634 n_subscribers = vec_len (vls_shd->workers_subscribed);
635 do_disconnect = s->session_state == VCL_STATE_LISTEN || !n_subscribers;
636 vcl_session_cleanup (wrk, s, vcl_session_handle (s), do_disconnect);
639 * No subscriber left, cleanup shared data
643 u32 shd_index = vls_shared_data_index (vls_shd);
645 clib_spinlock_unlock (&vls_shd->lock);
646 vls_shared_data_pool_runlock ();
648 vls_shared_data_free (shd_index);
650 /* All locks have been dropped */
654 /* Return, if this is not the owning worker */
655 if (vls_shd->owner_wrk_index != wrk->wrk_index)
658 ASSERT (vec_len (vls_shd->workers_subscribed));
661 * Check if we can change owner or close
663 vls_shd->owner_wrk_index = vls_shd->workers_subscribed[0];
664 vcl_send_session_worker_update (wrk, s, vls_shd->owner_wrk_index);
666 /* XXX is this still needed? */
667 if (vec_len (vls_shd->workers_subscribed) > 1)
668 clib_warning ("more workers need to be updated");
672 clib_spinlock_unlock (&vls_shd->lock);
673 vls_shared_data_pool_runlock ();
679 vls_init_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
681 vls_shared_data_t *vls_shd;
683 u32 vls_shd_index = vls_shared_data_alloc ();
685 vls_shared_data_pool_rlock ();
687 vls_shd = vls_shared_data_get (vls_shd_index);
688 vls_shd->owner_wrk_index = vls_wrk->wrk_index;
689 vls->shared_data_index = vls_shd_index;
690 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
692 vls_shared_data_pool_runlock ();
696 vls_share_session (vls_worker_t * vls_wrk, vcl_locked_session_t * vls)
698 vcl_worker_t *vcl_wrk = vcl_worker_get (vls_wrk->wrk_index);
699 vls_shared_data_t *vls_shd;
702 s = vcl_session_get (vcl_wrk, vls->session_index);
705 clib_warning ("wrk %u session %u vls %u NOT AVAILABLE",
706 vcl_wrk->wrk_index, vls->session_index, vls->vls_index);
710 ASSERT (vls->shared_data_index != ~0);
712 /* Reinit session lock */
713 clib_spinlock_init (&vls->lock);
715 vls_shared_data_pool_rlock ();
717 vls_shd = vls_shared_data_get (vls->shared_data_index);
719 clib_spinlock_lock (&vls_shd->lock);
720 vec_add1 (vls_shd->workers_subscribed, vls_wrk->wrk_index);
721 clib_spinlock_unlock (&vls_shd->lock);
723 vls_shared_data_pool_runlock ();
727 svm_fifo_add_subscriber (s->rx_fifo, vcl_wrk->vpp_wrk_index);
728 svm_fifo_add_subscriber (s->tx_fifo, vcl_wrk->vpp_wrk_index);
730 else if (s->session_state == VCL_STATE_LISTEN)
732 s->session_state = VCL_STATE_LISTEN_NO_MQ;
737 vls_share_sessions (vls_worker_t * vls_parent_wrk, vls_worker_t * vls_wrk)
739 vcl_locked_session_t *vls, *parent_vls;
742 pool_foreach (vls, vls_wrk->vls_pool) {
743 /* Initialize sharing on parent session */
744 if (vls->shared_data_index == ~0)
746 parent_vls = vls_session_get (vls_parent_wrk, vls->vls_index);
747 vls_init_share_session (vls_parent_wrk, parent_vls);
748 vls->shared_data_index = parent_vls->shared_data_index;
750 vls_share_session (vls_wrk, vls);
756 vls_worker_copy_on_fork (vcl_worker_t * parent_wrk)
758 vls_worker_t *vls_wrk = vls_worker_get_current (), *vls_parent_wrk;
759 vcl_worker_t *wrk = vcl_worker_get_current ();
760 u32 vls_index, session_index, wrk_index;
761 vcl_session_handle_t sh;
766 wrk->sessions = pool_dup (parent_wrk->sessions);
767 wrk->session_index_by_vpp_handles =
768 hash_dup (parent_wrk->session_index_by_vpp_handles);
773 vls_parent_wrk = vls_worker_get (parent_wrk->wrk_index);
775 hash_foreach (sh, vls_index, vls_parent_wrk->session_handle_to_vlsh_table,
777 vcl_session_handle_parse (sh, &wrk_index, &session_index);
778 hash_set (vls_wrk->session_handle_to_vlsh_table,
779 vcl_session_handle_from_index (session_index), vls_index);
782 vls_wrk->vls_pool = pool_dup (vls_parent_wrk->vls_pool);
784 vls_share_sessions (vls_parent_wrk, vls_wrk);
788 vls_mt_acq_locks (vcl_locked_session_t * vls, vls_mt_ops_t op, int *locks_acq)
790 vcl_worker_t *wrk = vcl_worker_get_current ();
791 vcl_session_t *s = 0;
796 s = vcl_session_get (wrk, vls->session_index);
797 if (PREDICT_FALSE (!s))
799 is_nonblk = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
806 is_nonblk = vcl_session_read_ready (s) != 0;
810 *locks_acq |= VLS_MT_LOCK_MQ;
813 case VLS_MT_OP_WRITE:
816 is_nonblk = vcl_session_write_ready (s) != 0;
820 *locks_acq |= VLS_MT_LOCK_MQ;
823 case VLS_MT_OP_XPOLL:
825 *locks_acq |= VLS_MT_LOCK_MQ;
827 case VLS_MT_OP_SPOOL:
828 vls_mt_spool_lock ();
829 *locks_acq |= VLS_MT_LOCK_SPOOL;
837 vls_mt_rel_locks (int locks_acq)
839 if (locks_acq & VLS_MT_LOCK_MQ)
841 if (locks_acq & VLS_MT_LOCK_SPOOL)
842 vls_mt_create_unlock ();
846 vls_mt_session_should_migrate (vcl_locked_session_t * vls)
848 return (vls_mt_wrk_supported ()
849 && vls->worker_index != vcl_get_worker_index ());
853 vls_mt_session_migrate (vcl_locked_session_t * vls)
855 u32 wrk_index = vcl_get_worker_index ();
858 vcl_session_t *session;
861 ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
864 * VCL session on current vcl worker already allocated. Update current
865 * owner worker and index and return
867 if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
869 vls->worker_index = wrk_index;
870 vls->session_index = (u32) p[0];
875 * Ask vcl worker that owns the original vcl session to clone it into
876 * current vcl worker session pool
879 if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
880 vls->owner_vcl_wrk_index)))
882 VERR ("session in owner worker(%u) is free", vls->owner_vcl_wrk_index);
887 src_sid = (u32) p[0];
888 wrk = vcl_worker_get_current ();
889 session = vcl_session_alloc (wrk);
890 sid = session->session_index;
891 vls_send_clone_and_share_rpc (wrk, vls, sid, vls_get_worker_index (),
892 vls->owner_vcl_wrk_index, vls->vls_index,
894 session->session_index = sid;
895 vls->worker_index = wrk_index;
896 vls->session_index = sid;
897 hash_set (vls->vcl_wrk_index_to_session_index, wrk_index, sid);
898 VDBG (1, "migrate session of worker (session): %u (%u) -> %u (%u)",
899 vls->owner_vcl_wrk_index, src_sid, wrk_index, sid);
901 if (PREDICT_FALSE ((session->flags & VCL_SESSION_F_IS_VEP)
902 && session->vep.next_sh != ~0))
904 /* TODO: rollback? */
905 VERR ("can't migrate nonempty epoll session");
909 else if (PREDICT_FALSE (!(session->flags & VCL_SESSION_F_IS_VEP) &&
910 session->session_state != VCL_STATE_CLOSED))
912 /* TODO: rollback? */
913 VERR ("migrate NOT supported, session_status (%u)",
914 session->session_state);
923 if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
927 #define vls_mt_guard(_vls, _op) \
928 int _locks_acq = 0; \
929 if (vls_mt_wrk_supported ()) \
931 if (PREDICT_FALSE (_vls \
932 && ((vcl_locked_session_t *)_vls)->worker_index != \
933 vcl_get_worker_index ())) \
934 vls_mt_session_migrate (_vls); \
938 if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
939 vls_mt_acq_locks (_vls, _op, &_locks_acq); \
942 #define vls_mt_unguard() \
943 if (PREDICT_FALSE (_locks_acq)) \
944 vls_mt_rel_locks (_locks_acq)
947 vls_write (vls_handle_t vlsh, void *buf, size_t nbytes)
949 vcl_locked_session_t *vls;
953 if (!(vls = vls_get_w_dlock (vlsh)))
954 return VPPCOM_EBADFD;
956 vls_mt_guard (vls, VLS_MT_OP_WRITE);
957 rv = vppcom_session_write (vls_to_sh_tu (vls), buf, nbytes);
959 vls_get_and_unlock (vlsh);
964 vls_write_msg (vls_handle_t vlsh, void *buf, size_t nbytes)
966 vcl_locked_session_t *vls;
970 if (!(vls = vls_get_w_dlock (vlsh)))
971 return VPPCOM_EBADFD;
972 vls_mt_guard (vls, VLS_MT_OP_WRITE);
973 rv = vppcom_session_write_msg (vls_to_sh_tu (vls), buf, nbytes);
975 vls_get_and_unlock (vlsh);
980 vls_sendto (vls_handle_t vlsh, void *buf, int buflen, int flags,
983 vcl_locked_session_t *vls;
987 if (!(vls = vls_get_w_dlock (vlsh)))
988 return VPPCOM_EBADFD;
989 vls_mt_guard (vls, VLS_MT_OP_WRITE);
990 rv = vppcom_session_sendto (vls_to_sh_tu (vls), buf, buflen, flags, ep);
992 vls_get_and_unlock (vlsh);
997 vls_read (vls_handle_t vlsh, void *buf, size_t nbytes)
999 vcl_locked_session_t *vls;
1003 if (!(vls = vls_get_w_dlock (vlsh)))
1004 return VPPCOM_EBADFD;
1005 vls_mt_guard (vls, VLS_MT_OP_READ);
1006 rv = vppcom_session_read (vls_to_sh_tu (vls), buf, nbytes);
1008 vls_get_and_unlock (vlsh);
1013 vls_recvfrom (vls_handle_t vlsh, void *buffer, uint32_t buflen, int flags,
1014 vppcom_endpt_t * ep)
1016 vcl_locked_session_t *vls;
1020 if (!(vls = vls_get_w_dlock (vlsh)))
1021 return VPPCOM_EBADFD;
1022 vls_mt_guard (vls, VLS_MT_OP_READ);
1023 rv = vppcom_session_recvfrom (vls_to_sh_tu (vls), buffer, buflen, flags,
1026 vls_get_and_unlock (vlsh);
1031 vls_attr (vls_handle_t vlsh, uint32_t op, void *buffer, uint32_t * buflen)
1033 vcl_locked_session_t *vls;
1037 if (!(vls = vls_get_w_dlock (vlsh)))
1038 return VPPCOM_EBADFD;
1039 if (vls_mt_session_should_migrate (vls))
1040 vls_mt_session_migrate (vls);
1041 rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
1042 vls_get_and_unlock (vlsh);
1047 vls_bind (vls_handle_t vlsh, vppcom_endpt_t * ep)
1049 vcl_locked_session_t *vls;
1053 if (!(vls = vls_get_w_dlock (vlsh)))
1054 return VPPCOM_EBADFD;
1055 rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
1056 vls_get_and_unlock (vlsh);
1061 vls_listen (vls_handle_t vlsh, int q_len)
1063 vcl_locked_session_t *vls;
1067 if (!(vls = vls_get_w_dlock (vlsh)))
1068 return VPPCOM_EBADFD;
1069 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1070 rv = vppcom_session_listen (vls_to_sh_tu (vls), q_len);
1072 vls_get_and_unlock (vlsh);
1077 vls_connect (vls_handle_t vlsh, vppcom_endpt_t * server_ep)
1079 vcl_locked_session_t *vls;
1083 if (!(vls = vls_get_w_dlock (vlsh)))
1084 return VPPCOM_EBADFD;
1085 vls_mt_guard (vls, VLS_MT_OP_XPOLL);
1086 rv = vppcom_session_connect (vls_to_sh_tu (vls), server_ep);
1088 vls_get_and_unlock (vlsh);
1093 vls_mp_checks (vcl_locked_session_t * vls, int is_add)
1095 vcl_worker_t *wrk = vcl_worker_get_current ();
1099 if (vls_mt_wrk_supported ())
1102 s = vcl_session_get (wrk, vls->session_index);
1103 switch (s->session_state)
1105 case VCL_STATE_LISTEN:
1108 vls_listener_wrk_set (vls, vls->worker_index, 1 /* is_active */ );
1111 vls_listener_wrk_stop_listen (vls, vls->worker_index);
1113 case VCL_STATE_LISTEN_NO_MQ:
1117 /* Register worker as listener */
1118 vls_listener_wrk_start_listen (vls, wrk->wrk_index);
1120 /* If owner worker did not attempt to accept/xpoll on the session,
1121 * force a listen stop for it, since it may not be interested in
1122 * accepting new sessions.
1123 * This is pretty much a hack done to give app workers the illusion
1124 * that it is fine to listen and not accept new sessions for a
1125 * given listener. Without it, we would accumulate unhandled
1126 * accepts on the passive worker message queue. */
1127 owner_wrk = vls_shared_get_owner (vls);
1128 if (!vls_listener_wrk_is_active (vls, owner_wrk))
1129 vls_listener_wrk_stop_listen (vls, owner_wrk);
1137 vls_accept (vls_handle_t listener_vlsh, vppcom_endpt_t * ep, int flags)
1139 vls_handle_t accepted_vlsh;
1140 vcl_locked_session_t *vls;
1144 if (!(vls = vls_get_w_dlock (listener_vlsh)))
1145 return VPPCOM_EBADFD;
1146 if (vcl_n_workers () > 1)
1147 vls_mp_checks (vls, 1 /* is_add */ );
1148 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1149 sh = vppcom_session_accept (vls_to_sh_tu (vls), ep, flags);
1151 vls_get_and_unlock (listener_vlsh);
1154 accepted_vlsh = vls_alloc (sh);
1155 if (PREDICT_FALSE (accepted_vlsh == VLS_INVALID_HANDLE))
1156 vppcom_session_close (sh);
1157 return accepted_vlsh;
1161 vls_create (uint8_t proto, uint8_t is_nonblocking)
1163 vcl_session_handle_t sh;
1167 vls_mt_guard (0, VLS_MT_OP_SPOOL);
1168 sh = vppcom_session_create (proto, is_nonblocking);
1170 if (sh == INVALID_SESSION_ID)
1171 return VLS_INVALID_HANDLE;
1173 vlsh = vls_alloc (sh);
1174 if (PREDICT_FALSE (vlsh == VLS_INVALID_HANDLE))
1175 vppcom_session_close (sh);
1181 vls_mt_session_cleanup (vcl_locked_session_t * vls)
1183 u32 session_index, wrk_index, current_vcl_wrk;
1184 vcl_worker_t *wrk = vcl_worker_get_current ();
1186 ASSERT (vls_mt_wrk_supported ());
1188 current_vcl_wrk = vcl_get_worker_index ();
1191 hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
1193 if (current_vcl_wrk != wrk_index)
1194 vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
1197 hash_free (vls->vcl_wrk_index_to_session_index);
1201 vls_close (vls_handle_t vlsh)
1203 vcl_locked_session_t *vls;
1207 vls_mt_table_wlock ();
1209 vls = vls_get_and_lock (vlsh);
1212 vls_mt_table_wunlock ();
1213 return VPPCOM_EBADFD;
1216 vls_mt_guard (vls, VLS_MT_OP_SPOOL);
1218 if (vls_is_shared (vls))
1219 rv = vls_unshare_session (vls, vcl_worker_get_current ());
1221 rv = vppcom_session_close (vls_to_sh (vls));
1223 if (vls_mt_wrk_supported ())
1224 vls_mt_session_cleanup (vls);
1229 vls_mt_table_wunlock ();
1235 vls_epoll_create (void)
1237 vcl_session_handle_t sh;
1242 sh = vppcom_epoll_create ();
1243 if (sh == INVALID_SESSION_ID)
1244 return VLS_INVALID_HANDLE;
1246 vlsh = vls_alloc (sh);
1247 if (vlsh == VLS_INVALID_HANDLE)
1248 vppcom_session_close (sh);
1254 vls_epoll_ctl_mp_checks (vcl_locked_session_t * vls, int op)
1256 if (vcl_n_workers () <= 1)
1258 vlsl->epoll_mp_check = 1;
1262 if (op == EPOLL_CTL_MOD)
1265 vlsl->epoll_mp_check = 1;
1266 vls_mp_checks (vls, op == EPOLL_CTL_ADD);
1270 vls_epoll_ctl (vls_handle_t ep_vlsh, int op, vls_handle_t vlsh,
1271 struct epoll_event *event)
1273 vcl_locked_session_t *ep_vls, *vls;
1274 vcl_session_handle_t ep_sh, sh;
1278 vls_mt_table_rlock ();
1279 ep_vls = vls_get_and_lock (ep_vlsh);
1280 vls = vls_get_and_lock (vlsh);
1282 if (vls_mt_session_should_migrate (ep_vls))
1283 vls_mt_session_migrate (ep_vls);
1285 ep_sh = vls_to_sh (ep_vls);
1286 sh = vls_to_sh (vls);
1288 if (PREDICT_FALSE (!vlsl->epoll_mp_check))
1289 vls_epoll_ctl_mp_checks (vls, op);
1291 vls_mt_table_runlock ();
1293 rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
1295 vls_mt_table_rlock ();
1296 ep_vls = vls_get (ep_vlsh);
1297 vls = vls_get (vlsh);
1299 vls_unlock (ep_vls);
1300 vls_mt_table_runlock ();
1305 vls_epoll_wait (vls_handle_t ep_vlsh, struct epoll_event *events,
1306 int maxevents, double wait_for_time)
1308 vcl_locked_session_t *vls;
1312 if (!(vls = vls_get_w_dlock (ep_vlsh)))
1313 return VPPCOM_EBADFD;
1314 vls_mt_guard (0, VLS_MT_OP_XPOLL);
1315 rv = vppcom_epoll_wait (vls_to_sh_tu (vls), events, maxevents,
1318 vls_get_and_unlock (ep_vlsh);
1323 vls_select_mp_checks (vcl_si_set * read_map)
1325 vcl_locked_session_t *vls;
1330 if (vcl_n_workers () <= 1)
1332 vlsl->select_mp_check = 1;
1339 vlsl->select_mp_check = 1;
1340 wrk = vcl_worker_get_current ();
1343 clib_bitmap_foreach (si, read_map, ({
1344 s = vcl_session_get (wrk, si);
1345 if (s->session_state == VCL_STATE_LISTEN)
1347 vls = vls_get (vls_session_index_to_vlsh (si));
1348 vls_mp_checks (vls, 1 /* is_add */);
1355 vls_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1356 vcl_si_set * except_map, double wait_for_time)
1361 vls_mt_guard (0, VLS_MT_OP_XPOLL);
1362 if (PREDICT_FALSE (!vlsl->select_mp_check))
1363 vls_select_mp_checks (read_map);
1364 rv = vppcom_select (n_bits, read_map, write_map, except_map, wait_for_time);
1370 vls_unshare_vcl_worker_sessions (vcl_worker_t * wrk)
1372 u32 current_wrk, is_current;
1373 vcl_locked_session_t *vls;
1376 if (pool_elts (vcm->workers) <= 1)
1379 current_wrk = vcl_get_worker_index ();
1380 is_current = current_wrk == wrk->wrk_index;
1383 pool_foreach (s, wrk->sessions) {
1384 vls = vls_get (vls_si_wi_to_vlsh (s->session_index, wrk->wrk_index));
1385 if (vls && (is_current || vls_is_shared_by_wrk (vls, current_wrk)))
1386 vls_unshare_session (vls, wrk);
1392 vls_cleanup_vcl_worker (vcl_worker_t * wrk)
1394 vls_worker_t *vls_wrk = vls_worker_get (wrk->wrk_index);
1396 /* Unshare sessions and also cleanup worker since child may have
1397 * called _exit () and therefore vcl may not catch the event */
1398 vls_unshare_vcl_worker_sessions (wrk);
1399 vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
1401 vls_worker_free (vls_wrk);
1405 vls_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
1407 vcl_worker_t *sub_child;
1410 if (child_wrk->forked_child != ~0)
1412 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
1415 /* Wait a bit, maybe the process is going away */
1416 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
1418 if (kill (sub_child->current_pid, 0) < 0)
1419 vls_cleanup_forked_child (child_wrk, sub_child);
1422 vls_cleanup_vcl_worker (child_wrk);
1423 VDBG (0, "Cleaned up forked child wrk %u", child_wrk->wrk_index);
1424 wrk->forked_child = ~0;
1427 static struct sigaction old_sa;
1430 vls_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
1432 vcl_worker_t *wrk, *child_wrk;
1434 if (vcl_get_worker_index () == ~0)
1437 if (sigaction (SIGCHLD, &old_sa, 0))
1439 VERR ("couldn't restore sigchld");
1443 wrk = vcl_worker_get_current ();
1444 if (wrk->forked_child == ~0)
1447 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
1451 if (si && si->si_pid != child_wrk->current_pid)
1453 VDBG (0, "unexpected child pid %u", si->si_pid);
1456 vls_cleanup_forked_child (wrk, child_wrk);
1459 if (old_sa.sa_flags & SA_SIGINFO)
1461 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
1462 fn (signum, si, uc);
1466 void (*fn) (int) = old_sa.sa_handler;
1473 vls_incercept_sigchld ()
1475 struct sigaction sa;
1476 clib_memset (&sa, 0, sizeof (sa));
1477 sa.sa_sigaction = vls_intercept_sigchld_handler;
1478 sa.sa_flags = SA_SIGINFO;
1479 if (sigaction (SIGCHLD, &sa, &old_sa))
1481 VERR ("couldn't intercept sigchld");
1487 vls_app_pre_fork (void)
1489 vls_incercept_sigchld ();
1490 vcl_flush_mq_events ();
1494 vls_app_fork_child_handler (void)
1496 vcl_worker_t *parent_wrk;
1497 int parent_wrk_index;
1499 parent_wrk_index = vcl_get_worker_index ();
1500 VDBG (0, "initializing forked child %u with parent wrk %u", getpid (),
1506 vcl_set_worker_index (~0);
1509 * Allocate and register vcl worker with vpp
1511 if (vppcom_worker_register ())
1513 VERR ("couldn't register new worker!");
1518 * Allocate/initialize vls worker and share sessions
1520 vls_worker_alloc ();
1521 parent_wrk = vcl_worker_get (parent_wrk_index);
1522 vls_worker_copy_on_fork (parent_wrk);
1523 parent_wrk->forked_child = vcl_get_worker_index ();
1525 /* Reset number of threads and set wrk index */
1526 vlsl->vls_mt_n_threads = 0;
1527 vlsl->vls_wrk_index = vcl_get_worker_index ();
1528 vlsl->select_mp_check = 0;
1529 vlsl->epoll_mp_check = 0;
1530 vls_mt_locks_init ();
1532 VDBG (0, "forked child main worker initialized");
1537 vls_app_fork_parent_handler (void)
1540 while (vcm->forking)
1547 vls_worker_t *wrk = vls_worker_get_current ();
1549 /* Unshare the sessions. VCL will clean up the worker */
1550 vls_unshare_vcl_worker_sessions (vcl_worker_get_current ());
1551 vls_worker_free (wrk);
1555 vls_clone_and_share_rpc_handler (void *args)
1557 vls_clone_and_share_msg_t *msg = (vls_clone_and_share_msg_t *) args;
1558 vls_worker_t *wrk = vls_worker_get_current (), *dst_wrk;
1559 vcl_locked_session_t *vls, *dst_vls;
1560 vcl_worker_t *vcl_wrk = vcl_worker_get_current (), *dst_vcl_wrk;
1561 vcl_session_t *s, *dst_s;
1563 vls = vls_session_get (wrk, msg->vls_index);
1565 if (!vls_mt_wrk_supported ())
1566 vls_init_share_session (wrk, vls);
1568 s = vcl_session_get (vcl_wrk, msg->session_index);
1569 dst_wrk = vls_worker_get (msg->origin_vls_wrk);
1570 dst_vcl_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1571 dst_vls = vls_session_get (dst_wrk, msg->origin_vls_index);
1572 dst_vls->shared_data_index = vls->shared_data_index;
1573 dst_s = vcl_session_get (dst_vcl_wrk, msg->origin_session_index);
1574 clib_memcpy (dst_s, s, sizeof (*s));
1576 dst_vcl_wrk->rpc_done = 1;
1578 VDBG (1, "proces session clone of worker (session): %u (%u) -> %u (%u)",
1579 vcl_wrk->wrk_index, msg->session_index, dst_vcl_wrk->wrk_index,
1580 msg->origin_session_index);
1584 vls_session_cleanup_rpc_handler (void *args)
1586 vls_sess_cleanup_msg_t *msg = (vls_sess_cleanup_msg_t *) args;
1587 vcl_worker_t *wrk = vcl_worker_get_current ();
1588 vcl_worker_t *dst_wrk = vcl_worker_get (msg->origin_vcl_wrk);
1590 vppcom_session_close (vcl_session_handle_from_index (msg->session_index));
1592 dst_wrk->rpc_done = 1;
1594 VDBG (1, "proces session cleanup of worker (session): %u (%u) from %u ()",
1595 wrk->wrk_index, msg->session_index, dst_wrk->wrk_index);
1599 vls_rpc_handler (void *args)
1601 vls_rpc_msg_t *msg = (vls_rpc_msg_t *) args;
1604 case VLS_RPC_CLONE_AND_SHARE:
1605 vls_clone_and_share_rpc_handler (msg->data);
1607 case VLS_RPC_SESS_CLEANUP:
1608 vls_session_cleanup_rpc_handler (msg->data);
1616 vls_send_clone_and_share_rpc (vcl_worker_t * wrk,
1617 vcl_locked_session_t * vls, u32 session_index,
1618 u32 vls_wrk_index, u32 dst_wrk_index,
1619 u32 dst_vls_index, u32 dst_session_index)
1621 u8 data[sizeof (u8) + sizeof (vls_clone_and_share_msg_t)];
1622 vls_clone_and_share_msg_t *msg;
1626 rpc = (vls_rpc_msg_t *) & data;
1627 rpc->type = VLS_RPC_CLONE_AND_SHARE;
1628 msg = (vls_clone_and_share_msg_t *) & rpc->data;
1629 msg->origin_vls_wrk = vls_wrk_index;
1630 msg->origin_vls_index = vls->vls_index;
1631 msg->origin_vcl_wrk = wrk->wrk_index;
1632 msg->origin_session_index = session_index;
1633 msg->vls_index = dst_vls_index;
1634 msg->session_index = dst_session_index;
1637 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1639 VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
1640 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
1641 msg->origin_session_index, ret);
1642 while (!ret && !wrk->rpc_done)
1647 vls_send_session_cleanup_rpc (vcl_worker_t * wrk,
1648 u32 dst_wrk_index, u32 dst_session_index)
1650 u8 data[sizeof (u8) + sizeof (vls_sess_cleanup_msg_t)];
1651 vls_sess_cleanup_msg_t *msg;
1655 rpc = (vls_rpc_msg_t *) & data;
1656 rpc->type = VLS_RPC_SESS_CLEANUP;
1657 msg = (vls_sess_cleanup_msg_t *) & rpc->data;
1658 msg->origin_vcl_wrk = wrk->wrk_index;
1659 msg->session_index = dst_session_index;
1662 ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
1664 VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
1665 dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
1666 while (!ret && !wrk->rpc_done)
1671 vls_app_create (char *app_name)
1675 if ((rv = vppcom_app_create (app_name)))
1678 vlsm = clib_mem_alloc (sizeof (vls_main_t));
1679 clib_memset (vlsm, 0, sizeof (*vlsm));
1680 clib_rwlock_init (&vlsm->vls_table_lock);
1681 clib_rwlock_init (&vlsm->shared_data_lock);
1682 pool_alloc (vlsm->workers, vcm->cfg.max_workers);
1684 pthread_atfork (vls_app_pre_fork, vls_app_fork_parent_handler,
1685 vls_app_fork_child_handler);
1686 atexit (vls_app_exit);
1687 vls_worker_alloc ();
1688 vlsl->vls_wrk_index = vcl_get_worker_index ();
1689 vls_mt_locks_init ();
1690 vcm->wrk_rpc_fn = vls_rpc_handler;
1695 vls_use_eventfd (void)
1697 return vcm->cfg.use_mq_eventfd;
1701 vls_mt_wrk_supported (void)
1703 return vcm->cfg.mt_wrk_supported;
1707 vls_use_real_epoll (void)
1709 if (vcl_get_worker_index () == ~0)
1712 return vcl_worker_get_current ()->vcl_needs_real_epoll;
1716 vls_register_vcl_worker (void)
1722 * fd.io coding-style-patch-verification: ON
1725 * eval: (c-set-style "gnu")