- clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid,
- format_api_error, ntohl (mp->retval));
- }
- else
- vcm->app_state = STATE_APP_ENABLED;
-}
-
-static void
-vppcom_app_send_attach (void)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_application_attach_t *bmp;
- u8 nsid_len = vec_len (vcm->cfg.namespace_id);
- u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
- vcm->cfg.app_proxy_transport_udp);
-
- bmp = vl_msg_api_alloc (sizeof (*bmp));
- memset (bmp, 0, sizeof (*bmp));
-
- bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
- bmp->client_index = vcm->my_client_index;
- bmp->context = htonl (0xfeedface);
- bmp->options[APP_OPTIONS_FLAGS] =
- APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
- (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
- (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
- (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
- bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
- (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
- (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
- bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
- bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
- bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
- bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
- if (nsid_len)
- {
- bmp->namespace_id_len = nsid_len;
- clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
- bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
- }
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
-}
-
-static int
-vppcom_app_attach (void)
-{
- vppcom_main_t *vcm = &vppcom_main;
- int rv;
-
- vppcom_app_send_attach ();
- rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
- if (PREDICT_FALSE (rv))
- {
- if (VPPCOM_DEBUG > 0)
- clib_warning ("[%d] application attach timed out, rv = %s (%d)",
- vcm->my_pid, vppcom_retval_str (rv), rv);
- return rv;
- }
- return VPPCOM_OK;
-}
-
-static void
-vppcom_app_detach (void)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_application_detach_t *bmp;
- bmp = vl_msg_api_alloc (sizeof (*bmp));
- memset (bmp, 0, sizeof (*bmp));
-
- bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
- bmp->client_index = vcm->my_client_index;
- bmp->context = htonl (0xfeedface);
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
-}
-
-static void
-vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
- mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- static svm_fifo_segment_create_args_t _a;
- svm_fifo_segment_create_args_t *a = &_a;
- int rv;
-
- memset (a, 0, sizeof (*a));
- if (mp->retval)
- {
- clib_warning ("[%d] attach failed: %U", vcm->my_pid,
- format_api_error, ntohl (mp->retval));
- return;
- }
-
- if (mp->segment_name_length == 0)
- {
- clib_warning ("[%d] segment_name_length zero", vcm->my_pid);
- return;
- }
-
- a->segment_name = (char *) mp->segment_name;
- a->segment_size = mp->segment_size;
-
- ASSERT (mp->app_event_queue_address);
-
- /* Attach to the segment vpp created */
- rv = svm_fifo_segment_attach (a);
- vec_reset_length (a->new_segment_indices);
- if (PREDICT_FALSE (rv))
- {
- clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid,
- mp->segment_name);
- return;
- }
-
- vcm->app_event_queue =
- uword_to_pointer (mp->app_event_queue_address,
- unix_shared_memory_queue_t *);
-
- vcm->app_state = STATE_APP_ATTACHED;
-}
-
-static void
-vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
- mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
-
- if (mp->retval)
- clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error,
- ntohl (mp->retval));
-
- vcm->app_state = STATE_APP_ENABLED;
-}
-
-static void
-vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
- mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- uword *p;
-
- p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
- if (p)
- {
- session_t *session = 0;
- int rv;
- clib_spinlock_lock (&vcm->sessions_lockp);
- rv = vppcom_session_at_index (p[0], &session);
- if (PREDICT_FALSE (rv))
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] invalid session, sid (%u) has been closed!",
- vcm->my_pid, p[0]);
- }
- hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
- session->state = STATE_DISCONNECT;
- clib_spinlock_unlock (&vcm->sessions_lockp);
- }
- else
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
- mp->handle);
- }
-
- if (mp->retval)
- clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid,
- format_api_error, ntohl (mp->retval));
-}
-
-static void
-vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- static svm_fifo_segment_create_args_t _a;
- svm_fifo_segment_create_args_t *a = &_a;
- int rv;
-
- memset (a, 0, sizeof (*a));
- a->segment_name = (char *) mp->segment_name;
- a->segment_size = mp->segment_size;
- /* Attach to the segment vpp created */
- rv = svm_fifo_segment_attach (a);
- vec_reset_length (a->new_segment_indices);
- if (PREDICT_FALSE (rv))
- {
- clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
- vcm->my_pid, mp->segment_name);
- return;
- }
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid,
- mp->segment_name, mp->segment_size);
-}
-
-static void
-vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- session_t *session = 0;
- vl_api_disconnect_session_reply_t *rmp;
- uword *p;
- int rv = 0;
-
- p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
- if (p)
- {
- int rval;
- clib_spinlock_lock (&vcm->sessions_lockp);
- rval = vppcom_session_at_index (p[0], &session);
- if (PREDICT_FALSE (rval))
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] invalid session, sid (%u) has been closed!",
- vcm->my_pid, p[0]);
- }
- else
- pool_put (vcm->sessions, session);
- clib_spinlock_unlock (&vcm->sessions_lockp);
- hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
- }
- else
- {
- clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
- mp->handle);
- rv = -11;
- }
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
-
- rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
- rmp->retval = htonl (rv);
- rmp->handle = mp->handle;
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
-}
-
-static void
-vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- session_t *session = 0;
- vl_api_reset_session_reply_t *rmp;
- uword *p;
- int rv = 0;
-
- p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
- if (p)
- {
- int rval;
- clib_spinlock_lock (&vcm->sessions_lockp);
- rval = vppcom_session_at_index (p[0], &session);
- if (PREDICT_FALSE (rval))
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] invalid session, sid (%u) has been closed!",
- vcm->my_pid, p[0]);
- }
- else
- pool_put (vcm->sessions, session);
- clib_spinlock_unlock (&vcm->sessions_lockp);
- hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
- }
- else
- {
- clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
- mp->handle);
- rv = -11;
- }
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
- rmp->retval = htonl (rv);
- rmp->handle = mp->handle;
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
-}
-
-static void
-vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- session_t *session;
- u32 session_index;
- svm_fifo_t *rx_fifo, *tx_fifo;
- u8 is_cut_thru = 0;
- int rv;
-
- if (mp->retval)
- {
- clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error,
- ntohl (mp->retval));
- return;
- }
-
- session_index = mp->context;
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid,
- session_index, session_index);
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- if (pool_is_free_index (vcm->sessions, session_index))
- {
- clib_spinlock_unlock (&vcm->sessions_lockp);
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] invalid session, sid %d is closed!",
- vcm->my_pid, session_index);
- return;
- }
-
- /* We've been redirected */
- if (mp->segment_name_length > 0)
- {
- static svm_fifo_segment_create_args_t _a;
- svm_fifo_segment_create_args_t *a = &_a;
-
- is_cut_thru = 1;
- memset (a, 0, sizeof (*a));
- a->segment_name = (char *) mp->segment_name;
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid,
- a->segment_name);
- rv = svm_fifo_segment_attach (a);
- vec_reset_length (a->new_segment_indices);
- if (PREDICT_FALSE (rv))
- {
- clib_spinlock_unlock (&vcm->sessions_lockp);
- clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
- vcm->my_pid, a->segment_name);
- return;
- }
- }
-
- /*
- * Setup session
- */
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] client sid %d", vcm->my_pid, session_index);
-
- session = pool_elt_at_index (vcm->sessions, session_index);
- session->is_cut_thru = is_cut_thru;
- session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
- unix_shared_memory_queue_t *);
-
- rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
- rx_fifo->client_session_index = session_index;
- tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
- tx_fifo->client_session_index = session_index;
-
- session->server_rx_fifo = rx_fifo;
- session->server_tx_fifo = tx_fifo;
- session->vpp_session_handle = mp->handle;
- session->state = STATE_CONNECT;
-
- /* Add it to lookup table */
- hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
- clib_spinlock_unlock (&vcm->sessions_lockp);
-}
-
-static void
-vppcom_send_connect_sock (session_t * session, u32 session_index)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_connect_sock_t *cmp;
-
- /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
- session->is_server = 0;
- cmp = vl_msg_api_alloc (sizeof (*cmp));
- memset (cmp, 0, sizeof (*cmp));
- cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
- cmp->client_index = vcm->my_client_index;
- cmp->context = session_index;
-
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] session_index = %d 0x%08x",
- vcm->my_pid, session_index, session_index);
-
- cmp->vrf = session->vrf;
- cmp->is_ip4 = session->peer_addr.is_ip4;
- clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
- cmp->port = session->peer_port;
- cmp->proto = session->proto;
- clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
-}
-
-static inline void
-vppcom_send_disconnect (session_t * session)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_disconnect_session_t *dmp;
-
- /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
- dmp = vl_msg_api_alloc (sizeof (*dmp));
- memset (dmp, 0, sizeof (*dmp));
- dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
- dmp->client_index = vcm->my_client_index;
- dmp->handle = session->vpp_session_handle;
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
-}
-
-static void
-vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- session_t *session = 0;
- int rv;
-
- if (mp->retval)
- clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error,
- ntohl (mp->retval));
-
- ASSERT (vcm->bind_session_index != ~0);
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- rv = vppcom_session_at_index (vcm->bind_session_index, &session);
- if (rv == VPPCOM_OK)
- {
- session->vpp_session_handle = mp->handle;
- hash_set (vcm->session_index_by_vpp_handles, mp->handle,
- vcm->bind_session_index);
- session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
- vcm->bind_session_index = ~0;
- }
- clib_spinlock_unlock (&vcm->sessions_lockp);
-}
-
-static void
-vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- session_t *session = 0;
- int rv;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- rv = vppcom_session_at_index (vcm->bind_session_index, &session);
- if (rv == VPPCOM_OK)
- {
- if ((VPPCOM_DEBUG > 1) && (mp->retval))
- clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error,
- ntohl (mp->retval));
-
- vcm->bind_session_index = ~0;
- session->state = STATE_START;
- }
- clib_spinlock_unlock (&vcm->sessions_lockp);
-}
-
-u8 *
-format_ip4_address (u8 * s, va_list * args)
-{
- u8 *a = va_arg (*args, u8 *);
- return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
-}
-
-u8 *
-format_ip6_address (u8 * s, va_list * args)
-{
- ip6_address_t *a = va_arg (*args, ip6_address_t *);
- u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
-
- i_max_n_zero = ARRAY_LEN (a->as_u16);
- max_n_zeros = 0;
- i_first_zero = i_max_n_zero;
- n_zeros = 0;
- for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
- {
- u32 is_zero = a->as_u16[i] == 0;
- if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
- {
- i_first_zero = i;
- n_zeros = 0;
- }
- n_zeros += is_zero;
- if ((!is_zero && n_zeros > max_n_zeros)
- || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
- {
- i_max_n_zero = i_first_zero;
- max_n_zeros = n_zeros;
- i_first_zero = ARRAY_LEN (a->as_u16);
- n_zeros = 0;
- }
- }
-
- last_double_colon = 0;
- for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
- {
- if (i == i_max_n_zero && max_n_zeros > 1)
- {
- s = format (s, "::");
- i += max_n_zeros - 1;
- last_double_colon = 1;
- }
- else
- {
- s = format (s, "%s%x",
- (last_double_colon || i == 0) ? "" : ":",
- clib_net_to_host_u16 (a->as_u16[i]));
- last_double_colon = 0;
- }
- }
-
- return s;
-}
-
-/* Format an IP46 address. */
-u8 *
-format_ip46_address (u8 * s, va_list * args)
-{
- ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
- ip46_type_t type = va_arg (*args, ip46_type_t);
- int is_ip4 = 1;
-
- switch (type)
- {
- case IP46_TYPE_ANY:
- is_ip4 = ip46_address_is_ip4 (ip46);
- break;
- case IP46_TYPE_IP4:
- is_ip4 = 1;
- break;
- case IP46_TYPE_IP6:
- is_ip4 = 0;
- break;
- }
-
- return is_ip4 ?
- format (s, "%U", format_ip4_address, &ip46->ip4) :
- format (s, "%U", format_ip6_address, &ip46->ip6);
-}
-
-static void
-vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_accept_session_reply_t *rmp;
- svm_fifo_t *rx_fifo, *tx_fifo;
- session_t *session;
- u32 session_index;
- int rv = 0;
-
- if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
- {
- clib_warning ("[%d] client session queue is full!", vcm->my_pid);
- rv = VNET_API_ERROR_QUEUE_FULL;
- goto send_reply;
- }
-
- if (VPPCOM_DEBUG > 1)
- {
- u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
- clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str,
- clib_net_to_host_u16 (mp->port));
- vec_free (ip_str);
- }
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- /* Allocate local session and set it up */
- pool_get (vcm->sessions, session);
- memset (session, 0, sizeof (*session));
- session_index = session - vcm->sessions;
-
- rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
- rx_fifo->client_session_index = session_index;
- tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
- tx_fifo->client_session_index = session_index;
-
- session->server_rx_fifo = rx_fifo;
- session->server_tx_fifo = tx_fifo;
- session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
- unix_shared_memory_queue_t *);
- session->state = STATE_ACCEPT;
- session->is_cut_thru = 0;
- session->is_server = 1;
- session->peer_port = mp->port;
- session->peer_addr.is_ip4 = mp->is_ip4;
- clib_memcpy (&session->peer_addr.ip46, mp->ip,
- sizeof (session->peer_addr.ip46));
-
- /* Add it to lookup table */
- hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
-
- clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
- clib_spinlock_unlock (&vcm->sessions_lockp);
-
- /*
- * Send accept reply to vpp
- */
-send_reply:
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
- rmp->retval = htonl (rv);
- rmp->handle = mp->handle;
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
-}
-
-/*
- * Acting as server for redirected connect requests
- */
-static void
-vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
-{
- static svm_fifo_segment_create_args_t _a;
- svm_fifo_segment_create_args_t *a = &_a;
- vppcom_main_t *vcm = &vppcom_main;
- u32 session_index;
- svm_fifo_segment_private_t *seg;
- unix_shared_memory_queue_t *client_q;
- vl_api_connect_session_reply_t *rmp;
- session_t *session = 0;
- int rv = 0;
- svm_fifo_t *rx_fifo;
- svm_fifo_t *tx_fifo;
- unix_shared_memory_queue_t *event_q = 0;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] client session queue is full!", vcm->my_pid);
- rv = VNET_API_ERROR_QUEUE_FULL;
- clib_spinlock_unlock (&vcm->sessions_lockp);
- goto send_reply;
- }
-
- /* Create the segment */
- memset (a, 0, sizeof (*a));
- a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c",
- vcm->my_pid, vcm->unique_segment_index++,
- 0);
- a->segment_size = vcm->cfg.segment_size;
- a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
- a->rx_fifo_size = vcm->cfg.rx_fifo_size;
- a->tx_fifo_size = vcm->cfg.tx_fifo_size;
-
- rv = svm_fifo_segment_create (a);
- if (PREDICT_FALSE (rv))
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
- vcm->my_pid, a->segment_name);
- vec_reset_length (a->new_segment_indices);
- rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
- goto send_reply;
- }
-
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name);
-
- pool_get (vcm->sessions, session);
- memset (session, 0, sizeof (*session));
- session_index = session - vcm->sessions;
-
- session->sm_seg_index = a->new_segment_indices[0];
- vec_reset_length (a->new_segment_indices);
-
- seg = svm_fifo_segment_get_segment (session->sm_seg_index);
- rx_fifo = session->server_rx_fifo =
- svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
- FIFO_SEGMENT_RX_FREELIST);
- if (PREDICT_FALSE (!session->server_rx_fifo))
- {
- svm_fifo_segment_delete (seg);
- clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
- vcm->my_pid, vcm->cfg.rx_fifo_size,
- vcm->cfg.rx_fifo_size);
- rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
- clib_spinlock_unlock (&vcm->sessions_lockp);
- goto send_reply;
- }
-
- tx_fifo = session->server_tx_fifo =
- svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
- FIFO_SEGMENT_TX_FREELIST);
- if (PREDICT_FALSE (!session->server_tx_fifo))
- {
- svm_fifo_segment_delete (seg);
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
- vcm->my_pid, vcm->cfg.tx_fifo_size,
- vcm->cfg.tx_fifo_size);
- rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
- clib_spinlock_unlock (&vcm->sessions_lockp);
- goto send_reply;
- }
-
- session->server_rx_fifo->master_session_index = session_index;
- session->server_tx_fifo->master_session_index = session_index;
- session->client_queue_address = mp->client_queue_address;
- session->is_cut_thru = 1;
- session->is_server = 1;
- session->peer_port = mp->port;
- session->peer_addr.is_ip4 = mp->is_ip4;
- clib_memcpy (&session->peer_addr.ip46, mp->ip,
- sizeof (session->peer_addr.ip46));
- {
- void *oldheap;
- ssvm_shared_header_t *sh = seg->ssvm.sh;
-
- ssvm_lock_non_recursive (sh, 1);
- oldheap = ssvm_push_heap (sh);
- event_q = session->vpp_event_queue =
- unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
- sizeof (session_fifo_event_t),
- vcm->my_pid, 0 /* signal not sent */ );
- ssvm_pop_heap (oldheap);
- ssvm_unlock_non_recursive (sh);
- }
-
- session->state = STATE_ACCEPT;
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] Connected cut-thru to client: sid %d",
- vcm->my_pid, session_index);
- clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
- clib_spinlock_unlock (&vcm->sessions_lockp);
-
-send_reply:
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
-
- rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
- rmp->context = mp->context;
- rmp->retval = htonl (rv);
- rmp->segment_name_length = vec_len (a->segment_name);
- clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
- vec_reset_length (a->segment_name);
-
- if (event_q)
- {
- rmp->vpp_event_queue_address = pointer_to_uword (event_q);
- rmp->server_rx_fifo = pointer_to_uword (rx_fifo);
- rmp->server_tx_fifo = pointer_to_uword (tx_fifo);
- }
- client_q =
- uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
-
- ASSERT (client_q);
- vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
-}
-
-static void
-vppcom_send_bind_sock (session_t * session)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_bind_sock_t *bmp;
-
- /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
- session->is_server = 1;
- bmp = vl_msg_api_alloc (sizeof (*bmp));
- memset (bmp, 0, sizeof (*bmp));
-
- bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
- bmp->client_index = vcm->my_client_index;
- bmp->context = htonl (0xfeedface);
- bmp->vrf = session->vrf;
- bmp->is_ip4 = session->lcl_addr.is_ip4;
- clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
- bmp->port = session->lcl_port;
- bmp->proto = session->proto;
- clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
-}
-
-static void
-vppcom_send_unbind_sock (u32 session_index)
-{
- vppcom_main_t *vcm = &vppcom_main;
- vl_api_unbind_sock_t *ump;
- session_t *session = 0;
- int rv;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- rv = vppcom_session_at_index (session_index, &session);
- if (PREDICT_FALSE (rv))
- {
- clib_spinlock_unlock (&vcm->sessions_lockp);
- if (VPPCOM_DEBUG > 0)
- clib_warning ("[%d] invalid session, sid (%u) has been closed!",
- vcm->my_pid, session_index);
- return;
- }
-
- ump = vl_msg_api_alloc (sizeof (*ump));
- memset (ump, 0, sizeof (*ump));
-
- ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
- ump->client_index = vcm->my_client_index;
- ump->handle = session->vpp_session_handle;
- clib_spinlock_unlock (&vcm->sessions_lockp);
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
-}
-
-static int
-vppcom_session_unbind_cut_thru (session_t * session)
-{
- svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
- svm_fifo_segment_private_t *seg;
- int rv = VPPCOM_OK;
-
- seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
- svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
- FIFO_SEGMENT_RX_FREELIST);
- svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
- FIFO_SEGMENT_TX_FREELIST);
- svm_fifo_segment_delete (seg);
-
- return rv;
-}
-
-static int
-vppcom_session_unbind (u32 session_index)
-{
- vppcom_main_t *vcm = &vppcom_main;
- int rv;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
- {
- clib_spinlock_unlock (&vcm->sessions_lockp);
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] invalid session, sid (%u) has been closed!",
- vcm->my_pid, session_index);
- return VPPCOM_EBADFD;
- }
- clib_spinlock_unlock (&vcm->sessions_lockp);
-
- vcm->bind_session_index = session_index;
- vppcom_send_unbind_sock (session_index);
- rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
- vcm->cfg.session_timeout);
- if (PREDICT_FALSE (rv))
- {
- vcm->bind_session_index = ~0;
- if (VPPCOM_DEBUG > 0)
- clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
- vcm->my_pid, vppcom_retval_str (rv), rv);
- return rv;
- }
- return VPPCOM_OK;
-}
-
-static inline int
-vppcom_session_disconnect (u32 session_index)
-{
- vppcom_main_t *vcm = &vppcom_main;
- int rv;
- session_t *session;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- rv = vppcom_session_at_index (session_index, &session);
- if (PREDICT_FALSE (rv))
- {
- clib_spinlock_unlock (&vcm->sessions_lockp);
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] invalid session, sid (%u) has been closed!",
- vcm->my_pid, session_index);