API: More gracefully fail when opening shared memory segment fails.
[vpp.git] / src / vlibmemory / memory_shared.c
index 6cea5df..fbdabd0 100644 (file)
@@ -104,8 +104,17 @@ vl_msg_api_alloc_internal (int nbytes, int pool, int may_return_null)
              if (now - rv->gc_mark_timestamp > 10)
                {
                  if (CLIB_DEBUG > 0)
-                   clib_warning ("garbage collect pool %d ring %d index %d",
-                                 pool, i, q->head);
+                   {
+                     u16 *msg_idp, msg_id;
+                     clib_warning
+                       ("garbage collect pool %d ring %d index %d", pool, i,
+                        q->head);
+                     msg_idp = (u16 *) (rv->data);
+                     msg_id = clib_net_to_host_u16 (*msg_idp);
+                     if (msg_id < vec_len (api_main.msg_names))
+                       clib_warning ("msg id %d name %s", (u32) msg_id,
+                                     api_main.msg_names[msg_id]);
+                   }
                  shmem_hdr->garbage_collects++;
                  goto collected;
                }
@@ -257,7 +266,7 @@ vl_msg_api_free_nolock (void *a)
 }
 
 void
-vl_set_memory_root_path (char *name)
+vl_set_memory_root_path (const char *name)
 {
   api_main_t *am = &api_main;
 
@@ -321,22 +330,40 @@ vl_set_api_pvt_heap_size (u64 size)
 }
 
 int
-vl_map_shmem (char *region_name, int is_vlib)
+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;
   void *oldheap;
   vl_shmem_hdr_t *shmem_hdr = 0;
   api_main_t *am = &api_main;
-  int i;
+  int i, rv;
   struct timespec ts, tsrem;
-
-  if (is_vlib == 0)
-    svm_region_init_chroot (am->root_path);
+  u32 vlib_input_queue_length;
 
   memset (a, 0, sizeof (*a));
 
-  a->name = region_name;
+  if (strstr (region_name, "-vpe-api"))
+    {
+      char root_path[strlen (region_name)];
+      strncpy (root_path, region_name, strlen (region_name) - 8);
+      a->root_path = root_path;
+      am->root_path = root_path;
+    }
+
+  if (is_vlib == 0)
+    {
+      rv = svm_region_init_chroot (am->root_path);
+      if (rv)
+       return rv;
+    }
+
+  if (a->root_path != NULL)
+    {
+      a->name = "/vpe-api";
+    }
+  else
+    a->name = region_name;
   a->size = am->api_size ? am->api_size : (16 << 20);
   a->flags = SVM_FLAGS_MHEAP;
   a->uid = am->api_uid;
@@ -449,9 +476,13 @@ vl_map_shmem (char *region_name, int is_vlib)
   shmem_hdr->version = VL_SHM_VERSION;
 
   /* vlib main input queue */
+  vlib_input_queue_length = 1024;
+  if (am->vlib_input_queue_length)
+    vlib_input_queue_length = am->vlib_input_queue_length;
+
   shmem_hdr->vl_input_queue =
-    unix_shared_memory_queue_init (1024, sizeof (uword), getpid (),
-                                  am->vlib_signal);
+    unix_shared_memory_queue_init (vlib_input_queue_length, sizeof (uword),
+                                  getpid (), am->vlib_signal);
 
   /* Set up the msg ring allocator */
 #define _(sz,n)                                                 \