Add fixed-size, preallocated pool support

Simply call pool_init_fixed(...) before using the pool. Note that
fixed, preallocated pools live in individually-mmap'ed address
segments, except for the free element bitmap. A large fixed pool can
exceed 4gb.

Fix tcp buffer allocator leak, remove broken assert

Change-Id: I4421082e12a77c41c6e20f7747f3150dcd01fc26
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/src/vnet/session/application_interface.c b/src/vnet/session/application_interface.c
index 566a52d..8dbc3a1 100644
--- a/src/vnet/session/application_interface.c
+++ b/src/vnet/session/application_interface.c
@@ -207,11 +207,22 @@
   return 0;
 }
 
+static u8 *cache_uri;
+static session_type_t cache_sst;
+static transport_endpoint_t *cache_tep;
+
 int
 parse_uri (char *uri, session_type_t * sst, transport_endpoint_t * tep)
 {
   unformat_input_t _input, *input = &_input;
 
+  if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
+    {
+      *sst = cache_sst;
+      *tep = *cache_tep;
+      return 0;
+    }
+
   /* Make sure */
   uri = (char *) format (0, "%s%c", uri, 0);
 
@@ -224,6 +235,14 @@
     }
   unformat_free (input);
 
+  vec_free (cache_uri);
+  cache_uri = (u8 *) uri;
+  cache_sst = *sst;
+  if (cache_tep)
+    clib_mem_free (cache_tep);
+  cache_tep = clib_mem_alloc (sizeof (*tep));
+  *cache_tep = *tep;
+
   return 0;
 }
 
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index dcd141f..17644e2 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -889,32 +889,24 @@
     session_vpp_event_queue_allocate (smm, i);
 
   /* Preallocate sessions */
-  if (num_threads == 1)
+  if (smm->preallocated_sessions)
     {
-      for (i = 0; i < smm->preallocated_sessions; i++)
+      if (num_threads == 1)
 	{
-	  stream_session_t *ss __attribute__ ((unused));
-	  pool_get_aligned (smm->sessions[0], ss, CLIB_CACHE_LINE_BYTES);
+	  pool_init_fixed (smm->sessions[0], smm->preallocated_sessions);
 	}
-
-      for (i = 0; i < smm->preallocated_sessions; i++)
-	pool_put_index (smm->sessions[0], i);
-    }
-  else
-    {
-      int j;
-      preallocated_sessions_per_worker = smm->preallocated_sessions /
-	(num_threads - 1);
-
-      for (j = 1; j < num_threads; j++)
+      else
 	{
-	  for (i = 0; i < preallocated_sessions_per_worker; i++)
+	  int j;
+	  preallocated_sessions_per_worker =
+	    (1.1 * (f64) smm->preallocated_sessions /
+	     (f64) (num_threads - 1));
+
+	  for (j = 1; j < num_threads; j++)
 	    {
-	      stream_session_t *ss __attribute__ ((unused));
-	      pool_get_aligned (smm->sessions[j], ss, CLIB_CACHE_LINE_BYTES);
+	      pool_init_fixed (smm->sessions[j],
+			       preallocated_sessions_per_worker);
 	    }
-	  for (i = 0; i < preallocated_sessions_per_worker; i++)
-	    pool_put_index (smm->sessions[j], i);
 	}
     }
 
diff --git a/src/vnet/session/session_cli.c b/src/vnet/session/session_cli.c
index 028dc9d..d9f516b 100755
--- a/src/vnet/session/session_cli.c
+++ b/src/vnet/session/session_cli.c
@@ -115,8 +115,8 @@
     {
       *proto = TRANSPORT_PROTO_UDP;
     }
-  else if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
-		     lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
+  if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
+		lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
     {
       *is_ip4 = 1;
       tuple_is_set = 1;
diff --git a/src/vnet/tcp/tcp.c b/src/vnet/tcp/tcp.c
index 0a826a5..a4c1308 100644
--- a/src/vnet/tcp/tcp.c
+++ b/src/vnet/tcp/tcp.c
@@ -1150,6 +1150,10 @@
   else
     {
       tc = tcp_connection_get (conn_index, vlib_get_thread_index ());
+      /* note: the connection may have already disappeared */
+      if (PREDICT_FALSE (tc == 0))
+	return;
+
       ASSERT (tc->state == TCP_STATE_SYN_RCVD);
     }
   tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
@@ -1244,7 +1248,7 @@
   vlib_thread_main_t *vtm = vlib_get_thread_main ();
   clib_error_t *error = 0;
   u32 num_threads;
-  int i, thread;
+  int thread;
   tcp_connection_t *tc __attribute__ ((unused));
   u32 preallocated_connections_per_thread;
 
@@ -1297,21 +1301,17 @@
     }
   for (; thread < num_threads; thread++)
     {
-      for (i = 0; i < preallocated_connections_per_thread; i++)
-	pool_get (tm->connections[thread], tc);
-
-      for (i = 0; i < preallocated_connections_per_thread; i++)
-	pool_put_index (tm->connections[thread], i);
+      if (preallocated_connections_per_thread)
+	pool_init_fixed (tm->connections[thread],
+			 preallocated_connections_per_thread);
     }
 
   /*
-   * Preallocate half-open connections
+   * Use a preallocated half-open connection pool?
    */
-  for (i = 0; i < tm->preallocated_half_open_connections; i++)
-    pool_get (tm->half_open_connections, tc);
-
-  for (i = 0; i < tm->preallocated_half_open_connections; i++)
-    pool_put_index (tm->half_open_connections, i);
+  if (tm->preallocated_half_open_connections)
+    pool_init_fixed (tm->half_open_connections,
+		     tm->preallocated_half_open_connections);
 
   /* Initialize per worker thread tx buffers (used for control messages) */
   vec_validate (tm->tx_buffers, num_threads - 1);
diff --git a/src/vnet/tcp/tcp_output.c b/src/vnet/tcp/tcp_output.c
index 0255551..15a9dcb 100644
--- a/src/vnet/tcp/tcp_output.c
+++ b/src/vnet/tcp/tcp_output.c
@@ -440,13 +440,16 @@
 always_inline int
 tcp_alloc_tx_buffers (tcp_main_t * tm, u8 thread_index, u32 n_free_buffers)
 {
+  u32 current_length = vec_len (tm->tx_buffers[thread_index]);
+
   vec_validate (tm->tx_buffers[thread_index],
-		vec_len (tm->tx_buffers[thread_index]) + n_free_buffers - 1);
+		current_length + n_free_buffers - 1);
   _vec_len (tm->tx_buffers[thread_index]) =
-    vlib_buffer_alloc_from_free_list (vlib_get_main (),
-				      tm->tx_buffers[thread_index],
-				      n_free_buffers,
-				      VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+    current_length + vlib_buffer_alloc_from_free_list (vlib_get_main (),
+						       tm->tx_buffers
+						       [thread_index],
+						       n_free_buffers,
+						       VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
   /* buffer shortage, report failure */
   if (vec_len (tm->tx_buffers[thread_index]) == 0)
     {
@@ -1293,11 +1296,17 @@
   if (is_syn)
     {
       tc = tcp_half_open_connection_get (index);
+      /* Note: the connection may have transitioned to ESTABLISHED... */
+      if (PREDICT_FALSE (tc == 0))
+	return;
       tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
     }
   else
     {
       tc = tcp_connection_get (index, thread_index);
+      /* Note: the connection may have been closed and pool_put */
+      if (PREDICT_FALSE (tc == 0))
+	return;
       tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
     }
 
@@ -1332,25 +1341,27 @@
 
       TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1);
 
-      /* Send one segment */
+      /* Send one segment. Note that n_bytes may be zero due to buffer shortfall  */
       n_bytes = tcp_prepare_retransmit_segment (tc, 0, tc->snd_mss, &b);
-      ASSERT (n_bytes);
-      bi = vlib_get_buffer_index (vm, b);
+
       /* TODO be less aggressive about this */
       scoreboard_clear (&tc->sack_sb);
 
       if (n_bytes == 0)
 	{
-	  clib_warning ("could not retransmit anything");
-	  clib_warning ("%U", format_tcp_connection, tc, 2);
-
+	  if (b)
+	    {
+	      clib_warning ("retransmit fail: %U", format_tcp_connection, tc,
+			    2);
+	      ASSERT (tc->rto_boff > 1 && tc->snd_una == tc->snd_congestion);
+	    }
 	  /* Try again eventually */
 	  tcp_retransmit_timer_set (tc);
-	  ASSERT (0 || (tc->rto_boff > 1
-			&& tc->snd_una == tc->snd_congestion));
 	  return;
 	}
 
+      bi = vlib_get_buffer_index (vm, b);
+
       /* For first retransmit, record timestamp (Eifel detection RFC3522) */
       if (tc->rto_boff == 1)
 	tc->snd_rxt_ts = tcp_time_now ();
@@ -1378,7 +1389,10 @@
 	tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
 
       if (PREDICT_FALSE (tcp_get_free_buffer_index (tm, &bi)))
-	return;
+	{
+	  clib_warning ("tcp_get_free_buffer_index FAIL");
+	  return;
+	}
       b = vlib_get_buffer (vm, bi);
       tcp_init_buffer (vm, b);
       tcp_push_hdr_i (tc, b, tc->state, 1);