session: measure dispatch period only if under load

Also reset pacer on tcp retransmit timeout

Change-Id: I5a9edee4c00d1d169248d79587a9b10437c2bd87
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index 3dd80ad..f689486 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -1366,6 +1366,7 @@
       _vec_len (wrk->postponed_event_vector) = 0;
 
       wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
+      wrk->dispatch_period = 500e-6;
 
       if (num_threads > 1)
 	clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
diff --git a/src/vnet/session/session.h b/src/vnet/session/session.h
index 131652a..3bdf6c9 100644
--- a/src/vnet/session/session.h
+++ b/src/vnet/session/session.h
@@ -229,6 +229,8 @@
   /** Peekers rw lock */
   clib_rwlock_t peekers_rw_locks;
 
+  u32 last_tx_packets;
+
 } session_manager_worker_t;
 
 struct _session_manager_main
diff --git a/src/vnet/session/session_node.c b/src/vnet/session/session_node.c
index 3413172..5ed681d 100644
--- a/src/vnet/session/session_node.c
+++ b/src/vnet/session/session_node.c
@@ -751,10 +751,12 @@
 session_update_dispatch_period (session_manager_worker_t * wrk, f64 now,
 				u32 thread_index)
 {
-  f64 sample, prev_period = wrk->dispatch_period, a = 0.8;
-
-  sample = now - wrk->last_vlib_time;
-  wrk->dispatch_period = a * sample + (1 - a) * prev_period;
+  if (wrk->last_tx_packets > 8)
+    {
+      f64 sample = now - wrk->last_vlib_time;
+      sample = (sample * wrk->last_tx_packets) / VLIB_FRAME_SIZE;
+      wrk->dispatch_period = (wrk->dispatch_period + sample) * 0.5;
+    }
   wrk->last_vlib_time = now;
 }
 
@@ -917,6 +919,7 @@
 
   _vec_len (fifo_events) = 0;
   wrk->free_event_vector = fifo_events;
+  wrk->last_tx_packets = n_tx_packets;
 
   vlib_node_increment_counter (vm, session_queue_node.index,
 			       SESSION_QUEUE_ERROR_TX, n_tx_packets);
diff --git a/src/vnet/session/transport.c b/src/vnet/session/transport.c
index 0dd9ccd..42149eb 100644
--- a/src/vnet/session/transport.c
+++ b/src/vnet/session/transport.c
@@ -529,23 +529,32 @@
 }
 
 void
-transport_connection_tx_pacer_init (transport_connection_t * tc,
-				    u32 rate_bytes_per_sec,
-				    u32 initial_bucket)
+transport_connection_tx_pacer_reset (transport_connection_t * tc,
+				     u32 rate_bytes_per_sec,
+				     u32 start_bucket, u64 time_now)
 {
-  vlib_main_t *vm = vlib_get_main ();
-  u64 time_now = vm->clib_time.last_cpu_time;
   spacer_t *pacer = &tc->pacer;
   f64 dispatch_period;
   u32 burst_size;
 
-  tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
   dispatch_period = transport_dispatch_period (tc->thread_index);
   burst_size = rate_bytes_per_sec * dispatch_period;
   spacer_update_max_burst_size (&tc->pacer, burst_size);
   spacer_set_pace_rate (&tc->pacer, rate_bytes_per_sec);
   pacer->last_update = time_now >> SPACER_CPU_TICKS_PER_PERIOD_SHIFT;
-  pacer->bucket = initial_bucket;
+  pacer->bucket = start_bucket;
+}
+
+void
+transport_connection_tx_pacer_init (transport_connection_t * tc,
+				    u32 rate_bytes_per_sec,
+				    u32 initial_bucket)
+{
+  vlib_main_t *vm = vlib_get_main ();
+  tc->flags |= TRANSPORT_CONNECTION_F_IS_TX_PACED;
+  transport_connection_tx_pacer_reset (tc, rate_bytes_per_sec,
+				       initial_bucket,
+				       vm->clib_time.last_cpu_time);
 }
 
 void
diff --git a/src/vnet/session/transport_interface.h b/src/vnet/session/transport_interface.h
index b7aa4b7..3bfed41 100644
--- a/src/vnet/session/transport_interface.h
+++ b/src/vnet/session/transport_interface.h
@@ -102,6 +102,9 @@
 void transport_update_time (f64 time_now, u8 thread_index);
 void transport_enable_disable (vlib_main_t * vm, u8 is_en);
 
+void transport_connection_tx_pacer_reset (transport_connection_t * tc,
+					  u32 rate_bytes_per_sec,
+					  u32 initial_bucket, u64 time_now);
 /**
  * Initialize tx pacer for connection
  *