tcp: custom geometry for timer wheel

Type: refactor

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I04f992e5d91d21f1e5bbafef070478cfe268d94a
diff --git a/src/vnet/CMakeLists.txt b/src/vnet/CMakeLists.txt
index 3ae20c8..dc8c0de 100644
--- a/src/vnet/CMakeLists.txt
+++ b/src/vnet/CMakeLists.txt
@@ -623,6 +623,7 @@
   tcp/tcp_cubic.c
   tcp/tcp_debug.c
   tcp/tcp_sack.c
+  tcp/tcp_timer.c
   tcp/tcp.c
 )
 
diff --git a/src/vnet/tcp/tcp.c b/src/vnet/tcp/tcp.c
index 0b3aeba..5ebb638 100644
--- a/src/vnet/tcp/tcp.c
+++ b/src/vnet/tcp/tcp.c
@@ -1146,7 +1146,7 @@
 
   tcp_set_time_now (wrk);
   tcp_handle_cleanups (wrk, now);
-  tw_timer_expire_timers_16t_2w_512sl (&wrk->timer_wheel, now);
+  tcp_timer_expire_timers (&wrk->timer_wheel, now);
   tcp_dispatch_pending_timers (wrk);
 }
 
@@ -1268,21 +1268,6 @@
 }
 
 static void
-tcp_initialize_timer_wheels (tcp_main_t * tm)
-{
-  vlib_main_t *vm = vlib_get_main ();
-  tw_timer_wheel_16t_2w_512sl_t *tw;
-  /* *INDENT-OFF* */
-  foreach_vlib_main (({
-    tw = &tm->wrk_ctx[ii].timer_wheel;
-    tw_timer_wheel_init_16t_2w_512sl (tw, tcp_expired_timers_dispatch,
-                                      TCP_TIMER_TICK, ~0);
-    tw->last_run_time = vlib_time_now (vm);
-  }));
-  /* *INDENT-ON* */
-}
-
-static void
 tcp_initialize_iss_seed (tcp_main_t * tm)
 {
   u32 default_seed = random_default_seed ();
@@ -1357,6 +1342,10 @@
        */
       if ((thread > 0 || num_threads == 1) && prealloc_conn_per_wrk)
 	pool_init_fixed (wrk->connections, prealloc_conn_per_wrk);
+
+      tcp_timer_initialize_wheel (&wrk->timer_wheel,
+				  tcp_expired_timers_dispatch,
+				  vlib_time_now (vm));
     }
 
   /*
@@ -1371,7 +1360,6 @@
       clib_spinlock_init (&tm->half_open_lock);
     }
 
-  tcp_initialize_timer_wheels (tm);
   tcp_initialize_iss_seed (tm);
 
   tm->bytes_per_buffer = vlib_buffer_get_default_data_size (vm);
diff --git a/src/vnet/tcp/tcp_timer.c b/src/vnet/tcp/tcp_timer.c
new file mode 100644
index 0000000..d98d0d1
--- /dev/null
+++ b/src/vnet/tcp/tcp_timer.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vnet/tcp/tcp_timer.h>
+#include <vppinfra/tw_timer_template.c>
+
+void
+tcp_timer_initialize_wheel (tcp_timer_wheel_t * tw,
+			    void (*expired_timer_cb) (u32 *), f64 now)
+{
+  tw_timer_wheel_init_tcp_twsl (tw, expired_timer_cb, TCP_TIMER_TICK, ~0);
+  tw->last_run_time = now;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/tcp/tcp_timer.h b/src/vnet/tcp/tcp_timer.h
index 914b5aa..f604152 100644
--- a/src/vnet/tcp/tcp_timer.h
+++ b/src/vnet/tcp/tcp_timer.h
@@ -23,8 +23,8 @@
 {
   ASSERT (tc->c_thread_index == vlib_get_thread_index ());
   ASSERT (tc->timers[timer_id] == TCP_TIMER_HANDLE_INVALID);
-  tc->timers[timer_id] = tw_timer_start_16t_2w_512sl (tw, tc->c_c_index,
-						      timer_id, interval);
+  tc->timers[timer_id] = tw_timer_start_tcp_twsl (tw, tc->c_c_index,
+						  timer_id, interval);
 }
 
 always_inline void
@@ -35,7 +35,7 @@
   if (tc->timers[timer_id] == TCP_TIMER_HANDLE_INVALID)
     return;
 
-  tw_timer_stop_16t_2w_512sl (tw, tc->timers[timer_id]);
+  tw_timer_stop_tcp_twsl (tw, tc->timers[timer_id]);
   tc->timers[timer_id] = TCP_TIMER_HANDLE_INVALID;
 }
 
@@ -45,10 +45,10 @@
 {
   ASSERT (tc->c_thread_index == vlib_get_thread_index ());
   if (tc->timers[timer_id] != TCP_TIMER_HANDLE_INVALID)
-    tw_timer_update_16t_2w_512sl (tw, tc->timers[timer_id], interval);
+    tw_timer_update_tcp_twsl (tw, tc->timers[timer_id], interval);
   else
-    tc->timers[timer_id] = tw_timer_start_16t_2w_512sl (tw, tc->c_c_index,
-							timer_id, interval);
+    tc->timers[timer_id] = tw_timer_start_tcp_twsl (tw, tc->c_c_index,
+						    timer_id, interval);
 }
 
 always_inline void
@@ -120,6 +120,15 @@
   return tc->timers[timer] != TCP_TIMER_HANDLE_INVALID;
 }
 
+always_inline void
+tcp_timer_expire_timers (tcp_timer_wheel_t * tw, f64 now)
+{
+  tw_timer_expire_timers_tcp_twsl (tw, now);
+}
+
+void tcp_timer_initialize_wheel (tcp_timer_wheel_t * tw,
+				 void (*expired_timer_cb) (u32 *), f64 now);
+
 #endif /* __included_tcp_timer_h__ */
 
 /*
diff --git a/src/vnet/tcp/tcp_types.h b/src/vnet/tcp/tcp_types.h
index 027f0e6..95d5b73 100644
--- a/src/vnet/tcp/tcp_types.h
+++ b/src/vnet/tcp/tcp_types.h
@@ -20,7 +20,6 @@
 #include <vppinfra/rbtree.h>
 #include <vnet/tcp/tcp_packet.h>
 #include <vnet/session/transport.h>
-#include <vppinfra/tw_timer_16t_2w_512sl.h>
 
 #define TCP_TICK 0.000001			/**< TCP tick period (s) */
 #define THZ (u32) (1/TCP_TICK)			/**< TCP tick frequency */
@@ -447,7 +446,35 @@
   return (tcp_connection_t *) tconn;
 }
 
-typedef tw_timer_wheel_16t_2w_512sl_t tcp_timer_wheel_t;
+/*
+ * Define custom timer wheel geometry
+ */
+
+#undef TW_TIMER_WHEELS
+#undef TW_SLOTS_PER_RING
+#undef TW_RING_SHIFT
+#undef TW_RING_MASK
+#undef TW_TIMERS_PER_OBJECT
+#undef LOG2_TW_TIMERS_PER_OBJECT
+#undef TW_SUFFIX
+#undef TW_OVERFLOW_VECTOR
+#undef TW_FAST_WHEEL_BITMAP
+#undef TW_TIMER_ALLOW_DUPLICATE_STOP
+#undef TW_START_STOP_TRACE_SIZE
+
+#define TW_TIMER_WHEELS 2
+#define TW_SLOTS_PER_RING 512
+#define TW_RING_SHIFT 9
+#define TW_RING_MASK (TW_SLOTS_PER_RING -1)
+#define TW_TIMERS_PER_OBJECT 16
+#define LOG2_TW_TIMERS_PER_OBJECT 4
+#define TW_SUFFIX _tcp_twsl
+#define TW_FAST_WHEEL_BITMAP 0
+#define TW_TIMER_ALLOW_DUPLICATE_STOP 1
+
+#include <vppinfra/tw_timer_template.h>
+
+typedef tw_timer_wheel_tcp_twsl_t tcp_timer_wheel_t;
 
 #endif /* SRC_VNET_TCP_TCP_TYPES_H_ */