]> git.puffer.fish Git - matthieu/frr.git/commitdiff
ospfd: time: replace local helpers with monotime
authorDavid Lamparter <equinox@opensourcerouting.org>
Tue, 17 Jan 2017 21:05:56 +0000 (22:05 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Mon, 23 Jan 2017 17:26:28 +0000 (18:26 +0100)
This ditches tv_add, tv_sub, tv_cmp, etc. in favour of
monotime{,_since,_until}() which actually makes the code much more
readable in some locations.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
ospfd/ospf_dump.c
ospfd/ospf_flood.c
ospfd/ospf_lsa.c
ospfd/ospf_lsa.h
ospfd/ospf_packet.c
ospfd/ospf_spf.c
ospfd/ospf_vty.c

index 4e9797184d3ec7e352904db7a4ef219db3ceae62..6e9e59f9b7b2dfd14ad1b17ef1f77f3c067ed9f2 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <zebra.h>
 
+#include "monotime.h"
 #include "linklist.h"
 #include "thread.h"
 #include "prefix.h"
@@ -317,8 +318,8 @@ ospf_timer_dump (struct thread *t, char *buf, size_t size)
   struct timeval result;
   if (!t)
     return "inactive";
-  
-  result = tv_sub (t->u.sands, recent_relative_time());
+
+  monotime_until (&t->u.sands, &result);
   return ospf_timeval_dump (&result, buf, size);
 }
 
index 7f83ddeaae2c35e0cc2a91747b3d2be8f2dafc07..32dd8e8a9186c47f834921c8bfdd974bf24d0c38 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <zebra.h>
 
+#include "monotime.h"
 #include "linklist.h"
 #include "prefix.h"
 #include "if.h"
@@ -277,8 +278,8 @@ ospf_flood (struct ospf *ospf, struct ospf_neighbor *nbr,
                       "while local one is initial instance.");
           ; /* Accept this LSA for quick LSDB resynchronization. */
         }
-      else if (tv_cmp (tv_sub (recent_relative_time (), current->tv_recv),
-                      msec2tv (ospf->min_ls_arrival)) < 0)
+      else if (monotime_since (&current->tv_recv, NULL)
+                 < ospf->min_ls_arrival * 1000LL)
         {
           if (IS_DEBUG_OSPF_EVENT)
            zlog_debug ("LSA[Flooding]: LSA is received recently.");
index 916d4d01c9ec4816bccf87e7dd7c2190dd11e606..94575d59443d7097699a97bc4e3282f02625bf39 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <zebra.h>
 
+#include "monotime.h"
 #include "linklist.h"
 #include "prefix.h"
 #include "if.h"
@@ -62,40 +63,6 @@ get_metric (u_char *metric)
 }
 
 
-struct timeval
-tv_adjust (struct timeval a)
-{
-  while (a.tv_usec >= 1000000)
-    {
-      a.tv_usec -= 1000000;
-      a.tv_sec++;
-    }
-
-  while (a.tv_usec < 0)
-    {
-      a.tv_usec += 1000000;
-      a.tv_sec--;
-    }
-
-  return a;
-}
-
-int
-tv_ceil (struct timeval a)
-{
-  a = tv_adjust (a);
-
-  return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
-}
-
-int
-tv_floor (struct timeval a)
-{
-  a = tv_adjust (a);
-
-  return a.tv_sec;
-}
-
 struct timeval
 int2tv (int a)
 {
@@ -115,50 +82,22 @@ msec2tv (int a)
   ret.tv_sec = a/1000;
   ret.tv_usec = (a%1000) * 1000;
 
-  return tv_adjust (ret);
-}
-
-struct timeval
-tv_add (struct timeval a, struct timeval b)
-{
-  struct timeval ret;
-
-  ret.tv_sec = a.tv_sec + b.tv_sec;
-  ret.tv_usec = a.tv_usec + b.tv_usec;
-
-  return tv_adjust (ret);
-}
-
-struct timeval
-tv_sub (struct timeval a, struct timeval b)
-{
-  struct timeval ret;
-
-  ret.tv_sec = a.tv_sec - b.tv_sec;
-  ret.tv_usec = a.tv_usec - b.tv_usec;
-
-  return tv_adjust (ret);
-}
-
-int
-tv_cmp (struct timeval a, struct timeval b)
-{
-  return (a.tv_sec == b.tv_sec ?
-         a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
+  return ret;
 }
 
 int
 ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
 {
-  struct timeval delta, now;
+  struct timeval delta;
   int delay = 0;
 
-  quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
-  delta = tv_sub (now, lsa->tv_orig);
-
-  if (tv_cmp (delta, msec2tv (OSPF_MIN_LS_INTERVAL)) < 0)
+  if (monotime_since (&lsa->tv_orig, &delta) < OSPF_MIN_LS_INTERVAL * 1000LL)
     {
-      delay = tv_ceil (tv_sub (msec2tv (OSPF_MIN_LS_INTERVAL), delta));
+      struct timeval minv = msec2tv (OSPF_MIN_LS_INTERVAL);
+      timersub (&minv, &delta, &minv);
+
+      /* TBD: remove padding to full sec, return timeval instead */
+      delay = minv.tv_sec + !!minv.tv_usec;
 
       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
         zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
@@ -174,12 +113,10 @@ ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
 int
 get_age (struct ospf_lsa *lsa)
 {
-  int age;
-
-  age = ntohs (lsa->data->ls_age) 
-        + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
+  struct timeval rel;
 
-  return age;
+  monotime_since (&lsa->tv_recv, &rel);
+  return ntohs (lsa->data->ls_age) + rel.tv_sec;
 }
 
 
index 28ecc9d4d62458a417a05068239f495363cfaa9f..8b9a0d4c490ea67823f3aac5a5a4ebe4810aa1ab 100644 (file)
@@ -227,14 +227,8 @@ struct as_external_lsa
 
 /* Prototypes. */
 /* XXX: Eek, time functions, similar are in lib/thread.c */
-extern struct timeval tv_adjust (struct timeval);
-extern int tv_ceil (struct timeval);
-extern int tv_floor (struct timeval);
 extern struct timeval int2tv (int);
 extern struct timeval msec2tv (int);
-extern struct timeval tv_add (struct timeval, struct timeval);
-extern struct timeval tv_sub (struct timeval, struct timeval);
-extern int tv_cmp (struct timeval, struct timeval);
 
 extern int get_age (struct ospf_lsa *);
 extern u_int16_t ospf_lsa_checksum (struct lsa_header *);
index f7d1d0fa7dd54b111172e13c1c3883c5326fbf61..2aa761d46fee12bfb3265c4355c9b83d459104a1 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <zebra.h>
 
+#include "monotime.h"
 #include "thread.h"
 #include "memory.h"
 #include "linklist.h"
@@ -521,16 +522,18 @@ ospf_ls_upd_timer (struct thread *thread)
              struct ospf_lsa *lsa;
              
              if ((lsa = rn->info) != NULL)
-               /* Don't retransmit an LSA if we received it within
-                 the last RxmtInterval seconds - this is to allow the
-                 neighbour a chance to acknowledge the LSA as it may
-                 have ben just received before the retransmit timer
-                 fired.  This is a small tweak to what is in the RFC,
-                 but it will cut out out a lot of retransmit traffic
-                 - MAG */
-               if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv), 
-                           int2tv (retransmit_interval)) >= 0)
-                 listnode_add (update, rn->info);
+                {
+                  /* Don't retransmit an LSA if we received it within
+                    the last RxmtInterval seconds - this is to allow the
+                    neighbour a chance to acknowledge the LSA as it may
+                    have ben just received before the retransmit timer
+                    fired.  This is a small tweak to what is in the RFC,
+                    but it will cut out out a lot of retransmit traffic
+                    - MAG */
+                  if (monotime_since (&lsa->tv_recv, NULL)
+                        >= retransmit_interval * 1000000LL)
+                    listnode_add (update, rn->info);
+                }
            }
        }
 
@@ -1469,10 +1472,8 @@ ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
            }
          else
            {
-             struct timeval t, now;
-             quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
-             t = tv_sub (now, nbr->last_send_ts);
-             if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
+              if (monotime_since (&nbr->last_send_ts, NULL)
+                    < nbr->v_inactivity * 1000000LL)
                {
                  /* In states Loading and Full the slave must resend
                     its last Database Description packet in response to
@@ -2074,12 +2075,8 @@ ospf_ls_upd (struct ospf *ospf, struct ip *iph, struct ospf_header *ospfh,
             recent) LSA instance. */
          else
            {
-             struct timeval now;
-             
-             quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
-             
-             if (tv_cmp (tv_sub (now, current->tv_orig), 
-                         msec2tv (ospf->min_ls_arrival)) >= 0)
+              if (monotime_since (&current->tv_orig, NULL)
+                    >= ospf->min_ls_arrival * 1000LL)
                /* Trap NSSA type later.*/
                ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
              DISCARD_LSA (lsa, 8);
index 5dfd41dd1e28cce5e4f7b15a726c53658bbd162e..dcbb0151fdae331ba5665c3134286d40d1536c01 100644 (file)
@@ -20,6 +20,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
 #include <zebra.h>
 
+#include "monotime.h"
 #include "thread.h"
 #include "memory.h"
 #include "hash.h"
@@ -1421,7 +1422,6 @@ void
 ospf_spf_calculate_schedule (struct ospf *ospf, ospf_spf_reason_t reason)
 {
   unsigned long delay, elapsed, ht;
-  struct timeval result;
 
   if (IS_DEBUG_OSPF_EVENT)
     zlog_debug ("SPF: calculation timer scheduled");
@@ -1440,11 +1440,9 @@ ospf_spf_calculate_schedule (struct ospf *ospf, ospf_spf_reason_t reason)
                     (void *)ospf->t_spf_calc);
       return;
     }
-  
-  /* XXX Monotic timers: we only care about relative time here. */
-  result = tv_sub (recent_relative_time (), ospf->ts_spf);
-  
-  elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000);
+
+  elapsed = monotime_since (&ospf->ts_spf, NULL) / 1000;
+
   ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
   
   if (ht > ospf->spf_max_holdtime)
index ce12974d40f7293439f240b4e68b722c2d663865..2a627f9221ecc3308e993ba63e477035d9d5949e 100644 (file)
@@ -23,6 +23,7 @@
 #include <zebra.h>
 #include <string.h>
 
+#include "monotime.h"
 #include "memory.h"
 #include "thread.h"
 #include "prefix.h"
@@ -2809,10 +2810,8 @@ show_ip_ospf_area (struct vty *vty, struct ospf_area *area, json_object *json_ar
             json_object_boolean_true_add(json_area, "indefiniteActiveAdmin");
           if (area->t_stub_router)
             {
-              struct timeval result;
-              unsigned long time_store = 0;
-              result = tv_sub (area->t_stub_router->u.sands, recent_relative_time());
-              time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
+              long time_store;
+              time_store = monotime_until(&area->t_stub_router->u.sands, NULL) / 1000LL;
               json_object_int_add(json_area, "activeStartupRemainderMsecs", time_store);
             }
         }
@@ -2971,9 +2970,8 @@ show_ip_ospf_common (struct vty *vty, struct ospf *ospf, u_char use_json)
     {
       if (use_json)
         {
-          unsigned long time_store = 0;
-          result = tv_sub (ospf->t_deferred_shutdown->u.sands, recent_relative_time());
-          time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
+          long time_store;
+          time_store = monotime_until(&ospf->t_deferred_shutdown->u.sands, NULL) / 1000LL;
           json_object_int_add(json, "deferredShutdownMsecs", time_store);
         }
       else
@@ -3066,11 +3064,9 @@ show_ip_ospf_common (struct vty *vty, struct ospf *ospf, u_char use_json)
     {
       if (ospf->ts_spf.tv_sec || ospf->ts_spf.tv_usec)
         {
-          unsigned long time_store = 0;
+          long time_store = 0;
 
-          result = tv_sub (recent_relative_time(), ospf->ts_spf);
-          result = tv_sub (result, recent_relative_time());
-          time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
+          time_store = monotime_since(&ospf->ts_spf, NULL) / 1000LL;
           json_object_int_add(json, "spfLastExecutedMsecs", time_store);
 
           time_store = (1000 * ospf->ts_spf_duration.tv_sec) + (ospf->ts_spf_duration.tv_usec / 1000);
@@ -3084,7 +3080,7 @@ show_ip_ospf_common (struct vty *vty, struct ospf *ospf, u_char use_json)
       vty_out (vty, " SPF algorithm ");
       if (ospf->ts_spf.tv_sec || ospf->ts_spf.tv_usec)
         {
-          result = tv_sub (recent_relative_time(), ospf->ts_spf);
+          monotime_since(&ospf->ts_spf, &result);
           vty_out (vty, "last executed %s ago%s",
                    ospf_timeval_dump (&result, timebuf, sizeof (timebuf)),
                    VTY_NEWLINE);
@@ -3098,13 +3094,10 @@ show_ip_ospf_common (struct vty *vty, struct ospf *ospf, u_char use_json)
 
   if (use_json)
     {
-      struct timeval temp_time;
-      unsigned long time_store = 0;
-
       if (ospf->t_spf_calc)
         {
-          temp_time = tv_sub (ospf->t_spf_calc->u.sands, recent_relative_time());
-          time_store = (1000 * temp_time.tv_sec) + (temp_time.tv_usec / 1000);
+          long time_store;
+          time_store = monotime_until(&ospf->t_spf_calc->u.sands, NULL) / 1000LL;
           json_object_int_add(json, "spfTimerDueInMsecs", time_store);
         }
 
@@ -3509,16 +3502,9 @@ show_ip_ospf_interface_sub (struct vty *vty, struct ospf *ospf, struct interface
           char timebuf[OSPF_TIME_DUMP_SIZE];
           if (use_json)
             {
-              struct timeval result;
-              unsigned long time_store = 0;
-             if (oi->t_hello)
-               result = tv_sub (oi->t_hello->u.sands, recent_relative_time());
-             else
-               {
-                 result.tv_sec = 0;
-                 result.tv_usec = 0;
-               }
-              time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
+              long time_store = 0;
+              if (oi->t_hello)
+                time_store = monotime_until(&oi->t_hello->u.sands, NULL) / 1000LL;
               json_object_int_add(json_interface_sub, "timerHelloInMsecs", time_store);
             }
           else
@@ -3708,11 +3694,9 @@ show_ip_ospf_neighbor_sub (struct vty *vty, struct ospf_interface *oi, json_obje
                       json_neighbor = json_object_new_object();
                       ospf_nbr_state_message (nbr, msgbuf, 16);
 
-                      struct timeval result;
-                      unsigned long time_store = 0;
+                      long time_store;
 
-                      result = tv_sub (nbr->t_inactivity->u.sands, recent_relative_time());
-                      time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
+                      time_store = monotime_until(&nbr->t_inactivity->u.sands, NULL) / 1000LL;
 
                       json_object_int_add (json_neighbor, "priority", nbr->priority);
                       json_object_string_add (json_neighbor, "state", msgbuf);
@@ -4093,9 +4077,8 @@ show_ip_ospf_nbr_nbma_detail_sub (struct vty *vty, struct ospf_interface *oi, st
   /* Show poll-interval timer. */
   if (use_json)
     {
-      struct timeval res = tv_sub (nbr_nbma->t_poll->u.sands, recent_relative_time ());
-      unsigned long time_store = 0;
-      time_store = (1000 * res.tv_sec) + (res.tv_usec / 1000);
+      long time_store;
+      time_store = monotime_until(&nbr_nbma->t_poll->u.sands, NULL) / 1000LL;
       json_object_int_add(json_sub, "pollIntervalTimerDueMsec", time_store);
     }
   else
@@ -4170,11 +4153,12 @@ show_ip_ospf_neighbor_detail_sub (struct vty *vty, struct ospf_interface *oi,
 
   if (nbr->ts_last_progress.tv_sec || nbr->ts_last_progress.tv_usec)
     {
-      struct timeval res = tv_sub (recent_relative_time (), nbr->ts_last_progress);
+      struct timeval res;
+      long time_store;
+
+      time_store = monotime_since(&nbr->ts_last_progress, &res) / 1000LL;
       if (use_json)
         {
-          unsigned long time_store = 0;
-          time_store = (1000 * res.tv_sec) + (res.tv_usec / 1000);
           json_object_int_add(json_sub, "lastPrgrsvChangeMsec", time_store);
         }
       else
@@ -4189,11 +4173,12 @@ show_ip_ospf_neighbor_detail_sub (struct vty *vty, struct ospf_interface *oi,
 
   if (nbr->ts_last_regress.tv_sec || nbr->ts_last_regress.tv_usec)
     {
-      struct timeval res = tv_sub (recent_relative_time (), nbr->ts_last_regress);
+      struct timeval res;
+      long time_store;
+
+      time_store = monotime_since(&nbr->ts_last_regress, &res) / 1000LL;
       if (use_json)
         {
-          unsigned long time_store = 0;
-          time_store = (1000 * res.tv_sec) + (res.tv_usec / 1000);
           json_object_int_add(json_sub, "lastRegressiveChangeMsec", time_store);
           if (nbr->last_regress_str)
             json_object_string_add(json_sub, "lastRegressiveChangeReason", nbr->last_regress_str);
@@ -4234,9 +4219,8 @@ show_ip_ospf_neighbor_detail_sub (struct vty *vty, struct ospf_interface *oi,
     {
       if (nbr->t_inactivity)
        {
-         struct timeval res = tv_sub (nbr->t_inactivity->u.sands, recent_relative_time ());
-         unsigned long time_store = 0;
-         time_store = (1000 * res.tv_sec) + (res.tv_usec / 1000);
+          long time_store;
+          time_store = monotime_until(&nbr->t_inactivity->u.sands, NULL) / 1000LL;
          json_object_int_add(json_sub, "routerDeadIntervalTimerDueMsec", time_store);
        }
       else