tcp: track pending timers
[vpp.git] / src / vnet / tcp / tcp_output.c
index 095f360..e9bff5b 100644 (file)
@@ -120,11 +120,6 @@ tcp_update_rcv_wnd (tcp_connection_t * tc)
    * Figure out how much space we have available
    */
   available_space = transport_max_rx_enqueue (&tc->connection);
-  if (PREDICT_FALSE (available_space < tc->rcv_opts.mss))
-    {
-      tc->rcv_wnd = 0;
-      return;
-    }
 
   /*
    * Use the above and what we know about what we've previously advertised
@@ -132,24 +127,22 @@ tcp_update_rcv_wnd (tcp_connection_t * tc)
    */
   observed_wnd = (i32) tc->rcv_wnd - (tc->rcv_nxt - tc->rcv_las);
 
-  /* Bad. Thou shalt not shrink */
+  /* Check if we are about to retract the window. Do the comparison before
+   * rounding to avoid errors. Per RFC7323 sec. 2.4 we could remove this */
   if (PREDICT_FALSE ((i32) available_space < observed_wnd))
     {
-      wnd = clib_max (observed_wnd, 0);
+      wnd = round_down_pow2 (clib_max (observed_wnd, 0), 1 << tc->rcv_wscale);
       TCP_EVT (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
     }
   else
     {
-      wnd = available_space;
+      /* Make sure we have a multiple of 1 << rcv_wscale. We round down to
+       * avoid advertising a window larger than what can be buffered */
+      wnd = round_down_pow2 (available_space, 1 << tc->rcv_wscale);
     }
 
-  /* Make sure we have a multiple of rcv_wscale */
-  if (wnd && tc->rcv_wscale)
-    {
-      wnd &= ~((1 << tc->rcv_wscale) - 1);
-      if (wnd == 0)
-       wnd = 1 << tc->rcv_wscale;
-    }
+  if (PREDICT_FALSE (wnd < tc->rcv_opts.mss))
+    wnd = 0;
 
   tc->rcv_wnd = clib_min (wnd, TCP_WND_MAX << tc->rcv_wscale);
 }
@@ -671,7 +664,10 @@ tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt,
   fib_protocol_t fib_proto;
 
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
-    return;
+    {
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
+      return;
+    }
 
   b = vlib_get_buffer (vm, bi);
   sw_if_index = vnet_buffer (pkt)->sw_if_index[VLIB_RX];
@@ -752,7 +748,10 @@ tcp_send_reset (tcp_connection_t * tc)
   u8 flags;
 
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
-    return;
+    {
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
+      return;
+    }
   b = vlib_get_buffer (vm, bi);
   tcp_init_buffer (vm, b);
 
@@ -814,6 +813,7 @@ tcp_send_syn (tcp_connection_t * tc)
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
     {
       tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT_SYN, 1);
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
       return;
     }
 
@@ -844,6 +844,7 @@ tcp_send_synack (tcp_connection_t * tc)
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
     {
       tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT, 1);
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
       return;
     }
 
@@ -880,6 +881,7 @@ tcp_send_fin (tcp_connection_t * tc)
       else
        /* Make sure retransmit retries a fin not data */
        tc->flags |= TCP_CONN_FINSNT;
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
       return;
     }
 
@@ -1020,6 +1022,7 @@ tcp_send_ack (tcp_connection_t * tc)
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
     {
       tcp_update_rcv_wnd (tc);
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
       return;
     }
   b = vlib_get_buffer (vm, bi);
@@ -1060,17 +1063,6 @@ tcp_program_retransmit (tcp_connection_t * tc)
     }
 }
 
-/**
- * Delayed ack timer handler
- *
- * Sends delayed ACK when timer expires
- */
-void
-tcp_timer_delack_handler (tcp_connection_t * tc)
-{
-  tcp_send_ack (tc);
-}
-
 /**
  * Send window update ack
  *
@@ -1129,7 +1121,10 @@ tcp_prepare_segment (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
   if (PREDICT_TRUE (seg_size <= bytes_per_buffer))
     {
       if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
-       return 0;
+       {
+         tcp_worker_stats_inc (wrk, no_buffer, 1);
+         return 0;
+       }
       *b = vlib_get_buffer (vm, bi);
       data = tcp_init_buffer (vm, *b);
       n_bytes = session_tx_fifo_peek_bytes (&tc->connection, data, offset,
@@ -1156,6 +1151,7 @@ tcp_prepare_segment (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
        {
          if (n_bufs)
            vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
+         tcp_worker_stats_inc (wrk, no_buffer, 1);
          return 0;
        }
 
@@ -1417,6 +1413,7 @@ tcp_timer_retransmit_handler (tcp_connection_t * tc)
       if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
        {
          tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT, 1);
+         tcp_worker_stats_inc (wrk, no_buffer, 1);
          return;
        }
 
@@ -1480,6 +1477,7 @@ tcp_timer_retransmit_syn_handler (tcp_connection_t * tc)
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
     {
       tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_RETRANSMIT_SYN, 1);
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
       return;
     }
 
@@ -1547,6 +1545,7 @@ tcp_timer_persist_handler (tcp_connection_t * tc)
   if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
     {
       tcp_persist_timer_set (&wrk->timer_wheel, tc);
+      tcp_worker_stats_inc (wrk, no_buffer, 1);
       return;
     }
 
@@ -2010,7 +2009,7 @@ tcp_do_retransmit (tcp_connection_t * tc, u32 max_burst_size)
 }
 
 int
-tcp_session_custom_tx (void *conn, u32 max_burst_size)
+tcp_session_custom_tx (void *conn, transport_send_params_t * sp)
 {
   tcp_connection_t *tc = (tcp_connection_t *) conn;
   u32 n_segs = 0;
@@ -2018,8 +2017,7 @@ tcp_session_custom_tx (void *conn, u32 max_burst_size)
   if (tcp_in_cong_recovery (tc) && (tc->flags & TCP_CONN_RXT_PENDING))
     {
       tc->flags &= ~TCP_CONN_RXT_PENDING;
-      n_segs = tcp_do_retransmit (tc, max_burst_size);
-      max_burst_size -= n_segs;
+      n_segs = tcp_do_retransmit (tc, sp->max_burst_size);
     }
 
   if (!(tc->flags & TCP_CONN_SNDACK))
@@ -2031,13 +2029,13 @@ tcp_session_custom_tx (void *conn, u32 max_burst_size)
   if (n_segs && !tc->pending_dupacks)
     return n_segs;
 
-  if (!max_burst_size)
+  if (sp->max_burst_size <= n_segs)
     {
       tcp_program_ack (tc);
-      return max_burst_size;
+      return n_segs;
     }
 
-  n_segs += tcp_send_acks (tc, max_burst_size);
+  n_segs += tcp_send_acks (tc, sp->max_burst_size - n_segs);
 
   return n_segs;
 }
@@ -2182,9 +2180,6 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
   u32 n_left_from, *from, thread_index = vm->thread_index;
   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
   u16 nexts[VLIB_FRAME_SIZE], *next;
-  vlib_node_runtime_t *error_node;
-
-  error_node = vlib_node_get_runtime (vm, tcp_node_index (output, is_ip4));
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -2222,8 +2217,8 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          tcp_check_if_gso (tc0, b[0]);
          tcp_check_if_gso (tc1, b[1]);
 
-         tcp_output_handle_packet (tc0, b[0], error_node, &next[0], is_ip4);
-         tcp_output_handle_packet (tc1, b[1], error_node, &next[1], is_ip4);
+         tcp_output_handle_packet (tc0, b[0], node, &next[0], is_ip4);
+         tcp_output_handle_packet (tc1, b[1], node, &next[1], is_ip4);
        }
       else
        {
@@ -2231,24 +2226,22 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
            {
              tcp_output_push_ip (vm, b[0], tc0, is_ip4);
              tcp_check_if_gso (tc0, b[0]);
-             tcp_output_handle_packet (tc0, b[0], error_node, &next[0],
-                                       is_ip4);
+             tcp_output_handle_packet (tc0, b[0], node, &next[0], is_ip4);
            }
          else
            {
-             b[0]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
+             b[0]->error = node->errors[TCP_ERROR_INVALID_CONNECTION];
              next[0] = TCP_OUTPUT_NEXT_DROP;
            }
          if (tc1 != 0)
            {
              tcp_output_push_ip (vm, b[1], tc1, is_ip4);
              tcp_check_if_gso (tc1, b[1]);
-             tcp_output_handle_packet (tc1, b[1], error_node, &next[1],
-                                       is_ip4);
+             tcp_output_handle_packet (tc1, b[1], node, &next[1], is_ip4);
            }
          else
            {
-             b[1]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
+             b[1]->error = node->errors[TCP_ERROR_INVALID_CONNECTION];
              next[1] = TCP_OUTPUT_NEXT_DROP;
            }
        }
@@ -2274,11 +2267,11 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
        {
          tcp_output_push_ip (vm, b[0], tc0, is_ip4);
          tcp_check_if_gso (tc0, b[0]);
-         tcp_output_handle_packet (tc0, b[0], error_node, &next[0], is_ip4);
+         tcp_output_handle_packet (tc0, b[0], node, &next[0], is_ip4);
        }
       else
        {
-         b[0]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
+         b[0]->error = node->errors[TCP_ERROR_INVALID_CONNECTION];
          next[0] = TCP_OUTPUT_NEXT_DROP;
        }