ospfd: time: replace local helpers with monotime

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>
This commit is contained in:
David Lamparter 2017-01-17 22:05:56 +01:00
parent 7790a2d69e
commit cbf3e3eb3a
7 changed files with 66 additions and 154 deletions

View File

@ -22,6 +22,7 @@
#include <zebra.h> #include <zebra.h>
#include "monotime.h"
#include "linklist.h" #include "linklist.h"
#include "thread.h" #include "thread.h"
#include "prefix.h" #include "prefix.h"
@ -317,8 +318,8 @@ ospf_timer_dump (struct thread *t, char *buf, size_t size)
struct timeval result; struct timeval result;
if (!t) if (!t)
return "inactive"; return "inactive";
result = tv_sub (t->u.sands, recent_relative_time()); monotime_until (&t->u.sands, &result);
return ospf_timeval_dump (&result, buf, size); return ospf_timeval_dump (&result, buf, size);
} }

View File

@ -22,6 +22,7 @@
#include <zebra.h> #include <zebra.h>
#include "monotime.h"
#include "linklist.h" #include "linklist.h"
#include "prefix.h" #include "prefix.h"
#include "if.h" #include "if.h"
@ -277,8 +278,8 @@ ospf_flood (struct ospf *ospf, struct ospf_neighbor *nbr,
"while local one is initial instance."); "while local one is initial instance.");
; /* Accept this LSA for quick LSDB resynchronization. */ ; /* Accept this LSA for quick LSDB resynchronization. */
} }
else if (tv_cmp (tv_sub (recent_relative_time (), current->tv_recv), else if (monotime_since (&current->tv_recv, NULL)
msec2tv (ospf->min_ls_arrival)) < 0) < ospf->min_ls_arrival * 1000LL)
{ {
if (IS_DEBUG_OSPF_EVENT) if (IS_DEBUG_OSPF_EVENT)
zlog_debug ("LSA[Flooding]: LSA is received recently."); zlog_debug ("LSA[Flooding]: LSA is received recently.");

View File

@ -22,6 +22,7 @@
#include <zebra.h> #include <zebra.h>
#include "monotime.h"
#include "linklist.h" #include "linklist.h"
#include "prefix.h" #include "prefix.h"
#include "if.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 struct timeval
int2tv (int a) int2tv (int a)
{ {
@ -115,50 +82,22 @@ msec2tv (int a)
ret.tv_sec = a/1000; ret.tv_sec = a/1000;
ret.tv_usec = (a%1000) * 1000; ret.tv_usec = (a%1000) * 1000;
return tv_adjust (ret); return 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);
} }
int int
ospf_lsa_refresh_delay (struct ospf_lsa *lsa) ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
{ {
struct timeval delta, now; struct timeval delta;
int delay = 0; int delay = 0;
quagga_gettime (QUAGGA_CLK_MONOTONIC, &now); if (monotime_since (&lsa->tv_orig, &delta) < OSPF_MIN_LS_INTERVAL * 1000LL)
delta = tv_sub (now, lsa->tv_orig);
if (tv_cmp (delta, msec2tv (OSPF_MIN_LS_INTERVAL)) < 0)
{ {
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)) if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds", zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
@ -174,12 +113,10 @@ ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
int int
get_age (struct ospf_lsa *lsa) get_age (struct ospf_lsa *lsa)
{ {
int age; struct timeval rel;
age = ntohs (lsa->data->ls_age) monotime_since (&lsa->tv_recv, &rel);
+ tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv)); return ntohs (lsa->data->ls_age) + rel.tv_sec;
return age;
} }

View File

@ -227,14 +227,8 @@ struct as_external_lsa
/* Prototypes. */ /* Prototypes. */
/* XXX: Eek, time functions, similar are in lib/thread.c */ /* 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 int2tv (int);
extern struct timeval msec2tv (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 int get_age (struct ospf_lsa *);
extern u_int16_t ospf_lsa_checksum (struct lsa_header *); extern u_int16_t ospf_lsa_checksum (struct lsa_header *);

View File

@ -22,6 +22,7 @@
#include <zebra.h> #include <zebra.h>
#include "monotime.h"
#include "thread.h" #include "thread.h"
#include "memory.h" #include "memory.h"
#include "linklist.h" #include "linklist.h"
@ -521,16 +522,18 @@ ospf_ls_upd_timer (struct thread *thread)
struct ospf_lsa *lsa; struct ospf_lsa *lsa;
if ((lsa = rn->info) != NULL) if ((lsa = rn->info) != NULL)
/* Don't retransmit an LSA if we received it within {
the last RxmtInterval seconds - this is to allow the /* Don't retransmit an LSA if we received it within
neighbour a chance to acknowledge the LSA as it may the last RxmtInterval seconds - this is to allow the
have ben just received before the retransmit timer neighbour a chance to acknowledge the LSA as it may
fired. This is a small tweak to what is in the RFC, have ben just received before the retransmit timer
but it will cut out out a lot of retransmit traffic fired. This is a small tweak to what is in the RFC,
- MAG */ but it will cut out out a lot of retransmit traffic
if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv), - MAG */
int2tv (retransmit_interval)) >= 0) if (monotime_since (&lsa->tv_recv, NULL)
listnode_add (update, rn->info); >= retransmit_interval * 1000000LL)
listnode_add (update, rn->info);
}
} }
} }
@ -1469,10 +1472,8 @@ ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
} }
else else
{ {
struct timeval t, now; if (monotime_since (&nbr->last_send_ts, NULL)
quagga_gettime (QUAGGA_CLK_MONOTONIC, &now); < nbr->v_inactivity * 1000000LL)
t = tv_sub (now, nbr->last_send_ts);
if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
{ {
/* In states Loading and Full the slave must resend /* In states Loading and Full the slave must resend
its last Database Description packet in response to 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. */ recent) LSA instance. */
else else
{ {
struct timeval now; if (monotime_since (&current->tv_orig, NULL)
>= ospf->min_ls_arrival * 1000LL)
quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
if (tv_cmp (tv_sub (now, current->tv_orig),
msec2tv (ospf->min_ls_arrival)) >= 0)
/* Trap NSSA type later.*/ /* Trap NSSA type later.*/
ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT); ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
DISCARD_LSA (lsa, 8); DISCARD_LSA (lsa, 8);

View File

@ -20,6 +20,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#include <zebra.h> #include <zebra.h>
#include "monotime.h"
#include "thread.h" #include "thread.h"
#include "memory.h" #include "memory.h"
#include "hash.h" #include "hash.h"
@ -1421,7 +1422,6 @@ void
ospf_spf_calculate_schedule (struct ospf *ospf, ospf_spf_reason_t reason) ospf_spf_calculate_schedule (struct ospf *ospf, ospf_spf_reason_t reason)
{ {
unsigned long delay, elapsed, ht; unsigned long delay, elapsed, ht;
struct timeval result;
if (IS_DEBUG_OSPF_EVENT) if (IS_DEBUG_OSPF_EVENT)
zlog_debug ("SPF: calculation timer scheduled"); 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); (void *)ospf->t_spf_calc);
return; return;
} }
/* XXX Monotic timers: we only care about relative time here. */ elapsed = monotime_since (&ospf->ts_spf, NULL) / 1000;
result = tv_sub (recent_relative_time (), ospf->ts_spf);
elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000);
ht = ospf->spf_holdtime * ospf->spf_hold_multiplier; ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
if (ht > ospf->spf_max_holdtime) if (ht > ospf->spf_max_holdtime)

View File

@ -23,6 +23,7 @@
#include <zebra.h> #include <zebra.h>
#include <string.h> #include <string.h>
#include "monotime.h"
#include "memory.h" #include "memory.h"
#include "thread.h" #include "thread.h"
#include "prefix.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"); json_object_boolean_true_add(json_area, "indefiniteActiveAdmin");
if (area->t_stub_router) if (area->t_stub_router)
{ {
struct timeval result; long time_store;
unsigned long time_store = 0; time_store = monotime_until(&area->t_stub_router->u.sands, NULL) / 1000LL;
result = tv_sub (area->t_stub_router->u.sands, recent_relative_time());
time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
json_object_int_add(json_area, "activeStartupRemainderMsecs", time_store); 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) if (use_json)
{ {
unsigned long time_store = 0; long time_store;
result = tv_sub (ospf->t_deferred_shutdown->u.sands, recent_relative_time()); time_store = monotime_until(&ospf->t_deferred_shutdown->u.sands, NULL) / 1000LL;
time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
json_object_int_add(json, "deferredShutdownMsecs", time_store); json_object_int_add(json, "deferredShutdownMsecs", time_store);
} }
else 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) 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); time_store = monotime_since(&ospf->ts_spf, NULL) / 1000LL;
result = tv_sub (result, recent_relative_time());
time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
json_object_int_add(json, "spfLastExecutedMsecs", time_store); json_object_int_add(json, "spfLastExecutedMsecs", time_store);
time_store = (1000 * ospf->ts_spf_duration.tv_sec) + (ospf->ts_spf_duration.tv_usec / 1000); 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 "); vty_out (vty, " SPF algorithm ");
if (ospf->ts_spf.tv_sec || ospf->ts_spf.tv_usec) 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", vty_out (vty, "last executed %s ago%s",
ospf_timeval_dump (&result, timebuf, sizeof (timebuf)), ospf_timeval_dump (&result, timebuf, sizeof (timebuf)),
VTY_NEWLINE); VTY_NEWLINE);
@ -3098,13 +3094,10 @@ show_ip_ospf_common (struct vty *vty, struct ospf *ospf, u_char use_json)
if (use_json) if (use_json)
{ {
struct timeval temp_time;
unsigned long time_store = 0;
if (ospf->t_spf_calc) if (ospf->t_spf_calc)
{ {
temp_time = tv_sub (ospf->t_spf_calc->u.sands, recent_relative_time()); long time_store;
time_store = (1000 * temp_time.tv_sec) + (temp_time.tv_usec / 1000); time_store = monotime_until(&ospf->t_spf_calc->u.sands, NULL) / 1000LL;
json_object_int_add(json, "spfTimerDueInMsecs", time_store); 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]; char timebuf[OSPF_TIME_DUMP_SIZE];
if (use_json) if (use_json)
{ {
struct timeval result; long time_store = 0;
unsigned long time_store = 0; if (oi->t_hello)
if (oi->t_hello) time_store = monotime_until(&oi->t_hello->u.sands, NULL) / 1000LL;
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);
json_object_int_add(json_interface_sub, "timerHelloInMsecs", time_store); json_object_int_add(json_interface_sub, "timerHelloInMsecs", time_store);
} }
else 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(); json_neighbor = json_object_new_object();
ospf_nbr_state_message (nbr, msgbuf, 16); ospf_nbr_state_message (nbr, msgbuf, 16);
struct timeval result; long time_store;
unsigned long time_store = 0;
result = tv_sub (nbr->t_inactivity->u.sands, recent_relative_time()); time_store = monotime_until(&nbr->t_inactivity->u.sands, NULL) / 1000LL;
time_store = (1000 * result.tv_sec) + (result.tv_usec / 1000);
json_object_int_add (json_neighbor, "priority", nbr->priority); json_object_int_add (json_neighbor, "priority", nbr->priority);
json_object_string_add (json_neighbor, "state", msgbuf); 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. */ /* Show poll-interval timer. */
if (use_json) if (use_json)
{ {
struct timeval res = tv_sub (nbr_nbma->t_poll->u.sands, recent_relative_time ()); long time_store;
unsigned long time_store = 0; time_store = monotime_until(&nbr_nbma->t_poll->u.sands, NULL) / 1000LL;
time_store = (1000 * res.tv_sec) + (res.tv_usec / 1000);
json_object_int_add(json_sub, "pollIntervalTimerDueMsec", time_store); json_object_int_add(json_sub, "pollIntervalTimerDueMsec", time_store);
} }
else 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) 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) 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); json_object_int_add(json_sub, "lastPrgrsvChangeMsec", time_store);
} }
else 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) 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) 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); json_object_int_add(json_sub, "lastRegressiveChangeMsec", time_store);
if (nbr->last_regress_str) if (nbr->last_regress_str)
json_object_string_add(json_sub, "lastRegressiveChangeReason", 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) if (nbr->t_inactivity)
{ {
struct timeval res = tv_sub (nbr->t_inactivity->u.sands, recent_relative_time ()); long time_store;
unsigned long time_store = 0; time_store = monotime_until(&nbr->t_inactivity->u.sands, NULL) / 1000LL;
time_store = (1000 * res.tv_sec) + (res.tv_usec / 1000);
json_object_int_add(json_sub, "routerDeadIntervalTimerDueMsec", time_store); json_object_int_add(json_sub, "routerDeadIntervalTimerDueMsec", time_store);
} }
else else