api: provide api definition over api
[vpp.git] / src / vlibmemory / memclnt_api.c
index e7d689b..299e8d9 100644 (file)
@@ -29,6 +29,7 @@
 #include <vlib/unix/unix.h>
 #include <vlibapi/api.h>
 #include <vlibmemory/api.h>
+#include <vlibapi/api_helper_macros.h>
 
 /**
  * @file
@@ -47,7 +48,6 @@
 #undef vl_typedefs
 
 /* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
 #define vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_endianfun
 
+#define vl_calcsizefun
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_calcsizefun
+
 static void
 vl_api_get_first_msg_id_t_handler (vl_api_get_first_msg_id_t *mp)
 {
@@ -131,9 +135,54 @@ vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
   vl_api_send_msg (reg, (u8 *) rmp);
 }
 
+static void
+vl_api_control_ping_t_handler (vl_api_control_ping_t *mp)
+{
+  vl_api_control_ping_reply_t *rmp;
+  int rv = 0;
+
+  REPLY_MACRO2 (VL_API_CONTROL_PING_REPLY,
+               ({ rmp->vpe_pid = ntohl (getpid ()); }));
+}
+
+static void
+vl_api_get_api_json_t_handler (vl_api_get_api_json_t *mp)
+{
+  vl_api_get_api_json_reply_t *rmp;
+  api_main_t *am = vlibapi_get_main ();
+  int rv = 0, n = 0;
+  u8 *s = 0;
+
+  vl_api_registration_t *rp =
+    vl_api_client_index_to_registration (mp->client_index);
+  if (rp == 0)
+    return;
+
+  s = format (s, "[\n");
+  u8 **ptr;
+  vec_foreach (ptr, am->json_api_repr)
+    {
+      s = format (s, "%s,", ptr[0]);
+    }
+  s[vec_len (s) - 1] = ']'; // Replace last comma with a bracket
+  vec_terminate_c_string (s);
+  n = vec_len (s);
+
+done:
+  REPLY_MACRO3 (VL_API_GET_API_JSON_REPLY, n, ({
+                 if (rv == 0)
+                   {
+                     vl_api_c_string_to_api_string ((char *) s, &rmp->json);
+                   }
+               }));
+  vec_free (s);
+}
+
 #define foreach_vlib_api_msg                                                  \
   _ (GET_FIRST_MSG_ID, get_first_msg_id)                                      \
-  _ (API_VERSIONS, api_versions)
+  _ (API_VERSIONS, api_versions)                                              \
+  _ (CONTROL_PING, control_ping)                                              \
+  _ (GET_API_JSON, get_api_json)
 
 /*
  * vl_api_init
@@ -141,6 +190,7 @@ vl_api_api_versions_t_handler (vl_api_api_versions_t *mp)
 static int
 vlib_api_init (void)
 {
+  api_main_t *am = vlibapi_get_main ();
   vl_msg_api_msg_config_t cfg;
   vl_msg_api_msg_config_t *c = &cfg;
 
@@ -158,12 +208,11 @@ vlib_api_init (void)
       c->id = VL_API_##N;                                                     \
       c->name = #n;                                                           \
       c->handler = vl_api_##n##_t_handler;                                    \
-      c->cleanup = vl_noop_handler;                                           \
       c->endian = vl_api_##n##_t_endian;                                      \
-      c->print = vl_api_##n##_t_print;                                        \
-      c->print_json = vl_api_##n##_t_print_json;                              \
+      c->format_fn = vl_api_##n##_t_format;                                   \
       c->tojson = vl_api_##n##_t_tojson;                                      \
       c->fromjson = vl_api_##n##_t_fromjson;                                  \
+      c->calc_size = vl_api_##n##_t_calc_size;                                \
       c->size = sizeof (vl_api_##n##_t);                                      \
       c->traced = 1;        /* trace, so these msgs print */                 \
       c->replay = 0;        /* don't replay client create/delete msgs */     \
@@ -175,6 +224,12 @@ vlib_api_init (void)
   foreach_vlib_api_msg;
 #undef _
 
+  /* Mark messages as mp safe */
+  vl_api_set_msg_thread_safe (am, VL_API_GET_FIRST_MSG_ID, 1);
+  vl_api_set_msg_thread_safe (am, VL_API_API_VERSIONS, 1);
+  vl_api_set_msg_thread_safe (am, VL_API_CONTROL_PING, 1);
+  vl_api_set_msg_thread_safe (am, VL_API_CONTROL_PING_REPLY, 1);
+
   return 0;
 }
 
@@ -479,7 +534,7 @@ api_rx_from_node (vlib_main_t *vm, vlib_node_runtime_t *node,
       if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
        {
          ASSERT (long_msg != 0);
-         _vec_len (long_msg) = 0;
+         vec_set_len (long_msg, 0);
          vec_add (long_msg, msg, msg_len);
          while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
            {
@@ -489,8 +544,9 @@ api_rx_from_node (vlib_main_t *vm, vlib_node_runtime_t *node,
              vec_add (long_msg, msg, msg_len);
            }
          msg = long_msg;
+         msg_len = vec_len (long_msg);
        }
-      vl_msg_api_handler_no_trace_no_free (msg);
+      vl_msg_api_handler_no_trace_no_free (msg, msg_len);
     }
 
   /* Free what we've been given. */
@@ -688,27 +744,40 @@ rpc_api_hookup (vlib_main_t *vm)
 {
   api_main_t *am = vlibapi_get_main ();
 #define _(N, n)                                                               \
-  vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler,            \
-                          vl_noop_handler, vl_noop_handler,                  \
-                          vl_api_##n##_t_print, sizeof (vl_api_##n##_t),     \
-                          0 /* do not trace */, vl_api_##n##_t_print_json,   \
-                          vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
+  vl_msg_api_config (&(vl_msg_api_msg_config_t){                              \
+    .id = VL_API_##N,                                                         \
+    .name = #n,                                                               \
+    .handler = vl_api_##n##_t_handler,                                        \
+    .format_fn = vl_api_##n##_t_format,                                       \
+    .size = sizeof (vl_api_##n##_t),                                          \
+    .traced = 0,                                                              \
+    .tojson = vl_api_##n##_t_tojson,                                          \
+    .fromjson = vl_api_##n##_t_fromjson,                                      \
+    .calc_size = vl_api_##n##_t_calc_size,                                    \
+  });
   foreach_rpc_api_msg;
 #undef _
 
 #define _(N, n)                                                               \
-  vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler,            \
-                          vl_noop_handler, vl_noop_handler,                  \
-                          vl_api_##n##_t_print, sizeof (vl_api_##n##_t),     \
-                          1 /* do trace */, vl_api_##n##_t_print_json,       \
-                          vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
-  foreach_plugin_trace_msg;
+  vl_msg_api_config (&(vl_msg_api_msg_config_t){                              \
+    .id = VL_API_##N,                                                         \
+    .name = #n,                                                               \
+    .handler = vl_api_##n##_t_handler,                                        \
+    .endian = vl_api_##n##_t_endian,                                          \
+    .format_fn = vl_api_##n##_t_format,                                       \
+    .size = sizeof (vl_api_##n##_t),                                          \
+    .traced = 1,                                                              \
+    .tojson = vl_api_##n##_t_tojson,                                          \
+    .fromjson = vl_api_##n##_t_fromjson,                                      \
+    .calc_size = vl_api_##n##_t_calc_size,                                    \
+  });
+  foreach_plugin_trace_msg
 #undef _
 
-  am->api_trace_cfg[VL_API_TRACE_PLUGIN_MSG_IDS].replay_enable = 0;
+    vl_api_allow_msg_replay (am, VL_API_TRACE_PLUGIN_MSG_IDS, 0);
 
   /* No reason to halt the parade to create a trace record... */
-  am->is_mp_safe[VL_API_TRACE_PLUGIN_MSG_IDS] = 1;
+  vl_api_set_msg_thread_safe (am, VL_API_TRACE_PLUGIN_MSG_IDS, 1);
   rpc_call_main_thread_cb_fn = vl_api_rpc_call_main_thread;
   return 0;
 }