hsa: Refactor quic_echo to allow other protocols 56/21056/9
authorNathan Skrzypczak <nathan.skrzypczak@gmail.com>
Mon, 5 Aug 2019 11:43:31 +0000 (13:43 +0200)
committerFlorin Coras <florin.coras@gmail.com>
Fri, 16 Aug 2019 14:47:38 +0000 (14:47 +0000)
Type: refactor

Change-Id: Iaef9091e1d057110530255e644fad1c298418966
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
src/plugins/hs_apps/CMakeLists.txt
src/plugins/hs_apps/sapi/echo_common.c [deleted file]
src/plugins/hs_apps/sapi/quic_echo.c [deleted file]
src/plugins/hs_apps/sapi/quic_echo.h [deleted file]
src/plugins/hs_apps/sapi/vpp_echo.c [new file with mode: 0644]
src/plugins/hs_apps/sapi/vpp_echo_bapi.c [new file with mode: 0644]
src/plugins/hs_apps/sapi/vpp_echo_common.c [new file with mode: 0644]
src/plugins/hs_apps/sapi/vpp_echo_common.h [new file with mode: 0644]
src/plugins/hs_apps/sapi/vpp_echo_proto_quic.c [new file with mode: 0644]
test/test_quic.py

index bf1f49c..2334cb5 100644 (file)
@@ -34,8 +34,12 @@ if(VPP_BUILD_HS_SAPI_APPS)
     DEPENDS api_headers
     NO_INSTALL
   )
-  add_vpp_executable(quic_echo
-    SOURCES sapi/quic_echo.c
+  add_vpp_executable(vpp_echo
+    SOURCES
+    sapi/vpp_echo.c
+    sapi/vpp_echo_common.c
+    sapi/vpp_echo_bapi.c
+    sapi/vpp_echo_proto_quic.c
     LINK_LIBRARIES vlibmemoryclient svm vppinfra pthread m rt
     DEPENDS api_headers
     NO_INSTALL
diff --git a/src/plugins/hs_apps/sapi/echo_common.c b/src/plugins/hs_apps/sapi/echo_common.c
deleted file mode 100644 (file)
index a5c2814..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-/*
- * Copyright (c) 2019 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <signal.h>
-
-#include <vnet/session/application_interface.h>
-
-typedef struct
-{
-  /* VNET_API_ERROR_FOO -> "Foo" hash table */
-  uword *error_string_by_error_number;
-} echo_common_main_t;
-
-echo_common_main_t echo_common_main;
-
-u8 *
-format_ip4_address (u8 * s, va_list * args)
-{
-  u8 *a = va_arg (*args, u8 *);
-  return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
-}
-
-u8 *
-format_ip6_address (u8 * s, va_list * args)
-{
-  ip6_address_t *a = va_arg (*args, ip6_address_t *);
-  u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
-
-  i_max_n_zero = ARRAY_LEN (a->as_u16);
-  max_n_zeros = 0;
-  i_first_zero = i_max_n_zero;
-  n_zeros = 0;
-  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
-    {
-      u32 is_zero = a->as_u16[i] == 0;
-      if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
-       {
-         i_first_zero = i;
-         n_zeros = 0;
-       }
-      n_zeros += is_zero;
-      if ((!is_zero && n_zeros > max_n_zeros)
-         || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
-       {
-         i_max_n_zero = i_first_zero;
-         max_n_zeros = n_zeros;
-         i_first_zero = ARRAY_LEN (a->as_u16);
-         n_zeros = 0;
-       }
-    }
-
-  last_double_colon = 0;
-  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
-    {
-      if (i == i_max_n_zero && max_n_zeros > 1)
-       {
-         s = format (s, "::");
-         i += max_n_zeros - 1;
-         last_double_colon = 1;
-       }
-      else
-       {
-         s = format (s, "%s%x",
-                     (last_double_colon || i == 0) ? "" : ":",
-                     clib_net_to_host_u16 (a->as_u16[i]));
-         last_double_colon = 0;
-       }
-    }
-
-  return s;
-}
-
-/* Format an IP46 address. */
-u8 *
-format_ip46_address (u8 * s, va_list * args)
-{
-  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
-  ip46_type_t type = va_arg (*args, ip46_type_t);
-  int is_ip4 = 1;
-
-  switch (type)
-    {
-    case IP46_TYPE_ANY:
-      is_ip4 = ip46_address_is_ip4 (ip46);
-      break;
-    case IP46_TYPE_IP4:
-      is_ip4 = 1;
-      break;
-    case IP46_TYPE_IP6:
-      is_ip4 = 0;
-      break;
-    }
-
-  return is_ip4 ?
-    format (s, "%U", format_ip4_address, &ip46->ip4) :
-    format (s, "%U", format_ip6_address, &ip46->ip6);
-}
-
-static uword
-unformat_data (unformat_input_t * input, va_list * args)
-{
-  u64 _a;
-  u64 *a = va_arg (*args, u64 *);
-  if (unformat (input, "%lluGb", &_a))
-    *a = _a << 30;
-  else if (unformat (input, "%lluMb", &_a))
-    *a = _a << 20;
-  else if (unformat (input, "%lluKb", &_a))
-    *a = _a << 10;
-  else if (unformat (input, "%llu", a))
-    ;
-  else
-    return 0;
-  return 1;
-}
-
-static u8 *
-format_api_error (u8 * s, va_list * args)
-{
-  echo_common_main_t *ecm = &echo_common_main;
-  i32 error = va_arg (*args, u32);
-  uword *p;
-
-  p = hash_get (ecm->error_string_by_error_number, -error);
-
-  if (p)
-    s = format (s, "%s", p[0]);
-  else
-    s = format (s, "%d", error);
-  return s;
-}
-
-static void
-init_error_string_table ()
-{
-  echo_common_main_t *ecm = &echo_common_main;
-  ecm->error_string_by_error_number = hash_create (0, sizeof (uword));
-
-#define _(n,v,s) hash_set (ecm->error_string_by_error_number, -v, s);
-  foreach_vnet_api_error;
-#undef _
-
-  hash_set (ecm->error_string_by_error_number, 99, "Misc");
-}
-
diff --git a/src/plugins/hs_apps/sapi/quic_echo.c b/src/plugins/hs_apps/sapi/quic_echo.c
deleted file mode 100644 (file)
index 312b703..0000000
+++ /dev/null
@@ -1,1916 +0,0 @@
-/*
- * Copyright (c) 2019 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <signal.h>
-
-#include <vlibmemory/api.h>
-
-#include <vpp/api/vpe_msg_enum.h>
-#include <svm/fifo_segment.h>
-#include <hs_apps/sapi/quic_echo.h>
-#include <hs_apps/sapi/echo_common.c>
-
-echo_main_t echo_main;
-
-/*
- *
- *  Format functions
- *
- */
-
-u8 *
-format_quic_echo_state (u8 * s, va_list * args)
-{
-  u32 state = va_arg (*args, u32);
-  if (state == STATE_START)
-    return format (s, "STATE_START");
-  if (state == STATE_ATTACHED)
-    return format (s, "STATE_ATTACHED");
-  if (state == STATE_LISTEN)
-    return format (s, "STATE_LISTEN");
-  if (state == STATE_READY)
-    return format (s, "STATE_READY");
-  if (state == STATE_DATA_DONE)
-    return format (s, "STATE_DATA_DONE");
-  if (state == STATE_DISCONNECTED)
-    return format (s, "STATE_DISCONNECTED");
-  if (state == STATE_DETACHED)
-    return format (s, "STATE_DETACHED");
-  else
-    return format (s, "unknown state");
-}
-
-static uword
-unformat_close (unformat_input_t * input, va_list * args)
-{
-  u8 *a = va_arg (*args, u8 *);
-  if (unformat (input, "Y"))
-    *a = ECHO_CLOSE_F_ACTIVE;
-  else if (unformat (input, "N"))
-    *a = ECHO_CLOSE_F_NONE;
-  else if (unformat (input, "W"))
-    *a = ECHO_CLOSE_F_PASSIVE;
-  else
-    return 0;
-  return 1;
-}
-
-static uword
-echo_unformat_timing_event (unformat_input_t * input, va_list * args)
-{
-  u8 *a = va_arg (*args, u8 *);
-  if (unformat (input, "start"))
-    *a = ECHO_EVT_START;
-  else if (unformat (input, "qconnected"))
-    *a = ECHO_EVT_LAST_QCONNECTED;
-  else if (unformat (input, "qconnect"))
-    *a = ECHO_EVT_FIRST_QCONNECT;
-  else if (unformat (input, "sconnected"))
-    *a = ECHO_EVT_LAST_SCONNECTED;
-  else if (unformat (input, "sconnect"))
-    *a = ECHO_EVT_FIRST_SCONNECT;
-  else if (unformat (input, "lastbyte"))
-    *a = ECHO_EVT_LAST_BYTE;
-  else if (unformat (input, "exit"))
-    *a = ECHO_EVT_EXIT;
-  else
-    return 0;
-  return 1;
-}
-
-u8 *
-echo_format_timing_event (u8 * s, va_list * args)
-{
-  u32 timing_event = va_arg (*args, u32);
-  if (timing_event == ECHO_EVT_START)
-    return format (s, "start");
-  if (timing_event == ECHO_EVT_FIRST_QCONNECT)
-    return format (s, "qconnect");
-  if (timing_event == ECHO_EVT_LAST_QCONNECTED)
-    return format (s, "qconnected");
-  if (timing_event == ECHO_EVT_FIRST_SCONNECT)
-    return format (s, "sconnect");
-  if (timing_event == ECHO_EVT_LAST_SCONNECTED)
-    return format (s, "sconnected");
-  if (timing_event == ECHO_EVT_LAST_BYTE)
-    return format (s, "lastbyte");
-  if (timing_event == ECHO_EVT_EXIT)
-    return format (s, "exit");
-  else
-    return format (s, "unknown timing event");
-}
-
-/*
- *
- *  End of format functions
- *
- */
-
-static void
-echo_session_prealloc (echo_main_t * em)
-{
-  /* We need to prealloc to avoid vec resize in threads */
-  echo_session_t *session;
-  int n_sessions = em->n_clients * (em->n_stream_clients + 1)
-    + em->i_am_master;
-  int i;
-  for (i = 0; i < n_sessions; i++)
-    {
-      pool_get (em->sessions, session);
-      clib_memset (session, 0, sizeof (*session));
-      session->session_index = session - em->sessions;
-      session->listener_index = SESSION_INVALID_INDEX;
-      session->session_state = QUIC_SESSION_STATE_INITIAL;
-    }
-}
-
-static echo_session_t *
-echo_session_new (echo_main_t * em)
-{
-  /* thread safe new prealloced session */
-  return pool_elt_at_index (em->sessions,
-                           clib_atomic_fetch_add (&em->nxt_available_sidx,
-                                                  1));
-}
-
-
-static int
-echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque)
-{
-  svm_msg_q_msg_t msg;
-  echo_rpc_msg_t *evt;
-  if (PREDICT_FALSE (svm_msg_q_lock (em->rpc_msq_queue)))
-    {
-      ECHO_LOG (1, "RPC lock failed");
-      return -1;
-    }
-  if (PREDICT_FALSE (svm_msg_q_ring_is_full (em->rpc_msq_queue, 0)))
-    {
-      svm_msg_q_unlock (em->rpc_msq_queue);
-      ECHO_LOG (1, "RPC ring is full");
-      return -2;
-    }
-  msg = svm_msg_q_alloc_msg_w_ring (em->rpc_msq_queue, 0);
-  if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (&msg)))
-    {
-      ECHO_LOG (1, "RPC msg is invalid");
-      svm_msg_q_unlock (em->rpc_msq_queue);
-      return -2;
-    }
-  evt = (echo_rpc_msg_t *) svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
-  evt->arg = arg;
-  evt->opaque = opaque;
-  evt->fp = fp;
-
-  svm_msg_q_add_and_unlock (em->rpc_msq_queue, &msg);
-  return 0;
-}
-
-static inline void
-echo_segment_handle_add_del (echo_main_t * em, u64 segment_handle, u8 add)
-{
-  clib_spinlock_lock (&em->segment_handles_lock);
-  if (add)
-    hash_set (em->shared_segment_handles, segment_handle, 1);
-  else
-    hash_unset (em->shared_segment_handles, segment_handle);
-  clib_spinlock_unlock (&em->segment_handles_lock);
-}
-
-static inline void
-echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid)
-{
-  clib_spinlock_lock (&em->sid_vpp_handles_lock);
-  if (sid == SESSION_INVALID_INDEX)
-    hash_unset (em->session_index_by_vpp_handles, handle);
-  else
-    hash_set (em->session_index_by_vpp_handles, handle, sid);
-  clib_spinlock_unlock (&em->sid_vpp_handles_lock);
-}
-
-static inline echo_session_t *
-echo_get_session_from_handle (echo_main_t * em, u64 handle)
-{
-  uword *p;
-  clib_spinlock_lock (&em->sid_vpp_handles_lock);
-  p = hash_get (em->session_index_by_vpp_handles, handle);
-  clib_spinlock_unlock (&em->sid_vpp_handles_lock);
-  if (!p)
-    {
-      ECHO_FAIL ("unknown handle 0x%lx", handle);
-      return 0;
-    }
-  return pool_elt_at_index (em->sessions, p[0]);
-}
-
-/*
- *
- *  Session API Calls
- *
- */
-
-void
-application_send_attach (echo_main_t * em)
-{
-  vl_api_application_attach_t *bmp;
-  vl_api_application_tls_cert_add_t *cert_mp;
-  vl_api_application_tls_key_add_t *key_mp;
-
-  bmp = vl_msg_api_alloc (sizeof (*bmp));
-  clib_memset (bmp, 0, sizeof (*bmp));
-
-  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;
-  bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
-  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;
-  bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
-  bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
-  bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
-  if (em->appns_id)
-    {
-      bmp->namespace_id_len = vec_len (em->appns_id);
-      clib_memcpy_fast (bmp->namespace_id, em->appns_id,
-                       bmp->namespace_id_len);
-      bmp->options[APP_OPTIONS_FLAGS] |= em->appns_flags;
-      bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = em->appns_secret;
-    }
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
-
-  cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
-  clib_memset (cert_mp, 0, sizeof (*cert_mp));
-  cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
-  cert_mp->client_index = em->my_client_index;
-  cert_mp->context = ntohl (0xfeedface);
-  cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
-  clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
-
-  key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
-  clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
-  key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
-  key_mp->client_index = em->my_client_index;
-  key_mp->context = ntohl (0xfeedface);
-  key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
-  clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
-}
-
-void
-application_detach (echo_main_t * em)
-{
-  vl_api_application_detach_t *bmp;
-  bmp = vl_msg_api_alloc (sizeof (*bmp));
-  clib_memset (bmp, 0, sizeof (*bmp));
-
-  bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
-  bmp->client_index = em->my_client_index;
-  bmp->context = ntohl (0xfeedface);
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
-}
-
-static void
-server_send_listen (echo_main_t * em)
-{
-  vl_api_bind_uri_t *bmp;
-  bmp = vl_msg_api_alloc (sizeof (*bmp));
-  clib_memset (bmp, 0, sizeof (*bmp));
-
-  bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
-  bmp->client_index = em->my_client_index;
-  bmp->context = ntohl (0xfeedface);
-  memcpy (bmp->uri, em->uri, vec_len (em->uri));
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
-}
-
-static void
-server_send_unbind (echo_main_t * em)
-{
-  vl_api_unbind_uri_t *ump;
-
-  ump = vl_msg_api_alloc (sizeof (*ump));
-  clib_memset (ump, 0, sizeof (*ump));
-
-  ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
-  ump->client_index = em->my_client_index;
-  memcpy (ump->uri, em->uri, vec_len (em->uri));
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
-}
-
-static void
-echo_send_connect (u8 * uri, u32 opaque)
-{
-  echo_main_t *em = &echo_main;
-  vl_api_connect_uri_t *cmp;
-  cmp = vl_msg_api_alloc (sizeof (*cmp));
-  clib_memset (cmp, 0, sizeof (*cmp));
-  cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
-  cmp->client_index = em->my_client_index;
-  cmp->context = ntohl (opaque);
-  memcpy (cmp->uri, uri, vec_len (uri));
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
-}
-
-static void
-echo_disconnect_session (echo_session_t * s, u32 opaque)
-{
-  echo_main_t *em = &echo_main;
-  vl_api_disconnect_session_t *dmp;
-  dmp = vl_msg_api_alloc (sizeof (*dmp));
-  clib_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;
-  ECHO_LOG (1, "Disconnect session 0x%lx", dmp->handle);
-  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
-}
-
-/*
- *
- *  End Session API Calls
- *
- */
-
-static int
-wait_for_segment_allocation (u64 segment_handle)
-{
-  echo_main_t *em = &echo_main;
-  f64 timeout;
-  timeout = clib_time_now (&em->clib_time) + TIMEOUT;
-  uword *segment_present;
-  ECHO_LOG (1, "Waiting for segment 0x%lx...", segment_handle);
-  while (clib_time_now (&em->clib_time) < timeout)
-    {
-      clib_spinlock_lock (&em->segment_handles_lock);
-      segment_present = hash_get (em->shared_segment_handles, segment_handle);
-      clib_spinlock_unlock (&em->segment_handles_lock);
-      if (segment_present != 0)
-       return 0;
-      if (em->time_to_stop == 1)
-       return 0;
-    }
-  ECHO_LOG (1, "timeout wait_for_segment_allocation (0x%lx)", segment_handle);
-  return -1;
-}
-
-static void
-quic_echo_notify_event (echo_main_t * em, echo_test_evt_t e)
-{
-  if (em->timing.events_sent & e)
-    return;
-  if (em->timing.start_event == e)
-    em->timing.start_time = clib_time_now (&em->clib_time);
-  else if (em->timing.end_event == e)
-    em->timing.end_time = clib_time_now (&em->clib_time);
-  em->timing.events_sent |= e;
-}
-
-static void
-echo_assert_test_suceeded (echo_main_t * em)
-{
-  CHECK (em->n_stream_clients * em->n_clients * em->bytes_to_receive,
-        em->stats.rx_total, "Not enough data received");
-  CHECK (em->n_stream_clients * em->n_clients * em->bytes_to_send,
-        em->stats.tx_total, "Not enough data sent");
-  clib_spinlock_lock (&em->sid_vpp_handles_lock);
-  CHECK (0, hash_elts (em->session_index_by_vpp_handles),
-        "Some sessions are still open");
-  clib_spinlock_unlock (&em->sid_vpp_handles_lock);
-}
-
-always_inline void
-echo_session_dequeue_notify (echo_session_t * s)
-{
-  int rv;
-  if (!svm_fifo_set_event (s->rx_fifo))
-    return;
-  if ((rv =
-       app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
-                              SESSION_IO_EVT_RX, SVM_Q_WAIT)))
-    ECHO_FAIL ("app_send_io_evt_to_vpp errored %d", rv);
-  svm_fifo_clear_deq_ntf (s->rx_fifo);
-}
-
-static int
-ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
-{
-  fifo_segment_create_args_t _a, *a = &_a;
-  fifo_segment_main_t *sm = &echo_main.segment_main;
-  int rv;
-
-  clib_memset (a, 0, sizeof (*a));
-  a->segment_name = (char *) name;
-  a->segment_type = type;
-
-  if (type == SSVM_SEGMENT_MEMFD)
-    a->memfd_fd = fd;
-
-  if ((rv = fifo_segment_attach (sm, a)))
-    return rv;
-  vec_reset_length (a->new_segment_indices);
-  return 0;
-}
-
-static void
-stop_signal (int signum)
-{
-  echo_main_t *em = &echo_main;
-  em->time_to_stop = 1;
-}
-
-int
-connect_to_vpp (char *name)
-{
-  echo_main_t *em = &echo_main;
-  api_main_t *am = &api_main;
-
-  if (em->use_sock_api)
-    {
-      if (vl_socket_client_connect ((char *) em->socket_name, name,
-                                   0 /* default rx, tx buffer */ ))
-       {
-         ECHO_FAIL ("socket connect failed");
-         return -1;
-       }
-
-      if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
-       {
-         ECHO_FAIL ("init shm api failed");
-         return -1;
-       }
-    }
-  else
-    {
-      if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
-       {
-         ECHO_FAIL ("shmem connect failed");
-         return -1;
-       }
-    }
-  em->vl_input_queue = am->shmem_hdr->vl_input_queue;
-  em->my_client_index = am->my_client_index;
-  return 0;
-}
-
-static void
-session_print_stats (echo_main_t * em, echo_session_t * session)
-{
-  f64 deltat = clib_time_now (&em->clib_time) - session->start;
-  ECHO_LOG (0, "Session 0x%x done in %.6fs RX[%.4f] TX[%.4f] Gbit/s\n",
-           session->vpp_session_handle, deltat,
-           (session->bytes_received * 8.0) / deltat / 1e9,
-           (session->bytes_sent * 8.0) / deltat / 1e9);
-}
-
-static void
-echo_event_didnt_happen (u8 e)
-{
-  echo_main_t *em = &echo_main;
-  u8 *s = format (0, "%U", echo_format_timing_event, e);
-  ECHO_LOG (0, "Expected event %s to happend, which did not", s);
-  em->has_failed = 1;
-}
-
-static void
-print_global_json_stats (echo_main_t * em)
-{
-  if (!(em->timing.events_sent & em->timing.start_event))
-    return echo_event_didnt_happen (em->timing.start_event);
-  if (!(em->timing.events_sent & em->timing.end_event))
-    return echo_event_didnt_happen (em->timing.end_event);
-  f64 deltat = em->timing.end_time - em->timing.start_time;
-  u8 *start_evt =
-    format (0, "%U", echo_format_timing_event, em->timing.start_event);
-  u8 *end_evt =
-    format (0, "%U", echo_format_timing_event, em->timing.end_event);
-  fformat (stdout, "{\n");
-  fformat (stdout, "\"time\": \"%.9f\",\n", deltat);
-  fformat (stdout, "\"start_evt\": \"%s\",\n", start_evt);
-  fformat (stdout, "\"end_evt\": \"%s\",\n", end_evt);
-  fformat (stdout, "\"rx_data\": %lld,\n", em->stats.rx_total);
-  fformat (stdout, "\"tx_rx\": %lld,\n", em->stats.tx_total);
-  fformat (stdout, "\"closing\": {\n");
-  fformat (stdout, "  \"reset\": { \"q\": %d, \"s\": %d },\n",
-          em->stats.reset_count.q, em->stats.reset_count.s);
-  fformat (stdout, "  \"close\": { \"q\": %d, \"s\": %d },\n",
-          em->stats.close_count.q, em->stats.close_count.s);
-  fformat (stdout, "  \"active\": { \"q\": %d, \"s\": %d },\n",
-          em->stats.active_count.q, em->stats.active_count.s);
-  fformat (stdout, "  \"clean\": { \"q\": %d, \"s\": %d }\n",
-          em->stats.clean_count.q, em->stats.clean_count.s);
-  fformat (stdout, "}\n");
-  fformat (stdout, "}\n");
-}
-
-static void
-print_global_stats (echo_main_t * em)
-{
-  u8 *s;
-  if (!(em->timing.events_sent & em->timing.start_event))
-    return echo_event_didnt_happen (em->timing.start_event);
-  if (!(em->timing.events_sent & em->timing.end_event))
-    return echo_event_didnt_happen (em->timing.end_event);
-  f64 deltat = em->timing.end_time - em->timing.start_time;
-  s = format (0, "%U:%U",
-             echo_format_timing_event, em->timing.start_event,
-             echo_format_timing_event, em->timing.end_event);
-  fformat (stdout, "Timing %s\n", s);
-  fformat (stdout, "-------- TX --------\n");
-  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
-          em->stats.tx_total, em->stats.tx_total / (1ULL << 20),
-          em->stats.tx_total / (1ULL << 30), deltat);
-  fformat (stdout, "%.4f Gbit/second\n",
-          (em->stats.tx_total * 8.0) / deltat / 1e9);
-  fformat (stdout, "-------- RX --------\n");
-  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
-          em->stats.rx_total, em->stats.rx_total / (1ULL << 20),
-          em->stats.rx_total / (1ULL << 30), deltat);
-  fformat (stdout, "%.4f Gbit/second\n",
-          (em->stats.rx_total * 8.0) / deltat / 1e9);
-  fformat (stdout, "--------------------\n");
-  fformat (stdout, "Received close on %dQ %dS\n", em->stats.close_count.q,
-          em->stats.close_count.s);
-  fformat (stdout, "Received reset on %dQ %dS\n", em->stats.reset_count.q,
-          em->stats.reset_count.s);
-  fformat (stdout, "Sent close on     %dQ %dS\n", em->stats.active_count.q,
-          em->stats.active_count.s);
-  fformat (stdout, "Discarded         %dQ %dS\n", em->stats.clean_count.q,
-          em->stats.clean_count.s);
-}
-
-static void
-echo_free_sessions (echo_main_t * em)
-{
-  /* Free marked sessions */
-  echo_session_t *s;
-  u32 *session_indexes = 0, *session_index;
-
-  /* *INDENT-OFF* */
-  pool_foreach (s, em->sessions,
-  ({
-    if (s->session_state == QUIC_SESSION_STATE_CLOSED)
-      vec_add1 (session_indexes, s->session_index);}
-  ));
-  /* *INDENT-ON* */
-  vec_foreach (session_index, session_indexes)
-  {
-    /* Free session */
-    s = pool_elt_at_index (em->sessions, *session_index);
-    echo_session_handle_add_del (em, s->vpp_session_handle,
-                                SESSION_INVALID_INDEX);
-    pool_put (em->sessions, s);
-    clib_memset (s, 0xfe, sizeof (*s));
-  }
-}
-
-static void
-echo_cleanup_session (echo_main_t * em, echo_session_t * s)
-{
-  echo_session_t *ls;
-  ASSERT (s->session_state < QUIC_SESSION_STATE_CLOSED);
-  if (s->session_type == QUIC_SESSION_TYPE_QUIC)
-    {
-      clib_atomic_sub_fetch (&em->n_quic_clients_connected, 1);
-    }
-  else if (s->session_type == QUIC_SESSION_TYPE_STREAM)
-    {
-      ls = pool_elt_at_index (em->sessions, s->listener_index);
-      ASSERT (ls->session_type == QUIC_SESSION_TYPE_QUIC);
-      if (!clib_atomic_sub_fetch (&ls->accepted_session_count, 1))
-       {
-         if (em->send_quic_disconnects == ECHO_CLOSE_F_ACTIVE)
-           {
-             echo_send_rpc (em, echo_disconnect_session, (void *) ls, 0);
-             em->stats.active_count.q++;
-           }
-         else if (em->send_quic_disconnects == ECHO_CLOSE_F_NONE)
-           {
-             echo_cleanup_session (em, ls);
-             em->stats.clean_count.q++;
-           }
-       }
-      clib_atomic_sub_fetch (&em->n_clients_connected, 1);
-    }
-  ECHO_LOG (1, "Cleanup sessions (still %uQ %uS)",
-           em->n_quic_clients_connected, em->n_clients_connected);
-  s->session_state = QUIC_SESSION_STATE_CLOSED;
-}
-
-static void
-echo_initiate_qsession_close_no_stream (echo_main_t * em)
-{
-  ECHO_LOG (1, "Closing Qsessions");
-  /* Close Quic session without streams */
-  echo_session_t *s;
-
-  /* *INDENT-OFF* */
-  pool_foreach (s, em->sessions,
-  ({
-    if (s->session_type == QUIC_SESSION_TYPE_QUIC)
-      {
-       ECHO_LOG (1,"ACTIVE close 0x%lx", s->vpp_session_handle);
-        if (em->send_quic_disconnects == ECHO_CLOSE_F_ACTIVE)
-          {
-           echo_send_rpc (em, echo_disconnect_session, (void *) s, 0);
-           em->stats.active_count.q++;
-          }
-       else if (em->send_quic_disconnects == ECHO_CLOSE_F_NONE)
-         {
-           echo_cleanup_session (em, s);
-           em->stats.clean_count.q++;
-         }
-      }
-  }));
-  /* *INDENT-ON* */
-  em->state = STATE_DATA_DONE;
-}
-
-static void
-test_recv_bytes (echo_main_t * em, echo_session_t * s, u8 * rx_buf,
-                u32 n_read)
-{
-  u32 i;
-  u8 expected;
-  for (i = 0; i < n_read; i++)
-    {
-      expected = (s->bytes_received + i) & 0xff;
-      if (rx_buf[i] == expected || em->max_test_msg > 0)
-       continue;
-      ECHO_LOG (0, "Session 0x%lx byte %lld was 0x%x expected 0x%x",
-               s->vpp_session_handle, s->bytes_received + i, rx_buf[i],
-               expected);
-      em->max_test_msg--;
-      if (em->max_test_msg == 0)
-       ECHO_LOG (0, "Too many errors, hiding next ones");
-      if (em->test_return_packets == RETURN_PACKETS_ASSERT)
-       ECHO_FAIL ("test-bytes errored");
-    }
-}
-
-static int
-recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
-{
-  int n_read;
-  n_read = app_recv_stream ((app_session_t *) s, rx_buf, vec_len (rx_buf));
-  if (n_read <= 0)
-    return 0;
-  if (svm_fifo_needs_deq_ntf (s->rx_fifo, n_read))
-    echo_session_dequeue_notify (s);
-
-  if (em->test_return_packets)
-    test_recv_bytes (em, s, rx_buf, n_read);
-
-  s->bytes_received += n_read;
-  s->bytes_to_receive -= n_read;
-  return n_read;
-}
-
-static int
-send_data_chunk (echo_session_t * s, u8 * tx_buf, int offset, int len)
-{
-  int n_sent;
-  int bytes_this_chunk = clib_min (s->bytes_to_send, len - offset);
-  if (!bytes_this_chunk)
-    return 0;
-  n_sent = app_send_stream ((app_session_t *) s, tx_buf + offset,
-                           bytes_this_chunk, 0);
-  if (n_sent < 0)
-    return 0;
-  s->bytes_to_send -= n_sent;
-  s->bytes_sent += n_sent;
-  return n_sent;
-}
-
-static int
-mirror_data_chunk (echo_main_t * em, echo_session_t * s, u8 * tx_buf, u64 len)
-{
-  u64 n_sent = 0;
-  while (n_sent < len && !em->time_to_stop)
-    n_sent += send_data_chunk (s, tx_buf, n_sent, len);
-  return n_sent;
-}
-
-static void
-echo_update_count_on_session_close (echo_main_t * em, echo_session_t * s)
-{
-
-  ECHO_LOG (1, "[%lu/%lu] -> S(%x) -> [%lu/%lu]",
-           s->bytes_received, s->bytes_received + s->bytes_to_receive,
-           s->session_index, s->bytes_sent,
-           s->bytes_sent + s->bytes_to_send);
-  clib_atomic_fetch_add (&em->stats.tx_total, s->bytes_sent);
-  clib_atomic_fetch_add (&em->stats.rx_total, s->bytes_received);
-
-  if (PREDICT_FALSE (em->stats.rx_total ==
-                    em->n_clients * em->n_stream_clients *
-                    em->bytes_to_receive))
-    quic_echo_notify_event (em, ECHO_EVT_LAST_BYTE);
-}
-
-static inline void
-echo_check_closed_listener (echo_main_t * em, echo_session_t * s)
-{
-  echo_session_t *ls;
-  /* if parent has died, terminate gracefully */
-  ls = pool_elt_at_index (em->sessions, s->listener_index);
-  if (ls->session_state < QUIC_SESSION_STATE_CLOSING)
-    return;
-  ECHO_LOG (2, "Session 0%lx died, close child 0x%lx", ls->vpp_session_handle,
-           s->vpp_session_handle);
-  clib_atomic_sub_fetch (&em->n_clients_connected, 1);
-  em->stats.clean_count.s++;
-  echo_update_count_on_session_close (em, s);
-  s->session_state = QUIC_SESSION_STATE_CLOSED;
-}
-
-/*
- * Rx/Tx polling thread per connection
- */
-static void
-echo_handle_data (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
-{
-  int n_read, n_sent = 0;
-
-  n_read = recv_data_chunk (em, s, rx_buf);
-  if (em->data_source == ECHO_TEST_DATA_SOURCE)
-    n_sent = send_data_chunk (s, em->connect_test_data,
-                             s->bytes_sent % em->tx_buf_size,
-                             em->tx_buf_size);
-  else if (em->data_source == ECHO_RX_DATA_SOURCE)
-    n_sent = mirror_data_chunk (em, s, rx_buf, n_read);
-  if (!s->bytes_to_send && !s->bytes_to_receive)
-    {
-      /* Session is done, need to close */
-      if (s->session_state == QUIC_SESSION_STATE_AWAIT_DATA)
-       s->session_state = QUIC_SESSION_STATE_CLOSING;
-      else
-       {
-         s->session_state = QUIC_SESSION_STATE_AWAIT_CLOSING;
-         if (em->send_stream_disconnects == ECHO_CLOSE_F_ACTIVE)
-           {
-             echo_send_rpc (em, echo_disconnect_session, (void *) s, 0);
-             em->stats.close_count.s++;
-           }
-         else if (em->send_stream_disconnects == ECHO_CLOSE_F_NONE)
-           {
-             s->session_state = QUIC_SESSION_STATE_CLOSING;
-             em->stats.clean_count.s++;
-           }
-       }
-      return;
-    }
-
-  /* Check for idle clients */
-  if (em->log_lvl > 1)
-    {
-      if (n_sent || n_read)
-       s->idle_cycles = 0;
-      else if (s->idle_cycles++ == 1e7)
-       {
-         s->idle_cycles = 0;
-         ECHO_LOG (1, "Idle client TX:%dB RX:%dB", s->bytes_to_send,
-                   s->bytes_to_receive);
-         ECHO_LOG (1, "Idle FIFOs TX:%dB RX:%dB",
-                   svm_fifo_max_dequeue (s->tx_fifo),
-                   svm_fifo_max_dequeue (s->rx_fifo));
-         ECHO_LOG (1, "Session 0x%lx state %u", s->vpp_session_handle,
-                   s->session_state);
-       }
-    }
-}
-
-static void *
-echo_data_thread_fn (void *arg)
-{
-  clib_mem_set_thread_index ();
-  echo_main_t *em = &echo_main;
-  u32 N = em->n_clients * em->n_stream_clients;
-  u32 n = (N + em->n_rx_threads - 1) / em->n_rx_threads;
-  u32 idx = (u64) arg;
-  u32 thread_n_sessions = clib_min (n, N - n * idx);
-
-  u32 i = 0;
-  u32 n_closed_sessions = 0;
-  u32 session_index;
-  u8 *rx_buf = 0;
-  echo_session_t *s;
-  vec_validate (rx_buf, em->rx_buf_size);
-
-  for (i = 0; !em->time_to_stop; i = (i + 1) % thread_n_sessions)
-    {
-      n_closed_sessions = i == 0 ? 0 : n_closed_sessions;
-      session_index = em->data_thread_args[n * idx + i];
-      if (session_index == SESSION_INVALID_INDEX)
-       continue;
-      s = pool_elt_at_index (em->sessions, session_index);
-      switch (s->session_state)
-       {
-       case QUIC_SESSION_STATE_READY:
-       case QUIC_SESSION_STATE_AWAIT_DATA:
-         echo_handle_data (em, s, rx_buf);
-         echo_check_closed_listener (em, s);
-         break;
-       case QUIC_SESSION_STATE_AWAIT_CLOSING:
-         echo_check_closed_listener (em, s);
-         break;
-       case QUIC_SESSION_STATE_CLOSING:
-         echo_update_count_on_session_close (em, s);
-         echo_cleanup_session (em, s);
-         break;
-       case QUIC_SESSION_STATE_CLOSED:
-         n_closed_sessions++;
-         break;
-       }
-      if (n_closed_sessions == thread_n_sessions)
-       break;
-    }
-  pthread_exit (0);
-}
-
-static void
-session_bound_handler (session_bound_msg_t * mp)
-{
-  echo_main_t *em = &echo_main;
-  echo_session_t *listen_session;
-  if (mp->retval)
-    {
-      ECHO_FAIL ("bind failed: %U", format_api_error,
-                clib_net_to_host_u32 (mp->retval));
-      return;
-    }
-  ECHO_LOG (0, "listening on %U:%u", format_ip46_address, mp->lcl_ip,
-           mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
-           clib_net_to_host_u16 (mp->lcl_port));
-
-  /* Allocate local session and set it up */
-  listen_session = echo_session_new (em);
-  listen_session->session_type = QUIC_SESSION_TYPE_LISTEN;
-  echo_session_handle_add_del (em, mp->handle, listen_session->session_index);
-  em->state = STATE_LISTEN;
-  em->listen_session_index = listen_session->session_index;
-}
-
-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;
-  echo_session_t *session, *ls;
-  /* Allocate local session and set it up */
-  session = echo_session_new (em);
-
-  if (wait_for_segment_allocation (mp->segment_handle))
-    {
-      ECHO_FAIL ("wait_for_segment_allocation errored");
-      return;
-    }
-
-  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
-  rx_fifo->client_session_index = session->session_index;
-  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
-  tx_fifo->client_session_index = session->session_index;
-
-  session->rx_fifo = rx_fifo;
-  session->tx_fifo = tx_fifo;
-  session->vpp_session_handle = mp->handle;
-  session->start = clib_time_now (&em->clib_time);
-  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
-                                        svm_msg_q_t *);
-  if (!(ls = echo_get_session_from_handle (em, mp->listener_handle)))
-    return;
-  session->listener_index = ls->session_index;
-
-  /* Add it to lookup table */
-  ECHO_LOG (1, "Accepted session 0x%lx -> 0x%lx", mp->handle,
-           mp->listener_handle);
-  echo_session_handle_add_del (em, mp->handle, session->session_index);
-
-  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);
-
-  if (ls->session_type == QUIC_SESSION_TYPE_LISTEN)
-    {
-      quic_echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
-      session->session_type = QUIC_SESSION_TYPE_QUIC;
-      session->accepted_session_count = 0;
-      if (em->cb_vft.quic_accepted_cb)
-       em->cb_vft.quic_accepted_cb (mp, session->session_index);
-      clib_atomic_fetch_add (&em->n_quic_clients_connected, 1);
-    }
-  else
-    {
-      session->session_type = QUIC_SESSION_TYPE_STREAM;
-      quic_echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
-      clib_atomic_fetch_add (&ls->accepted_session_count, 1);
-      if (em->i_am_master && em->cb_vft.server_stream_accepted_cb)
-       em->cb_vft.server_stream_accepted_cb (mp, session->session_index);
-      if (!em->i_am_master && em->cb_vft.client_stream_accepted_cb)
-       em->cb_vft.client_stream_accepted_cb (mp, session->session_index);
-      clib_atomic_fetch_add (&em->n_clients_connected, 1);
-    }
-
-  if (em->n_clients_connected == em->n_clients * em->n_stream_clients
-      && em->n_clients_connected != 0)
-    quic_echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
-  if (em->n_quic_clients_connected == em->n_clients
-      && em->state < STATE_READY)
-    {
-      quic_echo_notify_event (em, ECHO_EVT_LAST_QCONNECTED);
-      em->state = STATE_READY;
-      if (em->n_stream_clients == 0)
-       echo_initiate_qsession_close_no_stream (em);
-    }
-}
-
-static void
-session_connected_handler (session_connected_msg_t * mp)
-{
-  echo_main_t *em = &echo_main;
-  echo_session_t *session, *listen_session;
-  u32 listener_index = htonl (mp->context);
-  svm_fifo_t *rx_fifo, *tx_fifo;
-
-  if (mp->retval)
-    {
-      ECHO_FAIL ("connection failed with code: %U", format_api_error,
-                clib_net_to_host_u32 (mp->retval));
-      return;
-    }
-
-  session = echo_session_new (em);
-  if (wait_for_segment_allocation (mp->segment_handle))
-    {
-      ECHO_FAIL ("wait_for_segment_allocation errored");
-      return;
-    }
-
-  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
-  rx_fifo->client_session_index = session->session_index;
-  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
-  tx_fifo->client_session_index = session->session_index;
-
-  session->rx_fifo = rx_fifo;
-  session->tx_fifo = tx_fifo;
-  session->vpp_session_handle = mp->handle;
-  session->start = clib_time_now (&em->clib_time);
-  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
-                                        svm_msg_q_t *);
-
-  echo_session_handle_add_del (em, mp->handle, session->session_index);
-
-  if (listener_index == SESSION_INVALID_INDEX)
-    {
-      ECHO_LOG (1, "Connected session 0x%lx -> URI", mp->handle);
-      session->session_type = QUIC_SESSION_TYPE_QUIC;
-      session->accepted_session_count = 0;
-      if (em->cb_vft.quic_connected_cb)
-       em->cb_vft.quic_connected_cb (mp, session->session_index);
-      clib_atomic_fetch_add (&em->n_quic_clients_connected, 1);
-    }
-  else
-    {
-      listen_session = pool_elt_at_index (em->sessions, listener_index);
-      session->listener_index = listener_index;
-      ECHO_LOG (1, "Connected session 0x%lx -> 0x%lx", mp->handle,
-               listen_session->vpp_session_handle);
-      session->session_type = QUIC_SESSION_TYPE_STREAM;
-      clib_atomic_fetch_add (&listen_session->accepted_session_count, 1);
-      if (em->i_am_master && em->cb_vft.server_stream_connected_cb)
-       em->cb_vft.server_stream_connected_cb (mp, session->session_index);
-      if (!em->i_am_master && em->cb_vft.client_stream_connected_cb)
-       em->cb_vft.client_stream_connected_cb (mp, session->session_index);
-      clib_atomic_fetch_add (&em->n_clients_connected, 1);
-    }
-
-  if (em->n_clients_connected == em->n_clients * em->n_stream_clients
-      && em->n_clients_connected != 0)
-    quic_echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
-  if (em->n_quic_clients_connected == em->n_clients
-      && em->state < STATE_READY)
-    {
-      quic_echo_notify_event (em, ECHO_EVT_LAST_QCONNECTED);
-      em->state = STATE_READY;
-      if (em->n_stream_clients == 0)
-       echo_initiate_qsession_close_no_stream (em);
-    }
-}
-
-/*
- *
- *  ECHO Callback definitions
- *
- */
-
-
-static void
-echo_on_connected_connect (session_connected_msg_t * mp, u32 session_index)
-{
-  echo_main_t *em = &echo_main;
-  u8 *uri = format (0, "QUIC://session/%lu", mp->handle);
-  u64 i;
-
-  quic_echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
-  for (i = 0; i < em->n_stream_clients; i++)
-    echo_send_rpc (em, echo_send_connect, (void *) uri, session_index);
-
-  ECHO_LOG (0, "Qsession 0x%llx connected to %U:%d",
-           mp->handle, format_ip46_address, &mp->lcl.ip,
-           mp->lcl.is_ip4, clib_net_to_host_u16 (mp->lcl.port));
-}
-
-static void
-echo_on_connected_send (session_connected_msg_t * mp, u32 session_index)
-{
-  echo_main_t *em = &echo_main;
-  echo_session_t *session;
-
-  session = pool_elt_at_index (em->sessions, session_index);
-  session->bytes_to_send = em->bytes_to_send;
-  session->bytes_to_receive = em->bytes_to_receive;
-  session->session_state = QUIC_SESSION_STATE_READY;
-  em->data_thread_args[em->n_clients_connected] = session->session_index;
-}
-
-static void
-echo_on_connected_error (session_connected_msg_t * mp, u32 session_index)
-{
-  ECHO_FAIL ("Got a wrong connected on session %u [%lx]", session_index,
-            mp->handle);
-}
-
-static void
-echo_on_accept_recv (session_accepted_msg_t * mp, u32 session_index)
-{
-  echo_main_t *em = &echo_main;
-  echo_session_t *session;
-
-  session = pool_elt_at_index (em->sessions, session_index);
-  session->bytes_to_send = em->bytes_to_send;
-  session->bytes_to_receive = em->bytes_to_receive;
-  em->data_thread_args[em->n_clients_connected] = session->session_index;
-  session->session_state = QUIC_SESSION_STATE_READY;
-}
-
-static void
-echo_on_accept_connect (session_accepted_msg_t * mp, u32 session_index)
-{
-  echo_main_t *em = &echo_main;
-  ECHO_LOG (1, "Accept on QSession 0x%lx %u", mp->handle);
-  u8 *uri = format (0, "QUIC://session/%lu", mp->handle);
-  u32 i;
-
-  quic_echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
-  for (i = 0; i < em->n_stream_clients; i++)
-    echo_send_rpc (em, echo_send_connect, (void *) uri, session_index);
-}
-
-static void
-echo_on_accept_error (session_accepted_msg_t * mp, u32 session_index)
-{
-  ECHO_FAIL ("Got a wrong accept on session %u [%lx]", session_index,
-            mp->handle);
-}
-
-static void
-echo_on_accept_log_ip (session_accepted_msg_t * mp, u32 session_index)
-{
-  u8 *ip_str;
-  ip_str = format (0, "%U", format_ip46_address, &mp->rmt.ip, mp->rmt.is_ip4);
-  ECHO_LOG (0, "Accepted session from: %s:%d", ip_str,
-           clib_net_to_host_u16 (mp->rmt.port));
-
-}
-
-static const quic_echo_cb_vft_t default_cb_vft = {
-  /* Qsessions */
-  .quic_accepted_cb = echo_on_accept_log_ip,
-  .quic_connected_cb = echo_on_connected_connect,
-  /* client initiated streams */
-  .server_stream_accepted_cb = echo_on_accept_recv,
-  .client_stream_connected_cb = echo_on_connected_send,
-  /* server initiated streams */
-  .client_stream_accepted_cb = echo_on_accept_error,
-  .server_stream_connected_cb = echo_on_connected_error,
-};
-
-static const quic_echo_cb_vft_t server_stream_cb_vft = {
-  /* Qsessions */
-  .quic_accepted_cb = echo_on_accept_connect,
-  .quic_connected_cb = NULL,
-  /* client initiated streams */
-  .server_stream_accepted_cb = echo_on_accept_error,
-  .client_stream_connected_cb = echo_on_connected_error,
-  /* server initiated streams */
-  .client_stream_accepted_cb = echo_on_accept_recv,
-  .server_stream_connected_cb = echo_on_connected_send,
-};
-
-static uword
-echo_unformat_quic_setup_vft (unformat_input_t * input, va_list * args)
-{
-  echo_main_t *em = &echo_main;
-  if (unformat (input, "serverstream"))
-    em->cb_vft = server_stream_cb_vft;
-  else if (unformat (input, "default"))
-    ;
-  else
-    return 0;
-  return 1;
-}
-
-/*
- *
- *  End of ECHO callback definitions
- *
- */
-
-static void
-session_disconnected_handler (session_disconnected_msg_t * mp)
-{
-  app_session_evt_t _app_evt, *app_evt = &_app_evt;
-  session_disconnected_reply_msg_t *rmp;
-  echo_main_t *em = &echo_main;
-  echo_session_t *s;
-  int rv = 0;
-  ECHO_LOG (1, "passive close session 0x%lx", mp->handle);
-  if (!(s = echo_get_session_from_handle (em, mp->handle)))
-    return;
-
-  app_alloc_ctrl_evt_to_vpp (s->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 (s->vpp_evt_q, app_evt);
-
-  if (s->session_type == QUIC_SESSION_TYPE_STREAM)
-    {
-      session_print_stats (em, s);
-      if (s->bytes_to_receive || s->bytes_to_send)
-       s->session_state = QUIC_SESSION_STATE_AWAIT_DATA;
-      else
-       s->session_state = QUIC_SESSION_STATE_CLOSING;
-      em->stats.close_count.s++;
-    }
-  else
-    {
-      echo_cleanup_session (em, s);    /* We can clean Q/Lsessions right away */
-      em->stats.close_count.q++;
-    }
-}
-
-static void
-session_reset_handler (session_reset_msg_t * mp)
-{
-  app_session_evt_t _app_evt, *app_evt = &_app_evt;
-  echo_main_t *em = &echo_main;
-  session_reset_reply_msg_t *rmp;
-  echo_session_t *s = 0;
-  int rv = 0;
-
-  ECHO_LOG (1, "Reset session 0x%lx", mp->handle);
-  if (!(s = echo_get_session_from_handle (em, mp->handle)))
-    return;
-  if (s->session_type == QUIC_SESSION_TYPE_STREAM)
-    {
-      em->stats.reset_count.s++;
-      s->session_state = QUIC_SESSION_STATE_CLOSING;
-    }
-  else
-    {
-      em->stats.reset_count.q++;
-      echo_cleanup_session (em, s);    /* We can clean Q/Lsessions right away */
-    }
-
-  app_alloc_ctrl_evt_to_vpp (s->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 (s->vpp_evt_q, app_evt);
-}
-
-static void
-handle_mq_event (session_event_t * e)
-{
-  switch (e->event_type)
-    {
-    case SESSION_CTRL_EVT_BOUND:
-      session_bound_handler ((session_bound_msg_t *) e->data);
-      break;
-    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;
-    case SESSION_IO_EVT_RX:
-      break;
-    default:
-      ECHO_LOG (0, "unhandled event %u", e->event_type);
-    }
-}
-
-static int
-wait_for_state_change (echo_main_t * em, connection_state_t state,
-                      f64 timeout)
-{
-  f64 end_time = clib_time_now (&em->clib_time) + timeout;
-  while (!timeout || clib_time_now (&em->clib_time) < end_time)
-    {
-      if (em->state == state)
-       return 0;
-      if (em->time_to_stop)
-       return 1;
-    }
-  ECHO_LOG (1, "timeout waiting for %U", format_quic_echo_state, state);
-  return -1;
-}
-
-static void
-echo_process_rpcs (echo_main_t * em)
-{
-  echo_rpc_msg_t *rpc;
-  svm_msg_q_msg_t msg;
-  while (em->state < STATE_DATA_DONE && !em->time_to_stop)
-    {
-      if (svm_msg_q_sub (em->rpc_msq_queue, &msg, SVM_Q_TIMEDWAIT, 1))
-       continue;
-      rpc = svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
-      ((echo_rpc_t) rpc->fp) (rpc->arg, rpc->opaque);
-      svm_msg_q_free_msg (em->rpc_msq_queue, &msg);
-    }
-}
-
-static void *
-echo_mq_thread_fn (void *arg)
-{
-  clib_mem_set_thread_index ();
-  echo_main_t *em = &echo_main;
-  session_event_t *e;
-  svm_msg_q_msg_t msg;
-  int rv;
-  wait_for_state_change (em, STATE_ATTACHED, 0);
-  if (em->state < STATE_ATTACHED || !em->our_event_queue)
-    {
-      ECHO_FAIL ("Application failed to attach");
-      pthread_exit (0);
-    }
-
-  ECHO_LOG (1, "Waiting for data on %u clients", em->n_clients_connected);
-  while (1)
-    {
-      if (!(rv = svm_msg_q_sub (em->our_event_queue,
-                               &msg, SVM_Q_TIMEDWAIT, 1)))
-       {
-         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 (rv == ETIMEDOUT
-         && (em->time_to_stop || em->state == STATE_DETACHED))
-       break;
-      if (!em->n_clients_connected && !em->n_quic_clients_connected &&
-         em->state == STATE_READY)
-       {
-         em->state = STATE_DATA_DONE;
-       }
-    }
-  pthread_exit (0);
-}
-
-static void
-clients_run (echo_main_t * em)
-{
-  u64 i;
-  quic_echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
-  for (i = 0; i < em->n_clients; i++)
-    echo_send_connect (em->uri, SESSION_INVALID_INDEX);
-  wait_for_state_change (em, STATE_READY, 0);
-  ECHO_LOG (1, "App is ready");
-  echo_process_rpcs (em);
-}
-
-static void
-server_run (echo_main_t * em)
-{
-  server_send_listen (em);
-  wait_for_state_change (em, STATE_READY, 0);
-  ECHO_LOG (1, "App is ready");
-  echo_process_rpcs (em);
-  /* Cleanup */
-  server_send_unbind (em);
-  if (wait_for_state_change (em, STATE_DISCONNECTED, TIMEOUT))
-    {
-      ECHO_FAIL ("Timeout waiting for state disconnected");
-      return;
-    }
-}
-
-/*
- *
- *  Session API handlers
- *
- */
-
-static void
-vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
-                                          mp)
-{
-  echo_main_t *em = &echo_main;
-  int *fds = 0, i;
-  u32 n_fds = 0;
-  u64 segment_handle;
-  segment_handle = clib_net_to_host_u64 (mp->segment_handle);
-  ECHO_LOG (1, "Attached returned app %u", htons (mp->app_index));
-
-  if (mp->retval)
-    {
-      ECHO_FAIL ("attach failed: %U", format_api_error,
-                clib_net_to_host_u32 (mp->retval));
-      return;
-    }
-
-  if (mp->segment_name_length == 0)
-    {
-      ECHO_FAIL ("segment_name_length zero");
-      return;
-    }
-
-  ASSERT (mp->app_event_queue_address);
-  em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
-                                         svm_msg_q_t *);
-
-  if (mp->n_fds)
-    {
-      vec_validate (fds, mp->n_fds);
-      if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
-       {
-         ECHO_FAIL ("vl_socket_client_recv_fd_msg failed");
-         goto failed;
-       }
-
-      if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
-       if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
-         {
-           ECHO_FAIL ("svm_fifo_segment_attach failed");
-           goto failed;
-         }
-
-      if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
-       if (ssvm_segment_attach ((char *) mp->segment_name,
-                                SSVM_SEGMENT_MEMFD, fds[n_fds++]))
-         {
-           ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed",
-                      mp->segment_name);
-           goto failed;
-         }
-      if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
-       svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
-
-      vec_free (fds);
-    }
-  else
-    {
-      if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
-                              -1))
-       {
-         ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed",
-                    mp->segment_name);
-         return;
-       }
-    }
-  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
-  ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
-
-  em->state = STATE_ATTACHED;
-  return;
-failed:
-  for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
-    close (fds[i]);
-  vec_free (fds);
-}
-
-static void
-vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
-                                          mp)
-{
-  if (mp->retval)
-    {
-      ECHO_FAIL ("detach returned with err: %d", mp->retval);
-      return;
-    }
-  echo_main.state = STATE_DETACHED;
-}
-
-
-static void
-vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
-{
-  echo_main_t *em = &echo_main;
-  u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
-  echo_segment_handle_add_del (em, segment_handle, 0 /* add */ );
-  ECHO_LOG (1, "Unmaped segment 0x%lx", segment_handle);
-}
-
-static void
-vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
-{
-  fifo_segment_main_t *sm = &echo_main.segment_main;
-  fifo_segment_create_args_t _a, *a = &_a;
-  echo_main_t *em = &echo_main;
-  int *fds = 0, i;
-  char *seg_name = (char *) mp->segment_name;
-  u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
-
-  if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
-    {
-      vec_validate (fds, 1);
-      if (vl_socket_client_recv_fd_msg (fds, 1, 5))
-       {
-         ECHO_FAIL ("vl_socket_client_recv_fd_msg failed");
-         goto failed;
-       }
-
-      if (ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
-       {
-         ECHO_FAIL ("svm_fifo_segment_attach ('%s')"
-                    "failed on SSVM_SEGMENT_MEMFD", seg_name);
-         goto failed;
-       }
-      vec_free (fds);
-    }
-  else
-    {
-      clib_memset (a, 0, sizeof (*a));
-      a->segment_name = seg_name;
-      a->segment_size = mp->segment_size;
-      /* Attach to the segment vpp created */
-      if (fifo_segment_attach (sm, a))
-       {
-         ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed", seg_name);
-         goto failed;
-       }
-    }
-  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
-  ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
-  return;
-
-failed:
-  for (i = 0; i < vec_len (fds); i++)
-    close (fds[i]);
-  vec_free (fds);
-}
-
-static void
-vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
-{
-  echo_main_t *em = &echo_main;
-  if (mp->retval)
-    {
-      ECHO_FAIL ("bind failed: %U", format_api_error,
-                clib_net_to_host_u32 (mp->retval));
-      return;
-    }
-
-  em->state = STATE_LISTEN;
-}
-
-static void
-vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
-{
-  echo_session_t *listen_session;
-  echo_main_t *em = &echo_main;
-  if (mp->retval != 0)
-    {
-      ECHO_FAIL ("returned %d", ntohl (mp->retval));
-      return;
-    }
-  em->state = STATE_DISCONNECTED;
-  listen_session = pool_elt_at_index (em->sessions, em->listen_session_index);
-  echo_cleanup_session (em, listen_session);
-}
-
-static void
-vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
-                                          mp)
-{
-  echo_main_t *em = &echo_main;
-  echo_session_t *s;
-
-  if (mp->retval)
-    {
-      ECHO_FAIL ("vpp complained about disconnect: %d", ntohl (mp->retval));
-      return;
-    }
-
-  ECHO_LOG (1, "Got disonnected reply for session 0x%lx", mp->handle);
-  if (!(s = echo_get_session_from_handle (em, mp->handle)))
-    return;
-  if (s->session_type == QUIC_SESSION_TYPE_STREAM)
-    s->session_state = QUIC_SESSION_STATE_CLOSING;
-  else
-    echo_cleanup_session (em, s);      /* We can clean Q/Lsessions right away */
-}
-
-static void
-  vl_api_application_tls_cert_add_reply_t_handler
-  (vl_api_application_tls_cert_add_reply_t * mp)
-{
-  if (mp->retval)
-    ECHO_FAIL ("failed to add tls cert");
-}
-
-static void
-  vl_api_application_tls_key_add_reply_t_handler
-  (vl_api_application_tls_key_add_reply_t * mp)
-{
-  if (mp->retval)
-    ECHO_FAIL ("failed to add tls key");
-}
-
-static void
-vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
-{
-  echo_session_t *session;
-  echo_main_t *em = &echo_main;
-  u8 *uri;
-  if (!mp->retval)
-    return;
-  /* retry connect */
-  if (mp->context == SESSION_INVALID_INDEX)
-    {
-      ECHO_LOG (1, "Retrying connect %s", em->uri);
-      echo_send_rpc (em, echo_send_connect, (void *) em->uri,
-                    SESSION_INVALID_INDEX);
-    }
-  else
-    {
-      session = pool_elt_at_index (em->sessions, mp->context);
-      uri = format (0, "QUIC://session/%lu", session->vpp_session_handle);
-      ECHO_LOG (1, "Retrying connect %s", uri);
-      echo_send_rpc (em, echo_send_connect, (void *) uri, mp->context);
-    }
-
-}
-
-#define foreach_quic_echo_msg                                          \
-_(BIND_URI_REPLY, bind_uri_reply)                                      \
-_(UNBIND_URI_REPLY, unbind_uri_reply)                                  \
-_(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                  \
-_(APPLICATION_ATTACH_REPLY, application_attach_reply)                  \
-_(APPLICATION_DETACH_REPLY, application_detach_reply)                  \
-_(MAP_ANOTHER_SEGMENT, map_another_segment)                            \
-_(UNMAP_SEGMENT, unmap_segment)                                                \
-_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)      \
-_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)                \
-_(CONNECT_URI_REPLY, connect_uri_reply)                \
-
-void
-quic_echo_api_hookup (echo_main_t * em)
-{
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_quic_echo_msg;
-#undef _
-}
-
-/*
- *
- *  End Session API handlers
- *
- */
-
-static void
-print_usage_and_exit (void)
-{
-  fprintf (stderr,
-          "Usage: quic_echo [socket-name SOCKET] [client|server] [uri URI] [OPTIONS]\n"
-          "Generates traffic and assert correct teardown of the QUIC hoststack\n"
-          "\n"
-          "  socket-name PATH    Specify the binary socket path to connect to VPP\n"
-          "  use-svm-api         Use SVM API to connect to VPP\n"
-          "  test-bytes[:assert] Check data correctness when receiving (assert fails on first error)\n"
-          "  fifo-size N         Use N Kb fifos\n"
-          "  rx-buf N            Use N Kb RX buffer\n"
-          "  tx-buf N            Use N Kb TX test buffer\n"
-          "  appns NAMESPACE     Use the namespace NAMESPACE\n"
-          "  all-scope           all-scope option\n"
-          "  local-scope         local-scope option\n"
-          "  global-scope        global-scope option\n"
-          "  secret SECRET       set namespace secret\n"
-          "  chroot prefix PATH  Use PATH as memory root path\n"
-          "  quic-setup OPT      OPT=serverstream : Client open N connections. \n"
-          "                       On each one server opens M streams\n"
-          "                      OPT=default : Client open N connections.\n"
-          "                       On each one client opens M streams\n"
-          "  sclose=[Y|N|W]      When a stream is done,    pass[N] send[Y] or wait[W] for close\n"
-          "  qclose=[Y|N|W]      When a connection is done pass[N] send[Y] or wait[W] for close\n"
-          "\n"
-          "  time START:END      Time between evts START & END, events being :\n"
-          "                       start - Start of the app\n"
-          "                       qconnect    - first Connection connect sent\n"
-          "                       qconnected  - last Connection connected\n"
-          "                       sconnect    - first Stream connect sent\n"
-          "                       sconnected  - last Stream got connected\n"
-          "                       lastbyte    - Last expected byte received\n"
-          "                       exit        - Exiting of the app\n"
-          "  json                Output global stats in json\n"
-          "  log=N               Set the log level to [0: no output, 1:errors, 2:log]\n"
-          "  max-connects=N      Don't do more than N parallel connect_uri\n"
-          "\n"
-          "  nclients N[/M]      Open N QUIC connections, each one with M streams (M defaults to 1)\n"
-          "  nthreads N          Use N busy loop threads for data [in addition to main & msg queue]\n"
-          "  TX=1337[Kb|Mb|GB]   Send 1337 [K|M|G]bytes, use TX=RX to reflect the data\n"
-          "  RX=1337[Kb|Mb|GB]   Expect 1337 [K|M|G]bytes\n"
-          "\n"
-          "Default configuration is :\n"
-          " server nclients 1/1 RX=64Kb TX=RX\n"
-          " client nclients 1/1 RX=64Kb TX=64Kb\n");
-  exit (1);
-}
-
-
-void
-quic_echo_process_opts (int argc, char **argv)
-{
-  echo_main_t *em = &echo_main;
-  unformat_input_t _argv, *a = &_argv;
-  u32 tmp;
-  u8 *chroot_prefix;
-  u8 *uri = 0;
-  u8 default_f_active;
-
-  unformat_init_command_line (a, argv);
-  while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
-    {
-      if (unformat (a, "chroot prefix %s", &chroot_prefix))
-       {
-         vl_set_memory_root_path ((char *) chroot_prefix);
-       }
-      else if (unformat (a, "uri %s", &uri))
-       em->uri = format (0, "%s%c", uri, 0);
-      else if (unformat (a, "server"))
-       em->i_am_master = 1;
-      else if (unformat (a, "client"))
-       em->i_am_master = 0;
-      else if (unformat (a, "test-bytes:assert"))
-       em->test_return_packets = RETURN_PACKETS_ASSERT;
-      else if (unformat (a, "test-bytes"))
-       em->test_return_packets = RETURN_PACKETS_LOG_WRONG;
-      else if (unformat (a, "socket-name %s", &em->socket_name))
-       ;
-      else if (unformat (a, "use-svm-api"))
-       em->use_sock_api = 0;
-      else if (unformat (a, "fifo-size %d", &tmp))
-       em->fifo_size = tmp << 10;
-      else if (unformat (a, "rx-buf %d", &tmp))
-       em->rx_buf_size = tmp << 10;
-      else if (unformat (a, "tx-buf %d", &tmp))
-       em->rx_buf_size = tmp << 10;
-      else
-       if (unformat
-           (a, "nclients %d/%d", &em->n_clients, &em->n_stream_clients))
-       ;
-      else if (unformat (a, "nclients %d", &em->n_clients))
-       ;
-      else if (unformat (a, "nthreads %d", &em->n_rx_threads))
-       ;
-      else if (unformat (a, "appns %_%v%_", &em->appns_id))
-       ;
-      else if (unformat (a, "all-scope"))
-       em->appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
-                           | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
-      else if (unformat (a, "local-scope"))
-       em->appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
-      else if (unformat (a, "global-scope"))
-       em->appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
-      else if (unformat (a, "secret %lu", &em->appns_secret))
-       ;
-      else if (unformat (a, "quic-setup %U", echo_unformat_quic_setup_vft))
-       ;
-      else if (unformat (a, "TX=RX"))
-       em->data_source = ECHO_RX_DATA_SOURCE;
-      else if (unformat (a, "TX=%U", unformat_data, &em->bytes_to_send))
-       ;
-      else if (unformat (a, "RX=%U", unformat_data, &em->bytes_to_receive))
-       ;
-      else if (unformat (a, "json"))
-       em->output_json = 1;
-      else if (unformat (a, "log=%d", &em->log_lvl))
-       ;
-      else
-       if (unformat
-           (a, "sclose=%U", unformat_close, &em->send_stream_disconnects))
-       ;
-      else
-       if (unformat
-           (a, "qclose=%U", unformat_close, &em->send_quic_disconnects))
-       ;
-      else if (unformat (a, "time %U:%U",
-                        echo_unformat_timing_event, &em->timing.start_event,
-                        echo_unformat_timing_event, &em->timing.end_event))
-       ;
-      else
-       print_usage_and_exit ();
-    }
-
-  /* setting default for unset values
-   *
-   * bytes_to_send / bytes_to_receive & data_source  */
-  if (em->bytes_to_receive == (u64) ~ 0)
-    em->bytes_to_receive = 64 << 10;   /* default */
-  if (em->bytes_to_send == (u64) ~ 0)
-    em->bytes_to_send = 64 << 10;      /* default */
-  else if (em->bytes_to_send == 0)
-    em->data_source = ECHO_NO_DATA_SOURCE;
-  else
-    em->data_source = ECHO_TEST_DATA_SOURCE;
-
-  if (em->data_source == ECHO_INVALID_DATA_SOURCE)
-    em->data_source =
-      em->i_am_master ? ECHO_RX_DATA_SOURCE : ECHO_TEST_DATA_SOURCE;
-  if (em->data_source == ECHO_RX_DATA_SOURCE)
-    em->bytes_to_send = em->bytes_to_receive;
-
-  /* disconnect flags  */
-  if (em->i_am_master)
-    default_f_active =
-      em->bytes_to_send == 0 ? ECHO_CLOSE_F_ACTIVE : ECHO_CLOSE_F_PASSIVE;
-  else
-    default_f_active =
-      em->bytes_to_receive == 0 ? ECHO_CLOSE_F_PASSIVE : ECHO_CLOSE_F_ACTIVE;
-  if (em->send_stream_disconnects == ECHO_CLOSE_F_INVALID)
-    em->send_stream_disconnects = default_f_active;
-  if (em->send_quic_disconnects == ECHO_CLOSE_F_INVALID)
-    em->send_quic_disconnects = default_f_active;
-}
-
-int
-main (int argc, char **argv)
-{
-  echo_main_t *em = &echo_main;
-  fifo_segment_main_t *sm = &em->segment_main;
-  char *app_name;
-  u64 n_clients, i;
-  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
-  u32 rpc_queue_size = 64 << 10;
-
-  clib_mem_init_thread_safe (0, 256 << 20);
-  clib_memset (em, 0, sizeof (*em));
-  em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
-  clib_spinlock_init (&em->sid_vpp_handles_lock);
-  em->shared_segment_handles = hash_create (0, sizeof (uword));
-  clib_spinlock_init (&em->segment_handles_lock);
-  em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
-  em->use_sock_api = 1;
-  em->fifo_size = 64 << 10;
-  em->n_clients = 1;
-  em->n_stream_clients = 1;
-  em->max_test_msg = 50;
-  em->time_to_stop = 0;
-  em->i_am_master = 1;
-  em->n_rx_threads = 4;
-  em->test_return_packets = RETURN_PACKETS_NOTEST;
-  em->timing.start_event = ECHO_EVT_FIRST_QCONNECT;
-  em->timing.end_event = ECHO_EVT_LAST_BYTE;
-  em->bytes_to_receive = ~0;   /* defaulted when we know if server/client */
-  em->bytes_to_send = ~0;      /* defaulted when we know if server/client */
-  em->rx_buf_size = 1 << 20;
-  em->tx_buf_size = 1 << 20;
-  em->data_source = ECHO_INVALID_DATA_SOURCE;
-  em->uri = format (0, "%s%c", "quic://0.0.0.0/1234", 0);
-  em->cb_vft = default_cb_vft;
-  quic_echo_process_opts (argc, argv);
-
-  n_clients = em->n_clients * em->n_stream_clients;
-  vec_validate (em->data_thread_handles, em->n_rx_threads - 1);
-  vec_validate (em->data_thread_args, n_clients - 1);
-  for (i = 0; i < n_clients; i++)
-    em->data_thread_args[i] = SESSION_INVALID_INDEX;
-  clib_time_init (&em->clib_time);
-  init_error_string_table ();
-  fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
-  vec_validate (em->connect_test_data, em->tx_buf_size);
-  for (i = 0; i < em->tx_buf_size; i++)
-    em->connect_test_data[i] = i & 0xff;
-
-  /* *INDENT-OFF* */
-  svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = {
-    {rpc_queue_size, sizeof (echo_rpc_msg_t), 0},
-  };
-  /* *INDENT-ON* */
-  cfg->consumer_pid = getpid ();
-  cfg->n_rings = 1;
-  cfg->q_nitems = rpc_queue_size;
-  cfg->ring_cfgs = rc;
-  em->rpc_msq_queue = svm_msg_q_alloc (cfg);
-
-  signal (SIGINT, stop_signal);
-  signal (SIGQUIT, stop_signal);
-  signal (SIGTERM, stop_signal);
-  quic_echo_api_hookup (em);
-
-  app_name = em->i_am_master ? "quic_echo_server" : "quic_echo_client";
-  if (connect_to_vpp (app_name) < 0)
-    {
-      svm_region_exit ();
-      ECHO_FAIL ("Couldn't connect to vpe, exiting...\n");
-      exit (1);
-    }
-
-  echo_session_prealloc (em);
-  quic_echo_notify_event (em, ECHO_EVT_START);
-
-  application_send_attach (em);
-  if (wait_for_state_change (em, STATE_ATTACHED, TIMEOUT))
-    {
-      ECHO_FAIL ("Couldn't attach to vpp, did you run <session enable> ?\n");
-      exit (1);
-    }
-  if (pthread_create (&em->mq_thread_handle,
-                     NULL /*attr */ , echo_mq_thread_fn, 0))
-    {
-      ECHO_FAIL ("pthread create errored\n");
-      exit (1);
-    }
-  for (i = 0; i < em->n_rx_threads; i++)
-    if (pthread_create (&em->data_thread_handles[i],
-                       NULL /*attr */ , echo_data_thread_fn, (void *) i))
-      {
-       ECHO_FAIL ("pthread create errored\n");
-       exit (1);
-      }
-  if (em->i_am_master)
-    server_run (em);
-  else
-    clients_run (em);
-  quic_echo_notify_event (em, ECHO_EVT_EXIT);
-  if (em->output_json)
-    print_global_json_stats (em);
-  else
-    print_global_stats (em);
-  echo_free_sessions (em);
-  echo_assert_test_suceeded (em);
-  application_detach (em);
-  if (wait_for_state_change (em, STATE_DETACHED, TIMEOUT))
-    {
-      ECHO_FAIL ("ECHO-ERROR: Couldn't detach from vpp, exiting...\n");
-      exit (1);
-    }
-  int *rv;
-  pthread_join (em->mq_thread_handle, (void **) &rv);
-  if (rv)
-    {
-      ECHO_FAIL ("mq pthread errored %d", rv);
-      exit (1);
-    }
-  if (em->use_sock_api)
-    vl_socket_client_disconnect ();
-  else
-    vl_client_disconnect_from_vlib ();
-  ECHO_LOG (0, "Test complete !\n");
-  exit (em->has_failed);
-}
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */
diff --git a/src/plugins/hs_apps/sapi/quic_echo.h b/src/plugins/hs_apps/sapi/quic_echo.h
deleted file mode 100644 (file)
index aec56d0..0000000
+++ /dev/null
@@ -1,246 +0,0 @@
-/*
- * Copyright (c) 2019 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <signal.h>
-
-#include <vnet/session/application_interface.h>
-
-#define vl_typedefs            /* define message structures */
-#include <vpp/api/vpe_all_api_h.h>
-#undef vl_typedefs
-
-/* declare message handlers for each api */
-
-#define vl_endianfun           /* define message structures */
-#include <vpp/api/vpe_all_api_h.h>
-#undef vl_endianfun
-
-/* instantiate all the print functions we know about */
-#define vl_print(handle, ...)
-#define vl_printfun
-#include <vpp/api/vpe_all_api_h.h>
-#undef vl_printfun
-
-#define NITER 4000000
-#define TIMEOUT 10.0
-
-#define CHECK(expected, result, _fmt, _args...)                \
-    if (expected != result)                            \
-      ECHO_FAIL ("expected %d, got %d : " _fmt, expected, result, ##_args);
-
-#define ECHO_FAIL(_fmt,_args...)       \
-  {                                    \
-    echo_main_t *em = &echo_main;      \
-    em->has_failed = 1;                \
-    em->time_to_stop = 1;              \
-    if (em->log_lvl > 0)               \
-      clib_warning ("ECHO-ERROR: "_fmt, ##_args);      \
-  }
-
-#define ECHO_LOG(lvl, _fmt,_args...)   \
-  {                                    \
-    echo_main_t *em = &echo_main;      \
-    if (em->log_lvl > lvl)             \
-      clib_warning (_fmt, ##_args);    \
-  }
-
-typedef struct
-{
-  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
-#define _(type, name) type name;
-  foreach_app_session_field
-#undef _
-  u64 vpp_session_handle;
-  u64 bytes_sent;
-  u64 bytes_to_send;
-  volatile u64 bytes_received;
-  volatile u64 bytes_to_receive;
-  f64 start;
-  u32 listener_index;          /* listener index in echo session pool */
-  u32 idle_cycles;             /* consecutive enq/deq with no data */
-  volatile u64 accepted_session_count; /* sessions we accepted */
-} echo_session_t;
-
-typedef enum
-{
-  ECHO_NO_DATA_SOURCE,
-  ECHO_TEST_DATA_SOURCE,
-  ECHO_RX_DATA_SOURCE,
-  ECHO_INVALID_DATA_SOURCE
-} data_source_t;
-
-enum echo_close_f_t
-{
-  ECHO_CLOSE_F_INVALID = 0,
-  ECHO_CLOSE_F_PASSIVE,                /* wait for close msg */
-  ECHO_CLOSE_F_ACTIVE,         /* send close msg */
-  ECHO_CLOSE_F_NONE,           /* don't bother sending close msg */
-};
-
-enum quic_session_type_t
-{
-  QUIC_SESSION_TYPE_QUIC,
-  QUIC_SESSION_TYPE_STREAM,
-  QUIC_SESSION_TYPE_LISTEN,
-};
-
-enum quic_session_state_t
-{
-  QUIC_SESSION_STATE_INITIAL,
-  QUIC_SESSION_STATE_READY,
-  QUIC_SESSION_STATE_AWAIT_CLOSING,    /* Data transfer is done, wait for close evt */
-  QUIC_SESSION_STATE_AWAIT_DATA,       /* Peer closed, wait for outstanding data */
-  QUIC_SESSION_STATE_CLOSING,  /* told vpp to close */
-  QUIC_SESSION_STATE_CLOSED,   /* closed in vpp */
-};
-
-typedef enum
-{
-  STATE_START,
-  STATE_ATTACHED,
-  STATE_LISTEN,
-  STATE_READY,
-  STATE_DATA_DONE,
-  STATE_DISCONNECTED,
-  STATE_DETACHED
-} connection_state_t;
-
-typedef enum echo_test_evt_
-{
-  ECHO_EVT_START = 1,          /* app starts */
-  ECHO_EVT_FIRST_QCONNECT = (1 << 1),  /* First connect Quic session sent */
-  ECHO_EVT_LAST_QCONNECTED = (1 << 2), /* All Quic session are connected */
-  ECHO_EVT_FIRST_SCONNECT = (1 << 3),  /* First connect Stream session sent */
-  ECHO_EVT_LAST_SCONNECTED = (1 << 4), /* All Stream session are connected */
-  ECHO_EVT_LAST_BYTE = (1 << 5),       /* Last byte received */
-  ECHO_EVT_EXIT = (1 << 6),    /* app exits */
-} echo_test_evt_t;
-
-typedef struct _quic_echo_cb_vft
-{
-  void (*quic_connected_cb) (session_connected_msg_t * mp, u32 session_index);
-  void (*client_stream_connected_cb) (session_connected_msg_t * mp,
-                                     u32 session_index);
-  void (*server_stream_connected_cb) (session_connected_msg_t * mp,
-                                     u32 session_index);
-  void (*quic_accepted_cb) (session_accepted_msg_t * mp, u32 session_index);
-  void (*client_stream_accepted_cb) (session_accepted_msg_t * mp,
-                                    u32 session_index);
-  void (*server_stream_accepted_cb) (session_accepted_msg_t * mp,
-                                    u32 session_index);
-} quic_echo_cb_vft_t;
-
-
-typedef enum
-{
-  RETURN_PACKETS_NOTEST,
-  RETURN_PACKETS_LOG_WRONG,
-  RETURN_PACKETS_ASSERT,
-} test_return_packets_t;
-
-typedef struct teardown_stat_
-{
-  u32 q; /* quic sessions */
-  u32 s; /* stream sessions */
-} teardown_stat_t;
-
-typedef struct
-{
-  svm_queue_t *vl_input_queue; /* vpe input queue */
-  u32 my_client_index;         /* API client handle */
-  u8 *uri;                     /* The URI we're playing with */
-  echo_session_t *sessions;    /* Session pool */
-  svm_msg_q_t *our_event_queue;        /* Our event queue */
-  clib_time_t clib_time;       /* For deadman timers */
-  u8 *socket_name;
-  int i_am_master;
-  u32 listen_session_index;    /* Index of vpp listener session */
-
-  uword *session_index_by_vpp_handles; /* Hash table : quic_echo s_id -> vpp s_handle */
-  clib_spinlock_t sid_vpp_handles_lock;        /* Hash table lock */
-
-  uword *shared_segment_handles;       /* Hash table : segment_names -> 1*/
-  clib_spinlock_t segment_handles_lock;        /* Hash table lock */
-  quic_echo_cb_vft_t cb_vft;   /* cb vft for QUIC scenarios */
-  svm_msg_q_t *rpc_msq_queue;  /* MQ between quic_echo threads */
-  fifo_segment_main_t segment_main;
-
-  /* State of the connection, shared between msg RX thread and main thread */
-  volatile connection_state_t state;
-  volatile u8 time_to_stop;    /* Signal variables */
-  u8 has_failed;               /* stores the exit code */
-
-  /** Flag that decides if socket, instead of svm, api is used to connect to
-   * vpp. If sock api is used, shm binary api is subsequently bootstrapped
-   * and all other messages are exchanged using shm IPC. */
-  u8 use_sock_api;
-
-  u8 *connect_test_data;
-  u8 test_return_packets;
-  u64 bytes_to_send;           /* target per stream */
-  u64 bytes_to_receive;                /* target per stream */
-  u32 fifo_size;
-  u32 rx_buf_size;
-  u32 tx_buf_size;
-  data_source_t data_source;   /* Use no/dummy/mirrored data */
-  u8 send_quic_disconnects;    /* actively send disconnect */
-  u8 send_stream_disconnects;  /* actively send disconnect */
-  u8 output_json;              /* Output stats as JSON */
-  u8 log_lvl;                  /* Verbosity of the logging */
-  int max_test_msg;            /* Limit the number of incorrect data messages */
-
-  u8 *appns_id;
-  u64 appns_flags;
-  u64 appns_secret;
-
-  pthread_t *data_thread_handles;      /* vec of data thread handles */
-  pthread_t mq_thread_handle;  /* Message queue thread handle */
-  u32 *data_thread_args;
-
-  u32 n_clients;               /* Target number of QUIC sessions */
-  u32 n_stream_clients;                /* Target Number of STREAM sessions per QUIC session */
-  volatile u32 n_quic_clients_connected;       /* Number of connected QUIC sessions */
-  volatile u32 n_clients_connected;    /* Number of STREAM sessions connected */
-  u32 n_rx_threads;            /* Number of data threads */
-  volatile u32 nxt_available_sidx; /* next unused prealloced session_index */
-
-  struct {
-    u64 tx_total;
-    u64 rx_total;
-    teardown_stat_t reset_count; /* received reset from vpp */
-    teardown_stat_t close_count; /* received close from vpp */
-    teardown_stat_t active_count; /* sent close to vpp */
-    teardown_stat_t clean_count; /* cleaned up stale session */
-  } stats;
-
-  struct /* Event based timing : start & end depend on CLI specified events */
-  {
-    f64 start_time;
-    f64 end_time;
-    u8 events_sent;
-    u8 start_event;
-    u8 end_event;
-  } timing;
-} echo_main_t;
-
-typedef void (*echo_rpc_t) (void *arg, u32 opaque);
-
-typedef struct
-{
-  void *fp;
-  void *arg;
-  u32 opaque;
-} echo_rpc_msg_t;
diff --git a/src/plugins/hs_apps/sapi/vpp_echo.c b/src/plugins/hs_apps/sapi/vpp_echo.c
new file mode 100644 (file)
index 0000000..5c9690f
--- /dev/null
@@ -0,0 +1,1049 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <signal.h>
+
+#include <vlibmemory/api.h>
+#include <svm/fifo_segment.h>
+
+#include <hs_apps/sapi/vpp_echo_common.h>
+
+echo_main_t echo_main;
+
+static void
+echo_session_prealloc (echo_main_t * em)
+{
+  /* We need to prealloc to avoid vec resize in threads */
+  echo_session_t *session;
+  int i;
+  for (i = 0; i < em->n_sessions; i++)
+    {
+      pool_get (em->sessions, session);
+      clib_memset (session, 0, sizeof (*session));
+      session->session_index = session - em->sessions;
+      session->listener_index = SESSION_INVALID_INDEX;
+      session->session_state = ECHO_SESSION_STATE_INITIAL;
+    }
+}
+
+static void
+echo_assert_test_suceeded (echo_main_t * em)
+{
+  CHECK (em->n_clients * em->bytes_to_receive,
+        em->stats.rx_total, "Not enough data received");
+  CHECK (em->n_clients * em->bytes_to_send,
+        em->stats.tx_total, "Not enough data sent");
+  clib_spinlock_lock (&em->sid_vpp_handles_lock);
+  CHECK (0, hash_elts (em->session_index_by_vpp_handles),
+        "Some sessions are still open");
+  clib_spinlock_unlock (&em->sid_vpp_handles_lock);
+}
+
+always_inline void
+echo_session_dequeue_notify (echo_session_t * s)
+{
+  int rv;
+  if (!svm_fifo_set_event (s->rx_fifo))
+    return;
+  if ((rv =
+       app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
+                              SESSION_IO_EVT_RX, SVM_Q_WAIT)))
+    ECHO_FAIL ("app_send_io_evt_to_vpp errored %d", rv);
+  svm_fifo_clear_deq_ntf (s->rx_fifo);
+}
+
+static void
+stop_signal (int signum)
+{
+  echo_main_t *em = &echo_main;
+  em->time_to_stop = 1;
+}
+
+int
+connect_to_vpp (char *name)
+{
+  echo_main_t *em = &echo_main;
+  api_main_t *am = &api_main;
+
+  if (em->use_sock_api)
+    {
+      if (vl_socket_client_connect ((char *) em->socket_name, name,
+                                   0 /* default rx, tx buffer */ ))
+       {
+         ECHO_FAIL ("socket connect failed");
+         return -1;
+       }
+
+      if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
+       {
+         ECHO_FAIL ("init shm api failed");
+         return -1;
+       }
+    }
+  else
+    {
+      if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
+       {
+         ECHO_FAIL ("shmem connect failed");
+         return -1;
+       }
+    }
+  em->vl_input_queue = am->shmem_hdr->vl_input_queue;
+  em->my_client_index = am->my_client_index;
+  return 0;
+}
+
+static void
+echo_event_didnt_happen (u8 e)
+{
+  echo_main_t *em = &echo_main;
+  u8 *s = format (0, "%U", echo_format_timing_event, e);
+  ECHO_LOG (0, "Expected event %s to happend, which did not", s);
+  em->has_failed = 1;
+}
+
+static void
+print_global_json_stats (echo_main_t * em)
+{
+  if (!(em->timing.events_sent & em->timing.start_event))
+    return echo_event_didnt_happen (em->timing.start_event);
+  if (!(em->timing.events_sent & em->timing.end_event))
+    return echo_event_didnt_happen (em->timing.end_event);
+  f64 deltat = em->timing.end_time - em->timing.start_time;
+  u8 *start_evt =
+    format (0, "%U", echo_format_timing_event, em->timing.start_event);
+  u8 *end_evt =
+    format (0, "%U", echo_format_timing_event, em->timing.end_event);
+  fformat (stdout, "{\n");
+  fformat (stdout, "\"time\": \"%.9f\",\n", deltat);
+  fformat (stdout, "\"start_evt\": \"%s\",\n", start_evt);
+  fformat (stdout, "\"end_evt\": \"%s\",\n", end_evt);
+  fformat (stdout, "\"rx_data\": %lld,\n", em->stats.rx_total);
+  fformat (stdout, "\"tx_rx\": %lld,\n", em->stats.tx_total);
+  fformat (stdout, "\"closing\": {\n");
+  fformat (stdout, "  \"reset\": { \"q\": %d, \"s\": %d },\n",
+          em->stats.reset_count.q, em->stats.reset_count.s);
+  fformat (stdout, "  \"close\": { \"q\": %d, \"s\": %d },\n",
+          em->stats.close_count.q, em->stats.close_count.s);
+  fformat (stdout, "  \"active\": { \"q\": %d, \"s\": %d },\n",
+          em->stats.active_count.q, em->stats.active_count.s);
+  fformat (stdout, "  \"clean\": { \"q\": %d, \"s\": %d }\n",
+          em->stats.clean_count.q, em->stats.clean_count.s);
+  fformat (stdout, "}\n");
+  fformat (stdout, "}\n");
+}
+
+static void
+print_global_stats (echo_main_t * em)
+{
+  u8 *s;
+  if (!(em->timing.events_sent & em->timing.start_event))
+    return echo_event_didnt_happen (em->timing.start_event);
+  if (!(em->timing.events_sent & em->timing.end_event))
+    return echo_event_didnt_happen (em->timing.end_event);
+  f64 deltat = em->timing.end_time - em->timing.start_time;
+  s = format (0, "%U:%U",
+             echo_format_timing_event, em->timing.start_event,
+             echo_format_timing_event, em->timing.end_event);
+  fformat (stdout, "Timing %s\n", s);
+  fformat (stdout, "-------- TX --------\n");
+  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
+          em->stats.tx_total, em->stats.tx_total / (1ULL << 20),
+          em->stats.tx_total / (1ULL << 30), deltat);
+  fformat (stdout, "%.4f Gbit/second\n",
+          (em->stats.tx_total * 8.0) / deltat / 1e9);
+  fformat (stdout, "-------- RX --------\n");
+  fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.6f seconds\n",
+          em->stats.rx_total, em->stats.rx_total / (1ULL << 20),
+          em->stats.rx_total / (1ULL << 30), deltat);
+  fformat (stdout, "%.4f Gbit/second\n",
+          (em->stats.rx_total * 8.0) / deltat / 1e9);
+  fformat (stdout, "--------------------\n");
+  fformat (stdout, "Received close on %d streams (and %d Quic conn)\n",
+          em->stats.close_count.s, em->stats.close_count.q);
+  fformat (stdout, "Received reset on %d streams (and %d Quic conn)\n",
+          em->stats.reset_count.s, em->stats.reset_count.q);
+  fformat (stdout, "Sent close on     %d streams (and %d Quic conn)\n",
+          em->stats.active_count.s, em->stats.active_count.q);
+  fformat (stdout, "Discarded         %d streams (and %d Quic conn)\n",
+          em->stats.clean_count.s, em->stats.clean_count.q);
+}
+
+void
+echo_update_count_on_session_close (echo_main_t * em, echo_session_t * s)
+{
+
+  ECHO_LOG (1, "[%lu/%lu] -> S(%x) -> [%lu/%lu]",
+           s->bytes_received, s->bytes_received + s->bytes_to_receive,
+           s->session_index, s->bytes_sent,
+           s->bytes_sent + s->bytes_to_send);
+  clib_atomic_fetch_add (&em->stats.tx_total, s->bytes_sent);
+  clib_atomic_fetch_add (&em->stats.rx_total, s->bytes_received);
+
+  if (PREDICT_FALSE (em->stats.rx_total ==
+                    em->n_clients * em->bytes_to_receive))
+    echo_notify_event (em, ECHO_EVT_LAST_BYTE);
+}
+
+static void
+echo_free_sessions (echo_main_t * em)
+{
+  /* Free marked sessions */
+  echo_session_t *s;
+  u32 *session_indexes = 0, *session_index;
+
+  /* *INDENT-OFF* */
+  pool_foreach (s, em->sessions,
+  ({
+    if (s->session_state == ECHO_SESSION_STATE_CLOSED)
+      vec_add1 (session_indexes, s->session_index);}
+  ));
+  /* *INDENT-ON* */
+  vec_foreach (session_index, session_indexes)
+  {
+    /* Free session */
+    s = pool_elt_at_index (em->sessions, *session_index);
+    echo_session_handle_add_del (em, s->vpp_session_handle,
+                                SESSION_INVALID_INDEX);
+    pool_put (em->sessions, s);
+    clib_memset (s, 0xfe, sizeof (*s));
+  }
+}
+
+static void
+test_recv_bytes (echo_main_t * em, echo_session_t * s, u8 * rx_buf,
+                u32 n_read)
+{
+  u32 i;
+  u8 expected;
+  for (i = 0; i < n_read; i++)
+    {
+      expected = (s->bytes_received + i) & 0xff;
+      if (rx_buf[i] == expected || em->max_test_msg > 0)
+       continue;
+      ECHO_LOG (0, "Session 0x%lx byte %lld was 0x%x expected 0x%x",
+               s->vpp_session_handle, s->bytes_received + i, rx_buf[i],
+               expected);
+      em->max_test_msg--;
+      if (em->max_test_msg == 0)
+       ECHO_LOG (0, "Too many errors, hiding next ones");
+      if (em->test_return_packets == RETURN_PACKETS_ASSERT)
+       ECHO_FAIL ("test-bytes errored");
+    }
+}
+
+static int
+recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
+{
+  int n_read;
+  n_read = app_recv ((app_session_t *) s, rx_buf, vec_len (rx_buf));
+  if (n_read <= 0)
+    return 0;
+  if (svm_fifo_needs_deq_ntf (s->rx_fifo, n_read))
+    echo_session_dequeue_notify (s);
+
+  if (em->test_return_packets)
+    test_recv_bytes (em, s, rx_buf, n_read);
+
+  s->bytes_received += n_read;
+  s->bytes_to_receive -= n_read;
+  return n_read;
+}
+
+static int
+send_data_chunk (echo_session_t * s, u8 * tx_buf, int offset, int len)
+{
+  int n_sent;
+  int bytes_this_chunk = clib_min (s->bytes_to_send, len - offset);
+  if (!bytes_this_chunk)
+    return 0;
+  n_sent = app_send ((app_session_t *) s, tx_buf + offset,
+                    bytes_this_chunk, SVM_Q_WAIT);
+  if (n_sent < 0)
+    return 0;
+  s->bytes_to_send -= n_sent;
+  s->bytes_sent += n_sent;
+  return n_sent;
+}
+
+static int
+mirror_data_chunk (echo_main_t * em, echo_session_t * s, u8 * tx_buf, u64 len)
+{
+  u64 n_sent = 0;
+  while (n_sent < len && !em->time_to_stop)
+    n_sent += send_data_chunk (s, tx_buf, n_sent, len);
+  return n_sent;
+}
+
+static inline void
+echo_check_closed_listener (echo_main_t * em, echo_session_t * s)
+{
+  echo_session_t *ls;
+  /* if parent has died, terminate gracefully */
+  if (s->listener_index == SESSION_INVALID_INDEX)
+    return;
+  ls = pool_elt_at_index (em->sessions, s->listener_index);
+  if (ls->session_state < ECHO_SESSION_STATE_CLOSING)
+    return;
+  ECHO_LOG (2, "Session 0%lx died, close child 0x%lx", ls->vpp_session_handle,
+           s->vpp_session_handle);
+  echo_update_count_on_session_close (em, s);
+  em->proto_cb_vft->cleanup_cb (s, 1 /* parent_died */ );
+}
+
+/*
+ * Rx/Tx polling thread per connection
+ */
+static void
+echo_handle_data (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
+{
+  int n_read, n_sent = 0;
+
+  n_read = recv_data_chunk (em, s, rx_buf);
+  if (em->data_source == ECHO_TEST_DATA_SOURCE)
+    n_sent = send_data_chunk (s, em->connect_test_data,
+                             s->bytes_sent % em->tx_buf_size,
+                             em->tx_buf_size);
+  else if (em->data_source == ECHO_RX_DATA_SOURCE)
+    n_sent = mirror_data_chunk (em, s, rx_buf, n_read);
+  if (!s->bytes_to_send && !s->bytes_to_receive)
+    {
+      /* Session is done, need to close */
+      if (s->session_state == ECHO_SESSION_STATE_AWAIT_DATA)
+       s->session_state = ECHO_SESSION_STATE_CLOSING;
+      else
+       {
+         s->session_state = ECHO_SESSION_STATE_AWAIT_CLOSING;
+         if (em->send_stream_disconnects == ECHO_CLOSE_F_ACTIVE)
+           {
+             echo_send_rpc (em, echo_send_disconnect_session,
+                            (void *) s->vpp_session_handle, 0);
+             clib_atomic_fetch_add (&em->stats.active_count.s, 1);
+           }
+         else if (em->send_stream_disconnects == ECHO_CLOSE_F_NONE)
+           {
+             s->session_state = ECHO_SESSION_STATE_CLOSING;
+             clib_atomic_fetch_add (&em->stats.clean_count.s, 1);
+           }
+       }
+      return;
+    }
+
+  /* Check for idle clients */
+  if (em->log_lvl > 1)
+    {
+      if (n_sent || n_read)
+       s->idle_cycles = 0;
+      else if (s->idle_cycles++ == 1e7)
+       {
+         s->idle_cycles = 0;
+         ECHO_LOG (1, "Idle client TX:%dB RX:%dB", s->bytes_to_send,
+                   s->bytes_to_receive);
+         ECHO_LOG (1, "Idle FIFOs TX:%dB RX:%dB",
+                   svm_fifo_max_dequeue (s->tx_fifo),
+                   svm_fifo_max_dequeue (s->rx_fifo));
+         ECHO_LOG (1, "Session 0x%lx state %u", s->vpp_session_handle,
+                   s->session_state);
+       }
+    }
+}
+
+static void *
+echo_data_thread_fn (void *arg)
+{
+  clib_mem_set_thread_index ();
+  echo_main_t *em = &echo_main;
+  u32 N = em->n_clients;
+  u32 n = (N + em->n_rx_threads - 1) / em->n_rx_threads;
+  u32 idx = (u64) arg;
+  if (n * idx >= N)
+    {
+      ECHO_LOG (1, "Thread %u exiting, no sessions to care for", idx);
+      pthread_exit (0);
+    }
+  u32 thread_n_sessions = clib_min (n, N - n * idx);
+
+  u32 i = 0;
+  u32 n_closed_sessions = 0;
+  u32 session_index;
+  u8 *rx_buf = 0;
+  echo_session_t *s;
+  vec_validate (rx_buf, em->rx_buf_size);
+
+  for (i = 0; !em->time_to_stop; i = (i + 1) % thread_n_sessions)
+    {
+      n_closed_sessions = i == 0 ? 0 : n_closed_sessions;
+      session_index = em->data_thread_args[n * idx + i];
+      if (session_index == SESSION_INVALID_INDEX)
+       continue;
+      s = pool_elt_at_index (em->sessions, session_index);
+      switch (s->session_state)
+       {
+       case ECHO_SESSION_STATE_READY:
+       case ECHO_SESSION_STATE_AWAIT_DATA:
+         echo_handle_data (em, s, rx_buf);
+         echo_check_closed_listener (em, s);
+         break;
+       case ECHO_SESSION_STATE_AWAIT_CLOSING:
+         echo_check_closed_listener (em, s);
+         break;
+       case ECHO_SESSION_STATE_CLOSING:
+         echo_update_count_on_session_close (em, s);
+         em->proto_cb_vft->cleanup_cb (s, 0 /* parent_died */ );
+         break;
+       case ECHO_SESSION_STATE_CLOSED:
+         n_closed_sessions++;
+         break;
+       }
+      if (n_closed_sessions == thread_n_sessions)
+       break;
+    }
+  pthread_exit (0);
+}
+
+static void
+session_bound_handler (session_bound_msg_t * mp)
+{
+  echo_main_t *em = &echo_main;
+  echo_session_t *listen_session;
+  if (mp->retval)
+    {
+      ECHO_FAIL ("bind failed: %U", format_api_error,
+                clib_net_to_host_u32 (mp->retval));
+      return;
+    }
+  ECHO_LOG (0, "listening on %U:%u", format_ip46_address, mp->lcl_ip,
+           mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
+           clib_net_to_host_u16 (mp->lcl_port));
+
+  /* Allocate local session and set it up */
+  listen_session = echo_session_new (em);
+  listen_session->session_type = ECHO_SESSION_TYPE_LISTEN;
+  echo_session_handle_add_del (em, mp->handle, listen_session->session_index);
+  em->state = STATE_LISTEN;
+  em->listen_session_index = listen_session->session_index;
+  if (em->proto_cb_vft->bound_uri_cb)
+    em->proto_cb_vft->bound_uri_cb (mp, listen_session);
+}
+
+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;
+  echo_session_t *session, *ls;
+  /* Allocate local session and set it up */
+  session = echo_session_new (em);
+
+  if (wait_for_segment_allocation (mp->segment_handle))
+    {
+      ECHO_FAIL ("wait_for_segment_allocation errored");
+      return;
+    }
+
+  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
+  rx_fifo->client_session_index = session->session_index;
+  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
+  tx_fifo->client_session_index = session->session_index;
+
+  session->rx_fifo = rx_fifo;
+  session->tx_fifo = tx_fifo;
+
+  /* session->transport needed by app_send_dgram */
+  clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
+                   sizeof (ip46_address_t));
+  session->transport.is_ip4 = mp->rmt.is_ip4;
+  session->transport.rmt_port = mp->rmt.port;
+  clib_memcpy_fast (&session->transport.lcl_ip, &em->uri_elts.ip,
+                   sizeof (ip46_address_t));
+  session->transport.lcl_port = em->uri_elts.port;
+
+  session->vpp_session_handle = mp->handle;
+  session->start = clib_time_now (&em->clib_time);
+  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
+                                        svm_msg_q_t *);
+  if (!(ls = echo_get_session_from_handle (em, mp->listener_handle)))
+    return;
+  session->listener_index = ls->session_index;
+
+  /* Add it to lookup table */
+  ECHO_LOG (1, "Accepted session 0x%lx -> 0x%lx", mp->handle,
+           mp->listener_handle);
+  echo_session_handle_add_del (em, mp->handle, session->session_index);
+
+  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);
+  em->proto_cb_vft->accepted_cb (mp, session);
+}
+
+static void
+session_connected_handler (session_connected_msg_t * mp)
+{
+  echo_main_t *em = &echo_main;
+  echo_session_t *session;
+  u32 listener_index = htonl (mp->context);
+  svm_fifo_t *rx_fifo, *tx_fifo;
+
+  if (mp->retval)
+    {
+      ECHO_FAIL ("connection failed with code: %U", format_api_error,
+                clib_net_to_host_u32 (mp->retval));
+      return;
+    }
+
+  session = echo_session_new (em);
+  if (wait_for_segment_allocation (mp->segment_handle))
+    {
+      ECHO_FAIL ("wait_for_segment_allocation errored");
+      return;
+    }
+
+  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
+  rx_fifo->client_session_index = session->session_index;
+  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
+  tx_fifo->client_session_index = session->session_index;
+
+  session->rx_fifo = rx_fifo;
+  session->tx_fifo = tx_fifo;
+  session->vpp_session_handle = mp->handle;
+  session->start = clib_time_now (&em->clib_time);
+  session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
+                                        svm_msg_q_t *);
+  session->listener_index = listener_index;
+  /* session->transport needed by app_send_dgram */
+  clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
+                   sizeof (ip46_address_t));
+  session->transport.is_ip4 = mp->lcl.is_ip4;
+  session->transport.lcl_port = mp->lcl.port;
+  clib_memcpy_fast (&session->transport.rmt_ip, &em->uri_elts.ip,
+                   sizeof (ip46_address_t));
+  session->transport.rmt_port = em->uri_elts.port;
+
+  echo_session_handle_add_del (em, mp->handle, session->session_index);
+  em->proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
+                                 session->session_index, 0 /* is_failed */ );
+}
+
+/*
+ *
+ *  End of ECHO callback definitions
+ *
+ */
+
+static void
+session_disconnected_handler (session_disconnected_msg_t * mp)
+{
+  app_session_evt_t _app_evt, *app_evt = &_app_evt;
+  session_disconnected_reply_msg_t *rmp;
+  echo_main_t *em = &echo_main;
+  echo_session_t *s;
+  ECHO_LOG (1, "passive close session 0x%lx", mp->handle);
+  if (!(s = echo_get_session_from_handle (em, mp->handle)))
+    return;
+  em->proto_cb_vft->disconnected_cb (mp, s);
+
+  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
+                            SESSION_CTRL_EVT_DISCONNECTED_REPLY);
+  rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
+  rmp->retval = 0;
+  rmp->handle = mp->handle;
+  rmp->context = mp->context;
+  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
+}
+
+static void
+session_reset_handler (session_reset_msg_t * mp)
+{
+  app_session_evt_t _app_evt, *app_evt = &_app_evt;
+  echo_main_t *em = &echo_main;
+  session_reset_reply_msg_t *rmp;
+  echo_session_t *s = 0;
+  ECHO_LOG (1, "Reset session 0x%lx", mp->handle);
+  if (!(s = echo_get_session_from_handle (em, mp->handle)))
+    return;
+  em->proto_cb_vft->reset_cb (mp, s);
+
+  app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
+                            SESSION_CTRL_EVT_RESET_REPLY);
+  rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
+  rmp->retval = 0;
+  rmp->handle = mp->handle;
+  app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
+}
+
+static void
+handle_mq_event (session_event_t * e)
+{
+  switch (e->event_type)
+    {
+    case SESSION_CTRL_EVT_BOUND:
+      session_bound_handler ((session_bound_msg_t *) e->data);
+      break;
+    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;
+    case SESSION_IO_EVT_RX:
+      break;
+    default:
+      ECHO_LOG (0, "unhandled event %u", e->event_type);
+    }
+}
+
+static void
+echo_process_rpcs (echo_main_t * em)
+{
+  echo_rpc_msg_t *rpc;
+  svm_msg_q_msg_t msg;
+  while (em->state < STATE_DATA_DONE && !em->time_to_stop)
+    {
+      if (svm_msg_q_sub (em->rpc_msq_queue, &msg, SVM_Q_TIMEDWAIT, 1))
+       continue;
+      rpc = svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
+      ((echo_rpc_t) rpc->fp) (rpc->arg, rpc->opaque);
+      svm_msg_q_free_msg (em->rpc_msq_queue, &msg);
+    }
+}
+
+static void *
+echo_mq_thread_fn (void *arg)
+{
+  clib_mem_set_thread_index ();
+  echo_main_t *em = &echo_main;
+  session_event_t *e;
+  svm_msg_q_msg_t msg;
+  int rv;
+  wait_for_state_change (em, STATE_ATTACHED, 0);
+  if (em->state < STATE_ATTACHED || !em->our_event_queue)
+    {
+      ECHO_FAIL ("Application failed to attach");
+      pthread_exit (0);
+    }
+
+  while (1)
+    {
+      if (!(rv = svm_msg_q_sub (em->our_event_queue,
+                               &msg, SVM_Q_TIMEDWAIT, 1)))
+       {
+         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 (rv == ETIMEDOUT
+         && (em->time_to_stop || em->state == STATE_DETACHED))
+       break;
+    }
+  pthread_exit (0);
+}
+
+static void
+clients_run (echo_main_t * em)
+{
+  u64 i;
+  echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
+  for (i = 0; i < em->n_connects; i++)
+    echo_send_connect (em->uri, SESSION_INVALID_INDEX);
+  wait_for_state_change (em, STATE_READY, 0);
+  ECHO_LOG (1, "App is ready");
+  echo_process_rpcs (em);
+}
+
+static void
+server_run (echo_main_t * em)
+{
+  echo_send_listen (em);
+  wait_for_state_change (em, STATE_READY, 0);
+  ECHO_LOG (1, "App is ready");
+  echo_process_rpcs (em);
+  /* Cleanup */
+  echo_send_unbind (em);
+  if (wait_for_state_change (em, STATE_DISCONNECTED, TIMEOUT))
+    {
+      ECHO_FAIL ("Timeout waiting for state disconnected");
+      return;
+    }
+}
+
+static void
+print_usage_and_exit (void)
+{
+  echo_main_t *em = &echo_main;
+  int i;
+  fprintf (stderr,
+          "Usage: vpp_echo [socket-name SOCKET] [client|server] [uri URI] [OPTIONS]\n"
+          "Generates traffic and assert correct teardown of the QUIC hoststack\n"
+          "\n"
+          "  socket-name PATH    Specify the binary socket path to connect to VPP\n"
+          "  use-svm-api         Use SVM API to connect to VPP\n"
+          "  test-bytes[:assert] Check data correctness when receiving (assert fails on first error)\n"
+          "  fifo-size N         Use N Kb fifos\n"
+          "  rx-buf N            Use N Kb RX buffer\n"
+          "  tx-buf N            Use N Kb TX test buffer\n"
+          "  appns NAMESPACE     Use the namespace NAMESPACE\n"
+          "  all-scope           all-scope option\n"
+          "  local-scope         local-scope option\n"
+          "  global-scope        global-scope option\n"
+          "  secret SECRET       set namespace secret\n"
+          "  chroot prefix PATH  Use PATH as memory root path\n"
+          "  sclose=[Y|N|W]      When a stream is done,    pass[N] send[Y] or wait[W] for close\n"
+          "\n"
+          "  time START:END      Time between evts START & END, events being :\n"
+          "                       start - Start of the app\n"
+          "                       qconnect    - first Connection connect sent\n"
+          "                       qconnected  - last Connection connected\n"
+          "                       sconnect    - first Stream connect sent\n"
+          "                       sconnected  - last Stream got connected\n"
+          "                       lastbyte    - Last expected byte received\n"
+          "                       exit        - Exiting of the app\n"
+          "  json                Output global stats in json\n"
+          "  log=N               Set the log level to [0: no output, 1:errors, 2:log]\n"
+          "\n"
+          "  nclients N          Open N clients sending data\n"
+          "  nthreads N          Use N busy loop threads for data [in addition to main & msg queue]\n"
+          "  TX=1337[Kb|Mb|GB]   Send 1337 [K|M|G]bytes, use TX=RX to reflect the data\n"
+          "  RX=1337[Kb|Mb|GB]   Expect 1337 [K|M|G]bytes\n" "\n");
+  for (i = 0; i < TRANSPORT_N_PROTO; i++)
+    {
+      echo_proto_cb_vft_t *vft = em->available_proto_cb_vft[i];
+      if (vft && vft->print_usage_cb)
+       vft->print_usage_cb ();
+    }
+  fprintf (stderr, "\nDefault configuration is :\n"
+          " server nclients 1/1 RX=64Kb TX=RX\n"
+          " client nclients 1/1 RX=64Kb TX=64Kb\n");
+  exit (1);
+}
+
+static int
+echo_process_each_proto_opts (unformat_input_t * a)
+{
+  echo_main_t *em = &echo_main;
+  int i, rv;
+  for (i = 0; i < TRANSPORT_N_PROTO; i++)
+    {
+      echo_proto_cb_vft_t *vft = em->available_proto_cb_vft[i];
+      if (vft && vft->process_opts_cb)
+       if ((rv = vft->process_opts_cb (a)))
+         return rv;
+    }
+  return 0;
+}
+
+static void
+echo_set_each_proto_defaults_before_opts (echo_main_t * em)
+{
+  int i;
+  for (i = 0; i < TRANSPORT_N_PROTO; i++)
+    {
+      echo_proto_cb_vft_t *vft = em->available_proto_cb_vft[i];
+      if (vft && vft->set_defaults_before_opts_cb)
+       vft->set_defaults_before_opts_cb ();
+    }
+}
+
+void
+echo_process_opts (int argc, char **argv)
+{
+  echo_main_t *em = &echo_main;
+  unformat_input_t _argv, *a = &_argv;
+  u32 tmp;
+  u8 *chroot_prefix;
+  u8 *uri = 0;
+  u8 default_f_active;
+
+  unformat_init_command_line (a, argv);
+  while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
+    {
+      if (echo_process_each_proto_opts (a))
+       ;
+      else if (unformat (a, "chroot prefix %s", &chroot_prefix))
+       vl_set_memory_root_path ((char *) chroot_prefix);
+      else if (unformat (a, "uri %s", &uri))
+       em->uri = format (0, "%s%c", uri, 0);
+      else if (unformat (a, "server"))
+       em->i_am_master = 1;
+      else if (unformat (a, "client"))
+       em->i_am_master = 0;
+      else if (unformat (a, "test-bytes:assert"))
+       em->test_return_packets = RETURN_PACKETS_ASSERT;
+      else if (unformat (a, "test-bytes"))
+       em->test_return_packets = RETURN_PACKETS_LOG_WRONG;
+      else if (unformat (a, "socket-name %s", &em->socket_name))
+       ;
+      else if (unformat (a, "use-svm-api"))
+       em->use_sock_api = 0;
+      else if (unformat (a, "fifo-size %d", &tmp))
+       em->fifo_size = tmp << 10;
+      else if (unformat (a, "rx-buf %d", &tmp))
+       em->rx_buf_size = tmp << 10;
+      else if (unformat (a, "tx-buf %d", &tmp))
+       em->rx_buf_size = tmp << 10;
+      else if (unformat (a, "nclients %d", &em->n_clients))
+       {
+         em->n_sessions = em->n_clients + 1;
+         em->n_connects = em->n_clients;
+       }
+      else if (unformat (a, "nthreads %d", &em->n_rx_threads))
+       ;
+      else if (unformat (a, "appns %_%v%_", &em->appns_id))
+       ;
+      else if (unformat (a, "all-scope"))
+       em->appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
+                           | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
+      else if (unformat (a, "local-scope"))
+       em->appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
+      else if (unformat (a, "global-scope"))
+       em->appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
+      else if (unformat (a, "secret %lu", &em->appns_secret))
+       ;
+      else if (unformat (a, "TX=RX"))
+       em->data_source = ECHO_RX_DATA_SOURCE;
+      else if (unformat (a, "TX=%U", unformat_data, &em->bytes_to_send))
+       ;
+      else if (unformat (a, "RX=%U", unformat_data, &em->bytes_to_receive))
+       ;
+      else if (unformat (a, "json"))
+       em->output_json = 1;
+      else if (unformat (a, "log=%d", &em->log_lvl))
+       ;
+      else if (unformat (a, "sclose=%U",
+                        echo_unformat_close, &em->send_stream_disconnects))
+       ;
+      else if (unformat (a, "time %U:%U",
+                        echo_unformat_timing_event, &em->timing.start_event,
+                        echo_unformat_timing_event, &em->timing.end_event))
+       ;
+      else
+       print_usage_and_exit ();
+    }
+
+  /* setting default for unset values
+   *
+   * bytes_to_send / bytes_to_receive & data_source  */
+  if (em->bytes_to_receive == (u64) ~ 0)
+    em->bytes_to_receive = 64 << 10;   /* default */
+  if (em->bytes_to_send == (u64) ~ 0)
+    em->bytes_to_send = 64 << 10;      /* default */
+  else if (em->bytes_to_send == 0)
+    em->data_source = ECHO_NO_DATA_SOURCE;
+  else
+    em->data_source = ECHO_TEST_DATA_SOURCE;
+
+  if (em->data_source == ECHO_INVALID_DATA_SOURCE)
+    em->data_source =
+      em->i_am_master ? ECHO_RX_DATA_SOURCE : ECHO_TEST_DATA_SOURCE;
+  if (em->data_source == ECHO_RX_DATA_SOURCE)
+    em->bytes_to_send = em->bytes_to_receive;
+
+  /* disconnect flags  */
+  if (em->i_am_master)
+    default_f_active =
+      em->bytes_to_send == 0 ? ECHO_CLOSE_F_ACTIVE : ECHO_CLOSE_F_PASSIVE;
+  else
+    default_f_active =
+      em->bytes_to_receive == 0 ? ECHO_CLOSE_F_PASSIVE : ECHO_CLOSE_F_ACTIVE;
+  if (em->send_stream_disconnects == ECHO_CLOSE_F_INVALID)
+    em->send_stream_disconnects = default_f_active;
+}
+
+void
+echo_process_uri (echo_main_t * em)
+{
+  unformat_input_t _input, *input = &_input;
+  u32 port;
+  unformat_init_string (input, (char *) em->uri, strlen ((char *) em->uri));
+  if (unformat
+      (input, "%U://%U/%d", unformat_transport_proto,
+       &em->uri_elts.transport_proto, unformat_ip4_address,
+       &em->uri_elts.ip.ip4, &port))
+    em->uri_elts.is_ip4 = 1;
+  else
+    if (unformat
+       (input, "%U://%U/%d", unformat_transport_proto,
+        &em->uri_elts.transport_proto, unformat_ip6_address,
+        &em->uri_elts.ip.ip6, &port))
+    em->uri_elts.is_ip4 = 0;
+  else
+    ECHO_FAIL ("Unable to process uri");
+  em->uri_elts.port = clib_host_to_net_u16 (port);
+  unformat_free (input);
+}
+
+static void __clib_constructor
+vpp_echo_init ()
+{
+  /* init memory before proto register themselves */
+  echo_main_t *em = &echo_main;
+  clib_mem_init_thread_safe (0, 256 << 20);
+  clib_memset (em, 0, sizeof (*em));
+}
+
+int
+main (int argc, char **argv)
+{
+  echo_main_t *em = &echo_main;
+  fifo_segment_main_t *sm = &em->segment_main;
+  char *app_name;
+  u64 i;
+  svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
+  u32 rpc_queue_size = 64 << 10;
+
+  em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
+  clib_spinlock_init (&em->sid_vpp_handles_lock);
+  em->shared_segment_handles = hash_create (0, sizeof (uword));
+  clib_spinlock_init (&em->segment_handles_lock);
+  em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
+  em->use_sock_api = 1;
+  em->fifo_size = 64 << 10;
+  em->n_clients = 1;
+  em->n_connects = 1;
+  em->n_sessions = 2;
+  em->max_test_msg = 50;
+  em->time_to_stop = 0;
+  em->i_am_master = 1;
+  em->n_rx_threads = 4;
+  em->test_return_packets = RETURN_PACKETS_NOTEST;
+  em->timing.start_event = ECHO_EVT_FIRST_QCONNECT;
+  em->timing.end_event = ECHO_EVT_LAST_BYTE;
+  em->bytes_to_receive = ~0;   /* defaulted when we know if server/client */
+  em->bytes_to_send = ~0;      /* defaulted when we know if server/client */
+  em->rx_buf_size = 1 << 20;
+  em->tx_buf_size = 1 << 20;
+  em->data_source = ECHO_INVALID_DATA_SOURCE;
+  em->uri = format (0, "%s%c", "tcp://0.0.0.0/1234", 0);
+  echo_set_each_proto_defaults_before_opts (em);
+  echo_process_opts (argc, argv);
+  echo_process_uri (em);
+  em->proto_cb_vft = em->available_proto_cb_vft[em->uri_elts.transport_proto];
+  if (!em->proto_cb_vft)
+    {
+      ECHO_FAIL ("Protocol %U is not supported",
+                format_transport_proto, em->uri_elts.transport_proto);
+      exit (1);
+    }
+  if (em->proto_cb_vft->set_defaults_after_opts_cb)
+    em->proto_cb_vft->set_defaults_after_opts_cb ();
+
+  vec_validate (em->data_thread_handles, em->n_rx_threads);
+  vec_validate (em->data_thread_args, em->n_clients);
+  for (i = 0; i < em->n_clients; i++)
+    em->data_thread_args[i] = SESSION_INVALID_INDEX;
+  clib_time_init (&em->clib_time);
+  init_error_string_table ();
+  fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
+  vec_validate (em->connect_test_data, em->tx_buf_size);
+  for (i = 0; i < em->tx_buf_size; i++)
+    em->connect_test_data[i] = i & 0xff;
+
+  /* *INDENT-OFF* */
+  svm_msg_q_ring_cfg_t rc[1] = {
+    {rpc_queue_size, sizeof (echo_rpc_msg_t), 0},
+  };
+  /* *INDENT-ON* */
+  cfg->consumer_pid = getpid ();
+  cfg->n_rings = 1;
+  cfg->q_nitems = rpc_queue_size;
+  cfg->ring_cfgs = rc;
+  em->rpc_msq_queue = svm_msg_q_alloc (cfg);
+
+  signal (SIGINT, stop_signal);
+  signal (SIGQUIT, stop_signal);
+  signal (SIGTERM, stop_signal);
+  echo_api_hookup (em);
+
+  app_name = em->i_am_master ? "echo_server" : "echo_client";
+  if (connect_to_vpp (app_name))
+    {
+      svm_region_exit ();
+      ECHO_FAIL ("Couldn't connect to vpe, exiting...\n");
+      exit (1);
+    }
+
+  echo_session_prealloc (em);
+  echo_notify_event (em, ECHO_EVT_START);
+
+  echo_send_attach (em);
+  if (wait_for_state_change (em, STATE_ATTACHED, TIMEOUT))
+    {
+      ECHO_FAIL ("Couldn't attach to vpp, did you run <session enable> ?\n");
+      exit (1);
+    }
+  if (pthread_create (&em->mq_thread_handle,
+                     NULL /*attr */ , echo_mq_thread_fn, 0))
+    {
+      ECHO_FAIL ("pthread create errored\n");
+      exit (1);
+    }
+  for (i = 0; i < em->n_rx_threads; i++)
+    if (pthread_create (&em->data_thread_handles[i],
+                       NULL /*attr */ , echo_data_thread_fn, (void *) i))
+      {
+       ECHO_FAIL ("pthread create errored\n");
+       exit (1);
+      }
+  if (em->i_am_master)
+    server_run (em);
+  else
+    clients_run (em);
+  echo_notify_event (em, ECHO_EVT_EXIT);
+  if (em->output_json)
+    print_global_json_stats (em);
+  else
+    print_global_stats (em);
+  echo_free_sessions (em);
+  echo_assert_test_suceeded (em);
+  echo_send_detach (em);
+  if (wait_for_state_change (em, STATE_DETACHED, TIMEOUT))
+    {
+      ECHO_FAIL ("ECHO-ERROR: Couldn't detach from vpp, exiting...\n");
+      exit (1);
+    }
+  int *rv;
+  pthread_join (em->mq_thread_handle, (void **) &rv);
+  if (rv)
+    {
+      ECHO_FAIL ("mq pthread errored %d", rv);
+      exit (1);
+    }
+  if (em->use_sock_api)
+    vl_socket_client_disconnect ();
+  else
+    vl_client_disconnect_from_vlib ();
+  ECHO_LOG (0, "Test complete !\n");
+  exit (em->has_failed);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/hs_apps/sapi/vpp_echo_bapi.c b/src/plugins/hs_apps/sapi/vpp_echo_bapi.c
new file mode 100644 (file)
index 0000000..dafcd8a
--- /dev/null
@@ -0,0 +1,439 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <signal.h>
+
+#include <hs_apps/sapi/vpp_echo_common.h>
+
+/*
+ *
+ *  Binary API Messages
+ *
+ */
+
+void
+echo_send_attach (echo_main_t * em)
+{
+  vl_api_application_attach_t *bmp;
+  vl_api_application_tls_cert_add_t *cert_mp;
+  vl_api_application_tls_key_add_t *key_mp;
+
+  bmp = vl_msg_api_alloc (sizeof (*bmp));
+  clib_memset (bmp, 0, sizeof (*bmp));
+
+  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;
+  bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
+  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;
+  bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
+  bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
+  bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
+  if (em->appns_id)
+    {
+      bmp->namespace_id_len = vec_len (em->appns_id);
+      clib_memcpy_fast (bmp->namespace_id, em->appns_id,
+                       bmp->namespace_id_len);
+      bmp->options[APP_OPTIONS_FLAGS] |= em->appns_flags;
+      bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = em->appns_secret;
+    }
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
+
+  cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
+  clib_memset (cert_mp, 0, sizeof (*cert_mp));
+  cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
+  cert_mp->client_index = em->my_client_index;
+  cert_mp->context = ntohl (0xfeedface);
+  cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
+  clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
+
+  key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
+  clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
+  key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
+  key_mp->client_index = em->my_client_index;
+  key_mp->context = ntohl (0xfeedface);
+  key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
+  clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
+}
+
+void
+echo_send_detach (echo_main_t * em)
+{
+  vl_api_application_detach_t *bmp;
+  bmp = vl_msg_api_alloc (sizeof (*bmp));
+  clib_memset (bmp, 0, sizeof (*bmp));
+
+  bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
+  bmp->client_index = em->my_client_index;
+  bmp->context = ntohl (0xfeedface);
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
+}
+
+void
+echo_send_listen (echo_main_t * em)
+{
+  vl_api_bind_uri_t *bmp;
+  bmp = vl_msg_api_alloc (sizeof (*bmp));
+  clib_memset (bmp, 0, sizeof (*bmp));
+
+  bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
+  bmp->client_index = em->my_client_index;
+  bmp->context = ntohl (0xfeedface);
+  memcpy (bmp->uri, em->uri, vec_len (em->uri));
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
+}
+
+void
+echo_send_unbind (echo_main_t * em)
+{
+  vl_api_unbind_uri_t *ump;
+
+  ump = vl_msg_api_alloc (sizeof (*ump));
+  clib_memset (ump, 0, sizeof (*ump));
+
+  ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
+  ump->client_index = em->my_client_index;
+  memcpy (ump->uri, em->uri, vec_len (em->uri));
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
+}
+
+void
+echo_send_connect (u8 * uri, u32 opaque)
+{
+  echo_main_t *em = &echo_main;
+  vl_api_connect_uri_t *cmp;
+  cmp = vl_msg_api_alloc (sizeof (*cmp));
+  clib_memset (cmp, 0, sizeof (*cmp));
+  cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
+  cmp->client_index = em->my_client_index;
+  cmp->context = ntohl (opaque);
+  memcpy (cmp->uri, uri, vec_len (uri));
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
+}
+
+void
+echo_send_disconnect_session (u64 handle, u32 opaque)
+{
+  echo_main_t *em = &echo_main;
+  vl_api_disconnect_session_t *dmp;
+  dmp = vl_msg_api_alloc (sizeof (*dmp));
+  clib_memset (dmp, 0, sizeof (*dmp));
+  dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
+  dmp->client_index = em->my_client_index;
+  dmp->handle = handle;
+  ECHO_LOG (1, "Disconnect session 0x%lx", dmp->handle);
+  vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
+}
+
+/*
+ *
+ *  Helpers
+ *
+ */
+
+static int
+ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
+{
+  fifo_segment_create_args_t _a, *a = &_a;
+  fifo_segment_main_t *sm = &echo_main.segment_main;
+  int rv;
+
+  clib_memset (a, 0, sizeof (*a));
+  a->segment_name = (char *) name;
+  a->segment_type = type;
+
+  if (type == SSVM_SEGMENT_MEMFD)
+    a->memfd_fd = fd;
+
+  if ((rv = fifo_segment_attach (sm, a)))
+    return rv;
+  vec_reset_length (a->new_segment_indices);
+  return 0;
+}
+
+static inline void
+echo_segment_handle_add_del (echo_main_t * em, u64 segment_handle, u8 add)
+{
+  clib_spinlock_lock (&em->segment_handles_lock);
+  if (add)
+    hash_set (em->shared_segment_handles, segment_handle, 1);
+  else
+    hash_unset (em->shared_segment_handles, segment_handle);
+  clib_spinlock_unlock (&em->segment_handles_lock);
+}
+
+/*
+ *
+ *  Binary API callbacks
+ *
+ */
+
+static void
+vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
+                                          mp)
+{
+  echo_main_t *em = &echo_main;
+  int *fds = 0, i;
+  u32 n_fds = 0;
+  u64 segment_handle;
+  segment_handle = clib_net_to_host_u64 (mp->segment_handle);
+  ECHO_LOG (1, "Attached returned app %u", htons (mp->app_index));
+
+  if (mp->retval)
+    {
+      ECHO_FAIL ("attach failed: %U", format_api_error,
+                clib_net_to_host_u32 (mp->retval));
+      return;
+    }
+
+  if (mp->segment_name_length == 0)
+    {
+      ECHO_FAIL ("segment_name_length zero");
+      return;
+    }
+
+  ASSERT (mp->app_event_queue_address);
+  em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
+                                         svm_msg_q_t *);
+
+  if (mp->n_fds)
+    {
+      vec_validate (fds, mp->n_fds);
+      if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
+       {
+         ECHO_FAIL ("vl_socket_client_recv_fd_msg failed");
+         goto failed;
+       }
+
+      if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
+       if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
+         {
+           ECHO_FAIL ("svm_fifo_segment_attach failed");
+           goto failed;
+         }
+
+      if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
+       if (ssvm_segment_attach ((char *) mp->segment_name,
+                                SSVM_SEGMENT_MEMFD, fds[n_fds++]))
+         {
+           ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed",
+                      mp->segment_name);
+           goto failed;
+         }
+      if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
+       svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
+
+      vec_free (fds);
+    }
+  else
+    {
+      if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
+                              -1))
+       {
+         ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed",
+                    mp->segment_name);
+         return;
+       }
+    }
+  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
+  ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
+
+  em->state = STATE_ATTACHED;
+  return;
+failed:
+  for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
+    close (fds[i]);
+  vec_free (fds);
+}
+
+static void
+vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
+                                          mp)
+{
+  if (mp->retval)
+    {
+      ECHO_FAIL ("detach returned with err: %d", mp->retval);
+      return;
+    }
+  echo_main.state = STATE_DETACHED;
+}
+
+
+static void
+vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
+{
+  echo_main_t *em = &echo_main;
+  u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
+  echo_segment_handle_add_del (em, segment_handle, 0 /* add */ );
+  ECHO_LOG (1, "Unmaped segment 0x%lx", segment_handle);
+}
+
+static void
+vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
+{
+  fifo_segment_main_t *sm = &echo_main.segment_main;
+  fifo_segment_create_args_t _a, *a = &_a;
+  echo_main_t *em = &echo_main;
+  int *fds = 0, i;
+  char *seg_name = (char *) mp->segment_name;
+  u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
+
+  if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
+    {
+      vec_validate (fds, 1);
+      if (vl_socket_client_recv_fd_msg (fds, 1, 5))
+       {
+         ECHO_FAIL ("vl_socket_client_recv_fd_msg failed");
+         goto failed;
+       }
+
+      if (ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
+       {
+         ECHO_FAIL ("svm_fifo_segment_attach ('%s')"
+                    "failed on SSVM_SEGMENT_MEMFD", seg_name);
+         goto failed;
+       }
+      vec_free (fds);
+    }
+  else
+    {
+      clib_memset (a, 0, sizeof (*a));
+      a->segment_name = seg_name;
+      a->segment_size = mp->segment_size;
+      /* Attach to the segment vpp created */
+      if (fifo_segment_attach (sm, a))
+       {
+         ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed", seg_name);
+         goto failed;
+       }
+    }
+  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
+  ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
+  return;
+
+failed:
+  for (i = 0; i < vec_len (fds); i++)
+    close (fds[i]);
+  vec_free (fds);
+}
+
+static void
+vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
+{
+  if (mp->retval)
+    {
+      ECHO_FAIL ("bind failed: %U", format_api_error,
+                clib_net_to_host_u32 (mp->retval));
+    }
+}
+
+static void
+vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
+{
+  echo_session_t *listen_session;
+  echo_main_t *em = &echo_main;
+  if (mp->retval != 0)
+    {
+      ECHO_FAIL ("returned %d", ntohl (mp->retval));
+      return;
+    }
+  listen_session = pool_elt_at_index (em->sessions, em->listen_session_index);
+  em->proto_cb_vft->cleanup_cb (listen_session, 0 /* parent_died */ );
+  em->state = STATE_DISCONNECTED;
+}
+
+static void
+vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
+                                          mp)
+{
+  echo_main_t *em = &echo_main;
+  echo_session_t *s;
+
+  if (mp->retval)
+    {
+      ECHO_FAIL ("vpp complained about disconnect: %d", ntohl (mp->retval));
+      return;
+    }
+
+  ECHO_LOG (1, "Got disonnected reply for session 0x%lx", mp->handle);
+  if (!(s = echo_get_session_from_handle (em, mp->handle)))
+    return;
+  em->proto_cb_vft->disconnected_reply_cb (s);
+}
+
+static void
+  vl_api_application_tls_cert_add_reply_t_handler
+  (vl_api_application_tls_cert_add_reply_t * mp)
+{
+  if (mp->retval)
+    ECHO_FAIL ("failed to add tls cert");
+}
+
+static void
+  vl_api_application_tls_key_add_reply_t_handler
+  (vl_api_application_tls_key_add_reply_t * mp)
+{
+  if (mp->retval)
+    ECHO_FAIL ("failed to add tls key");
+}
+
+static void
+vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
+{
+  echo_main_t *em = &echo_main;
+  if (mp->retval && (em->proto_cb_vft->connected_cb))
+    em->proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
+                                   mp->context, 1 /* is_failed */ );
+}
+
+#define foreach_quic_echo_msg                                           \
+_(BIND_URI_REPLY, bind_uri_reply)                                       \
+_(UNBIND_URI_REPLY, unbind_uri_reply)                                   \
+_(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
+_(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
+_(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
+_(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
+_(UNMAP_SEGMENT, unmap_segment)                                         \
+_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
+_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
+_(CONNECT_URI_REPLY, connect_uri_reply)         \
+
+void
+echo_api_hookup (echo_main_t * em)
+{
+#define _(N,n)                                                  \
+    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
+                           vl_api_##n##_t_handler,              \
+                           vl_noop_handler,                     \
+                           vl_api_##n##_t_endian,               \
+                           vl_api_##n##_t_print,                \
+                           sizeof(vl_api_##n##_t), 1);
+  foreach_quic_echo_msg;
+#undef _
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/hs_apps/sapi/vpp_echo_common.c b/src/plugins/hs_apps/sapi/vpp_echo_common.c
new file mode 100644 (file)
index 0000000..013affd
--- /dev/null
@@ -0,0 +1,562 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <signal.h>
+
+#include <hs_apps/sapi/vpp_echo_common.h>
+
+/*
+ *
+ *  Format functions
+ *
+ */
+
+u8 *
+format_ip4_address (u8 * s, va_list * args)
+{
+  u8 *a = va_arg (*args, u8 *);
+  return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
+}
+
+u8 *
+format_ip6_address (u8 * s, va_list * args)
+{
+  ip6_address_t *a = va_arg (*args, ip6_address_t *);
+  u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
+
+  i_max_n_zero = ARRAY_LEN (a->as_u16);
+  max_n_zeros = 0;
+  i_first_zero = i_max_n_zero;
+  n_zeros = 0;
+  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
+    {
+      u32 is_zero = a->as_u16[i] == 0;
+      if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
+       {
+         i_first_zero = i;
+         n_zeros = 0;
+       }
+      n_zeros += is_zero;
+      if ((!is_zero && n_zeros > max_n_zeros)
+         || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
+       {
+         i_max_n_zero = i_first_zero;
+         max_n_zeros = n_zeros;
+         i_first_zero = ARRAY_LEN (a->as_u16);
+         n_zeros = 0;
+       }
+    }
+
+  last_double_colon = 0;
+  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
+    {
+      if (i == i_max_n_zero && max_n_zeros > 1)
+       {
+         s = format (s, "::");
+         i += max_n_zeros - 1;
+         last_double_colon = 1;
+       }
+      else
+       {
+         s = format (s, "%s%x",
+                     (last_double_colon || i == 0) ? "" : ":",
+                     clib_net_to_host_u16 (a->as_u16[i]));
+         last_double_colon = 0;
+       }
+    }
+
+  return s;
+}
+
+/* Format an IP46 address. */
+u8 *
+format_ip46_address (u8 * s, va_list * args)
+{
+  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
+  ip46_type_t type = va_arg (*args, ip46_type_t);
+  int is_ip4 = 1;
+
+  switch (type)
+    {
+    case IP46_TYPE_ANY:
+      is_ip4 = ip46_address_is_ip4 (ip46);
+      break;
+    case IP46_TYPE_IP4:
+      is_ip4 = 1;
+      break;
+    case IP46_TYPE_IP6:
+      is_ip4 = 0;
+      break;
+    }
+
+  return is_ip4 ?
+    format (s, "%U", format_ip4_address, &ip46->ip4) :
+    format (s, "%U", format_ip6_address, &ip46->ip6);
+}
+
+uword
+unformat_data (unformat_input_t * input, va_list * args)
+{
+  u64 _a;
+  u64 *a = va_arg (*args, u64 *);
+  if (unformat (input, "%lluGb", &_a))
+    *a = _a << 30;
+  else if (unformat (input, "%lluG", &_a))
+    *a = _a << 30;
+  else if (unformat (input, "%lluMb", &_a))
+    *a = _a << 20;
+  else if (unformat (input, "%lluM", &_a))
+    *a = _a << 20;
+  else if (unformat (input, "%lluKb", &_a))
+    *a = _a << 10;
+  else if (unformat (input, "%lluK", &_a))
+    *a = _a << 10;
+  else if (unformat (input, "%llu", a))
+    ;
+  else
+    return 0;
+  return 1;
+}
+
+u8 *
+format_api_error (u8 * s, va_list * args)
+{
+  echo_main_t *em = &echo_main;
+  i32 error = va_arg (*args, u32);
+  uword *p;
+
+  p = hash_get (em->error_string_by_error_number, -error);
+
+  if (p)
+    s = format (s, "%s", p[0]);
+  else
+    s = format (s, "%d", error);
+  return s;
+}
+
+void
+init_error_string_table ()
+{
+  echo_main_t *em = &echo_main;
+  em->error_string_by_error_number = hash_create (0, sizeof (uword));
+
+#define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
+  foreach_vnet_api_error;
+#undef _
+
+  hash_set (em->error_string_by_error_number, 99, "Misc");
+}
+
+u8 *
+echo_format_app_state (u8 * s, va_list * args)
+{
+  u32 state = va_arg (*args, u32);
+  if (state == STATE_START)
+    return format (s, "STATE_START");
+  if (state == STATE_ATTACHED)
+    return format (s, "STATE_ATTACHED");
+  if (state == STATE_LISTEN)
+    return format (s, "STATE_LISTEN");
+  if (state == STATE_READY)
+    return format (s, "STATE_READY");
+  if (state == STATE_DATA_DONE)
+    return format (s, "STATE_DATA_DONE");
+  if (state == STATE_DISCONNECTED)
+    return format (s, "STATE_DISCONNECTED");
+  if (state == STATE_DETACHED)
+    return format (s, "STATE_DETACHED");
+  else
+    return format (s, "unknown state");
+}
+
+uword
+echo_unformat_close (unformat_input_t * input, va_list * args)
+{
+  u8 *a = va_arg (*args, u8 *);
+  if (unformat (input, "Y"))
+    *a = ECHO_CLOSE_F_ACTIVE;
+  else if (unformat (input, "N"))
+    *a = ECHO_CLOSE_F_NONE;
+  else if (unformat (input, "W"))
+    *a = ECHO_CLOSE_F_PASSIVE;
+  else
+    return 0;
+  return 1;
+}
+
+uword
+echo_unformat_timing_event (unformat_input_t * input, va_list * args)
+{
+  u8 *a = va_arg (*args, u8 *);
+  if (unformat (input, "start"))
+    *a = ECHO_EVT_START;
+  else if (unformat (input, "qconnected"))
+    *a = ECHO_EVT_LAST_QCONNECTED;
+  else if (unformat (input, "qconnect"))
+    *a = ECHO_EVT_FIRST_QCONNECT;
+  else if (unformat (input, "sconnected"))
+    *a = ECHO_EVT_LAST_SCONNECTED;
+  else if (unformat (input, "sconnect"))
+    *a = ECHO_EVT_FIRST_SCONNECT;
+  else if (unformat (input, "lastbyte"))
+    *a = ECHO_EVT_LAST_BYTE;
+  else if (unformat (input, "exit"))
+    *a = ECHO_EVT_EXIT;
+  else
+    return 0;
+  return 1;
+}
+
+u8 *
+echo_format_timing_event (u8 * s, va_list * args)
+{
+  u32 timing_event = va_arg (*args, u32);
+  if (timing_event == ECHO_EVT_START)
+    return format (s, "start");
+  if (timing_event == ECHO_EVT_FIRST_QCONNECT)
+    return format (s, "qconnect");
+  if (timing_event == ECHO_EVT_LAST_QCONNECTED)
+    return format (s, "qconnected");
+  if (timing_event == ECHO_EVT_FIRST_SCONNECT)
+    return format (s, "sconnect");
+  if (timing_event == ECHO_EVT_LAST_SCONNECTED)
+    return format (s, "sconnected");
+  if (timing_event == ECHO_EVT_LAST_BYTE)
+    return format (s, "lastbyte");
+  if (timing_event == ECHO_EVT_EXIT)
+    return format (s, "exit");
+  else
+    return format (s, "unknown timing event");
+}
+
+uword
+unformat_transport_proto (unformat_input_t * input, va_list * args)
+{
+  u32 *proto = va_arg (*args, u32 *);
+  if (unformat (input, "tcp"))
+    *proto = TRANSPORT_PROTO_TCP;
+  else if (unformat (input, "TCP"))
+    *proto = TRANSPORT_PROTO_TCP;
+  else if (unformat (input, "udpc"))
+    *proto = TRANSPORT_PROTO_UDPC;
+  else if (unformat (input, "UDPC"))
+    *proto = TRANSPORT_PROTO_UDPC;
+  else if (unformat (input, "udp"))
+    *proto = TRANSPORT_PROTO_UDP;
+  else if (unformat (input, "UDP"))
+    *proto = TRANSPORT_PROTO_UDP;
+  else if (unformat (input, "sctp"))
+    *proto = TRANSPORT_PROTO_SCTP;
+  else if (unformat (input, "SCTP"))
+    *proto = TRANSPORT_PROTO_SCTP;
+  else if (unformat (input, "tls"))
+    *proto = TRANSPORT_PROTO_TLS;
+  else if (unformat (input, "TLS"))
+    *proto = TRANSPORT_PROTO_TLS;
+  else if (unformat (input, "quic"))
+    *proto = TRANSPORT_PROTO_QUIC;
+  else if (unformat (input, "QUIC"))
+    *proto = TRANSPORT_PROTO_QUIC;
+  else
+    return 0;
+  return 1;
+}
+
+u8 *
+format_transport_proto (u8 * s, va_list * args)
+{
+  u32 transport_proto = va_arg (*args, u32);
+  switch (transport_proto)
+    {
+    case TRANSPORT_PROTO_TCP:
+      s = format (s, "TCP");
+      break;
+    case TRANSPORT_PROTO_UDP:
+      s = format (s, "UDP");
+      break;
+    case TRANSPORT_PROTO_SCTP:
+      s = format (s, "SCTP");
+      break;
+    case TRANSPORT_PROTO_NONE:
+      s = format (s, "NONE");
+      break;
+    case TRANSPORT_PROTO_TLS:
+      s = format (s, "TLS");
+      break;
+    case TRANSPORT_PROTO_UDPC:
+      s = format (s, "UDPC");
+      break;
+    case TRANSPORT_PROTO_QUIC:
+      s = format (s, "QUIC");
+      break;
+    default:
+      s = format (s, "UNKNOWN");
+      break;
+    }
+  return s;
+}
+
+uword
+unformat_ip4_address (unformat_input_t * input, va_list * args)
+{
+  u8 *result = va_arg (*args, u8 *);
+  unsigned a[4];
+
+  if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
+    return 0;
+
+  if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
+    return 0;
+
+  result[0] = a[0];
+  result[1] = a[1];
+  result[2] = a[2];
+  result[3] = a[3];
+
+  return 1;
+}
+
+uword
+unformat_ip6_address (unformat_input_t * input, va_list * args)
+{
+  ip6_address_t *result = va_arg (*args, ip6_address_t *);
+  u16 hex_quads[8];
+  uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
+  uword c, n_colon, double_colon_index;
+
+  n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
+  double_colon_index = ARRAY_LEN (hex_quads);
+  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
+    {
+      hex_digit = 16;
+      if (c >= '0' && c <= '9')
+       hex_digit = c - '0';
+      else if (c >= 'a' && c <= 'f')
+       hex_digit = c + 10 - 'a';
+      else if (c >= 'A' && c <= 'F')
+       hex_digit = c + 10 - 'A';
+      else if (c == ':' && n_colon < 2)
+       n_colon++;
+      else
+       {
+         unformat_put_input (input);
+         break;
+       }
+
+      /* Too many hex quads. */
+      if (n_hex_quads >= ARRAY_LEN (hex_quads))
+       return 0;
+
+      if (hex_digit < 16)
+       {
+         hex_quad = (hex_quad << 4) | hex_digit;
+
+         /* Hex quad must fit in 16 bits. */
+         if (n_hex_digits >= 4)
+           return 0;
+
+         n_colon = 0;
+         n_hex_digits++;
+       }
+
+      /* Save position of :: */
+      if (n_colon == 2)
+       {
+         /* More than one :: ? */
+         if (double_colon_index < ARRAY_LEN (hex_quads))
+           return 0;
+         double_colon_index = n_hex_quads;
+       }
+
+      if (n_colon > 0 && n_hex_digits > 0)
+       {
+         hex_quads[n_hex_quads++] = hex_quad;
+         hex_quad = 0;
+         n_hex_digits = 0;
+       }
+    }
+
+  if (n_hex_digits > 0)
+    hex_quads[n_hex_quads++] = hex_quad;
+
+  {
+    word i;
+
+    /* Expand :: to appropriate number of zero hex quads. */
+    if (double_colon_index < ARRAY_LEN (hex_quads))
+      {
+       word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
+
+       for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
+         hex_quads[n_zero + i] = hex_quads[i];
+
+       for (i = 0; i < n_zero; i++)
+         hex_quads[double_colon_index + i] = 0;
+
+       n_hex_quads = ARRAY_LEN (hex_quads);
+      }
+
+    /* Too few hex quads given. */
+    if (n_hex_quads < ARRAY_LEN (hex_quads))
+      return 0;
+
+    for (i = 0; i < ARRAY_LEN (hex_quads); i++)
+      result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
+
+    return 1;
+  }
+}
+
+/*
+ *
+ *  End of format functions
+ *
+ */
+
+void
+echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid)
+{
+  clib_spinlock_lock (&em->sid_vpp_handles_lock);
+  if (sid == SESSION_INVALID_INDEX)
+    hash_unset (em->session_index_by_vpp_handles, handle);
+  else
+    hash_set (em->session_index_by_vpp_handles, handle, sid);
+  clib_spinlock_unlock (&em->sid_vpp_handles_lock);
+}
+
+echo_session_t *
+echo_session_new (echo_main_t * em)
+{
+  /* thread safe new prealloced session */
+  return pool_elt_at_index (em->sessions,
+                           clib_atomic_fetch_add (&em->nxt_available_sidx,
+                                                  1));
+}
+
+int
+echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque)
+{
+  svm_msg_q_msg_t msg;
+  echo_rpc_msg_t *evt;
+  if (PREDICT_FALSE (svm_msg_q_lock (em->rpc_msq_queue)))
+    {
+      ECHO_LOG (1, "RPC lock failed");
+      return -1;
+    }
+  if (PREDICT_FALSE (svm_msg_q_ring_is_full (em->rpc_msq_queue, 0)))
+    {
+      svm_msg_q_unlock (em->rpc_msq_queue);
+      ECHO_LOG (1, "RPC ring is full");
+      return -2;
+    }
+  msg = svm_msg_q_alloc_msg_w_ring (em->rpc_msq_queue, 0);
+  if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (&msg)))
+    {
+      ECHO_LOG (1, "RPC msg is invalid");
+      svm_msg_q_unlock (em->rpc_msq_queue);
+      return -2;
+    }
+  evt = (echo_rpc_msg_t *) svm_msg_q_msg_data (em->rpc_msq_queue, &msg);
+  evt->arg = arg;
+  evt->opaque = opaque;
+  evt->fp = fp;
+
+  svm_msg_q_add_and_unlock (em->rpc_msq_queue, &msg);
+  return 0;
+}
+
+echo_session_t *
+echo_get_session_from_handle (echo_main_t * em, u64 handle)
+{
+  uword *p;
+  clib_spinlock_lock (&em->sid_vpp_handles_lock);
+  p = hash_get (em->session_index_by_vpp_handles, handle);
+  clib_spinlock_unlock (&em->sid_vpp_handles_lock);
+  if (!p)
+    {
+      ECHO_FAIL ("unknown handle 0x%lx", handle);
+      return 0;
+    }
+  return pool_elt_at_index (em->sessions, p[0]);
+}
+
+int
+wait_for_segment_allocation (u64 segment_handle)
+{
+  echo_main_t *em = &echo_main;
+  f64 timeout;
+  timeout = clib_time_now (&em->clib_time) + TIMEOUT;
+  uword *segment_present;
+  ECHO_LOG (1, "Waiting for segment 0x%lx...", segment_handle);
+  while (clib_time_now (&em->clib_time) < timeout)
+    {
+      clib_spinlock_lock (&em->segment_handles_lock);
+      segment_present = hash_get (em->shared_segment_handles, segment_handle);
+      clib_spinlock_unlock (&em->segment_handles_lock);
+      if (segment_present != 0)
+       return 0;
+      if (em->time_to_stop == 1)
+       return 0;
+    }
+  ECHO_LOG (1, "timeout wait_for_segment_allocation (0x%lx)", segment_handle);
+  return -1;
+}
+
+int
+wait_for_state_change (echo_main_t * em, connection_state_t state,
+                      f64 timeout)
+{
+  f64 end_time = clib_time_now (&em->clib_time) + timeout;
+  while (!timeout || clib_time_now (&em->clib_time) < end_time)
+    {
+      if (em->state == state)
+       return 0;
+      if (em->time_to_stop)
+       return 1;
+    }
+  ECHO_LOG (1, "timeout waiting for %U", echo_format_app_state, state);
+  return -1;
+}
+
+void
+echo_notify_event (echo_main_t * em, echo_test_evt_t e)
+{
+  if (em->timing.events_sent & e)
+    return;
+  if (em->timing.start_event == e)
+    em->timing.start_time = clib_time_now (&em->clib_time);
+  else if (em->timing.end_event == e)
+    em->timing.end_time = clib_time_now (&em->clib_time);
+  em->timing.events_sent |= e;
+}
+
+void
+echo_session_print_stats (echo_main_t * em, echo_session_t * session)
+{
+  f64 deltat = clib_time_now (&em->clib_time) - session->start;
+  ECHO_LOG (0, "Session 0x%x done in %.6fs RX[%.4f] TX[%.4f] Gbit/s\n",
+           session->vpp_session_handle, deltat,
+           (session->bytes_received * 8.0) / deltat / 1e9,
+           (session->bytes_sent * 8.0) / deltat / 1e9);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/hs_apps/sapi/vpp_echo_common.h b/src/plugins/hs_apps/sapi/vpp_echo_common.h
new file mode 100644 (file)
index 0000000..2f9d391
--- /dev/null
@@ -0,0 +1,321 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __included_vpp_echo_common_h__
+#define __included_vpp_echo_common_h__
+
+#include <vnet/session/application_interface.h>
+#include <vpp/api/vpe_msg_enum.h>
+
+#define vl_typedefs            /* define message structures */
+#include <vpp/api/vpe_all_api_h.h>
+#undef vl_typedefs
+
+/* declare message handlers for each api */
+
+#define vl_endianfun           /* define message structures */
+#include <vpp/api/vpe_all_api_h.h>
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...)
+#define vl_printfun
+#include <vpp/api/vpe_all_api_h.h>
+#undef vl_printfun
+
+#define TIMEOUT 10.0
+
+#define CHECK(expected, result, _fmt, _args...)         \
+    if (expected != result)                             \
+      ECHO_FAIL ("expected %d, got %d : " _fmt, expected, result, ##_args);
+
+#define ECHO_FAIL(_fmt,_args...)        \
+  {                                     \
+    echo_main_t *em = &echo_main;       \
+    em->has_failed = 1;                 \
+    em->time_to_stop = 1;               \
+    if (em->log_lvl > 0)                \
+         clib_warning ("ECHO-ERROR: "_fmt, ##_args);    \
+  }
+
+#define ECHO_LOG(lvl, _fmt,_args...)    \
+  {                                     \
+    echo_main_t *em = &echo_main;       \
+    if (em->log_lvl > lvl)              \
+         clib_warning (_fmt, ##_args);  \
+  }
+
+#define ECHO_REGISTER_PROTO(proto, vft)                 \
+  static void __clib_constructor                        \
+  vpp_echo_init_##proto ()                              \
+  {                                                     \
+    echo_main_t *em = &echo_main;        \
+    em->available_proto_cb_vft[proto] = &vft;          \
+  }
+
+typedef struct
+{
+  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+#define _(type, name) type name;
+  foreach_app_session_field
+#undef _
+  u64 vpp_session_handle;
+  u64 bytes_sent;
+  u64 bytes_to_send;
+  volatile u64 bytes_received;
+  volatile u64 bytes_to_receive;
+  f64 start;
+  u32 listener_index;          /* listener index in echo session pool */
+  u32 idle_cycles;             /* consecutive enq/deq with no data */
+  volatile u64 accepted_session_count; /* sessions we accepted (as a listener) */
+} echo_session_t;
+
+typedef enum
+{
+  ECHO_NO_DATA_SOURCE,
+  ECHO_TEST_DATA_SOURCE,
+  ECHO_RX_DATA_SOURCE,
+  ECHO_INVALID_DATA_SOURCE
+} data_source_t;
+
+enum echo_close_f_t
+{
+  ECHO_CLOSE_F_INVALID = 0,
+  ECHO_CLOSE_F_PASSIVE,                /* wait for close msg */
+  ECHO_CLOSE_F_ACTIVE,         /* send close msg */
+  ECHO_CLOSE_F_NONE,           /* don't bother sending close msg */
+};
+
+enum quic_session_type_t
+{
+  ECHO_SESSION_TYPE_QUIC,
+  ECHO_SESSION_TYPE_STREAM,
+  ECHO_SESSION_TYPE_LISTEN,
+};
+
+enum quic_session_state_t
+{
+  ECHO_SESSION_STATE_INITIAL,
+  ECHO_SESSION_STATE_READY,
+  ECHO_SESSION_STATE_AWAIT_CLOSING,    /* Data transfer is done, wait for close evt */
+  ECHO_SESSION_STATE_AWAIT_DATA,       /* Peer closed, wait for outstanding data */
+  ECHO_SESSION_STATE_CLOSING,  /* told vpp to close */
+  ECHO_SESSION_STATE_CLOSED,   /* closed in vpp */
+};
+
+typedef enum
+{
+  STATE_START,
+  STATE_ATTACHED,
+  STATE_LISTEN,
+  STATE_READY,
+  STATE_DATA_DONE,
+  STATE_DISCONNECTED,
+  STATE_DETACHED
+} connection_state_t;
+
+typedef enum echo_test_evt_
+{
+  ECHO_EVT_START = 1,          /* app starts */
+  ECHO_EVT_FIRST_QCONNECT = (1 << 1),  /* First connect Quic session sent */
+  ECHO_EVT_LAST_QCONNECTED = (1 << 2), /* All Quic session are connected */
+  ECHO_EVT_FIRST_SCONNECT = (1 << 3),  /* First connect Stream session sent */
+  ECHO_EVT_LAST_SCONNECTED = (1 << 4), /* All Stream session are connected */
+  ECHO_EVT_LAST_BYTE = (1 << 5),       /* Last byte received */
+  ECHO_EVT_EXIT = (1 << 6),    /* app exits */
+} echo_test_evt_t;
+
+typedef union session_connected_bundled_msg_
+{
+  session_connected_msg_t *mp;
+  vl_api_connect_uri_reply_t *bmp;
+} session_connected_bundled_msg_t;
+
+typedef struct echo_proto_cb_vft_
+{
+  void (*connected_cb) (session_connected_bundled_msg_t * mp, u32 session_index, u8 is_failed);        /* Session is connected */
+  void (*accepted_cb) (session_accepted_msg_t * mp, echo_session_t * session); /* Session got accepted */
+  void (*bound_uri_cb) (session_bound_msg_t * mp, echo_session_t * session);   /* Session got bound */
+  void (*reset_cb) (session_reset_msg_t * mp, echo_session_t * s);     /* Received RESET on session */
+  void (*disconnected_cb) (session_disconnected_msg_t * mp, echo_session_t * s);       /* Received DISCONNECT on session */
+  void (*disconnected_reply_cb) (echo_session_t * s);  /* ACK disconnect we sent to vpp */
+  void (*cleanup_cb) (echo_session_t * s, u8 parent_died);     /* Session should be cleaned up (parent listener may be dead) */
+  /* Add CLI options */
+  int (*process_opts_cb) (unformat_input_t * a);
+  void (*set_defaults_before_opts_cb) (void);
+  void (*set_defaults_after_opts_cb) (void);
+  void (*print_usage_cb) (void);
+} echo_proto_cb_vft_t;
+
+typedef enum
+{
+  RETURN_PACKETS_NOTEST,
+  RETURN_PACKETS_LOG_WRONG,
+  RETURN_PACKETS_ASSERT,
+} test_return_packets_t;
+
+typedef struct teardown_stat_
+{
+  u32 q;                       /* quic sessions */
+  u32 s;                       /* stream sessions */
+} teardown_stat_t;
+
+typedef struct
+{
+  svm_queue_t *vl_input_queue; /* vpe input queue */
+  u32 my_client_index;         /* API client handle */
+  u8 *uri;                     /* The URI we're playing with */
+  echo_session_t *sessions;    /* Session pool */
+  svm_msg_q_t *our_event_queue;        /* Our event queue */
+  clib_time_t clib_time;       /* For deadman timers */
+  u8 *socket_name;
+  int i_am_master;
+  u32 listen_session_index;    /* Index of vpp listener session */
+
+  uword *session_index_by_vpp_handles; /* Hash table : quic_echo s_id -> vpp s_handle */
+  clib_spinlock_t sid_vpp_handles_lock;        /* Hash table lock */
+
+  uword *shared_segment_handles;       /* Hash table : segment_names -> 1 */
+  clib_spinlock_t segment_handles_lock;        /* Hash table lock */
+  echo_proto_cb_vft_t *proto_cb_vft;
+  svm_msg_q_t *rpc_msq_queue;  /* MQ between quic_echo threads */
+  fifo_segment_main_t segment_main;
+
+  /* State of the connection, shared between msg RX thread and main thread */
+  volatile connection_state_t state;
+  volatile u8 time_to_stop;    /* Signal variables */
+  u8 has_failed;               /* stores the exit code */
+
+  /** Flag that decides if socket, instead of svm, api is used to connect to
+   * vpp. If sock api is used, shm binary api is subsequently bootstrapped
+   * and all other messages are exchanged using shm IPC. */
+  u8 use_sock_api;
+
+  u8 *connect_test_data;
+  u8 test_return_packets;
+  u64 bytes_to_send;           /* target per stream */
+  u64 bytes_to_receive;                /* target per stream */
+  u32 fifo_size;
+  u32 rx_buf_size;
+  u32 tx_buf_size;
+  data_source_t data_source;   /* Use no/dummy/mirrored data */
+  u8 send_stream_disconnects;  /* actively send disconnect */
+  u8 output_json;              /* Output stats as JSON */
+  u8 log_lvl;                  /* Verbosity of the logging */
+  int max_test_msg;            /* Limit the number of incorrect data messages */
+
+  u8 *appns_id;
+  u64 appns_flags;
+  u64 appns_secret;
+
+  pthread_t *data_thread_handles;      /* vec of data thread handles */
+  pthread_t mq_thread_handle;  /* Message queue thread handle */
+  u32 *volatile data_thread_args;
+
+  u32 n_connects;              /* Target number of connects to send */
+  u32 n_sessions;              /* Number of sessions to prealloc */
+  u32 n_clients;               /* Target number of clients doing RX/TX */
+  u32 n_rx_threads;            /* Number of data threads */
+
+  volatile u32 n_clients_connected;    /* Number of STREAM sessions connected */
+  volatile u32 nxt_available_sidx;     /* next unused prealloced session_index */
+
+  /* VNET_API_ERROR_FOO -> "Foo" hash table */
+  uword *error_string_by_error_number;
+  echo_proto_cb_vft_t *available_proto_cb_vft[TRANSPORT_N_PROTO];
+
+  struct
+  {
+    u64 tx_total;
+    u64 rx_total;
+    teardown_stat_t reset_count;       /* received reset from vpp */
+    teardown_stat_t close_count;       /* received close from vpp */
+    teardown_stat_t active_count;      /* sent close to vpp */
+    teardown_stat_t clean_count;       /* cleaned up stale session */
+  } stats;
+
+  struct                       /* Event based timing : start & end depend on CLI specified events */
+  {
+    f64 start_time;
+    f64 end_time;
+    u8 events_sent;
+    u8 start_event;
+    u8 end_event;
+  } timing;
+
+  struct
+  {
+    u32 transport_proto;
+    ip46_address_t ip;
+    u32 port;
+    u8 is_ip4;
+  } uri_elts;
+} echo_main_t;
+
+extern echo_main_t echo_main;
+
+typedef void (*echo_rpc_t) (void *arg, u32 opaque);
+
+typedef struct
+{
+  void *fp;
+  void *arg;
+  u32 opaque;
+} echo_rpc_msg_t;
+
+u8 *format_ip4_address (u8 * s, va_list * args);
+u8 *format_ip6_address (u8 * s, va_list * args);
+u8 *format_ip46_address (u8 * s, va_list * args);
+uword unformat_data (unformat_input_t * input, va_list * args);
+u8 *format_api_error (u8 * s, va_list * args);
+void init_error_string_table ();
+u8 *echo_format_app_state (u8 * s, va_list * args);
+uword echo_unformat_close (unformat_input_t * input, va_list * args);
+uword echo_unformat_timing_event (unformat_input_t * input, va_list * args);
+u8 *echo_format_timing_event (u8 * s, va_list * args);
+uword unformat_transport_proto (unformat_input_t * input, va_list * args);
+u8 *format_transport_proto (u8 * s, va_list * args);
+uword unformat_ip4_address (unformat_input_t * input, va_list * args);
+uword unformat_ip6_address (unformat_input_t * input, va_list * args);
+
+void echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid);
+echo_session_t *echo_session_new (echo_main_t * em);
+int echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque);
+echo_session_t *echo_get_session_from_handle (echo_main_t * em, u64 handle);
+int wait_for_segment_allocation (u64 segment_handle);
+int wait_for_state_change (echo_main_t * em, connection_state_t state,
+                          f64 timeout);
+void echo_notify_event (echo_main_t * em, echo_test_evt_t e);
+void echo_session_print_stats (echo_main_t * em, echo_session_t * session);
+
+/* Binary API */
+
+void echo_send_attach (echo_main_t * em);
+void echo_send_detach (echo_main_t * em);
+void echo_send_listen (echo_main_t * em);
+void echo_send_unbind (echo_main_t * em);
+void echo_send_connect (u8 * uri, u32 opaque);
+void echo_send_disconnect_session (u64 handle, u32 opaque);
+void echo_api_hookup (echo_main_t * em);
+
+#endif /* __included_vpp_echo_common_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/hs_apps/sapi/vpp_echo_proto_quic.c b/src/plugins/hs_apps/sapi/vpp_echo_proto_quic.c
new file mode 100644 (file)
index 0000000..28c89a6
--- /dev/null
@@ -0,0 +1,507 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <signal.h>
+
+#include <hs_apps/sapi/vpp_echo_common.h>
+
+typedef struct _quic_echo_cb_vft
+{
+  void (*quic_connected_cb) (session_connected_msg_t * mp, u32 session_index);
+  void (*client_stream_connected_cb) (session_connected_msg_t * mp,
+                                     u32 session_index);
+  void (*server_stream_connected_cb) (session_connected_msg_t * mp,
+                                     u32 session_index);
+  void (*quic_accepted_cb) (session_accepted_msg_t * mp, u32 session_index);
+  void (*client_stream_accepted_cb) (session_accepted_msg_t * mp,
+                                    u32 session_index);
+  void (*server_stream_accepted_cb) (session_accepted_msg_t * mp,
+                                    u32 session_index);
+} quic_echo_cb_vft_t;
+
+typedef struct
+{
+  quic_echo_cb_vft_t cb_vft;   /* cb vft for QUIC scenarios */
+  u8 send_quic_disconnects;    /* actively send disconnect */
+  u32 n_stream_clients;                /* Target Number of STREAM sessions per QUIC session */
+  volatile u32 n_quic_clients_connected;       /* Number of connected QUIC sessions */
+} quic_echo_proto_main_t;
+
+quic_echo_proto_main_t quic_echo_proto_main;
+
+/*
+ *
+ *  ECHO Callback definitions
+ *
+ */
+
+static void
+quic_echo_on_connected_connect (session_connected_msg_t * mp,
+                               u32 session_index)
+{
+  echo_main_t *em = &echo_main;
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  u8 *uri = format (0, "QUIC://session/%lu", mp->handle);
+  u64 i;
+
+  echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
+  for (i = 0; i < eqm->n_stream_clients; i++)
+    echo_send_rpc (em, echo_send_connect, (void *) uri, session_index);
+
+  ECHO_LOG (0, "Qsession 0x%llx connected to %U:%d",
+           mp->handle, format_ip46_address, &mp->lcl.ip,
+           mp->lcl.is_ip4, clib_net_to_host_u16 (mp->lcl.port));
+}
+
+static void
+quic_echo_on_connected_send (session_connected_msg_t * mp, u32 session_index)
+{
+  static u32 client_index = 0;
+  echo_main_t *em = &echo_main;
+  echo_session_t *session;
+
+  session = pool_elt_at_index (em->sessions, session_index);
+  session->bytes_to_send = em->bytes_to_send;
+  session->bytes_to_receive = em->bytes_to_receive;
+  session->session_state = ECHO_SESSION_STATE_READY;
+  em->data_thread_args[client_index++] = session->session_index;
+}
+
+static void
+quic_echo_on_connected_error (session_connected_msg_t * mp, u32 session_index)
+{
+  ECHO_FAIL ("Got a wrong connected on session %u [%lx]", session_index,
+            mp->handle);
+}
+
+static void
+quic_echo_on_accept_recv (session_accepted_msg_t * mp, u32 session_index)
+{
+  static u32 client_index = 0;
+  echo_main_t *em = &echo_main;
+  echo_session_t *session;
+
+  session = pool_elt_at_index (em->sessions, session_index);
+  session->bytes_to_send = em->bytes_to_send;
+  session->bytes_to_receive = em->bytes_to_receive;
+  em->data_thread_args[client_index++] = session->session_index;
+  session->session_state = ECHO_SESSION_STATE_READY;
+}
+
+static void
+quic_echo_on_accept_connect (session_accepted_msg_t * mp, u32 session_index)
+{
+  echo_main_t *em = &echo_main;
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  ECHO_LOG (1, "Accept on QSession 0x%lx %u", mp->handle);
+  u8 *uri = format (0, "QUIC://session/%lu", mp->handle);
+  u32 i;
+
+  echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
+  for (i = 0; i < eqm->n_stream_clients; i++)
+    echo_send_rpc (em, echo_send_connect, (void *) uri, session_index);
+}
+
+static void
+quic_echo_on_accept_error (session_accepted_msg_t * mp, u32 session_index)
+{
+  ECHO_FAIL ("Got a wrong accept on session %u [%lx]", session_index,
+            mp->handle);
+}
+
+static void
+quic_echo_on_accept_log_ip (session_accepted_msg_t * mp, u32 session_index)
+{
+  u8 *ip_str;
+  ip_str = format (0, "%U", format_ip46_address, &mp->rmt.ip, mp->rmt.is_ip4);
+  ECHO_LOG (0, "Accepted session from: %s:%d", ip_str,
+           clib_net_to_host_u16 (mp->rmt.port));
+
+}
+
+static const quic_echo_cb_vft_t default_cb_vft = {
+  /* Qsessions */
+  .quic_accepted_cb = quic_echo_on_accept_log_ip,
+  .quic_connected_cb = quic_echo_on_connected_connect,
+  /* client initiated streams */
+  .server_stream_accepted_cb = quic_echo_on_accept_recv,
+  .client_stream_connected_cb = quic_echo_on_connected_send,
+  /* server initiated streams */
+  .client_stream_accepted_cb = quic_echo_on_accept_error,
+  .server_stream_connected_cb = quic_echo_on_connected_error,
+};
+
+static const quic_echo_cb_vft_t server_stream_cb_vft = {
+  /* Qsessions */
+  .quic_accepted_cb = quic_echo_on_accept_connect,
+  .quic_connected_cb = NULL,
+  /* client initiated streams */
+  .server_stream_accepted_cb = quic_echo_on_accept_error,
+  .client_stream_connected_cb = quic_echo_on_connected_error,
+  /* server initiated streams */
+  .client_stream_accepted_cb = quic_echo_on_accept_recv,
+  .server_stream_connected_cb = quic_echo_on_connected_send,
+};
+
+static void quic_echo_cleanup_cb (echo_session_t * s, u8 parent_died);
+
+static inline void
+quic_echo_cleanup_listener (u32 listener_index, echo_main_t * em,
+                           quic_echo_proto_main_t * eqm)
+{
+  echo_session_t *ls;
+  ls = pool_elt_at_index (em->sessions, listener_index);
+  ASSERT (ls->session_type == ECHO_SESSION_TYPE_QUIC);
+  if (!clib_atomic_sub_fetch (&ls->accepted_session_count, 1))
+    {
+      if (eqm->send_quic_disconnects == ECHO_CLOSE_F_ACTIVE)
+       {
+         echo_send_rpc (em, echo_send_disconnect_session,
+                        (void *) ls->vpp_session_handle, 0);
+         clib_atomic_fetch_add (&em->stats.active_count.q, 1);
+       }
+      else if (eqm->send_quic_disconnects == ECHO_CLOSE_F_NONE)
+       {
+         quic_echo_cleanup_cb (ls, 0 /* parent_died */ );
+         clib_atomic_fetch_add (&em->stats.clean_count.q, 1);
+       }
+    }
+}
+
+static void
+quic_echo_cleanup_cb (echo_session_t * s, u8 parent_died)
+{
+  echo_main_t *em = &echo_main;
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  ASSERT (s->session_state < ECHO_SESSION_STATE_CLOSED);
+  if (s->session_type == ECHO_SESSION_TYPE_QUIC)
+    {
+      if (parent_died)
+       clib_atomic_fetch_add (&em->stats.clean_count.q, 1);
+      /* Don't cleanup listener as it's handled by main() */
+      clib_atomic_sub_fetch (&eqm->n_quic_clients_connected, 1);
+    }
+  else if (s->session_type == ECHO_SESSION_TYPE_STREAM)
+    {
+      if (parent_died)
+       clib_atomic_fetch_add (&em->stats.clean_count.s, 1);
+      else
+       quic_echo_cleanup_listener (s->listener_index, em, eqm);
+      clib_atomic_sub_fetch (&em->n_clients_connected, 1);
+    }
+
+  ECHO_LOG (1, "Cleanup sessions (still %uQ %uS)",
+           eqm->n_quic_clients_connected, em->n_clients_connected);
+  s->session_state = ECHO_SESSION_STATE_CLOSED;
+  if (!em->n_clients_connected && !eqm->n_quic_clients_connected)
+    em->state = STATE_DATA_DONE;
+}
+
+static void
+quic_echo_initiate_qsession_close_no_stream (echo_main_t * em)
+{
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  ECHO_LOG (1, "Closing Qsessions");
+  /* Close Quic session without streams */
+  echo_session_t *s;
+
+  /* *INDENT-OFF* */
+  pool_foreach (s, em->sessions,
+  ({
+    if (s->session_type == ECHO_SESSION_TYPE_QUIC)
+      {
+        if (eqm->send_quic_disconnects == ECHO_CLOSE_F_ACTIVE)
+          {
+            ECHO_LOG (1,"ACTIVE close 0x%lx", s->vpp_session_handle);
+            echo_send_rpc (em, echo_send_disconnect_session, (void *) s->vpp_session_handle, 0);
+            clib_atomic_fetch_add (&em->stats.active_count.q, 1);
+          }
+        else if (eqm->send_quic_disconnects == ECHO_CLOSE_F_NONE)
+          {
+            ECHO_LOG (1,"Discard close 0x%lx", s->vpp_session_handle);
+            quic_echo_cleanup_cb (s, 0 /* parent_died */);
+            clib_atomic_fetch_add (&em->stats.clean_count.q, 1);
+          }
+        else
+          ECHO_LOG (1,"Passive close 0x%lx", s->vpp_session_handle);
+      }
+  }));
+  /* *INDENT-ON* */
+}
+
+static void
+quic_echo_on_connected (session_connected_msg_t * mp, u32 session_index)
+{
+  echo_main_t *em = &echo_main;
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  echo_session_t *listen_session;
+  echo_session_t *session = pool_elt_at_index (em->sessions, session_index);
+  if (session->listener_index == SESSION_INVALID_INDEX)
+    {
+      ECHO_LOG (1, "Connected session 0x%lx -> URI", mp->handle);
+      session->session_type = ECHO_SESSION_TYPE_QUIC;
+      session->accepted_session_count = 0;
+      if (eqm->cb_vft.quic_connected_cb)
+       eqm->cb_vft.quic_connected_cb (mp, session->session_index);
+      clib_atomic_fetch_add (&eqm->n_quic_clients_connected, 1);
+    }
+  else
+    {
+      listen_session =
+       pool_elt_at_index (em->sessions, session->listener_index);
+      ECHO_LOG (1, "Connected session 0x%lx -> 0x%lx", mp->handle,
+               listen_session->vpp_session_handle);
+      session->session_type = ECHO_SESSION_TYPE_STREAM;
+      clib_atomic_fetch_add (&listen_session->accepted_session_count, 1);
+      if (em->i_am_master && eqm->cb_vft.server_stream_connected_cb)
+       eqm->cb_vft.server_stream_connected_cb (mp, session->session_index);
+      if (!em->i_am_master && eqm->cb_vft.client_stream_connected_cb)
+       eqm->cb_vft.client_stream_connected_cb (mp, session->session_index);
+      clib_atomic_fetch_add (&em->n_clients_connected, 1);
+    }
+
+  if (em->n_clients_connected == em->n_clients
+      && em->n_clients_connected != 0)
+    echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
+
+  if (eqm->n_quic_clients_connected == em->n_connects
+      && em->state < STATE_READY)
+    {
+      echo_notify_event (em, ECHO_EVT_LAST_QCONNECTED);
+      em->state = STATE_READY;
+      if (eqm->n_stream_clients == 0)
+       quic_echo_initiate_qsession_close_no_stream (em);
+    }
+}
+
+static void
+quic_echo_retry_connect (u32 session_index)
+{
+  /* retry connect */
+  echo_session_t *session;
+  echo_main_t *em = &echo_main;
+  u8 *uri;
+  if (session_index == SESSION_INVALID_INDEX)
+    {
+      ECHO_LOG (1, "Retrying connect %s", em->uri);
+      echo_send_rpc (em, echo_send_connect, (void *) em->uri,
+                    SESSION_INVALID_INDEX);
+    }
+  else
+    {
+      session = pool_elt_at_index (em->sessions, session_index);
+      uri = format (0, "QUIC://session/%lu", session->vpp_session_handle);
+      ECHO_LOG (1, "Retrying connect %s", uri);
+      echo_send_rpc (em, echo_send_connect, (void *) uri, session_index);
+    }
+}
+
+static void
+quic_echo_connected_cb (session_connected_bundled_msg_t * mp,
+                       u32 session_index, u8 is_failed)
+{
+  if (is_failed)
+    return quic_echo_retry_connect (session_index);
+  return quic_echo_on_connected ((session_connected_msg_t *) mp,
+                                session_index);
+}
+
+static void
+quic_echo_accepted_cb (session_accepted_msg_t * mp, echo_session_t * session)
+{
+  echo_main_t *em = &echo_main;
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  echo_session_t *ls;
+  ls = pool_elt_at_index (em->sessions, session->listener_index);
+  if (ls->session_type == ECHO_SESSION_TYPE_LISTEN)
+    {
+      echo_notify_event (em, ECHO_EVT_FIRST_QCONNECT);
+      session->session_type = ECHO_SESSION_TYPE_QUIC;
+      session->accepted_session_count = 0;
+      if (eqm->cb_vft.quic_accepted_cb)
+       eqm->cb_vft.quic_accepted_cb (mp, session->session_index);
+      clib_atomic_fetch_add (&eqm->n_quic_clients_connected, 1);
+    }
+  else
+    {
+      session->session_type = ECHO_SESSION_TYPE_STREAM;
+      echo_notify_event (em, ECHO_EVT_FIRST_SCONNECT);
+      clib_atomic_fetch_add (&ls->accepted_session_count, 1);
+      if (em->i_am_master && eqm->cb_vft.server_stream_accepted_cb)
+       eqm->cb_vft.server_stream_accepted_cb (mp, session->session_index);
+      if (!em->i_am_master && eqm->cb_vft.client_stream_accepted_cb)
+       eqm->cb_vft.client_stream_accepted_cb (mp, session->session_index);
+      clib_atomic_fetch_add (&em->n_clients_connected, 1);
+    }
+
+  if (em->n_clients_connected == em->n_clients
+      && em->n_clients_connected != 0)
+    echo_notify_event (em, ECHO_EVT_LAST_SCONNECTED);
+
+  if (eqm->n_quic_clients_connected == em->n_connects
+      && em->state < STATE_READY)
+    {
+      echo_notify_event (em, ECHO_EVT_LAST_QCONNECTED);
+      em->state = STATE_READY;
+      if (eqm->n_stream_clients == 0)
+       quic_echo_initiate_qsession_close_no_stream (em);
+    }
+}
+
+static void
+quic_echo_disconnected_reply_cb (echo_session_t * s)
+{
+  if (s->session_type == ECHO_SESSION_TYPE_STREAM)
+    s->session_state = ECHO_SESSION_STATE_CLOSING;
+  else
+    quic_echo_cleanup_cb (s, 0 /* parent_died */ );    /* We can clean Q/Lsessions right away */
+}
+
+static void
+quic_echo_disconnected_cb (session_disconnected_msg_t * mp,
+                          echo_session_t * s)
+{
+  echo_main_t *em = &echo_main;
+  if (s->session_type == ECHO_SESSION_TYPE_STREAM)
+    {
+      echo_session_print_stats (em, s);
+      if (s->bytes_to_receive || s->bytes_to_send)
+       s->session_state = ECHO_SESSION_STATE_AWAIT_DATA;
+      else
+       s->session_state = ECHO_SESSION_STATE_CLOSING;
+      clib_atomic_fetch_add (&em->stats.close_count.s, 1);
+    }
+  else
+    {
+      quic_echo_cleanup_cb (s, 0 /* parent_died */ );  /* We can clean Q/Lsessions right away */
+      clib_atomic_fetch_add (&em->stats.close_count.q, 1);
+    }
+}
+
+static void
+quic_echo_reset_cb (session_reset_msg_t * mp, echo_session_t * s)
+{
+  echo_main_t *em = &echo_main;
+  if (s->session_type == ECHO_SESSION_TYPE_STREAM)
+    {
+      clib_atomic_fetch_add (&em->stats.reset_count.s, 1);
+      s->session_state = ECHO_SESSION_STATE_CLOSING;
+    }
+  else
+    {
+      clib_atomic_fetch_add (&em->stats.reset_count.q, 1);
+      quic_echo_cleanup_cb (s, 0 /* parent_died */ );  /* We can clean Q/Lsessions right away */
+    }
+}
+
+static uword
+quic_echo_unformat_setup_vft (unformat_input_t * input, va_list * args)
+{
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  if (unformat (input, "serverstream"))
+    eqm->cb_vft = server_stream_cb_vft;
+  else if (unformat (input, "default"))
+    ;
+  else
+    return 0;
+  return 1;
+}
+
+static int
+quic_echo_process_opts_cb (unformat_input_t * a)
+{
+  echo_main_t *em = &echo_main;
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  if (unformat (a, "nclients %d/%d", &em->n_clients, &eqm->n_stream_clients))
+    ;
+  else if (unformat (a, "quic-setup %U", quic_echo_unformat_setup_vft))
+    ;
+  else if (unformat (a, "qclose=%U",
+                    echo_unformat_close, &eqm->send_quic_disconnects))
+    ;
+  else
+    return 0;
+  return 1;
+}
+
+static void
+quic_echo_set_defaults_before_opts_cb ()
+{
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  eqm->cb_vft = default_cb_vft;
+  eqm->n_stream_clients = 1;
+}
+
+static void
+quic_echo_set_defaults_after_opts_cb ()
+{
+  quic_echo_proto_main_t *eqm = &quic_echo_proto_main;
+  echo_main_t *em = &echo_main;
+  u8 default_f_active;
+
+  em->n_connects = em->n_clients;
+  em->n_sessions =
+    clib_max (1,
+             eqm->n_stream_clients) * em->n_clients + eqm->n_stream_clients +
+    1;
+  em->n_clients = eqm->n_stream_clients * em->n_clients;
+
+  if (em->i_am_master)
+    default_f_active =
+      em->bytes_to_send == 0 ? ECHO_CLOSE_F_ACTIVE : ECHO_CLOSE_F_PASSIVE;
+  else
+    default_f_active =
+      em->bytes_to_receive == 0 ? ECHO_CLOSE_F_PASSIVE : ECHO_CLOSE_F_ACTIVE;
+  if (eqm->send_quic_disconnects == ECHO_CLOSE_F_INVALID)
+    eqm->send_quic_disconnects = default_f_active;
+}
+
+static void
+quic_echo_print_usage_cb ()
+{
+  fprintf (stderr,
+          "-- QUIC specific options -- \n"
+          "  quic-setup OPT      OPT=serverstream : Client open N connections. \n"
+          "                       On each one server opens M streams\n"
+          "                      OPT=default : Client open N connections.\n"
+          "                       On each one client opens M streams\n"
+          "  qclose=[Y|N|W]      When a connection is done pass[N] send[Y] or wait[W] for close\n"
+          "\n"
+          "  nclients N[/M]      Open N QUIC connections, each one with M streams (M defaults to 1)\n");
+}
+
+echo_proto_cb_vft_t quic_echo_proto_cb_vft = {
+  .disconnected_cb = quic_echo_disconnected_cb,
+  .connected_cb = quic_echo_connected_cb,
+  .accepted_cb = quic_echo_accepted_cb,
+  .reset_cb = quic_echo_reset_cb,
+  .disconnected_reply_cb = quic_echo_disconnected_reply_cb,
+  .cleanup_cb = quic_echo_cleanup_cb,
+  .process_opts_cb = quic_echo_process_opts_cb,
+  .print_usage_cb = quic_echo_print_usage_cb,
+  .set_defaults_before_opts_cb = quic_echo_set_defaults_before_opts_cb,
+  .set_defaults_after_opts_cb = quic_echo_set_defaults_after_opts_cb,
+};
+
+ECHO_REGISTER_PROTO (TRANSPORT_PROTO_QUIC, quic_echo_proto_cb_vft);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
index ed15cdc..bbf7d72 100644 (file)
@@ -173,7 +173,7 @@ class QUICEchoExternalTestCase(QUICTestCase):
         _args = self.server_echo_test_args + list(args)
         self.worker_server = QUICAppWorker(
             self.build_dir,
-            "quic_echo",
+            "vpp_echo",
             _args,
             self.logger)
         self.worker_server.start()
@@ -184,7 +184,7 @@ class QUICEchoExternalTestCase(QUICTestCase):
         # self.client_echo_test_args += "use-svm-api"
         self.worker_client = QUICAppWorker(
             self.build_dir,
-            "quic_echo",
+            "vpp_echo",
             _args,
             self.logger)
         self.worker_client.start()