aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/vnet/session
diff options
context:
space:
mode:
authorFlorin Coras <fcoras@cisco.com>2018-07-12 14:48:06 -0700
committerDamjan Marion <dmarion@me.com>2018-07-17 09:02:17 +0000
commit52207f1b7b60cb0784d5241f0a4d40eef531c67e (patch)
treeb83a1f01bd08872a2ae27e17f2fd0ddeb3f571b6 /src/tests/vnet/session
parent3c2fed5145d9e40a9ecd178c2866c813eddc6203 (diff)
session: send ctrl msg over mq
Change-Id: I242056bc46ddb671064665916b2687860292dcb2 Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/tests/vnet/session')
-rw-r--r--src/tests/vnet/session/tcp_echo.c414
-rw-r--r--src/tests/vnet/session/udp_echo.c498
2 files changed, 453 insertions, 459 deletions
diff --git a/src/tests/vnet/session/tcp_echo.c b/src/tests/vnet/session/tcp_echo.c
index f8b75d95ac7..6c99294099b 100644
--- a/src/tests/vnet/session/tcp_echo.c
+++ b/src/tests/vnet/session/tcp_echo.c
@@ -62,6 +62,7 @@ typedef enum
{
STATE_START,
STATE_ATTACHED,
+ STATE_LISTEN,
STATE_READY,
STATE_DISCONNECTING,
STATE_FAILED,
@@ -101,9 +102,6 @@ typedef struct
/* Our event queue */
svm_msg_q_t *our_event_queue;
- /* $$$ single thread only for the moment */
- svm_queue_t *vpp_event_queue;
-
u8 *socket_name;
pid_t my_pid;
@@ -240,16 +238,22 @@ init_error_string_table (echo_main_t * em)
hash_set (em->error_string_by_error_number, 99, "Misc");
}
-int
+static void handle_mq_event (session_event_t * e);
+
+static int
wait_for_state_change (echo_main_t * em, connection_state_t state)
{
+ svm_msg_q_msg_t msg;
+ session_event_t *e;
+ f64 timeout;
+
#if CLIB_DEBUG > 0
#define TIMEOUT 600.0
#else
#define TIMEOUT 600.0
#endif
- f64 timeout = clib_time_now (&em->clib_time) + TIMEOUT;
+ timeout = clib_time_now (&em->clib_time) + TIMEOUT;
while (clib_time_now (&em->clib_time) < timeout)
{
@@ -259,6 +263,14 @@ wait_for_state_change (echo_main_t * em, connection_state_t state)
return -1;
if (em->time_to_stop == 1)
return 0;
+ if (!em->our_event_queue)
+ continue;
+
+ if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_NOWAIT, 0))
+ continue;
+ e = svm_msg_q_msg_data (em->our_event_queue, &msg);
+ handle_mq_event (e);
+ svm_msg_q_free_msg (em->our_event_queue, &msg);
}
clib_warning ("timeout waiting for state %d", state);
return -1;
@@ -277,8 +289,9 @@ application_send_attach (echo_main_t * em)
bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
bmp->client_index = em->my_client_index;
bmp->context = ntohl (0xfeedface);
- bmp->options[APP_OPTIONS_FLAGS] =
- APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
+ bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
+ bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
+ bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
@@ -306,7 +319,7 @@ application_send_attach (echo_main_t * em)
vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
}
-int
+static int
application_attach (echo_main_t * em)
{
application_send_attach (em);
@@ -548,74 +561,6 @@ session_print_stats (echo_main_t * em, session_t * session)
}
static void
-vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
-{
- echo_main_t *em = &echo_main;
- session_t *session = 0;
- vl_api_disconnect_session_reply_t *rmp;
- uword *p;
- int rv = 0;
-
- p = hash_get (em->session_index_by_vpp_handles, mp->handle);
-
- if (p)
- {
- session = pool_elt_at_index (em->sessions, p[0]);
- hash_unset (em->session_index_by_vpp_handles, mp->handle);
- pool_put (em->sessions, session);
- }
- else
- {
- clib_warning ("couldn't find session key %llx", mp->handle);
- rv = -11;
- }
-
-// em->time_to_stop = 1;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
-
- rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
- rmp->retval = rv;
- rmp->handle = mp->handle;
- rmp->context = mp->context;
- vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
-
- if (session)
- session_print_stats (em, session);
-}
-
-static void
-vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
-{
- echo_main_t *em = &echo_main;
- vl_api_reset_session_reply_t *rmp;
- uword *p;
- int rv = 0;
-
- p = hash_get (em->session_index_by_vpp_handles, mp->handle);
-
- if (p)
- {
- clib_warning ("got reset");
- /* Cleanup later */
- em->time_to_stop = 1;
- }
- else
- {
- clib_warning ("couldn't find session key %llx", 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 = rv;
- rmp->handle = mp->handle;
- vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
-}
-
-static void
test_recv_bytes (session_t * s, u8 * rx_buf, u32 n_read)
{
int i;
@@ -661,7 +606,7 @@ recv_test_chunk (echo_main_t * em, session_t * s, u8 * rx_buf)
}
void
-client_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e,
+client_handle_fifo_event_rx (echo_main_t * em, session_event_t * e,
u8 * rx_buf)
{
session_t *s;
@@ -743,7 +688,7 @@ client_thread_fn (void *arg)
void *
client_rx_thread_fn (void *arg)
{
- session_fifo_event_t _e, *e = &_e;
+ session_event_t _e, *e = &_e;
echo_main_t *em = &echo_main;
static u8 *rx_buf = 0;
svm_msg_q_msg_t msg;
@@ -771,8 +716,104 @@ client_rx_thread_fn (void *arg)
pthread_exit (0);
}
+void
+client_send_connect (echo_main_t * em)
+{
+ vl_api_connect_uri_t *cmp;
+ cmp = vl_msg_api_alloc (sizeof (*cmp));
+ memset (cmp, 0, sizeof (*cmp));
+
+ cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
+ cmp->client_index = em->my_client_index;
+ cmp->context = ntohl (0xfeedface);
+ memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
+ vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
+}
+
+void
+client_send_disconnect (echo_main_t * em, session_t * s)
+{
+ vl_api_disconnect_session_t *dmp;
+ dmp = vl_msg_api_alloc (sizeof (*dmp));
+ memset (dmp, 0, sizeof (*dmp));
+ dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
+ dmp->client_index = em->my_client_index;
+ dmp->handle = s->vpp_session_handle;
+ vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
+}
+
+int
+client_disconnect (echo_main_t * em, session_t * s)
+{
+ client_send_disconnect (em, s);
+ pool_put (em->sessions, s);
+ memset (s, 0xfe, sizeof (*s));
+ return 0;
+}
+
+static void
+session_accepted_handler (session_accepted_msg_t * mp)
+{
+ app_session_evt_t _app_evt, *app_evt = &_app_evt;
+ session_accepted_reply_msg_t *rmp;
+ svm_fifo_t *rx_fifo, *tx_fifo;
+ echo_main_t *em = &echo_main;
+ session_t *session;
+ static f64 start_time;
+ u32 session_index;
+ u8 *ip_str;
+
+ if (start_time == 0.0)
+ start_time = clib_time_now (&em->clib_time);
+
+ ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
+ clib_warning ("Accepted session from: %s:%d", ip_str,
+ clib_net_to_host_u16 (mp->port));
+
+ /* Allocate local session and set it up */
+ pool_get (em->sessions, session);
+ session_index = session - em->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_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
+ svm_msg_q_t *);
+
+ /* Add it to lookup table */
+ hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
+
+ em->state = STATE_READY;
+
+ /* Stats printing */
+ if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
+ {
+ f64 now = clib_time_now (&em->clib_time);
+ fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
+ pool_elts (em->sessions), now - start_time,
+ (f64) pool_elts (em->sessions) / (now - start_time));
+ }
+
+ /*
+ * Send accept reply to vpp
+ */
+ app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
+ SESSION_CTRL_EVT_ACCEPTED_REPLY);
+ rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
+ rmp->handle = mp->handle;
+ rmp->context = mp->context;
+ app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
+
+ session->bytes_received = 0;
+ session->start = clib_time_now (&em->clib_time);
+}
+
static void
-vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
+session_connected_handler (session_connected_msg_t * mp)
{
echo_main_t *em = &echo_main;
session_t *session;
@@ -829,39 +870,96 @@ vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
mp->is_ip4, clib_net_to_host_u16 (mp->lcl_port));
}
-void
-client_send_connect (echo_main_t * em)
+static void
+session_disconnected_handler (session_disconnected_msg_t * mp)
{
- vl_api_connect_uri_t *cmp;
- cmp = vl_msg_api_alloc (sizeof (*cmp));
- memset (cmp, 0, sizeof (*cmp));
+ app_session_evt_t _app_evt, *app_evt = &_app_evt;
+ session_disconnected_reply_msg_t *rmp;
+ echo_main_t *em = &echo_main;
+ session_t *session = 0;
+ uword *p;
+ int rv = 0;
- cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
- cmp->client_index = em->my_client_index;
- cmp->context = ntohl (0xfeedface);
- memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri));
- vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
+ p = hash_get (em->session_index_by_vpp_handles, mp->handle);
+
+ if (p)
+ {
+ clib_warning ("disconnected");
+ session = pool_elt_at_index (em->sessions, p[0]);
+ hash_unset (em->session_index_by_vpp_handles, mp->handle);
+ pool_put (em->sessions, session);
+ }
+ else
+ {
+ clib_warning ("couldn't find session key %llx", mp->handle);
+ rv = -11;
+ }
+
+ app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
+ SESSION_CTRL_EVT_DISCONNECTED_REPLY);
+ rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
+ rmp->retval = rv;
+ rmp->handle = mp->handle;
+ rmp->context = mp->context;
+ app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
+
+ if (session)
+ session_print_stats (em, session);
}
-void
-client_send_disconnect (echo_main_t * em, session_t * s)
+static void
+session_reset_handler (session_reset_msg_t * mp)
{
- vl_api_disconnect_session_t *dmp;
- dmp = vl_msg_api_alloc (sizeof (*dmp));
- memset (dmp, 0, sizeof (*dmp));
- dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
- dmp->client_index = em->my_client_index;
- dmp->handle = s->vpp_session_handle;
- vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
+ app_session_evt_t _app_evt, *app_evt = &_app_evt;
+ echo_main_t *em = &echo_main;
+ session_reset_reply_msg_t *rmp;
+ session_t *session = 0;
+ uword *p;
+ int rv = 0;
+
+ p = hash_get (em->session_index_by_vpp_handles, mp->handle);
+
+ if (p)
+ {
+ session = pool_elt_at_index (em->sessions, p[0]);
+ clib_warning ("got reset");
+ /* Cleanup later */
+ em->time_to_stop = 1;
+ }
+ else
+ {
+ clib_warning ("couldn't find session key %llx", mp->handle);
+ return;
+ }
+
+ app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
+ SESSION_CTRL_EVT_RESET_REPLY);
+ rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
+ rmp->retval = rv;
+ rmp->handle = mp->handle;
+ app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
}
-int
-client_disconnect (echo_main_t * em, session_t * s)
+static void
+handle_mq_event (session_event_t * e)
{
- client_send_disconnect (em, s);
- pool_put (em->sessions, s);
- memset (s, 0xfe, sizeof (*s));
- return 0;
+ switch (e->event_type)
+ {
+ case SESSION_CTRL_EVT_ACCEPTED:
+ session_accepted_handler ((session_accepted_msg_t *) e->data);
+ break;
+ case SESSION_CTRL_EVT_CONNECTED:
+ session_connected_handler ((session_connected_msg_t *) e->data);
+ break;
+ case SESSION_CTRL_EVT_DISCONNECTED:
+ session_disconnected_handler ((session_disconnected_msg_t *) e->data);
+ break;
+ case SESSION_CTRL_EVT_RESET:
+ session_reset_handler ((session_reset_msg_t *) e->data);
+ break;
+ default:
+ clib_warning ("unhandled %u", e->event_type);
+ }
}
static void
@@ -869,6 +967,7 @@ clients_run (echo_main_t * em)
{
f64 start_time, deltat, timeout = 100.0;
svm_msg_q_msg_t msg;
+ session_event_t *e;
session_t *s;
int i;
@@ -890,7 +989,13 @@ clients_run (echo_main_t * em)
while (em->n_clients_connected < em->n_clients
&& (clib_time_now (&em->clib_time) - start_time < timeout)
&& em->state != STATE_FAILED)
- ;
+
+ {
+ svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0);
+ e = svm_msg_q_msg_data (em->our_event_queue, &msg);
+ handle_mq_event (e);
+ svm_msg_q_free_msg (em->our_event_queue, &msg);
+ }
if (em->n_clients_connected != em->n_clients)
{
@@ -921,9 +1026,12 @@ clients_run (echo_main_t * em)
if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_WAIT, 0))
{
clib_warning ("svm msg q returned");
+ continue;
}
- else
- svm_msg_q_free_msg (em->our_event_queue, &msg);
+ e = svm_msg_q_msg_data (em->our_event_queue, &msg);
+ if (e->event_type != FIFO_EVENT_APP_RX)
+ handle_mq_event (e);
+ svm_msg_q_free_msg (em->our_event_queue, &msg);
}
for (i = 0; i < em->n_clients; i++)
@@ -1055,72 +1163,10 @@ format_ip46_address (u8 * s, va_list * args)
}
static void
-vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
-{
- echo_main_t *em = &echo_main;
- vl_api_accept_session_reply_t *rmp;
- svm_fifo_t *rx_fifo, *tx_fifo;
- session_t *session;
- static f64 start_time;
- u32 session_index;
- u8 *ip_str;
-
- if (start_time == 0.0)
- start_time = clib_time_now (&em->clib_time);
-
- ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
- clib_warning ("Accepted session from: %s:%d", ip_str,
- clib_net_to_host_u16 (mp->port));
- em->vpp_event_queue =
- uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
-
- /* Allocate local session and set it up */
- pool_get (em->sessions, session);
- session_index = session - em->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;
-
- /* Add it to lookup table */
- hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
-
- em->state = STATE_READY;
-
- /* Stats printing */
- if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
- {
- f64 now = clib_time_now (&em->clib_time);
- fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
- pool_elts (em->sessions), now - start_time,
- (f64) pool_elts (em->sessions) / (now - start_time));
- }
-
- /*
- * Send accept reply to vpp
- */
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
- rmp->handle = mp->handle;
- rmp->context = mp->context;
- vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp);
-
- session->bytes_received = 0;
- session->start = clib_time_now (&em->clib_time);
-}
-
-void
-server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
+server_handle_fifo_event_rx (echo_main_t * em, session_event_t * e)
{
svm_fifo_t *rx_fifo, *tx_fifo;
int n_read;
- session_fifo_event_t evt;
- svm_queue_t *q;
session_t *session;
int rv;
u32 max_dequeue, offset, max_transfer, rx_buf_len;
@@ -1167,24 +1213,18 @@ server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e)
/* If event wasn't set, add one */
if (svm_fifo_set_event (tx_fifo))
- {
- /* Fabricate TX event, send to vpp */
- evt.fifo = tx_fifo;
- evt.event_type = FIFO_EVENT_APP_TX;
-
- q = em->vpp_event_queue;
- svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ );
- }
+ app_send_io_evt_to_vpp (session->vpp_evt_q, tx_fifo,
+ FIFO_EVENT_APP_TX, SVM_Q_WAIT);
}
}
while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop);
}
-void
+static void
server_handle_event_queue (echo_main_t * em)
{
- session_fifo_event_t _e, *e = &_e;
svm_msg_q_msg_t msg;
+ session_event_t *e;
while (1)
{
@@ -1195,13 +1235,8 @@ server_handle_event_queue (echo_main_t * em)
case FIFO_EVENT_APP_RX:
server_handle_fifo_event_rx (em, e);
break;
-
- case FIFO_EVENT_DISCONNECT:
- svm_msg_q_free_msg (em->our_event_queue, &msg);
- return;
-
default:
- clib_warning ("unknown event type %d", e->event_type);
+ handle_mq_event (e);
break;
}
if (PREDICT_FALSE (em->time_to_stop == 1))
@@ -1346,11 +1381,7 @@ static void
#define foreach_tcp_echo_msg \
_(BIND_URI_REPLY, bind_uri_reply) \
_(UNBIND_URI_REPLY, unbind_uri_reply) \
-_(ACCEPT_SESSION, accept_session) \
-_(CONNECT_SESSION_REPLY, connect_session_reply) \
-_(DISCONNECT_SESSION, disconnect_session) \
_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
-_(RESET_SESSION, reset_session) \
_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
_(APPLICATION_DETACH_REPLY, application_detach_reply) \
_(MAP_ANOTHER_SEGMENT, map_another_segment) \
@@ -1394,8 +1425,6 @@ main (int argc, char **argv)
/* make the main heap thread-safe */
h->flags |= MHEAP_FLAG_THREAD_SAFE;
- vec_validate (em->rx_buf, 128 << 10);
-
memset (em, 0, sizeof (*em));
em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
em->my_pid = getpid ();
@@ -1472,6 +1501,7 @@ main (int argc, char **argv)
em->test_return_packets = test_return_packets;
em->bytes_to_send = bytes_to_send;
em->time_to_stop = 0;
+ vec_validate (em->rx_buf, 128 << 10);
vec_validate (em->client_thread_handles, em->n_clients - 1);
vec_validate (em->thread_args, em->n_clients - 1);
diff --git a/src/tests/vnet/session/udp_echo.c b/src/tests/vnet/session/udp_echo.c
index d796b6b8de0..2e176ff4b7b 100644
--- a/src/tests/vnet/session/udp_echo.c
+++ b/src/tests/vnet/session/udp_echo.c
@@ -50,10 +50,12 @@
typedef enum
{
STATE_START,
+ STATE_ATTACHED,
STATE_BOUND,
STATE_READY,
STATE_FAILED,
STATE_DISCONNECTING,
+ STATE_DETACHED
} connection_state_t;
typedef struct
@@ -298,7 +300,7 @@ unformat_uri (unformat_input_t * input, va_list * args)
return 0;
}
-void
+static void
application_send_attach (udp_echo_main_t * utm)
{
vl_api_application_attach_t *bmp;
@@ -311,6 +313,7 @@ application_send_attach (udp_echo_main_t * utm)
bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ADD_SEGMENT;
bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
+ bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS;
bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = utm->fifo_size;
bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = utm->fifo_size;
@@ -370,6 +373,7 @@ vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
utm->our_event_queue =
uword_to_pointer (mp->app_event_queue_address, svm_msg_q_t *);
+ utm->state = STATE_ATTACHED;
}
static void
@@ -378,6 +382,7 @@ vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
{
if (mp->retval)
clib_warning ("detach returned with err: %d", mp->retval);
+ udp_echo_main.state = STATE_DETACHED;
}
u8 *
@@ -473,7 +478,199 @@ cut_through_thread_fn (void *arg)
}
static void
-udp_client_connect (udp_echo_main_t * utm)
+session_accepted_handler (session_accepted_msg_t * mp)
+{
+ app_session_evt_t _app_evt, *app_evt = &_app_evt;
+ udp_echo_main_t *utm = &udp_echo_main;
+ session_accepted_reply_msg_t *rmp;
+ svm_fifo_t *rx_fifo, *tx_fifo;
+ app_session_t *session;
+ static f64 start_time;
+ u32 session_index;
+ int rv = 0;
+
+ if (start_time == 0.0)
+ start_time = clib_time_now (&utm->clib_time);
+
+ utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
+ svm_msg_q_t *);
+ rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
+ tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
+
+ pool_get (utm->sessions, session);
+ memset (session, 0, sizeof (*session));
+ session_index = session - utm->sessions;
+
+ /* Cut-through case */
+ if (mp->server_event_queue_address)
+ {
+ clib_warning ("cut-through session");
+ utm->our_event_queue = uword_to_pointer (mp->server_event_queue_address,
+ svm_msg_q_t *);
+ rx_fifo->master_session_index = session_index;
+ tx_fifo->master_session_index = session_index;
+ utm->cut_through_session_index = session_index;
+ session->rx_fifo = rx_fifo;
+ session->tx_fifo = tx_fifo;
+
+ rv = pthread_create (&utm->cut_through_thread_handle,
+ NULL /*attr */ , cut_through_thread_fn, 0);
+ if (rv)
+ {
+ clib_warning ("pthread_create returned %d", rv);
+ rv = VNET_API_ERROR_SYSCALL_ERROR_1;
+ }
+ utm->do_echo = 1;
+ }
+ else
+ {
+ rx_fifo->client_session_index = session_index;
+ tx_fifo->client_session_index = session_index;
+ session->rx_fifo = rx_fifo;
+ session->tx_fifo = tx_fifo;
+ clib_memcpy (&session->transport.rmt_ip, mp->ip,
+ sizeof (ip46_address_t));
+ session->transport.is_ip4 = mp->is_ip4;
+ session->transport.rmt_port = mp->port;
+ }
+
+ hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
+ if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
+ {
+ f64 now = clib_time_now (&utm->clib_time);
+ fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
+ pool_elts (utm->sessions), now - start_time,
+ (f64) pool_elts (utm->sessions) / (now - start_time));
+ }
+
+ app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
+ SESSION_CTRL_EVT_ACCEPTED_REPLY);
+ rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
+ rmp->handle = mp->handle;
+ rmp->context = mp->context;
+ rmp->retval = rv;
+ app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
+
+ CLIB_MEMORY_BARRIER ();
+ utm->state = STATE_READY;
+}
+
+static void
+session_disconnected_handler (session_disconnected_msg_t * mp)
+{
+ app_session_evt_t _app_evt, *app_evt = &_app_evt;
+ udp_echo_main_t *utm = &udp_echo_main;
+ session_disconnected_reply_msg_t *rmp;
+ app_session_t *session;
+ uword *p;
+ int rv = 0;
+
+ p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
+
+ if (p)
+ {
+ session = pool_elt_at_index (utm->sessions, p[0]);
+ hash_unset (utm->session_index_by_vpp_handles, mp->handle);
+ pool_put (utm->sessions, session);
+ }
+ else
+ {
+ clib_warning ("couldn't find session key %llx", mp->handle);
+ return;
+ }
+
+ app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
+ SESSION_CTRL_EVT_DISCONNECTED_REPLY);
+ rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
+ rmp->retval = rv;
+ rmp->handle = mp->handle;
+ rmp->context = mp->context;
+ app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
+}
+
+static void
+session_connected_handler (session_connected_msg_t * mp)
+{
+ udp_echo_main_t *utm = &udp_echo_main;
+ unformat_input_t _input, *input = &_input;
+ session_endpoint_extended_t _sep, *sep = &_sep;
+ app_session_t *session;
+
+ ASSERT (utm->i_am_server == 0);
+
+ if (mp->retval)
+ {
+ clib_warning ("failed connect");
+ return;
+ }
+
+ ASSERT (mp->server_rx_fifo && mp->server_tx_fifo);
+
+ pool_get (utm->sessions, session);
+ session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
+ session->tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
+ session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
+ svm_msg_q_t *);
+ /* Cut-through case */
+ if (mp->client_event_queue_address)
+ {
+ clib_warning ("cut-through session");
+ utm->cut_through_session_index = session - utm->sessions;
+ utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
+ svm_msg_q_t *);
+ utm->our_event_queue = uword_to_pointer (mp->client_event_queue_address,
+ svm_msg_q_t *);
+ utm->do_echo = 1;
+ }
+ else
+ {
+ utm->connected_session = session - utm->sessions;
+ utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
+ svm_msg_q_t *);
+
+ clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
+ sizeof (ip46_address_t));
+ session->transport.is_ip4 = mp->is_ip4;
+ session->transport.lcl_port = mp->lcl_port;
+
+ unformat_init_vector (input, utm->connect_uri);
+ if (!unformat (input, "%U", unformat_uri, sep))
+ {
+ clib_warning ("can't figure out remote ip and port");
+ utm->state = STATE_FAILED;
+ unformat_free (input);
+ return;
+ }
+ unformat_free (input);
+ clib_memcpy (&session->transport.rmt_ip, &sep->ip,
+ sizeof (ip46_address_t));
+ session->transport.rmt_port = sep->port;
+ session->is_dgram = !utm->is_connected;
+ }
+ utm->state = STATE_READY;
+}
+
+static void
+handle_mq_event (session_event_t * e)
+{
+ switch (e->event_type)
+ {
+ case SESSION_CTRL_EVT_ACCEPTED:
+ session_accepted_handler ((session_accepted_msg_t *) e->data);
+ break;
+ case SESSION_CTRL_EVT_CONNECTED:
+ session_connected_handler ((session_connected_msg_t *) e->data);
+ break;
+ case SESSION_CTRL_EVT_DISCONNECTED:
+ session_disconnected_handler ((session_disconnected_msg_t *) e->data);
+ break;
+ default:
+ clib_warning ("unhandled %u", e->event_type);
+ }
+}
+
+static void
+udp_client_send_connect (udp_echo_main_t * utm)
{
vl_api_connect_uri_t *cmp;
cmp = vl_msg_api_alloc (sizeof (*cmp));
@@ -677,18 +874,41 @@ client_send_data (udp_echo_main_t * utm)
transfer_type);
}
+static int
+application_attach (udp_echo_main_t * utm)
+{
+ application_send_attach (utm);
+ if (wait_for_state_change (utm, STATE_ATTACHED))
+ {
+ clib_warning ("timeout waiting for STATE_ATTACHED");
+ return -1;
+ }
+ return 0;
+}
+
static void
client_test (udp_echo_main_t * utm)
{
+ f64 start_time, timeout = 100.0;
app_session_t *session;
+ svm_msg_q_msg_t msg;
+ session_event_t *e;
- application_send_attach (utm);
- udp_client_connect (utm);
+ if (application_attach (utm))
+ return;
+
+ udp_client_send_connect (utm);
+
+ start_time = clib_time_now (&utm->clib_time);
+ while (pool_elts (utm->sessions) != 1
+ && clib_time_now (&utm->clib_time) - start_time < timeout
+ && utm->state != STATE_FAILED)
- if (wait_for_state_change (utm, STATE_READY))
{
- clib_warning ("timeout waiting for STATE_READY");
- return;
+ svm_msg_q_sub (utm->our_event_queue, &msg, SVM_Q_WAIT, 0);
+ e = svm_msg_q_msg_data (utm->our_event_queue, &msg);
+ handle_mq_event (e);
+ svm_msg_q_free_msg (utm->our_event_queue, &msg);
}
if (utm->cut_through_session_index != ~0)
@@ -704,6 +924,7 @@ client_test (udp_echo_main_t * utm)
}
application_detach (utm);
+ wait_for_state_change (utm, STATE_DETACHED);
}
static void
@@ -792,84 +1013,6 @@ vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
vec_free (seg_name);
}
-/**
- * Acting as server for redirected connect requests
- */
-static void
-vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
-{
- u32 segment_index;
- udp_echo_main_t *utm = &udp_echo_main;
- svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
- svm_fifo_segment_create_args_t _a, *a = &_a;
- svm_fifo_segment_private_t *seg;
- svm_queue_t *client_q;
- vl_api_connect_session_reply_t *rmp;
- app_session_t *session = 0;
- int rv = 0;
-
- /* Create the segment */
- a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
- utm->unique_segment_index++, 0);
- a->segment_size = utm->configured_segment_size;
-
- rv = svm_fifo_segment_create (a);
- if (rv)
- {
- clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
- rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
- goto send_reply;
- }
-
- vec_add2 (utm->seg, seg, 1);
-
- segment_index = vec_len (sm->segments) - 1;
- memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
-
- pool_get (utm->sessions, session);
-
- session->rx_fifo = svm_fifo_segment_alloc_fifo
- (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
- ASSERT (session->rx_fifo);
-
- session->tx_fifo = svm_fifo_segment_alloc_fifo
- (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
- ASSERT (session->tx_fifo);
-
- session->rx_fifo->master_session_index = session - utm->sessions;
- session->tx_fifo->master_session_index = session - utm->sessions;
- utm->cut_through_session_index = session - utm->sessions;
-
- rv = pthread_create (&utm->cut_through_thread_handle,
- NULL /*attr */ , cut_through_thread_fn, 0);
- if (rv)
- {
- clib_warning ("pthread_create returned %d", rv);
- rv = VNET_API_ERROR_SYSCALL_ERROR_1;
- }
-
-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 = ntohl (rv);
- rmp->segment_name_length = vec_len (a->segment_name);
- if (session)
- {
- rmp->server_rx_fifo = pointer_to_uword (session->rx_fifo);
- rmp->server_tx_fifo = pointer_to_uword (session->tx_fifo);
- }
-
- memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
-
- vec_free (a->segment_name);
-
- client_q = uword_to_pointer (mp->client_queue_address, svm_queue_t *);
- vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
-}
-
static void
vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
{
@@ -881,184 +1024,9 @@ vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
utm->state = STATE_START;
}
-static void
-vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
-{
- udp_echo_main_t *utm = &udp_echo_main;
- vl_api_accept_session_reply_t *rmp;
- svm_fifo_t *rx_fifo, *tx_fifo;
- app_session_t *session;
- static f64 start_time;
- u32 session_index;
- int rv = 0;
-
- if (start_time == 0.0)
- start_time = clib_time_now (&utm->clib_time);
-
- utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
- svm_msg_q_t *);
- rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
- tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
-
- pool_get (utm->sessions, session);
- memset (session, 0, sizeof (*session));
- session_index = session - utm->sessions;
-
- /* Cut-through case */
- if (mp->server_event_queue_address)
- {
- clib_warning ("cut-through session");
- utm->our_event_queue = uword_to_pointer (mp->server_event_queue_address,
- svm_msg_q_t *);
- rx_fifo->master_session_index = session_index;
- tx_fifo->master_session_index = session_index;
- utm->cut_through_session_index = session_index;
- session->rx_fifo = rx_fifo;
- session->tx_fifo = tx_fifo;
-
- rv = pthread_create (&utm->cut_through_thread_handle,
- NULL /*attr */ , cut_through_thread_fn, 0);
- if (rv)
- {
- clib_warning ("pthread_create returned %d", rv);
- rv = VNET_API_ERROR_SYSCALL_ERROR_1;
- }
- utm->do_echo = 1;
- }
- else
- {
- rx_fifo->client_session_index = session_index;
- tx_fifo->client_session_index = session_index;
- session->rx_fifo = rx_fifo;
- session->tx_fifo = tx_fifo;
- clib_memcpy (&session->transport.rmt_ip, mp->ip,
- sizeof (ip46_address_t));
- session->transport.is_ip4 = mp->is_ip4;
- session->transport.rmt_port = mp->port;
- }
-
- hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
- if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
- {
- f64 now = clib_time_now (&utm->clib_time);
- fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
- pool_elts (utm->sessions), now - start_time,
- (f64) pool_elts (utm->sessions) / (now - start_time));
- }
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
- rmp->handle = mp->handle;
- rmp->context = mp->context;
- rmp->retval = rv;
- vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
-
- CLIB_MEMORY_BARRIER ();
- utm->state = STATE_READY;
-}
-
-static void
-vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
-{
- udp_echo_main_t *utm = &udp_echo_main;
- app_session_t *session;
- vl_api_disconnect_session_reply_t *rmp;
- uword *p;
- int rv = 0;
-
- p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
-
- if (p)
- {
- session = pool_elt_at_index (utm->sessions, p[0]);
- hash_unset (utm->session_index_by_vpp_handles, mp->handle);
- pool_put (utm->sessions, session);
- }
- else
- {
- clib_warning ("couldn't find session key %llx", 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 = rv;
- rmp->handle = mp->handle;
- rmp->context = mp->context;
- vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
-}
-
-static void
-vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
-{
- udp_echo_main_t *utm = &udp_echo_main;
- unformat_input_t _input, *input = &_input;
- session_endpoint_extended_t _sep, *sep = &_sep;
- app_session_t *session;
-
- ASSERT (utm->i_am_server == 0);
-
- if (mp->retval)
- {
- clib_warning ("failed connect");
- return;
- }
-
- ASSERT (mp->server_rx_fifo && mp->server_tx_fifo);
-
- pool_get (utm->sessions, session);
- session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
- session->tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
- session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
- svm_msg_q_t *);
- /* Cut-through case */
- if (mp->client_event_queue_address)
- {
- clib_warning ("cut-through session");
- utm->cut_through_session_index = session - utm->sessions;
- utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
- svm_msg_q_t *);
- utm->our_event_queue = uword_to_pointer (mp->client_event_queue_address,
- svm_msg_q_t *);
- utm->do_echo = 1;
- }
- else
- {
- utm->connected_session = session - utm->sessions;
- utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
- svm_msg_q_t *);
-
- clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
- sizeof (ip46_address_t));
- session->transport.is_ip4 = mp->is_ip4;
- session->transport.lcl_port = mp->lcl_port;
-
- unformat_init_vector (input, utm->connect_uri);
- if (!unformat (input, "%U", unformat_uri, sep))
- {
- clib_warning ("can't figure out remote ip and port");
- utm->state = STATE_FAILED;
- unformat_free (input);
- return;
- }
- unformat_free (input);
- clib_memcpy (&session->transport.rmt_ip, &sep->ip,
- sizeof (ip46_address_t));
- session->transport.rmt_port = sep->port;
- session->is_dgram = !utm->is_connected;
- }
- utm->state = STATE_READY;
-}
-
#define foreach_tcp_echo_msg \
_(BIND_URI_REPLY, bind_uri_reply) \
-_(CONNECT_URI, connect_uri) \
-_(CONNECT_SESSION_REPLY, connect_session_reply) \
_(UNBIND_URI_REPLY, unbind_uri_reply) \
-_(ACCEPT_SESSION, accept_session) \
-_(DISCONNECT_SESSION, disconnect_session) \
_(MAP_ANOTHER_SEGMENT, map_another_segment) \
_(UNMAP_SEGMENT, unmap_segment) \
_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
@@ -1113,7 +1081,7 @@ init_error_string_table (udp_echo_main_t * utm)
}
void
-server_handle_fifo_event_rx (udp_echo_main_t * utm, session_fifo_event_t * e)
+server_handle_fifo_event_rx (udp_echo_main_t * utm, session_event_t * e)
{
app_session_t *s;
int rv;
@@ -1134,7 +1102,7 @@ server_handle_fifo_event_rx (udp_echo_main_t * utm, session_fifo_event_t * e)
void
server_handle_event_queue (udp_echo_main_t * utm)
{
- session_fifo_event_t *e;
+ session_event_t *e;
svm_msg_q_msg_t msg;
while (utm->state != STATE_READY)
@@ -1154,12 +1122,8 @@ server_handle_event_queue (udp_echo_main_t * utm)
server_handle_fifo_event_rx (utm, e);
break;
- case FIFO_EVENT_DISCONNECT:
- utm->time_to_stop = 1;
- break;
-
default:
- clib_warning ("unknown event type %d", e->event_type);
+ handle_mq_event (e);
break;
}
svm_msg_q_free_msg (utm->our_event_queue, &msg);