- }
-
- return is_ip4 ?
- format (s, "%U", format_ip4_address, &ip46->ip4) :
- format (s, "%U", format_ip6_address, &ip46->ip6);
-}
-
-static inline void
-vppcom_send_accept_session_reply (u32 handle, int retval)
-{
- vl_api_accept_session_reply_t *rmp;
-
- 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 (retval);
- rmp->handle = handle;
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
-}
-
-static void
-vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
-{
- svm_fifo_t *rx_fifo, *tx_fifo;
- session_t *session;
- u32 session_index;
-
- clib_spinlock_lock (&vcm->sessions_lockp);
- if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
- {
- clib_warning ("[%d] client session queue is full!", getpid ());
- vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL,
- mp->handle);
- clib_spinlock_unlock (&vcm->sessions_lockp);
- return;
- }
-
- /* 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->vpp_handle = mp->handle;
- 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);
-
- if (VPPCOM_DEBUG > 1)
- {
- u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
- clib_warning ("[%d] received request to accept session (sid %d) "
- "from %s:%d", getpid (), session_index, ip_str,
- clib_net_to_host_u16 (mp->port));
- vec_free (ip_str);
- }
-}
-
-static void
-vppcom_send_connect_session_reply (session_t * session, u32 context,
- int retval, int handle)
-{
- vl_api_connect_session_reply_t *rmp;
- u32 len;
- unix_shared_memory_queue_t *client_q;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
-
- rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
- rmp->context = session ? session->client_context : context;
- rmp->retval = htonl (retval);
- rmp->handle = session ? session->vpp_handle : handle;
-
- if (session)
- {
- rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
- rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
- rmp->vpp_event_queue_address =
- pointer_to_uword (session->vpp_event_queue);
- rmp->segment_size = vcm->cfg.segment_size;
- len = vec_len (session->segment_name);
- rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
- clib_memcpy (rmp->segment_name, session->segment_name,
- rmp->segment_name_length - 1);
- clib_memcpy (rmp->lcl_ip, session->lcl_addr.ip46.as_u8,
- sizeof (rmp->lcl_ip));
- rmp->is_ip4 = session->lcl_addr.is_ip4;
- }
-
- client_q = uword_to_pointer (session->client_queue_address,
- unix_shared_memory_queue_t *);
- ASSERT (client_q);
- vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
-}
-
-/*
- * Acting as server for redirected connect requests
- */
-static void
-vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
-{
- u32 session_index;
- session_t *session = 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!", getpid ());
- clib_spinlock_unlock (&vcm->sessions_lockp);
- vppcom_send_accept_session_reply (VNET_API_ERROR_QUEUE_FULL, 0);
- return;
- }
-
- pool_get (vcm->sessions, session);
- memset (session, 0, sizeof (*session));
- session_index = session - vcm->sessions;
-
- session->client_context = mp->context;
- session->vpp_handle = 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));
-
- session->state = STATE_ACCEPT;
- clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
- if (VPPCOM_DEBUG > 1)
- clib_warning
- ("[%d] Got a cut-thru connect request to client: "
- "sid %d, clib_fifo_elts %u!\n", getpid (), session_index,
- clib_fifo_elts (vcm->client_session_index_fifo));
- clib_spinlock_unlock (&vcm->sessions_lockp);
-}
-
-static void
-vppcom_send_bind_sock (session_t * session)
-{
- 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)
-{
- 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!",
- getpid (), 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_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, u32 session_index)
-{
- svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
- svm_fifo_segment_private_t *seg;
- int rv = VPPCOM_OK;
-
- if (VPPCOM_DEBUG > 1)
- clib_warning ("[%d] sid %d, seg_nxd %d:\n"
- " server_rx_fifo %p, refcnt = %d\n"
- " server_tx_fifo %p, refcnt = %d",
- getpid (), session_index, session->sm_seg_index,
- session->server_rx_fifo, session->server_rx_fifo->refcnt,
- session->server_tx_fifo, session->server_tx_fifo->refcnt);
-
- 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)
-{
- 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!",
- getpid (), 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)",
- getpid (), vppcom_retval_str (rv), rv);
- return rv;