feature: Add packet trace API
[vpp.git] / src / vat / main.c
index e2c9b70..3e63aea 100644 (file)
@@ -15,6 +15,7 @@
 #include "vat.h"
 #include "plugin.h"
 #include <signal.h>
+#include <limits.h>
 
 vat_main_t vat_main;
 
@@ -26,22 +27,11 @@ vat_suspend (vlib_main_t * vm, f64 interval)
   /* do nothing in the standalone version, just return */
 }
 
-void
-fformat_append_cr (FILE * ofp, const char *fmt, ...)
-{
-  va_list va;
-
-  va_start (va, fmt);
-  (void) va_fformat (ofp, (char *) fmt, &va);
-  va_end (va);
-  fformat (ofp, "\n");
-}
-
 int
 connect_to_vpe (char *name)
 {
   vat_main_t *vam = &vat_main;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
 
   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
     return -1;
@@ -52,15 +42,31 @@ connect_to_vpe (char *name)
   return 0;
 }
 
+/* *INDENT-OFF* */
+
+
 vlib_main_t vlib_global_main;
-vlib_main_t **vlib_mains;
+
+static struct
+{
+  vec_header_t h;
+  vlib_main_t *vm;
+} __attribute__ ((packed)) __bootstrap_vlib_main_vector
+__attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
+{
+  .h.len = 1,
+  .vm = &vlib_global_main,
+};
+/* *INDENT-ON* */
+
+vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
+
 void
 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
 {
   clib_warning ("BUG");
 }
 
-
 static u8 *
 format_api_error (u8 * s, va_list * args)
 {
@@ -117,8 +123,10 @@ do_one_file (vat_main_t * vam)
       vec_free (this_cmd);
 
       this_cmd =
-       (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
-                               1 /* complain */ );
+       (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
+                               1 /* complain */ ,
+                               0 /* level */ ,
+                               8 /* max_level */ );
 
       if (vam->exec_mode == 0)
        {
@@ -195,7 +203,7 @@ do_one_file (vat_main_t * vam)
       if (vam->client_index_invalid)
        {
          vat_main_t *vam = &vat_main;
-         api_main_t *am = &api_main;
+         api_main_t *am = vlibapi_get_main ();
 
          vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
          vam->my_client_index = am->my_client_index;
@@ -218,14 +226,14 @@ init_error_string_table (vat_main_t * vam)
 }
 
 static i8 *
-eval_current_file (macro_main_t * mm, i32 complain)
+eval_current_file (clib_macro_main_t * mm, i32 complain)
 {
   vat_main_t *vam = &vat_main;
   return ((i8 *) format (0, "%s%c", vam->current_file, 0));
 }
 
 static i8 *
-eval_current_line (macro_main_t * mm, i32 complain)
+eval_current_line (clib_macro_main_t * mm, i32 complain)
 {
   vat_main_t *vam = &vat_main;
   return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
@@ -264,7 +272,7 @@ setup_signal_handlers (void)
 
   for (i = 1; i < 32; i++)
     {
-      memset (&sa, 0, sizeof (sa));
+      clib_memset (&sa, 0, sizeof (sa));
       sa.sa_sigaction = (void *) signal_handler;
       sa.sa_flags = SA_SIGINFO;
 
@@ -276,11 +284,13 @@ setup_signal_handlers (void)
        case SIGSTOP:
        case SIGUSR1:
        case SIGUSR2:
+       case SIGPROF:
          continue;
 
          /* ignore SIGPIPE, SIGCHLD */
        case SIGPIPE:
        case SIGCHLD:
+       case SIGWINCH:
          sa.sa_sigaction = (void *) SIG_IGN;
          break;
 
@@ -294,6 +304,95 @@ setup_signal_handlers (void)
     }
 }
 
+static void
+vat_find_plugin_path ()
+{
+  char *p, path[PATH_MAX];
+  int rv;
+  u8 *s;
+
+  /* find executable path */
+  if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
+    return;
+
+  /* readlink doesn't provide null termination */
+  path[rv] = 0;
+
+  /* strip filename */
+  if ((p = strrchr (path, '/')) == 0)
+    return;
+  *p = 0;
+
+  /* strip bin/ */
+  if ((p = strrchr (path, '/')) == 0)
+    return;
+  *p = 0;
+
+  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
+             "%s/lib/vpp_api_test_plugins", path, path);
+  vec_add1 (s, 0);
+  vat_plugin_path = (char *) s;
+}
+
+static void
+load_features (void)
+{
+  vat_registered_features_t *f;
+  vat_main_t *vam = &vat_main;
+  clib_error_t *error;
+
+  f = vam->feature_function_registrations;
+
+  while (f)
+    {
+      error = f->function (vam);
+      if (error)
+       {
+         clib_warning ("INIT FAILED");
+       }
+      f = f->next;
+    }
+}
+
+static inline clib_error_t *
+call_init_exit_functions_internal (vlib_main_t * vm,
+                                  _vlib_init_function_list_elt_t ** headp,
+                                  int call_once, int do_sort)
+{
+  clib_error_t *error = 0;
+  _vlib_init_function_list_elt_t *i;
+
+#if 0
+  /* Not worth copying the topological sort code */
+  if (do_sort && (error = vlib_sort_init_exit_functions (headp)))
+    return (error);
+#endif
+
+  i = *headp;
+  while (i)
+    {
+      if (call_once && !hash_get (vm->init_functions_called, i->f))
+       {
+         if (call_once)
+           hash_set1 (vm->init_functions_called, i->f);
+         error = i->f (vm);
+         if (error)
+           return error;
+       }
+      i = i->next_init_function;
+    }
+  return error;
+}
+
+clib_error_t *
+vlib_call_init_exit_functions (vlib_main_t * vm,
+                              _vlib_init_function_list_elt_t ** headp,
+                              int call_once)
+{
+  return call_init_exit_functions_internal (vm, headp, call_once,
+                                           1 /* do_sort */ );
+}
+
 int
 main (int argc, char **argv)
 {
@@ -305,17 +404,12 @@ main (int argc, char **argv)
   u8 *this_input_file;
   u8 interactive = 1;
   u8 json_output = 0;
-  u8 *heap;
-  mheap_t *h;
   int i;
+  f64 timeout;
+  clib_error_t *error;
+  vlib_main_t *vm = &vlib_global_main;
 
-  clib_mem_init (0, 128 << 20);
-
-  heap = clib_mem_get_per_cpu_heap ();
-  h = mheap_header (heap);
-
-  /* make the main heap thread-safe */
-  h->flags |= MHEAP_FLAG_THREAD_SAFE;
+  clib_mem_init_thread_safe (0, 128 << 20);
 
   clib_macro_init (&vam->macro_main);
   clib_macro_add_builtin (&vam->macro_main, "current_file",
@@ -327,6 +421,8 @@ main (int argc, char **argv)
   vec_validate (vam->cmd_reply, 0);
   vec_reset_length (vam->cmd_reply);
 
+  vat_find_plugin_path ();
+
   unformat_init_command_line (a, argv);
 
   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
@@ -388,7 +484,7 @@ main (int argc, char **argv)
   if (vam->socket_name && vat_socket_connect (vam))
     fformat (stderr, "WARNING: socket connection failed");
 
-  if (vam->socket_client_main.socket_fd == 0
+  if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
       && connect_to_vpe ("vpp_api_test") < 0)
     {
       svm_region_exit ();
@@ -403,9 +499,27 @@ main (int argc, char **argv)
 
   vec_validate (vam->inbuf, 4096);
 
+  load_features ();
+
   vam->current_file = (u8 *) "plugin-init";
   vat_plugin_init (vam);
 
+  /* Set up the init function hash table */
+  vm->init_functions_called = hash_create (0, 0);
+
+  /* Execute plugin init and api_init functions */
+  error = vlib_call_init_exit_functions
+    (vm, &vm->init_function_registrations, 1 /* call once */ );
+
+  if (error)
+    clib_error_report (error);
+
+  error = vlib_call_init_exit_functions
+    (vm, &vm->api_init_function_registrations, 1 /* call_once */ );
+
+  if (error)
+    clib_error_report (error);
+
   for (i = 0; i < vec_len (input_files); i++)
     {
       vam->ifp = fopen ((char *) input_files[i], "r");
@@ -432,6 +546,18 @@ main (int argc, char **argv)
       fclose (vam->ifp);
     }
 
+  /*
+   * Particularly when running a script, don't be in a hurry to leave.
+   * A reply message queued to this process will end up constipating
+   * the allocation rings.
+   */
+  timeout = vat_time_now (vam) + 2.0;
+  while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
+    ;
+
+  if (vat_time_now (vam) > timeout)
+    clib_warning ("BUG: message reply spin-wait timeout");
+
   vl_client_disconnect_from_vlib ();
   exit (0);
 }