tcp: cleanup connection/session fixes
[vpp.git] / src / vnet / tcp / tcp_input.c
index 42db82e..f77d484 100644 (file)
@@ -275,6 +275,14 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
                      vlib_buffer_t * b0, tcp_header_t * th0,
                      u32 * next0, u32 * error0)
 {
+  /* We could get a burst of RSTs interleaved with acks */
+  if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
+    {
+      tcp_send_reset (tc0);
+      *error0 = TCP_ERROR_CONNECTION_CLOSED;
+      goto drop;
+    }
+
   if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
     {
       *error0 = TCP_ERROR_SEGMENT_INVALID;
@@ -292,13 +300,7 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
     {
       *error0 = TCP_ERROR_PAWS;
       if (CLIB_DEBUG > 2)
-       {
-         clib_warning ("paws failed\n%U", format_tcp_connection, tc0, 2);
-         clib_warning ("seq %u seq_end %u ack %u",
-                       vnet_buffer (b0)->tcp.seq_number - tc0->irs,
-                       vnet_buffer (b0)->tcp.seq_end - tc0->irs,
-                       vnet_buffer (b0)->tcp.ack_number - tc0->iss);
-       }
+       clib_warning ("paws failed\n%U", format_tcp_connection, tc0, 2);
       TCP_EVT_DBG (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number,
                   vnet_buffer (b0)->tcp.seq_end);
 
@@ -317,7 +319,7 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
          if (!tcp_rst (th0))
            {
              tcp_make_ack (tc0, b0);
-             TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
+             TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0, vnet_buffer (b0)->tcp);
              goto error;
            }
        }
@@ -329,7 +331,6 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
                               vnet_buffer (b0)->tcp.seq_end))
     {
       *error0 = TCP_ERROR_RCV_WND;
-
       /* If our window is 0 and the packet is in sequence, let it pass
        * through for ack processing. It should be dropped later. */
       if (!(tc0->rcv_wnd == 0
@@ -339,7 +340,7 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
          if (!tcp_rst (th0))
            {
              tcp_make_ack (tc0, b0);
-             TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
+             TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0, vnet_buffer (b0)->tcp);
              goto error;
            }
          goto drop;
@@ -889,13 +890,14 @@ tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
   scoreboard_update_bytes (tc, sb);
   sb->last_sacked_bytes = sb->sacked_bytes
     - (old_sacked_bytes - sb->last_bytes_delivered);
-  ASSERT (sb->last_sacked_bytes <= sb->sacked_bytes);
+  ASSERT (sb->last_sacked_bytes <= sb->sacked_bytes || tcp_in_recovery (tc));
   ASSERT (sb->sacked_bytes == 0
          || sb->sacked_bytes < tc->snd_una_max - seq_max (tc->snd_una, ack));
   ASSERT (sb->last_sacked_bytes + sb->lost_bytes <= tc->snd_una_max
          - seq_max (tc->snd_una, ack));
   ASSERT (sb->head == TCP_INVALID_SACK_HOLE_INDEX || tcp_in_recovery (tc)
          || sb->holes[sb->head].start == ack + sb->snd_una_adv);
+  TCP_EVT_DBG (TCP_EVT_CC_SCOREBOARD, tc);
 }
 
 /**
@@ -936,6 +938,12 @@ tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
     }
 }
 
+/**
+ * Init loss recovery/fast recovery.
+ *
+ * Triggered by dup acks as opposed to timer timeout. Note that cwnd is
+ * updated in @ref tcp_cc_handle_event after fast retransmit
+ */
 void
 tcp_cc_init_congestion (tcp_connection_t * tc)
 {
@@ -949,7 +957,6 @@ tcp_cc_init_congestion (tcp_connection_t * tc)
 static void
 tcp_cc_recovery_exit (tcp_connection_t * tc)
 {
-  /* Deflate rto */
   tc->rto_boff = 0;
   tcp_update_rto (tc);
   tc->snd_rxt_ts = 0;
@@ -1057,32 +1064,35 @@ tcp_cc_handle_event (tcp_connection_t * tc, u32 is_dack)
 {
   u32 rxt_delivered;
 
+  if (tcp_in_fastrecovery (tc) && tcp_opts_sack_permitted (&tc->rcv_opts))
+    {
+      if (tc->bytes_acked)
+       goto partial_ack;
+      tcp_fast_retransmit (tc);
+      return;
+    }
   /*
    * Duplicate ACK. Check if we should enter fast recovery, or if already in
    * it account for the bytes that left the network.
    */
-  if (is_dack)
+  else if (is_dack && !tcp_in_recovery (tc))
     {
+      TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
       ASSERT (tc->snd_una != tc->snd_una_max
              || tc->sack_sb.last_sacked_bytes);
 
       tc->rcv_dupacks++;
 
+      /* Pure duplicate ack. If some data got acked, it's handled lower */
       if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD && !tc->bytes_acked)
        {
          ASSERT (tcp_in_fastrecovery (tc));
-         /* Pure duplicate ack. If some data got acked, it's handled lower */
          tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
          return;
        }
       else if (tcp_should_fastrecover (tc))
        {
-         /* Things are already bad */
-         if (tcp_in_cong_recovery (tc))
-           {
-             tc->rcv_dupacks = 0;
-             goto partial_ack_test;
-           }
+         ASSERT (!tcp_in_fastrecovery (tc));
 
          /* If of of the two conditions lower hold, reset dupacks because
           * we're probably after timeout (RFC6582 heuristics).
@@ -1126,7 +1136,6 @@ tcp_cc_handle_event (tcp_connection_t * tc, u32 is_dack)
            {
              tcp_fast_retransmit_no_sack (tc);
            }
-
          return;
        }
       else if (!tc->bytes_acked
@@ -1139,12 +1148,12 @@ tcp_cc_handle_event (tcp_connection_t * tc, u32 is_dack)
        goto partial_ack;
     }
 
-partial_ack_test:
-
   if (!tc->bytes_acked)
     return;
 
 partial_ack:
+  TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
+
   /*
    * Legitimate ACK. 1) See if we can exit recovery
    */
@@ -1171,17 +1180,18 @@ partial_ack:
   /*
    * Legitimate ACK. 2) If PARTIAL ACK try to retransmit
    */
-  TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
 
   /* RFC6675: If the incoming ACK is a cumulative acknowledgment,
-   * reset dupacks to 0 */
+   * reset dupacks to 0. Also needed if in congestion recovery */
   tc->rcv_dupacks = 0;
 
-  tcp_retransmit_first_unacked (tc);
-
   /* Post RTO timeout don't try anything fancy */
   if (tcp_in_recovery (tc))
-    return;
+    {
+      tc->cc_algo->rcv_ack (tc);
+      tc->tsecr_last_ack = tc->rcv_opts.tsecr;
+      return;
+    }
 
   /* Remove retransmitted bytes that have been delivered */
   ASSERT (tc->bytes_acked + tc->sack_sb.snd_una_adv
@@ -1234,6 +1244,16 @@ tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
   /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
   if (PREDICT_FALSE (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)))
     {
+      /* When we entered recovery, we reset snd_nxt to snd_una. Seems peer
+       * still has the data so accept the ack */
+      if (tcp_in_recovery (tc)
+         && seq_leq (vnet_buffer (b)->tcp.ack_number, tc->snd_congestion)
+         && seq_geq (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
+       {
+         tc->snd_una_max = tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
+         goto process_ack;
+       }
+
       /* If we have outstanding data and this is within the window, accept it,
        * probably retransmit has timed out. Otherwise ACK segment and then
        * drop it */
@@ -1261,10 +1281,7 @@ tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
       TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
                   vnet_buffer (b)->tcp.ack_number);
       if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
-       {
-         TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
-         tcp_cc_handle_event (tc, 1);
-       }
+       tcp_cc_handle_event (tc, 1);
       /* Don't drop yet */
       return 0;
     }
@@ -1272,7 +1289,7 @@ tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
   /*
    * Looks okay, process feedback
    */
-
+process_ack:
   if (tcp_opts_sack_permitted (&tc->rcv_opts))
     tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
 
@@ -1300,7 +1317,6 @@ tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
       if (!tcp_in_cong_recovery (tc))
        return 0;
       *error = TCP_ERROR_ACK_DUP;
-      TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
       return vnet_buffer (b)->tcp.data_len ? 0 : -1;
     }
 
@@ -1389,6 +1405,15 @@ tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
   ASSERT (tcp_sack_vector_is_sane (tc->snd_sacks));
 }
 
+u32
+tcp_sack_list_bytes (tcp_connection_t * tc)
+{
+  u32 bytes = 0, i;
+  for (i = 0; i < vec_len (tc->snd_sacks); i++)
+    bytes += tc->snd_sacks[i].end - tc->snd_sacks[i].start;
+  return bytes;
+}
+
 /** Enqueue data for delivery to application */
 always_inline int
 tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
@@ -1415,6 +1440,7 @@ tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
 
       /* Send ACK confirming the update */
       tc->flags |= TCP_CONN_SNDACK;
+      TCP_EVT_DBG (TCP_EVT_CC_INPUT, tc, data_len, written);
     }
   else if (written > 0)
     {
@@ -1487,6 +1513,7 @@ tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
          end = start + ooo_segment_length (s0->server_rx_fifo, newest);
          tcp_update_sack_list (tc, start, end);
          svm_fifo_newest_ooo_segment_reset (s0->server_rx_fifo);
+         TCP_EVT_DBG (TCP_EVT_CC_SACKS, tc);
        }
     }
 
@@ -1507,7 +1534,7 @@ tcp_can_delack (tcp_connection_t * tc)
       /* constrained to send ack */
       || (tc->flags & TCP_CONN_SNDACK) != 0
       /* we're almost out of tx wnd */
-      || tcp_available_snd_space (tc) < 4 * tc->snd_mss)
+      || tcp_available_cc_snd_space (tc) < 4 * tc->snd_mss)
     return 0;
 
   return 1;
@@ -1591,7 +1618,7 @@ tcp_segment_rcv (tcp_connection_t * tc, vlib_buffer_t * b, u32 * next0)
       *next0 = tcp_next_output (tc->c_is_ip4);
       tcp_make_ack (tc, b);
       vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
-      TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc);
+      TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc, vnet_buffer (b)->tcp);
       goto done;
     }
 
@@ -1772,9 +1799,7 @@ tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                                                   &error0)))
            {
              tcp_maybe_inc_err_counter (err_counters, error0);
-             TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0,
-                          vnet_buffer (b0)->tcp.seq_number,
-                          vnet_buffer (b0)->tcp.seq_end);
+             TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0, vnet_buffer (b0)->tcp);
              goto done;
            }
 
@@ -2449,7 +2474,7 @@ tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
              if (tc0->flags & TCP_CONN_FINPNDG)
                {
                  /* TX fifo finally drained */
-                 if (!stream_session_tx_fifo_max_dequeue (&tc0->connection))
+                 if (!session_tx_fifo_max_dequeue (&tc0->connection))
                    tcp_send_fin (tc0);
                }
              /* If FIN is ACKed */
@@ -2482,6 +2507,18 @@ tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                  tcp_maybe_inc_counter (rcv_process, error0, 1);
                  goto drop;
                }
+             if (tc0->flags & TCP_CONN_FINPNDG)
+               {
+                 /* TX fifo finally drained */
+                 if (!session_tx_fifo_max_dequeue (&tc0->connection))
+                   {
+                     tcp_send_fin (tc0);
+                     tcp_connection_timers_reset (tc0);
+                     tc0->state = TCP_STATE_LAST_ACK;
+                     tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE,
+                                       TCP_2MSL_TIME);
+                   }
+               }
              break;
            case TCP_STATE_CLOSING:
              /* In addition to the processing for the ESTABLISHED state, if
@@ -2520,13 +2557,9 @@ tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
 
              tc0->state = TCP_STATE_CLOSED;
              TCP_EVT_DBG (TCP_EVT_STATE_CHANGE, tc0);
-             tcp_connection_timers_reset (tc0);
-
-             /* Don't delete the connection/session yet. Instead, wait a
-              * reasonable amount of time until the pipes are cleared. In
-              * particular, this makes sure that we won't have dead sessions
-              * when processing events on the tx path */
-             tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
+             /* Delete the connection/session since the pipes should be
+              * clear by now */
+             tcp_connection_del (tc0);
 
              goto drop;
 
@@ -3247,6 +3280,7 @@ do {                                                              \
   /* FIN in reply to our FIN from the other side */
   _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   _(FIN_WAIT_1, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
+  _(CLOSING, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   /* FIN confirming that the peer (app) has closed */
   _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   _(FIN_WAIT_2, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);