Add support for tcp/session buffer chains
[vpp.git] / src / vnet / tcp / tcp_input.c
index 3c65a5e..ceb00fc 100644 (file)
@@ -208,6 +208,15 @@ tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
     }
 }
 
+/**
+ * RFC1323: Check against wrapped sequence numbers (PAWS). If we have
+ * timestamp to echo and it's less than tsval_recent, drop segment
+ * but still send an ACK in order to retain TCP's mechanism for detecting
+ * and recovering from half-open connections
+ *
+ * Or at least that's what the theory says. It seems that this might not work
+ * very well with packet reordering and fast retransmit. XXX
+ */
 always_inline int
 tcp_segment_check_paws (tcp_connection_t * tc)
 {
@@ -215,6 +224,27 @@ tcp_segment_check_paws (tcp_connection_t * tc)
     && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
 }
 
+/**
+ * Update tsval recent
+ */
+always_inline void
+tcp_update_timestamp (tcp_connection_t * tc, u32 seq, u32 seq_end)
+{
+  /*
+   * RFC1323: If Last.ACK.sent falls within the range of sequence numbers
+   * of an incoming segment:
+   *    SEG.SEQ <= Last.ACK.sent < SEG.SEQ + SEG.LEN
+   * then the TSval from the segment is copied to TS.Recent;
+   * otherwise, the TSval is ignored.
+   */
+  if (tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
+      && seq_leq (seq, tc->rcv_las) && seq_leq (tc->rcv_las, seq_end))
+    {
+      tc->tsval_recent = tc->opt.tsval;
+      tc->tsval_recent_age = tcp_time_now ();
+    }
+}
+
 /**
  * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
  *
@@ -228,21 +258,16 @@ static int
 tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
                      vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
 {
-  u8 paws_failed;
-
   if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
     return -1;
 
   tcp_options_parse (th0, &tc0->opt);
 
-  /* RFC1323: Check against wrapped sequence numbers (PAWS). If we have
-   * timestamp to echo and it's less than tsval_recent, drop segment
-   * but still send an ACK in order to retain TCP's mechanism for detecting
-   * and recovering from half-open connections */
-  paws_failed = tcp_segment_check_paws (tc0);
-  if (paws_failed)
+  if (tcp_segment_check_paws (tc0))
     {
       clib_warning ("paws failed");
+      TCP_EVT_DBG (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number,
+                  vnet_buffer (b0)->tcp.seq_end);
 
       /* If it just so happens that a segment updates tsval_recent for a
        * segment over 24 days old, invalidate tsval_recent. */
@@ -251,6 +276,7 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
        {
          /* Age isn't reset until we get a valid tsval (bsd inspired) */
          tc0->tsval_recent = 0;
+         clib_warning ("paws failed - really old segment. REALLY?");
        }
       else
        {
@@ -305,12 +331,9 @@ tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
       return -1;
     }
 
-  /* If PAWS passed and segment in window, save timestamp */
-  if (!paws_failed)
-    {
-      tc0->tsval_recent = tc0->opt.tsval;
-      tc0->tsval_recent_age = tcp_time_now ();
-    }
+  /* If segment in window, save timestamp */
+  tcp_update_timestamp (tc0, vnet_buffer (b0)->tcp.seq_number,
+                       vnet_buffer (b0)->tcp.seq_end);
 
   return 0;
 }
@@ -366,14 +389,17 @@ static int
 tcp_update_rtt (tcp_connection_t * tc, u32 ack)
 {
   u32 mrtt = 0;
+  u8 rtx_acked;
+
+  /* Determine if only rtx bytes are acked. TODO fast retransmit */
+  rtx_acked = tc->rto_boff && (tc->bytes_acked <= tc->snd_mss);
 
   /* Karn's rule, part 1. Don't use retransmitted segments to estimate
    * RTT because they're ambiguous. */
-  if (tc->rtt_seq && seq_gt (ack, tc->rtt_seq) && !tc->rto_boff)
+  if (tc->rtt_ts && seq_geq (ack, tc->rtt_seq) && !rtx_acked)
     {
       mrtt = tcp_time_now () - tc->rtt_ts;
     }
-
   /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
    * snd_una, i.e., the left side of the send window:
    * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
@@ -383,19 +409,22 @@ tcp_update_rtt (tcp_connection_t * tc, u32 ack)
       mrtt = tcp_time_now () - tc->opt.tsecr;
     }
 
+  /* Allow measuring of a new RTT */
+  tc->rtt_ts = 0;
+
+  /* If ACK moves left side of the wnd make sure boff is 0, even if mrtt is
+   * not valid */
+  if (tc->bytes_acked)
+    tc->rto_boff = 0;
+
   /* Ignore dubious measurements */
   if (mrtt == 0 || mrtt > TCP_RTT_MAX)
     return 0;
 
   tcp_estimate_rtt (tc, mrtt);
-
   tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
 
-  /* Allow measuring of RTT and make sure boff is 0 */
-  tc->rtt_seq = 0;
-  tc->rto_boff = 0;
-
-  return 1;
+  return 0;
 }
 
 /**
@@ -712,7 +741,7 @@ tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
 {
   u8 partial_ack;
 
-  if (tcp_in_cong_recovery (tc))
+  if (tcp_in_fastrecovery (tc))
     {
       partial_ack = seq_lt (tc->snd_una, tc->snd_congestion);
       if (!partial_ack)
@@ -726,6 +755,7 @@ tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
 
          /* Clear retransmitted bytes. XXX should we clear all? */
          tc->rtx_bytes = 0;
+
          tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
 
          /* In case snd_nxt is still in the past and output tries to
@@ -749,6 +779,13 @@ tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
       tc->cc_algo->rcv_ack (tc);
       tc->tsecr_last_ack = tc->opt.tsecr;
       tc->rcv_dupacks = 0;
+      if (tcp_in_recovery (tc))
+       {
+         tc->rtx_bytes -= clib_min (tc->bytes_acked, tc->rtx_bytes);
+         tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
+         if (seq_geq (tc->snd_una, tc->snd_congestion))
+           tcp_recovery_off (tc);
+       }
     }
 }
 
@@ -835,7 +872,8 @@ tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
          TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
          tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
        }
-      return -1;
+      /* Don't drop yet */
+      return 0;
     }
 
   if (tcp_opts_sack_permitted (&tc->opt))
@@ -873,7 +911,7 @@ tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
       tcp_cc_rcv_ack (tc, b);
 
       /* If everything has been acked, stop retransmit timer
-       * otherwise update */
+       * otherwise update. */
       if (tc->snd_una == tc->snd_una_max)
        tcp_retransmit_timer_reset (tc);
       else
@@ -932,10 +970,6 @@ tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
        {
          vec_add1 (new_list, tc->snd_sacks[i]);
        }
-      else
-       {
-         clib_warning ("dropped sack blocks");
-       }
     }
 
   ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
@@ -959,9 +993,8 @@ tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
       return TCP_ERROR_PURE_ACK;
     }
 
-  written = stream_session_enqueue_data (&tc->connection,
-                                        vlib_buffer_get_current (b),
-                                        data_len, 1 /* queue event */ );
+  written = stream_session_enqueue_data (&tc->connection, b, 0,
+                                        1 /* queue event */ , 1);
 
   TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
 
@@ -1011,7 +1044,6 @@ tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
                         u16 data_len)
 {
   stream_session_t *s0;
-  u32 offset;
   int rv;
 
   /* Pure ACK. Do nothing */
@@ -1020,13 +1052,10 @@ tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
       return TCP_ERROR_PURE_ACK;
     }
 
-  s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
-  offset = vnet_buffer (b)->tcp.seq_number - tc->irs;
-
-  clib_warning ("ooo: offset %d len %d", offset, data_len);
-
-  rv = svm_fifo_enqueue_with_offset (s0->server_rx_fifo, offset, data_len,
-                                    vlib_buffer_get_current (b));
+  /* Enqueue out-of-order data with absolute offset */
+  rv = stream_session_enqueue_data (&tc->connection, b,
+                                   vnet_buffer (b)->tcp.seq_number,
+                                   0 /* queue event */ , 0);
 
   /* Nothing written */
   if (rv)
@@ -1043,6 +1072,8 @@ tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
       ooo_segment_t *newest;
       u32 start, end;
 
+      s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
+
       /* Get the newest segment from the fifo */
       newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
       start = ooo_segment_offset (s0->server_rx_fifo, newest);
@@ -1542,6 +1573,9 @@ tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
              /* Notify app that we have connection */
              stream_session_connect_notify (&new_tc0->connection, sst, 0);
 
+             stream_session_init_fifos_pointers (&new_tc0->connection,
+                                                 new_tc0->irs + 1,
+                                                 new_tc0->iss + 1);
              /* Make sure after data segment processing ACK is sent */
              new_tc0->flags |= TCP_CONN_SNDACK;
            }
@@ -1552,7 +1586,9 @@ tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
 
              /* Notify app that we have connection */
              stream_session_connect_notify (&new_tc0->connection, sst, 0);
-
+             stream_session_init_fifos_pointers (&new_tc0->connection,
+                                                 new_tc0->irs + 1,
+                                                 new_tc0->iss + 1);
              tcp_make_synack (new_tc0, b0);
              next0 = tcp_next_output (is_ip4);
 
@@ -1755,6 +1791,11 @@ tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                  tcp_send_reset (b0, is_ip4);
                  goto drop;
                }
+
+             /* Update rtt and rto */
+             tc0->bytes_acked = 1;
+             tcp_update_rtt (tc0, vnet_buffer (b0)->tcp.ack_number);
+
              /* Switch state to ESTABLISHED */
              tc0->state = TCP_STATE_ESTABLISHED;
 
@@ -2139,6 +2180,10 @@ tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          tcp_make_synack (child0, b0);
          next0 = tcp_next_output (is_ip4);
 
+         /* Init fifo pointers after we have iss */
+         stream_session_init_fifos_pointers (&child0->connection,
+                                             child0->irs + 1,
+                                             child0->iss + 1);
        drop:
          if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
            {
@@ -2474,6 +2519,7 @@ do {                                                              \
   _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
   /* ACK for for a SYN-ACK -> tcp-rcv-process. */
   _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
+  _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   /* SYN-ACK for a SYN */
   _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
     TCP_ERROR_NONE);
@@ -2496,9 +2542,11 @@ do {                                                             \
   _(FIN_WAIT_1, TCP_FLAG_FIN, 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);
   _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
     TCP_ERROR_NONE);
   _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
+  _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
 #undef _
 }