interface: add api test file
[vpp.git] / src / vpp-api / vapi / vapi.c
index ef31724..513ff98 100644 (file)
@@ -32,6 +32,7 @@
 #include <vlibmemory/memory_client.h>
 
 #include <vapi/memclnt.api.vapi.h>
+#include <vapi/vlib.api.vapi.h>
 
 /* we need to use control pings for some stuff and because we're forced to put
  * the code in headers, we need a way to be able to grab the ids of these
@@ -40,7 +41,7 @@ vapi_msg_id_t vapi_msg_id_control_ping = 0;
 vapi_msg_id_t vapi_msg_id_control_ping_reply = 0;
 
 DEFINE_VAPI_MSG_IDS_MEMCLNT_API_JSON;
-DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
+DEFINE_VAPI_MSG_IDS_VLIB_API_JSON;
 
 struct
 {
@@ -221,6 +222,10 @@ vapi_msg_alloc (vapi_ctx_t ctx, size_t size)
       return NULL;
     }
   void *rv = vl_msg_api_alloc_or_null (size);
+  if (rv)
+    {
+      clib_memset (rv, 0, size);
+    }
   return rv;
 }
 
@@ -447,7 +452,7 @@ vapi_send (vapi_ctx_t ctx, void *msg)
       goto out;
     }
   int tmp;
-  svm_queue_t *q = api_main.shmem_hdr->vl_input_queue;
+  svm_queue_t *q = vlibapi_get_main ()->shmem_hdr->vl_input_queue;
 #if VAPI_DEBUG
   unsigned msgid = be16toh (*(u16 *) msg);
   if (msgid <= ctx->vl_msg_id_max)
@@ -474,6 +479,8 @@ vapi_send (vapi_ctx_t ctx, void *msg)
     {
       rv = VAPI_EAGAIN;
     }
+  else
+    VL_MSG_API_POISON (msg);
 out:
   VAPI_DBG ("vapi_send() rv = %d", rv);
   return rv;
@@ -488,7 +495,7 @@ vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
       rv = VAPI_EINVAL;
       goto out;
     }
-  svm_queue_t *q = api_main.shmem_hdr->vl_input_queue;
+  svm_queue_t *q = vlibapi_get_main ()->shmem_hdr->vl_input_queue;
 #if VAPI_DEBUG
   unsigned msgid1 = be16toh (*(u16 *) msg1);
   unsigned msgid2 = be16toh (*(u16 *) msg2);
@@ -518,6 +525,8 @@ vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2)
     {
       rv = VAPI_EAGAIN;
     }
+  else
+    VL_MSG_API_POISON (msg1);
 out:
   VAPI_DBG ("vapi_send() rv = %d", rv);
   return rv;
@@ -532,7 +541,7 @@ vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size,
       return VAPI_EINVAL;
     }
   vapi_error_e rv = VAPI_OK;
-  api_main_t *am = &api_main;
+  api_main_t *am = vlibapi_get_main ();
   uword data;
 
   if (am->our_pid == 0)
@@ -548,6 +557,7 @@ again:
 
   if (tmp == 0)
     {
+      VL_MSG_API_UNPOISON ((void *) data);
 #if VAPI_DEBUG_ALLOC
       vapi_add_to_be_freed ((void *) data);
 #endif
@@ -855,7 +865,7 @@ vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id)
 int
 vapi_get_client_index (vapi_ctx_t ctx)
 {
-  return api_main.my_client_index;
+  return vlibapi_get_main ()->my_client_index;
 }
 
 bool