svm: use explicit svm fifo segment main for slaves

Change-Id: Id39d64bf1b49345a3dc31c63360569212aba6865
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 6cb402f..c72de40 100644
--- a/src/svm/svm_fifo_segment.c
+++ b/src/svm/svm_fifo_segment.c
@@ -15,8 +15,6 @@
 
 #include <svm/svm_fifo_segment.h>
 
-svm_fifo_segment_main_t svm_fifo_segment_main;
-
 static void
 allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh,
 			 u32 data_size_in_bytes, int chunk_size)
@@ -203,9 +201,9 @@
  * Create an svm fifo segment and initialize as master
  */
 int
-svm_fifo_segment_create (svm_fifo_segment_create_args_t * a)
+svm_fifo_segment_create (svm_fifo_segment_main_t * sm,
+			 svm_fifo_segment_create_args_t * a)
 {
-  svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
   svm_fifo_segment_private_t *s;
   int rv;
 
@@ -237,9 +235,9 @@
  * Create an svm fifo segment in process-private memory
  */
 int
-svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a)
+svm_fifo_segment_create_process_private (svm_fifo_segment_main_t * sm,
+					 svm_fifo_segment_create_args_t * a)
 {
-  svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
   svm_fifo_segment_private_t *s;
   ssvm_shared_header_t *sh;
   u32 rnd_size = 0;
@@ -291,9 +289,9 @@
  * Attach as slave to an svm fifo segment
  */
 int
-svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a)
+svm_fifo_segment_attach (svm_fifo_segment_main_t * sm,
+			 svm_fifo_segment_create_args_t * a)
 {
-  svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
   svm_fifo_segment_private_t *s;
   int rv;
 
@@ -324,10 +322,9 @@
 }
 
 void
-svm_fifo_segment_delete (svm_fifo_segment_private_t * s)
+svm_fifo_segment_delete (svm_fifo_segment_main_t * sm,
+			 svm_fifo_segment_private_t * s)
 {
-  svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
-
   ssvm_delete (&s->ssvm);
   clib_memset (s, 0xfe, sizeof (*s));
   pool_put (sm->segments, s);
@@ -495,27 +492,26 @@
 }
 
 void
-svm_fifo_segment_main_init (u64 baseva, u32 timeout_in_seconds)
+svm_fifo_segment_main_init (svm_fifo_segment_main_t * sm, u64 baseva,
+			    u32 timeout_in_seconds)
 {
-  svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
-
   sm->next_baseva = baseva;
   sm->timeout_in_seconds = timeout_in_seconds;
 }
 
 u32
-svm_fifo_segment_index (svm_fifo_segment_private_t * s)
+svm_fifo_segment_index (svm_fifo_segment_main_t * sm,
+			svm_fifo_segment_private_t * s)
 {
-  return s - svm_fifo_segment_main.segments;
+  return s - sm->segments;
 }
 
 /**
  * Retrieve svm segments pool. Used only for debug purposes.
  */
 svm_fifo_segment_private_t *
-svm_fifo_segment_segments_pool (void)
+svm_fifo_segment_segments_pool (svm_fifo_segment_main_t * sm)
 {
-  svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
   return sm->segments;
 }
 
diff --git a/src/svm/svm_fifo_segment.h b/src/svm/svm_fifo_segment.h
index cbc327e..a769554 100644
--- a/src/svm/svm_fifo_segment.h
+++ b/src/svm/svm_fifo_segment.h
@@ -59,8 +59,6 @@
   u32 timeout_in_seconds;
 } svm_fifo_segment_main_t;
 
-extern svm_fifo_segment_main_t svm_fifo_segment_main;
-
 typedef struct
 {
   ssvm_segment_type_t segment_type;
@@ -73,10 +71,9 @@
 #define svm_fifo_segment_flags(_seg) _seg->h->flags
 
 static inline svm_fifo_segment_private_t *
-svm_fifo_segment_get_segment (u32 segment_index)
+svm_fifo_segment_get_segment (svm_fifo_segment_main_t * sm, u32 segment_index)
 {
-  svm_fifo_segment_main_t *ssm = &svm_fifo_segment_main;
-  return pool_elt_at_index (ssm->segments, segment_index);
+  return pool_elt_at_index (sm->segments, segment_index);
 }
 
 static inline u8
@@ -92,15 +89,19 @@
 }
 
 int svm_fifo_segment_init (svm_fifo_segment_private_t * s);
-int svm_fifo_segment_create (svm_fifo_segment_create_args_t * a);
-int svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t
-					     * a);
+int svm_fifo_segment_create (svm_fifo_segment_main_t * sm,
+			     svm_fifo_segment_create_args_t * a);
+int svm_fifo_segment_create_process_private (svm_fifo_segment_main_t * sm,
+					     svm_fifo_segment_create_args_t
+					     *);
 void svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s,
 					      u32 rx_fifo_size,
 					      u32 tx_fifo_size,
 					      u32 * n_fifo_pairs);
-int svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a);
-void svm_fifo_segment_delete (svm_fifo_segment_private_t * s);
+int svm_fifo_segment_attach (svm_fifo_segment_main_t * sm,
+			     svm_fifo_segment_create_args_t * a);
+void svm_fifo_segment_delete (svm_fifo_segment_main_t * sm,
+			      svm_fifo_segment_private_t * s);
 
 svm_fifo_t *svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s,
 					 u32 data_size_in_bytes,
@@ -108,15 +109,18 @@
 void svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s,
 				 svm_fifo_t * f,
 				 svm_fifo_segment_freelist_t index);
-void svm_fifo_segment_main_init (u64 baseva, u32 timeout_in_seconds);
-u32 svm_fifo_segment_index (svm_fifo_segment_private_t * s);
+void svm_fifo_segment_main_init (svm_fifo_segment_main_t * sm, u64 baseva,
+				 u32 timeout_in_seconds);
+u32 svm_fifo_segment_index (svm_fifo_segment_main_t * sm,
+			    svm_fifo_segment_private_t * s);
 u32 svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment);
 u32 svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t *
 				     fifo_segment, u32 fifo_size_in_bytes);
 void svm_fifo_segment_info (svm_fifo_segment_private_t * seg, uword * address,
 			    u64 * size);
 
-svm_fifo_segment_private_t *svm_fifo_segment_segments_pool (void);
+svm_fifo_segment_private_t
+  * svm_fifo_segment_segments_pool (svm_fifo_segment_main_t * sm);
 
 format_function_t format_svm_fifo_segment;
 format_function_t format_svm_fifo_segment_type;
diff --git a/src/svm/test_svm_fifo1.c b/src/svm/test_svm_fifo1.c
index 243f8b6..0a09916 100644
--- a/src/svm/test_svm_fifo1.c
+++ b/src/svm/test_svm_fifo1.c
@@ -15,10 +15,13 @@
 
 #include "svm_fifo_segment.h"
 
+svm_fifo_segment_main_t segment_main;
+
 clib_error_t *
 hello_world (int verbose)
 {
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  svm_fifo_segment_main_t *sm = &segment_main;
   svm_fifo_segment_private_t *sp;
   svm_fifo_t *f;
   int rv;
@@ -31,12 +34,12 @@
   a->segment_name = "fifo-test1";
   a->segment_size = 256 << 10;
 
-  rv = svm_fifo_segment_create (a);
+  rv = svm_fifo_segment_create (sm, a);
 
   if (rv)
     return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
 
-  sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
+  sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
 
   f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
 
@@ -72,6 +75,7 @@
 master (int verbose)
 {
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  svm_fifo_segment_main_t *sm = &segment_main;
   svm_fifo_segment_private_t *sp;
   svm_fifo_t *f;
   int rv;
@@ -84,12 +88,12 @@
   a->segment_name = "fifo-test1";
   a->segment_size = 256 << 10;
 
-  rv = svm_fifo_segment_create (a);
+  rv = svm_fifo_segment_create (sm, a);
 
   if (rv)
     return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
 
-  sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
+  sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
 
   f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
 
@@ -109,6 +113,7 @@
 mempig (int verbose)
 {
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  svm_fifo_segment_main_t *sm = &segment_main;
   svm_fifo_segment_private_t *sp;
   svm_fifo_t *f;
   svm_fifo_t **flist = 0;
@@ -120,12 +125,12 @@
   a->segment_name = "fifo-test1";
   a->segment_size = 256 << 10;
 
-  rv = svm_fifo_segment_create (a);
+  rv = svm_fifo_segment_create (sm, a);
 
   if (rv)
     return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
 
-  sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
+  sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
 
   for (i = 0; i < 1000; i++)
     {
@@ -166,6 +171,7 @@
 offset (int verbose)
 {
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  svm_fifo_segment_main_t *sm = &segment_main;
   svm_fifo_segment_private_t *sp;
   svm_fifo_t *f;
   int rv;
@@ -178,12 +184,12 @@
   a->segment_name = "fifo-test1";
   a->segment_size = 256 << 10;
 
-  rv = svm_fifo_segment_create (a);
+  rv = svm_fifo_segment_create (sm, a);
 
   if (rv)
     return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
 
-  sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
+  sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
 
   f = svm_fifo_segment_alloc_fifo (sp, 200 << 10, FIFO_SEGMENT_RX_FREELIST);
 
@@ -225,6 +231,7 @@
 slave (int verbose)
 {
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  svm_fifo_segment_main_t *sm = &segment_main;
   svm_fifo_segment_private_t *sp;
   svm_fifo_t *f;
   ssvm_shared_header_t *sh;
@@ -243,7 +250,7 @@
   if (rv)
     return clib_error_return (0, "svm_fifo_segment_attach returned %d", rv);
 
-  sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
+  sp = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
   sh = sp->ssvm.sh;
   fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
 
@@ -269,11 +276,12 @@
 int
 test_ssvm_fifo1 (unformat_input_t * input)
 {
+  svm_fifo_segment_main_t *sm = &segment_main;
   clib_error_t *error = 0;
   int verbose = 0;
   int test_id = 0;
 
-  svm_fifo_segment_main_init (0x200000000ULL, 20);
+  svm_fifo_segment_main_init (sm, 0x200000000ULL, 20);
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
diff --git a/src/tests/vnet/session/tcp_echo.c b/src/tests/vnet/session/tcp_echo.c
index bde55ad..e553a3a 100644
--- a/src/tests/vnet/session/tcp_echo.c
+++ b/src/tests/vnet/session/tcp_echo.c
@@ -142,8 +142,7 @@
    * and all other messages are exchanged using shm IPC. */
   u8 use_sock_api;
 
-  /* convenience */
-  svm_fifo_segment_main_t *segment_main;
+  svm_fifo_segment_main_t segment_main;
 } echo_main_t;
 
 echo_main_t echo_main;
@@ -350,6 +349,7 @@
 ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
 {
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  svm_fifo_segment_main_t *sm = &echo_main.segment_main;
   int rv;
 
   clib_memset (a, 0, sizeof (*a));
@@ -359,7 +359,7 @@
   if (type == SSVM_SEGMENT_MEMFD)
     a->memfd_fd = fd;
 
-  if ((rv = svm_fifo_segment_attach (a)))
+  if ((rv = svm_fifo_segment_attach (sm, a)))
     {
       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
       return rv;
@@ -514,6 +514,7 @@
 static void
 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
 {
+  svm_fifo_segment_main_t *sm = &echo_main.segment_main;
   svm_fifo_segment_create_args_t _a, *a = &_a;
   int rv;
 
@@ -521,7 +522,7 @@
   a->segment_name = (char *) mp->segment_name;
   a->segment_size = mp->segment_size;
   /* Attach to the segment vpp created */
-  rv = svm_fifo_segment_attach (a);
+  rv = svm_fifo_segment_attach (sm, a);
   if (rv)
     {
       clib_warning ("svm_fifo_segment_attach ('%s') failed",
@@ -1384,6 +1385,7 @@
 {
   int i_am_server = 1, test_return_packets = 0;
   echo_main_t *em = &echo_main;
+  svm_fifo_segment_main_t *sm = &em->segment_main;
   unformat_input_t _argv, *a = &_argv;
   u8 *chroot_prefix;
   u8 *uri = 0;
@@ -1406,7 +1408,7 @@
 
   clib_time_init (&em->clib_time);
   init_error_string_table (em);
-  svm_fifo_segment_main_init (0x200000000ULL, 20);
+  svm_fifo_segment_main_init (sm, 0x200000000ULL, 20);
   unformat_init_command_line (a, argv);
 
   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
@@ -1467,7 +1469,6 @@
     }
 
   em->i_am_master = i_am_server;
-  em->segment_main = &svm_fifo_segment_main;
   em->test_return_packets = test_return_packets;
   em->bytes_to_send = bytes_to_send;
   em->time_to_stop = 0;
diff --git a/src/tests/vnet/session/udp_echo.c b/src/tests/vnet/session/udp_echo.c
index ea68ee1..b8c4c5d 100644
--- a/src/tests/vnet/session/udp_echo.c
+++ b/src/tests/vnet/session/udp_echo.c
@@ -121,8 +121,7 @@
   /* VNET_API_ERROR_FOO -> "Foo" hash table */
   uword *error_string_by_error_number;
 
-  /* convenience */
-  svm_fifo_segment_main_t *segment_main;
+  svm_fifo_segment_main_t segment_main;
 
   u8 *connect_test_data;
 
@@ -336,8 +335,9 @@
 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
 					   mp)
 {
-  udp_echo_main_t *utm = &udp_echo_main;
   svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
+  udp_echo_main_t *utm = &udp_echo_main;
+  svm_fifo_segment_main_t *sm = &utm->segment_main;
   int rv;
 
   if (mp->retval)
@@ -359,7 +359,7 @@
   ASSERT (mp->app_event_queue_address);
 
   /* Attach to the segment vpp created */
-  rv = svm_fifo_segment_attach (a);
+  rv = svm_fifo_segment_attach (sm, a);
   if (rv)
     {
       clib_warning ("svm_fifo_segment_attach ('%s') failed",
@@ -912,8 +912,9 @@
 static void
 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
 {
-  udp_echo_main_t *utm = &udp_echo_main;
   svm_fifo_segment_create_args_t _a, *a = &_a;
+  udp_echo_main_t *utm = &udp_echo_main;
+  svm_fifo_segment_main_t *sm = &utm->segment_main;
   svm_fifo_segment_private_t *seg;
   int rv;
 
@@ -921,14 +922,14 @@
   a->segment_name = (char *) mp->segment_name;
   a->segment_size = mp->segment_size;
   /* Attach to the segment vpp created */
-  rv = svm_fifo_segment_attach (a);
+  rv = svm_fifo_segment_attach (sm, a);
   if (rv)
     {
       clib_warning ("svm_fifo_segment_attach ('%s') failed",
 		    mp->segment_name);
       return;
     }
-  seg = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
+  seg = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]);
   clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name,
 		seg->ssvm.ssvm_size);
   hash_set (utm->segments_table, clib_net_to_host_u64 (mp->segment_handle),
@@ -939,6 +940,7 @@
 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
 {
   udp_echo_main_t *utm = &udp_echo_main;
+  svm_fifo_segment_main_t *sm = &utm->segment_main;
   svm_fifo_segment_private_t *seg;
   u64 *seg_indexp, segment_handle;
 
@@ -950,8 +952,8 @@
       return;
     }
   hash_unset (utm->segments_table, segment_handle);
-  seg = svm_fifo_segment_get_segment ((u32) seg_indexp[0]);
-  svm_fifo_segment_delete (seg);
+  seg = svm_fifo_segment_get_segment (sm, (u32) seg_indexp[0]);
+  svm_fifo_segment_delete (sm, seg);
   clib_warning ("Unmapped segment '%s'", segment_handle);
 }
 
@@ -1206,6 +1208,7 @@
 main (int argc, char **argv)
 {
   udp_echo_main_t *utm = &udp_echo_main;
+  svm_fifo_segment_main_t *sm = &utm->segment_main;
   u8 *uri = (u8 *) "udp://6.0.1.1/1234";
   unformat_input_t _argv, *a = &_argv;
   int i_am_server = 1;
@@ -1217,7 +1220,7 @@
 
   clib_mem_init_thread_safe (0, 256 << 20);
 
-  svm_fifo_segment_main_init (0x200000000ULL, 20);
+  svm_fifo_segment_main_init (sm, 0x200000000ULL, 20);
 
   vec_validate (utm->rx_buf, 128 << 10);
   utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
@@ -1226,7 +1229,6 @@
   utm->have_return = 1;
   utm->bytes_to_send = 1024;
   utm->fifo_size = 128 << 10;
-  utm->segment_main = &svm_fifo_segment_main;
   utm->cut_through_session_index = ~0;
   clib_time_init (&utm->clib_time);
 
diff --git a/src/vcl/vcl_bapi.c b/src/vcl/vcl_bapi.c
index c6e7fdf..cd3aaaa 100644
--- a/src/vcl/vcl_bapi.c
+++ b/src/vcl/vcl_bapi.c
@@ -75,7 +75,7 @@
   if (type == SSVM_SEGMENT_MEMFD)
     a->memfd_fd = fd;
 
-  if ((rv = svm_fifo_segment_attach (a)))
+  if ((rv = svm_fifo_segment_attach (&vcm->segment_main, a)))
     {
       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
       return rv;
@@ -88,14 +88,15 @@
 static void
 vcl_segment_detach (u64 segment_handle)
 {
+  svm_fifo_segment_main_t *sm = &vcm->segment_main;
   svm_fifo_segment_private_t *segment;
   u32 segment_index;
 
   segment_index = vcl_segment_table_lookup (segment_handle);
   if (segment_index == (u32) ~ 0)
     return;
-  segment = svm_fifo_segment_get_segment (segment_index);
-  svm_fifo_segment_delete (segment);
+  segment = svm_fifo_segment_get_segment (sm, segment_index);
+  svm_fifo_segment_delete (sm, segment);
   vcl_segment_table_del (segment_handle);
 }
 
diff --git a/src/vcl/vcl_private.h b/src/vcl/vcl_private.h
index 6ca0471..6a289d6 100644
--- a/src/vcl/vcl_private.h
+++ b/src/vcl/vcl_private.h
@@ -326,6 +326,8 @@
   /** Mapped segments table */
   uword *segment_table;
 
+  svm_fifo_segment_main_t segment_main;
+
 #ifdef VCL_ELOG
   /* VPP Event-logger */
   elog_main_t elog_main;
diff --git a/src/vcl/vppcom.c b/src/vcl/vppcom.c
index 34b663b..db767e9 100644
--- a/src/vcl/vppcom.c
+++ b/src/vcl/vppcom.c
@@ -767,7 +767,7 @@
   vcm->main_pid = getpid ();
   vcm->app_name = format (0, "%s", app_name);
   vppcom_init_error_string_table ();
-  svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
+  svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
 			      20 /* timeout in secs */ );
   pool_alloc (vcm->workers, vcl_cfg->max_workers);
   clib_spinlock_init (&vcm->workers_lock);