From 5e6305fb02ccdfd38c8c5e369a960deaa7602eba Mon Sep 17 00:00:00 2001
From: Florin Coras <fcoras@cisco.com>
Date: Wed, 12 Feb 2020 07:42:01 +0000
Subject: tcp: minimal set of worker stats

Type: feature

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I9dafe564229095d50285276a654f4983f93faff2
---
 src/vnet/tcp/tcp.c         | 90 ++++++++++++++++++++++++++++++++++++++++++----
 src/vnet/tcp/tcp.h         | 27 ++++++++++++++
 src/vnet/tcp/tcp_error.def |  1 -
 src/vnet/tcp/tcp_output.c  |  4 +++
 4 files changed, 115 insertions(+), 7 deletions(-)

(limited to 'src')

diff --git a/src/vnet/tcp/tcp.c b/src/vnet/tcp/tcp.c
index ec24082347f..f83f00aa695 100644
--- a/src/vnet/tcp/tcp.c
+++ b/src/vnet/tcp/tcp.c
@@ -368,6 +368,7 @@ tcp_connection_close (tcp_connection_t * tc)
 	  tcp_connection_set_state (tc, TCP_STATE_CLOSED);
 	  tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.closewait_time);
 	  session_transport_closed_notify (&tc->connection);
+	  tcp_worker_stats_inc (tc->c_thread_index, rst_unread, 1);
 	  break;
 	}
       if (!transport_max_tx_dequeue (&tc->connection))
@@ -1376,6 +1377,7 @@ tcp_timer_waitclose_handler (u32 conn_index, u32 thread_index)
 
       if (!(tc->flags & TCP_CONN_FINPNDG))
 	{
+	  clib_warning ("close-wait with fin sent");
 	  tcp_connection_set_state (tc, TCP_STATE_CLOSED);
 	  tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.cleanup_time);
 	  break;
@@ -1391,6 +1393,7 @@ tcp_timer_waitclose_handler (u32 conn_index, u32 thread_index)
 
       /* Make sure we don't wait in LAST ACK forever */
       tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.lastack_time);
+      tcp_worker_stats_inc (thread_index, to_closewait, 1);
 
       /* Don't delete the connection yet */
       break;
@@ -1412,13 +1415,21 @@ tcp_timer_waitclose_handler (u32 conn_index, u32 thread_index)
 	  tcp_connection_set_state (tc, TCP_STATE_CLOSED);
 	  tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.cleanup_time);
 	}
+      tcp_worker_stats_inc (thread_index, to_finwait1, 1);
       break;
     case TCP_STATE_LAST_ACK:
+      tcp_connection_timers_reset (tc);
+      tcp_connection_set_state (tc, TCP_STATE_CLOSED);
+      tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.cleanup_time);
+      session_transport_closed_notify (&tc->connection);
+      tcp_worker_stats_inc (thread_index, to_lastack, 1);
+      break;
     case TCP_STATE_CLOSING:
       tcp_connection_timers_reset (tc);
       tcp_connection_set_state (tc, TCP_STATE_CLOSED);
       tcp_timer_set (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.cleanup_time);
       session_transport_closed_notify (&tc->connection);
+      tcp_worker_stats_inc (thread_index, to_closing, 1);
       break;
     default:
       tcp_connection_del (tc);
@@ -1441,15 +1452,18 @@ static void
 tcp_expired_timers_dispatch (u32 * expired_timers)
 {
   u32 thread_index = vlib_get_thread_index ();
-  u32 connection_index, timer_id;
+  u32 connection_index, timer_id, n_expired;
   tcp_connection_t *tc;
   int i;
 
+  n_expired = vec_len (expired_timers);
+  tcp_worker_stats_inc (thread_index, timer_expirations, n_expired);
+
   /*
    * Invalidate all timer handles before dispatching. This avoids dangling
    * index references to timer wheel pool entries that have been freed.
    */
-  for (i = 0; i < vec_len (expired_timers); i++)
+  for (i = 0; i < n_expired; i++)
     {
       connection_index = expired_timers[i] & 0x0FFFFFFF;
       timer_id = expired_timers[i] >> 28;
@@ -1467,7 +1481,7 @@ tcp_expired_timers_dispatch (u32 * expired_timers)
   /*
    * Dispatch expired timers
    */
-  for (i = 0; i < vec_len (expired_timers); i++)
+  for (i = 0; i < n_expired; i++)
     {
       connection_index = expired_timers[i] & 0x0FFFFFFF;
       timer_id = expired_timers[i] >> 28;
@@ -1964,8 +1978,8 @@ tcp_configure_v6_source_address_range (vlib_main_t * vm,
 }
 
 static clib_error_t *
-tcp_src_address (vlib_main_t * vm,
-		 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
+tcp_src_address_fn (vlib_main_t * vm,
+		    unformat_input_t * input, vlib_cli_command_t * cmd_arg)
 {
   ip4_address_t v4start, v4end;
   ip6_address_t v6start, v6end;
@@ -2047,7 +2061,7 @@ VLIB_CLI_COMMAND (tcp_src_address_command, static) =
 {
   .path = "tcp src-address",
   .short_help = "tcp src-address <ip-addr> [- <ip-addr>] add src address range",
-  .function = tcp_src_address,
+  .function = tcp_src_address_fn,
 };
 /* *INDENT-ON* */
 
@@ -2261,6 +2275,70 @@ VLIB_CLI_COMMAND (show_tcp_punt_command, static) =
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+show_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
+		   vlib_cli_command_t * cmd)
+{
+  tcp_main_t *tm = vnet_get_tcp_main ();
+  tcp_worker_ctx_t *wrk;
+  u32 thread;
+
+  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
+			      input);
+  for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
+    {
+      wrk = tcp_get_worker (thread);
+      vlib_cli_output (vm, "Thread %d:\n", thread);
+
+#define _(name,type,str)					\
+  if (wrk->stats.name)						\
+    vlib_cli_output (vm, " %ld %s", wrk->stats.name, str);
+      foreach_tcp_wrk_stat
+#undef _
+    }
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_tcp_stats_command, static) =
+{
+  .path = "show tcp stats",
+  .short_help = "show tcp stats",
+  .function = show_tcp_stats_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+clear_tcp_stats_fn (vlib_main_t * vm, unformat_input_t * input,
+		    vlib_cli_command_t * cmd)
+{
+  tcp_main_t *tm = vnet_get_tcp_main ();
+  tcp_worker_ctx_t *wrk;
+  u32 thread;
+
+  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
+			      input);
+
+  for (thread = 0; thread < vec_len (tm->wrk_ctx); thread++)
+    {
+      wrk = tcp_get_worker (thread);
+      clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
+    }
+
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (clear_tcp_stats_command, static) =
+{
+  .path = "clear tcp stats",
+  .short_help = "clear tcp stats",
+  .function = clear_tcp_stats_fn,
+};
+/* *INDENT-ON* */
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vnet/tcp/tcp.h b/src/vnet/tcp/tcp.h
index 59ccf8f1a73..1549efbeb7f 100644
--- a/src/vnet/tcp/tcp.h
+++ b/src/vnet/tcp/tcp.h
@@ -502,6 +502,24 @@ typedef struct _tcp_lookup_dispatch
   u8 next, error;
 } tcp_lookup_dispatch_t;
 
+#define foreach_tcp_wrk_stat					\
+  _(timer_expirations, u64, "timer expirations")		\
+  _(rxt_segs, u64, "segments retransmitted")			\
+  _(tr_events, u32, "timer retransmit events")			\
+  _(to_closewait, u32, "timeout close-wait")			\
+  _(to_finwait1, u32, "timeout fin-wait-1")			\
+  _(to_lastack, u32, "timeout last-ack")			\
+  _(to_closing, u32, "timeout closing")				\
+  _(tr_abort, u32, "timer retransmit abort")			\
+  _(rst_unread, u32, "reset on close due to unread data")	\
+
+typedef struct tcp_wrk_stats_
+{
+#define _(name, type, str) type name;
+  foreach_tcp_wrk_stat
+#undef _
+} tcp_wrk_stats_t;
+
 typedef struct tcp_worker_ctx_
 {
   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
@@ -538,8 +556,17 @@ typedef struct tcp_worker_ctx_
   /** worker timer wheel */
   tw_timer_wheel_16t_2w_512sl_t timer_wheel;
 
+    CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
+
+  tcp_wrk_stats_t stats;
 } tcp_worker_ctx_t;
 
+#define tcp_worker_stats_inc(_ti,_stat,_val) 		\
+  tcp_main.wrk_ctx[_ti].stats._stat += _val
+
+#define tcp_workerp_stats_inc(_wrk,_stat,_val) 		\
+  _wrk->stats._stat += _val
+
 typedef struct tcp_iss_seed_
 {
   u64 first;
diff --git a/src/vnet/tcp/tcp_error.def b/src/vnet/tcp/tcp_error.def
index 5acc576e3cb..d51e7ebf756 100644
--- a/src/vnet/tcp/tcp_error.def
+++ b/src/vnet/tcp/tcp_error.def
@@ -37,7 +37,6 @@ tcp_error (ACK_DUP, "Duplicate ACK")
 tcp_error (ACK_OLD, "Old ACK")
 tcp_error (ACK_FUTURE, "Future ACK")
 tcp_error (PKTS_SENT, "Packets sent")
-tcp_error (FILTERED_DUPACKS, "Filtered duplicate ACKs")
 tcp_error (RST_SENT, "Resets sent")
 tcp_error (RST_RCVD, "Resets received")
 tcp_error (INVALID_CONNECTION, "Invalid connection")
diff --git a/src/vnet/tcp/tcp_output.c b/src/vnet/tcp/tcp_output.c
index e843c7ea990..1b9530b4e1a 100644
--- a/src/vnet/tcp/tcp_output.c
+++ b/src/vnet/tcp/tcp_output.c
@@ -1400,6 +1400,7 @@ tcp_prepare_retransmit_segment (tcp_worker_ctx_t * wrk,
 
   tc->bytes_retrans += n_bytes;
   tc->segs_retrans += 1;
+  tcp_workerp_stats_inc (wrk, rxt_segs, 1);
   TCP_EVT (TCP_EVT_CC_RTX, tc, offset, n_bytes);
 
   return n_bytes;
@@ -1452,6 +1453,7 @@ tcp_timer_retransmit_handler (u32 tc_index, u32 thread_index)
   vlib_buffer_t *b = 0;
   u32 bi, n_bytes;
 
+  tcp_workerp_stats_inc (wrk, tr_events, 1);
   tc = tcp_connection_get (tc_index, thread_index);
 
   /* Note: the connection may have been closed and pool_put */
@@ -1505,6 +1507,7 @@ tcp_timer_retransmit_handler (u32 tc_index, u32 thread_index)
 	  session_transport_closed_notify (&tc->connection);
 	  tcp_connection_timers_reset (tc);
 	  tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.closewait_time);
+	  tcp_workerp_stats_inc (wrk, tr_abort, 1);
 	  return;
 	}
 
@@ -1556,6 +1559,7 @@ tcp_timer_retransmit_handler (u32 tc_index, u32 thread_index)
 	  tcp_connection_set_state (tc, TCP_STATE_CLOSED);
 	  tcp_connection_timers_reset (tc);
 	  tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.cleanup_time);
+	  tcp_workerp_stats_inc (wrk, tr_abort, 1);
 	  return;
 	}
 
-- 
cgit