Variable-message-length tracing support, VPP-370

Clean up several message handlers which spuriously depended on having
a vlib_main_t * pointer passed as a second argument. That definitely
doesn't happen when replaying an api trace...

Change-Id: Id4cf9745f770933566cb13698ee779333ee35d79
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/vlib-api/vlibapi/api_shared.c b/vlib-api/vlibapi/api_shared.c
index 2b2d81c..6a04fac 100644
--- a/vlib-api/vlibapi/api_shared.c
+++ b/vlib-api/vlibapi/api_shared.c
@@ -21,6 +21,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/mman.h>
@@ -85,8 +86,10 @@
   u8 **this_trace;
   u8 **old_trace;
   u8 *msg_copy;
+  u32 length;
   trace_cfg_t *cfgp;
   u16 msg_id = ntohs (*((u16 *) msg));
+  msgbuf_t *header = (msgbuf_t *) (((u8 *) msg) - offsetof (msgbuf_t, data));
 
   cfgp = am->api_trace_cfg + msg_id;
 
@@ -116,8 +119,10 @@
       this_trace = old_trace;
     }
 
-  vec_validate (msg_copy, cfgp->size - 1);
-  clib_memcpy (msg_copy, msg, cfgp->size);
+  length = clib_net_to_host_u32 (header->data_len);
+
+  vec_validate (msg_copy, length - 1);
+  clib_memcpy (msg_copy, msg, length);
   *this_trace = msg_copy;
 }
 
@@ -254,6 +259,7 @@
        */
       for (i = 0; i < vec_len (tp->traces); i++)
 	{
+	  u32 msg_length;
 	  /*sa_ignore NO_NULL_CHK */
 	  msg = tp->traces[i];
 	  /*
@@ -262,6 +268,13 @@
 	   */
 	  if (!msg)
 	    continue;
+
+	  msg_length = clib_host_to_net_u32 (vec_len (msg));
+	  if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
+	      != sizeof (msg_length))
+	    {
+	      return (-14);
+	    }
 	  if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
 	    {
 	      return (-11);
@@ -273,6 +286,7 @@
       /* Wrap case: write oldest -> end of buffer */
       for (i = tp->curindex; i < vec_len (tp->traces); i++)
 	{
+	  u32 msg_length;
 	  msg = tp->traces[i];
 	  /*
 	   * This retarded check required to pass
@@ -281,6 +295,13 @@
 	  if (!msg)
 	    continue;
 
+	  msg_length = clib_host_to_net_u32 (vec_len (msg));
+	  if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
+	      != sizeof (msg_length))
+	    {
+	      return (-14);
+	    }
+
 	  if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
 	    {
 	      return (-12);
@@ -289,6 +310,7 @@
       /* write beginning of buffer -> oldest-1 */
       for (i = 0; i < tp->curindex; i++)
 	{
+	  u32 msg_length;
 	  /*sa_ignore NO_NULL_CHK */
 	  msg = tp->traces[i];
 	  /*
@@ -298,6 +320,13 @@
 	  if (!msg)
 	    continue;
 
+	  msg_length = clib_host_to_net_u32 (vec_len (msg));
+	  if (fwrite (&msg_length, 1, sizeof (msg_length), fp)
+	      != sizeof (msg_length))
+	    {
+	      return (-14);
+	    }
+
 	  if (fwrite (msg, 1, vec_len (msg), fp) != vec_len (msg))
 	    {
 	      return (-13);
@@ -668,7 +697,6 @@
   c->cleanup = cleanup;
   c->endian = endian;
   c->print = print;
-  c->size = size;
   c->traced = traced;
   c->replay = 1;
   c->message_bounce = 0;
@@ -759,7 +787,7 @@
   u8 *msg;
   u8 endian_swap_needed = 0;
   api_main_t *am = &api_main;
-  static u8 *tmpbuf;
+  u8 *tmpbuf = 0;
   u32 nitems;
   void **saved_print_handlers = 0;
 
@@ -838,6 +866,9 @@
       int size;
       u16 msg_id;
 
+      size = clib_host_to_net_u32 (*(u32 *) msg);
+      msg += sizeof (u32);
+
       if (clib_arch_is_little_endian)
 	msg_id = ntohs (*((u16 *) msg));
       else
@@ -850,7 +881,6 @@
 	  munmap (hp, file_size);
 	  return;
 	}
-      size = cfgp->size;
       msg += size;
     }
 
@@ -864,6 +894,9 @@
       if (which == DUMP)
 	vlib_cli_output (vm, "---------- trace %d -----------\n", i);
 
+      size = clib_host_to_net_u32 (*(u32 *) msg);
+      msg += sizeof (u32);
+
       if (clib_arch_is_little_endian)
 	msg_id = ntohs (*((u16 *) msg));
       else
@@ -874,9 +907,9 @@
 	{
 	  vlib_cli_output (vm, "Ugh: msg id %d no trace config\n", msg_id);
 	  munmap (hp, file_size);
+	  vec_free (tmpbuf);
 	  return;
 	}
-      size = cfgp->size;
 
       /* Copy the buffer (from the read-only mmap'ed file) */
       vec_validate (tmpbuf, size - 1 + sizeof (uword));
@@ -897,6 +930,7 @@
 	    {
 	      vlib_cli_output (vm, "Ugh: msg id %d no endian swap\n", msg_id);
 	      munmap (hp, file_size);
+	      vec_free (tmpbuf);
 	      return;
 	    }
 	  endian_fp = am->msg_endian_handlers[msg_id];
@@ -997,6 +1031,7 @@
     }
 
   munmap (hp, file_size);
+  vec_free (tmpbuf);
 }
 
 u8 *