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 "monotime.h"
#include "linklist.h"
#include "thread.h"
#include "prefix.h"
@ -318,7 +319,7 @@ ospf_timer_dump (struct thread *t, char *buf, size_t size)
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);
}

View 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.");

View 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;
struct timeval rel;
age = ntohs (lsa->data->ls_age)
+ tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
return age;
monotime_since (&lsa->tv_recv, &rel);
return ntohs (lsa->data->ls_age) + rel.tv_sec;
}

View 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 *);

View File

@ -22,6 +22,7 @@
#include <zebra.h>
#include "monotime.h"
#include "thread.h"
#include "memory.h"
#include "linklist.h"
@ -521,6 +522,7 @@ 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
@ -528,11 +530,12 @@ ospf_ls_upd_timer (struct thread *thread)
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)
if (monotime_since (&lsa->tv_recv, NULL)
>= retransmit_interval * 1000000LL)
listnode_add (update, rn->info);
}
}
}
if (listcount (update) > 0)
ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT);
@ -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);

View 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");
@ -1441,10 +1441,8 @@ ospf_spf_calculate_schedule (struct ospf *ospf, ospf_spf_reason_t reason)
return;
}
/* XXX Monotic timers: we only care about relative time here. */
result = tv_sub (recent_relative_time (), ospf->ts_spf);
elapsed = monotime_since (&ospf->ts_spf, NULL) / 1000;
elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000);
ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
if (ht > ospf->spf_max_holdtime)

View 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;
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);
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