vat2: fix argument parsing
[vpp.git] / src / vat2 / main.c
index b3606f3..1aea690 100644 (file)
@@ -18,7 +18,7 @@
 #include <stdbool.h>
 #include <ctype.h>
 #include <getopt.h>
-#include <assert.h>
+#include <string.h>
 #include <vlib/vlib.h>
 #include <vlibapi/api_types.h>
 #include <vppinfra/hash.h>
 #include <limits.h>
 #include "vat2.h"
 
+/*
+ * Filter these messages as they are used to manage the API connection to VPP
+ */
+char *filter_messages_strings[] = { "memclnt_create",
+                                   "memclnt_delete",
+                                   "sockclnt_create",
+                                   "sockclnt_delete",
+                                   "memclnt_rx_thread_suspend",
+                                   "memclnt_read_timeout",
+                                   "rx_thread_exit",
+                                   "trace_plugin_msg_ids",
+                                   0 };
+
+static bool
+filter_message (char *msgname)
+{
+  char **p = filter_messages_strings;
+
+  while (*p)
+    {
+      if (strcmp (*p, msgname) == 0)
+       return true;
+      p++;
+    }
+  return false;
+}
+
 uword *function_by_name;
 bool debug = false;
 
@@ -58,8 +85,7 @@ vat2_find_plugin_path ()
     return;
   *p = 0;
 
-  s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vat2_plugins:"
-              "%s/lib/vat2_plugins", path, path);
+  s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path);
   vec_add1 (s, 0);
   vat2_plugin_path = (char *) s;
 }
@@ -89,15 +115,16 @@ struct apifuncs_s
 {
   cJSON (*f) (cJSON *);
   cJSON (*tojson) (void *);
+  u32 crc;
 };
 
 struct apifuncs_s *apifuncs = 0;
 
 void
 vat2_register_function (char *name, cJSON (*f) (cJSON *),
-                       cJSON (*tojson) (void *))
+                       cJSON (*tojson) (void *), u32 crc)
 {
-  struct apifuncs_s funcs = { .f = f, .tojson = tojson };
+  struct apifuncs_s funcs = { .f = f, .tojson = tojson, .crc = crc };
   vec_add1 (apifuncs, funcs);
   hash_set_mem (function_by_name, name, vec_len (apifuncs) - 1);
 }
@@ -105,12 +132,27 @@ vat2_register_function (char *name, cJSON (*f) (cJSON *),
 static int
 vat2_exec_command_by_name (char *msgname, cJSON *o)
 {
+  u32 crc = 0;
+  if (filter_message (msgname))
+    return 0;
+
+  cJSON *crc_obj = cJSON_GetObjectItem (o, "_crc");
+  if (crc_obj)
+    {
+      char *crc_str = cJSON_GetStringValue (crc_obj);
+      crc = (u32) strtol (crc_str, NULL, 16);
+    }
+
   uword *p = hash_get_mem (function_by_name, msgname);
   if (!p)
     {
-      fprintf (stderr, "No such command %s", msgname);
+      fprintf (stderr, "No such command %s\n", msgname);
       return -1;
     }
+  if (crc && crc != apifuncs[p[0]].crc)
+    {
+      fprintf (stderr, "API CRC does not match: %s!\n", msgname);
+    }
 
   cJSON *(*fp) (cJSON *);
   fp = (void *) apifuncs[p[0]].f;
@@ -143,29 +185,45 @@ vat2_exec_command (cJSON *o)
     }
 
   char *name = cJSON_GetStringValue (msg_id_obj);
-  assert (name);
+
   return vat2_exec_command_by_name (name, o);
 }
+
 static void
 print_template (char *msgname)
 {
   uword *p = hash_get_mem (function_by_name, msgname);
   if (!p)
-    {
-      fprintf (stderr, "no such message: %s", msgname);
-    }
+    goto error;
+
   cJSON *(*fp) (void *);
   fp = (void *) apifuncs[p[0]].tojson;
-  assert (fp);
+  if (!fp)
+    goto error;
+
   void *scratch = malloc (2048);
+  if (!scratch)
+    goto error;
+
   memset (scratch, 0, 2048);
   cJSON *t = fp (scratch);
+  if (!t)
+    goto error;
   free (scratch);
   char *output = cJSON_Print (t);
+  if (!output)
+    goto error;
+
   cJSON_Delete (t);
   printf ("%s\n", output);
   free (output);
+
+  return;
+
+error:
+  fprintf (stderr, "error printing template for: %s\n", msgname);
 }
+
 static void
 dump_apis (void)
 {
@@ -181,14 +239,15 @@ print_help (void)
     "Usage: vat2 [OPTION] <message-name> <JSON object>\n"
     "Send API message to VPP and print reply\n"
     "\n"
-    "-d, --debug       Print additional information\n"
-    "-p, --prefix      Specify shared memory prefix to connect to a given VPP "
-    "instance\n"
-    "-f, --file        File containing a JSON object with the arguments for "
-    "the message to send\n"
-    "--dump-apis       List all APIs available in VAT2 (might not reflect "
-    "running VPP)\n"
-    "-t, --template    Print a template JSON object for given API message\n"
+    "-d, --debug                    Print additional information\n"
+    "-p, --prefix <prefix>          Specify shared memory prefix to connect "
+    "to a given VPP instance\n"
+    "-f, --file <filename>          File containing a JSON object with the "
+    "arguments for the message to send\n"
+    "-t, --template <message-name>  Print a template JSON object for given API"
+    " message\n"
+    "--dump-apis                    List all APIs available in VAT2 (might "
+    "not reflect running VPP)\n"
     "\n";
   printf ("%s", help_string);
 }
@@ -197,26 +256,25 @@ int main (int argc, char **argv)
 {
   /* Create a heap of 64MB */
   clib_mem_init (0, 64 << 20);
-  char *filename = 0, *prefix = 0;
+  char *filename = 0, *prefix = 0, *template = 0;
   int index;
   int c;
   opterr = 0;
   cJSON *o = 0;
   int option_index = 0;
   bool dump_api = false;
-  bool template = false;
+  bool debug = false;
   char *msgname = 0;
-  static int debug_flag;
   static struct option long_options[] = {
-    { "debug", no_argument, &debug_flag, 1 },
-    { "prefix", optional_argument, 0, 'p' },
+    { "debug", no_argument, 0, 'd' },
+    { "prefix", required_argument, 0, 'p' },
     { "file", required_argument, 0, 'f' },
     { "dump-apis", no_argument, 0, 0 },
-    { "template", no_argument, 0, 't' },
+    { "template", required_argument, 0, 't' },
     { 0, 0, 0, 0 }
   };
 
-  while ((c = getopt_long (argc, argv, "hdp:f:", long_options,
+  while ((c = getopt_long (argc, argv, "hdp:f:t:", long_options,
                           &option_index)) != -1)
     {
       switch (c)
@@ -229,7 +287,7 @@ int main (int argc, char **argv)
          debug = true;
          break;
        case 't':
-         template = true;
+         template = optarg;
          break;
        case 'p':
          prefix = optarg;
@@ -244,9 +302,8 @@ int main (int argc, char **argv)
          abort ();
        }
     }
-  debug = debug_flag == 1 ? true : false;
-  DBG ("debug = %d, filename = %s shared memory prefix: %s\n", debug, filename,
-       prefix);
+  DBG ("debug = %d, filename = %s, template = %s, shared memory prefix: %s\n",
+       debug, filename, template, prefix);
 
   for (index = optind; index < argc; index++)
     DBG ("Non-option argument %s\n", argv[index]);
@@ -269,7 +326,7 @@ int main (int argc, char **argv)
 
   if (template)
     {
-      print_template (argv[index]);
+      print_template (template);
       exit (0);
     }
 
@@ -292,6 +349,12 @@ int main (int argc, char **argv)
     }
   }
 
+  if (!msgname && !filename)
+    {
+      print_help ();
+      exit (-1);
+    }
+
   /* Read message from file */
   if (filename) {
       if (argc > index)
@@ -310,6 +373,7 @@ int main (int argc, char **argv)
       fprintf(stderr, "%s: can't open file: %s\n", argv[0], filename);
       exit(-1);
     }
+
     chunksize = bufsize = 1024;
     char *buf = malloc(bufsize);
     while ((n = fread (buf + n_read, 1, chunksize, f)))
@@ -324,17 +388,17 @@ int main (int argc, char **argv)
     fclose(f);
     if (n_read) {
       o = cJSON_Parse(buf);
-      free(buf);
       if (!o) {
         fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr());
         exit(-1);
       }
     }
+    free (buf);
   }
 
-  if (!msgname && !filename)
+  if (!o)
     {
-      print_help ();
+      fprintf (stderr, "%s: Failed parsing JSON input\n", argv[0]);
       exit (-1);
     }