api: multiple connections per process
[vpp.git] / src / vlibmemory / vlib_api_cli.c
old mode 100644 (file)
new mode 100755 (executable)
index d6eca59..3f8e1c9
@@ -101,7 +101,7 @@ vl_api_client_command (vlib_main_t * vm,
   vl_api_registration_t **regpp, *regp;
   svm_queue_t *q;
   char *health;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   u32 *confused_indices = 0;
 
   if (!pool_elts (am->vl_clients))
@@ -161,7 +161,7 @@ static clib_error_t *
 vl_api_status_command (vlib_main_t * vm,
                       unformat_input_t * input, vlib_cli_command_t * cli_cmd)
 {
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
 
   /* check if rx_trace and tx_trace are not null pointers */
   if (am->rx_trace == 0)
@@ -228,7 +228,7 @@ vl_api_message_table_command (vlib_main_t * vm,
                              unformat_input_t * input,
                              vlib_cli_command_t * cli_cmd)
 {
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   int i;
   int verbose = 0;
 
@@ -304,7 +304,7 @@ vl_api_show_plugin_command (vlib_main_t * vm,
                            unformat_input_t * input,
                            vlib_cli_command_t * cli_cmd)
 {
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   vl_api_msg_range_t *rp = 0;
   int i;
 
@@ -402,10 +402,9 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
   struct stat statb;
   size_t file_size;
   u8 *msg;
-  u8 endian_swap_needed = 0;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   u8 *tmpbuf = 0;
-  u32 nitems;
+  u32 nitems, nitems_msgtbl;
   void **saved_print_handlers = 0;
 
   fd = open ((char *) filename, O_RDONLY);
@@ -443,14 +442,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
     }
   close (fd);
 
-  if ((clib_arch_is_little_endian && hp->endian == VL_API_BIG_ENDIAN)
-      || (clib_arch_is_big_endian && hp->endian == VL_API_LITTLE_ENDIAN))
-    endian_swap_needed = 1;
-
-  if (endian_swap_needed)
-    nitems = ntohl (hp->nitems);
-  else
-    nitems = hp->nitems;
+  nitems = ntohl (hp->nitems);
 
   if (last_index == (u32) ~ 0)
     {
@@ -473,9 +465,26 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       saved_print_handlers = (void **) vec_dup (am->msg_print_handlers);
       vl_msg_api_custom_dump_configure (am);
     }
+  msg = (u8 *) (hp + 1);
 
+  u16 *msgid_vec = 0;
+  serialize_main_t _sm, *sm = &_sm;
+  u32 msgtbl_size = ntohl (hp->msgtbl_size);
+  u8 *name_and_crc;
 
-  msg = (u8 *) (hp + 1);
+  unserialize_open_data (sm, msg, msgtbl_size);
+  unserialize_integer (sm, &nitems_msgtbl, sizeof (u32));
+
+  for (i = 0; i < nitems_msgtbl; i++)
+    {
+      u16 msg_index = unserialize_likely_small_unsigned_integer (sm);
+      unserialize_cstring (sm, (char **) &name_and_crc);
+      u16 msg_index2 = vl_msg_api_get_msg_index (name_and_crc);
+      vec_validate (msgid_vec, msg_index);
+      msgid_vec[msg_index] = msg_index2;
+    }
+
+  msg += msgtbl_size;
 
   for (i = 0; i < first_index; i++)
     {
@@ -486,11 +495,9 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       size = clib_host_to_net_u32 (*(u32 *) msg);
       msg += sizeof (u32);
 
-      if (clib_arch_is_little_endian)
-       msg_id = ntohs (*((u16 *) msg));
-      else
-       msg_id = *((u16 *) msg);
-
+      msg_id = ntohs (*((u16 *) msg));
+      if (msg_id < vec_len (msgid_vec))
+       msg_id = msgid_vec[msg_id];
       cfgp = am->api_trace_cfg + msg_id;
       if (!cfgp)
        {
@@ -507,7 +514,6 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
   for (; i <= last_index; i++)
     {
       trace_cfg_t *cfgp;
-      u16 *msg_idp;
       u16 msg_id;
       int size;
 
@@ -517,10 +523,11 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       size = clib_host_to_net_u32 (*(u32 *) msg);
       msg += sizeof (u32);
 
-      if (clib_arch_is_little_endian)
-       msg_id = ntohs (*((u16 *) msg));
-      else
-       msg_id = *((u16 *) msg);
+      msg_id = ntohs (*((u16 *) msg));
+      if (msg_id < vec_len (msgid_vec))
+       {
+         msg_id = msgid_vec[msg_id];
+       }
 
       cfgp = am->api_trace_cfg + msg_id;
       if (!cfgp)
@@ -535,15 +542,14 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       /* Copy the buffer (from the read-only mmap'ed file) */
       vec_validate (tmpbuf, size - 1 + sizeof (uword));
       clib_memcpy (tmpbuf + sizeof (uword), msg, size);
-      memset (tmpbuf, 0xf, sizeof (uword));
+      clib_memset (tmpbuf, 0xf, sizeof (uword));
 
       /*
-       * Endian swap if needed. All msg data is supposed to be
-       * in network byte order. All msg handlers are supposed to
-       * know that. The generic message dumpers don't know that.
-       * One could fix apigen, I suppose.
+       * Endian swap if needed. All msg data is supposed to be in
+       * network byte order.
        */
-      if ((which == DUMP && clib_arch_is_little_endian) || endian_swap_needed)
+      if (((which == DUMP || which == CUSTOM_DUMP)
+          && clib_arch_is_little_endian))
        {
          void (*endian_fp) (void *);
          if (msg_id >= vec_len (am->msg_endian_handlers)
@@ -562,7 +568,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
       /* msg_id always in network byte order */
       if (clib_arch_is_little_endian)
        {
-         msg_idp = (u16 *) (tmpbuf + sizeof (uword));
+         u16 *msg_idp = (u16 *) (tmpbuf + sizeof (uword));
          *msg_idp = msg_id;
        }
 
@@ -662,9 +668,10 @@ api_trace_command_fn (vlib_main_t * vm,
                      unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   u32 nitems = 256 << 10;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   vl_api_trace_which_t which = VL_API_TRACE_RX;
-  u8 *filename;
+  u8 *filename = 0;
+  u8 *chroot_filename = 0;
   u32 first = 0;
   u32 last = (u32) ~ 0;
   FILE *fp;
@@ -685,13 +692,12 @@ api_trace_command_fn (vlib_main_t * vm,
        }
       else if (unformat (input, "save %s", &filename))
        {
-         u8 *chroot_filename;
          if (strstr ((char *) filename, "..")
              || index ((char *) filename, '/'))
            {
              vlib_cli_output (vm, "illegal characters in filename '%s'",
                               filename);
-             return 0;
+             goto out;
            }
 
          chroot_filename = format (0, "/tmp/%s%c", filename, 0);
@@ -702,7 +708,7 @@ api_trace_command_fn (vlib_main_t * vm,
          if (fp == NULL)
            {
              vlib_cli_output (vm, "Couldn't create %s\n", chroot_filename);
-             return 0;
+             goto out;
            }
          rv = vl_msg_api_trace_save (am, which, fp);
          fclose (fp);
@@ -724,7 +730,7 @@ api_trace_command_fn (vlib_main_t * vm,
            vlib_cli_output (vm, "Unknown error while saving: %d", rv);
          else
            vlib_cli_output (vm, "API trace saved to %s\n", chroot_filename);
-         vec_free (chroot_filename);
+         goto out;
        }
       else if (unformat (input, "dump %s", &filename))
        {
@@ -772,6 +778,9 @@ api_trace_command_fn (vlib_main_t * vm,
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);
     }
+out:
+  vec_free (filename);
+  vec_free (chroot_filename);
   return 0;
 }
 
@@ -795,7 +804,7 @@ vl_api_trace_command (vlib_main_t * vm,
 {
   u32 nitems = 1024;
   vl_api_trace_which_t which = VL_API_TRACE_RX;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -861,8 +870,8 @@ configure:
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (trace, static) =
 {
-  .path = "set api-trace [on][on tx][on rx][off][free][debug on][debug off]",
-  .short_help = "API trace",
+  .path = "set api-trace",
+  .short_help = "API trace [on][on tx][on rx][off][free][debug on][debug off]",
   .function = vl_api_trace_command,
 };
 /* *INDENT-ON* */
@@ -872,7 +881,7 @@ api_trace_config_fn (vlib_main_t * vm, unformat_input_t * input)
 {
   u32 nitems = 256 << 10;
   vl_api_trace_which_t which = VL_API_TRACE_RX;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -905,7 +914,7 @@ VLIB_CONFIG_FUNCTION (api_trace_config_fn, "api-trace");
 static clib_error_t *
 api_queue_config_fn (vlib_main_t * vm, unformat_input_t * input)
 {
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   u32 nitems;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
@@ -995,7 +1004,7 @@ dump_api_table_file_command_fn (vlib_main_t * vm,
                                vlib_cli_command_t * cmd)
 {
   u8 *filename = 0;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   serialize_main_t _sm, *sm = &_sm;
   clib_error_t *error;
   u32 nmsgs;
@@ -1048,7 +1057,7 @@ dump_api_table_file_command_fn (vlib_main_t * vm,
       item->crc = extract_crc (name_and_crc);
       item->which = 0;         /* file */
     }
-  serialize_close (sm);
+  unserialize_close (sm);
 
   /* Compare with the current image? */
   if (compare_current)
@@ -1082,6 +1091,7 @@ dump_api_table_file_command_fn (vlib_main_t * vm,
 
   if (compare_current)
     {
+      u8 *dashes = 0;
       ndifferences = 0;
 
       /*
@@ -1090,8 +1100,11 @@ dump_api_table_file_command_fn (vlib_main_t * vm,
        * are identical. Otherwise, the crc is different, or a message is
        * present in only one of the tables.
        */
-      vlib_cli_output (vm, "%=60s %s", "Message Name", "Result");
-
+      vlib_cli_output (vm, "%-60s | %s", "Message Name", "Result");
+      vec_validate_init_empty (dashes, 60, '-');
+      vec_terminate_c_string (dashes);
+      vlib_cli_output (vm, "%60s-|-%s", dashes, "-----------------");
+      vec_free (dashes);
       for (i = 0; i < vec_len (table);)
        {
          /* Last message lonely? */
@@ -1114,24 +1127,24 @@ dump_api_table_file_command_fn (vlib_main_t * vm,
          ndifferences++;
 
          /* Only in one of two tables? */
-         if (strncmp ((char *) table[i].name, (char *) table[i + 1].name,
-                      vec_len (table[i].name)))
+         if (i + 1 == vec_len (table)
+             || strcmp ((char *) table[i].name, (char *) table[i + 1].name))
            {
            last_unique:
-             vlib_cli_output (vm, "%-60s only in %s",
+             vlib_cli_output (vm, "%-60s only in %s",
                               table[i].name, table[i].which ?
                               "image" : "file");
              i++;
              continue;
            }
          /* In both tables, but with different signatures */
-         vlib_cli_output (vm, "%-60s definition changed", table[i].name);
+         vlib_cli_output (vm, "%-60s definition changed", table[i].name);
          i += 2;
        }
       if (ndifferences == 0)
        vlib_cli_output (vm, "No api message signature differences found.");
       else
-       vlib_cli_output (vm, "Found %u api message signature differences",
+       vlib_cli_output (vm, "\nFound %u api message signature differences",
                         ndifferences);
       goto cleanup;
     }