tcp: fix retransmit with no sacks
[vpp.git] / src / vnet / tcp / tcp_output.c
index e3228c0..14d1bd6 100644 (file)
@@ -406,7 +406,7 @@ tcp_update_burst_snd_vars (tcp_connection_t * tc)
   tcp_update_rcv_wnd (tc);
 
   if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
-    tc->flags |= TCP_CONN_TRACK_BURST;
+    tcp_bt_check_app_limited (tc);
 
   if (tc->snd_una == tc->snd_nxt)
     {
@@ -842,8 +842,10 @@ tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt,
       int bogus = ~0;
       ASSERT ((pkt_ih6->ip_version_traffic_class_and_flow_label & 0xF0) ==
              0x60);
-      ih6 = vlib_buffer_push_ip6 (vm, b, &pkt_ih6->dst_address,
-                                 &pkt_ih6->src_address, IP_PROTOCOL_TCP);
+      ih6 = vlib_buffer_push_ip6_custom (vm, b, &pkt_ih6->dst_address,
+                                        &pkt_ih6->src_address,
+                                        IP_PROTOCOL_TCP,
+                                        tc->ipv6_flow_label);
       th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
       ASSERT (!bogus);
     }
@@ -909,8 +911,9 @@ tcp_push_ip_hdr (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
       ip6_header_t *ih;
       int bogus = ~0;
 
-      ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6,
-                                &tc->c_rmt_ip6, IP_PROTOCOL_TCP);
+      ih = vlib_buffer_push_ip6_custom (vm, b, &tc->c_lcl_ip6,
+                                       &tc->c_rmt_ip6, IP_PROTOCOL_TCP,
+                                       tc->ipv6_flow_label);
       th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih, &bogus);
       ASSERT (!bogus);
     }
@@ -1124,17 +1127,22 @@ tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b, u32 snd_nxt,
   TCP_EVT (TCP_EVT_PKTIZE, tc);
 }
 
+always_inline u32
+tcp_buffer_len (vlib_buffer_t * b)
+{
+  u32 data_len = b->current_length;
+  if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
+    data_len += b->total_length_not_including_first_buffer;
+  return data_len;
+}
+
 u32
 tcp_session_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
 {
   tcp_connection_t *tc = (tcp_connection_t *) tconn;
 
-  if (tc->flags & TCP_CONN_TRACK_BURST)
-    {
-      tcp_bt_check_app_limited (tc);
-      tcp_bt_track_tx (tc);
-      tc->flags &= ~TCP_CONN_TRACK_BURST;
-    }
+  if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
+    tcp_bt_track_tx (tc, tcp_buffer_len (b));
 
   tcp_push_hdr_i (tc, b, tc->snd_nxt, /* compute opts */ 0, /* burst */ 1,
                  /* update_snd_nxt */ 1);
@@ -1392,6 +1400,8 @@ tcp_prepare_retransmit_segment (tcp_worker_ctx_t * wrk,
   max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
 
   start = tc->snd_una + offset;
+  ASSERT (seq_leq (start + max_deq_bytes, tc->snd_nxt));
+
   n_bytes = tcp_prepare_segment (wrk, tc, offset, max_deq_bytes, b);
   if (!n_bytes)
     return 0;
@@ -1521,7 +1531,8 @@ tcp_timer_retransmit_handler (u32 tc_index)
 
       /* Send the first unacked segment. If we're short on buffers, return
        * as soon as possible */
-      n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
+      n_bytes = clib_min (tc->snd_mss, tc->snd_nxt - tc->snd_una);
+      n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, n_bytes, &b);
       if (!n_bytes)
        {
          tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
@@ -1733,7 +1744,7 @@ tcp_timer_persist_handler (u32 index)
   if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
     {
       tcp_bt_check_app_limited (tc);
-      tcp_bt_track_tx (tc);
+      tcp_bt_track_tx (tc, n_bytes);
     }
 
   tcp_push_hdr_i (tc, b, tc->snd_nxt, /* compute opts */ 0,
@@ -1780,6 +1791,9 @@ tcp_transmit_unsent (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
   available_wnd = tc->snd_wnd - offset;
   burst_size = clib_min (burst_size, available_wnd / tc->snd_mss);
 
+  if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
+    tcp_bt_check_app_limited (tc);
+
   while (n_segs < burst_size)
     {
       n_written = tcp_prepare_segment (wrk, tc, offset, tc->snd_mss, &b);
@@ -1791,6 +1805,9 @@ tcp_transmit_unsent (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
       offset += n_written;
       n_segs += 1;
 
+      if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
+       tcp_bt_track_tx (tc, n_written);
+
       tc->snd_nxt += n_written;
       tc->snd_una_max = seq_max (tc->snd_nxt, tc->snd_una_max);
     }
@@ -1862,13 +1879,10 @@ tcp_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
   vlib_buffer_t *b = 0;
   sack_scoreboard_t *sb;
   int snd_space;
-  u64 time_now;
 
   ASSERT (tcp_in_cong_recovery (tc));
 
-  time_now = wrk->vm->clib_time.last_cpu_time;
-  burst_bytes = transport_connection_tx_pacer_burst (&tc->connection,
-                                                    time_now);
+  burst_bytes = transport_connection_tx_pacer_burst (&tc->connection);
   burst_size = clib_min (burst_size, burst_bytes / tc->snd_mss);
   if (!burst_size)
     {
@@ -1897,8 +1911,8 @@ tcp_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
       && tc->rxt_head != tc->snd_una
       && tcp_retransmit_should_retry_head (tc, sb))
     {
-      n_written = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss,
-                                                 &b);
+      max_bytes = clib_min (tc->snd_mss, tc->snd_congestion - tc->snd_una);
+      n_written = tcp_prepare_retransmit_segment (wrk, tc, 0, max_bytes, &b);
       if (!n_written)
        {
          tcp_program_retransmit (tc);
@@ -1922,7 +1936,7 @@ tcp_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
 
   while (snd_space > 0 && n_segs < burst_size)
     {
-      hole = scoreboard_next_rxt_hole (sb, hole, max_deq, &can_rescue,
+      hole = scoreboard_next_rxt_hole (sb, hole, max_deq != 0, &can_rescue,
                                       &snd_limited);
       if (!hole)
        {
@@ -1990,6 +2004,8 @@ tcp_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
       tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
 
       sb->high_rxt += n_written;
+      ASSERT (seq_leq (sb->high_rxt, tc->snd_nxt));
+
       snd_space -= n_written;
       n_segs += 1;
     }
@@ -2001,9 +2017,7 @@ done:
 
   if (reset_pacer)
     {
-      transport_connection_tx_pacer_reset_bucket (&tc->connection,
-                                                 vm->clib_time.
-                                                 last_cpu_time);
+      transport_connection_tx_pacer_reset_bucket (&tc->connection);
     }
   else
     {
@@ -2022,20 +2036,17 @@ static int
 tcp_retransmit_no_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
                        u32 burst_size)
 {
-  u32 n_written = 0, offset = 0, bi, max_deq, n_segs_now;
+  u32 n_written = 0, offset = 0, bi, max_deq, n_segs_now, max_bytes;
   u32 burst_bytes, sent_bytes;
   vlib_main_t *vm = wrk->vm;
   int snd_space, n_segs = 0;
   u8 cc_limited = 0;
   vlib_buffer_t *b;
-  u64 time_now;
 
-  ASSERT (tcp_in_fastrecovery (tc));
+  ASSERT (tcp_in_cong_recovery (tc));
   TCP_EVT (TCP_EVT_CC_EVT, tc, 0);
 
-  time_now = wrk->vm->clib_time.last_cpu_time;
-  burst_bytes = transport_connection_tx_pacer_burst (&tc->connection,
-                                                    time_now);
+  burst_bytes = transport_connection_tx_pacer_burst (&tc->connection);
   burst_size = clib_min (burst_size, burst_bytes / tc->snd_mss);
   if (!burst_size)
     {
@@ -2053,8 +2064,12 @@ tcp_retransmit_no_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
    * segment. */
   while (snd_space > 0 && n_segs < burst_size)
     {
-      n_written = tcp_prepare_retransmit_segment (wrk, tc, offset,
-                                                 tc->snd_mss, &b);
+      max_bytes = clib_min (tc->snd_mss,
+                           tc->snd_congestion - tc->snd_una - offset);
+      if (!max_bytes)
+       break;
+      n_written = tcp_prepare_retransmit_segment (wrk, tc, offset, max_bytes,
+                                                 &b);
 
       /* Nothing left to retransmit */
       if (n_written == 0)
@@ -2083,7 +2098,7 @@ send_unsent:
       snd_space = clib_min (max_deq, snd_space);
       burst_size = clib_min (burst_size - n_segs, snd_space / tc->snd_mss);
       n_segs_now = tcp_transmit_unsent (wrk, tc, burst_size);
-      if (max_deq > n_segs_now * tc->snd_mss)
+      if (n_segs_now && max_deq > n_segs_now * tc->snd_mss)
        tcp_program_retransmit (tc);
       n_segs += n_segs_now;
     }
@@ -2270,8 +2285,9 @@ tcp_output_push_ip (vlib_main_t * vm, vlib_buffer_t * b0,
     ih0 = vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
                                IP_PROTOCOL_TCP, tcp_csum_offload (tc0));
   else
-    ih0 = vlib_buffer_push_ip6 (vm, b0, &tc0->c_lcl_ip6, &tc0->c_rmt_ip6,
-                               IP_PROTOCOL_TCP);
+    ih0 =
+      vlib_buffer_push_ip6_custom (vm, b0, &tc0->c_lcl_ip6, &tc0->c_rmt_ip6,
+                                  IP_PROTOCOL_TCP, tc0->ipv6_flow_label);
 
 }