Type: improvement
Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I521c110fd4d7445bd585c96d4c768f16a0a7d3b8
- if (vls_mt_wrk_supported ())
- if (PREDICT_FALSE (vppcom_worker_index () == ~0))
- vls_register_vcl_worker ();
+ /* Make sure the vcl worker is valid. Could be that epoll fd was created on
+ * one thread but it is now used on another */
+ if (PREDICT_FALSE (vppcom_worker_index () == ~0))
+ vls_register_vcl_worker ();
ldpw = ldp_worker_get_current ();
if (epfd == ldpw->vcl_mq_epfd)
ldpw = ldp_worker_get_current ();
if (epfd == ldpw->vcl_mq_epfd)
VCFG_DBG (0, "VCL<%d>: configured tls-engine %u (0x%x)",
getpid (), vcl_cfg->tls_engine, vcl_cfg->tls_engine);
}
VCFG_DBG (0, "VCL<%d>: configured tls-engine %u (0x%x)",
getpid (), vcl_cfg->tls_engine, vcl_cfg->tls_engine);
}
- else if (unformat (line_input, "multi-thread"))
+ else if (unformat (line_input, "multi-thread-workers"))
- vcl_cfg->mt_supported = 1;
- VCFG_DBG (0, "VCL<%d>: configured with multithread", getpid ());
+ vcl_cfg->mt_wrk_supported = 1;
+ VCFG_DBG (0, "VCL<%d>: configured with multithread workers",
+ getpid ());
}
else if (unformat (line_input, "}"))
{
}
else if (unformat (line_input, "}"))
{
+vls_mt_table_rlock (void)
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_reader_lock (&vlsm->vls_table_lock);
}
static inline void
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_reader_lock (&vlsm->vls_table_lock);
}
static inline void
-vls_table_runlock (void)
+vls_mt_table_runlock (void)
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
}
static inline void
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_reader_unlock (&vlsm->vls_table_lock);
}
static inline void
+vls_mt_table_wlock (void)
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_writer_lock (&vlsm->vls_table_lock);
}
static inline void
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_writer_lock (&vlsm->vls_table_lock);
}
static inline void
-vls_table_wunlock (void)
+vls_mt_table_wunlock (void)
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
{
if (vlsl->vls_mt_n_threads > 1)
clib_rwlock_writer_unlock (&vlsm->vls_table_lock);
vls_mt_add (void)
{
vlsl->vls_mt_n_threads += 1;
vls_mt_add (void)
{
vlsl->vls_mt_n_threads += 1;
+
+ /* If multi-thread workers are supported, for each new thread register a new
+ * vcl worker with vpp. Otherwise, all threads use the same vcl worker, so
+ * update the vcl worker's thread local worker index variable */
if (vls_mt_wrk_supported ())
vls_register_vcl_worker ();
else
if (vls_mt_wrk_supported ())
vls_register_vcl_worker ();
else
{
vcl_session_handle_t sh;
sh = vls_to_sh (vls);
{
vcl_session_handle_t sh;
sh = vls_to_sh (vls);
+ vls_mt_table_runlock ();
vls_worker_t *wrk = vls_worker_get_current ();
vcl_locked_session_t *vls;
vls_worker_t *wrk = vls_worker_get_current ();
vcl_locked_session_t *vls;
pool_get_zero (wrk->vls_pool, vls);
vls->session_index = vppcom_session_index (sh);
pool_get_zero (wrk->vls_pool, vls);
vls->session_index = vppcom_session_index (sh);
}
clib_spinlock_init (&vls->lock);
}
clib_spinlock_init (&vls->lock);
+ vls_mt_table_wunlock ();
vls_get_w_dlock (vls_handle_t vlsh)
{
vcl_locked_session_t *vls;
vls_get_w_dlock (vls_handle_t vlsh)
{
vcl_locked_session_t *vls;
vls = vls_get_and_lock (vlsh);
if (!vls)
vls = vls_get_and_lock (vlsh);
if (!vls)
+ vls_mt_table_runlock ();
vls_get_and_unlock (vls_handle_t vlsh)
{
vcl_locked_session_t *vls;
vls_get_and_unlock (vls_handle_t vlsh)
{
vcl_locked_session_t *vls;
vls = vls_get (vlsh);
vls_unlock (vls);
vls = vls_get (vlsh);
vls_unlock (vls);
+ vls_mt_table_runlock ();
}
static inline void
vls_dunlock (vcl_locked_session_t * vls)
{
vls_unlock (vls);
}
static inline void
vls_dunlock (vcl_locked_session_t * vls)
{
vls_unlock (vls);
+ vls_mt_table_runlock ();
}
static vcl_locked_session_t *
}
static vcl_locked_session_t *
vlsh = vls_si_to_vlsh (session_index);
vlsh = vls_si_to_vlsh (session_index);
+ vls_mt_table_runlock ();
vls_mt_create_unlock ();
}
vls_mt_create_unlock ();
}
+static inline u8
+vls_mt_session_should_migrate (vcl_locked_session_t * vls)
+{
+ return (vls_mt_wrk_supported ()
+ && vls->worker_index != vcl_get_worker_index ());
+}
+
-vls_session_migrate (vcl_locked_session_t * vls)
+vls_mt_session_migrate (vcl_locked_session_t * vls)
{
u32 wrk_index = vcl_get_worker_index ();
vcl_worker_t *wrk;
{
u32 wrk_index = vcl_get_worker_index ();
vcl_worker_t *wrk;
vcl_session_t *session;
uword *p;
vcl_session_t *session;
uword *p;
- if (!vls_mt_wrk_supported ())
- return;
+ ASSERT (vls_mt_wrk_supported () && vls->worker_index != wrk_index);
- if (PREDICT_TRUE (vls->worker_index == wrk_index))
- return;
+ /*
+ * VCL session on current vcl worker already allocated. Update current
+ * owner worker and index and return
+ */
if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
{
vls->worker_index = wrk_index;
if ((p = hash_get (vls->vcl_wrk_index_to_session_index, wrk_index)))
{
vls->worker_index = wrk_index;
- /* migrate from orignal vls */
+ /*
+ * Ask vcl worker that owns the original vcl session to clone it into
+ * current vcl worker session pool
+ */
+
if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
vls->owner_vcl_wrk_index)))
{
if (!(p = hash_get (vls->vcl_wrk_index_to_session_index,
vls->owner_vcl_wrk_index)))
{
-#define vls_mt_guard(_vls, _op) \
- int _locks_acq = 0; \
- if (PREDICT_FALSE (vcl_get_worker_index () == ~0)) \
- vls_mt_add (); \
- if (PREDICT_FALSE (_vls && vls_mt_wrk_supported () && \
- ((vcl_locked_session_t *)_vls)->worker_index != \
- vcl_get_worker_index ())) \
- vls_session_migrate (_vls); \
- if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
- vls_mt_acq_locks (_vls, _op, &_locks_acq); \
+static inline void
+vls_mt_detect (void)
+{
+ if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
+ vls_mt_add ();
+}
-#define vls_mt_unguard() \
- if (PREDICT_FALSE (_locks_acq)) \
+#define vls_mt_guard(_vls, _op) \
+ int _locks_acq = 0; \
+ if (vls_mt_wrk_supported ()) \
+ { \
+ if (PREDICT_FALSE (_vls \
+ && ((vcl_locked_session_t *)_vls)->worker_index != \
+ vcl_get_worker_index ())) \
+ vls_mt_session_migrate (_vls); \
+ } \
+ else \
+ { \
+ if (PREDICT_FALSE (vlsl->vls_mt_n_threads > 1)) \
+ vls_mt_acq_locks (_vls, _op, &_locks_acq); \
+ } \
+
+#define vls_mt_unguard() \
+ if (PREDICT_FALSE (_locks_acq)) \
vls_mt_rel_locks (_locks_acq)
int
vls_mt_rel_locks (_locks_acq)
int
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_WRITE);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_READ);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_READ);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_READ);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_READ);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
- if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
- vls_mt_add ();
-
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
- vls_session_migrate (vls);
+ if (vls_mt_session_should_migrate (vls))
+ vls_mt_session_migrate (vls);
rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
vls_get_and_unlock (vlsh);
return rv;
rv = vppcom_session_attr (vls_to_sh_tu (vls), op, buffer, buflen);
vls_get_and_unlock (vlsh);
return rv;
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
rv = vppcom_session_bind (vls_to_sh_tu (vls), ep);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_XPOLL);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_XPOLL);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_XPOLL);
if (!(vls = vls_get_w_dlock (vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (vls, VLS_MT_OP_XPOLL);
vcl_locked_session_t *vls;
int sh;
vcl_locked_session_t *vls;
int sh;
if (!(vls = vls_get_w_dlock (listener_vlsh)))
return VPPCOM_EBADFD;
if (vcl_n_workers () > 1)
if (!(vls = vls_get_w_dlock (listener_vlsh)))
return VPPCOM_EBADFD;
if (vcl_n_workers () > 1)
vcl_session_handle_t sh;
vls_handle_t vlsh;
vcl_session_handle_t sh;
vls_handle_t vlsh;
vls_mt_guard (0, VLS_MT_OP_SPOOL);
sh = vppcom_session_create (proto, is_nonblocking);
vls_mt_unguard ();
vls_mt_guard (0, VLS_MT_OP_SPOOL);
sh = vppcom_session_create (proto, is_nonblocking);
vls_mt_unguard ();
-vls_migrate_session_close (vcl_locked_session_t * vls)
+vls_mt_session_cleanup (vcl_locked_session_t * vls)
- u32 session_index, wrk_index;
+ u32 session_index, wrk_index, current_vcl_wrk;
vcl_worker_t *wrk = vcl_worker_get_current ();
vcl_worker_t *wrk = vcl_worker_get_current ();
- if (!vls_mt_wrk_supported ())
- return;
+ ASSERT (vls_mt_wrk_supported ());
+
+ current_vcl_wrk = vcl_get_worker_index ();
/* *INDENT-OFF* */
hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
({
/* *INDENT-OFF* */
hash_foreach (wrk_index, session_index, vls->vcl_wrk_index_to_session_index,
({
- if (vcl_get_worker_index () != wrk_index)
- {
- vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
- }
+ if (current_vcl_wrk != wrk_index)
+ vls_send_session_cleanup_rpc (wrk, wrk_index, session_index);
}));
/* *INDENT-ON* */
hash_free (vls->vcl_wrk_index_to_session_index);
}));
/* *INDENT-ON* */
hash_free (vls->vcl_wrk_index_to_session_index);
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
+ vls_mt_detect ();
+ vls_mt_table_wlock ();
vls = vls_get_and_lock (vlsh);
if (!vls)
{
vls = vls_get_and_lock (vlsh);
if (!vls)
{
+ vls_mt_table_wunlock ();
else
rv = vppcom_session_close (vls_to_sh (vls));
else
rv = vppcom_session_close (vls_to_sh (vls));
- vls_migrate_session_close (vls);
+ if (vls_mt_wrk_supported ())
+ vls_mt_session_cleanup (vls);
vls_free (vls);
vls_mt_unguard ();
vls_free (vls);
vls_mt_unguard ();
+ vls_mt_table_wunlock ();
vcl_session_handle_t sh;
vls_handle_t vlsh;
vcl_session_handle_t sh;
vls_handle_t vlsh;
- if (PREDICT_FALSE (vcl_get_worker_index () == ~0))
- vls_mt_add ();
sh = vppcom_epoll_create ();
if (sh == INVALID_SESSION_ID)
sh = vppcom_epoll_create ();
if (sh == INVALID_SESSION_ID)
vcl_session_handle_t ep_sh, sh;
int rv;
vcl_session_handle_t ep_sh, sh;
int rv;
+ vls_mt_detect ();
+ vls_mt_table_rlock ();
ep_vls = vls_get_and_lock (ep_vlsh);
vls = vls_get_and_lock (vlsh);
ep_vls = vls_get_and_lock (ep_vlsh);
vls = vls_get_and_lock (vlsh);
- vls_session_migrate (ep_vls);
+
+ if (vls_mt_session_should_migrate (ep_vls))
+ vls_mt_session_migrate (ep_vls);
+
ep_sh = vls_to_sh (ep_vls);
sh = vls_to_sh (vls);
if (PREDICT_FALSE (!vlsl->epoll_mp_check))
vls_epoll_ctl_mp_checks (vls, op);
ep_sh = vls_to_sh (ep_vls);
sh = vls_to_sh (vls);
if (PREDICT_FALSE (!vlsl->epoll_mp_check))
vls_epoll_ctl_mp_checks (vls, op);
+ vls_mt_table_runlock ();
rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
rv = vppcom_epoll_ctl (ep_sh, op, sh, event);
ep_vls = vls_get (ep_vlsh);
vls = vls_get (vlsh);
vls_unlock (vls);
vls_unlock (ep_vls);
ep_vls = vls_get (ep_vlsh);
vls = vls_get (vlsh);
vls_unlock (vls);
vls_unlock (ep_vls);
+ vls_mt_table_runlock ();
vcl_locked_session_t *vls;
int rv;
vcl_locked_session_t *vls;
int rv;
if (!(vls = vls_get_w_dlock (ep_vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (0, VLS_MT_OP_XPOLL);
if (!(vls = vls_get_w_dlock (ep_vlsh)))
return VPPCOM_EBADFD;
vls_mt_guard (0, VLS_MT_OP_XPOLL);
vls_mt_guard (0, VLS_MT_OP_XPOLL);
if (PREDICT_FALSE (!vlsl->select_mp_check))
vls_select_mp_checks (read_map);
vls_mt_guard (0, VLS_MT_OP_XPOLL);
if (PREDICT_FALSE (!vlsl->select_mp_check))
vls_select_mp_checks (read_map);
wrk->rpc_done = 0;
ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
wrk->rpc_done = 0;
ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
- VDBG (1,
- "send session clone of worker (session): %u (%u) -> %u (%u), ret=%d",
+ VDBG (1, "send session clone to wrk (session): %u (%u) -> %u (%u), ret=%d",
dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
msg->origin_session_index, ret);
while (!ret && !wrk->rpc_done)
dst_wrk_index, msg->session_index, msg->origin_vcl_wrk,
msg->origin_session_index, ret);
while (!ret && !wrk->rpc_done)
wrk->rpc_done = 0;
ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
wrk->rpc_done = 0;
ret = vcl_send_worker_rpc (dst_wrk_index, rpc, sizeof (data));
- VDBG (1,
- "send session cleanup of worker (session): %u (%u) from %u (), ret=%d",
+ VDBG (1, "send session cleanup to wrk (session): %u (%u) from %u, ret=%d",
dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
while (!ret && !wrk->rpc_done)
;
dst_wrk_index, msg->session_index, msg->origin_vcl_wrk, ret);
while (!ret && !wrk->rpc_done)
;
unsigned char
vls_mt_wrk_supported (void)
{
unsigned char
vls_mt_wrk_supported (void)
{
- return vcm->cfg.mt_supported;
+ return vcm->cfg.mt_wrk_supported;
u8 *vpp_api_socket_name;
u8 *vpp_api_chroot;
u32 tls_engine;
u8 *vpp_api_socket_name;
u8 *vpp_api_chroot;
u32 tls_engine;
} vppcom_cfg_t;
void vppcom_cfg (vppcom_cfg_t * vcl_cfg);
} vppcom_cfg_t;
void vppcom_cfg (vppcom_cfg_t * vcl_cfg);