session: add support for memfd segments

- update segment manager and session api to work with both flavors of
  ssvm segments
- added generic ssvm slave/master init and del functions
- cleanup/refactor tcp_echo
- fixed uses of svm fifo pool as vector

Change-Id: Ieee8b163faa407da6e77e657a2322de213a9d2a0
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/svm/svm_fifo_segment.c b/src/svm/svm_fifo_segment.c
index a56a714..4d1833e 100644
--- a/src/svm/svm_fifo_segment.c
+++ b/src/svm/svm_fifo_segment.c
@@ -58,7 +58,7 @@
 
 static void
 preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
-			svm_fifo_segment_create_args_t * a)
+			svm_fifo_segment_create_args_t * a, u32 protect_size)
 {
   svm_fifo_segment_header_t *fsh = s->h;
   u32 rx_fifo_size, tx_fifo_size, pairs_to_allocate;
@@ -99,18 +99,17 @@
 
   /* Calculate space requirements */
   pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size;
-  pairs_to_allocate = clib_min (s->ssvm.ssvm_size / pair_size,
-				a->preallocated_fifo_pairs);
+  if (protect_size)
+    protect_size += mheap_bytes (s->ssvm.sh->heap);
+  pairs_to_allocate =
+    clib_min ((s->ssvm.ssvm_size - protect_size) / pair_size,
+	      a->preallocated_fifo_pairs);
   rx_fifo_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate;
   tx_fifo_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate;
 
   vec_validate_init_empty (fsh->free_fifos,
 			   clib_max (rx_freelist_index, tx_freelist_index),
 			   0);
-  if (0)
-    clib_warning ("rx_fifo_size %u (%d mb), tx_fifo_size %u (%d mb)",
-		  rx_fifo_size, rx_fifo_size >> 20,
-		  tx_fifo_size, tx_fifo_size >> 20);
 
   /* Allocate rx fifo space. May fail. */
   rx_fifo_space = clib_mem_alloc_aligned_at_offset
@@ -164,16 +163,18 @@
   a->preallocated_fifo_pairs -= pairs_to_allocate;
 }
 
-/** (master) create an svm fifo segment */
+/**
+ * Create an svm fifo segment and initialize as master
+ */
 int
 svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
 {
-  int rv;
-  svm_fifo_segment_private_t *s;
   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
+  svm_fifo_segment_private_t *s;
   ssvm_shared_header_t *sh;
   svm_fifo_segment_header_t *fsh;
   void *oldheap;
+  int rv;
 
   /* Allocate a fresh segment */
   pool_get (sm->segments, s);
@@ -185,16 +186,14 @@
   s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
   s->ssvm.requested_va = sm->next_baseva;
 
-  rv = ssvm_master_init (&s->ssvm, s - sm->segments);
-
-  if (rv)
+  if ((rv = ssvm_master_init (&s->ssvm, a->segment_type)))
     {
-      _vec_len (s) = vec_len (s) - 1;
+      pool_put (sm->segments, s);
       return (rv);
     }
 
   /* Note: requested_va updated due to seg base addr randomization */
-  sm->next_baseva = s->ssvm.requested_va + a->segment_size;
+  sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size;
 
   sh = s->ssvm.sh;
   oldheap = ssvm_push_heap (sh);
@@ -202,10 +201,8 @@
   /* Set up svm_fifo_segment shared header */
   fsh = clib_mem_alloc (sizeof (*fsh));
   memset (fsh, 0, sizeof (*fsh));
-  sh->opaque[0] = fsh;
-  s->h = fsh;
-  fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
-  preallocate_fifo_pairs (s, a);
+  s->h = sh->opaque[0] = fsh;
+  preallocate_fifo_pairs (s, a, a->seg_protected_space);
 
   ssvm_pop_heap (oldheap);
 
@@ -214,7 +211,9 @@
   return (0);
 }
 
-/** Create an svm fifo segment in process-private memory */
+/**
+ * Create an svm fifo segment in process-private memory
+ */
 int
 svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
 {
@@ -272,12 +271,10 @@
       /* Set up svm_fifo_segment shared header */
       fsh = clib_mem_alloc (sizeof (*fsh));
       memset (fsh, 0, sizeof (*fsh));
-      sh->opaque[0] = fsh;
-      s->h = fsh;
       fsh->flags = FIFO_SEGMENT_F_IS_PRIVATE;
+      s->h = sh->opaque[0] = fsh;
       if (!a->private_segment_count)
 	fsh->flags |= FIFO_SEGMENT_F_IS_MAIN_HEAP;
-      fsh->segment_name = format (0, "%s%c", a->segment_name, 0);
 
       if (a->private_segment_count)
 	{
@@ -285,7 +282,7 @@
 	    fsh->flags |= FIFO_SEGMENT_F_IS_PREALLOCATED;
 	  oldheap = clib_mem_get_heap ();
 	  clib_mem_set_heap (sh->heap);
-	  preallocate_fifo_pairs (s, a);
+	  preallocate_fifo_pairs (s, a, i == 0 ? a->seg_protected_space : 0);
 	  clib_mem_set_heap (oldheap);
 	}
       sh->ready = 1;
@@ -295,15 +292,15 @@
   return (0);
 }
 
-/** (slave) attach to an svm fifo segment */
+/**
+ * Attach as slave to an svm fifo segment
+ */
 int
 svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
 {
-  int rv;
-  svm_fifo_segment_private_t *s;
   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
-  ssvm_shared_header_t *sh;
-  svm_fifo_segment_header_t *fsh;
+  svm_fifo_segment_private_t *s;
+  int rv;
 
   /* Allocate a fresh segment */
   pool_get (sm->segments, s);
@@ -313,19 +310,19 @@
   s->ssvm.my_pid = getpid ();
   s->ssvm.name = format (0, "%s%c", a->segment_name, 0);
   s->ssvm.requested_va = sm->next_baseva;
+  if (a->segment_type == SSVM_SEGMENT_MEMFD)
+    s->ssvm.fd = a->memfd_fd;
+  else
+    s->ssvm.attach_timeout = sm->timeout_in_seconds;
 
-  rv = ssvm_slave_init (&s->ssvm, sm->timeout_in_seconds);
-
-  if (rv)
+  if ((rv = ssvm_slave_init (&s->ssvm, a->segment_type)))
     {
       _vec_len (s) = vec_len (s) - 1;
       return (rv);
     }
 
   /* Fish the segment header */
-  sh = s->ssvm.sh;
-  fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
-  s->h = fsh;
+  s->h = s->ssvm.sh->opaque[0];
 
   vec_add1 (a->new_segment_indices, s - sm->segments);
   return (0);
@@ -335,6 +332,7 @@
 svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
 {
   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
+
   if (s->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
     {
       /* Don't try to free vpp's heap! */
@@ -342,15 +340,18 @@
 	mheap_free (s->ssvm.sh->heap);
       clib_mem_free (s->ssvm.sh);
       clib_mem_free (s->h);
-      pool_put (sm->segments, s);
     }
   else
     {
       ssvm_delete (&s->ssvm);
-      pool_put (sm->segments, s);
     }
+  memset (s, 0xfe, sizeof (*s));
+  pool_put (sm->segments, s);
 }
 
+/**
+ * Allocate fifo in svm segment
+ */
 svm_fifo_t *
 svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
 			     u32 data_size_in_bytes,
@@ -590,6 +591,53 @@
   return count;
 }
 
+void
+svm_fifo_segment_info (svm_fifo_segment_private_t * seg, uword * address,
+		       u64 * size)
+{
+  if (seg->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
+    {
+      mheap_t *heap_header;
+
+      *address = pointer_to_uword (seg->ssvm.sh->heap);
+      heap_header = mheap_header (seg->ssvm.sh->heap);
+      *size = heap_header->max_size;
+    }
+  else
+    {
+      *address = seg->ssvm.sh->ssvm_va;
+      *size = seg->ssvm.ssvm_size;
+    }
+}
+
+mheap_t *
+svm_fifo_segment_heap (svm_fifo_segment_private_t * seg)
+{
+  return seg->ssvm.sh->heap;
+}
+
+u8 *
+format_svm_fifo_segment_type (u8 * s, va_list * args)
+{
+  svm_fifo_segment_private_t *sp;
+  sp = va_arg (*args, svm_fifo_segment_private_t *);
+  ssvm_segment_type_t st = ssvm_type (&sp->ssvm);
+
+  if ((sp->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
+      && !(sp->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP))
+    s = format (s, "%s", "private-heap");
+  else if ((sp->h->flags & FIFO_SEGMENT_F_IS_PRIVATE)
+	   && (sp->h->flags & FIFO_SEGMENT_F_IS_MAIN_HEAP))
+    s = format (s, "%s", "main-heap");
+  else if (st == SSVM_SEGMENT_MEMFD)
+    s = format (s, "%s", "memfd");
+  else if (st == SSVM_SEGMENT_SHM)
+    s = format (s, "%s", "shm");
+  else
+    s = format (s, "%s", "unknown");
+  return s;
+}
+
 /**
  * Segment format function
  */
@@ -599,18 +647,14 @@
   svm_fifo_segment_private_t *sp
     = va_arg (*args, svm_fifo_segment_private_t *);
   int verbose = va_arg (*args, int);
-  ssvm_shared_header_t *sh;
-  svm_fifo_segment_header_t *fsh;
+  svm_fifo_segment_header_t *fsh = sp->h;
+  u32 count, indent;
   svm_fifo_t *f;
   int i;
-  u32 count;
-  u32 indent = format_get_indent (s) + 2;
 
-  sh = sp->ssvm.sh;
-  fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
-
-  s = format (s, "%USegment Heap: %U\n", format_white_space, indent,
-	      format_mheap, sh->heap, verbose);
+  indent = format_get_indent (s) + 2;
+  s = format (s, "%U segment heap: %U\n", format_white_space, indent,
+	      format_mheap, svm_fifo_segment_heap (sp), verbose);
   s = format (s, "%U segment has %u active fifos\n",
 	      format_white_space, indent, svm_fifo_segment_num_fifos (sp));