X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=utcp.c;h=6aa985f72701ee2eb0f6e4f1a3f9497cc6ddedd0;hb=97f8b67ac77d0623e536d302eb1e08666be7d411;hp=cf722e51d2afafd9008c179aea5ce70645cbd561;hpb=41112d3f0b47bf28cdf4b12798b4588c74005e46;p=utcp diff --git a/utcp.c b/utcp.c index cf722e5..6aa985f 100644 --- a/utcp.c +++ b/utcp.c @@ -27,8 +27,6 @@ #include #include #include -#include -#include #include #include "utcp_priv.h" @@ -45,15 +43,37 @@ #undef poll #endif -#ifndef timersub -#define timersub(a, b, r)\ - do {\ - (r)->tv_sec = (a)->tv_sec - (b)->tv_sec;\ - (r)->tv_usec = (a)->tv_usec - (b)->tv_usec;\ - if((r)->tv_usec < 0)\ - (r)->tv_sec--, (r)->tv_usec += USEC_PER_SEC;\ - } while (0) -#endif +static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) { + r->tv_sec = a->tv_sec - b->tv_sec; + r->tv_nsec = a->tv_nsec - b->tv_nsec; + + if(r->tv_nsec < 0) { + r->tv_sec--, r->tv_nsec += NSEC_PER_SEC; + } +} + +static int32_t timespec_diff_usec(const struct timespec *a, const struct timespec *b) { + int64_t diff = (a->tv_sec - b->tv_sec) * 1000000000 + a->tv_sec - b->tv_sec; + return diff / 1000; +} + +static bool timespec_lt(const struct timespec *a, const struct timespec *b) { + if(a->tv_sec == b->tv_sec) { + return a->tv_nsec < b->tv_nsec; + } else { + return a->tv_sec < b->tv_sec; + } +} + +static void timespec_clear(struct timespec *a) { + a->tv_sec = 0; +} + +static bool timespec_isset(const struct timespec *a) { + return a->tv_sec; +} + +static long CLOCK_GRANULARITY; // usec static inline size_t min(size_t a, size_t b) { return a < b ? a : b; @@ -70,6 +90,14 @@ static inline size_t max(size_t a, size_t b) { #define UTCP_DEBUG_DATALEN 20 #endif +#ifndef UTCP_CLOCK +#if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__) +#define UTCP_CLOCK CLOCK_MONOTONIC_RAW +#else +#define UTCP_CLOCK CLOCK_MONOTONIC +#endif +#endif + static void debug(struct utcp_connection *c, const char *format, ...) { struct timespec tv; char buf[1024]; @@ -140,7 +168,7 @@ static void set_state(struct utcp_connection *c, enum state state) { c->state = state; if(state == ESTABLISHED) { - timerclear(&c->conn_timeout); + timespec_clear(&c->conn_timeout); } debug(c, "state %s\n", strstate[state]); @@ -493,19 +521,27 @@ static void update_rtt(struct utcp_connection *c, uint32_t rtt) { } static void start_retransmit_timer(struct utcp_connection *c) { - gettimeofday(&c->rtrx_timeout, NULL); - c->rtrx_timeout.tv_usec += c->utcp->rto; + clock_gettime(UTCP_CLOCK, &c->rtrx_timeout); + + uint32_t rto = c->utcp->rto; + + while(rto > USEC_PER_SEC) { + c->rtrx_timeout.tv_sec++; + rto -= USEC_PER_SEC; + } + + c->rtrx_timeout.tv_nsec += c->utcp->rto * 1000; - while(c->rtrx_timeout.tv_usec >= 1000000) { - c->rtrx_timeout.tv_usec -= 1000000; + if(c->rtrx_timeout.tv_nsec >= NSEC_PER_SEC) { + c->rtrx_timeout.tv_nsec -= NSEC_PER_SEC; c->rtrx_timeout.tv_sec++; } - debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_usec); + debug(c, "rtrx_timeout %ld.%06lu\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_nsec); } static void stop_retransmit_timer(struct utcp_connection *c) { - timerclear(&c->rtrx_timeout); + timespec_clear(&c->rtrx_timeout); debug(c, "rtrx_timeout cleared\n"); } @@ -544,7 +580,7 @@ struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_re print_packet(c, "send", &pkt, sizeof(pkt)); utcp->send(utcp, &pkt, sizeof(pkt)); - gettimeofday(&c->conn_timeout, NULL); + clock_gettime(UTCP_CLOCK, &c->conn_timeout); c->conn_timeout.tv_sec += utcp->timeout; start_retransmit_timer(c); @@ -618,7 +654,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) { if(!c->rtt_start.tv_sec) { // Start RTT measurement - gettimeofday(&c->rtt_start, NULL); + clock_gettime(UTCP_CLOCK, &c->rtt_start); c->rtt_seq = pkt->hdr.seq + seglen; debug(c, "starting RTT measurement, expecting ack %u\n", c->rtt_seq); } @@ -713,14 +749,15 @@ ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) { if(!is_reliable(c)) { c->snd.una = c->snd.nxt = c->snd.last; buffer_discard(&c->sndbuf, c->sndbuf.used); + c->do_poll = true; } - if(is_reliable(c) && !timerisset(&c->rtrx_timeout)) { + if(is_reliable(c) && !timespec_isset(&c->rtrx_timeout)) { start_retransmit_timer(c); } - if(is_reliable(c) && !timerisset(&c->conn_timeout)) { - gettimeofday(&c->conn_timeout, NULL); + if(is_reliable(c) && !timespec_isset(&c->conn_timeout)) { + clock_gettime(UTCP_CLOCK, &c->conn_timeout); c->conn_timeout.tv_sec += c->utcp->timeout; } @@ -1415,10 +1452,10 @@ synack: // RTT measurement if(c->rtt_start.tv_sec) { if(c->rtt_seq == hdr.ack) { - struct timeval now, diff; - gettimeofday(&now, NULL); - timersub(&now, &c->rtt_start, &diff); - update_rtt(c, diff.tv_sec * 1000000 + diff.tv_usec); + struct timespec now; + clock_gettime(UTCP_CLOCK, &now); + int32_t diff = timespec_diff_usec(&now, &c->rtt_start); + update_rtt(c, diff); c->rtt_start.tv_sec = 0; } else if(c->rtt_seq < hdr.ack) { debug(c, "cancelling RTT measurement: %u < %u\n", c->rtt_seq, hdr.ack); @@ -1448,6 +1485,7 @@ synack: if(data_acked) { buffer_discard(&c->sndbuf, data_acked); + c->do_poll = true; } // Also advance snd.nxt if possible @@ -1490,7 +1528,7 @@ synack: case CLOSING: if(c->snd.una == c->snd.last) { - gettimeofday(&c->conn_timeout, NULL); + clock_gettime(UTCP_CLOCK, &c->conn_timeout); c->conn_timeout.tv_sec += utcp->timeout; set_state(c, TIME_WAIT); } @@ -1541,10 +1579,10 @@ synack: if(advanced) { if(c->snd.una == c->snd.last) { stop_retransmit_timer(c); - timerclear(&c->conn_timeout); + timespec_clear(&c->conn_timeout); } else if(is_reliable(c)) { start_retransmit_timer(c); - gettimeofday(&c->conn_timeout, NULL); + clock_gettime(UTCP_CLOCK, &c->conn_timeout); c->conn_timeout.tv_sec += utcp->timeout; } } @@ -1672,7 +1710,7 @@ skip_ack: break; case FIN_WAIT_2: - gettimeofday(&c->conn_timeout, NULL); + clock_gettime(UTCP_CLOCK, &c->conn_timeout); c->conn_timeout.tv_sec += utcp->timeout; set_state(c, TIME_WAIT); break; @@ -1803,7 +1841,7 @@ int utcp_shutdown(struct utcp_connection *c, int dir) { ack(c, false); - if(!timerisset(&c->rtrx_timeout)) { + if(!timespec_isset(&c->rtrx_timeout)) { start_retransmit_timer(c); } @@ -1921,10 +1959,10 @@ int utcp_abort(struct utcp_connection *c) { * The return value is the time to the next timeout in milliseconds, * or maybe a negative value if the timeout is infinite. */ -struct timeval utcp_timeout(struct utcp *utcp) { - struct timeval now; - gettimeofday(&now, NULL); - struct timeval next = {now.tv_sec + 3600, now.tv_usec}; +struct timespec utcp_timeout(struct utcp *utcp) { + struct timespec now; + clock_gettime(UTCP_CLOCK, &now); + struct timespec next = {now.tv_sec + 3600, now.tv_nsec}; for(int i = 0; i < utcp->nconnections; i++) { struct utcp_connection *c = utcp->connections[i]; @@ -1944,7 +1982,7 @@ struct timeval utcp_timeout(struct utcp *utcp) { continue; } - if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &now, <)) { + if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) { errno = ETIMEDOUT; c->state = CLOSED; @@ -1959,14 +1997,15 @@ struct timeval utcp_timeout(struct utcp *utcp) { continue; } - if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &now, <)) { + if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &now)) { debug(c, "retransmitting after timeout\n"); retransmit(c); } if(c->poll) { - if((c->state == ESTABLISHED || c->state == CLOSE_WAIT)) { - uint32_t len = buffer_free(&c->sndbuf); + if((c->state == ESTABLISHED || c->state == CLOSE_WAIT) && c->do_poll) { + c->do_poll = false; + uint32_t len = buffer_free(&c->sndbuf); if(len) { c->poll(c, len); @@ -1976,18 +2015,18 @@ struct timeval utcp_timeout(struct utcp *utcp) { } } - if(timerisset(&c->conn_timeout) && timercmp(&c->conn_timeout, &next, <)) { + if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &next)) { next = c->conn_timeout; } - if(timerisset(&c->rtrx_timeout) && timercmp(&c->rtrx_timeout, &next, <)) { + if(timespec_isset(&c->rtrx_timeout) && timespec_lt(&c->rtrx_timeout, &next)) { next = c->rtrx_timeout; } } - struct timeval diff; + struct timespec diff; - timersub(&next, &now, &diff); + timespec_sub(&next, &now, &diff); return diff; } @@ -2017,6 +2056,12 @@ struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_ return NULL; } + if(!CLOCK_GRANULARITY) { + struct timespec res; + clock_getres(UTCP_CLOCK, &res); + CLOCK_GRANULARITY = res.tv_sec * USEC_PER_SEC + res.tv_nsec / 1000; + } + utcp->accept = accept; utcp->pre_accept = pre_accept; utcp->send = send; @@ -2091,9 +2136,9 @@ void utcp_reset_timers(struct utcp *utcp) { return; } - struct timeval now, then; + struct timespec now, then; - gettimeofday(&now, NULL); + clock_gettime(UTCP_CLOCK, &now); then = now; @@ -2106,11 +2151,11 @@ void utcp_reset_timers(struct utcp *utcp) { continue; } - if(timerisset(&c->rtrx_timeout)) { + if(timespec_isset(&c->rtrx_timeout)) { c->rtrx_timeout = now; } - if(timerisset(&c->conn_timeout)) { + if(timespec_isset(&c->conn_timeout)) { c->conn_timeout = then; } @@ -2163,6 +2208,8 @@ void utcp_set_sndbuf(struct utcp_connection *c, size_t size) { if(c->sndbuf.maxsize != size) { c->sndbuf.maxsize = -1; } + + c->do_poll = buffer_free(&c->sndbuf); } size_t utcp_get_rcvbuf(struct utcp_connection *c) { @@ -2230,6 +2277,7 @@ void utcp_set_recv_cb(struct utcp_connection *c, utcp_recv_t recv) { void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) { if(c) { c->poll = poll; + c->do_poll = buffer_free(&c->sndbuf); } } @@ -2251,21 +2299,21 @@ void utcp_expect_data(struct utcp_connection *c, bool expect) { if(expect) { // If we expect data, start the connection timer. - if(!timerisset(&c->conn_timeout)) { - gettimeofday(&c->conn_timeout, NULL); + if(!timespec_isset(&c->conn_timeout)) { + clock_gettime(UTCP_CLOCK, &c->conn_timeout); c->conn_timeout.tv_sec += c->utcp->timeout; } } else { // If we want to cancel expecting data, only clear the timer when there is no unACKed data. if(c->snd.una == c->snd.last) { - timerclear(&c->conn_timeout); + timespec_clear(&c->conn_timeout); } } } void utcp_offline(struct utcp *utcp, bool offline) { - struct timeval now; - gettimeofday(&now, NULL); + struct timespec now; + clock_gettime(UTCP_CLOCK, &now); for(int i = 0; i < utcp->nconnections; i++) { struct utcp_connection *c = utcp->connections[i]; @@ -2277,7 +2325,7 @@ void utcp_offline(struct utcp *utcp, bool offline) { utcp_expect_data(c, offline); if(!offline) { - if(timerisset(&c->rtrx_timeout)) { + if(timespec_isset(&c->rtrx_timeout)) { c->rtrx_timeout = now; } @@ -2289,3 +2337,7 @@ void utcp_offline(struct utcp *utcp, bool offline) { utcp->rto = START_RTO; } } + +void utcp_set_clock_granularity(long granularity) { + CLOCK_GRANULARITY = granularity; +}