if (PREDICT_FALSE (will_expand && vlib_num_workers ()))
{
clib_rwlock_writer_lock (&tm->half_open_rwlock);
- pool_get (tm->half_open_ctx_pool, ctx);
- ctx_index = ctx - tm->half_open_ctx_pool;
+ pool_get_zero (tm->half_open_ctx_pool, ctx);
+ ctx->c_c_index = ctx - tm->half_open_ctx_pool;
+ ctx_index = ctx->c_c_index;
clib_rwlock_writer_unlock (&tm->half_open_rwlock);
}
else
{
/* reader lock assumption: only main thread will call pool_get */
clib_rwlock_reader_lock (&tm->half_open_rwlock);
- pool_get (tm->half_open_ctx_pool, ctx);
- ctx_index = ctx - tm->half_open_ctx_pool;
+ pool_get_zero (tm->half_open_ctx_pool, ctx);
+ ctx->c_c_index = ctx - tm->half_open_ctx_pool;
+ ctx_index = ctx->c_c_index;
clib_rwlock_reader_unlock (&tm->half_open_rwlock);
}
- clib_memset (ctx, 0, sizeof (*ctx));
return ctx_index;
}
/* Free app session pre-allocated when transport was established */
if (ctx->tls_type == TRANSPORT_PROTO_TLS)
session_free (session_get (ctx->c_s_index, ctx->c_thread_index));
- goto failed;
+ ctx->no_app_session = 1;
+ goto send_reply;
}
/* For DTLS the app session is not preallocated because the underlying udp
if (ctx->tls_type == TRANSPORT_PROTO_DTLS)
{
session_type_t st;
+ /* Cleanup half-open session as we don't get notification from udp */
+ session_half_open_delete_notify (&ctx->connection);
app_session = session_alloc (ctx->c_thread_index);
app_session->session_state = SESSION_STATE_CREATED;
ctx->c_s_index = app_session->session_index;
failed:
ctx->no_app_session = 1;
tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
+send_reply:
return app_worker_connect_notify (app_wrk, 0, err,
ctx->parent_app_api_context);
}
}
}
+static void
+tls_session_cleanup_ho (session_t *s)
+{
+ tls_ctx_t *ctx;
+ u32 ho_index;
+
+ /* session opaque stores the opaque passed on connect */
+ ho_index = s->opaque;
+ ctx = tls_ctx_half_open_get (ho_index);
+ session_half_open_delete_notify (&ctx->connection);
+ tls_ctx_half_open_reader_unlock ();
+ tls_ctx_half_open_free (ho_index);
+}
+
int
tls_add_segment_callback (u32 client_index, u64 segment_handle)
{
ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
- if (err)
- {
- app_worker_t *app_wrk;
- u32 api_context;
- int rv = 0;
-
- app_wrk = app_worker_get_if_valid (ho_ctx->parent_app_wrk_index);
- if (app_wrk)
- {
- api_context = ho_ctx->parent_app_api_context;
- app_worker_connect_notify (app_wrk, 0, err, api_context);
- }
- tls_ctx_half_open_reader_unlock ();
- tls_ctx_half_open_free (ho_ctx_index);
- return rv;
- }
-
ctx_handle = tls_ctx_alloc (ho_ctx->tls_ctx_engine);
ctx = tls_ctx_get (ctx_handle);
clib_memcpy_fast (ctx, ho_ctx, sizeof (*ctx));
+ /* Half-open freed on tcp half-open cleanup notification */
tls_ctx_half_open_reader_unlock ();
- tls_ctx_half_open_free (ho_ctx_index);
ctx->c_thread_index = vlib_get_thread_index ();
ctx->tls_ctx_handle = ctx_handle;
{
tls_ctx_t *ctx;
- ctx = tls_ctx_get_w_thread (ctx_handle, 1 /* udp allocs on thread 1 */);
+ ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
ctx->tls_session_handle = session_handle (us);
ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
tls_session_connected_callback (u32 tls_app_index, u32 ho_ctx_index,
session_t *tls_session, session_error_t err)
{
+ if (err)
+ {
+ app_worker_t *app_wrk;
+ tls_ctx_t *ho_ctx;
+ u32 api_context;
+
+ ho_ctx = tls_ctx_half_open_get (ho_ctx_index);
+ app_wrk = app_worker_get_if_valid (ho_ctx->parent_app_wrk_index);
+ if (app_wrk)
+ {
+ api_context = ho_ctx->parent_app_api_context;
+ app_worker_connect_notify (app_wrk, 0, err, api_context);
+ }
+ tls_ctx_half_open_reader_unlock ();
+
+ return 0;
+ }
+
if (session_get_transport_proto (tls_session) == TRANSPORT_PROTO_TCP)
return tls_session_connected_cb (tls_app_index, ho_ctx_index, tls_session,
err);
us = session_get_from_handle (ctx->tls_session_handle);
us->opaque = ctx_handle;
us->flags &= ~SESSION_F_IS_MIGRATING;
+
+ /* Probably the app detached while the session was migrating. Cleanup */
+ if (session_half_open_migrated_notify (&ctx->connection))
+ {
+ ctx->no_app_session = 1;
+ tls_disconnect (ctx->tls_ctx_handle, vlib_get_thread_index ());
+ return;
+ }
+
if (svm_fifo_max_dequeue (us->tx_fifo))
session_send_io_evt_to_thread (us->tx_fifo, SESSION_IO_EVT_TX);
}
ctx->tls_session_handle = new_sh;
cloned_ctx = tls_ctx_detach (ctx);
ctx->is_migrated = 1;
+ session_half_open_migrate_notify (&ctx->connection);
session_send_rpc_evt_to_thread (new_thread, dtls_migrate_ctx,
(void *) cloned_ctx);
.session_disconnect_callback = tls_session_disconnect_callback,
.session_connected_callback = tls_session_connected_callback,
.session_reset_callback = tls_session_reset_callback,
+ .half_open_cleanup_callback = tls_session_cleanup_ho,
.add_segment_callback = tls_add_segment_callback,
.del_segment_callback = tls_del_segment_callback,
.builtin_app_rx_callback = tls_app_rx_callback,
ctx->tcp_is_ip4 = sep->is_ip4;
ctx->tls_type = sep->transport_proto;
ctx->ckpair_index = ccfg->ckpair_index;
+ ctx->c_proto = TRANSPORT_PROTO_TLS;
+ ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
if (ccfg->hostname[0])
{
ctx->srv_hostname = format (0, "%s", ccfg->hostname);
if ((rv = vnet_connect (cargs)))
return rv;
+ /* Track half-open tcp session in case we need to clean it up */
+ ctx->tls_session_handle = cargs->sh;
+
TLS_DBG (1, "New connect request %u engine %d", ctx_index, engine_type);
- return 0;
+ return ctx_index;
}
void
return &ctx->connection;
}
+static transport_connection_t *
+tls_half_open_get (u32 ho_index)
+{
+ tls_main_t *tm = &tls_main;
+ tls_ctx_t *ctx;
+ ctx = tls_ctx_half_open_get (ho_index);
+ clib_rwlock_reader_unlock (&tm->half_open_rwlock);
+ return &ctx->connection;
+}
+
+static void
+tls_cleanup_ho (u32 ho_index)
+{
+ tls_main_t *tm = &tls_main;
+ session_handle_t tcp_sh;
+ tls_ctx_t *ctx;
+
+ ctx = tls_ctx_half_open_get (ho_index);
+ tcp_sh = ctx->tls_session_handle;
+ clib_rwlock_reader_unlock (&tm->half_open_rwlock);
+ session_cleanup_half_open (tcp_sh);
+ tls_ctx_half_open_free (ho_index);
+}
+
int
tls_custom_tx_callback (void *session, transport_send_params_t * sp)
{
u8 *
format_tls_half_open (u8 * s, va_list * args)
{
- u32 tc_index = va_arg (*args, u32);
+ u32 ho_index = va_arg (*args, u32);
u32 __clib_unused thread_index = va_arg (*args, u32);
- tls_ctx_t *ctx = tls_ctx_half_open_get (tc_index);
- s = format (s, "[TLS] half-open app %u", ctx->parent_app_wrk_index);
+ session_t *tcp_ho;
+ tls_ctx_t *ho_ctx;
+
+ ho_ctx = tls_ctx_half_open_get (ho_index);
+
+ tcp_ho = session_get_from_handle (ho_ctx->tls_session_handle);
+ s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u ts %d:%d",
+ ho_ctx->c_thread_index, ho_ctx->c_s_index, "TLS",
+ ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
+ tcp_ho->thread_index, tcp_ho->session_index);
+
tls_ctx_half_open_reader_unlock ();
return s;
}
.stop_listen = tls_stop_listen,
.get_connection = tls_connection_get,
.get_listener = tls_listener_get,
+ .get_half_open = tls_half_open_get,
+ .cleanup_ho = tls_cleanup_ho,
.custom_tx = tls_custom_tx_callback,
.format_connection = format_tls_connection,
.format_half_open = format_tls_half_open,
.name = "tls",
.short_name = "J",
.tx_type = TRANSPORT_TX_INTERNAL,
- .service_type = TRANSPORT_SERVICE_APP,
+ .service_type = TRANSPORT_SERVICE_VC,
},
};
return -1;
}
- ctx_handle = tls_ctx_alloc_w_thread (engine_type, 1 /* because of udp */);
- ctx = tls_ctx_get_w_thread (ctx_handle, 1);
+ ctx_handle = tls_ctx_alloc_w_thread (engine_type, transport_cl_thread ());
+ ctx = tls_ctx_get_w_thread (ctx_handle, transport_cl_thread ());
ctx->parent_app_wrk_index = sep->app_wrk_index;
ctx->parent_app_api_context = sep->opaque;
ctx->tcp_is_ip4 = sep->is_ip4;
ctx->ckpair_index = ccfg->ckpair_index;
ctx->tls_type = sep->transport_proto;
ctx->tls_ctx_handle = ctx_handle;
+ ctx->c_proto = TRANSPORT_PROTO_DTLS;
+ ctx->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
if (ccfg->hostname[0])
{
ctx->srv_hostname = format (0, "%s", ccfg->hostname);
TLS_DBG (1, "New DTLS connect request %x engine %d", ctx_handle,
engine_type);
- return 0;
+ return ctx_handle;
+}
+
+static transport_connection_t *
+dtls_half_open_get (u32 ho_index)
+{
+ tls_ctx_t *ho_ctx;
+ ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
+ return &ho_ctx->connection;
}
static void
/* No op */
}
+static void
+dtls_cleanup_ho (u32 ho_index)
+{
+ tls_ctx_t *ctx;
+ ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
+ tls_ctx_free (ctx);
+}
+
+u8 *
+format_dtls_half_open (u8 *s, va_list *args)
+{
+ u32 ho_index = va_arg (*args, u32);
+ u32 __clib_unused thread_index = va_arg (*args, u32);
+ tls_ctx_t *ho_ctx;
+ session_t *us;
+
+ ho_ctx = tls_ctx_get_w_thread (ho_index, transport_cl_thread ());
+
+ us = session_get_from_handle (ho_ctx->tls_session_handle);
+ s = format (s, "[%d:%d][%s] half-open app_wrk %u engine %u us %d:%d",
+ ho_ctx->c_thread_index, ho_ctx->c_s_index, "DTLS",
+ ho_ctx->parent_app_wrk_index, ho_ctx->tls_ctx_engine,
+ us->thread_index, us->session_index);
+
+ return s;
+}
+
static const transport_proto_vft_t dtls_proto = {
.enable = 0,
.connect = dtls_connect,
.stop_listen = tls_stop_listen,
.get_connection = tls_connection_get,
.get_listener = tls_listener_get,
+ .get_half_open = dtls_half_open_get,
.custom_tx = tls_custom_tx_callback,
.cleanup = dtls_cleanup_callback,
+ .cleanup_ho = dtls_cleanup_ho,
.format_connection = format_tls_connection,
- .format_half_open = format_tls_half_open,
+ .format_half_open = format_dtls_half_open,
.format_listener = format_tls_listener,
.get_transport_endpoint = tls_transport_endpoint_get,
.get_transport_listener_endpoint = tls_transport_listener_endpoint_get,
.name = "dtls",
.short_name = "D",
.tx_type = TRANSPORT_TX_INTERNAL,
- .service_type = TRANSPORT_SERVICE_APP,
+ .service_type = TRANSPORT_SERVICE_VC,
},
};