svm vcl: add helper fn that discovers mqs in segment

Type: improvement

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I1b083ee793a7cf91b1001bfe88353fa5e6515c42
diff --git a/src/svm/fifo_segment.c b/src/svm/fifo_segment.c
index 3e9aecb..76cc046 100644
--- a/src/svm/fifo_segment.c
+++ b/src/svm/fifo_segment.c
@@ -301,6 +301,7 @@
   fsh->max_log2_fifo_size = min_log2 (max_fifo);
   fsh->n_cached_bytes = 0;
   fsh->n_reserved_bytes = fsh->byte_index;
+  fsh->start_byte_index = fsh->byte_index;
   ASSERT (fsh->max_byte_index <= sh->ssvm_size - offset);
 
   fs->max_byte_index = fsh->max_byte_index;
@@ -1054,6 +1055,38 @@
   return mq;
 }
 
+void
+fifo_segment_msg_qs_discover (fifo_segment_t *fs, int *fds, u32 n_fds)
+{
+  svm_msg_q_shared_t *smq;
+  u32 n_mqs, size, i;
+  uword offset = 0, n_alloced;
+  svm_msg_q_t *mq;
+
+  n_mqs = fs->h->n_mqs;
+  if (n_fds && n_mqs != n_fds)
+    {
+      clib_warning ("expected %u fds got %u", n_mqs, n_fds);
+      return;
+    }
+
+  vec_validate (fs->mqs, n_mqs - 1);
+  n_alloced = fs->h->n_reserved_bytes - fs->h->start_byte_index;
+  ASSERT (n_alloced % n_mqs == 0);
+  size = n_alloced / n_mqs;
+
+  offset = fs->h->start_byte_index;
+  for (i = 0; i < n_mqs; i++)
+    {
+      mq = vec_elt_at_index (fs->mqs, i);
+      smq = (svm_msg_q_shared_t *) ((u8 *) fs->h + offset);
+      svm_msg_q_attach (mq, smq);
+      if (n_fds)
+	svm_msg_q_set_eventfd (mq, fds[i]);
+      offset += size;
+    }
+}
+
 uword
 fifo_segment_msg_q_offset (fifo_segment_t *fs, u32 mq_index)
 {
diff --git a/src/svm/fifo_segment.h b/src/svm/fifo_segment.h
index 268b48b..9d2ab28 100644
--- a/src/svm/fifo_segment.h
+++ b/src/svm/fifo_segment.h
@@ -167,6 +167,15 @@
 					u32 mq_index);
 
 /**
+ *  Discover mqs on mq only segment
+ *
+ *  @param fs		fifo segment for mq
+ *  @param fds  	array of fds is mqs use eventfds
+ *  @param n_fds	number of fds
+ */
+void fifo_segment_msg_qs_discover (fifo_segment_t *fs, int *fds, u32 n_fds);
+
+/**
  * Message queue offset on segment
  *
  * @param fs		fifo segment for mq
diff --git a/src/svm/fifo_types.h b/src/svm/fifo_types.h
index 03adbd3..7b844f0 100644
--- a/src/svm/fifo_types.h
+++ b/src/svm/fifo_types.h
@@ -150,6 +150,7 @@
   CLIB_CACHE_LINE_ALIGN_MARK (allocator);
   uword byte_index;
   uword max_byte_index;
+  uword start_byte_index;
   CLIB_CACHE_LINE_ALIGN_MARK (slice);
   fifo_segment_slice_t slices[0]; /** Fixed array of slices */
 };
diff --git a/src/vcl/vcl_bapi.c b/src/vcl/vcl_bapi.c
index 48695a3..618a46d 100644
--- a/src/vcl/vcl_bapi.c
+++ b/src/vcl/vcl_bapi.c
@@ -100,11 +100,6 @@
 				fds[n_fds++]))
 	  goto failed;
 
-      vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
-			     mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
-			     &wrk->ctrl_mq);
-      vcm->ctrl_mq = wrk->ctrl_mq;
-
       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
 	{
 	  segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
@@ -126,6 +121,13 @@
 	  n_fds++;
 	}
 
+      vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0), fds + n_fds,
+				mp->n_fds - n_fds);
+      vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
+			     mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
+			     &wrk->ctrl_mq);
+      vcm->ctrl_mq = wrk->ctrl_mq;
+
       vec_free (fds);
     }
   else
diff --git a/src/vcl/vcl_private.c b/src/vcl/vcl_private.c
index b9745d2..85fe0a8 100644
--- a/src/vcl/vcl_private.c
+++ b/src/vcl/vcl_private.c
@@ -456,6 +456,29 @@
   return 0;
 }
 
+int
+vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
+{
+  fifo_segment_t *fs;
+  u32 fs_index;
+
+  fs_index = vcl_segment_table_lookup (segment_handle);
+  if (fs_index == VCL_INVALID_SEGMENT_INDEX)
+    {
+      VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
+      return -1;
+    }
+
+  clib_rwlock_reader_lock (&vcm->segment_table_lock);
+
+  fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
+  fifo_segment_msg_qs_discover (fs, fds, n_fds);
+
+  clib_rwlock_reader_unlock (&vcm->segment_table_lock);
+
+  return 0;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vcl/vcl_private.h b/src/vcl/vcl_private.h
index 7104adc..e4e73e0 100644
--- a/src/vcl/vcl_private.h
+++ b/src/vcl/vcl_private.h
@@ -699,6 +699,7 @@
 				vcl_session_t *s);
 int vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
 			   svm_msg_q_t **mq);
+int vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds);
 
 /*
  * VCL Binary API
diff --git a/src/vcl/vcl_sapi.c b/src/vcl/vcl_sapi.c
index 1bab7ea..5258722 100644
--- a/src/vcl/vcl_sapi.c
+++ b/src/vcl/vcl_sapi.c
@@ -71,10 +71,6 @@
 			    SSVM_SEGMENT_MEMFD, fds[n_fds_used++]))
       goto failed;
 
-  vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_ctrl_mq,
-			 mp->vpp_ctrl_mq_thread, &wrk->ctrl_mq);
-  vcm->ctrl_mq = wrk->ctrl_mq;
-
   if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
     {
       segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
@@ -93,6 +89,12 @@
       vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
     }
 
+  vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0),
+			    fds + n_fds_used, mp->n_fds - n_fds_used);
+  vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_ctrl_mq,
+			 mp->vpp_ctrl_mq_thread, &wrk->ctrl_mq);
+  vcm->ctrl_mq = wrk->ctrl_mq;
+
   vcm->app_index = mp->app_index;
 
   return 0;