session: use pool for segment manager properties

Change-Id: I280fea2610dcfc0b2da84973b9f567daec42f1f6
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
index 9a519c7..9cb4cb7 100644
--- a/src/vnet/session/application.c
+++ b/src/vnet/session/application.c
@@ -146,11 +146,12 @@
 void
 application_del (application_t * app)
 {
+  segment_manager_properties_t *props;
+  vnet_unbind_args_t _a, *a = &_a;
   segment_manager_t *sm;
   u64 handle, *handles = 0;
   u32 index;
   int i;
-  vnet_unbind_args_t _a, *a = &_a;
 
   /*
    * The app event queue allocated in first segment is cleared with
@@ -207,7 +208,8 @@
 	  segment_manager_del (sm);
 	}
     }
-
+  props = segment_manager_properties_get (app->sm_properties);
+  segment_manager_properties_free (props);
   application_table_del (app);
   pool_put (app_pool, app);
 }
@@ -243,7 +245,8 @@
    */
   sm = segment_manager_new ();
   sm->app_index = app->index;
-  props = &app->sm_properties;
+  props = segment_manager_properties_alloc ();
+  app->sm_properties = segment_manager_properties_index (props);
   props->add_segment_size = options[SESSION_OPTIONS_ADD_SEGMENT_SIZE];
   props->rx_fifo_size = options[SESSION_OPTIONS_RX_FIFO_SIZE];
   props->rx_fifo_size =
@@ -259,7 +262,7 @@
   props->private_segment_size = options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE];
 
   first_seg_size = options[SESSION_OPTIONS_SEGMENT_SIZE];
-  if ((rv = segment_manager_init (sm, props, first_seg_size)))
+  if ((rv = segment_manager_init (sm, app->sm_properties, first_seg_size)))
     return rv;
   sm->first_is_protected = 1;
 
@@ -329,7 +332,7 @@
     }
 
   sm = segment_manager_new ();
-  sm->properties = &app->sm_properties;
+  sm->properties_index = app->sm_properties;
 
   return sm;
 }
@@ -745,6 +748,7 @@
 {
   application_t *app = va_arg (*args, application_t *);
   CLIB_UNUSED (int verbose) = va_arg (*args, int);
+  segment_manager_properties_t *props;
   const u8 *app_ns_name;
   u8 *app_name;
 
@@ -763,13 +767,13 @@
 
   app_name = app_get_name_from_reg_index (app);
   app_ns_name = app_namespace_id_from_index (app->ns_index);
+  props = segment_manager_properties_get (app->sm_properties);
   if (verbose)
     s =
       format (s, "%-10d%-20s%-15s%-15d%-15d%-15d%-15d", app->index, app_name,
 	      app_ns_name, app->api_client_index,
-	      app->sm_properties.add_segment_size,
-	      app->sm_properties.rx_fifo_size,
-	      app->sm_properties.tx_fifo_size);
+	      props->add_segment_size,
+	      props->rx_fifo_size, props->tx_fifo_size);
   else
     s = format (s, "%-10d%-20s%-15s%-20d", app->index, app_name, app_ns_name,
 		app->api_client_index);
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
index 94a4e25..2892bc5 100644
--- a/src/vnet/session/application.h
+++ b/src/vnet/session/application.h
@@ -97,7 +97,7 @@
   u8 first_segment_manager_in_use;
 
   /** Segment manager properties. Shared by all segment managers */
-  segment_manager_properties_t sm_properties;
+  u32 sm_properties;
 
   u16 proxied_transports;
 } application_t;
diff --git a/src/vnet/session/segment_manager.c b/src/vnet/session/segment_manager.c
index f5a4024..e448023 100644
--- a/src/vnet/session/segment_manager.c
+++ b/src/vnet/session/segment_manager.c
@@ -27,6 +27,11 @@
  */
 segment_manager_t *segment_managers = 0;
 
+/*
+ * Pool of segment manager properties
+ */
+static segment_manager_properties_t *segment_manager_properties_pool;
+
 /**
  * Process private segment index
  */
@@ -38,6 +43,36 @@
 u32 default_fifo_size = 1 << 16;
 u32 default_segment_size = 1 << 20;
 
+segment_manager_properties_t *
+segment_manager_properties_alloc (void)
+{
+  segment_manager_properties_t *props;
+  pool_get (segment_manager_properties_pool, props);
+  memset (props, 0, sizeof (*props));
+  return props;
+}
+
+void
+segment_manager_properties_free (segment_manager_properties_t * props)
+{
+  pool_put (segment_manager_properties_pool, props);
+  memset (props, 0xFB, sizeof (*props));
+}
+
+segment_manager_properties_t *
+segment_manager_properties_get (u32 smp_index)
+{
+  if (pool_is_free_index (segment_manager_properties_pool, smp_index))
+    return 0;
+  return pool_elt_at_index (segment_manager_properties_pool, smp_index);
+}
+
+u32
+segment_manager_properties_index (segment_manager_properties_t * p)
+{
+  return p - segment_manager_properties_pool;
+}
+
 void
 segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size)
 {
@@ -52,17 +87,18 @@
 			       u8 * segment_name)
 {
   svm_fifo_segment_create_args_t _ca, *ca = &_ca;
+  segment_manager_properties_t *props;
   int rv;
 
   memset (ca, 0, sizeof (*ca));
-
-  if (!sm->properties->use_private_segment)
+  props = segment_manager_properties_get (sm->properties_index);
+  if (!props->use_private_segment)
     {
       ca->segment_name = (char *) segment_name;
       ca->segment_size = segment_size;
-      ca->rx_fifo_size = sm->properties->rx_fifo_size;
-      ca->tx_fifo_size = sm->properties->tx_fifo_size;
-      ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
+      ca->rx_fifo_size = props->rx_fifo_size;
+      ca->tx_fifo_size = props->tx_fifo_size;
+      ca->preallocated_fifo_pairs = props->preallocated_fifo_pairs;
 
       rv = svm_fifo_segment_create (ca);
       if (rv)
@@ -81,11 +117,11 @@
 
       ca->segment_name = "process-private-segment";
       ca->segment_size = ~0;
-      ca->rx_fifo_size = sm->properties->rx_fifo_size;
-      ca->tx_fifo_size = sm->properties->tx_fifo_size;
-      ca->preallocated_fifo_pairs = sm->properties->preallocated_fifo_pairs;
-      ca->private_segment_count = sm->properties->private_segment_count;
-      ca->private_segment_size = sm->properties->private_segment_size;
+      ca->rx_fifo_size = props->rx_fifo_size;
+      ca->tx_fifo_size = props->tx_fifo_size;
+      ca->preallocated_fifo_pairs = props->preallocated_fifo_pairs;
+      ca->private_segment_count = props->private_segment_count;
+      ca->private_segment_size = props->private_segment_size;
 
       /* Default to a small private segment */
       if (ca->private_segment_size == 0)
@@ -130,15 +166,17 @@
 int
 session_manager_add_segment (segment_manager_t * sm)
 {
-  u8 *segment_name;
   svm_fifo_segment_create_args_t _ca, *ca = &_ca;
+  segment_manager_properties_t *props;
   u32 add_segment_size;
+  u8 *segment_name;
   int rv;
 
   memset (ca, 0, sizeof (*ca));
+  props = segment_manager_properties_get (sm->properties_index);
   segment_name = format (0, "%d-%d%c", getpid (), segment_name_counter++, 0);
-  add_segment_size = sm->properties->add_segment_size ?
-    sm->properties->add_segment_size : default_segment_size;
+  add_segment_size = props->add_segment_size ?
+    props->add_segment_size : default_segment_size;
 
   rv = session_manager_add_segment_i (sm, add_segment_size, segment_name);
   vec_free (segment_name);
@@ -171,17 +209,14 @@
  * Returns error if svm segment allocation fails.
  */
 int
-segment_manager_init (segment_manager_t * sm,
-		      segment_manager_properties_t * properties,
+segment_manager_init (segment_manager_t * sm, u32 props_index,
 		      u32 first_seg_size)
 {
   int rv;
 
   /* app allocates these */
-  sm->properties = properties;
-
+  sm->properties_index = props_index;
   first_seg_size = first_seg_size > 0 ? first_seg_size : default_segment_size;
-
   rv = session_manager_add_first_segment (sm, first_seg_size);
   if (rv)
     {
@@ -348,6 +383,7 @@
 				     u32 * fifo_segment_index)
 {
   svm_fifo_segment_private_t *fifo_segment;
+  segment_manager_properties_t *props;
   u32 fifo_size, sm_index;
   u8 added_a_segment = 0;
   int i;
@@ -359,19 +395,20 @@
   clib_spinlock_lock (&sm->lockp);
 
   /* Allocate svm fifos */
+  props = segment_manager_properties_get (sm->properties_index);
 again:
   for (i = 0; i < vec_len (sm->segment_indices); i++)
     {
       *fifo_segment_index = sm->segment_indices[i];
       fifo_segment = svm_fifo_segment_get_segment (*fifo_segment_index);
 
-      fifo_size = sm->properties->rx_fifo_size;
+      fifo_size = props->rx_fifo_size;
       fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
       *server_rx_fifo =
 	svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
 				     FIFO_SEGMENT_RX_FREELIST);
 
-      fifo_size = sm->properties->tx_fifo_size;
+      fifo_size = props->tx_fifo_size;
       fifo_size = (fifo_size == 0) ? default_fifo_size : fifo_size;
       *server_tx_fifo =
 	svm_fifo_segment_alloc_fifo (fifo_segment, fifo_size,
@@ -404,7 +441,7 @@
   /* See if we're supposed to create another segment */
   if (*server_rx_fifo == 0)
     {
-      if (sm->properties->add_segment && !sm->properties->use_private_segment)
+      if (props->add_segment && !props->use_private_segment)
 	{
 	  if (added_a_segment)
 	    {
@@ -625,7 +662,7 @@
   return 0;
 }
 
-		          /* *INDENT-OFF* */
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (segment_manager_show_command, static) =
 {
   .path = "show segment-manager",
diff --git a/src/vnet/session/segment_manager.h b/src/vnet/session/segment_manager.h
index 8c43880..f649461 100644
--- a/src/vnet/session/segment_manager.h
+++ b/src/vnet/session/segment_manager.h
@@ -59,7 +59,7 @@
    * Pointer to manager properties. Could be shared among all of
    * an app's segment managers s
    */
-  segment_manager_properties_t *properties;
+  u32 properties_index;
 
   /**
    * First segment should not be deleted unless segment manger is deleted.
@@ -96,9 +96,7 @@
 
 segment_manager_t *segment_manager_new ();
 int
-segment_manager_init (segment_manager_t * sm,
-		      segment_manager_properties_t * properties,
-		      u32 seg_size);
+segment_manager_init (segment_manager_t * sm, u32 props_index, u32 seg_size);
 
 void segment_manager_get_segment_info (u32 index, u8 ** name, u32 * size);
 int
@@ -122,6 +120,11 @@
 				    unix_shared_memory_queue_t * q);
 void segment_manager_app_detach (segment_manager_t * sm);
 
+segment_manager_properties_t *segment_manager_properties_alloc (void);
+void segment_manager_properties_free (segment_manager_properties_t * p);
+segment_manager_properties_t *segment_manager_properties_get (u32 smp_index);
+u32 segment_manager_properties_index (segment_manager_properties_t * p);
+
 #endif /* SRC_VNET_SESSION_SEGMENT_MANAGER_H_ */
 /*
  * fd.io coding-style-patch-verification: ON
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
index e0348df..fb74e6b 100755
--- a/src/vnet/session/session_api.c
+++ b/src/vnet/session/session_api.c
@@ -212,6 +212,7 @@
 {
   vl_api_connect_sock_t *mp = mp_arg;
   unix_shared_memory_queue_t *server_q, *client_q;
+  segment_manager_properties_t *props;
   vlib_main_t *vm = vlib_get_main ();
   f64 timeout = vlib_time_now (vm) + 0.5;
   application_t *app;
@@ -241,8 +242,9 @@
       return -1;
     }
 
-  mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
-  mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
+  props = segment_manager_properties_get (app->sm_properties);
+  mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = props->rx_fifo_size;
+  mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = props->tx_fifo_size;
 
   /*
    * Bounce message handlers MUST NOT block the data-plane.