#include <vnet/session/session.h>
#include <vnet/session/session_rules_table.h>
#include <vnet/tcp/tcp.h>
+#include <sys/epoll.h>
#define SESSION_TEST_I(_cond, _comment, _args...) \
({ \
} \
}
+#define ST_DBG(_comment, _args...) \
+ fformat(stderr, _comment "\n", ##_args); \
+
void
dummy_session_reset_callback (session_t * s)
{
volatile u32 connected_session_thread = ~0;
int
dummy_session_connected_callback (u32 app_index, u32 api_context,
- session_t * s, u8 is_fail)
+ session_t * s, session_error_t err)
{
if (s)
{
{
/* fails spectacularly */
/* vnet_delete_loopback_interface (sw_if_index); */
+
+ vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index, 0);
}
static int
session_test_basic (vlib_main_t * vm, unformat_input_t * input)
{
- session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
+ session_endpoint_cfg_t server_sep = SESSION_ENDPOINT_CFG_NULL;
u64 options[APP_OPTIONS_N_OPTIONS], bind4_handle, bind6_handle;
u32 server_index;
int error = 0;
server_sep.is_ip4 = 1;
vnet_listen_args_t bind_args = {
- .sep = server_sep,
+ .sep_ext = server_sep,
.app_index = 0,
+ .wrk_map_index = 0,
};
bind_args.app_index = server_index;
u32 server_index, client_index, sw_if_index[2], tries = 0;
u64 options[APP_OPTIONS_N_OPTIONS], dummy_secret = 1234;
u16 dummy_server_port = 1234, dummy_client_port = 5678;
- session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
+ session_endpoint_cfg_t server_sep = SESSION_ENDPOINT_CFG_NULL;
ip4_address_t intf_addr[3];
transport_connection_t *tc;
session_t *s;
server_sep.is_ip4 = 1;
server_sep.port = dummy_server_port;
vnet_listen_args_t bind_args = {
- .sep = server_sep,
+ .sep_ext = server_sep,
.app_index = server_index,
};
error = vnet_listen (&bind_args);
SESSION_TEST ((error == 0), "connect should work");
/* wait for stuff to happen */
- while ((connected_session_index == ~0
- || vec_len (tcp_main.wrk_ctx[0].pending_acks)) && ++tries < 100)
+ while (connected_session_index == ~0 && ++tries < 100)
vlib_process_suspend (vm, 100e-3);
clib_warning ("waited %.1f seconds for connections", tries / 10.0);
SESSION_TEST ((connected_session_index != ~0), "session should exist");
connect_args.app_index = client_index;
error = vnet_connect (&connect_args);
SESSION_TEST ((error != 0), "client connect should return error code");
- SESSION_TEST ((error == VNET_API_ERROR_INVALID_VALUE),
+ SESSION_TEST ((error == SESSION_E_INVALID_RMT_IP),
"error code should be invalid value (zero ip)");
SESSION_TEST ((dummy_segment_count == 0),
"shouldn't have received request to map new segment");
SESSION_TEST ((error == 0), "client attachment should work");
error = vnet_connect (&connect_args);
SESSION_TEST ((error != 0), "client connect should return error code");
- SESSION_TEST ((error == VNET_API_ERROR_SESSION_CONNECT),
+ SESSION_TEST ((error == SESSION_E_NOINTF),
"error code should be connect (nothing in local scope)");
detach_args.app_index = client_index;
vnet_application_detach (&detach_args);
vnet_application_attach (&attach_args);
error = vnet_connect (&connect_args);
SESSION_TEST ((error != 0), "client connect should return error code");
- SESSION_TEST ((error == VNET_API_ERROR_SESSION_CONNECT),
- "error code should be connect (not in same ns)");
+ SESSION_TEST ((error == SESSION_E_NOROUTE),
+ "error code should be noroute (not in same ns)");
detach_args.app_index = client_index;
vnet_application_detach (&detach_args);
/* Try connecting */
error = vnet_connect (&connect_args);
SESSION_TEST ((error != 0), "connect should fail");
- SESSION_TEST ((error == VNET_API_ERROR_APP_CONNECT_FILTERED),
- "connect should be filtered");
+ SESSION_TEST ((error == SESSION_E_FILTERED), "connect should be filtered");
sep.ip.ip4.as_u32 -= 1 << 24;
error = vnet_connect (&connect_args);
SESSION_TEST ((error != 0), "connect should fail");
- SESSION_TEST ((error == VNET_API_ERROR_APP_CONNECT_FILTERED),
- "connect should be filtered");
+ SESSION_TEST ((error == SESSION_E_FILTERED), "connect should be filtered");
/*
* Lookup test namespace
connect_args.app_index = server_index;
error = vnet_connect (&connect_args);
SESSION_TEST ((error != 0), "connect should fail");
- SESSION_TEST ((error == VNET_API_ERROR_APP_CONNECT_FILTERED),
- "connect should be filtered");
+ SESSION_TEST ((error == SESSION_E_FILTERED), "connect should be filtered");
args.table_args.is_add = 0;
vnet_session_rule_add_del (&args);
if (verbose)
unformat_free (&tmp_input);
vec_free (attach_args.name);
+ session_delete_loopback (sw_if_index);
+ return 0;
+}
+
+static inline void
+wait_for_event (svm_msg_q_t * mq, int fd, int epfd, u8 use_eventfd)
+{
+ if (!use_eventfd)
+ {
+ svm_msg_q_lock (mq);
+ while (svm_msg_q_is_empty (mq))
+ svm_msg_q_wait (mq);
+ }
+ else
+ {
+ int __clib_unused n_read, rv;
+ struct epoll_event ep_evt;
+ u64 buf;
+
+ while (1)
+ {
+ rv = epoll_wait (epfd, &ep_evt, 1, -1);
+ if (rv < 0)
+ {
+ ST_DBG ("epoll error");
+ exit (1);
+ }
+ else if (rv > 0 && (ep_evt.events & EPOLLIN))
+ {
+ n_read = read (fd, &buf, sizeof (buf));
+ }
+ else
+ continue;
+
+ if (!svm_msg_q_is_empty (mq))
+ {
+ svm_msg_q_lock (mq);
+ break;
+ }
+ }
+ }
+}
+
+static int
+session_test_mq_speed (vlib_main_t * vm, unformat_input_t * input)
+{
+ int error, __clib_unused verbose, use_eventfd = 0;
+ u64 i, n_test_msgs = 1 << 10, *counter;
+ u64 options[APP_OPTIONS_N_OPTIONS];
+ int epfd = -1, rv, prod_fd = -1;
+ svm_fifo_t *rx_fifo, *tx_fifo;
+ vl_api_registration_t *reg;
+ struct epoll_event ep_evt;
+ u32 app_index, api_index;
+ app_worker_t *app_wrk;
+ segment_manager_t *sm;
+ svm_msg_q_msg_t msg;
+ application_t *app;
+ svm_msg_q_t *mq;
+ f64 start, diff;
+ svm_queue_t *q;
+ session_t s;
+ pid_t pid;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "verbose"))
+ verbose = 1;
+ else if (unformat (input, "%d", &n_test_msgs))
+ ;
+ else if (unformat (input, "use-eventfd"))
+ use_eventfd = 1;
+ else
+ {
+ vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
+ input);
+ return -1;
+ }
+ }
+
+ q = clib_mem_alloc (sizeof (*q));
+ api_index = vl_api_memclnt_create_internal ("session_mq_test_api", q);
+
+ clib_memset (options, 0, sizeof (options));
+ options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
+ options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
+ options[APP_OPTIONS_EVT_QUEUE_SIZE] = 2048;
+
+ reg = vl_api_client_index_to_registration (api_index);
+ /* Shut up coverity */
+ if (reg == 0)
+ abort ();
+ if (!session_main.evt_qs_use_memfd_seg)
+ reg->clib_file_index = VL_API_INVALID_FI;
+
+ vnet_app_attach_args_t attach_args = {
+ .api_client_index = api_index,
+ .options = options,
+ .namespace_id = 0,
+ .session_cb_vft = &dummy_session_cbs,
+ .name = format (0, "session_mq_test"),
+ };
+ error = vnet_application_attach (&attach_args);
+ SESSION_TEST ((error == 0), "server attachment should work");
+
+ app_index = attach_args.app_index;
+
+ app = application_get (app_index);
+ app_wrk = application_get_worker (app, 0);
+ mq = app_wrk->event_queue;
+ if (use_eventfd)
+ {
+ svm_msg_q_alloc_producer_eventfd (mq);
+ svm_msg_q_alloc_consumer_eventfd (mq);
+ prod_fd = svm_msg_q_get_producer_eventfd (mq);
+ SESSION_TEST (prod_fd != -1, "mq producer eventd valid %u", prod_fd);
+ }
+
+ sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk);
+ segment_manager_alloc_session_fifos (sm, 0, &rx_fifo, &tx_fifo);
+ s.rx_fifo = rx_fifo;
+ s.tx_fifo = tx_fifo;
+ s.session_state = SESSION_STATE_READY;
+ counter = (u64 *) rx_fifo->head_chunk->data;
+ start = vlib_time_now (vm);
+
+ pid = fork ();
+ if (pid < 0)
+ SESSION_TEST (0, "fork failed");
+
+ if (pid == 0)
+ {
+ if (use_eventfd)
+ {
+ epfd = epoll_create1 (0);
+ SESSION_TEST (epfd != -1, "epfd created");
+ ep_evt.events = EPOLLIN;
+ ep_evt.data.u64 = prod_fd;
+ rv = epoll_ctl (epfd, EPOLL_CTL_ADD, prod_fd, &ep_evt);
+ SESSION_TEST (rv == 0, "epoll returned %d", rv);
+ }
+
+ for (i = 0; i < n_test_msgs; i++)
+ {
+ wait_for_event (mq, prod_fd, epfd, use_eventfd);
+ svm_msg_q_sub_w_lock (mq, &msg);
+ svm_msg_q_free_msg (mq, &msg);
+ svm_msg_q_unlock (mq);
+ *counter = *counter + 1;
+ svm_fifo_unset_event (rx_fifo);
+ }
+ exit (0);
+ }
+ else
+ {
+ ST_DBG ("client pid %u", pid);
+ for (i = 0; i < n_test_msgs; i++)
+ {
+ while (svm_fifo_has_event (rx_fifo))
+ ;
+ app_worker_lock_and_send_event (app_wrk, &s, SESSION_IO_EVT_RX);
+ }
+ }
+
+ diff = vlib_time_now (vm) - start;
+ ST_DBG ("done %u events in %.2f sec: %f evts/s", *counter,
+ diff, *counter / diff);
+
+ vnet_app_detach_args_t detach_args = {
+ .app_index = app_index,
+ .api_client_index = ~0,
+ };
+ vnet_application_detach (&detach_args);
+ return 0;
+}
+
+static int
+session_test_mq_basic (vlib_main_t * vm, unformat_input_t * input)
+{
+ svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
+ svm_msg_q_msg_t msg1, msg2, msg[12];
+ int __clib_unused verbose, i, rv;
+ svm_msg_q_t *mq;
+ svm_msg_q_ring_t *ring;
+ u8 *rings_ptr;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "verbose"))
+ verbose = 1;
+ else
+ {
+ vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
+ input);
+ return -1;
+ }
+ }
+
+ svm_msg_q_ring_cfg_t rc[2] = { {8, 8, 0}
+ , {8, 16, 0}
+ };
+ cfg->consumer_pid = ~0;
+ cfg->n_rings = 2;
+ cfg->q_nitems = 16;
+ cfg->ring_cfgs = rc;
+
+ mq = svm_msg_q_alloc (cfg);
+ SESSION_TEST (mq != 0, "svm_msg_q_alloc");
+ SESSION_TEST (vec_len (mq->rings) == 2, "ring allocation");
+ rings_ptr = (u8 *) mq->rings + vec_bytes (mq->rings);
+ vec_foreach (ring, mq->rings)
+ {
+ SESSION_TEST (ring->data == rings_ptr, "ring data");
+ rings_ptr += (uword) ring->nitems * ring->elsize;
+ }
+
+ msg1 = svm_msg_q_alloc_msg (mq, 8);
+ rv = (mq->rings[0].cursize != 1
+ || msg1.ring_index != 0 || msg1.elt_index != 0);
+ SESSION_TEST (rv == 0, "msg alloc1");
+
+ msg2 = svm_msg_q_alloc_msg (mq, 15);
+ rv = (mq->rings[1].cursize != 1
+ || msg2.ring_index != 1 || msg2.elt_index != 0);
+ SESSION_TEST (rv == 0, "msg alloc2");
+
+ svm_msg_q_free_msg (mq, &msg1);
+ SESSION_TEST (mq->rings[0].cursize == 0, "free msg");
+
+ for (i = 0; i < 12; i++)
+ {
+ msg[i] = svm_msg_q_alloc_msg (mq, 7);
+ *(u32 *) svm_msg_q_msg_data (mq, &msg[i]) = i;
+ }
+
+ rv = (mq->rings[0].cursize != 8 || mq->rings[1].cursize != 5);
+ SESSION_TEST (rv == 0, "msg alloc3");
+
+ *(u32 *) svm_msg_q_msg_data (mq, &msg2) = 123;
+ svm_msg_q_add (mq, &msg2, SVM_Q_NOWAIT);
+ for (i = 0; i < 12; i++)
+ svm_msg_q_add (mq, &msg[i], SVM_Q_NOWAIT);
+
+ rv = svm_msg_q_sub (mq, &msg2, SVM_Q_NOWAIT, 0);
+ SESSION_TEST (rv == 0, "dequeue1");
+
+ SESSION_TEST (msg2.ring_index == 1 && msg2.elt_index == 0,
+ "dequeue1 result");
+ rv = (*(u32 *) svm_msg_q_msg_data (mq, &msg2) == 123);
+ SESSION_TEST (rv, "dequeue 1 data");
+
+ svm_msg_q_free_msg (mq, &msg2);
+
+ for (i = 0; i < 12; i++)
+ {
+ if (svm_msg_q_sub (mq, &msg[i], SVM_Q_NOWAIT, 0))
+ SESSION_TEST (0, "dequeue2");
+ if (i < 8)
+ {
+ if (msg[i].ring_index != 0 || msg[i].elt_index != (i + 1) % 8)
+ SESSION_TEST (0, "dequeue2 result2");
+ }
+ else
+ {
+ if (msg[i].ring_index != 1 || msg[i].elt_index != (i - 8) + 1)
+ SESSION_TEST (0, "dequeue2 result3");
+ }
+ if (*(u32 *) svm_msg_q_msg_data (mq, &msg[i]) != i)
+ SESSION_TEST (0, "dequeue2 wrong data");
+ svm_msg_q_free_msg (mq, &msg[i]);
+ }
+ rv = (mq->rings[0].cursize == 0 && mq->rings[1].cursize == 0);
+ SESSION_TEST (rv, "post dequeue");
+
return 0;
}
res = session_test_proxy (vm, input);
else if (unformat (input, "endpt-cfg"))
res = session_test_endpoint_cfg (vm, input);
+ else if (unformat (input, "mq-speed"))
+ res = session_test_mq_speed (vm, input);
+ else if (unformat (input, "mq-basic"))
+ res = session_test_mq_basic (vm, input);
else if (unformat (input, "all"))
{
if ((res = session_test_basic (vm, input)))
goto done;
if ((res = session_test_endpoint_cfg (vm, input)))
goto done;
+ if ((res = session_test_mq_speed (vm, input)))
+ goto done;
+ if ((res = session_test_mq_basic (vm, input)))
+ goto done;
}
else
break;