api: verify message size on receipt
[vpp.git] / src / vlibmemory / memory_client.c
index b509063..54bc8d8 100644 (file)
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_endianfun
 
+#define vl_calcsizefun
+#include <vlibmemory/vl_memory_api_h.h>
+#undef vl_calcsizefun
+
 /* instantiate all the print functions we know about */
 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
 #define vl_printfun
@@ -63,7 +67,7 @@ rx_thread_fn (void *arg)
 
   vlibapi_set_main (a->am);
   vlibapi_set_memory_client_main (a->mm);
-  clib_mem_free (a);
+  free (a);
 
   mm = vlibapi_get_memory_client_main ();
   q = vlibapi_get_main ()->vl_input_queue;
@@ -160,7 +164,6 @@ void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
 int
 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
 {
-  svm_region_t *svm;
   vl_api_memclnt_create_t *mp;
   vl_api_memclnt_create_reply_t *rp;
   svm_queue_t *vl_input_queue;
@@ -181,7 +184,6 @@ vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
       return -1;
     }
 
-  svm = am->vlib_rp;
   shmem_hdr = am->shmem_hdr;
 
   if (shmem_hdr == 0 || shmem_hdr->vl_input_queue == 0)
@@ -193,12 +195,10 @@ vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
   CLIB_MEM_UNPOISON (shmem_hdr, sizeof (*shmem_hdr));
   VL_MSG_API_SVM_QUEUE_UNPOISON (shmem_hdr->vl_input_queue);
 
-  pthread_mutex_lock (&svm->mutex);
-  oldheap = svm_push_data_heap (svm);
+  oldheap = vl_msg_push_heap ();
   vl_input_queue = svm_queue_alloc_and_init (input_queue_size, sizeof (uword),
                                             getpid ());
-  svm_pop_heap (oldheap);
-  pthread_mutex_unlock (&svm->mutex);
+  vl_msg_pop_heap (oldheap);
 
   am->my_client_index = ~0;
   am->my_registration = 0;
@@ -244,7 +244,8 @@ vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
        }
       rv = clib_net_to_host_u32 (rp->response);
 
-      vl_msg_api_handler ((void *) rp);
+      msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
+      vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
       break;
     }
   return (rv);
@@ -256,11 +257,9 @@ vl_api_memclnt_delete_reply_t_handler (vl_api_memclnt_delete_reply_t * mp)
   void *oldheap;
   api_main_t *am = vlibapi_get_main ();
 
-  pthread_mutex_lock (&am->vlib_rp->mutex);
-  oldheap = svm_push_data_heap (am->vlib_rp);
+  oldheap = vl_msg_push_heap ();
   svm_queue_free (am->vl_input_queue);
-  pthread_mutex_unlock (&am->vlib_rp->mutex);
-  svm_pop_heap (oldheap);
+  vl_msg_pop_heap (oldheap);
 
   am->my_client_index = ~0;
   am->my_registration = 0;
@@ -295,6 +294,7 @@ vl_client_disconnect (void)
   svm_queue_t *vl_input_queue;
   api_main_t *am = vlibapi_get_main ();
   time_t begin;
+  msgbuf_t *msgbuf;
 
   vl_input_queue = am->vl_input_queue;
   vl_client_send_disconnect (0 /* wait for reply */ );
@@ -327,10 +327,12 @@ vl_client_disconnect (void)
       if (ntohs (rp->_vl_msg_id) != VL_API_MEMCLNT_DELETE_REPLY)
        {
          clib_warning ("queue drain: %d", ntohs (rp->_vl_msg_id));
-         vl_msg_api_handler ((void *) rp);
+         msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
+         vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
          continue;
        }
-      vl_msg_api_handler ((void *) rp);
+      msgbuf = (msgbuf_t *) ((u8 *) rp - offsetof (msgbuf_t, data));
+      vl_msg_api_handler ((void *) rp, ntohl (msgbuf->data_len));
       break;
     }
 
@@ -368,14 +370,14 @@ _(MEMCLNT_KEEPALIVE, memclnt_keepalive)
 void
 vl_client_install_client_message_handlers (void)
 {
-
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                            vl_api_##n##_t_handler,             \
-                            noop_handler,                       \
-                            vl_api_##n##_t_endian,              \
-                            vl_api_##n##_t_print,               \
-                            sizeof(vl_api_##n##_t), 1);
+  api_main_t *am = vlibapi_get_main ();
+#define _(N, n)                                                               \
+  vl_msg_api_set_handlers (                                                   \
+    VL_API_##N, #n, vl_api_##n##_t_handler, noop_handler,                     \
+    vl_api_##n##_t_endian, vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0,  \
+    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);                       \
+  am->api_trace_cfg[VL_API_##N].replay_enable = 0;
   foreach_api_msg;
 #undef _
 }
@@ -401,7 +403,7 @@ vl_client_api_unmap (void)
 u8
 vl_mem_client_is_connected (void)
 {
-  return (memory_client_main.connected_to_vlib != 0);
+  return (my_memory_client_main->connected_to_vlib != 0);
 }
 
 static int
@@ -434,7 +436,7 @@ connect_to_vlib_internal (const char *svm_name,
       if (thread_fn == rx_thread_fn)
        {
          rx_thread_fn_arg_t *arg;
-         arg = clib_mem_alloc (sizeof (*arg));
+         arg = malloc (sizeof (*arg));
          arg->am = vlibapi_get_main ();
          arg->mm = vlibapi_get_memory_client_main ();
          thread_fn_arg = (void *) arg;
@@ -575,6 +577,11 @@ vl_client_get_first_plugin_msg_id (const char *plugin_name)
   old_handler = am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY];
   am->msg_handlers[VL_API_GET_FIRST_MSG_ID_REPLY] = (void *)
     vl_api_get_first_msg_id_reply_t_handler;
+  if (!am->msg_calc_size_funcs[VL_API_GET_FIRST_MSG_ID_REPLY])
+    {
+      am->msg_calc_size_funcs[VL_API_GET_FIRST_MSG_ID_REPLY] =
+       (uword (*) (void *)) vl_api_get_first_msg_id_reply_t_calc_size;
+    }
 
   /* Ask the data-plane for the message-ID base of the indicated plugin */
   mm->first_msg_id_reply_ready = 0;