tcp: track reorder with sacks
[vpp.git] / src / vnet / tcp / tcp.c
index e4335e0..1563be4 100644 (file)
@@ -186,7 +186,7 @@ tcp_session_get_listener (u32 listener_index)
  *
  */
 static void
-tcp_half_open_connection_del (tcp_connection_t * tc)
+tcp_half_open_connection_free (tcp_connection_t * tc)
 {
   tcp_main_t *tm = vnet_get_tcp_main ();
   clib_spinlock_lock_if_init (&tm->half_open_lock);
@@ -214,9 +214,10 @@ tcp_half_open_connection_cleanup (tcp_connection_t * tc)
   if (tc->c_thread_index != vlib_get_thread_index ())
     return 1;
 
+  session_half_open_delete_notify (TRANSPORT_PROTO_TCP, tc->c_s_ho_handle);
   wrk = tcp_get_worker (tc->c_thread_index);
   tcp_timer_reset (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT_SYN);
-  tcp_half_open_connection_del (tc);
+  tcp_half_open_connection_free (tc);
   return 0;
 }
 
@@ -443,6 +444,18 @@ tcp_session_cleanup (u32 conn_index, u32 thread_index)
   tcp_connection_cleanup (tc);
 }
 
+static void
+tcp_session_cleanup_ho (u32 conn_index)
+{
+  tcp_worker_ctx_t *wrk;
+  tcp_connection_t *tc;
+
+  tc = tcp_half_open_connection_get (conn_index);
+  wrk = tcp_get_worker (tc->c_thread_index);
+  tcp_timer_reset (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT_SYN);
+  tcp_half_open_connection_free (tc);
+}
+
 static void
 tcp_session_reset (u32 conn_index, u32 thread_index)
 {
@@ -668,7 +681,7 @@ tcp_init_snd_vars (tcp_connection_t * tc)
   tc->snd_una = tc->iss;
   tc->snd_nxt = tc->iss + 1;
   tc->snd_una_max = tc->snd_nxt;
-  tc->srtt = 100;              /* 100 ms */
+  tc->srtt = 0.1 * THZ;                /* 100 ms */
 
   if (!tcp_cfg.csum_offload)
     tc->cfg_flags |= TCP_CFG_F_NO_CSUM_OFFLOAD;
@@ -912,17 +925,21 @@ tcp_snd_space_inline (tcp_connection_t * tc)
    * to force the peer to send enough dupacks to start retransmitting as
    * per Limited Transmit (RFC3042)
    */
-  if (PREDICT_FALSE (tc->rcv_dupacks != 0 || tc->sack_sb.sacked_bytes))
+  if (PREDICT_FALSE (tc->rcv_dupacks || tc->sack_sb.sacked_bytes))
     {
-      if (tc->limited_transmit != tc->snd_nxt
-         && (seq_lt (tc->limited_transmit, tc->snd_nxt - 2 * tc->snd_mss)
-             || seq_gt (tc->limited_transmit, tc->snd_nxt)))
+      int snt_limited, n_pkts;
+
+      n_pkts = tcp_opts_sack_permitted (&tc->rcv_opts)
+       ? tc->sack_sb.reorder - 1 : 2;
+
+      if ((seq_lt (tc->limited_transmit, tc->snd_nxt - n_pkts * tc->snd_mss)
+          || seq_gt (tc->limited_transmit, tc->snd_nxt)))
        tc->limited_transmit = tc->snd_nxt;
 
       ASSERT (seq_leq (tc->limited_transmit, tc->snd_nxt));
 
-      int snt_limited = tc->snd_nxt - tc->limited_transmit;
-      snd_space = clib_max ((int) 2 * tc->snd_mss - snt_limited, 0);
+      snt_limited = tc->snd_nxt - tc->limited_transmit;
+      snd_space = clib_max (n_pkts * tc->snd_mss - snt_limited, 0);
     }
   return tcp_round_snd_space (tc, snd_space);
 }
@@ -1051,7 +1068,6 @@ tcp_timer_waitclose_handler (tcp_connection_t * tc)
 static timer_expiration_handler *timer_expiration_handlers[TCP_N_TIMERS] =
 {
     tcp_timer_retransmit_handler,
-    tcp_timer_delack_handler,
     tcp_timer_persist_handler,
     tcp_timer_waitclose_handler,
     tcp_timer_retransmit_syn_handler,
@@ -1083,6 +1099,13 @@ tcp_dispatch_pending_timers (tcp_worker_ctx_t * wrk)
       if (PREDICT_FALSE (!tc))
        continue;
 
+      /* Skip if the timer is not pending. Probably it was reset while
+       * wating for dispatch */
+      if (PREDICT_FALSE (!(tc->pending_timers & (1 << timer_id))))
+       continue;
+
+      tc->pending_timers &= ~(1 << timer_id);
+
       /* Skip timer if it was rearmed while pending dispatch */
       if (PREDICT_FALSE (tc->timers[timer_id] != TCP_TIMER_HANDLE_INVALID))
        continue;
@@ -1148,6 +1171,7 @@ const static transport_proto_vft_t tcp_proto = {
   .connect = tcp_session_open,
   .close = tcp_session_close,
   .cleanup = tcp_session_cleanup,
+  .cleanup_ho = tcp_session_cleanup_ho,
   .reset = tcp_session_reset,
   .send_params = tcp_session_send_params,
   .update_time = tcp_update_time,
@@ -1227,6 +1251,7 @@ tcp_expired_timers_dispatch (u32 * expired_timers)
       TCP_EVT (TCP_EVT_TIMER_POP, connection_index, timer_id);
 
       tc->timers[timer_id] = TCP_TIMER_HANDLE_INVALID;
+      tc->pending_timers |= (1 << timer_id);
     }
 
   clib_fifo_add (wrk->pending_timers, expired_timers, n_expired);
@@ -1244,13 +1269,14 @@ tcp_expired_timers_dispatch (u32 * expired_timers)
 static void
 tcp_initialize_timer_wheels (tcp_main_t * tm)
 {
+  vlib_main_t *vm = vlib_get_main ();
   tw_timer_wheel_16t_2w_512sl_t *tw;
   /* *INDENT-OFF* */
   foreach_vlib_main (({
     tw = &tm->wrk_ctx[ii].timer_wheel;
     tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
                                       TCP_TIMER_TICK, ~0);
-    tw->last_run_time = vlib_time_now (this_vlib_main);
+    tw->last_run_time = vlib_time_now (vm);
   }));
   /* *INDENT-ON* */
 }
@@ -1339,11 +1365,6 @@ tcp_main_enable (vlib_main_t * vm)
     pool_init_fixed (tm->half_open_connections,
                     tcp_cfg.preallocated_half_open_connections);
 
-  /* Initialize clocks per tick for TCP timestamp. Used to compute
-   * monotonically increasing timestamps. */
-  tm->tstamp_ticks_per_clock = vm->clib_time.seconds_per_clock
-    / TCP_TSTAMP_RESOLUTION;
-
   if (num_threads > 1)
     {
       clib_spinlock_init (&tm->half_open_lock);
@@ -1407,7 +1428,7 @@ tcp_configuration_init (void)
   tcp_cfg.enable_tx_pacing = 1;
   tcp_cfg.allow_tso = 0;
   tcp_cfg.csum_offload = 1;
-  tcp_cfg.cc_algo = TCP_CC_NEWRENO;
+  tcp_cfg.cc_algo = TCP_CC_CUBIC;
   tcp_cfg.rwnd_min_update_ack = 1;
 
   /* Time constants defined as timer tick (100ms) multiples */