session/tcp: improve preallocated segment handling
[vpp.git] / src / vnet / tcp / tcp.c
index a4c1308..f779428 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;
@@ -561,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
@@ -572,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);
 }
 
@@ -691,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);
 
@@ -784,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",
@@ -1014,7 +1035,7 @@ tcp_round_snd_space (tcp_connection_t * tc, u32 snd_space)
   /* 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))
-    return 0;
+    return snd_space < tc->cwnd ? 0 : snd_space;
 
   /* round down to mss multiple */
   return snd_space - (snd_space % tc->snd_mss);
@@ -1146,6 +1167,7 @@ tcp_timer_establish_handler (u32 conn_index)
     {
       ASSERT (tc->state == TCP_STATE_SYN_SENT);
       stream_session_connect_notify (&tc->connection, 1 /* fail */ );
+      TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
     }
   else
     {
@@ -1153,8 +1175,11 @@ tcp_timer_establish_handler (u32 conn_index)
       /* note: the connection may have already disappeared */
       if (PREDICT_FALSE (tc == 0))
        return;
-
+      TCP_DBG ("establish pop: %U", format_tcp_connection, tc, 2);
       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);
@@ -1345,6 +1370,8 @@ tcp_main_enable (vlib_main_t * vm)
 
   vec_validate (tm->tx_frames[0], num_threads - 1);
   vec_validate (tm->tx_frames[1], num_threads - 1);
+  vec_validate (tm->ip_lookup_tx_frames[0], num_threads - 1);
+  vec_validate (tm->ip_lookup_tx_frames[1], num_threads - 1);
 
   tm->bytes_per_buffer = vlib_buffer_free_list_buffer_size
     (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);