bfd: refactor code to fix misc warnings
[vpp.git] / src / vnet / bfd / bfd_main.c
index ea58413..23e3f0f 100644 (file)
  * @brief BFD nodes implementation
  */
 
-#if WITH_LIBSSL > 0
-#include <openssl/sha.h>
-#endif
-
-#if __SSE4_2__
-#include <x86intrin.h>
-#endif
-
 #include <vlibmemory/api.h>
 #include <vppinfra/random.h>
 #include <vppinfra/error.h>
 #include <vnet/bfd/bfd_protocol.h>
 #include <vnet/bfd/bfd_main.h>
 #include <vlib/log.h>
+#include <vnet/crypto/crypto.h>
 
 static u64
 bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
 {
   u64 checksum = 0;
 #if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__)
-  checksum = crc32_u64 (0, discriminator);
-  checksum = crc32_u64 (checksum, expire_time);
-  checksum = crc32_u64 (checksum, secret);
+  checksum = clib_crc32c_u64 (0, discriminator);
+  checksum = clib_crc32c_u64 (checksum, expire_time);
+  checksum = clib_crc32c_u64 (checksum, secret);
 #else
   checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
 #endif
@@ -52,15 +45,24 @@ bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
 }
 
 static u64
-bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
+bfd_usec_to_nsec (u64 us)
 {
-  return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
+  return us * NSEC_PER_USEC;
 }
 
 u32
-bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
+bfd_nsec_to_usec (u64 nsec)
+{
+  return nsec / NSEC_PER_USEC;
+}
+
+always_inline u64
+bfd_time_now_nsec (vlib_main_t * vm, f64 * vm_time)
 {
-  return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
+  f64 _vm_time = vlib_time_now (vm);
+  if (vm_time)
+    *vm_time = _vm_time;
+  return _vm_time * NSEC_PER_SEC;
 }
 
 static vlib_node_registration_t bfd_process_node;
@@ -105,12 +107,12 @@ bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
   bs->remote_discr = 0;
   bs->hop_type = BFD_HOP_TYPE_SINGLE;
   bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
-  bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
-  bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
+  bs->config_desired_min_tx_nsec = bm->default_desired_min_tx_nsec;
+  bs->effective_desired_min_tx_nsec = bm->default_desired_min_tx_nsec;
   bs->remote_min_rx_usec = 1;
-  bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
+  bs->remote_min_rx_nsec = bfd_usec_to_nsec (bs->remote_min_rx_usec);
   bs->remote_min_echo_rx_usec = 0;
-  bs->remote_min_echo_rx_clocks = 0;
+  bs->remote_min_echo_rx_nsec = 0;
   bs->remote_demand = 0;
   bs->auth.remote_seq_number = 0;
   bs->auth.remote_seq_number_known = 0;
@@ -130,7 +132,7 @@ bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
 }
 
 static void
-bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
+bfd_set_state (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs,
               bfd_state_e new_state, int handling_wakeup)
 {
   if (bs->local_state != new_state)
@@ -139,7 +141,8 @@ bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
               bfd_state_string (bs->local_state),
               bfd_state_string (new_state));
       bs->local_state = new_state;
-      bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
+      bfd_on_state_change (bm, bs, bfd_time_now_nsec (vm, NULL),
+                          handling_wakeup);
     }
 }
 
@@ -169,22 +172,21 @@ bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
 }
 
 static void
-bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
+bfd_recalc_tx_interval (bfd_session_t *bs)
 {
-  bs->transmit_interval_clocks =
-    clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
+  bs->transmit_interval_nsec =
+    clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_rx_nsec);
   BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
-          BFD_CLK_PRN (bs->transmit_interval_clocks));
+          BFD_CLK_PRN (bs->transmit_interval_nsec));
 }
 
 static void
-bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
+bfd_recalc_echo_tx_interval (bfd_session_t *bs)
 {
-  bs->echo_transmit_interval_clocks =
-    clib_max (bs->effective_desired_min_tx_clocks,
-             bs->remote_min_echo_rx_clocks);
+  bs->echo_transmit_interval_nsec =
+    clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_echo_rx_nsec);
   BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
-          BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
+          BFD_CLK_PRN (bs->echo_transmit_interval_nsec));
 }
 
 static void
@@ -193,10 +195,10 @@ bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
   if (bs->local_detect_mult > 1)
     {
       /* common case - 75-100% of transmit interval */
-      bs->tx_timeout_clocks = bs->last_tx_clocks +
+      bs->tx_timeout_nsec = bs->last_tx_nsec +
        (1 - .25 * (random_f64 (&bm->random_seed))) *
-       bs->transmit_interval_clocks;
-      if (bs->tx_timeout_clocks < now)
+       bs->transmit_interval_nsec;
+      if (bs->tx_timeout_nsec < now)
        {
          /*
           * the timeout is in the past, which means that either remote
@@ -204,18 +206,18 @@ bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 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;
+                  (now - bs->tx_timeout_nsec) /
+                  bs->transmit_interval_nsec, now, bs->tx_timeout_nsec);
+         bs->tx_timeout_nsec = now;
        }
     }
   else
     {
       /* special case - 75-90% of transmit interval */
-      bs->tx_timeout_clocks = bs->last_tx_clocks +
+      bs->tx_timeout_nsec = bs->last_tx_nsec +
        (.9 - .15 * (random_f64 (&bm->random_seed))) *
-       bs->transmit_interval_clocks;
-      if (bs->tx_timeout_clocks < now)
+       bs->transmit_interval_nsec;
+      if (bs->tx_timeout_nsec < now)
        {
          /*
           * the timeout is in the past, which means that either remote
@@ -223,53 +225,53 @@ bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 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;
+                  (now - bs->tx_timeout_nsec) /
+                  bs->transmit_interval_nsec, now, bs->tx_timeout_nsec);
+         bs->tx_timeout_nsec = now;
        }
     }
-  if (bs->tx_timeout_clocks)
+  if (bs->tx_timeout_nsec)
     {
-      BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
-              bs->tx_timeout_clocks - now,
-              (bs->tx_timeout_clocks - now) / bm->cpu_cps,
-              bs->tx_timeout_clocks);
+      BFD_DBG ("Next transmit in %lu nsec/%.02fs@%lu",
+              bs->tx_timeout_nsec - now,
+              (bs->tx_timeout_nsec - now) * SEC_PER_NSEC,
+              bs->tx_timeout_nsec);
     }
 }
 
 static void
-bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
+bfd_calc_next_echo_tx (bfd_session_t *bs, u64 now)
 {
-  bs->echo_tx_timeout_clocks =
-    bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
-  if (bs->echo_tx_timeout_clocks < now)
+  bs->echo_tx_timeout_nsec =
+    bs->echo_last_tx_nsec + bs->echo_transmit_interval_nsec;
+  if (bs->echo_tx_timeout_nsec < now)
     {
       /* huh, we've missed it already, transmit now */
       BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
               "is %lu)",
-              (now - bs->echo_tx_timeout_clocks) /
-              bs->echo_transmit_interval_clocks,
-              now, bs->echo_tx_timeout_clocks);
-      bs->echo_tx_timeout_clocks = now;
+              (now - bs->echo_tx_timeout_nsec) /
+              bs->echo_transmit_interval_nsec,
+              now, bs->echo_tx_timeout_nsec);
+      bs->echo_tx_timeout_nsec = now;
     }
-  BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
-          bs->echo_tx_timeout_clocks - now,
-          (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
-          bs->echo_tx_timeout_clocks);
+  BFD_DBG ("Next echo transmit in %lu nsec/%.02fs@%lu",
+          bs->echo_tx_timeout_nsec - now,
+          (bs->echo_tx_timeout_nsec - now) * SEC_PER_NSEC,
+          bs->echo_tx_timeout_nsec);
 }
 
 static void
-bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
+bfd_recalc_detection_time (bfd_session_t *bs)
 {
   if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
     {
-      bs->detection_time_clocks =
+      bs->detection_time_nsec =
        bs->remote_detect_mult *
-       clib_max (bs->effective_required_min_rx_clocks,
-                 bs->remote_desired_min_tx_clocks);
-      BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
-              bs->detection_time_clocks,
-              bs->detection_time_clocks / bm->cpu_cps);
+       clib_max (bs->effective_required_min_rx_nsec,
+                 bs->remote_desired_min_tx_nsec);
+      BFD_DBG ("Recalculated detection time %lu nsec/%.3fs",
+              bs->detection_time_nsec,
+              bs->detection_time_nsec * SEC_PER_NSEC);
     }
 }
 
@@ -282,13 +284,13 @@ bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
   u64 tx_timeout = 0;
   if (BFD_STATE_up == bs->local_state)
     {
-      rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
+      rx_timeout = bs->last_rx_nsec + bs->detection_time_nsec;
     }
   if (BFD_STATE_up != bs->local_state ||
       (!bs->remote_demand && bs->remote_min_rx_usec) ||
       BFD_POLL_NOT_NEEDED != bs->poll_state)
     {
-      tx_timeout = bs->tx_timeout_clocks;
+      tx_timeout = bs->tx_timeout_nsec;
     }
   if (tx_timeout && rx_timeout)
     {
@@ -302,45 +304,57 @@ bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
     {
       next = rx_timeout;
     }
-  if (bs->echo && next > bs->echo_tx_timeout_clocks)
+  if (bs->echo && next > bs->echo_tx_timeout_nsec)
     {
-      next = bs->echo_tx_timeout_clocks;
+      next = bs->echo_tx_timeout_nsec;
     }
   BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
           "next=%s",
-          bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
+          bs->bs_idx, tx_timeout, bs->echo_tx_timeout_nsec, rx_timeout,
           next == tx_timeout
-          ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
-  /* sometimes the wheel expires an event a bit sooner than requested, account
-     for that here */
-  if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
-              next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
+          ? "tx" : (next == bs->echo_tx_timeout_nsec ? "echo tx" : "rx"));
+  if (next)
     {
       int send_signal = 0;
-      bs->wheel_time_clocks = next;
-      BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
-              "future), %u);",
-              &bm->wheel, bs->wheel_time_clocks,
-              (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
-              (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
-              bm->cpu_cps, bs->bs_idx);
+      bs->event_time_nsec = next;
+      /* add extra tick if it's not even */
+      u32 wheel_time_ticks =
+       (bs->event_time_nsec - now) / bm->nsec_per_tw_tick +
+       ((bs->event_time_nsec - now) % bm->nsec_per_tw_tick != 0);
+      BFD_DBG ("event_time_nsec %lu (%lu nsec/%.3fs in future) -> "
+              "wheel_time_ticks %u", bs->event_time_nsec,
+              bs->event_time_nsec - now,
+              (bs->event_time_nsec - now) * SEC_PER_NSEC, wheel_time_ticks);
+      wheel_time_ticks = wheel_time_ticks ? wheel_time_ticks : 1;
       bfd_lock (bm);
-      timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
+      if (bs->tw_id)
+       {
+         TW (tw_timer_update) (&bm->wheel, bs->tw_id, wheel_time_ticks);
+         BFD_DBG ("tw_timer_update(%p, %u, %lu);", &bm->wheel, bs->tw_id,
+                  wheel_time_ticks);
+       }
+      else
+       {
+         bs->tw_id =
+           TW (tw_timer_start) (&bm->wheel, bs->bs_idx, 0, wheel_time_ticks);
+         BFD_DBG ("tw_timer_start(%p, %u, 0, %lu) == %u;", &bm->wheel,
+                  bs->bs_idx, wheel_time_ticks);
+       }
 
       if (!handling_wakeup)
        {
 
          /* Send only if it is earlier than current awaited wakeup time */
          send_signal =
-           (bs->wheel_time_clocks < bm->bfd_process_next_wakeup_clocks) &&
+           (bs->event_time_nsec < bm->bfd_process_next_wakeup_nsec) &&
            /*
             * If the wake-up time is within 2x the delay of the event propagation delay,
             * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
             * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
             * due to double scheduling of the node on the pending list.
             */
-           (bm->bfd_process_next_wakeup_clocks - bs->wheel_time_clocks >
-            2 * bm->bfd_process_wakeup_event_delay_clocks) &&
+           (bm->bfd_process_next_wakeup_nsec - bs->event_time_nsec >
+            2 * bm->bfd_process_wakeup_event_delay_nsec) &&
            /* Must be no events in flight to send an event */
            (!bm->bfd_process_wakeup_events_in_flight);
 
@@ -348,7 +362,7 @@ bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
          if (send_signal)
            {
              bm->bfd_process_wakeup_events_in_flight++;
-             bm->bfd_process_wakeup_event_start_clocks = now;
+             bm->bfd_process_wakeup_event_start_nsec = now;
            }
        }
       bfd_unlock (bm);
@@ -366,57 +380,53 @@ bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
 static void
 bfd_set_effective_desired_min_tx (bfd_main_t * bm,
                                  bfd_session_t * bs, u64 now,
-                                 u64 desired_min_tx_clocks)
+                                 u64 desired_min_tx_nsec)
 {
-  bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
+  bs->effective_desired_min_tx_nsec = desired_min_tx_nsec;
   BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
-          BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
-  bfd_recalc_detection_time (bm, bs);
-  bfd_recalc_tx_interval (bm, bs);
-  bfd_recalc_echo_tx_interval (bm, bs);
+          BFD_CLK_PRN (bs->effective_desired_min_tx_nsec));
+  bfd_recalc_detection_time (bs);
+  bfd_recalc_tx_interval (bs);
+  bfd_recalc_echo_tx_interval (bs);
   bfd_calc_next_tx (bm, bs, now);
 }
 
 static void
-bfd_set_effective_required_min_rx (bfd_main_t * bm,
-                                  bfd_session_t * bs,
-                                  u64 required_min_rx_clocks)
+bfd_set_effective_required_min_rx (bfd_session_t *bs, u64 required_min_rx_nsec)
 {
-  bs->effective_required_min_rx_clocks = required_min_rx_clocks;
+  bs->effective_required_min_rx_nsec = required_min_rx_nsec;
   BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
-          BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
-  bfd_recalc_detection_time (bm, bs);
+          BFD_CLK_PRN (bs->effective_required_min_rx_nsec));
+  bfd_recalc_detection_time (bs);
 }
 
 static void
-bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
-                               u64 now, u32 remote_required_min_rx_usec)
+bfd_set_remote_required_min_rx (bfd_session_t *bs,
+                               u32 remote_required_min_rx_usec)
 {
   if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
     {
       bs->remote_min_rx_usec = remote_required_min_rx_usec;
-      bs->remote_min_rx_clocks =
-       bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
+      bs->remote_min_rx_nsec = bfd_usec_to_nsec (remote_required_min_rx_usec);
       BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
-              BFD_CLK_PRN (bs->remote_min_rx_clocks));
-      bfd_recalc_detection_time (bm, bs);
-      bfd_recalc_tx_interval (bm, bs);
+              BFD_CLK_PRN (bs->remote_min_rx_nsec));
+      bfd_recalc_detection_time (bs);
+      bfd_recalc_tx_interval (bs);
     }
 }
 
 static void
-bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
-                                    u64 now,
+bfd_set_remote_required_min_echo_rx (bfd_session_t *bs,
                                     u32 remote_required_min_echo_rx_usec)
 {
   if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
     {
       bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
-      bs->remote_min_echo_rx_clocks =
-       bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
+      bs->remote_min_echo_rx_nsec =
+       bfd_usec_to_nsec (bs->remote_min_echo_rx_usec);
       BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
-              BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
-      bfd_recalc_echo_tx_interval (bm, bs);
+              BFD_CLK_PRN (bs->remote_min_echo_rx_nsec));
+      bfd_recalc_echo_tx_interval (bs);
     }
 }
 
@@ -437,25 +447,24 @@ bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
   BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
   vlib_log_info (bm->log_class, "start BFD session: %U",
                 format_bfd_session_brief, bs);
-  bfd_set_effective_required_min_rx (bm, bs,
-                                    bs->config_required_min_rx_clocks);
-  bfd_recalc_tx_interval (bm, bs);
+  bfd_set_effective_required_min_rx (bs, bs->config_required_min_rx_nsec);
+  bfd_recalc_tx_interval (bs);
   vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
                             BFD_EVENT_NEW_SESSION, bs->bs_idx);
   bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
 }
 
 void
-bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
+bfd_session_set_flags (vlib_main_t * vm, bfd_session_t * bs, u8 admin_up_down)
 {
   bfd_main_t *bm = &bfd_main;
-  u64 now = clib_cpu_time_now ();
+  u64 now = bfd_time_now_nsec (vm, NULL);
   if (admin_up_down)
     {
       BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
       vlib_log_info (bm->log_class, "set session admin-up: %U",
                     format_bfd_session_brief, bs);
-      bfd_set_state (bm, bs, BFD_STATE_down, 0);
+      bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
       bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
       bfd_calc_next_tx (bm, bs, now);
       bfd_set_timer (bm, bs, now, 0);
@@ -466,7 +475,7 @@ bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
       vlib_log_info (bm->log_class, "set session admin-down: %U",
                     format_bfd_session_brief, bs);
       bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
-      bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
+      bfd_set_state (vm, bm, bs, BFD_STATE_admin_down, 0);
       bfd_calc_next_tx (bm, bs, now);
       bfd_set_timer (bm, bs, now, 0);
     }
@@ -637,35 +646,33 @@ bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
       bs->echo = 0;
       bfd_set_effective_desired_min_tx (bm, bs, now,
                                        clib_max
-                                       (bs->config_desired_min_tx_clocks,
-                                        bm->default_desired_min_tx_clocks));
-      bfd_set_effective_required_min_rx (bm, bs,
-                                        bs->config_required_min_rx_clocks);
+                                       (bs->config_desired_min_tx_nsec,
+                                        bm->default_desired_min_tx_nsec));
+      bfd_set_effective_required_min_rx (bs, bs->config_required_min_rx_nsec);
       bfd_set_timer (bm, bs, now, handling_wakeup);
       break;
     case BFD_STATE_down:
       bs->echo = 0;
       bfd_set_effective_desired_min_tx (bm, bs, now,
                                        clib_max
-                                       (bs->config_desired_min_tx_clocks,
-                                        bm->default_desired_min_tx_clocks));
-      bfd_set_effective_required_min_rx (bm, bs,
-                                        bs->config_required_min_rx_clocks);
+                                       (bs->config_desired_min_tx_nsec,
+                                        bm->default_desired_min_tx_nsec));
+      bfd_set_effective_required_min_rx (bs, bs->config_required_min_rx_nsec);
       bfd_set_timer (bm, bs, now, handling_wakeup);
       break;
     case BFD_STATE_init:
       bs->echo = 0;
       bfd_set_effective_desired_min_tx (bm, bs, now,
-                                       bs->config_desired_min_tx_clocks);
+                                       bs->config_desired_min_tx_nsec);
       bfd_set_timer (bm, bs, now, handling_wakeup);
       break;
     case BFD_STATE_up:
       bfd_set_effective_desired_min_tx (bm, bs, now,
-                                       bs->config_desired_min_tx_clocks);
+                                       bs->config_desired_min_tx_nsec);
       if (BFD_POLL_NOT_NEEDED == bs->poll_state)
        {
-         bfd_set_effective_required_min_rx (bm, bs,
-                                            bs->config_required_min_rx_clocks);
+         bfd_set_effective_required_min_rx (bs,
+                                            bs->config_required_min_rx_nsec);
        }
       bfd_set_timer (bm, bs, now, handling_wakeup);
       break;
@@ -682,19 +689,18 @@ bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
 }
 
 static void
-bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
-                     bfd_main_t * bm, bfd_session_t * bs, u64 now)
+bfd_on_config_change (bfd_main_t *bm, bfd_session_t *bs, u64 now)
 {
   /*
    * if remote demand mode is set and we need to do a poll, set the next
    * timeout so that the session wakes up immediately
    */
   if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
-      bs->poll_state_start_or_timeout_clocks < now)
+      bs->poll_state_start_or_timeout_nsec < now)
     {
-      bs->tx_timeout_clocks = now;
+      bs->tx_timeout_nsec = now;
     }
-  bfd_recalc_detection_time (bm, bs);
+  bfd_recalc_detection_time (bs);
   bfd_set_timer (bm, bs, now, 0);
 }
 
@@ -765,9 +771,9 @@ bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
   return 0;
 }
 
-#if WITH_LIBSSL > 0
 static void
-bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
+bfd_add_sha1_auth_section (vlib_main_t *vm, vlib_buffer_t *b,
+                          bfd_session_t *bs)
 {
   bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
   bfd_auth_sha1_t *auth = &pkt->sha1_auth;
@@ -791,14 +797,19 @@ bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
   clib_memcpy (auth->hash, bs->auth.curr_key->key,
               sizeof (bs->auth.curr_key->key));
   unsigned char hash[sizeof (auth->hash)];
-  SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
+
+  vnet_crypto_op_t op;
+  vnet_crypto_op_init (&op, VNET_CRYPTO_OP_SHA1_HASH);
+  op.src = (u8 *) pkt;
+  op.len = sizeof (*pkt);
+  op.digest = hash;
+  vnet_crypto_process_ops (vm, &op, 1);
   BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
   clib_memcpy (auth->hash, hash, sizeof (hash));
 }
-#endif
 
 static void
-bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
+bfd_add_auth_section (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t *bs)
 {
   bfd_main_t *bm = &bfd_main;
   if (bs->auth.curr_key)
@@ -817,21 +828,11 @@ bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
                         "internal error, unexpected BFD auth type '%d'",
                         auth_type);
          break;
-#if WITH_LIBSSL > 0
        case BFD_AUTH_TYPE_keyed_sha1:
          /* fallthrough */
        case BFD_AUTH_TYPE_meticulous_keyed_sha1:
-         bfd_add_sha1_auth_section (b, bs);
+         bfd_add_sha1_auth_section (vm, b, bs);
          break;
-#else
-       case BFD_AUTH_TYPE_keyed_sha1:
-         /* fallthrough */
-       case BFD_AUTH_TYPE_meticulous_keyed_sha1:
-         vlib_log_crit (bm->log_class,
-                        "internal error, unexpected BFD auth type '%d'",
-                        auth_type);
-         break;
-#endif
        }
     }
 }
@@ -854,8 +855,7 @@ bfd_is_echo_possible (bfd_session_t * bs)
 }
 
 static void
-bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
-                       vlib_buffer_t * b)
+bfd_init_control_frame (bfd_session_t *bs, vlib_buffer_t *b)
 {
   bfd_pkt_t *pkt = vlib_buffer_get_current (b);
   u32 bfd_length = 0;
@@ -872,8 +872,8 @@ bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
   if (bs->echo)
     {
       pkt->req_min_rx =
-       clib_host_to_net_u32 (bfd_clocks_to_usec
-                             (bm, bs->effective_required_min_rx_clocks));
+       clib_host_to_net_u32 (bfd_nsec_to_usec
+                             (bs->effective_required_min_rx_nsec));
     }
   else
     {
@@ -885,8 +885,7 @@ bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
 }
 
 static void
-bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
-              bfd_main_t * bm, bfd_session_t * bs, u64 now)
+bfd_send_echo (vlib_main_t *vm, bfd_main_t *bm, bfd_session_t *bs, u64 now)
 {
   if (!bfd_is_echo_possible (bs))
     {
@@ -894,10 +893,7 @@ bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
       bs->echo = 0;
       return;
     }
-  /* sometimes the wheel expires an event a bit sooner than requested,
-     account
-     for that here */
-  if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
+  if (now >= bs->echo_tx_timeout_nsec)
     {
       BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
       u32 bi;
@@ -908,15 +904,13 @@ bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
        }
       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
       ASSERT (b->current_data == 0);
-      clib_memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
-      VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
       bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
       clib_memset (pkt, 0, sizeof (*pkt));
       pkt->discriminator = bs->local_discr;
-      pkt->expire_time_clocks =
-       now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
+      pkt->expire_time_nsec =
+       now + bs->echo_transmit_interval_nsec * bs->local_detect_mult;
       pkt->checksum =
-       bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
+       bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec,
                                bs->echo_secret);
       b->current_length = sizeof (*pkt);
       if (!bfd_echo_add_transport_layer (vm, bi, bs))
@@ -933,20 +927,19 @@ bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
          vlib_buffer_free_one (vm, bi);
          return;
        }
-      bs->echo_last_tx_clocks = now;
-      bfd_calc_next_echo_tx (bm, bs, now);
+      bs->echo_last_tx_nsec = now;
+      bfd_calc_next_echo_tx (bs, now);
     }
   else
     {
       BFD_DBG
        ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
-        now, bs->echo_tx_timeout_clocks);
+        now, bs->echo_tx_timeout_nsec);
     }
 }
 
 static void
-bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
-                  bfd_main_t * bm, bfd_session_t * bs, u64 now)
+bfd_send_periodic (vlib_main_t *vm, bfd_main_t *bm, bfd_session_t *bs, u64 now)
 {
   if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
     {
@@ -966,11 +959,7 @@ bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
       BFD_DBG ("Remote demand is set, not sending periodic control frame");
       return;
     }
-  /*
-   * sometimes the wheel expires an event a bit sooner than requested, account
-   * for that here
-   */
-  if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
+  if (now >= bs->tx_timeout_nsec)
     {
       BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
               bs);
@@ -982,21 +971,19 @@ bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
        }
       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
       ASSERT (b->current_data == 0);
-      clib_memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
-      VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
-      bfd_init_control_frame (bm, bs, b);
+      bfd_init_control_frame (bs, b);
       switch (bs->poll_state)
        {
        case BFD_POLL_NEEDED:
-         if (now < bs->poll_state_start_or_timeout_clocks)
+         if (now < bs->poll_state_start_or_timeout_nsec)
            {
-             BFD_DBG ("Cannot start a poll sequence yet, need to wait "
-                      "for " BFD_CLK_FMT,
-                      BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
+             BFD_DBG ("Cannot start a poll sequence yet, need to wait for "
+                      BFD_CLK_FMT,
+                      BFD_CLK_PRN (bs->poll_state_start_or_timeout_nsec -
                                    now));
              break;
            }
-         bs->poll_state_start_or_timeout_clocks = now;
+         bs->poll_state_start_or_timeout_nsec = now;
          bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
          /* fallthrough */
        case BFD_POLL_IN_PROGRESS:
@@ -1008,35 +995,34 @@ bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
          /* fallthrough */
          break;
        }
-      bfd_add_auth_section (b, bs);
+      bfd_add_auth_section (vm, b, bs);
       bfd_add_transport_layer (vm, bi, bs);
       if (!bfd_transport_control_frame (vm, bi, bs))
        {
          vlib_buffer_free_one (vm, bi);
        }
-      bs->last_tx_clocks = now;
+      bs->last_tx_nsec = now;
       bfd_calc_next_tx (bm, bs, now);
     }
   else
     {
       BFD_DBG
        ("No need to send control frame now, now is %lu, tx_timeout is %lu",
-        now, bs->tx_timeout_clocks);
+        now, bs->tx_timeout_nsec);
     }
 }
 
 void
-bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
-                             bfd_main_t * bm, bfd_session_t * bs,
-                             int is_local)
+bfd_init_final_control_frame (vlib_main_t *vm, vlib_buffer_t *b,
+                             bfd_session_t *bs)
 {
   BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
-  bfd_init_control_frame (bm, bs, b);
+  bfd_init_control_frame (bs, b);
   bfd_pkt_set_final (vlib_buffer_get_current (b));
-  bfd_add_auth_section (b, bs);
+  bfd_add_auth_section (vm, b, bs);
   u32 bi = vlib_get_buffer_index (vm, b);
   bfd_add_transport_layer (vm, bi, bs);
-  bs->last_tx_clocks = clib_cpu_time_now ();
+  bs->last_tx_nsec = bfd_time_now_nsec (vm, NULL);
   /*
    * RFC allows to include changes in final frame, so if there were any
    * pending, we already did that, thus we can clear any pending poll needs
@@ -1045,19 +1031,27 @@ bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
 }
 
 static void
-bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
-                     int handling_wakeup)
+bfd_check_rx_timeout (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs,
+                     u64 now, int handling_wakeup)
 {
-  /*
-   * sometimes the wheel expires an event a bit sooner than requested, account
-   * for that here
-   */
-  if (bs->last_rx_clocks + bs->detection_time_clocks <=
-      now + bm->wheel_inaccuracy)
+  if (bs->last_rx_nsec + bs->detection_time_nsec <= now)
     {
       BFD_DBG ("Rx timeout, session goes down");
+      /*
+       * RFC 5880 6.8.1. State Variables
+
+       * bfd.RemoteDiscr
+
+       * The remote discriminator for this BFD session.  This is the
+       * discriminator chosen by the remote system, and is totally opaque
+       * to the local system.  This MUST be initialized to zero.  If a
+       * period of a Detection Time passes without the receipt of a valid,
+       * authenticated BFD packet from the remote system, this variable
+       * MUST be set to zero.
+       */
+      bs->remote_discr = 0;
       bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
-      bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
+      bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup);
       /*
        * If the remote system does not receive any
        * BFD Control packets for a Detection Time, it SHOULD reset
@@ -1065,56 +1059,52 @@ bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
        * since it is no longer required to maintain previous session state)
        * and then can transmit at its own rate.
        */
-      bfd_set_remote_required_min_rx (bm, bs, now, 1);
+      bfd_set_remote_required_min_rx (bs, 1);
     }
-  else if (bs->echo &&
-          bs->echo_last_rx_clocks +
-          bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
-          now + bm->wheel_inaccuracy)
+  else if (bs->echo
+          && bs->echo_last_rx_nsec +
+          bs->echo_transmit_interval_nsec * bs->local_detect_mult <= now)
     {
       BFD_DBG ("Echo rx timeout, session goes down");
       bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
-      bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
+      bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup);
     }
 }
 
 void
-bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
-               bfd_session_t * bs, u64 now)
+bfd_on_timeout (vlib_main_t *vm, bfd_main_t *bm, bfd_session_t *bs, u64 now)
 {
   BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
   switch (bs->local_state)
     {
     case BFD_STATE_admin_down:
-      bfd_send_periodic (vm, rt, bm, bs, now);
-      break;
+      /* fallthrough */
     case BFD_STATE_down:
-      bfd_send_periodic (vm, rt, bm, bs, now);
+      bfd_send_periodic (vm, bm, bs, now);
       break;
     case BFD_STATE_init:
-      bfd_check_rx_timeout (bm, bs, now, 1);
-      bfd_send_periodic (vm, rt, bm, bs, now);
+      bfd_check_rx_timeout (vm, bm, bs, now, 1);
+      bfd_send_periodic (vm, bm, bs, now);
       break;
     case BFD_STATE_up:
-      bfd_check_rx_timeout (bm, bs, now, 1);
+      bfd_check_rx_timeout (vm, bm, bs, now, 1);
       if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
          bfd_is_echo_possible (bs))
        {
          /* switch on echo function as main detection method now */
          BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
          bs->echo = 1;
-         bs->echo_last_rx_clocks = now;
-         bs->echo_tx_timeout_clocks = now;
-         bfd_set_effective_required_min_rx (bm, bs,
-                                            clib_max
-                                            (bm->min_required_min_rx_while_echo_clocks,
-                                             bs->config_required_min_rx_clocks));
+         bs->echo_last_rx_nsec = now;
+         bs->echo_tx_timeout_nsec = now;
+         bfd_set_effective_required_min_rx (
+           bs, clib_max (bm->min_required_min_rx_while_echo_nsec,
+                         bs->config_required_min_rx_nsec));
          bfd_set_poll_state (bs, BFD_POLL_NEEDED);
        }
-      bfd_send_periodic (vm, rt, bm, bs, now);
+      bfd_send_periodic (vm, bm, bs, now);
       if (bs->echo)
        {
-         bfd_send_echo (vm, rt, bm, bs, now);
+         bfd_send_echo (vm, bm, bs, now);
        }
       break;
     }
@@ -1124,7 +1114,8 @@ bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
  * bfd process node function
  */
 static uword
-bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
+bfd_process (vlib_main_t *vm, CLIB_UNUSED (vlib_node_runtime_t *rt),
+            CLIB_UNUSED (vlib_frame_t *f))
 {
   bfd_main_t *bm = &bfd_main;
   u32 *expired = 0;
@@ -1135,36 +1126,51 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
 
   while (1)
     {
-      u64 now = clib_cpu_time_now ();
+      f64 vm_time;
+      u64 now = bfd_time_now_nsec (vm, &vm_time);
+      BFD_DBG ("wakeup, now is %llunsec, vlib_time_now() is %.9f", now,
+              vm_time);
       bfd_lock (bm);
-      u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
-      BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
-              &bm->wheel, next_expire);
-      bm->bfd_process_next_wakeup_clocks =
-       (i64) next_expire >= 0 ? next_expire : ~0;
-      bfd_unlock (bm);
-      if ((i64) next_expire < 0)
+      f64 timeout;
+      if (pool_elts (bm->sessions))
        {
-         BFD_DBG ("wait for event without timeout");
-         (void) vlib_process_wait_for_event (vm);
-         event_type = vlib_process_get_events (vm, &event_data);
-       }
-      else
-       {
-         f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
-         BFD_DBG ("wait for event with timeout %.02f", timeout);
-         if (timeout < 0)
+         u32 first_expires_in_ticks =
+           TW (tw_timer_first_expires_in_ticks) (&bm->wheel);
+         if (!first_expires_in_ticks)
            {
-             BFD_DBG ("negative timeout, already expired, skipping wait");
-             event_type = ~0;
+             BFD_DBG
+               ("tw_timer_first_expires_in_ticks(%p) returns 0ticks",
+                &bm->wheel);
+             timeout = bm->wheel.next_run_time - vm_time;
+             BFD_DBG ("wheel.next_run_time is %.9f",
+                      bm->wheel.next_run_time);
+             u64 next_expire_nsec = now + timeout * SEC_PER_NSEC;
+             bm->bfd_process_next_wakeup_nsec = next_expire_nsec;
+             bfd_unlock (bm);
            }
          else
            {
-             (void) vlib_process_wait_for_event_or_clock (vm, timeout);
-             event_type = vlib_process_get_events (vm, &event_data);
+             BFD_DBG ("tw_timer_first_expires_in_ticks(%p) returns %luticks",
+                      &bm->wheel, first_expires_in_ticks);
+             u64 next_expire_nsec =
+               now + first_expires_in_ticks * bm->nsec_per_tw_tick;
+             bm->bfd_process_next_wakeup_nsec = next_expire_nsec;
+             bfd_unlock (bm);
+             ASSERT (next_expire_nsec - now <= UINT32_MAX);
+             // cast to u32 to avoid warning
+             timeout = (u32) (next_expire_nsec - now) * SEC_PER_NSEC;
            }
+         BFD_DBG ("vlib_process_wait_for_event_or_clock(vm, %.09f)",
+                  timeout);
+         (void) vlib_process_wait_for_event_or_clock (vm, timeout);
        }
-      now = clib_cpu_time_now ();
+      else
+       {
+         bfd_unlock (bm);
+         (void) vlib_process_wait_for_event (vm);
+       }
+      event_type = vlib_process_get_events (vm, &event_data);
+      now = bfd_time_now_nsec (vm, &vm_time);
       uword *session_index;
       switch (event_type)
        {
@@ -1172,9 +1178,10 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
          /* nothing to do here */
          break;
        case BFD_EVENT_RESCHEDULE:
+         BFD_DBG ("reschedule event");
          bfd_lock (bm);
-         bm->bfd_process_wakeup_event_delay_clocks =
-           now - bm->bfd_process_wakeup_event_start_clocks;
+         bm->bfd_process_wakeup_event_delay_nsec =
+           now - bm->bfd_process_wakeup_event_start_nsec;
          bm->bfd_process_wakeup_events_in_flight--;
          bfd_unlock (bm);
          /* nothing to do here - reschedule is done automatically after
@@ -1188,7 +1195,7 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
              {
                bfd_session_t *bs =
                  pool_elt_at_index (bm->sessions, *session_index);
-               bfd_send_periodic (vm, rt, bm, bs, now);
+               bfd_send_periodic (vm, bm, bs, now);
                bfd_set_timer (bm, bs, now, 1);
              }
            else
@@ -1207,7 +1214,7 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
              {
                bfd_session_t *bs =
                  pool_elt_at_index (bm->sessions, *session_index);
-               bfd_on_config_change (vm, rt, bm, bs, now);
+               bfd_on_config_change (bm, bs, now);
              }
            else
              {
@@ -1221,11 +1228,10 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
          vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
          break;
        }
-      BFD_DBG ("advancing wheel, now is %lu", now);
-      BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
-              expired);
+      BFD_DBG ("tw_timer_expire_timers_vec(%p, %.04f);", &bm->wheel, vm_time);
       bfd_lock (bm);
-      expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
+      expired =
+       TW (tw_timer_expire_timers_vec) (&bm->wheel, vm_time, expired);
       BFD_DBG ("Expired %d elements", vec_len (expired));
       u32 *p = NULL;
       vec_foreach (p, expired)
@@ -1234,7 +1240,8 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
        if (!pool_is_free_index (bm->sessions, bs_idx))
          {
            bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
-           bfd_on_timeout (vm, rt, bm, bs, now);
+           bs->tw_id = 0;      /* timer is gone because it expired */
+           bfd_on_timeout (vm, bm, bs, now);
            bfd_set_timer (bm, bs, now, 1);
          }
       }
@@ -1255,7 +1262,6 @@ bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
 /*
  * bfd process node declaration
  */
-/* *INDENT-OFF* */
 VLIB_REGISTER_NODE (bfd_process_node, static) = {
   .function = bfd_process,
   .type = VLIB_NODE_TYPE_PROCESS,
@@ -1263,10 +1269,10 @@ VLIB_REGISTER_NODE (bfd_process_node, static) = {
   .n_next_nodes = 0,
   .next_nodes = {},
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
-bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
+bfd_sw_interface_up_down (CLIB_UNUSED (vnet_main_t *vnm),
+                         CLIB_UNUSED (u32 sw_if_index), u32 flags)
 {
   // bfd_main_t *bm = &bfd_main;
   // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
@@ -1280,7 +1286,8 @@ bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
 
 static clib_error_t *
-bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
+bfd_hw_interface_up_down (CLIB_UNUSED (vnet_main_t *vnm),
+                         CLIB_UNUSED (u32 hw_if_index), u32 flags)
 {
   // bfd_main_t *bm = &bfd_main;
   if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
@@ -1316,15 +1323,14 @@ bfd_main_init (vlib_main_t * vm)
   bm->vlib_main = vm;
   bm->vnet_main = vnet_get_main ();
   clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
-  bm->cpu_cps = vm->clib_time.clocks_per_second;
-  BFD_DBG ("cps is %.2f", bm->cpu_cps);
-  bm->default_desired_min_tx_clocks =
-    bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
-  bm->min_required_min_rx_while_echo_clocks =
-    bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
-  const u64 now = clib_cpu_time_now ();
-  timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
-  bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
+  bm->nsec_per_tw_tick = (f64) NSEC_PER_SEC / BFD_TW_TPS;
+  bm->default_desired_min_tx_nsec =
+    bfd_usec_to_nsec (BFD_DEFAULT_DESIRED_MIN_TX_USEC);
+  bm->min_required_min_rx_while_echo_nsec =
+    bfd_usec_to_nsec (BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
+  BFD_DBG ("tw_timer_wheel_init(%p, %p, %.04f, %u)", &bm->wheel, NULL,
+          1.00 / BFD_TW_TPS, ~0);
+  TW (tw_timer_wheel_init) (&bm->wheel, NULL, 1.00 / BFD_TW_TPS, ~0);
   bm->log_class = vlib_log_register_class ("bfd", 0);
   vlib_log_debug (bm->log_class, "initialized");
   bm->owner_thread_index = ~0;
@@ -1497,7 +1503,7 @@ bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
 }
 
 static int
-bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
+bfd_verify_pkt_auth_seq_num (vlib_main_t * vm, bfd_session_t * bs,
                             u32 received_seq_num, int is_meticulous)
 {
   /*
@@ -1506,13 +1512,12 @@ bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
    * This variable MUST be set to zero after no packets have been
    * received on this session for at least twice the Detection Time.
    */
-  u64 now = clib_cpu_time_now ();
-  if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
+  u64 now = bfd_time_now_nsec (vm, NULL);
+  if (now - bs->last_rx_nsec > bs->detection_time_nsec * 2)
     {
-      BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
-              "detection_time=%u clocks, resetting remote_seq_number_known "
-              "flag",
-              now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
+      BFD_DBG ("BFD peer unresponsive for %lu nsec, which is > 2 * "
+              "detection_time=%u nsec, resetting remote_seq_number_known "
+              "flag", now - bs->last_rx_nsec, bs->detection_time_nsec * 2);
       bs->auth.remote_seq_number_known = 0;
     }
   if (bs->auth.remote_seq_number_known)
@@ -1565,14 +1570,13 @@ bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
 }
 
 static int
-bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
-                             bfd_session_t * bs, u8 bfd_key_id,
-                             bfd_auth_key_t * auth_key)
+bfd_verify_pkt_auth_key_sha1 (vlib_main_t *vm, const bfd_pkt_t *pkt,
+                             u32 pkt_size, CLIB_UNUSED (bfd_session_t *bs),
+                             u8 bfd_key_id, bfd_auth_key_t *auth_key)
 {
   ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
          auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
 
-  u8 result[SHA_DIGEST_LENGTH];
   bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
   if (pkt_size < sizeof (*with_common))
     {
@@ -1607,42 +1611,40 @@ bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
         auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
       return 0;
     }
-  SHA_CTX ctx;
-  if (!SHA1_Init (&ctx))
-    {
-      BFD_ERR ("SHA1_Init failed");
-      return 0;
-    }
-  /* ignore last 20 bytes - use the actual key data instead pkt data */
-  if (!SHA1_Update (&ctx, with_sha1,
-                   sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
-    {
-      BFD_ERR ("SHA1_Update failed");
-      return 0;
-    }
-  if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
-    {
-      BFD_ERR ("SHA1_Update failed");
-      return 0;
-    }
-  if (!SHA1_Final (result, &ctx))
-    {
-      BFD_ERR ("SHA1_Final failed");
-      return 0;
-    }
-  if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
-    {
+
+  u8 hash_from_packet[STRUCT_SIZE_OF (bfd_auth_sha1_t, hash)];
+  u8 calculated_hash[STRUCT_SIZE_OF (bfd_auth_sha1_t, hash)];
+  clib_memcpy (hash_from_packet, with_sha1->sha1_auth.hash,
+              sizeof (with_sha1->sha1_auth.hash));
+  clib_memcpy (with_sha1->sha1_auth.hash, auth_key->key,
+              sizeof (auth_key->key));
+  vnet_crypto_op_t op;
+  vnet_crypto_op_init (&op, VNET_CRYPTO_OP_SHA1_HASH);
+  op.src = (u8 *) with_sha1;
+  op.len = sizeof (*with_sha1);
+  op.digest = calculated_hash;
+  vnet_crypto_process_ops (vm, &op, 1);
+
+  /* Restore the modified data within the packet */
+  clib_memcpy (with_sha1->sha1_auth.hash, hash_from_packet,
+              sizeof (with_sha1->sha1_auth.hash));
+
+  if (0 ==
+      memcmp (calculated_hash, hash_from_packet, sizeof (calculated_hash)))
+    {
+      clib_memcpy (with_sha1->sha1_auth.hash, hash_from_packet,
+                  sizeof (hash_from_packet));
       return 1;
     }
   BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
-          format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
-          format_hex_bytes, result, SHA_DIGEST_LENGTH);
+          format_hex_bytes, hash_from_packet, sizeof (hash_from_packet),
+          format_hex_bytes, calculated_hash, sizeof (calculated_hash));
   return 0;
 }
 
 static int
-bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
-                        bfd_session_t * bs, u8 bfd_key_id,
+bfd_verify_pkt_auth_key (vlib_main_t * vm, const bfd_pkt_t * pkt,
+                        u32 pkt_size, bfd_session_t * bs, u8 bfd_key_id,
                         bfd_auth_key_t * auth_key)
 {
   bfd_main_t *bm = &bfd_main;
@@ -1655,41 +1657,30 @@ bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
                    bfd_auth_type_str (auth_key->auth_type));
       return 0;
     case BFD_AUTH_TYPE_simple_password:
-      vlib_log_err (bm->log_class,
-                   "internal error, not implemented, unexpected auth_type=%d:%s",
-                   auth_key->auth_type,
-                   bfd_auth_type_str (auth_key->auth_type));
-      return 0;
+      /* fallthrough */
     case BFD_AUTH_TYPE_keyed_md5:
       /* fallthrough */
     case BFD_AUTH_TYPE_meticulous_keyed_md5:
-      vlib_log_err
-       (bm->log_class,
-        "internal error, not implemented, unexpected auth_type=%d:%s",
-        auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
+      vlib_log_err (
+       bm->log_class,
+       "internal error, not implemented, unexpected auth_type=%d:%s",
+       auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
       return 0;
     case BFD_AUTH_TYPE_keyed_sha1:
       /* fallthrough */
     case BFD_AUTH_TYPE_meticulous_keyed_sha1:
-#if WITH_LIBSSL > 0
       do
        {
          const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
                                                      *) pkt)->
                                                    sha1_auth.seq_num);
-         return bfd_verify_pkt_auth_seq_num (bs, seq_num,
-                                             bfd_auth_type_is_meticulous
-                                             (auth_key->auth_type))
-           && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
-                                            auth_key);
+         return bfd_verify_pkt_auth_seq_num (
+                  vm, bs, seq_num,
+                  bfd_auth_type_is_meticulous (auth_key->auth_type)) &&
+                bfd_verify_pkt_auth_key_sha1 (vm, pkt, pkt_size, bs,
+                                              bfd_key_id, auth_key);
        }
       while (0);
-#else
-      vlib_log_err
-       (bm->log_class,
-        "internal error, attempt to use SHA1 without SSL support");
-      return 0;
-#endif
     }
   return 0;
 }
@@ -1702,7 +1693,8 @@ bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
  * @return 1 if bfd packet is valid
  */
 int
-bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
+bfd_verify_pkt_auth (vlib_main_t * vm, const bfd_pkt_t * pkt, u16 pkt_size,
+                    bfd_session_t * bs)
 {
   if (bfd_pkt_get_auth_present (pkt))
     {
@@ -1713,7 +1705,7 @@ bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
          if (bs->auth.is_delayed && bs->auth.next_key)
            {
              /* yes, switch is scheduled - make sure the auth is valid */
-             if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
+             if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
                                           bs->auth.next_bfd_key_id,
                                           bs->auth.next_key))
                {
@@ -1726,7 +1718,7 @@ bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
       else
        {
          /* yes, using authentication, verify the key */
-         if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
+         if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
                                       bs->auth.curr_bfd_key_id,
                                       bs->auth.curr_key))
            {
@@ -1739,7 +1731,7 @@ bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
              if (bs->auth.is_delayed && bs->auth.next_key)
                {
                  /* delayed switch present, verify if that key works */
-                 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
+                 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
                                               bs->auth.next_bfd_key_id,
                                               bs->auth.next_key))
                    {
@@ -1780,7 +1772,8 @@ bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
 }
 
 void
-bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
+bfd_consume_pkt (vlib_main_t * vm, bfd_main_t * bm, const bfd_pkt_t * pkt,
+                u32 bs_idx)
 {
   bfd_lock_check (bm);
 
@@ -1794,8 +1787,8 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
   bs->remote_state = bfd_pkt_get_state (pkt);
   bs->remote_demand = bfd_pkt_get_demand (pkt);
   bs->remote_diag = bfd_pkt_get_diag_code (pkt);
-  u64 now = clib_cpu_time_now ();
-  bs->last_rx_clocks = now;
+  u64 now = bfd_time_now_nsec (vm, NULL);
+  bs->last_rx_nsec = now;
   if (bfd_pkt_get_auth_present (pkt))
     {
       bfd_auth_type_e auth_type =
@@ -1829,14 +1822,12 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
          while (0);
        }
     }
-  bs->remote_desired_min_tx_clocks =
-    bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
+  bs->remote_desired_min_tx_nsec =
+    bfd_usec_to_nsec (clib_net_to_host_u32 (pkt->des_min_tx));
   bs->remote_detect_mult = pkt->head.detect_mult;
-  bfd_set_remote_required_min_rx (bm, bs, now,
-                                 clib_net_to_host_u32 (pkt->req_min_rx));
-  bfd_set_remote_required_min_echo_rx (bm, bs, now,
-                                      clib_net_to_host_u32
-                                      (pkt->req_min_echo_rx));
+  bfd_set_remote_required_min_rx (bs, clib_net_to_host_u32 (pkt->req_min_rx));
+  bfd_set_remote_required_min_echo_rx (
+    bs, clib_net_to_host_u32 (pkt->req_min_echo_rx));
   if (bfd_pkt_get_final (pkt))
     {
       if (BFD_POLL_IN_PROGRESS == bs->poll_state)
@@ -1845,10 +1836,12 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
          bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
          if (BFD_STATE_up == bs->local_state)
            {
-             bfd_set_effective_required_min_rx (bm, bs,
-                                                clib_max (bs->echo *
-                                                          bm->min_required_min_rx_while_echo_clocks,
-                                                          bs->config_required_min_rx_clocks));
+             bfd_set_effective_desired_min_tx (
+               bm, bs, now, bs->config_desired_min_tx_nsec);
+             bfd_set_effective_required_min_rx (
+               bs,
+               clib_max (bs->echo * bm->min_required_min_rx_while_echo_nsec,
+                         bs->config_required_min_rx_nsec));
            }
        }
       else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
@@ -1858,10 +1851,9 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
           * time, so calculate that here
           */
          BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
-                  BFD_CLK_PRN (now -
-                               bs->poll_state_start_or_timeout_clocks));
-         bs->poll_state_start_or_timeout_clocks =
-           now + (now - bs->poll_state_start_or_timeout_clocks);
+                  BFD_CLK_PRN (now - bs->poll_state_start_or_timeout_nsec));
+         bs->poll_state_start_or_timeout_nsec =
+           now + (now - bs->poll_state_start_or_timeout_nsec);
          BFD_DBG
            ("Poll sequence terminated, but another is needed, bs_idx=%u",
             bs->bs_idx);
@@ -1879,19 +1871,19 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
   if (BFD_STATE_admin_down == bs->remote_state)
     {
       bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
-      bfd_set_state (bm, bs, BFD_STATE_down, 0);
+      bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
     }
   else if (BFD_STATE_down == bs->local_state)
     {
       if (BFD_STATE_down == bs->remote_state)
        {
          bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
-         bfd_set_state (bm, bs, BFD_STATE_init, 0);
+         bfd_set_state (vm, bm, bs, BFD_STATE_init, 0);
        }
       else if (BFD_STATE_init == bs->remote_state)
        {
          bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
-         bfd_set_state (bm, bs, BFD_STATE_up, 0);
+         bfd_set_state (vm, bm, bs, BFD_STATE_up, 0);
        }
     }
   else if (BFD_STATE_init == bs->local_state)
@@ -1900,7 +1892,7 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
          BFD_STATE_init == bs->remote_state)
        {
          bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
-         bfd_set_state (bm, bs, BFD_STATE_up, 0);
+         bfd_set_state (vm, bm, bs, BFD_STATE_up, 0);
        }
     }
   else                         /* BFD_STATE_up == bs->local_state */
@@ -1908,13 +1900,13 @@ bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
       if (BFD_STATE_down == bs->remote_state)
        {
          bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
-         bfd_set_state (bm, bs, BFD_STATE_down, 0);
+         bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
        }
     }
 }
 
 int
-bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
+bfd_consume_echo_pkt (vlib_main_t * vm, bfd_main_t * bm, vlib_buffer_t * b)
 {
   bfd_echo_pkt_t *pkt = NULL;
   if (b->current_length != sizeof (*pkt))
@@ -1929,22 +1921,22 @@ bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
     }
   BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
   u64 checksum =
-    bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
+    bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec,
                            bs->echo_secret);
   if (checksum != pkt->checksum)
     {
       BFD_DBG ("Invalid echo packet, checksum mismatch");
       return 1;
     }
-  u64 now = clib_cpu_time_now ();
-  if (pkt->expire_time_clocks < now)
+  u64 now = bfd_time_now_nsec (vm, NULL);
+  if (pkt->expire_time_nsec < now)
     {
       BFD_DBG ("Stale packet received, expire time %lu < now %lu",
-              pkt->expire_time_clocks, now);
+              pkt->expire_time_nsec, now);
     }
   else
     {
-      bs->echo_last_rx_clocks = now;
+      bs->echo_last_rx_nsec = now;
     }
   return 1;
 }
@@ -1953,34 +1945,28 @@ u8 *
 format_bfd_session (u8 * s, va_list * args)
 {
   const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
-  u32 indent = format_get_indent (s) + vlib_log_get_indent ();
   s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
-             "%Ulocal-discriminator=%u remote-discriminator=%u\n"
-             "%Ulocal-diag=%s echo-active=%s\n"
-             "%Udesired-min-tx=%u required-min-rx=%u\n"
-             "%Urequired-min-echo-rx=%u detect-mult=%u\n"
-             "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
-             "%Uremote-demand=%s poll-state=%s\n"
-             "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
-             "%U      is-delayed=%s\n"
-             "%U      curr-key=%U\n"
-             "%U      next-key=%U",
+             "local-discriminator=%u remote-discriminator=%u\n"
+             "local-diag=%s echo-active=%s\n"
+             "desired-min-tx=%u required-min-rx=%u\n"
+             "required-min-echo-rx=%u detect-mult=%u\n"
+             "remote-min-rx=%u remote-min-echo-rx=%u\n"
+             "remote-demand=%s poll-state=%s\n"
+             "auth: local-seq-num=%u remote-seq-num=%u\n"
+             "      is-delayed=%s\n"
+             "      curr-key=%U\n"
+             "      next-key=%U",
              bs->bs_idx, bfd_state_string (bs->local_state),
-             bfd_state_string (bs->remote_state), format_white_space, indent,
-             bs->local_discr, bs->remote_discr, format_white_space, indent,
-             bfd_diag_code_string (bs->local_diag),
-             (bs->echo ? "yes" : "no"), format_white_space, indent,
-             bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
-             format_white_space, indent, 1, bs->local_detect_mult,
-             format_white_space, indent, bs->remote_min_rx_usec,
-             bs->remote_min_echo_rx_usec, format_white_space, indent,
+             bfd_state_string (bs->remote_state), bs->local_discr,
+             bs->remote_discr, bfd_diag_code_string (bs->local_diag),
+             (bs->echo ? "yes" : "no"), bs->config_desired_min_tx_usec,
+             bs->config_required_min_rx_usec, 1, bs->local_detect_mult,
+             bs->remote_min_rx_usec, bs->remote_min_echo_rx_usec,
              (bs->remote_demand ? "yes" : "no"),
-             bfd_poll_state_string (bs->poll_state), format_white_space,
-             indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
-             format_white_space, indent,
-             (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
-             indent, format_bfd_auth_key, bs->auth.curr_key,
-             format_white_space, indent, format_bfd_auth_key,
+             bfd_poll_state_string (bs->poll_state),
+             bs->auth.local_seq_number, bs->auth.remote_seq_number,
+             (bs->auth.is_delayed ? "yes" : "no"),
+             format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
              bs->auth.next_key);
   return s;
 }
@@ -2025,22 +2011,27 @@ bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
   bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
   if (is_delayed)
     {
-      if (bs->auth.next_key == key)
+      if (bs->auth.next_key == key && bs->auth.next_bfd_key_id == bfd_key_id)
        {
          /* already using this key, no changes required */
          return 0;
        }
-      bs->auth.next_key = key;
+      if (bs->auth.next_key != key)
+       {
+         ++key->use_count;
+         bs->auth.next_key = key;
+       }
       bs->auth.next_bfd_key_id = bfd_key_id;
       bs->auth.is_delayed = 1;
     }
   else
     {
-      if (bs->auth.curr_key == key)
+      if (bs->auth.curr_key == key && bs->auth.curr_bfd_key_id == bfd_key_id)
        {
          /* already using this key, no changes required */
          return 0;
        }
+      ++key->use_count;
       if (bs->auth.curr_key)
        {
          --bs->auth.curr_key->use_count;
@@ -2049,7 +2040,6 @@ bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
       bs->auth.curr_bfd_key_id = bfd_key_id;
       bs->auth.is_delayed = 0;
     }
-  ++key->use_count;
   BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
   vlib_log_info (bm->log_class, "session auth modified: %U",
                 format_bfd_session_brief, bs);
@@ -2060,7 +2050,6 @@ vnet_api_error_t
 bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
 {
   bfd_main_t *bm = &bfd_main;
-#if WITH_LIBSSL > 0
   if (!is_delayed)
     {
       /* not delayed - deactivate the current key right now */
@@ -2091,11 +2080,6 @@ bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
   vlib_log_info (bm->log_class, "session auth modified: %U",
                 format_bfd_session_brief, bs);
   return 0;
-#else
-  vlib_log_err (bm->log_class,
-               "SSL missing, cannot deactivate BFD authentication");
-  return VNET_API_ERROR_BFD_NOTSUPP;
-#endif
 }
 
 vnet_api_error_t
@@ -2142,11 +2126,10 @@ bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
 
       bs->local_detect_mult = detect_mult;
       bs->config_desired_min_tx_usec = desired_min_tx_usec;
-      bs->config_desired_min_tx_clocks =
-       bfd_usec_to_clocks (bm, desired_min_tx_usec);
+      bs->config_desired_min_tx_nsec = bfd_usec_to_nsec (desired_min_tx_usec);
       bs->config_required_min_rx_usec = required_min_rx_usec;
-      bs->config_required_min_rx_clocks =
-       bfd_usec_to_clocks (bm, required_min_rx_usec);
+      bs->config_required_min_rx_nsec =
+       bfd_usec_to_nsec (required_min_rx_usec);
       BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
 
       vlib_log_info (bm->log_class, "changed session params: %U",
@@ -2166,7 +2149,6 @@ bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
                  const u8 * key_data)
 {
   bfd_main_t *bm = &bfd_main;
-#if WITH_LIBSSL > 0
   bfd_auth_key_t *auth_key = NULL;
   if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
     {
@@ -2210,17 +2192,11 @@ bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
   clib_memset (auth_key->key, 0, sizeof (auth_key->key));
   clib_memcpy (auth_key->key, key_data, key_len);
   return 0;
-#else
-  vlib_log_err (bm->log_class,
-               "SSL missing, cannot manipulate authentication keys");
-  return VNET_API_ERROR_BFD_NOTSUPP;
-#endif
 }
 
 vnet_api_error_t
 bfd_auth_del_key (u32 conf_key_id)
 {
-#if WITH_LIBSSL > 0
   bfd_auth_key_t *auth_key = NULL;
   bfd_main_t *bm = &bfd_main;
   uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
@@ -2250,11 +2226,6 @@ bfd_auth_del_key (u32 conf_key_id)
       return VNET_API_ERROR_BFD_ENOENT;
     }
   return 0;
-#else
-  vlib_log_err (bm->log_class,
-               "SSL missing, cannot manipulate authentication keys");
-  return VNET_API_ERROR_BFD_NOTSUPP;
-#endif
 }
 
 bfd_main_t bfd_main;