hsa: fix builtin echo apps with multiple workers
[vpp.git] / src / plugins / hs_apps / echo_server.c
index c8335e3..63150d5 100644 (file)
@@ -47,7 +47,9 @@ typedef struct
   u32 private_segment_size;    /**< Size of private segments  */
   char *server_uri;            /**< Server URI */
   u32 tls_engine;              /**< TLS engine: mbedtls/openssl */
+  u32 ckpair_index;            /**< Cert and key for tls/quic */
   u8 is_dgram;                 /**< set if transport is dgram */
+
   /*
    * Test state
    */
@@ -301,26 +303,10 @@ static session_cb_vft_t echo_server_session_cb_vft = {
   .session_reset_callback = echo_server_session_reset_callback
 };
 
-/* Abuse VPP's input queue */
-static int
-create_api_loopback (vlib_main_t * vm)
-{
-  echo_server_main_t *esm = &echo_server_main;
-  api_main_t *am = vlibapi_get_main ();
-  vl_shmem_hdr_t *shmem_hdr;
-
-  shmem_hdr = am->shmem_hdr;
-  esm->vl_input_queue = shmem_hdr->vl_input_queue;
-  esm->my_client_index = vl_api_memclnt_create_internal ("echo_server",
-                                                        esm->vl_input_queue);
-  return 0;
-}
-
 static int
 echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
 {
-  vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
-  vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
+  vnet_app_add_cert_key_pair_args_t _ck_pair, *ck_pair = &_ck_pair;
   echo_server_main_t *esm = &echo_server_main;
   vnet_app_attach_args_t _a, *a = &_a;
   u64 options[APP_OPTIONS_N_OPTIONS];
@@ -342,7 +328,8 @@ echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
   if (esm->private_segment_size)
     segment_size = esm->private_segment_size;
 
-  a->api_client_index = esm->my_client_index;
+  a->api_client_index = ~0;
+  a->name = format (0, "echo_server");
   a->session_cb_vft = &echo_server_session_cb_vft;
   a->options = options;
   a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
@@ -369,18 +356,16 @@ echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
       return -1;
     }
   esm->app_index = a->app_index;
+  vec_free (a->name);
+
+  clib_memset (ck_pair, 0, sizeof (*ck_pair));
+  ck_pair->cert = (u8 *) test_srv_crt_rsa;
+  ck_pair->key = (u8 *) test_srv_key_rsa;
+  ck_pair->cert_len = test_srv_crt_rsa_len;
+  ck_pair->key_len = test_srv_key_rsa_len;
+  vnet_app_add_cert_key_pair (ck_pair);
+  esm->ckpair_index = ck_pair->index;
 
-  clib_memset (a_cert, 0, sizeof (*a_cert));
-  a_cert->app_index = a->app_index;
-  vec_validate (a_cert->cert, test_srv_crt_rsa_len);
-  clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
-  vnet_app_add_tls_cert (a_cert);
-
-  clib_memset (a_key, 0, sizeof (*a_key));
-  a_key->app_index = a->app_index;
-  vec_validate (a_key->key, test_srv_key_rsa_len);
-  clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
-  vnet_app_add_tls_key (a_key);
   return 0;
 }
 
@@ -392,22 +377,36 @@ echo_server_detach (void)
   int rv;
 
   da->app_index = esm->app_index;
+  da->api_client_index = ~0;
   rv = vnet_application_detach (da);
   esm->app_index = ~0;
+  vnet_app_del_cert_key_pair (esm->ckpair_index);
   return rv;
 }
 
 static int
 echo_server_listen ()
 {
-  int rv;
+  i32 rv;
   echo_server_main_t *esm = &echo_server_main;
-  vnet_listen_args_t _a, *a = &_a;
-  clib_memset (a, 0, sizeof (*a));
-  a->app_index = esm->app_index;
-  a->uri = esm->server_uri;
-  rv = vnet_bind_uri (a);
-  esm->listener_handle = a->handle;
+  vnet_listen_args_t _args = { 0 }, *args = &_args;
+
+  args->sep_ext.app_wrk_index = 0;
+
+  if ((rv = parse_uri (esm->server_uri, &args->sep_ext)))
+    {
+      return -1;
+    }
+  args->app_index = esm->app_index;
+  args->sep_ext.ckpair_index = esm->ckpair_index;
+
+  if (args->sep_ext.transport_proto == TRANSPORT_PROTO_UDP)
+    {
+      args->sep_ext.transport_flags = TRANSPORT_CFG_F_CONNECTED;
+    }
+
+  rv = vnet_listen (args);
+  esm->listener_handle = args->handle;
   return rv;
 }
 
@@ -420,15 +419,6 @@ echo_server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags,
   u32 num_threads;
   int i;
 
-  if (esm->my_client_index == (u32) ~ 0)
-    {
-      if (create_api_loopback (vm))
-       {
-         clib_warning ("failed to create api loopback");
-         return -1;
-       }
-    }
-
   num_threads = 1 /* main thread */  + vtm->n_threads;
   vec_validate (echo_server_main.vpp_queue, num_threads - 1);
   vec_validate (esm->rx_buf, num_threads - 1);
@@ -459,12 +449,23 @@ static clib_error_t *
 echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
                               vlib_cli_command_t * cmd)
 {
+  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
   echo_server_main_t *esm = &echo_server_main;
   u8 server_uri_set = 0, *appns_id = 0;
   u64 tmp, appns_flags = 0, appns_secret = 0;
   char *default_uri = "tcp://0.0.0.0/1234";
-  int rv, is_stop = 0;
-  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
+  int rv, is_stop = 0, barrier_acq_needed = 0;
+  clib_error_t *error = 0;
+
+  /* The request came over the binary api and the inband cli handler
+   * is not mp_safe. Drop the barrier to make sure the workers are not
+   * blocked.
+   */
+  if (vlib_num_workers () && vlib_thread_is_main_w_barrier ())
+    {
+      barrier_acq_needed = 1;
+      vlib_worker_thread_barrier_release (vm);
+    }
 
   esm->no_echo = 0;
   esm->fifo_size = 64 << 10;
@@ -494,8 +495,11 @@ echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
                         unformat_memory_size, &tmp))
        {
          if (tmp >= 0x100000000ULL)
-           return clib_error_return
-             (0, "private segment size %lld (%llu) too large", tmp, tmp);
+           {
+             error = clib_error_return (
+               0, "private segment size %lld (%llu) too large", tmp, tmp);
+             goto cleanup;
+           }
          esm->private_segment_size = tmp;
        }
       else if (unformat (input, "appns %_%v%_", &appns_id))
@@ -514,8 +518,11 @@ echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
       else if (unformat (input, "tls-engine %d", &esm->tls_engine))
        ;
       else
-       return clib_error_return (0, "failed: unknown input `%U'",
-                                 format_unformat_error, input);
+       {
+         error = clib_error_return (0, "failed: unknown input `%U'",
+                                    format_unformat_error, input);
+         goto cleanup;
+       }
     }
 
   if (is_stop)
@@ -523,15 +530,17 @@ echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
       if (esm->app_index == (u32) ~ 0)
        {
          clib_warning ("server not running");
-         return clib_error_return (0, "failed: server not running");
+         error = clib_error_return (0, "failed: server not running");
+         goto cleanup;
        }
       rv = echo_server_detach ();
       if (rv)
        {
          clib_warning ("failed: detach");
-         return clib_error_return (0, "failed: server detach %d", rv);
+         error = clib_error_return (0, "failed: server detach %d", rv);
+         goto cleanup;
        }
-      return 0;
+      goto cleanup;
     }
 
   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
@@ -543,19 +552,28 @@ echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
     }
 
   if ((rv = parse_uri ((char *) esm->server_uri, &sep)))
-    return clib_error_return (0, "Uri parse error: %d", rv);
+    {
+      error = clib_error_return (0, "Uri parse error: %d", rv);
+      goto cleanup;
+    }
   esm->transport_proto = sep.transport_proto;
   esm->is_dgram = (sep.transport_proto == TRANSPORT_PROTO_UDP);
 
   rv = echo_server_create (vm, appns_id, appns_flags, appns_secret);
-  vec_free (appns_id);
   if (rv)
     {
       vec_free (esm->server_uri);
-      return clib_error_return (0, "failed: server_create returned %d", rv);
+      error = clib_error_return (0, "failed: server_create returned %d", rv);
+      goto cleanup;
     }
 
-  return 0;
+cleanup:
+  vec_free (appns_id);
+
+  if (barrier_acq_needed)
+    vlib_worker_thread_barrier_sync (vm);
+
+  return error;
 }
 
 /* *INDENT-OFF* */