hsa: echo client connects as rpc
[vpp.git] / src / plugins / hs_apps / echo_client.c
index 188aa90..d2ae252 100644 (file)
@@ -65,9 +65,8 @@ send_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
          svm_fifo_t *f = s->data.tx_fifo;
          rv = clib_min (svm_fifo_max_enqueue_prod (f), bytes_this_chunk);
          svm_fifo_enqueue_nocopy (f, rv);
-         session_send_io_evt_to_thread_custom (&f->master_session_index,
-                                               s->thread_index,
-                                               SESSION_IO_EVT_TX);
+         session_send_io_evt_to_thread_custom (
+           &f->shr->master_session_index, s->thread_index, SESSION_IO_EVT_TX);
        }
       else
        rv = app_send_stream (&s->data, test_data + test_buf_offset,
@@ -101,9 +100,8 @@ send_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
          hdr.lcl_port = at->lcl_port;
          svm_fifo_enqueue (f, sizeof (hdr), (u8 *) & hdr);
          svm_fifo_enqueue_nocopy (f, rv);
-         session_send_io_evt_to_thread_custom (&f->master_session_index,
-                                               s->thread_index,
-                                               SESSION_IO_EVT_TX);
+         session_send_io_evt_to_thread_custom (
+           &f->shr->master_session_index, s->thread_index, SESSION_IO_EVT_TX);
        }
       else
        {
@@ -310,7 +308,6 @@ echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
   return 0;
 }
 
-/* *INDENT-OFF* */
 VLIB_REGISTER_NODE (echo_clients_node) =
 {
   .function = echo_client_node_fn,
@@ -318,19 +315,37 @@ VLIB_REGISTER_NODE (echo_clients_node) =
   .type = VLIB_NODE_TYPE_INPUT,
   .state = VLIB_NODE_STATE_DISABLED,
 };
-/* *INDENT-ON* */
 
-static int
-create_api_loopback (echo_client_main_t * ecm)
+static void
+ec_reset_runtime_config (echo_client_main_t *ecm)
 {
-  api_main_t *am = vlibapi_get_main ();
-  vl_shmem_hdr_t *shmem_hdr;
-
-  shmem_hdr = am->shmem_hdr;
-  ecm->vl_input_queue = shmem_hdr->vl_input_queue;
-  ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
-                                                        ecm->vl_input_queue);
-  return 0;
+  ecm->n_clients = 1;
+  ecm->quic_streams = 1;
+  ecm->bytes_to_send = 8192;
+  ecm->no_return = 0;
+  ecm->fifo_size = 64 << 10;
+  ecm->connections_per_batch = 1000;
+  ecm->private_segment_count = 0;
+  ecm->private_segment_size = 256 << 20;
+  ecm->no_output = 0;
+  ecm->test_bytes = 0;
+  ecm->test_failed = 0;
+  ecm->tls_engine = CRYPTO_ENGINE_OPENSSL;
+  ecm->no_copy = 0;
+  ecm->run_test = ECHO_CLIENTS_STARTING;
+  ecm->ready_connections = 0;
+  ecm->connect_conn_index = 0;
+  ecm->rx_total = 0;
+  ecm->tx_total = 0;
+  ecm->barrier_acq_needed = 0;
+  ecm->prealloc_sessions = 0;
+  ecm->prealloc_fifos = 0;
+  ecm->appns_id = 0;
+  ecm->appns_secret = 0;
+  ecm->attach_flags = 0;
+  ecm->syn_timeout = 20.0;
+  ecm->test_timeout = 20.0;
+  vec_free (ecm->connect_uri);
 }
 
 static int
@@ -341,8 +356,37 @@ echo_clients_init (vlib_main_t * vm)
   u32 num_threads;
   int i;
 
-  if (create_api_loopback (ecm))
-    return -1;
+  ec_reset_runtime_config (ecm);
+
+  /* Store cli process node index for signaling */
+  ecm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
+  ecm->vlib_main = vm;
+
+  if (vlib_num_workers ())
+    {
+      /* The request came over the binary api and the inband cli handler
+       * is not mp_safe. Drop the barrier to make sure the workers are not
+       * blocked.
+       */
+      if (vlib_thread_is_main_w_barrier ())
+       {
+         ecm->barrier_acq_needed = 1;
+         vlib_worker_thread_barrier_release (vm);
+       }
+      /*
+       * There's a good chance that both the client and the server echo
+       * apps will be enabled so make sure the session queue node polls on
+       * the main thread as connections will probably be established on it.
+       */
+      vlib_node_set_state (vm, session_queue_node.index,
+                          VLIB_NODE_STATE_POLLING);
+
+      clib_spinlock_init (&ecm->sessions_lock);
+    }
+
+  /* App init done only once */
+  if (ecm->app_is_init)
+    return 0;
 
   num_threads = 1 /* main thread */  + vtm->n_threads;
 
@@ -355,16 +399,46 @@ echo_clients_init (vlib_main_t * vm)
   for (i = 0; i < num_threads; i++)
     vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
 
-  ecm->is_init = 1;
+  ecm->app_is_init = 1;
 
   vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
   vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
   vec_validate (ecm->quic_session_index_by_thread, vtm->n_vlib_mains);
   vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
 
+  vlib_worker_thread_barrier_sync (vm);
+  vnet_session_enable_disable (vm, 1 /* turn on session and transports */);
+  vlib_worker_thread_barrier_release (vm);
+
+  /* Turn on the builtin client input nodes */
+  for (i = 0; i < vtm->n_vlib_mains; i++)
+    vlib_node_set_state (vlib_get_main_by_index (i), echo_clients_node.index,
+                        VLIB_NODE_STATE_POLLING);
+
   return 0;
 }
 
+static void
+echo_clients_cleanup (echo_client_main_t *ecm)
+{
+  int i;
+
+  for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
+    {
+      vec_reset_length (ecm->connection_index_by_thread[i]);
+      vec_reset_length (ecm->connections_this_batch_by_thread[i]);
+      vec_reset_length (ecm->quic_session_index_by_thread[i]);
+    }
+
+  pool_free (ecm->sessions);
+  vec_free (ecm->connect_uri);
+  vec_free (ecm->appns_id);
+  clib_spinlock_free (&ecm->sessions_lock);
+
+  if (ecm->barrier_acq_needed)
+    vlib_worker_thread_barrier_sync (ecm->vlib_main);
+}
+
 static int
 quic_echo_clients_qsession_connected_callback (u32 app_index, u32 api_context,
                                               session_t * s,
@@ -457,9 +531,9 @@ quic_echo_clients_session_connected_callback (u32 app_index, u32 api_context,
   session->bytes_to_send = ecm->bytes_to_send;
   session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
   session->data.rx_fifo = s->rx_fifo;
-  session->data.rx_fifo->client_session_index = session_index;
+  session->data.rx_fifo->shr->client_session_index = session_index;
   session->data.tx_fifo = s->tx_fifo;
-  session->data.tx_fifo->client_session_index = session_index;
+  session->data.tx_fifo->shr->client_session_index = session_index;
   session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
   session->vpp_session_handle = session_handle (s);
 
@@ -524,9 +598,9 @@ echo_clients_session_connected_callback (u32 app_index, u32 api_context,
   session->bytes_to_send = ecm->bytes_to_send;
   session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
   session->data.rx_fifo = s->rx_fifo;
-  session->data.rx_fifo->client_session_index = session_index;
+  session->data.rx_fifo->shr->client_session_index = session_index;
   session->data.tx_fifo = s->tx_fifo;
-  session->data.tx_fifo->client_session_index = session_index;
+  session->data.tx_fifo->shr->client_session_index = session_index;
   session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
   session->vpp_session_handle = session_handle (s);
 
@@ -605,7 +679,8 @@ echo_clients_rx_callback (session_t * s)
       return -1;
     }
 
-  sp = pool_elt_at_index (ecm->sessions, s->rx_fifo->client_session_index);
+  sp =
+    pool_elt_at_index (ecm->sessions, s->rx_fifo->shr->client_session_index);
   receive_data_chunk (ecm, sp);
 
   if (svm_fifo_max_dequeue_cons (s->rx_fifo))
@@ -623,7 +698,6 @@ echo_client_add_segment_callback (u32 client_index, u64 segment_handle)
   return 0;
 }
 
-/* *INDENT-OFF* */
 static session_cb_vft_t echo_clients = {
   .session_reset_callback = echo_clients_session_reset_callback,
   .session_connected_callback = echo_clients_session_connected_callback,
@@ -632,23 +706,22 @@ static session_cb_vft_t echo_clients = {
   .builtin_app_rx_callback = echo_clients_rx_callback,
   .add_segment_callback = echo_client_add_segment_callback
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
-echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
+echo_clients_attach ()
 {
-  vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
-  vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
-  u32 prealloc_fifos, segment_size = 256 << 20;
+  vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
   echo_client_main_t *ecm = &echo_client_main;
   vnet_app_attach_args_t _a, *a = &_a;
+  u32 prealloc_fifos;
   u64 options[18];
   int rv;
 
   clib_memset (a, 0, sizeof (*a));
   clib_memset (options, 0, sizeof (options));
 
-  a->api_client_index = ecm->my_client_index;
+  a->api_client_index = ~0;
+  a->name = format (0, "echo_client");
   if (ecm->transport_proto == TRANSPORT_PROTO_QUIC)
     echo_clients.session_connected_callback =
       quic_echo_clients_session_connected_callback;
@@ -656,12 +729,9 @@ echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
 
   prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
 
-  if (ecm->private_segment_size)
-    segment_size = ecm->private_segment_size;
-
   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
-  options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
-  options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
+  options[APP_OPTIONS_SEGMENT_SIZE] = ecm->private_segment_size;
+  options[APP_OPTIONS_ADD_SEGMENT_SIZE] = ecm->private_segment_size;
   options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
   options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
@@ -669,30 +739,30 @@ echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
   options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
   options[APP_OPTIONS_PCT_FIRST_ALLOC] = 100;
-  if (appns_id)
+  options[APP_OPTIONS_FLAGS] |= ecm->attach_flags;
+  if (ecm->appns_id)
     {
-      options[APP_OPTIONS_FLAGS] |= appns_flags;
-      options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
+      options[APP_OPTIONS_NAMESPACE_SECRET] = ecm->appns_secret;
+      a->namespace_id = ecm->appns_id;
     }
   a->options = options;
-  a->namespace_id = appns_id;
 
   if ((rv = vnet_application_attach (a)))
     return clib_error_return (0, "attach returned %d", rv);
 
   ecm->app_index = a->app_index;
+  vec_free (a->name);
+
+  clib_memset (ck_pair, 0, sizeof (*ck_pair));
+  ck_pair->cert = (u8 *) test_srv_crt_rsa;
+  ck_pair->key = (u8 *) test_srv_key_rsa;
+  ck_pair->cert_len = test_srv_crt_rsa_len;
+  ck_pair->key_len = test_srv_key_rsa_len;
+  vnet_app_add_cert_key_pair (ck_pair);
+  ecm->ckpair_index = ck_pair->index;
+
+  ecm->test_client_attached = 1;
 
-  clib_memset (a_cert, 0, sizeof (*a_cert));
-  a_cert->app_index = a->app_index;
-  vec_validate (a_cert->cert, test_srv_crt_rsa_len);
-  clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
-  vnet_app_add_tls_cert (a_cert);
-
-  clib_memset (a_key, 0, sizeof (*a_key));
-  a_key->app_index = a->app_index;
-  vec_validate (a_key->key, test_srv_key_rsa_len);
-  clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
-  vnet_app_add_tls_key (a_key);
   return 0;
 }
 
@@ -703,11 +773,16 @@ echo_clients_detach ()
   vnet_app_detach_args_t _da, *da = &_da;
   int rv;
 
+  if (!ecm->test_client_attached)
+    return 0;
+
   da->app_index = ecm->app_index;
   da->api_client_index = ~0;
   rv = vnet_application_detach (da);
   ecm->test_client_attached = 0;
   ecm->app_index = ~0;
+  vnet_app_del_cert_key_pair (ecm->ckpair_index);
+
   return rv;
 }
 
@@ -735,159 +810,173 @@ echo_clients_start_tx_pthread (echo_client_main_t * ecm)
   return 0;
 }
 
-clib_error_t *
-echo_clients_connect (vlib_main_t * vm, u32 n_clients)
+static int
+ec_transport_needs_crypto (transport_proto_t proto)
+{
+  return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
+        proto == TRANSPORT_PROTO_QUIC;
+}
+
+int
+echo_clients_connect_rpc (void *args)
 {
   echo_client_main_t *ecm = &echo_client_main;
-  vnet_connect_args_t _a, *a = &_a;
-  int i, rv;
+  vnet_connect_args_t _a = {}, *a = &_a;
+  vlib_main_t *vm = vlib_get_main ();
+  int rv, needs_crypto;
+  u32 n_clients, ci;
+
+  n_clients = ecm->n_clients;
+  needs_crypto = ec_transport_needs_crypto (ecm->transport_proto);
+  clib_memcpy (&a->sep_ext, &ecm->connect_sep, sizeof (ecm->connect_sep));
+  a->app_index = ecm->app_index;
 
-  clib_memset (a, 0, sizeof (*a));
+  ci = ecm->connect_conn_index;
+
+  vlib_worker_thread_barrier_sync (vm);
 
-  for (i = 0; i < n_clients; i++)
+  while (ci < n_clients)
     {
-      a->uri = (char *) ecm->connect_uri;
-      a->api_context = i;
-      a->app_index = ecm->app_index;
+      /* Crude pacing for call setups  */
+      if (ci - ecm->ready_connections > 128)
+       {
+         ecm->connect_conn_index = ci;
+         break;
+       }
 
-      vlib_worker_thread_barrier_sync (vm);
-      if ((rv = vnet_connect_uri (a)))
+      a->api_context = ci;
+      if (needs_crypto)
        {
-         vlib_worker_thread_barrier_release (vm);
-         return clib_error_return (0, "connect returned: %d", rv);
+         session_endpoint_alloc_ext_cfg (&a->sep_ext,
+                                         TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
+         a->sep_ext.ext_cfg->crypto.ckpair_index = ecm->ckpair_index;
        }
-      vlib_worker_thread_barrier_release (vm);
 
-      /* Crude pacing for call setups  */
-      if ((i % 16) == 0)
-       vlib_process_suspend (vm, 100e-6);
-      ASSERT (i + 1 >= ecm->ready_connections);
-      while (i + 1 - ecm->ready_connections > 128)
-       vlib_process_suspend (vm, 1e-3);
+      rv = vnet_connect (a);
+
+      if (needs_crypto)
+       clib_mem_free (a->sep_ext.ext_cfg);
+
+      if (rv)
+       {
+         clib_warning ("connect returned: %U", format_session_error, rv);
+         signal_evt_to_cli (2);
+         break;
+       }
+
+      ci += 1;
     }
+
+  vlib_worker_thread_barrier_release (vm);
+
+  if (ci < ecm->expected_connections)
+    echo_clients_program_connects ();
+
   return 0;
 }
 
-#define ec_cli_output(_fmt, _args...)                  \
-  if (!ecm->no_output)                                 \
-    vlib_cli_output(vm, _fmt, ##_args)
+void
+echo_clients_program_connects (void)
+{
+  session_send_rpc_evt_to_thread_force (0, echo_clients_connect_rpc, 0);
+}
+
+#define ec_cli(_fmt, _args...)                                                \
+  if (!ecm->no_output)                                                        \
+  vlib_cli_output (vm, _fmt, ##_args)
 
 static clib_error_t *
-echo_clients_command_fn (vlib_main_t * vm,
-                        unformat_input_t * input, vlib_cli_command_t * cmd)
+echo_clients_command_fn (vlib_main_t *vm, unformat_input_t *input,
+                        vlib_cli_command_t *cmd)
 {
+  unformat_input_t _line_input, *line_input = &_line_input;
+  char *default_uri = "tcp://6.0.1.1/1234", *transfer_type;
   echo_client_main_t *ecm = &echo_client_main;
-  vlib_thread_main_t *thread_main = vlib_get_thread_main ();
-  u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
-  f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
-  char *default_uri = "tcp://6.0.1.1/1234";
   uword *event_data = 0, event_type;
-  f64 time_before_connects;
-  u32 n_clients = 1;
-  int preallocate_sessions = 0;
-  char *transfer_type;
   clib_error_t *error = 0;
-  u8 *appns_id = 0;
-  int i;
-  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
-  int rv;
+  int rv, had_config = 1;
+  u64 tmp, total_bytes;
+  f64 delta;
 
-  ecm->quic_streams = 1;
-  ecm->bytes_to_send = 8192;
-  ecm->no_return = 0;
-  ecm->fifo_size = 64 << 10;
-  ecm->connections_per_batch = 1000;
-  ecm->private_segment_count = 0;
-  ecm->private_segment_size = 0;
-  ecm->no_output = 0;
-  ecm->test_bytes = 0;
-  ecm->test_failed = 0;
-  ecm->vlib_main = vm;
-  ecm->tls_engine = CRYPTO_ENGINE_OPENSSL;
-  ecm->no_copy = 0;
-  ecm->run_test = ECHO_CLIENTS_STARTING;
+  if (ecm->test_client_attached)
+    return clib_error_return (0, "failed: already running!");
 
-  if (thread_main->n_vlib_mains > 1)
-    clib_spinlock_init (&ecm->sessions_lock);
-  vec_free (ecm->connect_uri);
+  if (echo_clients_init (vm))
+    {
+      error = clib_error_return (0, "failed init");
+      goto cleanup;
+    }
+
+  if (!unformat_user (input, unformat_line_input, line_input))
+    {
+      had_config = 0;
+      goto parse_config;
+    }
 
-  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "uri %s", &ecm->connect_uri))
+      if (unformat (line_input, "uri %s", &ecm->connect_uri))
        ;
-      else if (unformat (input, "nclients %d", &n_clients))
+      else if (unformat (line_input, "nclients %d", &ecm->n_clients))
        ;
-      else if (unformat (input, "quic-streams %d", &ecm->quic_streams))
+      else if (unformat (line_input, "quic-streams %d", &ecm->quic_streams))
        ;
-      else if (unformat (input, "mbytes %lld", &tmp))
+      else if (unformat (line_input, "mbytes %lld", &tmp))
        ecm->bytes_to_send = tmp << 20;
-      else if (unformat (input, "gbytes %lld", &tmp))
+      else if (unformat (line_input, "gbytes %lld", &tmp))
        ecm->bytes_to_send = tmp << 30;
-      else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
+      else if (unformat (line_input, "bytes %U", unformat_memory_size,
+                        &ecm->bytes_to_send))
        ;
-      else if (unformat (input, "test-timeout %f", &test_timeout))
+      else if (unformat (line_input, "test-timeout %f", &ecm->test_timeout))
        ;
-      else if (unformat (input, "syn-timeout %f", &syn_timeout))
+      else if (unformat (line_input, "syn-timeout %f", &ecm->syn_timeout))
        ;
-      else if (unformat (input, "no-return"))
+      else if (unformat (line_input, "no-return"))
        ecm->no_return = 1;
-      else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
+      else if (unformat (line_input, "fifo-size %d", &ecm->fifo_size))
        ecm->fifo_size <<= 10;
-      else if (unformat (input, "private-segment-count %d",
+      else if (unformat (line_input, "private-segment-count %d",
                         &ecm->private_segment_count))
        ;
-      else if (unformat (input, "private-segment-size %U",
-                        unformat_memory_size, &tmp))
-       {
-         if (tmp >= 0x100000000ULL)
-           return clib_error_return
-             (0, "private segment size %lld (%llu) too large", tmp, tmp);
-         ecm->private_segment_size = tmp;
-       }
-      else if (unformat (input, "preallocate-fifos"))
+      else if (unformat (line_input, "private-segment-size %U",
+                        unformat_memory_size, &ecm->private_segment_size))
+       ;
+      else if (unformat (line_input, "preallocate-fifos"))
        ecm->prealloc_fifos = 1;
-      else if (unformat (input, "preallocate-sessions"))
-       preallocate_sessions = 1;
-      else
-       if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
+      else if (unformat (line_input, "preallocate-sessions"))
+       ecm->prealloc_sessions = 1;
+      else if (unformat (line_input, "client-batch %d",
+                        &ecm->connections_per_batch))
        ;
-      else if (unformat (input, "appns %_%v%_", &appns_id))
+      else if (unformat (line_input, "appns %_%v%_", &ecm->appns_id))
        ;
-      else if (unformat (input, "all-scope"))
-       appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
-                       | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
-      else if (unformat (input, "local-scope"))
-       appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
-      else if (unformat (input, "global-scope"))
-       appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
-      else if (unformat (input, "secret %lu", &appns_secret))
+      else if (unformat (line_input, "all-scope"))
+       ecm->attach_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE |
+                             APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
+      else if (unformat (line_input, "local-scope"))
+       ecm->attach_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
+      else if (unformat (line_input, "global-scope"))
+       ecm->attach_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
+      else if (unformat (line_input, "secret %lu", &ecm->appns_secret))
        ;
-      else if (unformat (input, "no-output"))
+      else if (unformat (line_input, "no-output"))
        ecm->no_output = 1;
-      else if (unformat (input, "test-bytes"))
+      else if (unformat (line_input, "test-bytes"))
        ecm->test_bytes = 1;
-      else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
+      else if (unformat (line_input, "tls-engine %d", &ecm->tls_engine))
        ;
       else
-       return clib_error_return (0, "failed: unknown input `%U'",
-                                 format_unformat_error, input);
-    }
-
-  /* Store cli process node index for signalling */
-  ecm->cli_node_index =
-    vlib_get_current_process (vm)->node_runtime.node_index;
-
-  if (ecm->is_init == 0)
-    {
-      if (echo_clients_init (vm))
-       return clib_error_return (0, "failed init");
+       {
+         error = clib_error_return (0, "failed: unknown input `%U'",
+                                    format_unformat_error, line_input);
+         goto cleanup;
+       }
     }
 
+parse_config:
 
-  ecm->ready_connections = 0;
-  ecm->expected_connections = n_clients * ecm->quic_streams;
-  ecm->rx_total = 0;
-  ecm->tx_total = 0;
+  ecm->expected_connections = ecm->n_clients * ecm->quic_streams;
 
   if (!ecm->connect_uri)
     {
@@ -895,151 +984,138 @@ echo_clients_command_fn (vlib_main_t * vm,
       ecm->connect_uri = format (0, "%s%c", default_uri, 0);
     }
 
-  if ((rv = parse_uri ((char *) ecm->connect_uri, &sep)))
-    return clib_error_return (0, "Uri parse error: %d", rv);
-  ecm->transport_proto = sep.transport_proto;
-  ecm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
-
-#if ECHO_CLIENT_PTHREAD
-  echo_clients_start_tx_pthread ();
-#endif
-
-  vlib_worker_thread_barrier_sync (vm);
-  vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
-  vlib_worker_thread_barrier_release (vm);
-
-  if (ecm->test_client_attached == 0)
+  if ((rv = parse_uri ((char *) ecm->connect_uri, &ecm->connect_sep)))
     {
-      if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
-       {
-         vec_free (appns_id);
-         clib_error_report (error);
-         return error;
-       }
-      vec_free (appns_id);
+      error = clib_error_return (0, "Uri parse error: %d", rv);
+      goto cleanup;
     }
-  ecm->test_client_attached = 1;
+  ecm->transport_proto = ecm->connect_sep.transport_proto;
+  ecm->is_dgram = (ecm->transport_proto == TRANSPORT_PROTO_UDP);
 
-  /* Turn on the builtin client input nodes */
-  for (i = 0; i < thread_main->n_vlib_mains; i++)
-    vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
-                        VLIB_NODE_STATE_POLLING);
+  if (ecm->prealloc_sessions)
+    pool_init_fixed (ecm->sessions, 1.1 * ecm->n_clients);
 
-  if (preallocate_sessions)
-    pool_init_fixed (ecm->sessions, 1.1 * n_clients);
-
-  /* Fire off connect requests */
-  time_before_connects = vlib_time_now (vm);
-  if ((error = echo_clients_connect (vm, n_clients)))
+  if ((error = echo_clients_attach ()))
     {
+      clib_error_report (error);
       goto cleanup;
     }
 
-  /* Park until the sessions come up, or ten seconds elapse... */
-  vlib_process_wait_for_event_or_clock (vm, syn_timeout);
+  /*
+   * Start. Fire off connect requests
+   */
+
+  ecm->syn_start_time = vlib_time_now (vm);
+  echo_clients_program_connects ();
+
+  /*
+   * Park until the sessions come up, or syn_timeout seconds pass
+   */
+
+  vlib_process_wait_for_event_or_clock (vm, ecm->syn_timeout);
   event_type = vlib_process_get_events (vm, &event_data);
   switch (event_type)
     {
     case ~0:
-      ec_cli_output ("Timeout with only %d sessions active...",
-                    ecm->ready_connections);
+      ec_cli ("Timeout with only %d sessions active...",
+             ecm->ready_connections);
       error = clib_error_return (0, "failed: syn timeout with %d sessions",
                                 ecm->ready_connections);
       goto cleanup;
 
     case 1:
-      delta = vlib_time_now (vm) - time_before_connects;
+      delta = vlib_time_now (vm) - ecm->syn_start_time;
       if (delta != 0.0)
-       ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
-                      n_clients, delta, ((f64) n_clients) / delta);
-
-      ecm->test_start_time = vlib_time_now (ecm->vlib_main);
-      ec_cli_output ("Test started at %.6f", ecm->test_start_time);
+       ec_cli ("%d three-way handshakes in %.2f seconds %.2f/s",
+               ecm->n_clients, delta, ((f64) ecm->n_clients) / delta);
       break;
 
+    case 2:
+      error = clib_error_return (0, "failed: connect returned");
+      goto cleanup;
     default:
-      ec_cli_output ("unexpected event(1): %d", event_type);
+      ec_cli ("unexpected event(1): %d", event_type);
       error = clib_error_return (0, "failed: unexpected event(1): %d",
                                 event_type);
       goto cleanup;
     }
 
-  /* Now wait for the sessions to finish... */
-  vlib_process_wait_for_event_or_clock (vm, test_timeout);
+  /*
+   * Wait for the sessions to finish or test_timeout seconds pass
+   */
+  ecm->test_start_time = vlib_time_now (ecm->vlib_main);
+  ec_cli ("Test started at %.6f", ecm->test_start_time);
+  vlib_process_wait_for_event_or_clock (vm, ecm->test_timeout);
   event_type = vlib_process_get_events (vm, &event_data);
   switch (event_type)
     {
     case ~0:
-      ec_cli_output ("Timeout with %d sessions still active...",
-                    ecm->ready_connections);
+      ec_cli ("Timeout with %d sessions still active...",
+             ecm->ready_connections);
       error = clib_error_return (0, "failed: timeout with %d sessions",
                                 ecm->ready_connections);
       goto cleanup;
 
     case 2:
       ecm->test_end_time = vlib_time_now (vm);
-      ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
+      ec_cli ("Test finished at %.6f", ecm->test_end_time);
       break;
 
     default:
-      ec_cli_output ("unexpected event(2): %d", event_type);
+      ec_cli ("unexpected event(2): %d", event_type);
       error = clib_error_return (0, "failed: unexpected event(2): %d",
                                 event_type);
       goto cleanup;
     }
 
+  /*
+   * Done. Compute stats
+   */
   delta = ecm->test_end_time - ecm->test_start_time;
-  if (delta != 0.0)
+  if (delta == 0.0)
     {
-      total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
-      transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
-      ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
-                    total_bytes, total_bytes / (1ULL << 20),
-                    total_bytes / (1ULL << 30), delta);
-      ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
-                    transfer_type);
-      ec_cli_output ("%.4f gbit/second %s",
-                    (((f64) total_bytes * 8.0) / delta / 1e9),
-                    transfer_type);
-    }
-  else
-    {
-      ec_cli_output ("zero delta-t?");
+      ec_cli ("zero delta-t?");
       error = clib_error_return (0, "failed: zero delta-t");
       goto cleanup;
     }
 
+  total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
+  transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
+  ec_cli ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", total_bytes,
+         total_bytes / (1ULL << 20), total_bytes / (1ULL << 30), delta);
+  ec_cli ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
+         transfer_type);
+  ec_cli ("%.4f gbit/second %s", (((f64) total_bytes * 8.0) / delta / 1e9),
+         transfer_type);
+
   if (ecm->test_bytes && ecm->test_failed)
     error = clib_error_return (0, "failed: test bytes");
 
 cleanup:
+
+  /*
+   * Cleanup
+   */
   ecm->run_test = ECHO_CLIENTS_EXITING;
   vlib_process_wait_for_event_or_clock (vm, 10e-3);
-  for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
-    {
-      vec_reset_length (ecm->connection_index_by_thread[i]);
-      vec_reset_length (ecm->connections_this_batch_by_thread[i]);
-      vec_reset_length (ecm->quic_session_index_by_thread[i]);
-    }
-
-  pool_free (ecm->sessions);
 
   /* Detach the application, so we can use different fifo sizes next time */
-  if (ecm->test_client_attached)
+  if (echo_clients_detach ())
     {
-      if (echo_clients_detach ())
-       {
-         error = clib_error_return (0, "failed: app detach");
-         ec_cli_output ("WARNING: app detach failed...");
-       }
+      error = clib_error_return (0, "failed: app detach");
+      ec_cli ("WARNING: app detach failed...");
     }
+
+  echo_clients_cleanup (ecm);
+  if (had_config)
+    unformat_free (line_input);
+
   if (error)
-    ec_cli_output ("test failed");
-  vec_free (ecm->connect_uri);
+    ec_cli ("test failed");
+
   return error;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (echo_clients_command, static) =
 {
   .path = "test echo clients",
@@ -1051,13 +1127,12 @@ VLIB_CLI_COMMAND (echo_clients_command, static) =
   .function = echo_clients_command_fn,
   .is_mp_safe = 1,
 };
-/* *INDENT-ON* */
 
 clib_error_t *
 echo_clients_main_init (vlib_main_t * vm)
 {
   echo_client_main_t *ecm = &echo_client_main;
-  ecm->is_init = 0;
+  ecm->app_is_init = 0;
   return 0;
 }