vlib: barrier sync elog tracing improvements
[vpp.git] / src / vpp / api / api_main.c
index 97b501e..8f33f04 100644 (file)
@@ -47,16 +47,74 @@ api_main_init (vlib_main_t * vm)
 
   vam->vlib_main = vm;
   vam->my_client_index = (u32) ~ 0;
+  /* Ensure that vam->inbuf is never NULL */
+  vec_validate (vam->inbuf, 0);
+  vec_validate (vam->cmd_reply, 0);
+  vec_reset_length (vam->cmd_reply);
   init_error_string_table (vam);
-  vat_api_hookup (vam);
   rv = vat_plugin_init (vam);
   if (rv)
     clib_warning ("vat_plugin_init returned %d", rv);
+
   return 0;
 }
 
 VLIB_INIT_FUNCTION (api_main_init);
 
+void
+vat_plugin_hash_create (void)
+{
+  vat_main_t *vam = &vat_main;
+
+  vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
+  vam->function_by_name = hash_create_string (0, sizeof (uword));
+  vam->help_by_name = hash_create_string (0, sizeof (uword));
+}
+
+static void
+maybe_register_api_client (vat_main_t * vam)
+{
+  vl_api_registration_t **regpp;
+  vl_api_registration_t *regp;
+  svm_region_t *svm;
+  void *oldheap;
+  api_main_t *am = &api_main;
+
+  if (vam->my_client_index != ~0)
+    return;
+
+  pool_get (am->vl_clients, regpp);
+
+  svm = am->vlib_rp;
+
+  pthread_mutex_lock (&svm->mutex);
+  oldheap = svm_push_data_heap (svm);
+  *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
+
+  regp = *regpp;
+  memset (regp, 0, sizeof (*regp));
+  regp->registration_type = REGISTRATION_TYPE_SHMEM;
+  regp->vl_api_registration_pool_index = regpp - am->vl_clients;
+  regp->vlib_rp = svm;
+  regp->shmem_hdr = am->shmem_hdr;
+
+  /* Loopback connection */
+  regp->vl_input_queue = am->shmem_hdr->vl_input_queue;
+
+  regp->name = format (0, "%s", "vpp-internal");
+  vec_add1 (regp->name, 0);
+
+  pthread_mutex_unlock (&svm->mutex);
+  svm_pop_heap (oldheap);
+
+  vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
+    (regp->vl_api_registration_pool_index,
+     am->shmem_hdr->application_restarts);
+
+  vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
+  api_sw_interface_dump (vam);
+}
+
 static clib_error_t *
 api_command_fn (vlib_main_t * vm,
                unformat_input_t * input, vlib_cli_command_t * cmd)
@@ -71,19 +129,22 @@ api_command_fn (vlib_main_t * vm,
   int (*fp) (vat_main_t *);
   api_main_t *am = &api_main;
 
-  vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
+  maybe_register_api_client (vam);
+
+  /* vec_validated in the init routine */
+  _vec_len (vam->inbuf) = 0;
 
-  vec_reset_length (vam->inbuf);
   vam->input = &_input;
 
   while (((c = unformat_get_input (input)) != '\n') &&
         (c != UNFORMAT_END_OF_INPUT))
     vec_add1 (vam->inbuf, c);
 
-  /* Add 1 octet's worth of extra space in case there are no args... */
+  /* Null-terminate the command */
   vec_add1 (vam->inbuf, 0);
 
-  /*$$$$ reinstall macro evaluator */
+  /* In case no args given */
+  vec_add1 (vam->inbuf, 0);
 
   /* Split input into cmd + args */
   this_cmd = cmdp = vam->inbuf;
@@ -104,7 +165,7 @@ api_command_fn (vlib_main_t * vm,
   /* Advance past the command */
   while (argsp < (this_cmd + vec_len (this_cmd)))
     {
-      if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && argsp != 0)
+      if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
        {
          argsp++;
        }
@@ -114,15 +175,19 @@ api_command_fn (vlib_main_t * vm,
   /* NULL terminate the command */
   *argsp++ = 0;
 
-  while (argsp < (this_cmd + vec_len (this_cmd)))
-    {
-      if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
-       {
-         argsp++;
-       }
-      else
-       break;
-    }
+  /* No arguments? Ensure that argsp points to a proper (empty) string */
+  if (argsp == (this_cmd + vec_len (this_cmd) - 1))
+    argsp[0] = 0;
+  else
+    while (argsp < (this_cmd + vec_len (this_cmd)))
+      {
+       if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
+         {
+           argsp++;
+         }
+       else
+         break;
+      }
 
   /* Blank input line? */
   if (*cmdp == 0)
@@ -164,6 +229,7 @@ VLIB_CLI_COMMAND (api_command, static) =
   .path = "binary-api",
   .short_help = "binary-api [help] <name> [<args>]",
   .function = api_command_fn,
+  .is_mp_safe = 1,
 };
 /* *INDENT-ON* */
 
@@ -193,7 +259,7 @@ api_cli_output (void *notused, const char *fmt, ...)
 }
 
 u16
-vl_client_get_first_plugin_msg_id (char *plugin_name)
+vl_client_get_first_plugin_msg_id (const char *plugin_name)
 {
   api_main_t *am = &api_main;
   vl_api_msg_range_t *rp;
@@ -211,11 +277,10 @@ vl_client_get_first_plugin_msg_id (char *plugin_name)
 uword
 unformat_sw_if_index (unformat_input_t * input, va_list * args)
 {
+  void *vam_unused = va_arg (*args, void *);
   u32 *result = va_arg (*args, u32 *);
   vnet_main_t *vnm = vnet_get_main ();
   u32 sw_if_index = ~0;
-  u8 *if_name;
-  uword *p;
 
   if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
     {