BFD: immediately honor reduced remote_min_rx interval

Change-Id: I7f09b45c926557d2ad0e2706b38fa56ff8194a3d
Signed-off-by: Klement Sekera <ksekera@cisco.com>
diff --git a/src/vnet/bfd/bfd_main.c b/src/vnet/bfd/bfd_main.c
index e25eadf..62be184 100644
--- a/src/vnet/bfd/bfd_main.c
+++ b/src/vnet/bfd/bfd_main.c
@@ -126,38 +126,36 @@
       if (bs->local_detect_mult > 1)
 	{
 	  /* common case - 75-100% of transmit interval */
-	  bs->tx_timeout_clocks = now +
+	  bs->tx_timeout_clocks = bs->last_tx_clocks +
 	    (1 - .25 * (random_f64 (&bm->random_seed))) *
 	    bs->transmit_interval_clocks;
 	  if (bs->tx_timeout_clocks < now)
 	    {
-	      /* huh, we've missed it already, skip the missed events */
-	      const u64 missed =
-		(now - bs->tx_timeout_clocks) / bs->transmit_interval_clocks;
-	      BFD_ERR ("Missed %lu transmit events (now is %lu, calc "
-		       "tx_timeout is %lu)!",
-		       missed, now, bs->tx_timeout_clocks);
-	      bs->tx_timeout_clocks +=
-		(missed + 1) * bs->transmit_interval_clocks;
+	      /* huh, we've missed it already, transmit now */
+	      BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
+		       "tx_timeout is %lu)",
+		       (now - bs->tx_timeout_clocks) /
+		       bs->transmit_interval_clocks,
+		       now, bs->tx_timeout_clocks);
+	      bs->tx_timeout_clocks = now;
 	    }
 	}
       else
 	{
 	  /* special case - 75-90% of transmit interval */
 	  bs->tx_timeout_clocks =
-	    now +
+	    bs->last_tx_clocks +
 	    (.9 - .15 * (random_f64 (&bm->random_seed))) *
 	    bs->transmit_interval_clocks;
 	  if (bs->tx_timeout_clocks < now)
 	    {
-	      /* huh, we've missed it already, skip the missed events */
-	      const u64 missed =
-		(now - bs->tx_timeout_clocks) / bs->transmit_interval_clocks;
-	      BFD_ERR ("Missed %lu transmit events (now is %lu, calc "
-		       "tx_timeout is %lu)!",
-		       missed, now, bs->tx_timeout_clocks);
-	      bs->tx_timeout_clocks +=
-		(missed + 1) * bs->transmit_interval_clocks;
+	      /* huh, we've missed it already, transmit now */
+	      BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
+		       "tx_timeout is %lu)",
+		       (now - bs->tx_timeout_clocks) /
+		       bs->transmit_interval_clocks,
+		       now, bs->tx_timeout_clocks);
+	      bs->tx_timeout_clocks = now;
 	    }
 	}
     }
@@ -485,7 +483,7 @@
   pkt->your_disc = bs->remote_discr;
   pkt->des_min_tx = clib_host_to_net_u32 (bs->desired_min_tx_us);
   pkt->req_min_rx = clib_host_to_net_u32 (bs->required_min_rx_us);
-  pkt->req_min_echo_rx = clib_host_to_net_u32 (0);	/* FIXME */
+  pkt->req_min_echo_rx = clib_host_to_net_u32 (bs->required_min_echo_rx_us);
   b->current_length = bfd_length;
 }
 
@@ -519,6 +517,7 @@
 	}
       bfd_init_control_frame (b, bs);
       bfd_add_transport_layer (vm, b, bs);
+      bs->last_tx_clocks = now;
       bfd_calc_next_tx (bm, bs, now);
     }
   else
@@ -537,6 +536,7 @@
   bfd_init_control_frame (b, bs);
   bfd_pkt_set_final (vlib_buffer_get_current (b));
   bfd_add_transport_layer (vm, b, bs);
+  bs->last_tx_clocks = clib_cpu_time_now ();
 }
 
 static void
@@ -946,6 +946,7 @@
 		 "bfd.LocalDiag=%s, "
 		 "bfd.DesiredMinTxInterval=%u, "
 		 "bfd.RequiredMinRxInterval=%u, "
+		 "bfd.RequiredMinEchoRxInterval=%u, "
 		 "bfd.RemoteMinRxInterval=%u, "
 		 "bfd.DemandMode=%s, "
 		 "bfd.RemoteDemandMode=%s, "
@@ -954,7 +955,8 @@
 		 bfd_state_string (bs->remote_state), bs->local_discr,
 		 bs->remote_discr, bfd_diag_code_string (bs->local_diag),
 		 bs->desired_min_tx_us, bs->required_min_rx_us,
-		 bs->remote_min_rx_us, (bs->local_demand ? "yes" : "no"),
+		 bs->required_min_echo_rx_us, bs->remote_min_rx_us,
+		 (bs->local_demand ? "yes" : "no"),
 		 (bs->remote_demand ? "yes" : "no"), bs->local_detect_mult);
 }
 
diff --git a/src/vnet/bfd/bfd_main.h b/src/vnet/bfd/bfd_main.h
index c72ea92..cc82c83 100644
--- a/src/vnet/bfd/bfd_main.h
+++ b/src/vnet/bfd/bfd_main.h
@@ -75,16 +75,19 @@
   /* desired min tx interval (clocks) */
   u64 desired_min_tx_clocks;
 
-  /* required min rx interval */
+  /* required min rx interval (microseconds) */
   u32 required_min_rx_us;
 
+  /* required min echo rx interval (microseconds) */
+  u32 required_min_echo_rx_us;
+
   /* remote min rx interval (microseconds) */
   u32 remote_min_rx_us;
 
   /* remote min rx interval (clocks) */
   u64 remote_min_rx_clocks;
 
-  /* remote desired min tx interval */
+  /* remote desired min tx interval (microseconds) */
   u32 remote_desired_min_tx_us;
 
   /* 1 if in demand mode, 0 otherwise */
@@ -108,6 +111,9 @@
   /* next time at which to transmit a packet */
   u64 tx_timeout_clocks;
 
+  /* timestamp of last packet transmitted */
+  u64 last_tx_clocks;
+
   /* timestamp of last packet received */
   u64 last_rx_clocks;
 
diff --git a/src/vnet/bfd/bfd_udp.c b/src/vnet/bfd/bfd_udp.c
index 3c747d8..677f1e2 100644
--- a/src/vnet/bfd/bfd_udp.c
+++ b/src/vnet/bfd/bfd_udp.c
@@ -146,6 +146,7 @@
     }
   bs->config_desired_min_tx_us = desired_min_tx_us;
   bs->required_min_rx_us = required_min_rx_us;
+  bs->required_min_echo_rx_us = required_min_rx_us; /* FIXME */
   bs->local_detect_mult = detect_mult;
   bfd_session_start (bum->bfd_main, bs);
   return 0;