VPP-1576: fix a set of coverity warnings
[vpp.git] / src / plugins / unittest / session_test.c
index 1689709..fa6a99f 100644 (file)
@@ -19,6 +19,7 @@
 #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...)              \
 ({                                                             \
@@ -40,6 +41,9 @@
     }                                                          \
 }
 
+#define ST_DBG(_comment, _args...)                             \
+    fformat(stderr,  _comment "\n",  ##_args);                 \
+
 void
 dummy_session_reset_callback (session_t * s)
 {
@@ -161,8 +165,8 @@ session_test_basic (vlib_main_t * vm, unformat_input_t * input)
 {
   session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
   u64 options[APP_OPTIONS_N_OPTIONS], bind4_handle, bind6_handle;
-  clib_error_t *error = 0;
   u32 server_index;
+  int error = 0;
 
   clib_memset (options, 0, sizeof (options));
   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
@@ -182,36 +186,36 @@ session_test_basic (vlib_main_t * vm, unformat_input_t * input)
   vec_free (attach_args.name);
 
   server_sep.is_ip4 = 1;
-  vnet_bind_args_t bind_args = {
+  vnet_listen_args_t bind_args = {
     .sep = server_sep,
     .app_index = 0,
   };
 
   bind_args.app_index = server_index;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "server bind4 should work");
   bind4_handle = bind_args.handle;
 
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error != 0), "double server bind4 should not work");
 
   bind_args.sep.is_ip4 = 0;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "server bind6 should work");
   bind6_handle = bind_args.handle;
 
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error != 0), "double server bind6 should not work");
 
-  vnet_unbind_args_t unbind_args = {
+  vnet_unlisten_args_t unbind_args = {
     .handle = bind4_handle,
     .app_index = server_index,
   };
-  error = vnet_unbind (&unbind_args);
+  error = vnet_unlisten (&unbind_args);
   SESSION_TEST ((error == 0), "unbind4 should work");
 
   unbind_args.handle = bind6_handle;
-  error = vnet_unbind (&unbind_args);
+  error = vnet_unlisten (&unbind_args);
   SESSION_TEST ((error == 0), "unbind6 should work");
 
   vnet_app_detach_args_t detach_args = {
@@ -277,8 +281,8 @@ session_test_endpoint_cfg (vlib_main_t * vm, unformat_input_t * input)
   ip4_address_t intf_addr[3];
   transport_connection_t *tc;
   session_t *s;
-  clib_error_t *error;
   u8 *appns_id;
+  int error;
 
   /*
    * Create the loopbacks
@@ -306,8 +310,7 @@ session_test_endpoint_cfg (vlib_main_t * vm, unformat_input_t * input)
     .is_add = 1
   };
   error = vnet_app_namespace_add_del (&ns_args);
-  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
-               clib_error_get_code (error));
+  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error);
 
   /*
    * Attach client/server
@@ -340,11 +343,11 @@ session_test_endpoint_cfg (vlib_main_t * vm, unformat_input_t * input)
 
   server_sep.is_ip4 = 1;
   server_sep.port = dummy_server_port;
-  vnet_bind_args_t bind_args = {
+  vnet_listen_args_t bind_args = {
     .sep = server_sep,
     .app_index = server_index,
   };
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "server bind should work");
 
   /*
@@ -424,13 +427,12 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
   session_endpoint_t client_sep = SESSION_ENDPOINT_NULL;
   session_endpoint_t intf_sep = SESSION_ENDPOINT_NULL;
-  clib_error_t *error = 0;
   u8 *ns_id = format (0, "appns1");
   app_namespace_t *app_ns;
   application_t *server;
   session_t *s;
   u64 handle;
-  int code;
+  int error = 0;
 
   server_sep.is_ip4 = 1;
   server_sep.port = dummy_port;
@@ -447,7 +449,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
     .name = format (0, "session_test"),
   };
 
-  vnet_bind_args_t bind_args = {
+  vnet_listen_args_t bind_args = {
     .sep = server_sep,
     .app_index = 0,
   };
@@ -458,7 +460,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   };
   clib_memcpy (&connect_args.sep, &client_sep, sizeof (client_sep));
 
-  vnet_unbind_args_t unbind_args = {
+  vnet_unlisten_args_t unbind_args = {
     .handle = bind_args.handle,
     .app_index = 0,
   };
@@ -487,8 +489,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
     .is_add = 1
   };
   error = vnet_app_namespace_add_del (&ns_args);
-  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
-               clib_error_get_code (error));
+  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error);
 
   app_ns = app_namespace_get_from_id (ns_id);
   SESSION_TEST ((app_ns != 0), "should find ns %v status", ns_id);
@@ -509,9 +510,8 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
 
   error = vnet_application_attach (&attach_args);
   SESSION_TEST ((error != 0), "app attachment should fail");
-  code = clib_error_get_code (error);
-  SESSION_TEST ((code == VNET_API_ERROR_APP_WRONG_NS_SECRET),
-               "code should be wrong ns secret: %d", code);
+  SESSION_TEST ((error == VNET_API_ERROR_APP_WRONG_NS_SECRET),
+               "code should be wrong ns secret: %d", error);
 
   /*
    * Attach server with global default scope
@@ -530,7 +530,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
                "server should be in the default ns");
 
   bind_args.app_index = server_index;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "server bind should work");
 
   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
@@ -544,7 +544,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
 
   unbind_args.app_index = server_index;
   unbind_args.handle = bind_args.handle;
-  error = vnet_unbind (&unbind_args);
+  error = vnet_unlisten (&unbind_args);
   SESSION_TEST ((error == 0), "unbind should work");
 
   s = session_lookup_listener (server_st_index, &server_sep);
@@ -570,7 +570,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
                "server should be in the right ns");
 
   bind_args.app_index = server_index;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "bind should work");
   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
   s = session_lookup_listener (server_st_index, &server_sep);
@@ -594,15 +594,13 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   connect_args.app_index = client_index;
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error != 0), "client connect should return error code");
-  code = clib_error_get_code (error);
-  SESSION_TEST ((code == VNET_API_ERROR_INVALID_VALUE),
+  SESSION_TEST ((error == VNET_API_ERROR_INVALID_VALUE),
                "error code should be invalid value (zero ip)");
   SESSION_TEST ((dummy_segment_count == 0),
                "shouldn't have received request to map new segment");
   connect_args.sep.ip.ip4.as_u8[0] = 127;
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error == 0), "client connect should not return error code");
-  code = clib_error_get_code (error);
   SESSION_TEST ((dummy_segment_count == 1),
                "should've received request to map new segment");
   SESSION_TEST ((dummy_accept == 1), "should've received accept request");
@@ -616,8 +614,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   SESSION_TEST ((error == 0), "client attachment should work");
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error != 0), "client connect should return error code");
-  code = clib_error_get_code (error);
-  SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT),
+  SESSION_TEST ((error == VNET_API_ERROR_SESSION_CONNECT),
                "error code should be connect (nothing in local scope)");
   detach_args.app_index = client_index;
   vnet_application_detach (&detach_args);
@@ -627,7 +624,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
    */
   unbind_args.handle = bind_args.handle;
   unbind_args.app_index = server_index;
-  error = vnet_unbind (&unbind_args);
+  error = vnet_unlisten (&unbind_args);
   SESSION_TEST ((error == 0), "unbind should work");
 
   s = session_lookup_listener (server_st_index, &server_sep);
@@ -650,7 +647,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
                "app should be in the right ns");
 
   bind_args.app_index = server_index;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "bind should work");
 
   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
@@ -662,7 +659,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
                "listener should exist in local table");
 
   unbind_args.handle = bind_args.handle;
-  error = vnet_unbind (&unbind_args);
+  error = vnet_unlisten (&unbind_args);
   SESSION_TEST ((error == 0), "unbind should work");
 
   handle = session_lookup_local_endpoint (server_local_st_index, &server_sep);
@@ -679,8 +676,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   vnet_application_attach (&attach_args);
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error != 0), "client connect should return error code");
-  code = clib_error_get_code (error);
-  SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT),
+  SESSION_TEST ((error == VNET_API_ERROR_SESSION_CONNECT),
                "error code should be connect (not in same ns)");
   detach_args.app_index = client_index;
   vnet_application_detach (&detach_args);
@@ -697,12 +693,11 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   session_create_lookpback (0, &sw_if_index, &intf_addr);
 
   /*
-   * Update namespace
+   * Update namespace with interface
    */
   ns_args.sw_if_index = sw_if_index;
   error = vnet_app_namespace_add_del (&ns_args);
-  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
-               clib_error_get_code (error));
+  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error);
 
   /*
    * Attach server with local and global scope
@@ -719,7 +714,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   server_wrk_index = application_get_default_worker (server)->wrk_index;
 
   bind_args.app_index = server_index;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
   s = session_lookup_listener (server_st_index, &server_sep);
   SESSION_TEST ((s == 0), "zero listener should not exist in global table");
@@ -751,8 +746,7 @@ session_test_rule_table (vlib_main_t * vm, unformat_input_t * input)
   u16 lcl_port = 1234, rmt_port = 4321;
   u32 action_index = 1, res;
   ip4_address_t lcl_lkup, rmt_lkup;
-  clib_error_t *error;
-  int verbose = 0;
+  int verbose = 0, error;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -1023,13 +1017,14 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
   u32 dummy_server_api_index = ~0;
   transport_connection_t *tc;
   u32 dummy_port = 1111;
-  clib_error_t *error = 0;
   u8 is_filtered = 0, *ns_id = format (0, "appns1");
   session_t *listener, *s;
   app_namespace_t *default_ns = app_namespace_get_default ();
   u32 local_ns_index = default_ns->local_table_index;
-  int verbose = 0, rv;
+  int verbose = 0;
   app_namespace_t *app_ns;
+  app_listener_t *al;
+  int error = 0;
   u64 handle;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
@@ -1056,7 +1051,7 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
     .name = format (0, "session_test"),
   };
 
-  vnet_bind_args_t bind_args = {
+  vnet_listen_args_t bind_args = {
     .sep = server_sep,
     .app_index = 0,
   };
@@ -1074,10 +1069,11 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
   server_index = attach_args.app_index;
 
   bind_args.app_index = server_index;
-  error = vnet_bind (&bind_args);
+  error = vnet_listen (&bind_args);
   SESSION_TEST ((error == 0), "server bound to %U/%d", format_ip46_address,
                &server_sep.ip, 1, server_sep.port);
-  listener = listen_session_get_from_handle (bind_args.handle);
+  al = app_listener_get_w_handle (bind_args.handle);
+  listener = app_listener_get_session (al);
   ip4_address_t lcl_ip = {
     .as_u32 = clib_host_to_net_u32 (0x01020304),
   };
@@ -1257,8 +1253,7 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
   /* Try connecting */
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error != 0), "connect should fail");
-  rv = clib_error_get_code (error);
-  SESSION_TEST ((rv == VNET_API_ERROR_APP_CONNECT_FILTERED),
+  SESSION_TEST ((error == VNET_API_ERROR_APP_CONNECT_FILTERED),
                "connect should be filtered");
 
   sep.ip.ip4.as_u32 -= 1 << 24;
@@ -1489,8 +1484,7 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
     .is_add = 1
   };
   error = vnet_app_namespace_add_del (&ns_args);
-  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
-               clib_error_get_code (error));
+  SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error);
   app_ns = app_namespace_get_from_id (ns_id);
 
   attach_args.namespace_id = ns_id;
@@ -1533,8 +1527,7 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
 
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error != 0), "connect should fail");
-  rv = clib_error_get_code (error);
-  SESSION_TEST ((rv == VNET_API_ERROR_APP_CONNECT_FILTERED),
+  SESSION_TEST ((error == VNET_API_ERROR_APP_CONNECT_FILTERED),
                "connect should be filtered");
 
   /*
@@ -1547,8 +1540,7 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
   connect_args.app_index = server_index;
   error = vnet_connect (&connect_args);
   SESSION_TEST ((error != 0), "connect should fail");
-  rv = clib_error_get_code (error);
-  SESSION_TEST ((rv == VNET_API_ERROR_APP_CONNECT_FILTERED),
+  SESSION_TEST ((error == VNET_API_ERROR_APP_CONNECT_FILTERED),
                "connect should be filtered");
 
   args.table_args.is_add = 0;
@@ -1587,13 +1579,12 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input)
   unformat_input_t tmp_input;
   u32 server_index, app_index;
   u32 dummy_server_api_index = ~0, sw_if_index = 0;
-  clib_error_t *error = 0;
   u8 is_filtered = 0;
   session_t *s;
   transport_connection_t *tc;
   u16 lcl_port = 1234, rmt_port = 4321;
   app_namespace_t *app_ns;
-  int verbose = 0;
+  int verbose = 0, error = 0;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -1705,6 +1696,180 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input)
   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 (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;
+  u32 fifo_segment_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, &rx_fifo, &tx_fifo,
+                                      &fifo_segment_index);
+  s.rx_fifo = rx_fifo;
+  s.tx_fifo = tx_fifo;
+  s.session_state = SESSION_STATE_READY;
+  counter = (u64 *) rx_fifo->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 clib_error_t *
 session_test (vlib_main_t * vm,
              unformat_input_t * input, vlib_cli_command_t * cmd_arg)
@@ -1727,6 +1892,8 @@ session_test (vlib_main_t * vm,
        res = session_test_proxy (vm, input);
       else if (unformat (input, "endpt-cfg"))
        res = session_test_endpoint_cfg (vm, input);
+      else if (unformat (input, "mq"))
+       res = session_test_mq (vm, input);
       else if (unformat (input, "all"))
        {
          if ((res = session_test_basic (vm, input)))
@@ -1741,6 +1908,8 @@ session_test (vlib_main_t * vm,
            goto done;
          if ((res = session_test_endpoint_cfg (vm, input)))
            goto done;
+         if ((res = session_test_mq (vm, input)))
+           goto done;
        }
       else
        break;