ipsec: Changes to make ipsec encoder/decoders reusable by the plugins
[vpp.git] / src / vlibmemory / memory_shared.c
index 1ccd563..6c8ec30 100644 (file)
@@ -29,6 +29,7 @@
 #include <vppinfra/format.h>
 #include <vppinfra/byte_order.h>
 #include <vppinfra/error.h>
+#include <vppinfra/elog.h>
 #include <svm/queue.h>
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
@@ -41,7 +42,7 @@
 
 #define DEBUG_MESSAGE_BUFFER_OVERRUN 0
 
-static inline void *
+CLIB_NOSANITIZE_ADDR static inline void *
 vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
 {
   int i;
@@ -176,6 +177,7 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
     rv = clib_mem_alloc (nbytes);
 
   rv->q = 0;
+  rv->gc_mark_timestamp = 0;
   svm_pop_heap (oldheap);
   pthread_mutex_unlock (&am->vlib_rp->mutex);
 
@@ -190,6 +192,7 @@ out:
 #endif
   rv->data_len = htonl (nbytes - sizeof (msgbuf_t));
 
+  VL_MSG_API_UNPOISON (rv->data);
   return (rv->data);
 }
 
@@ -207,6 +210,16 @@ vl_msg_api_alloc (int nbytes)
   return vl_msg_api_alloc_internal (nbytes, pool, 0 /* may_return_null */ );
 }
 
+void *
+vl_msg_api_alloc_zero (int nbytes)
+{
+  void *ret;
+
+  ret = vl_msg_api_alloc (nbytes);
+  clib_memset (ret, 0, nbytes);
+  return ret;
+}
+
 void *
 vl_msg_api_alloc_or_null (int nbytes)
 {
@@ -224,6 +237,16 @@ vl_msg_api_alloc_as_if_client (int nbytes)
   return vl_msg_api_alloc_internal (nbytes, 0, 0 /* may_return_null */ );
 }
 
+void *
+vl_msg_api_alloc_zero_as_if_client (int nbytes)
+{
+  void *ret;
+
+  ret = vl_msg_api_alloc_as_if_client (nbytes);
+  clib_memset (ret, 0, nbytes);
+  return ret;
+}
+
 void *
 vl_msg_api_alloc_as_if_client_or_null (int nbytes)
 {
@@ -274,6 +297,7 @@ vl_msg_api_free (void *a)
        ASSERT (*overrun == 0x1badbabe);
       }
 #endif
+      VL_MSG_API_POISON (rv->data);
       return;
     }
 
@@ -309,6 +333,7 @@ vl_msg_api_free_nolock (void *a)
   if (rv->q)
     {
       rv->q = 0;
+      VL_MSG_API_POISON (rv->data);
       return;
     }
 
@@ -514,7 +539,7 @@ vl_map_shmem (const char *region_name, int is_vlib)
   svm_map_region_args_t _a, *a = &_a;
   svm_region_t *vlib_rp, *root_rp;
   api_main_t *am = &api_main;
-  int i, rv;
+  int i;
   struct timespec ts, tsrem;
   char *vpe_api_region_suffix = "-vpe-api";
 
@@ -551,7 +576,7 @@ vl_map_shmem (const char *region_name, int is_vlib)
          while (nanosleep (&ts, &tsrem) < 0)
            ts = tsrem;
          tfd = open ((char *) api_name, O_RDWR);
-         if (tfd > 0)
+         if (tfd >= 0)
            break;
        }
       vec_free (api_name);
@@ -561,9 +586,7 @@ vl_map_shmem (const char *region_name, int is_vlib)
          return -2;
        }
       close (tfd);
-      rv = svm_region_init_chroot (am->root_path);
-      if (rv)
-       return rv;
+      svm_region_init_chroot_uid_gid (am->root_path, getuid (), getgid ());
     }
 
   if (a->root_path != NULL)
@@ -733,10 +756,10 @@ void
 vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
 {
   api_main_t *am = &api_main;
-  uword *trace = (uword *) elem;
+  void *msg = (void *) *(uword *) elem;
 
   if (am->tx_trace && am->tx_trace->enabled)
-    vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
+    vl_msg_api_trace (am, am->tx_trace, msg);
 
   /*
    * Announce a probable binary API client bug:
@@ -745,7 +768,28 @@ vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem)
    */
   if (PREDICT_FALSE
       (am->vl_clients /* vpp side */  && (q->cursize == q->maxsize)))
-    clib_warning ("WARNING: client input queue at %llx is stuffed...", q);
+    {
+      if (PREDICT_FALSE (am->elog_trace_api_messages))
+       {
+          /* *INDENT-OFF* */
+          ELOG_TYPE_DECLARE (e) =
+            {
+              .format = "api-client-queue-stuffed: %x%x",
+              .format_args = "i4i4",
+            };
+          /* *INDENT-ON* */
+         struct
+         {
+           u32 hi, low;
+         } *ed;
+         ed = ELOG_DATA (am->elog_main, e);
+         ed->hi = (uword) q >> 32;
+         ed->low = (uword) q & 0xFFFFFFFF;
+         clib_warning ("WARNING: client input queue at %llx is stuffed...",
+                       q);
+       }
+    }
+  VL_MSG_API_POISON (msg);
   (void) svm_queue_add (q, elem, 0 /* nowait */ );
 }
 
@@ -759,12 +803,13 @@ void
 vl_msg_api_send_shmem_nolock (svm_queue_t * q, u8 * elem)
 {
   api_main_t *am = &api_main;
-  uword *trace = (uword *) elem;
+  void *msg = (void *) *(uword *) elem;
 
   if (am->tx_trace && am->tx_trace->enabled)
-    vl_msg_api_trace (am, am->tx_trace, (void *) trace[0]);
+    vl_msg_api_trace (am, am->tx_trace, msg);
 
   (void) svm_queue_add_nolock (q, elem);
+  VL_MSG_API_POISON (msg);
 }
 
 /*