Improve session debugging
[vpp.git] / src / vnet / tcp / tcp_input.c
index 318d8ec..35bc909 100644 (file)
@@ -533,12 +533,13 @@ tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
   sack_scoreboard_t *sb = &tc->sack_sb;
   sack_block_t *blk, tmp;
   sack_scoreboard_hole_t *hole, *next_hole, *last_hole, *new_hole;
-  u32 blk_index = 0, old_sacked_bytes, hole_index;
+  u32 blk_index = 0, old_sacked_bytes, delivered_bytes, hole_index;
   int i, j;
 
   sb->last_sacked_bytes = 0;
   sb->snd_una_adv = 0;
   old_sacked_bytes = sb->sacked_bytes;
+  delivered_bytes = 0;
 
   if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
     return;
@@ -584,6 +585,8 @@ tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
       last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
                                          tc->snd_una, tc->snd_una_max);
       sb->tail = scoreboard_hole_index (sb, last_hole);
+      tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
+      sb->max_byte_sacked = tmp.end;
     }
   else
     {
@@ -614,37 +617,43 @@ tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
                {
                  /* Bytes lost because snd_wnd left edge advances */
                  if (next_hole && seq_leq (next_hole->start, ack))
-                   sb->sacked_bytes -= next_hole->start - hole->end;
+                   delivered_bytes += next_hole->start - hole->end;
                  else
-                   sb->sacked_bytes -= ack - hole->end;
+                   delivered_bytes += ack - hole->end;
                }
              else
                {
                  sb->sacked_bytes += scoreboard_hole_bytes (hole);
                }
 
-             /* snd_una needs to be advanced */
-             if (seq_geq (ack, hole->end))
-               {
-                 if (next_hole && seq_lt (ack, next_hole->start))
-                   sb->snd_una_adv = next_hole->start - ack;
-                 else
-                   sb->snd_una_adv = sb->max_byte_sacked - ack;
-
-                 /* all these can be delivered */
-                 sb->sacked_bytes -= sb->snd_una_adv;
-               }
-
              /* About to remove last hole */
              if (hole == last_hole)
                {
                  sb->tail = hole->prev;
                  last_hole = scoreboard_last_hole (sb);
-                 /* keep track of max byte sacked in case the last hole
+                 /* keep track of max byte sacked for when the last hole
                   * is acked */
                  if (seq_gt (hole->end, sb->max_byte_sacked))
                    sb->max_byte_sacked = hole->end;
                }
+
+             /* snd_una needs to be advanced */
+             if (blk->end == ack && seq_geq (ack, hole->end))
+               {
+                 if (next_hole && seq_lt (ack, next_hole->start))
+                   {
+                     sb->snd_una_adv = next_hole->start - ack;
+
+                     /* all these can be delivered */
+                     delivered_bytes += sb->snd_una_adv;
+                   }
+                 else if (!next_hole)
+                   {
+                     sb->snd_una_adv = sb->max_byte_sacked - ack;
+                     delivered_bytes += sb->snd_una_adv;
+                   }
+               }
+
              scoreboard_remove_hole (sb, hole);
              hole = next_hole;
            }
@@ -693,8 +702,8 @@ tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
        }
     }
 
-  sb->last_sacked_bytes = sb->sacked_bytes + sb->snd_una_adv
-    - old_sacked_bytes;
+  sb->last_sacked_bytes = sb->sacked_bytes - old_sacked_bytes;
+  sb->sacked_bytes -= delivered_bytes;
 }
 
 /** Update snd_wnd
@@ -713,9 +722,11 @@ tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
       TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
 
       /* Set probe timer if we just got 0 wnd */
-      if (tc->snd_wnd < tc->snd_mss
-         && !tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
-       tcp_persist_timer_set (tc);
+      if (tc->snd_wnd < tc->snd_mss)
+       {
+         if (!tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
+           tcp_persist_timer_set (tc);
+       }
       else
        tcp_persist_timer_reset (tc);
     }
@@ -754,6 +765,7 @@ static void
 tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
 {
   u8 partial_ack;
+  u32 bytes_advanced;
 
   if (tcp_in_fastrecovery (tc))
     {
@@ -795,10 +807,14 @@ tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
       tc->rcv_dupacks = 0;
       if (tcp_in_recovery (tc))
        {
-         tc->rtx_bytes -= clib_min (tc->bytes_acked, tc->rtx_bytes);
+         bytes_advanced = tc->bytes_acked + tc->sack_sb.snd_una_adv;
+         tc->rtx_bytes -= clib_min (bytes_advanced, 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);
+           {
+             tc->rtx_bytes = 0;
+             tcp_recovery_off (tc);
+           }
        }
     }
 }
@@ -1135,6 +1151,10 @@ tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
          error = TCP_ERROR_SEGMENT_OLD;
          *next0 = TCP_NEXT_DROP;
 
+         /* Completely in the past (possible retransmit) */
+         if (seq_lt (vnet_buffer (b)->tcp.seq_end, tc->rcv_nxt))
+           goto done;
+
          /* Chop off the bytes in the past */
          n_bytes_to_drop = tc->rcv_nxt - vnet_buffer (b)->tcp.seq_number;
          n_data_bytes -= n_bytes_to_drop;
@@ -1208,7 +1228,7 @@ format_tcp_rx_trace (u8 * s, va_list * args)
   s = format (s, "%U\n%U%U",
              format_tcp_header, &t->tcp_header, 128,
              format_white_space, indent,
-             format_tcp_connection_verbose, &t->tcp_connection);
+             format_tcp_connection, &t->tcp_connection, 1);
 
   return s;
 }
@@ -1223,7 +1243,7 @@ format_tcp_rx_trace_short (u8 * s, va_list * args)
   s = format (s, "%d -> %d (%U)",
              clib_net_to_host_u16 (t->tcp_header.src_port),
              clib_net_to_host_u16 (t->tcp_header.dst_port), format_tcp_state,
-             &t->tcp_connection.state);
+             t->tcp_connection.state);
 
   return s;
 }
@@ -1553,6 +1573,7 @@ tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
 
          new_tc0->c_thread_index = my_thread_index;
+         new_tc0->c_c_index = new_tc0 - tm->connections[my_thread_index];
 
          /* Cleanup half-open connection XXX lock */
          pool_put (tm->half_open_connections, tc0);
@@ -2151,6 +2172,7 @@ tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          child0->c_rmt_port = th0->src_port;
          child0->c_is_ip4 = is_ip4;
          child0->c_thread_index = my_thread_index;
+         child0->state = TCP_STATE_SYN_RCVD;
 
          if (is_ip4)
            {
@@ -2180,7 +2202,6 @@ tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          child0->irs = vnet_buffer (b0)->tcp.seq_number;
          child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
          child0->rcv_las = child0->rcv_nxt;
-         child0->state = TCP_STATE_SYN_RCVD;
 
          /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
           * segments are used to initialize PAWS. */
@@ -2436,7 +2457,7 @@ tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
 
                  if (error0 == TCP_ERROR_DISPATCH)
                    clib_warning ("disp error state %U flags %U",
-                                 format_tcp_state, &state0, format_tcp_flags,
+                                 format_tcp_state, state0, format_tcp_flags,
                                  (int) flags0);
                }
            }