tcp: horizontal scaling improvments
[vpp.git] / src / vnet / tcp / tcp.c
index 6edf52a..04f1e06 100644 (file)
@@ -160,6 +160,7 @@ tcp_half_open_connection_new (void)
 {
   tcp_main_t *tm = vnet_get_tcp_main ();
   tcp_connection_t *tc = 0;
+  ASSERT (vlib_get_thread_index () == 0);
   pool_get (tm->half_open_connections, tc);
   memset (tc, 0, sizeof (*tc));
   tc->c_c_index = tc - tm->half_open_connections;
@@ -316,8 +317,10 @@ tcp_connection_close (tcp_connection_t * tc)
       tcp_send_fin (tc);
       tc->state = TCP_STATE_LAST_ACK;
       break;
+    case TCP_STATE_FIN_WAIT_1:
+      break;
     default:
-      clib_warning ("shouldn't be here");
+      clib_warning ("state: %u", tc->state);
     }
 
   TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc);
@@ -559,6 +562,22 @@ tcp_connection_fib_attach (tcp_connection_t * tc)
 }
 #endif /* 0 */
 
+/**
+ * Initialize connection send variables.
+ */
+void
+tcp_init_snd_vars (tcp_connection_t * tc)
+{
+  u32 time_now;
+
+  /* Set random initial sequence */
+  time_now = tcp_time_now ();
+  tc->iss = random_u32 (&time_now);
+  tc->snd_una = tc->iss;
+  tc->snd_nxt = tc->iss + 1;
+  tc->snd_una_max = tc->snd_nxt;
+}
+
 /** Initialize tcp connection variables
  *
  * Should be called after having received a msg from the peer, i.e., a SYN or
@@ -570,6 +589,9 @@ tcp_connection_init_vars (tcp_connection_t * tc)
   tcp_init_mss (tc);
   scoreboard_init (&tc->sack_sb);
   tcp_cc_init (tc);
+  if (tc->state == TCP_STATE_SYN_RCVD)
+    tcp_init_snd_vars (tc);
+
   //  tcp_connection_fib_attach (tc);
 }
 
@@ -689,6 +711,7 @@ tcp_connection_open (transport_endpoint_t * rmt)
 
   TCP_EVT_DBG (TCP_EVT_OPEN, tc);
   tc->state = TCP_STATE_SYN_SENT;
+  tcp_init_snd_vars (tc);
   tcp_send_syn (tc);
   clib_spinlock_unlock_if_init (&tm->half_open_lock);
 
@@ -782,7 +805,7 @@ format_tcp_vars (u8 * s, va_list * args)
              tc->snd_wnd, tc->rcv_wnd, tc->snd_wl1 - tc->irs,
              tc->snd_wl2 - tc->iss);
   s = format (s, " flight size %u send space %u rcv_wnd_av %d\n",
-             tcp_flight_size (tc), tcp_available_snd_space (tc),
+             tcp_flight_size (tc), tcp_available_output_snd_space (tc),
              tcp_rcv_wnd_available (tc));
   s = format (s, " cong %U ", format_tcp_congestion_status, tc);
   s = format (s, "cwnd %u ssthresh %u rtx_bytes %u bytes_acked %u\n",
@@ -798,7 +821,8 @@ format_tcp_vars (u8 * s, va_list * args)
   s = format (s, "rtt_seq %u\n", tc->rtt_seq);
   s = format (s, " tsval_recent %u tsval_recent_age %u\n", tc->tsval_recent,
              tcp_time_now () - tc->tsval_recent_age);
-  s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb);
+  s = format (s, " scoreboard: %U\n", format_tcp_scoreboard, &tc->sack_sb,
+             tc);
   if (vec_len (tc->snd_sacks))
     s = format (s, " sacks tx: %U\n", format_tcp_sacks, tc);
 
@@ -835,6 +859,8 @@ format_tcp_connection (u8 * s, va_list * args)
   tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
   u32 verbose = va_arg (*args, u32);
 
+  if (!tc)
+    return s;
   s = format (s, "%-50U", format_tcp_connection_id, tc);
   if (verbose)
     {
@@ -858,7 +884,7 @@ format_tcp_session (u8 * s, va_list * args)
   if (tc)
     s = format (s, "%U", format_tcp_connection, tc, verbose);
   else
-    s = format (s, "empty");
+    s = format (s, "empty\n");
   return s;
 }
 
@@ -930,7 +956,11 @@ u8 *
 format_tcp_sack_hole (u8 * s, va_list * args)
 {
   sack_scoreboard_hole_t *hole = va_arg (*args, sack_scoreboard_hole_t *);
-  s = format (s, "[%u, %u]", hole->start, hole->end);
+  tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
+  if (tc)
+    s = format (s, "  [%u, %u]", hole->start - tc->iss, hole->end - tc->iss);
+  else
+    s = format (s, "  [%u, %u]", hole->start, hole->end);
   return s;
 }
 
@@ -938,6 +968,7 @@ u8 *
 format_tcp_scoreboard (u8 * s, va_list * args)
 {
   sack_scoreboard_t *sb = va_arg (*args, sack_scoreboard_t *);
+  tcp_connection_t *tc = va_arg (*args, tcp_connection_t *);
   sack_scoreboard_hole_t *hole;
   s = format (s, "sacked_bytes %u last_sacked_bytes %u lost_bytes %u\n",
              sb->sacked_bytes, sb->last_sacked_bytes, sb->lost_bytes);
@@ -952,7 +983,7 @@ format_tcp_scoreboard (u8 * s, va_list * args)
 
   while (hole)
     {
-      s = format (s, "%U", format_tcp_sack_hole, hole);
+      s = format (s, "%U", format_tcp_sack_hole, hole, tc);
       hole = scoreboard_next_hole (sb, hole);
     }
 
@@ -1001,13 +1032,10 @@ tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
       return tc->snd_wnd <= snd_space ? tc->snd_wnd : 0;
     }
 
-  /* If we can't write at least a segment, don't try at all */
+  /* If not snd_wnd constrained and we can't write at least a segment,
+   * don't try at all */
   if (PREDICT_FALSE (snd_space < tc->snd_mss))
-    {
-      if (snd_space > clib_min (tc->mss, tc->rcv_opts.mss) - TCP_HDR_LEN_MAX)
-       return snd_space;
-      return 0;
-    }
+    return 0;
 
   /* round down to mss multiple */
   return snd_space - (snd_space % tc->snd_mss);
@@ -1030,7 +1058,7 @@ tcp_snd_space (tcp_connection_t * tc)
 
   if (PREDICT_TRUE (tcp_in_cong_recovery (tc) == 0))
     {
-      snd_space = tcp_available_snd_space (tc);
+      snd_space = tcp_available_output_snd_space (tc);
 
       /* If we haven't gotten dupacks or if we did and have gotten sacked
        * bytes then we can still send as per Limited Transmit (RFC3042) */
@@ -1051,17 +1079,20 @@ tcp_snd_space (tcp_connection_t * tc)
   if (tcp_in_recovery (tc))
     {
       tc->snd_nxt = tc->snd_una_max;
-      snd_space = tcp_available_wnd (tc) - tc->snd_rxt_bytes
+      snd_space = tcp_available_snd_wnd (tc) - tc->snd_rxt_bytes
        - (tc->snd_una_max - tc->snd_congestion);
       if (snd_space <= 0 || (tc->snd_una_max - tc->snd_una) >= tc->snd_wnd)
        return 0;
       return tcp_round_snd_space (tc, snd_space);
     }
 
-  /* If in fast recovery, send 1 SMSS if wnd allows */
-  if (tcp_in_fastrecovery (tc)
-      && tcp_available_snd_space (tc) && !tcp_fastrecovery_sent_1_smss (tc))
+  /* RFC 5681: When previously unsent data is available and the new value of
+   * cwnd and the receiver's advertised window allow, a TCP SHOULD send 1*SMSS
+   * bytes of previously unsent data. */
+  if (tcp_in_fastrecovery (tc) && !tcp_fastrecovery_sent_1_smss (tc))
     {
+      if (tcp_available_output_snd_space (tc) < tc->snd_mss)
+       return 0;
       tcp_fastrecovery_1_smss_on (tc);
       return tc->snd_mss;
     }
@@ -1073,7 +1104,8 @@ u32
 tcp_session_send_space (transport_connection_t * trans_conn)
 {
   tcp_connection_t *tc = (tcp_connection_t *) trans_conn;
-  return tcp_snd_space (tc);
+  return clib_min (tcp_snd_space (tc),
+                  tc->snd_wnd - (tc->snd_nxt - tc->snd_una));
 }
 
 i32
@@ -1139,7 +1171,14 @@ tcp_timer_establish_handler (u32 conn_index)
   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);
+      /* Start cleanup. App wasn't notified yet so use delete notify as
+       * opposed to delete to cleanup session layer state. */
+      stream_session_delete_notify (&tc->connection);
     }
   tc->timers[TCP_TIMER_ESTABLISH] = TCP_TIMER_HANDLE_INVALID;
   tcp_connection_cleanup (tc);
@@ -1233,7 +1272,7 @@ tcp_main_enable (vlib_main_t * vm)
   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;
 
@@ -1286,21 +1325,17 @@ tcp_main_enable (vlib_main_t * vm)
     }
   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);
@@ -1314,9 +1349,14 @@ tcp_main_enable (vlib_main_t * vm)
   tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
     / TCP_TSTAMP_RESOLUTION;
 
+  if (tm->local_endpoints_table_buckets == 0)
+    tm->local_endpoints_table_buckets = 250000;
+  if (tm->local_endpoints_table_memory == 0)
+    tm->local_endpoints_table_memory = 512 << 20;
+
   clib_bihash_init_24_8 (&tm->local_endpoints_table, "local endpoint table",
-                        1000000 /* $$$$ config parameter nbuckets */ ,
-                        (512 << 20) /*$$$ config parameter table size */ );
+                        tm->local_endpoints_table_buckets,
+                        tm->local_endpoints_table_memory);
 
   /* Initialize [port-allocator] random number seed */
   tm->port_allocator_seed = (u32) clib_cpu_time_now ();
@@ -1370,6 +1410,7 @@ static clib_error_t *
 tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
 {
   tcp_main_t *tm = vnet_get_tcp_main ();
+  u64 tmp;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
@@ -1380,6 +1421,19 @@ tcp_config_fn (vlib_main_t * vm, unformat_input_t * input)
       else if (unformat (input, "preallocated-half-open-connections %d",
                         &tm->preallocated_half_open_connections))
        ;
+      else if (unformat (input, "local-endpoints-table-memory %U",
+                        unformat_memory_size, &tmp))
+       {
+         if (tmp >= 0x100000000)
+           return clib_error_return (0, "memory size %llx (%lld) too large",
+                                     tmp, tmp);
+         tm->local_endpoints_table_memory = tmp;
+       }
+      else if (unformat (input, "local-endpoints-table-buckets %d",
+                        &tm->local_endpoints_table_buckets))
+       ;
+
+
       else
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);