vppinfra: improve clocks_per_second convergence
[vpp.git] / src / vnet / bfd / bfd_main.c
index bd2addf..a9a6d05 100644 (file)
@@ -60,7 +60,7 @@ bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
 u32
 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
 {
-  return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
+  return ((f64) clocks / bm->cpu_cps) * USEC_PER_SECOND;
 }
 
 static vlib_node_registration_t bfd_process_node;
@@ -774,7 +774,7 @@ bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
   b->current_length += sizeof (*auth);
   pkt->pkt.head.length += sizeof (*auth);
   bfd_pkt_set_auth_present (&pkt->pkt);
-  memset (auth, 0, sizeof (*auth));
+  clib_memset (auth, 0, sizeof (*auth));
   auth->type_len.type = bs->auth.curr_key->auth_type;
   /*
    * only meticulous authentication types require incrementing seq number
@@ -860,7 +860,7 @@ bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
   bfd_pkt_t *pkt = vlib_buffer_get_current (b);
   u32 bfd_length = 0;
   bfd_length = sizeof (bfd_pkt_t);
-  memset (pkt, 0, sizeof (*pkt));
+  clib_memset (pkt, 0, sizeof (*pkt));
   bfd_pkt_set_version (pkt, 1);
   bfd_pkt_set_diag_code (pkt, bs->local_diag);
   bfd_pkt_set_state (pkt, bs->local_state);
@@ -908,10 +908,9 @@ 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);
-      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);
-      memset (pkt, 0, sizeof (*pkt));
+      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;
@@ -982,7 +981,6 @@ 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);
-      memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
       bfd_init_control_frame (bm, bs, b);
       switch (bs->poll_state)
@@ -1056,6 +1054,19 @@ bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
       now + bm->wheel_inaccuracy)
     {
       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);
       /*
@@ -1315,8 +1326,8 @@ bfd_main_init (vlib_main_t * vm)
   bm->random_seed = random_default_seed ();
   bm->vlib_main = vm;
   bm->vnet_main = vnet_get_main ();
-  memset (&bm->wheel, 0, sizeof (bm->wheel));
-  bm->cpu_cps = vm->clib_time.clocks_per_second;
+  clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
+  bm->cpu_cps = (u64) 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);
@@ -1343,7 +1354,7 @@ bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
   bfd_lock (bm);
 
   pool_get (bm->sessions, result);
-  memset (result, 0, sizeof (*result));
+  clib_memset (result, 0, sizeof (*result));
   result->bs_idx = result - bm->sessions;
   result->transport = t;
   const unsigned limit = 1000;
@@ -2207,7 +2218,7 @@ bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
                auth_key - bm->auth_keys);
     }
   auth_key->auth_type = auth_type;
-  memset (auth_key->key, 0, sizeof (auth_key->key));
+  clib_memset (auth_key->key, 0, sizeof (auth_key->key));
   clib_memcpy (auth_key->key, key_data, key_len);
   return 0;
 #else
@@ -2238,7 +2249,7 @@ bfd_auth_del_key (u32 conf_key_id)
          return VNET_API_ERROR_BFD_EINUSE;
        }
       hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
-      memset (auth_key, 0, sizeof (*auth_key));
+      clib_memset (auth_key, 0, sizeof (*auth_key));
       pool_put (bm->auth_keys, auth_key);
     }
   else