misc: move part of vpe apis to vlibmemory
[vpp.git] / src / tools / vppapigen / vppapigen_c.py
index 9b16413..2d526c1 100644 (file)
@@ -380,7 +380,7 @@ class FromJSON():
         int i;
         cJSON *array = cJSON_GetObjectItem(o, "{n}");
         int size = cJSON_GetArraySize(array);
-        if (size != {lfield}) return 0;
+        if (size != {lfield}) goto error;
         for (i = 0; i < size; i++) {{
             cJSON *e = cJSON_GetArrayItem(array, i);
             {call}
@@ -416,7 +416,7 @@ class FromJSON():
             if o.lengthfield:
                 write('    s = u8string_fromjson(o, "{}");\n'
                       .format(o.fieldname))
-                write('    if (!s) return 0;\n')
+                write('    if (!s) goto error;\n')
                 write('    {} = vec_len(s);\n'.format(lfield))
 
                 write('    {realloc} = realloc({realloc}, {msgsize} + '
@@ -427,7 +427,7 @@ class FromJSON():
 
                 write('    vec_free(s);\n')
             else:
-                write('    u8string_fromjson2(o, "{n}", a->{n});\n'
+                write('    if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n'
                       .format(n=o.fieldname))
             return
 
@@ -438,7 +438,7 @@ class FromJSON():
                 call = ('vl_api_{t}_fromjson(e, &d[i]);'
                         .format(t=o.fieldtype))
             else:
-                call = ('{t}_fromjson({msgvar}, len, e, &d[i]); '
+                call = ('if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; '
                         .format(t=o.fieldtype, msgvar=msgvar))
             write(forloop_vla.format(lfield=lfield,
                                      t=o.fieldtype,
@@ -547,6 +547,7 @@ class FromJSON():
     def print_define(self, o):
         '''Convert JSON object to VPP API message'''
         write = self.stream.write
+        error = 0
         write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson '
               '(cJSON *o, int *len) {{\n'.format(name=o.name))
         write('    cJSON *item __attribute__ ((unused));\n')
@@ -563,14 +564,17 @@ class FromJSON():
             write('    item = cJSON_GetObjectItem(o, "{}");\n'
                   .format(t.fieldname))
             write('    if (!item) goto error;\n')
+            error += 1
             self._dispatch[t.type](self, t, toplevel=True)
             write('\n')
 
         write('    *len = l;\n')
         write('    return a;\n')
-        write('\n  error:\n')
-        write('    free(a);\n')
-        write('    return 0;\n')
+
+        if error:
+            write('\n  error:\n')
+            write('    free(a);\n')
+            write('    return 0;\n')
         write('}\n')
 
     def print_using(self, o):
@@ -1206,7 +1210,7 @@ def generate_include_counters(s, stream):
         write('   {}_N_ERROR\n'.format(csetname.upper()))
         write('}} vl_counter_{}_enum_t;\n'.format(csetname))
 
-        write('extern vl_counter_t {}_error_counters[];\n'.format(csetname))
+        write('extern vlib_error_desc_t {}_error_counters[];\n'.format(csetname))
 
 
 def generate_include_types(s, module, stream):
@@ -1340,6 +1344,8 @@ def generate_c_boilerplate(services, defines, counters, file_crc,
               '   .cleanup = vl_noop_handler,\n'
               '   .endian = vl_api_{n}_t_endian,\n'
               '   .print = vl_api_{n}_t_print,\n'
+              '   .traced = 1,\n'
+              '   .replay = 1,\n'
               '   .is_autoendian = {auto}}};\n'
               .format(n=s.caller, ID=s.caller.upper(),
                       auto=d.autoendian))
@@ -1369,7 +1375,7 @@ def generate_c_boilerplate(services, defines, counters, file_crc,
 
     for cnt in counters:
         csetname = cnt.name
-        write('vl_counter_t {}_error_counters[] = {{\n'.format(csetname))
+        write('vlib_error_desc_t {}_error_counters[] = {{\n'.format(csetname))
         for c in cnt.block:
             write('  {\n')
             write('   .name = "{}",\n'.format(c['name']))
@@ -1472,11 +1478,7 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin,
                   .format(n=e, ID=e.upper()))
 
     write('}\n')
-    if plugin:
-        write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
-    else:
-        write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n'
-              .format(module))
+    write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
     write('{\n')
     write('   {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
     write('   mainp->vat_main = vam;\n')
@@ -1530,6 +1532,7 @@ api_{n} (cJSON *o)
   char *p;
   int l;
   vac_read(&p, &l, 5); // XXX: Fix timeout
+  if (p == 0 || l == 0) return 0;
     // XXX Will fail in case of event received. Do loop
   if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{
     fprintf(stderr, "Mismatched reply\\n");
@@ -1569,6 +1572,10 @@ api_{n} (cJSON *o)
     char *p;
     int l;
     vac_read(&p, &l, 5); // XXX: Fix timeout
+    if (p == 0 || l == 0) {{
+      cJSON_free(reply);
+      return 0;
+    }}
 
     /* Message can be one of [_details, control_ping_reply
      * or unrelated event]
@@ -1579,6 +1586,10 @@ api_{n} (cJSON *o)
     }}
 
     if (reply_msg_id == details_msg_id) {{
+        if (l < sizeof(vl_api_{r}_t)) {{
+            cJSON_free(reply);
+            return 0;
+        }}
         vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
         vl_api_{r}_t_endian(rmp);
         cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
@@ -1670,7 +1681,9 @@ def generate_c_test2_boilerplate(services, defines, module, stream):
 #include <vnet/ethernet/ethernet_format_fns.h>
 
 #define vl_typedefs             /* define message structures */
-#include <vpp/api/vpe_all_api_h.h>
+#include <vlibmemory/vl_memory_api_h.h>
+#include <vpp/api/vpe_types.api.h>
+#include <vpp/api/vpe.api.h>
 #undef vl_typedefs
 
 #include "{module}.api_enum.h"
@@ -1700,12 +1713,12 @@ def generate_c_test2_boilerplate(services, defines, module, stream):
             continue
         c_test_api_service(s, s.stream, stream)
 
-    write('void vat2_register_function(char *, cJSON * (*)(cJSON *));\n')
+    write('void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *));\n')
     # write('__attribute__((constructor))')
     write('clib_error_t *\n')
     write('vat2_register_plugin (void) {\n')
     for s in services:
-        write('   vat2_register_function("{n}", api_{n});\n'
+        write('   vat2_register_function("{n}", api_{n}, (cJSON * (*)(void *))vl_api_{n}_t_tojson);\n'
               .format(n=s.caller))
     write('   return 0;\n')
     write('}\n')