From 16b3f6a9c07a4e81c2a14b6978aeab8915369e1c Mon Sep 17 00:00:00 2001 From: Reyk Floeter Date: Sun, 10 Mar 2013 23:32:54 +0000 Subject: This diff changes relayd to use the monotonic clock instead of gettimeofday(). It was also bugging me for some time to have all these checks of gettimeofday()'s return value: it should not fail. So this diff introduces a void getmonotime(struct timeval *tv) that calls clock_gettime(CLOCK_MONOTONIC, &ts) and converts the output to a struct timeval that can be used with the existing code and the timeval-specific timer functions (timerclear, timersub, ...). It does not return a status but calls fatal() on error-that-should-not-happen. ok sthen@ chris@ --- usr.sbin/relayctl/relayctl.c | 5 ++--- usr.sbin/relayd/check_icmp.c | 5 ++--- usr.sbin/relayd/hce.c | 8 +++----- usr.sbin/relayd/log.c | 13 ++++++++++++- usr.sbin/relayd/pfe.c | 5 ++--- usr.sbin/relayd/relay.c | 28 +++++++++++++--------------- usr.sbin/relayd/relay_http.c | 14 +++++++------- usr.sbin/relayd/relay_udp.c | 18 ++++++------------ usr.sbin/relayd/relayd.c | 6 ++---- usr.sbin/relayd/relayd.h | 4 ++-- 10 files changed, 51 insertions(+), 55 deletions(-) (limited to 'usr.sbin') diff --git a/usr.sbin/relayctl/relayctl.c b/usr.sbin/relayctl/relayctl.c index 382f9551d84..c78886ec032 100644 --- a/usr.sbin/relayctl/relayctl.c +++ b/usr.sbin/relayctl/relayctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayctl.c,v 1.45 2011/05/20 09:43:53 reyk Exp $ */ +/* $OpenBSD: relayctl.c,v 1.46 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard @@ -443,8 +443,7 @@ show_session_msg(struct imsg *imsg) a, ntohs(con->se_in.port), b, ntohs(con->se_out.port), con->se_done ? "DONE" : "RUNNING"); - if (gettimeofday(&tv_now, NULL)) - fatal("show_session_msg: gettimeofday"); + getmonotime(&tv_now); print_time(&tv_now, &con->se_tv_start, a, sizeof(a)); print_time(&tv_now, &con->se_tv_last, b, sizeof(b)); printf("\tage %s, idle %s, relay %u, pid %u", diff --git a/usr.sbin/relayd/check_icmp.c b/usr.sbin/relayd/check_icmp.c index 992a426a1a2..e63f26fed4a 100644 --- a/usr.sbin/relayd/check_icmp.c +++ b/usr.sbin/relayd/check_icmp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: check_icmp.c,v 1.35 2013/01/29 15:04:42 sthen Exp $ */ +/* $OpenBSD: check_icmp.c,v 1.36 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard @@ -95,8 +95,7 @@ check_icmp_add(struct ctl_icmp_event *cie, int flags, struct timeval *start, if (start != NULL) bcopy(start, &cie->tv_start, sizeof(cie->tv_start)); bcopy(&cie->env->sc_timeout, &tv, sizeof(tv)); - if (gettimeofday(&cie->tv_start, NULL) == -1) - fatal("check_icmp_add: gettimeofday"); + getmonotime(&cie->tv_start); event_del(&cie->ev); event_set(&cie->ev, cie->s, EV_TIMEOUT|flags, fn, cie); event_add(&cie->ev, &tv); diff --git a/usr.sbin/relayd/hce.c b/usr.sbin/relayd/hce.c index 3d5551f4890..dd1447d015b 100644 --- a/usr.sbin/relayd/hce.c +++ b/usr.sbin/relayd/hce.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hce.c,v 1.63 2012/05/09 12:54:13 giovanni Exp $ */ +/* $OpenBSD: hce.c,v 1.64 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard @@ -156,8 +156,7 @@ hce_launch_checks(int fd, short event, void *arg) } } - if (gettimeofday(&tv, NULL) == -1) - fatal("hce_launch_checks: gettimeofday"); + getmonotime(&tv); TAILQ_FOREACH(table, env->sc_tables, entry) { if (table->conf.flags & F_DISABLE) @@ -260,8 +259,7 @@ hce_notify_done(struct host *host, enum host_error he) else logopt = RELAYD_OPT_LOGNOTIFY; - if (gettimeofday(&tv_now, NULL) == -1) - fatal("hce_notify_done: gettimeofday"); + getmonotime(&tv_now); timersub(&tv_now, &host->cte.tv_start, &tv_dur); if (timercmp(&host->cte.tv_start, &tv_dur, >)) duration = (tv_dur.tv_sec * 1000) + (tv_dur.tv_usec / 1000.0); diff --git a/usr.sbin/relayd/log.c b/usr.sbin/relayd/log.c index da5a2fb2fa6..3e0ba36f1c2 100644 --- a/usr.sbin/relayd/log.c +++ b/usr.sbin/relayd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.18 2012/11/27 05:00:28 guenther Exp $ */ +/* $OpenBSD: log.c,v 1.19 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -455,3 +455,14 @@ printb_flags(const u_int32_t v, const char *bits) return (r); } + +void +getmonotime(struct timeval *tv) +{ + struct timespec ts; + + if (clock_gettime(CLOCK_MONOTONIC, &ts)) + fatal("clock_gettime"); + + TIMESPEC_TO_TIMEVAL(tv, &ts); +} diff --git a/usr.sbin/relayd/pfe.c b/usr.sbin/relayd/pfe.c index a678c9de8eb..4c0f04f8a7a 100644 --- a/usr.sbin/relayd/pfe.c +++ b/usr.sbin/relayd/pfe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfe.c,v 1.73 2012/10/03 08:33:31 reyk Exp $ */ +/* $OpenBSD: pfe.c,v 1.74 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard @@ -729,8 +729,7 @@ pfe_statistics(int fd, short events, void *arg) u_long cnt; timerclear(&tv); - if (gettimeofday(&tv_now, NULL) == -1) - fatal("pfe_statistics: gettimeofday"); + getmonotime(&tv_now); TAILQ_FOREACH(rdr, env->sc_rdrs, entry) { cnt = check_table(env, rdr, rdr->table); diff --git a/usr.sbin/relayd/relay.c b/usr.sbin/relayd/relay.c index 541d34a1f44..fa05de64c2a 100644 --- a/usr.sbin/relayd/relay.c +++ b/usr.sbin/relayd/relay.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay.c,v 1.163 2013/03/09 14:43:06 bluhm Exp $ */ +/* $OpenBSD: relay.c,v 1.164 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 - 2012 Reyk Floeter @@ -318,8 +318,7 @@ relay_statistics(int fd, short events, void *arg) */ timerclear(&tv); - if (gettimeofday(&tv_now, NULL) == -1) - fatal("relay_init: gettimeofday"); + getmonotime(&tv_now); TAILQ_FOREACH(rlay, env->sc_relays, rl_entry) { bzero(&crs, sizeof(crs)); @@ -741,8 +740,8 @@ relay_write(struct bufferevent *bev, void *arg) struct ctl_relay_event *cre = arg; struct rsession *con = cre->con; - if (gettimeofday(&con->se_tv_last, NULL) == -1) - goto fail; + getmonotime(&con->se_tv_last); + if (con->se_done) goto done; if (relay_splice(cre->dst) == -1) @@ -780,8 +779,8 @@ relay_read(struct bufferevent *bev, void *arg) struct rsession *con = cre->con; struct evbuffer *src = EVBUFFER_INPUT(bev); - if (gettimeofday(&con->se_tv_last, NULL) == -1) - goto fail; + getmonotime(&con->se_tv_last); + if (!EVBUFFER_LENGTH(src)) return; if (relay_bufferevent_write_buffer(cre->dst, src) == -1) @@ -891,8 +890,8 @@ relay_splicelen(struct ctl_relay_event *cre) __func__, con->se_id, cre->dir, len); if (len > cre->splicelen) { - if (gettimeofday(&con->se_tv_last, NULL) == -1) - return (-1); + getmonotime(&con->se_tv_last); + cre->splicelen = len; return (1); } @@ -1049,10 +1048,6 @@ relay_accept(int fd, short event, void *arg) con->se_out.dir = RELAY_DIR_RESPONSE; con->se_retry = rlay->rl_conf.dstretry; con->se_bnds = -1; - if (gettimeofday(&con->se_tv_start, NULL) == -1) - goto err; - bcopy(&con->se_tv_start, &con->se_tv_last, sizeof(con->se_tv_last)); - bcopy(&ss, &con->se_in.ss, sizeof(con->se_in.ss)); con->se_out.port = rlay->rl_conf.dstport; switch (ss.ss_family) { case AF_INET: @@ -1062,6 +1057,10 @@ relay_accept(int fd, short event, void *arg) con->se_in.port = ((struct sockaddr_in6 *)&ss)->sin6_port; break; } + bcopy(&ss, &con->se_in.ss, sizeof(con->se_in.ss)); + + getmonotime(&con->se_tv_start); + bcopy(&con->se_tv_start, &con->se_tv_last, sizeof(con->se_tv_last)); relay_sessions++; SPLAY_INSERT(session_tree, &rlay->rl_sessions, con); @@ -1454,8 +1453,7 @@ relay_connect(struct rsession *con) if (relay_inflight < 1) fatalx("relay_connect: no connection in flight"); - if (gettimeofday(&con->se_tv_start, NULL) == -1) - return (-1); + getmonotime(&con->se_tv_start); if (!TAILQ_EMPTY(&rlay->rl_tables)) { if (relay_from_table(con) != 0) diff --git a/usr.sbin/relayd/relay_http.c b/usr.sbin/relayd/relay_http.c index cfbf7cb13fa..69ee12b18a0 100644 --- a/usr.sbin/relayd/relay_http.c +++ b/usr.sbin/relayd/relay_http.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay_http.c,v 1.10 2013/03/09 14:43:06 bluhm Exp $ */ +/* $OpenBSD: relay_http.c,v 1.11 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 - 2012 Reyk Floeter @@ -78,8 +78,8 @@ relay_read_http(struct bufferevent *bev, void *arg) const char *errstr; size_t size; - if (gettimeofday(&con->se_tv_last, NULL) == -1) - goto fail; + getmonotime(&con->se_tv_last); + size = EVBUFFER_LENGTH(src); DPRINTF("%s: size %lu, to read %lld", __func__, size, cre->toread); if (!size) { @@ -393,8 +393,8 @@ relay_read_httpcontent(struct bufferevent *bev, void *arg) struct evbuffer *src = EVBUFFER_INPUT(bev); size_t size; - if (gettimeofday(&con->se_tv_last, NULL) == -1) - goto fail; + getmonotime(&con->se_tv_last); + size = EVBUFFER_LENGTH(src); DPRINTF("%s: dir %d, size %lu, to read %lld", __func__, cre->dir, size, cre->toread); @@ -446,8 +446,8 @@ relay_read_httpchunks(struct bufferevent *bev, void *arg) long lval; size_t size; - if (gettimeofday(&con->se_tv_last, NULL) == -1) - goto fail; + getmonotime(&con->se_tv_last); + size = EVBUFFER_LENGTH(src); DPRINTF("%s: dir %d, size %lu, to read %lld", __func__, cre->dir, size, cre->toread); diff --git a/usr.sbin/relayd/relay_udp.c b/usr.sbin/relayd/relay_udp.c index 75a53cdc56f..4782e4acbf5 100644 --- a/usr.sbin/relayd/relay_udp.c +++ b/usr.sbin/relayd/relay_udp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay_udp.c,v 1.27 2013/01/17 20:34:18 bluhm Exp $ */ +/* $OpenBSD: relay_udp.c,v 1.28 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -254,15 +254,6 @@ relay_udp_server(int fd, short sig, void *arg) con->se_in.dir = RELAY_DIR_REQUEST; con->se_out.dir = RELAY_DIR_RESPONSE; con->se_retry = rlay->rl_conf.dstretry; - - if (gettimeofday(&con->se_tv_start, NULL) == -1) { - free(con); - free(priv); - return; - } - - bcopy(&con->se_tv_start, &con->se_tv_last, sizeof(con->se_tv_last)); - bcopy(&ss, &con->se_in.ss, sizeof(con->se_in.ss)); con->se_out.port = rlay->rl_conf.dstport; switch (ss.ss_family) { case AF_INET: @@ -272,6 +263,10 @@ relay_udp_server(int fd, short sig, void *arg) con->se_in.port = ((struct sockaddr_in6 *)&ss)->sin6_port; break; } + bcopy(&ss, &con->se_in.ss, sizeof(con->se_in.ss)); + + getmonotime(&con->se_tv_start); + bcopy(&con->se_tv_start, &con->se_tv_last, sizeof(con->se_tv_last)); relay_sessions++; SPLAY_INSERT(session_tree, &rlay->rl_sessions, con); @@ -468,8 +463,7 @@ relay_dns_request(struct rsession *con) if (debug) relay_dns_log(con, buf, len); - if (gettimeofday(&con->se_tv_start, NULL) == -1) - return (-1); + getmonotime(&con->se_tv_start); if (!TAILQ_EMPTY(&rlay->rl_tables)) { if (relay_from_table(con) != 0) diff --git a/usr.sbin/relayd/relayd.c b/usr.sbin/relayd/relayd.c index 19d30d7f904..ce09304e7a1 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.115 2013/01/17 20:34:18 bluhm Exp $ */ +/* $OpenBSD: relayd.c,v 1.116 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -789,9 +789,7 @@ event_again(struct event *ev, int fd, short event, { struct timeval tv_next, tv_now, tv; - if (gettimeofday(&tv_now, NULL) == -1) - fatal("event_again: gettimeofday"); - + getmonotime(&tv_now); bcopy(end, &tv_next, sizeof(tv_next)); timersub(&tv_now, start, &tv_now); timersub(&tv_next, &tv_now, &tv_next); diff --git a/usr.sbin/relayd/relayd.h b/usr.sbin/relayd/relayd.h index 9a502648b44..e9c7f98003b 100644 --- a/usr.sbin/relayd/relayd.h +++ b/usr.sbin/relayd/relayd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.h,v 1.166 2013/03/09 14:43:06 bluhm Exp $ */ +/* $OpenBSD: relayd.h,v 1.167 2013/03/10 23:32:53 reyk Exp $ */ /* * Copyright (c) 2006 - 2012 Reyk Floeter @@ -945,7 +945,7 @@ const char *print_host(struct sockaddr_storage *, char *, size_t); const char *print_time(struct timeval *, struct timeval *, char *, size_t); const char *print_httperror(u_int); const char *printb_flags(const u_int32_t, const char *); - +void getmonotime(struct timeval *); /* pfe.c */ pid_t pfe(struct privsep *, struct privsep_proc *); -- cgit v1.2.3