tcp: fix tcp packet trace
[vpp.git] / src / vnet / tcp / tcp_input.c
index c15c328..1bcdd4c 100644 (file)
@@ -75,6 +75,18 @@ typedef enum _tcp_state_next
     TCP_STATE_N_NEXT,
 } tcp_state_next_t;
 
+typedef enum _tcp_input_next
+{
+  TCP_INPUT_NEXT_DROP,
+  TCP_INPUT_NEXT_LISTEN,
+  TCP_INPUT_NEXT_RCV_PROCESS,
+  TCP_INPUT_NEXT_SYN_SENT,
+  TCP_INPUT_NEXT_ESTABLISHED,
+  TCP_INPUT_NEXT_RESET,
+  TCP_INPUT_NEXT_PUNT,
+  TCP_INPUT_N_NEXT
+} tcp_input_next_t;
+
 #define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT          \
                                         : TCP_NEXT_TCP6_OUTPUT)
 
@@ -594,7 +606,7 @@ tcp_handle_postponed_dequeues (tcp_worker_ctx_t * wrk)
 
       tc->burst_acked = 0;
     }
-  _vec_len (wrk->pending_deq_acked) = 0;
+  vec_set_len (wrk->pending_deq_acked, 0);
 }
 
 static void
@@ -1099,7 +1111,7 @@ tcp_handle_disconnects (tcp_worker_ctx_t * wrk)
          tcp_disconnect_pending_off (tc);
          session_transport_closing_notify (&tc->connection);
        }
-      _vec_len (wrk->pending_disconnects) = 0;
+      vec_set_len (wrk->pending_disconnects, 0);
     }
 
   if (vec_len (wrk->pending_resets))
@@ -1112,7 +1124,7 @@ tcp_handle_disconnects (tcp_worker_ctx_t * wrk)
          tcp_disconnect_pending_off (tc);
          tcp_handle_rst (tc);
        }
-      _vec_len (wrk->pending_resets) = 0;
+      vec_set_len (wrk->pending_resets, 0);
     }
 }
 
@@ -1149,7 +1161,6 @@ tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
   ASSERT (data_len);
   written = session_enqueue_stream_connection (&tc->connection, b, 0,
                                               1 /* queue event */ , 1);
-  tc->bytes_in += written;
 
   TCP_EVT (TCP_EVT_INPUT, tc, 0, data_len, written);
 
@@ -1157,17 +1168,20 @@ tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
   if (PREDICT_TRUE (written == data_len))
     {
       tc->rcv_nxt += written;
+      tc->bytes_in += written;
     }
   /* If more data written than expected, account for out-of-order bytes. */
   else if (written > data_len)
     {
       tc->rcv_nxt += written;
+      tc->bytes_in += data_len;
       TCP_EVT (TCP_EVT_CC_INPUT, tc, data_len, written);
     }
   else if (written > 0)
     {
       /* We've written something but FIFO is probably full now */
       tc->rcv_nxt += written;
+      tc->bytes_in += written;
       error = TCP_ERROR_PARTIALLY_ENQUEUED;
     }
   else
@@ -1354,9 +1368,13 @@ format_tcp_rx_trace (u8 * s, va_list * args)
   tcp_connection_t *tc = &t->tcp_connection;
   u32 indent = format_get_indent (s);
 
-  s = format (s, "%U state %U\n%U%U", format_tcp_connection_id, tc,
-             format_tcp_state, tc->state, format_white_space, indent,
-             format_tcp_header, &t->tcp_header, 128);
+  if (!tc->c_lcl_port)
+    s = format (s, "no tcp connection\n%U%U", format_white_space, indent,
+               format_tcp_header, &t->tcp_header, 128);
+  else
+    s = format (s, "%U state %U\n%U%U", format_tcp_connection_id, tc,
+               format_tcp_state, tc->state, format_white_space, indent,
+               format_tcp_header, &t->tcp_header, 128);
 
   return s;
 }
@@ -1426,44 +1444,6 @@ tcp_established_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
     }
 }
 
-always_inline void
-tcp_node_inc_counter_i (vlib_main_t * vm, u32 tcp4_node, u32 tcp6_node,
-                       u8 is_ip4, u32 evt, u32 val)
-{
-  if (is_ip4)
-    vlib_node_increment_counter (vm, tcp4_node, evt, val);
-  else
-    vlib_node_increment_counter (vm, tcp6_node, evt, val);
-}
-
-#define tcp_maybe_inc_counter(node_id, err, count)                     \
-{                                                                      \
-  if (next0 != tcp_next_drop (is_ip4))                                 \
-    tcp_node_inc_counter_i (vm, tcp4_##node_id##_node.index,           \
-                            tcp6_##node_id##_node.index, is_ip4, err,  \
-                           1);                                         \
-}
-#define tcp_inc_counter(node_id, err, count)                           \
-  tcp_node_inc_counter_i (vm, tcp4_##node_id##_node.index,             \
-                          tcp6_##node_id##_node.index, is_ip4,         \
-                          err, count)
-#define tcp_maybe_inc_err_counter(cnts, err)                           \
-{                                                                      \
-  cnts[err] += (next0 != tcp_next_drop (is_ip4));                      \
-}
-#define tcp_inc_err_counter(cnts, err, val)                            \
-{                                                                      \
-  cnts[err] += val;                                                    \
-}
-#define tcp_store_err_counters(node_id, cnts)                          \
-{                                                                      \
-  int i;                                                               \
-  for (i = 0; i < TCP_N_ERROR; i++)                                    \
-    if (cnts[i])                                                       \
-      tcp_inc_counter(node_id, i, cnts[i]);                            \
-}
-
-
 always_inline uword
 tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                          vlib_frame_t * frame, int is_ip4)
@@ -1793,6 +1773,45 @@ tcp_check_tx_offload (tcp_connection_t * tc, int is_ipv4)
     tc->cfg_flags |= TCP_CFG_F_TSO;
 }
 
+static void
+tcp_input_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
+                      vlib_buffer_t **bs, u16 *nexts, u32 n_bufs, u8 is_ip4)
+{
+  tcp_connection_t *tc;
+  tcp_header_t *tcp;
+  tcp_rx_trace_t *t;
+  u8 flags;
+  int i;
+
+  for (i = 0; i < n_bufs; i++)
+    {
+      if (!(bs[i]->flags & VLIB_BUFFER_IS_TRACED))
+       continue;
+
+      t = vlib_add_trace (vm, node, bs[i], sizeof (*t));
+      if (nexts[i] == TCP_INPUT_NEXT_DROP || nexts[i] == TCP_INPUT_NEXT_PUNT ||
+         nexts[i] == TCP_INPUT_NEXT_RESET)
+       {
+         tc = 0;
+       }
+      else
+       {
+         flags = vnet_buffer (bs[i])->tcp.flags;
+
+         if (flags == TCP_STATE_LISTEN)
+           tc = tcp_listener_get (vnet_buffer (bs[i])->tcp.connection_index);
+         else if (flags == TCP_STATE_SYN_SENT)
+           tc = tcp_half_open_connection_get (
+             vnet_buffer (bs[i])->tcp.connection_index);
+         else
+           tc = tcp_connection_get (vnet_buffer (bs[i])->tcp.connection_index,
+                                    vm->thread_index);
+       }
+      tcp = tcp_buffer_hdr (bs[i]);
+      tcp_set_rx_trace_data (t, tc, tcp, bs[i], is_ip4);
+    }
+}
+
 always_inline uword
 tcp46_syn_sent_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
                       vlib_frame_t *frame, int is_ip4)
@@ -2657,7 +2676,6 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
 
   while (n_left_from > 0)
     {
-      u32 error = TCP_ERROR_NONE;
       tcp_connection_t *lc, *child;
 
       /* Flags initialized with connection state after lookup */
@@ -2665,21 +2683,22 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
        {
          lc = tcp_listener_get (vnet_buffer (b[0])->tcp.connection_index);
        }
-      else /* We are in TimeWait state*/
+      /* Probably we are in time-wait or closed state */
+      else
        {
          tcp_connection_t *tc;
          tc = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
                                   thread_index);
          if (tc->state != TCP_STATE_TIME_WAIT)
            {
-             error = TCP_ERROR_CREATE_EXISTS;
+             tcp_inc_counter (listen, TCP_ERROR_CREATE_EXISTS, 1);
              goto done;
            }
 
          if (PREDICT_FALSE (!syn_during_timewait (tc, b[0], &tw_iss)))
            {
              /* This SYN can't be accepted */
-             error = TCP_ERROR_CREATE_EXISTS;
+             tcp_inc_counter (listen, TCP_ERROR_CREATE_EXISTS, 1);
              goto done;
            }
 
@@ -2689,7 +2708,7 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
          /* listener was cleaned up */
          if (!lc)
            {
-             error = TCP_ERROR_NO_LISTENER;
+             tcp_inc_counter (listen, TCP_ERROR_NO_LISTENER, 1);
              goto done;
            }
        }
@@ -2699,7 +2718,7 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
        tcp_lookup_connection (lc->c_fib_index, b[0], thread_index, is_ip4);
       if (PREDICT_FALSE (child->state != TCP_STATE_LISTEN))
        {
-         error = TCP_ERROR_CREATE_EXISTS;
+         tcp_inc_counter (listen, TCP_ERROR_CREATE_EXISTS, 1);
          goto done;
        }
 
@@ -2716,7 +2735,7 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
 
       if (tcp_options_parse (tcp_buffer_hdr (b[0]), &child->rcv_opts, 1))
        {
-         error = TCP_ERROR_OPTIONS;
+         tcp_inc_counter (listen, TCP_ERROR_OPTIONS, 1);
          tcp_connection_free (child);
          goto done;
        }
@@ -2746,7 +2765,7 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
                                 lc->c_thread_index, 0 /* notify */ ))
        {
          tcp_connection_cleanup (child);
-         error = TCP_ERROR_CREATE_SESSION_FAIL;
+         tcp_inc_counter (listen, TCP_ERROR_CREATE_SESSION_FAIL, 1);
          goto done;
        }
 
@@ -2754,12 +2773,11 @@ tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
       child->tx_fifo_size = transport_tx_fifo_size (&child->connection);
 
       tcp_send_synack (child);
+      n_syns += 1;
 
     done:
-
       b += 1;
       n_left_from -= 1;
-      n_syns += (error == TCP_ERROR_NONE);
     }
 
   tcp_inc_counter (listen, TCP_ERROR_SYNS_RCVD, n_syns);
@@ -2818,18 +2836,6 @@ VLIB_REGISTER_NODE (tcp6_listen_node) =
 };
 /* *INDENT-ON* */
 
-typedef enum _tcp_input_next
-{
-  TCP_INPUT_NEXT_DROP,
-  TCP_INPUT_NEXT_LISTEN,
-  TCP_INPUT_NEXT_RCV_PROCESS,
-  TCP_INPUT_NEXT_SYN_SENT,
-  TCP_INPUT_NEXT_ESTABLISHED,
-  TCP_INPUT_NEXT_RESET,
-  TCP_INPUT_NEXT_PUNT,
-  TCP_INPUT_N_NEXT
-} tcp_input_next_t;
-
 #define foreach_tcp4_input_next                 \
   _ (DROP, "ip4-drop")                          \
   _ (LISTEN, "tcp4-listen")                     \
@@ -2850,28 +2856,6 @@ typedef enum _tcp_input_next
 
 #define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
 
-static void
-tcp_input_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
-                      vlib_buffer_t ** bs, u32 n_bufs, u8 is_ip4)
-{
-  tcp_connection_t *tc;
-  tcp_header_t *tcp;
-  tcp_rx_trace_t *t;
-  int i;
-
-  for (i = 0; i < n_bufs; i++)
-    {
-      if (bs[i]->flags & VLIB_BUFFER_IS_TRACED)
-       {
-         t = vlib_add_trace (vm, node, bs[i], sizeof (*t));
-         tc = tcp_connection_get (vnet_buffer (bs[i])->tcp.connection_index,
-                                  vm->thread_index);
-         tcp = vlib_buffer_get_current (bs[i]);
-         tcp_set_rx_trace_data (t, tc, tcp, bs[i], is_ip4);
-       }
-    }
-}
-
 static void
 tcp_input_set_error_next (tcp_main_t * tm, u16 * next, u32 * error, u8 is_ip4)
 {
@@ -2892,9 +2876,8 @@ tcp_input_set_error_next (tcp_main_t * tm, u16 * next, u32 * error, u8 is_ip4)
 }
 
 static inline void
-tcp_input_dispatch_buffer (tcp_main_t * tm, tcp_connection_t * tc,
-                          vlib_buffer_t * b, u16 * next,
-                          vlib_node_runtime_t * error_node)
+tcp_input_dispatch_buffer (tcp_main_t *tm, tcp_connection_t *tc,
+                          vlib_buffer_t *b, u16 *next, u16 *err_counters)
 {
   tcp_header_t *tcp;
   u32 error;
@@ -2916,7 +2899,7 @@ tcp_input_dispatch_buffer (tcp_main_t * tm, tcp_connection_t * tc,
 
   if (PREDICT_FALSE (error != TCP_ERROR_NONE))
     {
-      b->error = error_node->errors[error];
+      tcp_inc_err_counter (err_counters, error, 1);
       if (error == TCP_ERROR_DISPATCH)
        clib_warning ("tcp conn %u disp error state %U flags %U",
                      tc->c_c_index, format_tcp_state, tc->state,
@@ -2932,6 +2915,7 @@ tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
   tcp_main_t *tm = vnet_get_tcp_main ();
   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
   u16 nexts[VLIB_FRAME_SIZE], *next;
+  u16 err_counters[TCP_N_ERROR] = { 0 };
 
   tcp_update_time_now (tcp_get_worker (thread_index));
 
@@ -2970,8 +2954,8 @@ tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
          vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
 
-         tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], node);
-         tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], node);
+         tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], err_counters);
+         tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], err_counters);
        }
       else
        {
@@ -2979,24 +2963,26 @@ tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
            {
              ASSERT (tcp_lookup_is_valid (tc0, b[0], tcp_buffer_hdr (b[0])));
              vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
-             tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], node);
+             tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0],
+                                        err_counters);
            }
          else
            {
              tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
-             b[0]->error = node->errors[error0];
+             tcp_inc_err_counter (err_counters, error0, 1);
            }
 
          if (PREDICT_TRUE (tc1 != 0))
            {
              ASSERT (tcp_lookup_is_valid (tc1, b[1], tcp_buffer_hdr (b[1])));
              vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
-             tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], node);
+             tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1],
+                                        err_counters);
            }
          else
            {
              tcp_input_set_error_next (tm, &next[1], &error1, is_ip4);
-             b[1]->error = node->errors[error1];
+             tcp_inc_err_counter (err_counters, error1, 1);
            }
        }
 
@@ -3022,12 +3008,12 @@ tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
        {
          ASSERT (tcp_lookup_is_valid (tc0, b[0], tcp_buffer_hdr (b[0])));
          vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
-         tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], node);
+         tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], err_counters);
        }
       else
        {
          tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
-         b[0]->error = node->errors[error0];
+         tcp_inc_err_counter (err_counters, error0, 1);
        }
 
       b += 1;
@@ -3036,8 +3022,9 @@ tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
     }
 
   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
-    tcp_input_trace_frame (vm, node, bufs, frame->n_vectors, is_ip4);
+    tcp_input_trace_frame (vm, node, bufs, nexts, frame->n_vectors, is_ip4);
 
+  tcp_store_err_counters (input, err_counters);
   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
   return frame->n_vectors;
 }
@@ -3385,7 +3372,7 @@ do {                                                              \
   _(CLOSED, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP,
     TCP_ERROR_CONNECTION_CLOSED);
   _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_CONNECTION_CLOSED);
-  _(CLOSED, TCP_FLAG_SYN, TCP_INPUT_NEXT_RESET, TCP_ERROR_CONNECTION_CLOSED);
+  _ (CLOSED, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
   _(CLOSED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET,
     TCP_ERROR_CONNECTION_CLOSED);
 #undef _