svm: allow mq attachments at random offsets

Type: feature

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: Ic373cd2c11272da539eb4b0db27227f36f2f9688
diff --git a/src/vnet/session/application_interface.h b/src/vnet/session/application_interface.h
index 096af1e..9614257 100644
--- a/src/vnet/session/application_interface.h
+++ b/src/vnet/session/application_interface.h
@@ -370,6 +370,7 @@
   uword server_tx_fifo;
   u64 segment_handle;
   uword vpp_event_queue_address;
+  u32 mq_index;
   transport_endpoint_t rmt;
   u8 flags;
 } __clib_packed session_accepted_msg_t;
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
index 7683760..eca199f 100644
--- a/src/vnet/session/segment_manager.c
+++ b/src/vnet/session/segment_manager.c
@@ -875,7 +875,6 @@
   u32 fifo_evt_size, session_evt_size = 256, notif_q_size;
   svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
   svm_msg_q_t *q;
-  void *base;
 
   fifo_evt_size = sizeof (session_event_t);
   notif_q_size = clib_max (16, props->evt_q_size >> 4);
@@ -890,8 +889,7 @@
   cfg->q_nitems = props->evt_q_size;
   cfg->ring_cfgs = rc;
 
-  base = fifo_segment_alloc (segment, svm_msg_q_size_to_alloc (cfg));
-  q = svm_msg_q_init (base, cfg);
+  q = fifo_segment_msg_q_alloc (segment, 0, cfg);
 
   if (props->use_mq_eventfd)
     {
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index 259e212..9a4d29b 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -1508,7 +1508,6 @@
   fifo_segment_t *eqs = &smm->evt_qs_segment;
   uword eqs_size = 64 << 20;
   pid_t vpp_pid = getpid ();
-  void *base;
   int i;
 
   if (smm->configured_event_queue_length)
@@ -1531,6 +1530,9 @@
 
   fifo_segment_init (eqs);
 
+  /* Special fifo segment that's filled only with mqs */
+  eqs->h->n_mqs = vec_len (smm->wrk);
+
   for (i = 0; i < vec_len (smm->wrk); i++)
     {
       svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
@@ -1544,8 +1546,7 @@
       cfg->q_nitems = evt_q_length;
       cfg->ring_cfgs = rc;
 
-      base = fifo_segment_alloc (eqs, svm_msg_q_size_to_alloc (cfg));
-      smm->wrk[i].vpp_event_queue = svm_msg_q_init (base, cfg);
+      smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (eqs, i, cfg);
 
       if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
 	clib_warning ("eventfd returned");
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
index 8fe1acf..4602a78 100644
--- a/src/vnet/session/session_api.c
+++ b/src/vnet/session/session_api.c
@@ -130,7 +130,8 @@
 {
   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
   svm_msg_q_msg_t _msg, *msg = &_msg;
-  svm_msg_q_t *vpp_queue, *app_mq;
+  svm_msg_q_t *app_mq;
+  fifo_segment_t *eq_seg;
   session_t *listener;
   session_accepted_msg_t *mp;
   session_event_t *evt;
@@ -152,6 +153,8 @@
   mp->segment_handle = session_segment_handle (s);
   mp->flags = s->flags;
 
+  eq_seg = session_main_get_evt_q_segment ();
+
   if (session_has_transport (s))
     {
       listener = listen_session_get_from_handle (s->listener_handle);
@@ -164,8 +167,9 @@
 	  if (listener)
 	    mp->listener_handle = listen_session_get_handle (listener);
 	}
-      vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
-      mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
+      mp->vpp_event_queue_address =
+	fifo_segment_msg_q_offset (eq_seg, s->thread_index);
+      mp->mq_index = s->thread_index;
       mp->handle = session_handle (s);
 
       session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ );
@@ -180,8 +184,9 @@
       mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
       mp->rmt.port = ct->c_rmt_port;
       mp->handle = session_handle (s);
-      vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
-      mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
+      mp->vpp_event_queue_address =
+	fifo_segment_msg_q_offset (eq_seg, s->thread_index);
+      mp->mq_index = s->thread_index;
     }
   svm_msg_q_add_and_unlock (app_mq, msg);
 
@@ -262,8 +267,9 @@
 {
   svm_msg_q_msg_t _msg, *msg = &_msg;
   session_connected_msg_t *mp;
-  svm_msg_q_t *vpp_mq, *app_mq;
+  svm_msg_q_t *app_mq;
   transport_connection_t *tc;
+  fifo_segment_t *eq_seg;
   app_worker_t *app_wrk;
   session_event_t *evt;
 
@@ -289,6 +295,8 @@
   if (err)
     goto done;
 
+  eq_seg = session_main_get_evt_q_segment ();
+
   if (session_has_transport (s))
     {
       tc = session_get_transport (s);
@@ -299,9 +307,9 @@
 	  goto done;
 	}
 
-      vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
       mp->handle = session_handle (s);
-      mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
+      mp->vpp_event_queue_address =
+	fifo_segment_msg_q_offset (eq_seg, s->thread_index);
 
       session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ );
 
@@ -318,8 +326,8 @@
       mp->handle = session_handle (s);
       mp->lcl.port = cct->c_lcl_port;
       mp->lcl.is_ip4 = cct->c_is_ip4;
-      vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
-      mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
+      mp->vpp_event_queue_address =
+	fifo_segment_msg_q_offset (eq_seg, s->thread_index);
       mp->server_rx_fifo = pointer_to_uword (s->rx_fifo->shr);
       mp->server_tx_fifo = pointer_to_uword (s->tx_fifo->shr);
       mp->segment_handle = session_segment_handle (s);
@@ -341,9 +349,10 @@
 			  session_handle_t handle, int rv)
 {
   svm_msg_q_msg_t _msg, *msg = &_msg;
-  svm_msg_q_t *app_mq, *vpp_evt_q;
+  svm_msg_q_t *app_mq;
   transport_endpoint_t tep;
   session_bound_msg_t *mp;
+  fifo_segment_t *eq_seg;
   app_worker_t *app_wrk;
   session_event_t *evt;
   app_listener_t *al;
@@ -381,8 +390,8 @@
   mp->lcl_is_ip4 = tep.is_ip4;
   clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip));
 
-  vpp_evt_q = session_main_get_vpp_event_queue (0);
-  mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
+  eq_seg = session_main_get_evt_q_segment ();
+  mp->vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, ls->thread_index);
 
   if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
     {
@@ -425,10 +434,14 @@
 {
   svm_msg_q_msg_t _msg, *msg = &_msg;
   session_migrated_msg_t *mp;
-  svm_msg_q_t *vpp_evt_q;
+  fifo_segment_t *eq_seg;
   app_worker_t *app_wrk;
   session_event_t *evt;
   svm_msg_q_t *app_mq;
+  u32 thread_index;
+
+  thread_index = session_thread_from_handle (new_sh);
+  eq_seg = session_main_get_evt_q_segment ();
 
   app_wrk = app_worker_get (s->app_wrk_index);
   app_mq = app_wrk->event_queue;
@@ -441,9 +454,8 @@
   mp = (session_migrated_msg_t *) evt->data;
   mp->handle = session_handle (s);
   mp->new_handle = new_sh;
-  mp->vpp_thread_index = session_thread_from_handle (new_sh);
-  vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index);
-  mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
+  mp->vpp_thread_index = thread_index;
+  mp->vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, thread_index);
   mp->segment_handle = session_segment_handle (s);
   svm_msg_q_add_and_unlock (app_mq, msg);
 }
@@ -601,12 +613,10 @@
 {
   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
   vl_api_app_attach_reply_t *rmp;
-  ssvm_private_t *segp;
+  fifo_segment_t *segp, *evt_q_segment = 0;
   vnet_app_attach_args_t _a, *a = &_a;
-  fifo_segment_t *evt_q_segment;
   u8 fd_flags = 0, ctrl_thread;
   vl_api_registration_t *reg;
-  svm_msg_q_t *ctrl_mq;
 
   reg = vl_api_client_index_to_registration (mp->client_index);
   if (!reg)
@@ -669,19 +679,19 @@
     if (!rv)
       {
 	ctrl_thread = vlib_num_workers () ? 1 : 0;
-	ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
-	segp = a->segment;
+	segp = (fifo_segment_t *) a->segment;
 	rmp->app_index = clib_host_to_net_u32 (a->app_index);
-	rmp->app_mq = pointer_to_uword (a->app_evt_q);
-	rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
+	rmp->app_mq = fifo_segment_msg_q_offset (segp, 0);
+	rmp->vpp_ctrl_mq =
+	  fifo_segment_msg_q_offset (evt_q_segment, ctrl_thread);
 	rmp->vpp_ctrl_mq_thread = ctrl_thread;
 	rmp->n_fds = n_fds;
 	rmp->fd_flags = fd_flags;
-	if (vec_len (segp->name))
+	if (vec_len (segp->ssvm.name))
 	  {
-	    vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
+	    vl_api_vec_to_api_string (segp->ssvm.name, &rmp->segment_name);
 	  }
-	rmp->segment_size = segp->ssvm_size;
+	rmp->segment_size = segp->ssvm.ssvm_size;
 	rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
       }
   }));
@@ -755,13 +765,14 @@
     rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
     if (!rv && mp->is_add)
       {
+	rmp->app_event_queue_address =
+	  fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
+	rmp->n_fds = n_fds;
+	rmp->fd_flags = fd_flags;
 	if (vec_len (args.segment->name))
 	  {
 	    vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
 	  }
-	rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
-	rmp->n_fds = n_fds;
-	rmp->fd_flags = fd_flags;
       }
   }));
   /* *INDENT-ON* */
@@ -1341,7 +1352,6 @@
   app_ns_api_handle_t *handle;
   app_sapi_msg_t msg = { 0 };
   app_worker_t *app_wrk;
-  svm_msg_q_t *ctrl_mq;
   application_t *app;
 
   /* Make sure name is null terminated */
@@ -1390,10 +1400,11 @@
   if (!rv)
     {
       ctrl_thread = vlib_num_workers ()? 1 : 0;
-      ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
       rmp->app_index = a->app_index;
-      rmp->app_mq = pointer_to_uword (a->app_evt_q);
-      rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
+      rmp->app_mq =
+	fifo_segment_msg_q_offset ((fifo_segment_t *) a->segment, 0);
+      rmp->vpp_ctrl_mq =
+	fifo_segment_msg_q_offset (evt_q_segment, ctrl_thread);
       rmp->vpp_ctrl_mq_thread = ctrl_thread;
       rmp->n_fds = n_fds;
       rmp->fd_flags = fd_flags;
@@ -1502,7 +1513,8 @@
   if (!rv && mp->is_add)
     {
       /* No segment name and size. This supports only memfds */
-      rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
+      rmp->app_event_queue_address =
+	fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
       rmp->n_fds = n_fds;
       rmp->fd_flags = fd_flags;
 
diff --git a/src/vnet/session/session_debug.c b/src/vnet/session/session_debug.c
index c2718f3..cd4198c 100644
--- a/src/vnet/session/session_debug.c
+++ b/src/vnet/session/session_debug.c
@@ -120,7 +120,6 @@
 void
 dump_thread_0_event_queue (void)
 {
-  session_main_t *smm = vnet_get_session_main ();
   vlib_main_t *vm = &vlib_global_main;
   u32 my_thread_index = vm->thread_index;
   session_event_t _e, *e = &_e;
@@ -130,7 +129,7 @@
   svm_msg_q_t *mq;
   int i, index;
 
-  mq = smm->wrk[my_thread_index].vpp_event_queue;
+  mq = session_main_get_vpp_event_queue (my_thread_index);
   index = mq->q->head;
 
   for (i = 0; i < mq->q->cursize; i++)
diff --git a/src/vnet/session/session_node.c b/src/vnet/session/session_node.c
index 2fde85b..49e4e5a 100644
--- a/src/vnet/session/session_node.c
+++ b/src/vnet/session/session_node.c
@@ -472,8 +472,8 @@
   evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
   rmp = (session_worker_update_reply_msg_t *) evt->data;
   rmp->handle = mp->handle;
-  rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
-  rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
+  rmp->rx_fifo = pointer_to_uword (s->rx_fifo->shr);
+  rmp->tx_fifo = pointer_to_uword (s->tx_fifo->shr);
   rmp->segment_handle = session_segment_handle (s);
   svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);