Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/rtadvd Use clock_gettime(2) instead of gettimeofday...



details:   https://anonhg.NetBSD.org/src/rev/1fbe826e7f4c
branches:  trunk
changeset: 808852:1fbe826e7f4c
user:      roy <roy%NetBSD.org@localhost>
date:      Fri Jun 05 14:09:20 2015 +0000

description:
Use clock_gettime(2) instead of gettimeofday(2) so we can use a monotonic
clock rather than the wall clock.
Use timespec rather than timeval structs to make this transition easier.
Kill custom timeval comparison functions in favor of timespeccmp(3).

diffstat:

 usr.sbin/rtadvd/config.c |   18 ++++----
 usr.sbin/rtadvd/dump.c   |    8 +-
 usr.sbin/rtadvd/rrenum.c |   10 ++-
 usr.sbin/rtadvd/rtadvd.c |   47 +++++++++++----------
 usr.sbin/rtadvd/rtadvd.h |    8 +-
 usr.sbin/rtadvd/timer.c  |  102 +++++++++++++---------------------------------
 usr.sbin/rtadvd/timer.h  |   28 +++---------
 7 files changed, 84 insertions(+), 137 deletions(-)

diffs (truncated from 553 to 300 lines):

diff -r e8e6d5d65f4f -r 1fbe826e7f4c usr.sbin/rtadvd/config.c
--- a/usr.sbin/rtadvd/config.c  Fri Jun 05 07:44:39 2015 +0000
+++ b/usr.sbin/rtadvd/config.c  Fri Jun 05 14:09:20 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: config.c,v 1.33 2013/01/24 19:55:28 christos Exp $     */
+/*     $NetBSD: config.c,v 1.34 2015/06/05 14:09:20 roy Exp $  */
 /*     $KAME: config.c,v 1.93 2005/10/17 14:40:02 suz Exp $    */
 
 /*
@@ -444,8 +444,8 @@
 
                makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
                if (agetflag(entbuf)) {
-                       struct timeval now;
-                       gettimeofday(&now, 0);
+                       struct timespec now;
+                       clock_gettime(CLOCK_MONOTONIC, &now);
                        pfx->vltimeexpire =
                                now.tv_sec + pfx->validlifetime;
                }
@@ -464,8 +464,8 @@
 
                makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
                if (agetflag(entbuf)) {
-                       struct timeval now;
-                       gettimeofday(&now, 0);
+                       struct timespec now;
+                       clock_gettime(CLOCK_MONOTONIC, &now);
                        pfx->pltimeexpire =
                                now.tv_sec + pfx->preflifetime;
                }
@@ -958,7 +958,7 @@
 invalidate_prefix(struct prefix *prefix)
 {
        char ntopbuf[INET6_ADDRSTRLEN];
-       struct timeval timo;
+       struct timespec timo;
        struct rainfo *rai = prefix->rainfo;
 
        if (prefix->timer) {    /* sanity check */
@@ -981,7 +981,7 @@
                delete_prefix(prefix);
        }
        timo.tv_sec = prefix_timo;
-       timo.tv_usec = 0;
+       timo.tv_nsec = 0;
        rtadvd_set_timer(&timo, prefix->timer);
 }
 
@@ -1200,7 +1200,7 @@
 
        TAILQ_FOREACH(pfx, &rainfo->prefix, next) {     
                uint32_t vltime, pltime;
-               struct timeval now;
+               struct timespec now;
 
                CHECKLEN(sizeof(*ndopt_pi));
                ndopt_pi = (struct nd_opt_prefix_info *)buf;
@@ -1218,7 +1218,7 @@
                        vltime = 0;
                else {
                        if (pfx->vltimeexpire || pfx->pltimeexpire)
-                               gettimeofday(&now, NULL);
+                               clock_gettime(CLOCK_MONOTONIC, &now);
                        if (pfx->vltimeexpire == 0)
                                vltime = pfx->validlifetime;
                        else
diff -r e8e6d5d65f4f -r 1fbe826e7f4c usr.sbin/rtadvd/dump.c
--- a/usr.sbin/rtadvd/dump.c    Fri Jun 05 07:44:39 2015 +0000
+++ b/usr.sbin/rtadvd/dump.c    Fri Jun 05 14:09:20 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dump.c,v 1.11 2013/07/09 09:34:59 roy Exp $    */
+/*     $NetBSD: dump.c,v 1.12 2015/06/05 14:09:20 roy Exp $    */
 /*     $KAME: dump.c,v 1.34 2004/06/14 05:35:59 itojun Exp $   */
 
 /*
@@ -99,9 +99,9 @@
        struct dnssl_domain *dnsd;
        char *p, len;
        char prefixbuf[INET6_ADDRSTRLEN];
-       struct timeval now;
+       struct timespec now;
 
-       gettimeofday(&now, NULL); /* XXX: unused in most cases */
+       clock_gettime(CLOCK_MONOTONIC, &now); /* XXX: unused in most cases */
        TAILQ_FOREACH(rai, &ralist, next) {
                fprintf(fp, "%s:\n", rai->ifname);
 
@@ -198,7 +198,7 @@
                                pfx->autoconfflg ? "A" : "",
                                "");
                        if (pfx->timer) {
-                               struct timeval *rest;
+                               struct timespec *rest;
 
                                rest = rtadvd_timer_rest(pfx->timer);
                                if (rest) { /* XXX: what if not? */
diff -r e8e6d5d65f4f -r 1fbe826e7f4c usr.sbin/rtadvd/rrenum.c
--- a/usr.sbin/rtadvd/rrenum.c  Fri Jun 05 07:44:39 2015 +0000
+++ b/usr.sbin/rtadvd/rrenum.c  Fri Jun 05 14:09:20 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rrenum.c,v 1.16 2013/05/17 07:53:05 skrll Exp $        */
+/*     $NetBSD: rrenum.c,v 1.17 2015/06/05 14:09:20 roy Exp $  */
 /*     $KAME: rrenum.c,v 1.14 2004/06/14 05:36:00 itojun Exp $ */
 
 /*
@@ -209,7 +209,7 @@
                                continue; /* non-advertising IF */
 
                        TAILQ_FOREACH(pp, &rai->prefix, next) {
-                               struct timeval now;
+                               struct timespec now;
 
                                if (prefix_match(&pp->prefix, pp->prefixlen,
                                                 &rpm->rpm_prefix,
@@ -218,13 +218,15 @@
                                        pp->validlifetime = ntohl(rpu->rpu_vltime);
                                        pp->preflifetime = ntohl(rpu->rpu_pltime);
                                        if (irr->irr_rrf_decrvalid) {
-                                               gettimeofday(&now, 0);
+                                               clock_gettime(CLOCK_MONOTONIC,
+                                                   &now);
                                                pp->vltimeexpire =
                                                        now.tv_sec + pp->validlifetime;
                                        } else
                                                pp->vltimeexpire = 0;
                                        if (irr->irr_rrf_decrprefd) {
-                                               gettimeofday(&now, 0);
+                                               clock_gettime(CLOCK_MONOTONIC,
+                                                   &now);
                                                pp->pltimeexpire =
                                                        now.tv_sec + pp->preflifetime;
                                        } else
diff -r e8e6d5d65f4f -r 1fbe826e7f4c usr.sbin/rtadvd/rtadvd.c
--- a/usr.sbin/rtadvd/rtadvd.c  Fri Jun 05 07:44:39 2015 +0000
+++ b/usr.sbin/rtadvd/rtadvd.c  Fri Jun 05 14:09:20 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtadvd.c,v 1.46 2014/02/27 17:43:02 joerg Exp $        */
+/*     $NetBSD: rtadvd.c,v 1.47 2015/06/05 14:09:20 roy Exp $  */
 /*     $KAME: rtadvd.c,v 1.92 2005/10/17 14:40:02 suz Exp $    */
 
 /*
@@ -175,7 +175,7 @@
 main(int argc, char *argv[])
 {
        struct pollfd set[2];
-       struct timeval *timeout;
+       struct timespec *timeout;
        int i, ch;
        int fflag = 0, logopt;
        struct passwd *pw;
@@ -311,20 +311,20 @@
                                getconfig(*argv++, 0);
                }
 
+               /* timer expiration check and reset the timer */
+               timeout = rtadvd_check_timer();
+
                if (do_die) {
                        die();
                        /*NOTREACHED*/
                }
 
-               /* timer expiration check and reset the timer */
-               timeout = rtadvd_check_timer();
-
                if (timeout != NULL) {
                        syslog(LOG_DEBUG,
                            "<%s> set timer to %ld:%ld. waiting for "
                            "inputs or timeout", __func__,
                            (long int)timeout->tv_sec,
-                           (long int)timeout->tv_usec);
+                           (long int)timeout->tv_nsec);
                } else {
                        syslog(LOG_DEBUG,
                            "<%s> there's no timer. waiting for inputs",
@@ -332,7 +332,8 @@
                }
 
                if ((i = poll(set, 2, timeout ? (timeout->tv_sec * 1000 +
-                   timeout->tv_usec / 1000) : INFTIM)) < 0) {
+                   (timeout->tv_nsec + 999999) / 1000000) : INFTIM)) < 0)
+               {
                        /* EINTR would occur upon SIGUSR1 for status dump */
                        if (errno != EINTR)
                                syslog(LOG_ERR, "<%s> poll: %s",
@@ -991,7 +992,7 @@
 ra_timer_set_short_delay(struct rainfo *rai)
 {
        long delay;     /* must not be greater than 1000000 */
-       struct timeval interval, now, min_delay, tm_tmp, *rest;
+       struct timespec interval, now, min_delay, tm_tmp, *rest;
 
        /*
         * Compute a random delay. If the computed value
@@ -1002,9 +1003,9 @@
         */
        delay = arc4random() % MAX_RA_DELAY_TIME;
        interval.tv_sec = 0;
-       interval.tv_usec = delay;
+       interval.tv_nsec = delay;
        rest = rtadvd_timer_rest(rai->timer);
-       if (TIMEVAL_LT(*rest, interval)) {
+       if (timespeccmp(rest, &interval, <)) {
                syslog(LOG_DEBUG, "<%s> random delay is larger than "
                    "the rest of current timer", __func__);
                interval = *rest;
@@ -1017,13 +1018,13 @@
         * MIN_DELAY_BETWEEN_RAS plus the random value after the
         * previous advertisement was sent.
         */
-       gettimeofday(&now, NULL);
-       TIMEVAL_SUB(&now, &rai->lastsent, &tm_tmp);
+       clock_gettime(CLOCK_MONOTONIC, &now);
+       timespecsub(&now, &rai->lastsent, &tm_tmp);
        min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
-       min_delay.tv_usec = 0;
-       if (TIMEVAL_LT(tm_tmp, min_delay)) {
-               TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
-               TIMEVAL_ADD(&min_delay, &interval, &interval);
+       min_delay.tv_nsec = 0;
+       if (timespeccmp(&tm_tmp, &min_delay, <)) {
+               timespecsub(&min_delay, &tm_tmp, &min_delay);
+               timespecadd(&min_delay, &interval, &interval);
        }
        rtadvd_set_timer(&interval, rai->timer);
 }
@@ -1199,7 +1200,7 @@
        struct prefix *pp;
        int inconsistent = 0;
        char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
-       struct timeval now;
+       struct timespec now;
 
 #if 0                          /* impossible */
        if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
@@ -1245,7 +1246,7 @@
                 * XXX: can we really expect that all routers on the link
                 * have synchronized clocks?
                 */
-               gettimeofday(&now, NULL);
+               clock_gettime(CLOCK_MONOTONIC, &now);
                preferred_time += now.tv_sec;
 
                if (!pp->timer && rai->clockskew &&
@@ -1281,7 +1282,7 @@
 
        valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
        if (pp->vltimeexpire) {
-               gettimeofday(&now, NULL);
+               clock_gettime(CLOCK_MONOTONIC, &now);
                valid_time += now.tv_sec;
 
                if (!pp->timer && rai->clockskew &&
@@ -1767,7 +1768,7 @@
        rai->raoutput++;
 
        /* update timestamp */
-       gettimeofday(&rai->lastsent, NULL);
+       clock_gettime(CLOCK_MONOTONIC, &rai->lastsent);
 
        /* reset waiting conter */
        rai->waiting = 0;
@@ -1796,7 +1797,7 @@
 
 /* update RA timer */
 void
-ra_timer_update(void *data, struct timeval *tm)
+ra_timer_update(void *data, struct timespec *tm)
 {
        struct rainfo *rai = (struct rainfo *)data;
        long interval;
@@ -1823,12 +1824,12 @@
                interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
 
        tm->tv_sec = interval;
-       tm->tv_usec = 0;
+       tm->tv_nsec = 0;
 
        syslog(LOG_DEBUG,
               "<%s> RA timer on %s is set to %ld:%ld",
               __func__, rai->ifname,
-              (long int)tm->tv_sec, (long int)tm->tv_usec);
+              (long int)tm->tv_sec, (long int)tm->tv_nsec);
 
        return;
 }
diff -r e8e6d5d65f4f -r 1fbe826e7f4c usr.sbin/rtadvd/rtadvd.h
--- a/usr.sbin/rtadvd/rtadvd.h  Fri Jun 05 07:44:39 2015 +0000
+++ b/usr.sbin/rtadvd/rtadvd.h  Fri Jun 05 14:09:20 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtadvd.h,v 1.13 2013/07/09 09:34:59 roy Exp $  */
+/*     $NetBSD: rtadvd.h,v 1.14 2015/06/05 14:09:20 roy Exp $  */
 /*     $KAME: rtadvd.h,v 1.30 2005/10/17 14:40:02 suz Exp $    */
 
 /*
@@ -63,7 +63,7 @@
 #define MAX_INITIAL_RTR_ADVERTISEMENTS    3
 #define MAX_FINAL_RTR_ADVERTISEMENTS      3
 #define MIN_DELAY_BETWEEN_RAS             3



Home | Main Index | Thread Index | Old Index