X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fvnet%2Fsession%2Fsession_api.c;h=181312cc95228d341ae36da4a1e22d57e90bd8ea;hb=9a9adb2db318ca8043a7f337fedb020137d56610;hp=8116b673a32aa72b59ba2416256c4c4d1b6eccd4;hpb=db42b7bdefef4f26601782e9e2cc35096e847544;p=vpp.git diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c index 8116b673a32..181312cc952 100755 --- a/src/vnet/session/session_api.c +++ b/src/vnet/session/session_api.c @@ -47,10 +47,11 @@ _(DISCONNECT_SESSION, disconnect_session) \ _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ _(ACCEPT_SESSION_REPLY, accept_session_reply) \ _(RESET_SESSION_REPLY, reset_session_reply) \ -_(BIND_SOCK, bind_sock) \ +_(BIND_SOCK, bind_sock) \ _(UNBIND_SOCK, unbind_sock) \ _(CONNECT_SOCK, connect_sock) \ _(SESSION_ENABLE_DISABLE, session_enable_disable) \ +_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \ static int send_add_segment_callback (u32 api_client_index, const u8 * segment_name, @@ -96,15 +97,15 @@ send_session_accept_callback (stream_session_t * s) memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION); - + mp->context = server->index; listener = listen_session_get (s->session_type, s->listener_index); - tp_vft = session_get_transport_vft (s->session_type); + tp_vft = transport_protocol_get_vft (s->session_type); tc = tp_vft->get_connection (s->connection_index, s->thread_index); mp->listener_handle = listen_session_get_handle (listener); - mp->handle = stream_session_handle (s); - mp->server_rx_fifo = (u64) s->server_rx_fifo; - mp->server_tx_fifo = (u64) s->server_tx_fifo; - mp->vpp_event_queue_address = (u64) vpp_queue; + mp->handle = session_handle (s); + mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); + mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); + mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); mp->port = tc->rmt_port; mp->is_ip4 = tc->is_ip4; clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); @@ -128,7 +129,7 @@ send_session_disconnect_callback (stream_session_t * s) mp = vl_msg_api_alloc (sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION); - mp->handle = stream_session_handle (s); + mp->handle = session_handle (s); vl_msg_api_send_shmem (q, (u8 *) & mp); } @@ -147,18 +148,19 @@ send_session_reset_callback (stream_session_t * s) mp = vl_msg_api_alloc (sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION); - mp->handle = stream_session_handle (s); + mp->handle = session_handle (s); vl_msg_api_send_shmem (q, (u8 *) & mp); } -static int +int send_session_connected_callback (u32 app_index, u32 api_context, stream_session_t * s, u8 is_fail) { - vl_api_connect_uri_reply_t *mp; + vl_api_connect_session_reply_t *mp; unix_shared_memory_queue_t *q; application_t *app; unix_shared_memory_queue_t *vpp_queue; + transport_connection_t *tc; app = application_get (app_index); q = vl_api_client_index_to_input_queue (app->api_client_index); @@ -167,30 +169,32 @@ send_session_connected_callback (u32 app_index, u32 api_context, return -1; mp = vl_msg_api_alloc (sizeof (*mp)); - mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY); + mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY); mp->context = api_context; - if (!is_fail) - { - vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); - mp->server_rx_fifo = (u64) s->server_rx_fifo; - mp->server_tx_fifo = (u64) s->server_tx_fifo; - mp->handle = stream_session_handle (s); - mp->vpp_event_queue_address = (u64) vpp_queue; - mp->retval = 0; - } - else - { - mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT_FAIL); - } - vl_msg_api_send_shmem (q, (u8 *) & mp); + if (is_fail) + goto done; - /* Remove client if connect failed */ - if (!is_fail) + tc = session_get_transport (s); + if (!tc) { - s->session_state = SESSION_STATE_READY; + is_fail = 1; + goto done; } + vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); + mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); + mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); + mp->handle = session_handle (s); + mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); + clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); + mp->is_ip4 = tc->is_ip4; + mp->lcl_port = tc->lcl_port; + +done: + mp->retval = is_fail ? + clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0; + vl_msg_api_send_shmem (q, (u8 *) & mp); return 0; } @@ -202,7 +206,7 @@ send_session_connected_callback (u32 app_index, u32 api_context, static int redirect_connect_callback (u32 server_api_client_index, void *mp_arg) { - vl_api_connect_uri_t *mp = mp_arg; + vl_api_connect_sock_t *mp = mp_arg; unix_shared_memory_queue_t *server_q, *client_q; vlib_main_t *vm = vlib_get_main (); f64 timeout = vlib_time_now (vm) + 0.5; @@ -225,8 +229,14 @@ redirect_connect_callback (u32 server_api_client_index, void *mp_arg) } /* Tell the server the client's API queue address, so it can reply */ - mp->client_queue_address = (u64) client_q; + mp->client_queue_address = pointer_to_uword (client_q); app = application_lookup (mp->client_index); + if (!app) + { + clib_warning ("no client application"); + return -1; + } + mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size; mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size; @@ -243,7 +253,7 @@ redirect_connect_callback (u32 server_api_client_index, void *mp_arg) { /* correctly enqueued */ case 0: - return VNET_CONNECT_REDIRECTED; + return VNET_API_ERROR_SESSION_REDIRECT; /* continue spinning, wait for pthread_mutex_trylock to work */ case -1: @@ -261,7 +271,7 @@ out: return rv; } -static session_cb_vft_t uri_session_cb_vft = { +static session_cb_vft_t session_cb_vft = { .session_accept_callback = send_session_accept_callback, .session_disconnect_callback = send_session_disconnect_callback, .session_connected_callback = send_session_connected_callback, @@ -270,23 +280,6 @@ static session_cb_vft_t uri_session_cb_vft = { .redirect_connect_callback = redirect_connect_callback }; -static int -api_session_not_valid (u32 session_index, u32 thread_index) -{ - session_manager_main_t *smm = vnet_get_session_manager_main (); - stream_session_t *pool; - - if (thread_index >= vec_len (smm->sessions)) - return VNET_API_ERROR_INVALID_VALUE; - - pool = smm->sessions[thread_index]; - - if (pool_is_free_index (pool, session_index)) - return VNET_API_ERROR_INVALID_VALUE_2; - - return 0; -} - static void vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) { @@ -303,7 +296,8 @@ vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) { vl_api_application_attach_reply_t *rmp; vnet_app_attach_args_t _a, *a = &_a; - int rv; + clib_error_t *error = 0; + int rv = 0; if (session_manager_is_enabled () == 0) { @@ -316,21 +310,36 @@ vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) "Out of options, fix api message definition"); memset (a, 0, sizeof (*a)); - a->api_client_index = mp->client_index; a->options = mp->options; - a->session_cb_vft = &uri_session_cb_vft; + a->session_cb_vft = &session_cb_vft; - rv = vnet_application_attach (a); + if (mp->namespace_id_len > 64) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } + + if (mp->namespace_id_len) + { + vec_validate (a->namespace_id, mp->namespace_id_len - 1); + clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len); + } + + if ((error = vnet_application_attach (a))) + { + rv = clib_error_get_code (error); + clib_error_report (error); + } + vec_free (a->namespace_id); done: + /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({ - rmp->retval = rv; if (!rv) { rmp->segment_name_length = 0; - /* $$$$ policy? */ rmp->segment_size = a->segment_size; if (a->segment_name_length) { @@ -433,10 +442,11 @@ done: static void vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) { - vl_api_connect_uri_reply_t *rmp; + vl_api_connect_session_reply_t *rmp; vnet_connect_args_t _a, *a = &_a; application_t *app; - int rv; + clib_error_t *error = 0; + int rv = 0; if (session_manager_is_enabled () == 0) { @@ -451,21 +461,29 @@ vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) a->api_context = mp->context; a->app_index = app->index; a->mp = mp; - rv = vnet_connect_uri (a); + if ((error = vnet_connect_uri (a))) + { + rv = clib_error_get_code (error); + if (rv != VNET_API_ERROR_SESSION_REDIRECT) + clib_error_report (error); + } } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) + /* + * Don't reply to stream (tcp) connects. The reply will come once + * the connection is established. In case of the redirects, the reply + * will come from the server app. + */ + if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT) return; - /* Got some error, relay it */ - done: /* *INDENT-OFF* */ - REPLY_MACRO (VL_API_CONNECT_URI_REPLY); + REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY); /* *INDENT-ON* */ } @@ -534,8 +552,8 @@ vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) if (!app) return; - stream_session_parse_handle (mp->handle, &index, &thread_index); - s = stream_session_get_if_valid (index, thread_index); + session_parse_handle (mp->handle, &index, &thread_index); + s = session_get_if_valid (index, thread_index); if (s == 0 || app->index != s->app_index) { clib_warning ("Invalid session!"); @@ -558,24 +576,32 @@ static void vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) { stream_session_t *s; - int rv; u32 session_index, thread_index; - session_index = stream_session_index_from_handle (mp->handle); - thread_index = stream_session_thread_from_handle (mp->handle); - if (api_session_not_valid (session_index, thread_index)) - return; - - s = stream_session_get (session_index, thread_index); - rv = mp->retval; + vnet_disconnect_args_t _a, *a = &_a; - if (rv) + /* Server isn't interested, kill the session */ + if (mp->retval) { - /* Server isn't interested, kill the session */ - stream_session_disconnect (s); - return; + a->app_index = mp->context; + a->handle = mp->handle; + vnet_disconnect_session (a); + } + else + { + session_parse_handle (mp->handle, &session_index, &thread_index); + s = session_get_if_valid (session_index, thread_index); + if (!s) + { + clib_warning ("session doesn't exist"); + return; + } + if (s->app_index != mp->context) + { + clib_warning ("app doesn't own session"); + return; + } + s->session_state = SESSION_STATE_READY; } - - s->session_state = SESSION_STATE_READY; } static void @@ -590,7 +616,8 @@ vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) { vl_api_bind_sock_reply_t *rmp; vnet_bind_args_t _a, *a = &_a; - int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + int rv = 0; + clib_error_t *error; application_t *app; if (session_manager_is_enabled () == 0) @@ -602,19 +629,29 @@ vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) app = application_lookup (mp->client_index); if (app) { + ip46_address_t *ip46 = (ip46_address_t *) mp->ip; memset (a, 0, sizeof (*a)); - clib_memcpy (&a->tep.ip, mp->ip, (mp->is_ip4 ? - sizeof (ip4_address_t) : - sizeof (ip6_address_t))); - a->tep.is_ip4 = mp->is_ip4; - a->tep.port = mp->port; - a->tep.vrf = mp->vrf; + a->sep.is_ip4 = mp->is_ip4; + a->sep.ip = *ip46; + a->sep.port = mp->port; + a->sep.fib_index = mp->vrf; + a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; + a->sep.transport_proto = mp->proto; a->app_index = app->index; - rv = vnet_bind (a); + if ((error = vnet_bind (a))) + { + rv = clib_error_get_code (error); + clib_error_report (error); + } } done: - REPLY_MACRO (VL_API_BIND_SOCK_REPLY); + /* *INDENT-OFF* */ + REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({ + if (!rv) + rmp->handle = a->handle; + })); + /* *INDENT-ON* */ } static void @@ -623,7 +660,8 @@ vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) vl_api_unbind_sock_reply_t *rmp; vnet_unbind_args_t _a, *a = &_a; application_t *app; - int rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + clib_error_t *error; + int rv = 0; if (session_manager_is_enabled () == 0) { @@ -636,7 +674,11 @@ vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) { a->app_index = mp->client_index; a->handle = mp->handle; - rv = vnet_unbind (a); + if ((error = vnet_unbind (a))) + { + rv = clib_error_get_code (error); + clib_error_report (error); + } } done: @@ -646,10 +688,11 @@ done: static void vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) { - vl_api_connect_sock_reply_t *rmp; + vl_api_connect_session_reply_t *rmp; vnet_connect_args_t _a, *a = &_a; application_t *app; - int rv; + clib_error_t *error = 0; + int rv = 0; if (session_manager_is_enabled () == 0) { @@ -660,26 +703,79 @@ vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) app = application_lookup (mp->client_index); if (app) { - clib_memcpy (&a->tep.ip, mp->ip, - (mp->is_ip4 ? sizeof (ip4_address_t) : - sizeof (ip6_address_t))); + unix_shared_memory_queue_t *client_q; + ip46_address_t *ip46 = (ip46_address_t *) mp->ip; + + client_q = vl_api_client_index_to_input_queue (mp->client_index); + mp->client_queue_address = pointer_to_uword (client_q); + a->sep.is_ip4 = mp->is_ip4; + a->sep.ip = *ip46; + a->sep.port = mp->port; + a->sep.transport_proto = mp->proto; + a->sep.fib_index = mp->vrf; + a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; a->api_context = mp->context; a->app_index = app->index; a->mp = mp; - rv = vnet_connect (a); + if ((error = vnet_connect (a))) + { + rv = clib_error_get_code (error); + if (rv != VNET_API_ERROR_SESSION_REDIRECT) + clib_error_report (error); + } } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) + if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT) return; /* Got some error, relay it */ done: - REPLY_MACRO (VL_API_CONNECT_URI_REPLY); + REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY); +} + +static void +vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) +{ + vl_api_app_namespace_add_del_reply_t *rmp; + u8 *ns_id = 0; + clib_error_t *error = 0; + int rv = 0; + if (!session_manager_is_enabled ()) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } + + if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id)) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } + + vec_validate (ns_id, mp->namespace_id_len - 1); + clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len); + vnet_app_namespace_add_del_args_t args = { + .ns_id = ns_id, + .secret = clib_net_to_host_u64 (mp->secret), + .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index), + .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id), + .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id), + .is_add = 1 + }; + error = vnet_app_namespace_add_del (&args); + if (error) + { + rv = clib_error_get_code (error); + clib_error_report (error); + } + vec_free (ns_id); +done: + REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY); } static clib_error_t *