tcp: count first lost hole (VPP-1465)
[vpp.git] / src / vnet / tcp / tcp_input.c
index e6c12f0..87bacc2 100644 (file)
@@ -135,7 +135,8 @@ tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
   data = (const u8 *) (th + 1);
 
   /* Zero out all flags but those set in SYN */
-  to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
+  to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE
+               | TCP_OPTS_FLAG_SACK);
 
   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
     {
@@ -666,39 +667,45 @@ scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index,
 static void
 scoreboard_update_bytes (tcp_connection_t * tc, sack_scoreboard_t * sb)
 {
-  sack_scoreboard_hole_t *hole, *prev;
+  sack_scoreboard_hole_t *left, *right;
   u32 bytes = 0, blks = 0;
 
   sb->lost_bytes = 0;
   sb->sacked_bytes = 0;
-  hole = scoreboard_last_hole (sb);
-  if (!hole)
+  left = scoreboard_last_hole (sb);
+  if (!left)
     return;
 
-  if (seq_gt (sb->high_sacked, hole->end))
+  if (seq_gt (sb->high_sacked, left->end))
     {
-      bytes = sb->high_sacked - hole->end;
+      bytes = sb->high_sacked - left->end;
       blks = 1;
     }
 
-  while ((prev = scoreboard_prev_hole (sb, hole))
-        && (bytes < (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss
-            && blks < TCP_DUPACK_THRESHOLD))
+  while ((right = left)
+        && bytes < (TCP_DUPACK_THRESHOLD - 1) * tc->snd_mss
+        && blks < TCP_DUPACK_THRESHOLD
+        /* left not updated if above conditions fail */
+        && (left = scoreboard_prev_hole (sb, right)))
     {
-      bytes += hole->start - prev->end;
+      bytes += right->start - left->end;
       blks++;
-      hole = prev;
     }
 
-  while (hole)
+  /* left is first lost */
+  if (left)
     {
-      sb->lost_bytes += scoreboard_hole_bytes (hole);
-      hole->is_lost = 1;
-      prev = hole;
-      hole = scoreboard_prev_hole (sb, hole);
-      if (hole)
-       bytes += prev->start - hole->end;
+      do
+       {
+         sb->lost_bytes += scoreboard_hole_bytes (right);
+         left->is_lost = 1;
+         left = scoreboard_prev_hole (sb, right);
+         if (left)
+           bytes += right->start - left->end;
+       }
+      while ((right = left));
     }
+
   sb->sacked_bytes = bytes;
 }
 
@@ -814,7 +821,8 @@ tcp_scoreboard_is_sane_post_recovery (tcp_connection_t * tc)
 {
   sack_scoreboard_hole_t *hole;
   hole = scoreboard_first_hole (&tc->sack_sb);
-  return (!hole || seq_geq (hole->start, tc->snd_una));
+  return (!hole || (seq_geq (hole->start, tc->snd_una)
+                   && seq_lt (hole->end, tc->snd_una_max)));
 }
 
 void
@@ -973,6 +981,14 @@ tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
        }
     }
 
+  if (pool_elts (sb->holes) == 1)
+    {
+      hole = scoreboard_first_hole (sb);
+      if (hole->start == ack + sb->snd_una_adv
+         && hole->end == tc->snd_una_max)
+       scoreboard_remove_hole (sb, hole);
+    }
+
   scoreboard_update_bytes (tc, sb);
   sb->last_sacked_bytes = sb->sacked_bytes
     - (old_sacked_bytes - sb->last_bytes_delivered);
@@ -1401,7 +1417,9 @@ process_ack:
       if (!tcp_in_cong_recovery (tc))
        return 0;
       *error = TCP_ERROR_ACK_DUP;
-      return vnet_buffer (b)->tcp.data_len ? 0 : -1;
+      if (vnet_buffer (b)->tcp.data_len || tcp_is_fin (th))
+       return 0;
+      return -1;
     }
 
   /*
@@ -2282,8 +2300,8 @@ tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          if (tcp_opts_wscale (&new_tc0->rcv_opts))
            new_tc0->snd_wscale = new_tc0->rcv_opts.wscale;
 
-         /* RFC1323: SYN and SYN-ACK wnd not scaled */
-         new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
+         new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
+           << new_tc0->snd_wscale;
          new_tc0->snd_wl1 = seq0;
          new_tc0->snd_wl2 = ack0;
 
@@ -2672,9 +2690,12 @@ 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);
-             /* Delete the connection/session since the pipes should be
-              * clear by now */
-             tcp_connection_del (tc0);
+
+             /* Don't free the connection from the data path since
+              * we can't ensure that we have no packets already enqueued
+              * to output. Rely instead on the waitclose timer */
+             tcp_connection_timers_reset (tc0);
+             tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, 1);
 
              goto drop;
 
@@ -3441,6 +3462,8 @@ do {                                                              \
   /* 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_RCVD, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
+    TCP_ERROR_NONE);
   _(SYN_RCVD, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
     TCP_ERROR_NONE);
@@ -3484,6 +3507,7 @@ do {                                                              \
   _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
     TCP_ERROR_NONE);
   _(LAST_ACK, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
+  _(LAST_ACK, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   _(TIME_WAIT, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
   _(TIME_WAIT, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
     TCP_ERROR_NONE);