X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fvnet%2Fsession%2Fsession_api.c;h=d0af0fac27944a8b601d1ca61708baa3fbd30ca5;hb=cdb7170ab44affe51c574eb2218744fff1bdd369;hp=8852fc6ee08bdced50d57d60a2dcee9ca53e55c6;hpb=e04c29942af6a130591059679531c9ffa3d7237a;p=vpp.git diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c old mode 100644 new mode 100755 index 8852fc6ee08..d0af0fac279 --- a/src/vnet/session/session_api.c +++ b/src/vnet/session/session_api.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015-2016 Cisco and/or its affiliates. + * Copyright (c) 2015-2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -16,9 +16,13 @@ #include #include #include +#include +#include +#include +#include +#include #include -#include "application_interface.h" #define vl_typedefs /* define message structures */ #include @@ -38,411 +42,415 @@ #define foreach_session_api_msg \ _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \ +_(APPLICATION_ATTACH, application_attach) \ +_(APPLICATION_DETACH, application_detach) \ _(BIND_URI, bind_uri) \ _(UNBIND_URI, unbind_uri) \ _(CONNECT_URI, connect_uri) \ _(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) \ -_(DISCONNECT_SOCK, disconnect_sock) \ -_(DISCONNECT_SOCK_REPLY, disconnect_sock_reply) \ -_(ACCEPT_SOCK_REPLY, accept_sock_reply) \ -_(RESET_SOCK_REPLY, reset_sock_reply) \ _(SESSION_ENABLE_DISABLE, session_enable_disable) \ +_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \ +_(SESSION_RULE_ADD_DEL, session_rule_add_del) \ +_(SESSION_RULES_DUMP, session_rules_dump) \ +_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \ +_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \ +_(APP_WORKER_ADD_DEL, app_worker_add_del) \ +static int +session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds) +{ + clib_error_t *error; + if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) + { + clib_warning ("can't send memfd fd"); + return -1; + } + error = vl_api_send_fd_msg (reg, fds, n_fds); + if (error) + { + clib_error_report (error); + return -1; + } + return 0; +} static int -send_add_segment_callback (u32 api_client_index, const u8 * segment_name, - u32 segment_size) +send_add_segment_callback (u32 api_client_index, u64 segment_handle) { + int fds[SESSION_N_FD_TYPE], n_fds = 0; vl_api_map_another_segment_t *mp; - unix_shared_memory_queue_t *q; + svm_fifo_segment_private_t *fs; + vl_api_registration_t *reg; + ssvm_private_t *sp; + u8 fd_flags = 0; - q = vl_api_client_index_to_input_queue (api_client_index); + reg = vl_mem_api_client_index_to_registration (api_client_index); + if (!reg) + { + clib_warning ("no api registration for client: %u", api_client_index); + return -1; + } - if (!q) - return -1; + fs = segment_manager_get_segment_w_handle (segment_handle); + sp = &fs->ssvm; + if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD) + { + if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) + { + clib_warning ("can't send memfd fd"); + return -1; + } + + fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; + fds[n_fds] = sp->fd; + n_fds += 1; + } - mp = vl_msg_api_alloc (sizeof (*mp)); - memset (mp, 0, sizeof (*mp)); + mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); + clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT); - mp->segment_size = segment_size; - strncpy ((char *) mp->segment_name, (char *) segment_name, + mp->segment_size = sp->ssvm_size; + mp->fd_flags = fd_flags; + mp->segment_handle = clib_host_to_net_u64 (segment_handle); + strncpy ((char *) mp->segment_name, (char *) sp->name, sizeof (mp->segment_name) - 1); - vl_msg_api_send_shmem (q, (u8 *) & mp); + vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); + + if (n_fds) + return session_send_fds (reg, fds, n_fds); return 0; } static int -send_session_accept_uri_callback (stream_session_t * s) +send_del_segment_callback (u32 api_client_index, u64 segment_handle) { - vl_api_accept_session_t *mp; - unix_shared_memory_queue_t *q, *vpp_queue; - application_t *server = application_get (s->app_index); - - q = vl_api_client_index_to_input_queue (server->api_client_index); - vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); - - if (!q) - return -1; + vl_api_unmap_segment_t *mp; + vl_api_registration_t *reg; - mp = vl_msg_api_alloc (sizeof (*mp)); - mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION); - - /* Note: session_type is the first octet in all types of sessions */ + reg = vl_mem_api_client_index_to_registration (api_client_index); + if (!reg) + { + clib_warning ("no registration: %u", api_client_index); + return -1; + } - mp->accept_cookie = server->accept_cookie; - mp->server_rx_fifo = (u64) s->server_rx_fifo; - mp->server_tx_fifo = (u64) s->server_tx_fifo; - mp->session_thread_index = s->thread_index; - mp->session_index = s->session_index; - mp->session_type = s->session_type; - mp->vpp_event_queue_address = (u64) vpp_queue; - vl_msg_api_send_shmem (q, (u8 *) & mp); + mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); + clib_memset (mp, 0, sizeof (*mp)); + mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT); + mp->segment_handle = clib_host_to_net_u64 (segment_handle); + vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); return 0; } -static void -send_session_disconnect_uri_callback (stream_session_t * s) +static int +mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg) { - vl_api_disconnect_session_t *mp; - unix_shared_memory_queue_t *q; - application_t *app = application_get (s->app_index); - - q = vl_api_client_index_to_input_queue (app->api_client_index); - - if (!q) - return; - - 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->session_thread_index = s->thread_index; - mp->session_index = s->session_index; - vl_msg_api_send_shmem (q, (u8 *) & mp); + int rv; + u8 try = 0; + while (try < 100) + { + rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, + SESSION_MQ_CTRL_EVT_RING, + SVM_Q_NOWAIT, msg); + if (!rv) + return 0; + try++; + usleep (1); + } + clib_warning ("failed to alloc msg"); + return -1; } static int -send_session_connected_uri_callback (u32 api_client_index, - stream_session_t * s, u8 is_fail) +mq_send_session_accepted_cb (session_t * s) { - vl_api_connect_uri_reply_t *mp; - unix_shared_memory_queue_t *q; - application_t *app = application_lookup (api_client_index); - u8 *seg_name; - unix_shared_memory_queue_t *vpp_queue; - - q = vl_api_client_index_to_input_queue (app->api_client_index); - - if (!q) + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); + svm_msg_q_msg_t _msg, *msg = &_msg; + svm_msg_q_t *vpp_queue, *app_mq; + session_t *listener; + session_accepted_msg_t *mp; + session_event_t *evt; + application_t *app; + app_listener_t *al; + + app = application_get (app_wrk->app_index); + app_mq = app_wrk->event_queue; + if (mq_try_lock_and_alloc_msg (app_mq, msg)) return -1; - mp = vl_msg_api_alloc (sizeof (*mp)); - mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY); - mp->context = app->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->session_thread_index = s->thread_index; - mp->session_index = s->session_index; - mp->session_type = s->session_type; - mp->vpp_event_queue_address = (u64) vpp_queue; - mp->client_event_queue_address = (u64) app->event_queue; - mp->retval = 0; - - session_manager_get_segment_info (s->server_segment_index, &seg_name, - &mp->segment_size); - mp->segment_name_length = vec_len (seg_name); - if (mp->segment_name_length) - clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length); - } - else + evt = svm_msg_q_msg_data (app_mq, msg); + clib_memset (evt, 0, sizeof (*evt)); + evt->event_type = SESSION_CTRL_EVT_ACCEPTED; + mp = (session_accepted_msg_t *) evt->data; + clib_memset (mp, 0, sizeof (*mp)); + mp->context = app->app_index; + mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); + mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); + mp->segment_handle = session_segment_handle (s); + + if (session_has_transport (s)) { - mp->retval = VNET_API_ERROR_SESSION_CONNECT_FAIL; - } - - vl_msg_api_send_shmem (q, (u8 *) & mp); + listener = listen_session_get (s->listener_index); + al = app_listener_get (app, listener->al_index); + mp->listener_handle = app_listener_handle (al); + if (application_is_proxy (app)) + { + listener = + app_worker_first_listener (app_wrk, session_get_fib_proto (s), + session_get_transport_proto (s)); + if (listener) + mp->listener_handle = listen_session_get_handle (listener); + } + vpp_queue = session_main_get_vpp_event_queue (s->thread_index); + mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); + mp->handle = session_handle (s); - /* Remove client if connect failed */ - if (is_fail) - { - application_del (app); + session_get_endpoint (s, (ip46_address_t *) mp->ip, &mp->port, + &mp->is_ip4, 0 /* is_lcl */ ); } else { - s->session_state = SESSION_STATE_READY; + ct_connection_t *ct; + + ct = (ct_connection_t *) session_get_transport (s); + listener = listen_session_get (s->listener_index); + al = app_listener_get (app, listener->al_index); + mp->listener_handle = app_listener_handle (al); + mp->is_ip4 = session_type_is_ip4 (listener->session_type); + mp->handle = session_handle (s); + mp->port = ct->c_rmt_port; + vpp_queue = session_main_get_vpp_event_queue (0); + mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); } + svm_msg_q_add_and_unlock (app_mq, msg); return 0; } -/** - * Redirect a connect_uri message to the indicated server. - * Only sent if the server has bound the related port with - * URI_OPTIONS_FLAGS_USE_FIFO - */ -static int -redirect_connect_uri_callback (u32 server_api_client_index, void *mp_arg) +static inline void +mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh, + session_evt_type_t evt_type) { - vl_api_connect_uri_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; - int rv = 0; - - server_q = vl_api_client_index_to_input_queue (server_api_client_index); - - if (!server_q) - { - rv = VNET_API_ERROR_INVALID_VALUE; - goto out; - } + svm_msg_q_msg_t _msg, *msg = &_msg; + session_disconnected_msg_t *mp; + svm_msg_q_t *app_mq; + session_event_t *evt; - client_q = vl_api_client_index_to_input_queue (mp->client_index); - if (!client_q) - { - rv = VNET_API_ERROR_INVALID_VALUE_2; - goto out; - } + app_mq = app_wrk->event_queue; + if (mq_try_lock_and_alloc_msg (app_mq, msg)) + return; + evt = svm_msg_q_msg_data (app_mq, msg); + clib_memset (evt, 0, sizeof (*evt)); + evt->event_type = evt_type; + mp = (session_disconnected_msg_t *) evt->data; + mp->handle = sh; + mp->context = app_wrk->api_client_index; + svm_msg_q_add_and_unlock (app_mq, msg); +} - /* Tell the server the client's API queue address, so it can reply */ - mp->client_queue_address = (u64) client_q; +static inline void +mq_notify_close_subscribers (u32 app_index, session_handle_t sh, + svm_fifo_t * f, session_evt_type_t evt_type) +{ + app_worker_t *app_wrk; + application_t *app; + int i; - /* - * Bounce message handlers MUST NOT block the data-plane. - * Spin waiting for the queue lock, but - */ + app = application_get (app_index); + if (!app) + return; - while (vlib_time_now (vm) < timeout) + for (i = 0; i < f->n_subscribers; i++) { - rv = - unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ ); - switch (rv) - { - /* correctly enqueued */ - case 0: - return VNET_CONNECT_REDIRECTED; - - /* continue spinning, wait for pthread_mutex_trylock to work */ - case -1: - continue; - - /* queue stuffed, drop the msg */ - case -2: - rv = VNET_API_ERROR_QUEUE_FULL; - goto out; - } + if (!(app_wrk = application_get_worker (app, f->subscribers[i]))) + continue; + mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED); } -out: - /* Dispose of the message */ - vl_msg_api_free (mp); - return rv; -} - -static u64 -make_session_handle (stream_session_t * s) -{ - return (u64) s->session_index << 32 | (u64) s->thread_index; } -static int -send_session_accept_callback (stream_session_t * s) +static void +mq_send_session_disconnected_cb (session_t * s) { - vl_api_accept_sock_t *mp; - unix_shared_memory_queue_t *q, *vpp_queue; - application_t *server = application_get (s->app_index); + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); + session_handle_t sh = session_handle (s); - q = vl_api_client_index_to_input_queue (server->api_client_index); - vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); - - if (!q) - return -1; + mq_send_session_close_evt (app_wrk, session_handle (s), + SESSION_CTRL_EVT_DISCONNECTED); - mp = vl_msg_api_alloc (sizeof (*mp)); - mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SOCK); + if (svm_fifo_n_subscribers (s->rx_fifo)) + mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo, + SESSION_CTRL_EVT_DISCONNECTED); +} - /* Note: session_type is the first octet in all types of sessions */ +static void +mq_send_session_reset_cb (session_t * s) +{ + app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); + session_handle_t sh = session_handle (s); - mp->accept_cookie = server->accept_cookie; - mp->server_rx_fifo = (u64) s->server_rx_fifo; - mp->server_tx_fifo = (u64) s->server_tx_fifo; - mp->handle = make_session_handle (s); - mp->vpp_event_queue_address = (u64) vpp_queue; - vl_msg_api_send_shmem (q, (u8 *) & mp); + mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET); - return 0; + if (svm_fifo_n_subscribers (s->rx_fifo)) + mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo, + SESSION_CTRL_EVT_RESET); } static int -send_session_connected_callback (u32 api_client_index, stream_session_t * s, - u8 is_fail) +mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context, + session_t * s, u8 is_fail) { - vl_api_connect_sock_reply_t *mp; - unix_shared_memory_queue_t *q; - application_t *app = application_lookup (api_client_index); - u8 *seg_name; - unix_shared_memory_queue_t *vpp_queue; - - q = vl_api_client_index_to_input_queue (app->api_client_index); - - if (!q) - return -1; - - mp = vl_msg_api_alloc (sizeof (*mp)); - mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SOCK_REPLY); - mp->context = app->api_context; - mp->retval = is_fail; - if (!is_fail) + svm_msg_q_msg_t _msg, *msg = &_msg; + session_connected_msg_t *mp; + svm_msg_q_t *vpp_mq, *app_mq; + transport_connection_t *tc; + app_worker_t *app_wrk; + session_event_t *evt; + + app_wrk = app_worker_get (app_wrk_index); + app_mq = app_wrk->event_queue; + if (!app_mq) { - 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 = make_session_handle (s); - mp->vpp_event_queue_address = (u64) vpp_queue; - mp->client_event_queue_address = (u64) app->event_queue; - - session_manager_get_segment_info (s->server_segment_index, &seg_name, - &mp->segment_size); - mp->segment_name_length = vec_len (seg_name); - if (mp->segment_name_length) - clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length); + clib_warning ("app %u with api index: %u not attached", + app_wrk->app_index, app_wrk->api_client_index); + return -1; } - vl_msg_api_send_shmem (q, (u8 *) & mp); + if (mq_try_lock_and_alloc_msg (app_mq, msg)) + return -1; + + evt = svm_msg_q_msg_data (app_mq, msg); + clib_memset (evt, 0, sizeof (*evt)); + evt->event_type = SESSION_CTRL_EVT_CONNECTED; + mp = (session_connected_msg_t *) evt->data; + clib_memset (mp, 0, sizeof (*mp)); + mp->context = api_context; - /* Remove client if connect failed */ if (is_fail) - application_del (app); + goto done; - return 0; -} + if (session_has_transport (s)) + { + tc = session_get_transport (s); + if (!tc) + { + is_fail = 1; + goto done; + } -static void -send_session_disconnect_callback (stream_session_t * s) -{ - vl_api_disconnect_sock_t *mp; - unix_shared_memory_queue_t *q; - application_t *app = application_get (s->app_index); + vpp_mq = session_main_get_vpp_event_queue (s->thread_index); + mp->handle = session_handle (s); + mp->vpp_event_queue_address = pointer_to_uword (vpp_mq); - q = vl_api_client_index_to_input_queue (app->api_client_index); + session_get_endpoint (s, (ip46_address_t *) mp->lcl_ip, &mp->lcl_port, + &mp->is_ip4, 1 /* is_lcl */ ); - if (!q) - return; + mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); + mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); + mp->segment_handle = session_segment_handle (s); + } + else + { + ct_connection_t *cct; + session_t *ss; + + cct = (ct_connection_t *) session_get_transport (s); + mp->handle = session_handle (s); + mp->lcl_port = cct->c_lcl_port; + mp->is_ip4 = cct->c_is_ip4; + vpp_mq = session_main_get_vpp_event_queue (0); + mp->vpp_event_queue_address = pointer_to_uword (vpp_mq); + mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); + mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); + mp->segment_handle = session_segment_handle (s); + ss = ct_session_get_peer (s); + mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo); + mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo); + mp->ct_segment_handle = session_segment_handle (ss); + } - mp = vl_msg_api_alloc (sizeof (*mp)); - memset (mp, 0, sizeof (*mp)); - mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SOCK); +done: + mp->retval = is_fail ? + clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0; - mp->handle = make_session_handle (s); - vl_msg_api_send_shmem (q, (u8 *) & mp); + svm_msg_q_add_and_unlock (app_mq, msg); + return 0; } -/** - * Redirect a connect_uri message to the indicated server. - * Only sent if the server has bound the related port with - * URI_OPTIONS_FLAGS_USE_FIFO - */ static int -redirect_connect_callback (u32 server_api_client_index, void *mp_arg) +mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context, + session_handle_t handle, int rv) { - 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; - int rv = 0; - - server_q = vl_api_client_index_to_input_queue (server_api_client_index); - - if (!server_q) + svm_msg_q_msg_t _msg, *msg = &_msg; + svm_msg_q_t *app_mq, *vpp_evt_q; + transport_connection_t *tc; + session_bound_msg_t *mp; + app_worker_t *app_wrk; + session_event_t *evt; + app_listener_t *al; + session_t *ls = 0; + app_wrk = app_worker_get (app_wrk_index); + app_mq = app_wrk->event_queue; + if (!app_mq) { - rv = VNET_API_ERROR_INVALID_VALUE; - goto out; + clib_warning ("app %u with api index: %u not attached", + app_wrk->app_index, app_wrk->api_client_index); + return -1; } - client_q = vl_api_client_index_to_input_queue (mp->client_index); - if (!client_q) - { - rv = VNET_API_ERROR_INVALID_VALUE_2; - goto out; - } + if (mq_try_lock_and_alloc_msg (app_mq, msg)) + return -1; - /* Tell the server the client's API queue address, so it can reply */ - mp->client_queue_address = (u64) client_q; + evt = svm_msg_q_msg_data (app_mq, msg); + clib_memset (evt, 0, sizeof (*evt)); + evt->event_type = SESSION_CTRL_EVT_BOUND; + mp = (session_bound_msg_t *) evt->data; + mp->context = api_context; - /* - * Bounce message handlers MUST NOT block the data-plane. - * Spin waiting for the queue lock, but - */ + if (rv) + goto done; + + mp->handle = handle; + al = app_listener_get_w_handle (handle); + if (al->session_index != SESSION_INVALID_INDEX) + ls = app_listener_get_session (al); + else + ls = app_listener_get_local_session (al); + tc = listen_session_get_transport (ls); + mp->lcl_port = tc->lcl_port; + mp->lcl_is_ip4 = tc->is_ip4; + clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); - while (vlib_time_now (vm) < timeout) + vpp_evt_q = session_main_get_vpp_event_queue (0); + mp->vpp_evt_q = pointer_to_uword (vpp_evt_q); + + if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL) { - rv = - unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ ); - switch (rv) - { - /* correctly enqueued */ - case 0: - return VNET_CONNECT_REDIRECTED; - - /* continue spinning, wait for pthread_mutex_trylock to work */ - case -1: - continue; - - /* queue stuffed, drop the msg */ - case -2: - rv = VNET_API_ERROR_QUEUE_FULL; - goto out; - } + mp->rx_fifo = pointer_to_uword (ls->rx_fifo); + mp->tx_fifo = pointer_to_uword (ls->tx_fifo); } -out: - /* Dispose of the message */ - vl_msg_api_free (mp); - return rv; -} -static session_cb_vft_t uri_session_cb_vft = { - .session_accept_callback = send_session_accept_uri_callback, - .session_disconnect_callback = send_session_disconnect_uri_callback, - .session_connected_callback = send_session_connected_uri_callback, - .add_segment_callback = send_add_segment_callback, - .redirect_connect_callback = redirect_connect_uri_callback -}; +done: + mp->retval = rv; + svm_msg_q_add_and_unlock (app_mq, msg); + return 0; +} -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, +static session_cb_vft_t session_mq_cb_vft = { + .session_accept_callback = mq_send_session_accepted_cb, + .session_disconnect_callback = mq_send_session_disconnected_cb, + .session_connected_callback = mq_send_session_connected_cb, + .session_reset_callback = mq_send_session_reset_cb, .add_segment_callback = send_add_segment_callback, - .redirect_connect_callback = redirect_connect_callback + .del_segment_callback = send_del_segment_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) { @@ -455,172 +463,291 @@ vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) } static void -vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) +vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) { - vl_api_bind_uri_reply_t *rmp; - vnet_bind_args_t _a, *a = &_a; - char segment_name[128]; - u32 segment_name_length; - int rv; - - _Static_assert (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <= - sizeof (mp->options), - "Out of options, fix api message definition"); + int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; + vl_api_application_attach_reply_t *rmp; + ssvm_private_t *segp, *evt_q_segment; + vnet_app_attach_args_t _a, *a = &_a; + vl_api_registration_t *reg; + u8 fd_flags = 0; + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; - segment_name_length = ARRAY_LEN (segment_name); + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - memset (a, 0, sizeof (*a)); + STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <= + sizeof (mp->options), + "Out of options, fix api message definition"); - a->uri = (char *) mp->uri; + clib_memset (a, 0, sizeof (*a)); a->api_client_index = mp->client_index; a->options = mp->options; - a->segment_name = segment_name; - a->segment_name_length = segment_name_length; - a->session_cb_vft = &uri_session_cb_vft; + a->session_cb_vft = &session_mq_cb_vft; + if (mp->namespace_id_len > 64) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } - a->options[SESSION_OPTIONS_SEGMENT_SIZE] = mp->initial_segment_size; - a->options[SESSION_OPTIONS_ACCEPT_COOKIE] = mp->accept_cookie; - rv = vnet_bind_uri (a); + if (mp->namespace_id_len) + { + vec_validate (a->namespace_id, mp->namespace_id_len - 1); + clib_memcpy_fast (a->namespace_id, mp->namespace_id, + mp->namespace_id_len); + } + + if ((rv = vnet_application_attach (a))) + { + clib_warning ("attach returned: %d", rv); + vec_free (a->namespace_id); + goto done; + } + vec_free (a->namespace_id); + + /* Send event queues segment */ + if ((evt_q_segment = session_main_get_evt_q_segment ())) + { + fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT; + fds[n_fds] = evt_q_segment->fd; + n_fds += 1; + } + /* Send fifo segment fd if needed */ + if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) + { + fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; + fds[n_fds] = a->segment->fd; + n_fds += 1; + } + if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) + { + fd_flags |= SESSION_FD_F_MQ_EVENTFD; + fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q); + n_fds += 1; + } + +done: /* *INDENT-OFF* */ - REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({ - rmp->retval = rv; + REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({ if (!rv) { + segp = a->segment; + rmp->app_index = clib_host_to_net_u32 (a->app_index); rmp->segment_name_length = 0; - /* $$$$ policy? */ - rmp->segment_size = mp->initial_segment_size; - if (segment_name_length) + rmp->segment_size = segp->ssvm_size; + if (vec_len (segp->name)) { - memcpy (rmp->segment_name, segment_name, segment_name_length); - rmp->segment_name_length = segment_name_length; + memcpy (rmp->segment_name, segp->name, vec_len (segp->name)); + rmp->segment_name_length = vec_len (segp->name); } - rmp->server_event_queue_address = a->server_event_queue_address; + rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q); + rmp->n_fds = n_fds; + rmp->fd_flags = fd_flags; + rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle); } })); /* *INDENT-ON* */ + + if (n_fds) + session_send_fds (reg, fds, n_fds); } static void -vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) +vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) { - vl_api_unbind_uri_reply_t *rmp; - int rv; + vl_api_application_detach_reply_t *rmp; + int rv = VNET_API_ERROR_INVALID_VALUE_2; + vnet_app_detach_args_t _a, *a = &_a; + application_t *app; + + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - rv = vnet_unbind_uri ((char *) mp->uri, mp->client_index); + app = application_lookup (mp->client_index); + if (app) + { + a->app_index = app->app_index; + a->api_client_index = mp->client_index; + rv = vnet_application_detach (a); + } - REPLY_MACRO (VL_API_UNBIND_URI_REPLY); +done: + REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); } static void -vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) +vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) { - vl_api_connect_uri_reply_t *rmp; - vnet_connect_args_t _a, *a = &_a; + vl_api_bind_uri_reply_t *rmp; + vnet_listen_args_t _a, *a = &_a; + application_t *app = 0; + app_worker_t *app_wrk; int rv; - a->uri = (char *) mp->uri; - a->api_client_index = mp->client_index; - a->api_context = mp->context; - a->options = mp->options; - a->session_cb_vft = &uri_session_cb_vft; - a->mp = mp; + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - rv = vnet_connect_uri (a); + app = application_lookup (mp->client_index); + if (app) + { + clib_memset (a, 0, sizeof (*a)); + a->uri = (char *) mp->uri; + a->app_index = app->app_index; + rv = vnet_bind_uri (a); + } + else + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + } - if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) - return; +done: - /* Got some error, relay it */ + REPLY_MACRO (VL_API_BIND_URI_REPLY); - /* *INDENT-OFF* */ - REPLY_MACRO2 (VL_API_CONNECT_URI_REPLY, ({ - rmp->retval = rv; - })); - /* *INDENT-ON* */ + if (app) + { + app_wrk = application_get_worker (app, 0); + mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, + rv); + } } static void -vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) +vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) { - vl_api_disconnect_session_reply_t *rmp; + vl_api_unbind_uri_reply_t *rmp; + application_t *app; + vnet_unlisten_args_t _a, *a = &_a; int rv; - rv = api_session_not_valid (mp->session_index, mp->session_thread_index); - if (!rv) - rv = vnet_disconnect_session (mp->client_index, mp->session_index, - mp->session_thread_index); + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } + + app = application_lookup (mp->client_index); + if (app) + { + a->uri = (char *) mp->uri; + a->app_index = app->app_index; + rv = vnet_unbind_uri (a); + } + else + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + } - REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY); +done: + REPLY_MACRO (VL_API_UNBIND_URI_REPLY); } static void -vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * - mp) +vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) { - if (api_session_not_valid (mp->session_index, mp->session_thread_index)) + vl_api_connect_uri_reply_t *rmp; + vnet_connect_args_t _a, *a = &_a; + application_t *app; + int rv = 0; + + if (session_main_is_enabled () == 0) { - clib_warning ("Invalid session!"); - return; + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; } - /* Client objected to disconnecting the session, log and continue */ - if (mp->retval) + app = application_lookup (mp->client_index); + if (app) { - clib_warning ("client retval %d", mp->retval); - return; + clib_memset (a, 0, sizeof (*a)); + a->uri = (char *) mp->uri; + a->api_context = mp->context; + a->app_index = app->app_index; + if ((rv = vnet_connect_uri (a))) + clib_warning ("connect_uri returned: %d", rv); + } + else + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - /* Disconnect has been confirmed. Confirm close to transport */ - vnet_disconnect_session (mp->client_index, mp->session_index, - mp->session_thread_index); + /* + * 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) + return; + +done: + REPLY_MACRO (VL_API_CONNECT_URI_REPLY); } static void -vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) +vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) { - stream_session_t *s; + vl_api_disconnect_session_reply_t *rmp; + vnet_disconnect_args_t _a, *a = &_a; + application_t *app; + int rv = 0; - if (api_session_not_valid (mp->session_index, mp->session_thread_index)) + if (session_main_is_enabled () == 0) { - clib_warning ("Invalid session!"); - return; + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; } - /* Client objected to resetting the session, log and continue */ - if (mp->retval) + app = application_lookup (mp->client_index); + if (app) { - clib_warning ("client retval %d", mp->retval); - return; + a->handle = mp->handle; + a->app_index = app->app_index; + rv = vnet_disconnect_session (a); + } + else + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } - s = stream_session_get (mp->session_index, mp->session_thread_index); - - /* This comes as a response to a reset, transport only waiting for - * confirmation to remove connection state, no need to disconnect */ - stream_session_cleanup (s); +done: + REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle); } static void -vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) +vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * + mp) { - stream_session_t *s; - int rv; - - if (api_session_not_valid (mp->session_index, mp->session_thread_index)) - return; - - s = stream_session_get (mp->session_index, mp->session_thread_index); - rv = mp->retval; + vnet_disconnect_args_t _a, *a = &_a; + application_t *app; - if (rv) + /* Client objected to disconnecting the session, log and continue */ + if (mp->retval) { - /* Server isn't interested, kill the session */ - stream_session_disconnect (s); + clib_warning ("client retval %d", mp->retval); return; } - s->session_state = SESSION_STATE_READY; + /* Disconnect has been confirmed. Confirm close to transport */ + app = application_lookup (mp->context); + if (app) + { + a->handle = mp->handle; + a->app_index = app->app_index; + vnet_disconnect_session (a); + } } static void @@ -633,66 +760,107 @@ vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t static void vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) { + vnet_listen_args_t _a, *a = &_a; vl_api_bind_sock_reply_t *rmp; - vnet_bind_args_t _a, *a = &_a; - char segment_name[128]; - u32 segment_name_length; - int rv; - - STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <= - sizeof (mp->options), - "Out of options, fix api message definition"); - - segment_name_length = ARRAY_LEN (segment_name); - - 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; + application_t *app = 0; + app_worker_t *app_wrk; + ip46_address_t *ip46; + int rv = 0; - a->api_client_index = mp->client_index; - a->options = mp->options; - a->segment_name = segment_name; - a->segment_name_length = segment_name_length; - a->session_cb_vft = &session_cb_vft; + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - rv = vnet_bind_uri (a); + app = application_lookup (mp->client_index); + if (!app) + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + goto done; + } - /* *INDENT-OFF* */ - REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY, ({ - rmp->retval = rv; - if (!rv) - { - rmp->segment_name_length = 0; - rmp->segment_size = mp->options[SESSION_OPTIONS_SEGMENT_SIZE]; - if (segment_name_length) - { - memcpy(rmp->segment_name, segment_name, segment_name_length); - rmp->segment_name_length = segment_name_length; - } - rmp->server_event_queue_address = a->server_event_queue_address; - } - })); - /* *INDENT-ON* */ + ip46 = (ip46_address_t *) mp->ip; + clib_memset (a, 0, sizeof (*a)); + 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->app_index; + a->wrk_map_index = mp->wrk_index; + + if ((rv = vnet_listen (a))) + clib_warning ("listen returned: %d", rv); + +done: + /* Actual reply sent only over mq */ + REPLY_MACRO (VL_API_BIND_SOCK_REPLY); + + if (app) + { + app_wrk = application_get_worker (app, mp->wrk_index); + mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, + rv); + } } static void 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; - int rv; + vnet_unlisten_args_t _a, *a = &_a; + app_worker_t *app_wrk; + application_t *app = 0; + int rv = 0; - a->api_client_index = mp->client_index; - a->handle = mp->handle; + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - rv = vnet_unbind (a); + app = application_lookup (mp->client_index); + if (app) + { + a->app_index = app->app_index; + a->handle = mp->handle; + a->wrk_map_index = mp->wrk_index; + if ((rv = vnet_unlisten (a))) + clib_warning ("unlisten returned: %d", rv); + } +done: REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY); + + /* + * Send reply over msg queue + */ + svm_msg_q_msg_t _msg, *msg = &_msg; + session_unlisten_reply_msg_t *ump; + svm_msg_q_t *app_mq; + session_event_t *evt; + + if (!app) + return; + + app_wrk = application_get_worker (app, a->wrk_map_index); + if (!app_wrk) + return; + + app_mq = app_wrk->event_queue; + if (mq_try_lock_and_alloc_msg (app_mq, msg)) + return; + + evt = svm_msg_q_msg_data (app_mq, msg); + clib_memset (evt, 0, sizeof (*evt)); + evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY; + ump = (session_unlisten_reply_msg_t *) evt->data; + ump->context = mp->context; + ump->handle = mp->handle; + ump->retval = rv; + svm_msg_q_add_and_unlock (app_mq, msg); } static void @@ -700,114 +868,464 @@ vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) { vl_api_connect_sock_reply_t *rmp; vnet_connect_args_t _a, *a = &_a; - int rv; + application_t *app = 0; + int rv = 0; - 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->options = mp->options; - a->session_cb_vft = &session_cb_vft; - a->api_context = mp->context; - a->mp = mp; + if (session_main_is_enabled () == 0) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - rv = vnet_connect (a); + app = application_lookup (mp->client_index); + if (app) + { + svm_queue_t *client_q; + ip46_address_t *ip46 = (ip46_address_t *) mp->ip; + + clib_memset (a, 0, sizeof (*a)); + 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.peer.fib_index = mp->vrf; + a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX; + if (mp->hostname_len) + { + vec_validate (a->sep_ext.hostname, mp->hostname_len - 1); + clib_memcpy_fast (a->sep_ext.hostname, mp->hostname, + mp->hostname_len); + } + a->api_context = mp->context; + a->app_index = app->app_index; + a->wrk_map_index = mp->wrk_index; + if ((rv = vnet_connect (a))) + clib_warning ("connect returned: %u", rv); + vec_free (a->sep_ext.hostname); + } + else + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + } - if (rv == 0 || rv == VNET_CONNECT_REDIRECTED) + if (rv == 0) return; /* Got some error, relay it */ +done: + REPLY_MACRO (VL_API_CONNECT_SOCK_REPLY); + + if (app) + { + app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index); + mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1); + } +} + +static void +vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp) +{ + int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; + vl_api_app_worker_add_del_reply_t *rmp; + vl_api_registration_t *reg; + application_t *app; + u8 fd_flags = 0; + + if (!session_main_is_enabled ()) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index)); + if (!app) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } + + vnet_app_worker_add_del_args_t args = { + .app_index = app->app_index, + .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index), + .api_client_index = mp->client_index, + .is_add = mp->is_add + }; + rv = vnet_app_worker_add_del (&args); + if (rv) + { + clib_warning ("app worker add/del returned: %d", rv); + goto done; + } + + if (!mp->is_add) + goto done; + + /* Send fifo segment fd if needed */ + if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) + { + fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; + fds[n_fds] = args.segment->fd; + n_fds += 1; + } + if (application_segment_manager_properties (app)->use_mq_eventfd) + { + fd_flags |= SESSION_FD_F_MQ_EVENTFD; + fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q); + n_fds += 1; + } + /* *INDENT-OFF* */ - REPLY_MACRO2 (VL_API_CONNECT_URI_REPLY, ({ - rmp->retval = rv; +done: + REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({ + rmp->is_add = mp->is_add; + rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index); + rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle); + if (!rv && mp->is_add) + { + if (vec_len (args.segment->name)) + { + memcpy (rmp->segment_name, args.segment->name, + vec_len (args.segment->name)); + rmp->segment_name_length = vec_len (args.segment->name); + } + rmp->app_event_queue_address = pointer_to_uword (args.evt_q); + rmp->n_fds = n_fds; + rmp->fd_flags = fd_flags; + } })); /* *INDENT-ON* */ + + if (n_fds) + session_send_fds (reg, fds, n_fds); } static void -vl_api_disconnect_sock_t_handler (vl_api_disconnect_sock_t * mp) +vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) { - vnet_disconnect_args_t _a, *a = &_a; - vl_api_disconnect_sock_reply_t *rmp; - int rv; + vl_api_app_namespace_add_del_reply_t *rmp; + u32 appns_index = 0; + u8 *ns_id = 0; + int rv = 0; + if (!session_main_is_enabled ()) + { + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; + } - a->api_client_index = mp->client_index; - a->handle = mp->handle; - rv = vnet_disconnect (a); + 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_fast (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 + }; + rv = vnet_app_namespace_add_del (&args); + if (!rv) + { + appns_index = app_namespace_index_from_id (ns_id); + if (appns_index == APP_NAMESPACE_INVALID_INDEX) + { + clib_warning ("app ns lookup failed"); + rv = VNET_API_ERROR_UNSPECIFIED; + } + } + vec_free (ns_id); - REPLY_MACRO (VL_API_DISCONNECT_SOCK_REPLY); + /* *INDENT-OFF* */ +done: + REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({ + if (!rv) + rmp->appns_index = clib_host_to_net_u32 (appns_index); + })); + /* *INDENT-ON* */ } static void -vl_api_disconnect_sock_reply_t_handler (vl_api_disconnect_sock_reply_t * mp) +vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp) { - vnet_disconnect_args_t _a, *a = &_a; + vl_api_session_rule_add_del_reply_t *rmp; + session_rule_add_del_args_t args; + session_rule_table_add_del_args_t *table_args = &args.table_args; + u8 fib_proto; + int rv = 0; - /* Client objected to disconnecting the session, log and continue */ - if (mp->retval) + clib_memset (&args, 0, sizeof (args)); + fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; + + table_args->lcl.fp_len = mp->lcl_plen; + table_args->lcl.fp_proto = fib_proto; + table_args->rmt.fp_len = mp->rmt_plen; + table_args->rmt.fp_proto = fib_proto; + table_args->lcl_port = mp->lcl_port; + table_args->rmt_port = mp->rmt_port; + table_args->action_index = clib_net_to_host_u32 (mp->action_index); + table_args->is_add = mp->is_add; + mp->tag[sizeof (mp->tag) - 1] = 0; + table_args->tag = format (0, "%s", mp->tag); + args.appns_index = clib_net_to_host_u32 (mp->appns_index); + args.scope = mp->scope; + args.transport_proto = mp->transport_proto; + + clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr)); + clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr)); + ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4); + ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4); + rv = vnet_session_rule_add_del (&args); + if (rv) + clib_warning ("rule add del returned: %d", rv); + vec_free (table_args->tag); + REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY); +} + +static void +send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local, + u8 transport_proto, u32 appns_index, u8 * tag, + vl_api_registration_t * reg, u32 context) +{ + vl_api_session_rules_details_t *rmp = 0; + session_mask_or_match_4_t *match = + (session_mask_or_match_4_t *) & rule->match; + session_mask_or_match_4_t *mask = + (session_mask_or_match_4_t *) & rule->mask; + + rmp = vl_msg_api_alloc (sizeof (*rmp)); + clib_memset (rmp, 0, sizeof (*rmp)); + rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); + rmp->context = context; + + rmp->is_ip4 = 1; + clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip)); + clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip)); + rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip); + rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip); + rmp->lcl_port = match->lcl_port; + rmp->rmt_port = match->rmt_port; + rmp->action_index = clib_host_to_net_u32 (rule->action_index); + rmp->scope = + is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL; + rmp->transport_proto = transport_proto; + rmp->appns_index = clib_host_to_net_u32 (appns_index); + if (tag) { - clib_warning ("client retval %d", mp->retval); - return; + clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); + rmp->tag[vec_len (tag)] = 0; } - a->api_client_index = mp->client_index; - a->handle = mp->handle; + vl_api_send_msg (reg, (u8 *) rmp); +} + +static void +send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local, + u8 transport_proto, u32 appns_index, u8 * tag, + vl_api_registration_t * reg, u32 context) +{ + vl_api_session_rules_details_t *rmp = 0; + session_mask_or_match_6_t *match = + (session_mask_or_match_6_t *) & rule->match; + session_mask_or_match_6_t *mask = + (session_mask_or_match_6_t *) & rule->mask; + + rmp = vl_msg_api_alloc (sizeof (*rmp)); + clib_memset (rmp, 0, sizeof (*rmp)); + rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); + rmp->context = context; + + rmp->is_ip4 = 0; + clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip)); + clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip)); + rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip); + rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip); + rmp->lcl_port = match->lcl_port; + rmp->rmt_port = match->rmt_port; + rmp->action_index = clib_host_to_net_u32 (rule->action_index); + rmp->scope = + is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL; + rmp->transport_proto = transport_proto; + rmp->appns_index = clib_host_to_net_u32 (appns_index); + if (tag) + { + clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); + rmp->tag[vec_len (tag)] = 0; + } - vnet_disconnect (a); + vl_api_send_msg (reg, (u8 *) rmp); } static void -vl_api_reset_sock_reply_t_handler (vl_api_reset_sock_reply_t * mp) +send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto, + u8 tp, u8 is_local, u32 appns_index, + vl_api_registration_t * reg, u32 context) { - stream_session_t *s; - u32 session_index, thread_index; + mma_rule_16_t *rule16; + mma_rule_40_t *rule40; + mma_rules_table_16_t *srt16; + mma_rules_table_40_t *srt40; + u32 ri; - /* Client objected to resetting the session, log and continue */ - if (mp->retval) + if (is_local || fib_proto == FIB_PROTOCOL_IP4) { - clib_warning ("client retval %d", mp->retval); - return; + u8 *tag = 0; + /* *INDENT-OFF* */ + srt16 = &srt->session_rules_tables_16; + pool_foreach (rule16, srt16->rules, ({ + ri = mma_rules_table_rule_index_16 (srt16, rule16); + tag = session_rules_table_rule_tag (srt, ri, 1); + send_session_rule_details4 (rule16, is_local, tp, appns_index, tag, + reg, context); + })); + /* *INDENT-ON* */ } - - if (api_parse_session_handle (mp->handle, &session_index, &thread_index)) + if (is_local || fib_proto == FIB_PROTOCOL_IP6) { - clib_warning ("Invalid handle"); - return; + u8 *tag = 0; + /* *INDENT-OFF* */ + srt40 = &srt->session_rules_tables_40; + pool_foreach (rule40, srt40->rules, ({ + ri = mma_rules_table_rule_index_40 (srt40, rule40); + tag = session_rules_table_rule_tag (srt, ri, 1); + send_session_rule_details6 (rule40, is_local, tp, appns_index, tag, + reg, context); + })); + /* *INDENT-ON* */ } +} + +static void +vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp) +{ + vl_api_registration_t *reg; + session_table_t *st; + u8 tp; - s = stream_session_get (session_index, thread_index); + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; - /* This comes as a response to a reset, transport only waiting for - * confirmation to remove connection state, no need to disconnect */ - stream_session_cleanup (s); + /* *INDENT-OFF* */ + session_table_foreach (st, ({ + for (tp = 0; tp < TRANSPORT_N_PROTO; tp++) + { + send_session_rules_table_details (&st->session_rules[tp], + st->active_fib_proto, tp, + st->is_local, st->appns_index, reg, + mp->context); + } + })); + /* *INDENT-ON* */ } static void -vl_api_accept_sock_reply_t_handler (vl_api_accept_sock_reply_t * mp) +vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t * + mp) { - stream_session_t *s; - u32 session_index, thread_index; - - if (api_parse_session_handle (mp->handle, &session_index, &thread_index)) + vl_api_app_namespace_add_del_reply_t *rmp; + vnet_app_add_tls_cert_args_t _a, *a = &_a; + clib_error_t *error; + application_t *app; + u32 cert_len; + int rv = 0; + if (!session_main_is_enabled ()) { - clib_warning ("Invalid handle"); - return; + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; } - s = stream_session_get (session_index, thread_index); + if (!(app = application_lookup (mp->client_index))) + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + goto done; + } + clib_memset (a, 0, sizeof (*a)); + a->app_index = app->app_index; + cert_len = clib_net_to_host_u16 (mp->cert_len); + if (cert_len > 10000) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } + vec_validate (a->cert, cert_len); + clib_memcpy_fast (a->cert, mp->cert, cert_len); + if ((error = vnet_app_add_tls_cert (a))) + { + rv = clib_error_get_code (error); + clib_error_report (error); + } + vec_free (a->cert); +done: + REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY); +} - if (mp->retval) +static void +vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t * + mp) +{ + vl_api_app_namespace_add_del_reply_t *rmp; + vnet_app_add_tls_key_args_t _a, *a = &_a; + clib_error_t *error; + application_t *app; + u32 key_len; + int rv = 0; + if (!session_main_is_enabled ()) { - /* Server isn't interested, kill the session */ - stream_session_disconnect (s); - return; + rv = VNET_API_ERROR_FEATURE_DISABLED; + goto done; } + if (!(app = application_lookup (mp->client_index))) + { + rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; + goto done; + } + clib_memset (a, 0, sizeof (*a)); + a->app_index = app->app_index; + key_len = clib_net_to_host_u16 (mp->key_len); + if (key_len > 10000) + { + rv = VNET_API_ERROR_INVALID_VALUE; + goto done; + } + vec_validate (a->key, key_len); + clib_memcpy_fast (a->key, mp->key, key_len); + if ((error = vnet_app_add_tls_key (a))) + { + rv = clib_error_get_code (error); + clib_error_report (error); + } + vec_free (a->key); +done: + REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY); +} - s->session_state = SESSION_STATE_READY; +static clib_error_t * +application_reaper_cb (u32 client_index) +{ + application_t *app = application_lookup (client_index); + vnet_app_detach_args_t _a, *a = &_a; + if (app) + { + a->app_index = app->app_index; + a->api_client_index = client_index; + vnet_application_detach (a); + } + return 0; } +VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); + #define vl_msg_name_crc_list #include #undef vl_msg_name_crc_list @@ -823,7 +1341,7 @@ setup_message_id_table (api_main_t * am) /* * session_api_hookup * Add uri's API message handlers to the table. - * vlib has alread mapped shared memory and + * vlib has already mapped shared memory and * added the client registration handlers. * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() */ @@ -861,6 +1379,7 @@ session_api_hookup (vlib_main_t * vm) } VLIB_API_INIT_FUNCTION (session_api_hookup); + /* * fd.io coding-style-patch-verification: ON *