xref: /openbsd-src/sbin/unwind/libunbound/util/rtt.c (revision 6d08cb1b6ef8df7f13d111d5d728770dbc3bc95a)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * util/rtt.c - UDP round trip time estimator for resend timeouts.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file contains a data type and functions to help estimate good
40ae8c6e27Sflorian  * round trip times for UDP resend timeout values.
41ae8c6e27Sflorian  */
42ae8c6e27Sflorian #include "config.h"
43ae8c6e27Sflorian #include "util/rtt.h"
44ae8c6e27Sflorian #include "iterator/iterator.h"
45ae8c6e27Sflorian 
46ae8c6e27Sflorian /* overwritten by config: infra_cache_min_rtt: */
47ae8c6e27Sflorian int RTT_MIN_TIMEOUT = 50;
48*6d08cb1bSflorian /* overwritten by config: infra_cache_max_rtt: */
49*6d08cb1bSflorian int RTT_MAX_TIMEOUT = 120000;
50*6d08cb1bSflorian 
51ae8c6e27Sflorian /** calculate RTO from rtt information */
52ae8c6e27Sflorian static int
calc_rto(const struct rtt_info * rtt)53ae8c6e27Sflorian calc_rto(const struct rtt_info* rtt)
54ae8c6e27Sflorian {
55ae8c6e27Sflorian 	/* From Stevens, Unix Network Programming, Vol1, 3rd ed., p.598 */
56ae8c6e27Sflorian 	int rto = rtt->srtt + 4*rtt->rttvar;
57ae8c6e27Sflorian 	if(rto < RTT_MIN_TIMEOUT)
58ae8c6e27Sflorian 		rto = RTT_MIN_TIMEOUT;
59ae8c6e27Sflorian 	if(rto > RTT_MAX_TIMEOUT)
60ae8c6e27Sflorian 		rto = RTT_MAX_TIMEOUT;
61ae8c6e27Sflorian 	return rto;
62ae8c6e27Sflorian }
63ae8c6e27Sflorian 
64ae8c6e27Sflorian void
rtt_init(struct rtt_info * rtt)65ae8c6e27Sflorian rtt_init(struct rtt_info* rtt)
66ae8c6e27Sflorian {
67ae8c6e27Sflorian 	rtt->srtt = 0;
68ae8c6e27Sflorian 	rtt->rttvar = UNKNOWN_SERVER_NICENESS/4;
69ae8c6e27Sflorian 	rtt->rto = calc_rto(rtt);
70ae8c6e27Sflorian 	/* default value from the book is 0 + 4*0.75 = 3 seconds */
71ae8c6e27Sflorian 	/* first RTO is 0 + 4*0.094 = 0.376 seconds */
72ae8c6e27Sflorian }
73ae8c6e27Sflorian 
74ae8c6e27Sflorian int
rtt_timeout(const struct rtt_info * rtt)75ae8c6e27Sflorian rtt_timeout(const struct rtt_info* rtt)
76ae8c6e27Sflorian {
77ae8c6e27Sflorian 	return rtt->rto;
78ae8c6e27Sflorian }
79ae8c6e27Sflorian 
80ae8c6e27Sflorian int
rtt_unclamped(const struct rtt_info * rtt)81ae8c6e27Sflorian rtt_unclamped(const struct rtt_info* rtt)
82ae8c6e27Sflorian {
83ae8c6e27Sflorian 	if(calc_rto(rtt) != rtt->rto) {
84ae8c6e27Sflorian 		/* timeout fallback has happened */
85ae8c6e27Sflorian 		return rtt->rto;
86ae8c6e27Sflorian 	}
87ae8c6e27Sflorian 	/* return unclamped value */
88ae8c6e27Sflorian 	return rtt->srtt + 4*rtt->rttvar;
89ae8c6e27Sflorian }
90ae8c6e27Sflorian 
91ae8c6e27Sflorian void
rtt_update(struct rtt_info * rtt,int ms)92ae8c6e27Sflorian rtt_update(struct rtt_info* rtt, int ms)
93ae8c6e27Sflorian {
94ae8c6e27Sflorian 	int delta = ms - rtt->srtt;
95ae8c6e27Sflorian 	rtt->srtt += delta / 8; /* g = 1/8 */
96ae8c6e27Sflorian 	if(delta < 0)
97ae8c6e27Sflorian 		delta = -delta; /* |delta| */
98ae8c6e27Sflorian 	rtt->rttvar += (delta - rtt->rttvar) / 4; /* h = 1/4 */
99ae8c6e27Sflorian 	rtt->rto = calc_rto(rtt);
100ae8c6e27Sflorian }
101ae8c6e27Sflorian 
102ae8c6e27Sflorian void
rtt_lost(struct rtt_info * rtt,int orig)103ae8c6e27Sflorian rtt_lost(struct rtt_info* rtt, int orig)
104ae8c6e27Sflorian {
105ae8c6e27Sflorian 	/* exponential backoff */
106ae8c6e27Sflorian 
107ae8c6e27Sflorian 	/* if a query succeeded and put down the rto meanwhile, ignore this */
108ae8c6e27Sflorian 	if(rtt->rto < orig)
109ae8c6e27Sflorian 		return;
110ae8c6e27Sflorian 
111ae8c6e27Sflorian 	/* the original rto is doubled, not the current one to make sure
112ae8c6e27Sflorian 	 * that the values in the cache are not increased by lots of
113ae8c6e27Sflorian 	 * queries simultaneously as they time out at the same time */
114ae8c6e27Sflorian 	orig *= 2;
115ae8c6e27Sflorian 	if(rtt->rto <= orig) {
116ae8c6e27Sflorian 		rtt->rto = orig;
117ae8c6e27Sflorian 		if(rtt->rto > RTT_MAX_TIMEOUT)
118ae8c6e27Sflorian 			rtt->rto = RTT_MAX_TIMEOUT;
119ae8c6e27Sflorian 	}
120ae8c6e27Sflorian }
121ae8c6e27Sflorian 
rtt_notimeout(const struct rtt_info * rtt)122ae8c6e27Sflorian int rtt_notimeout(const struct rtt_info* rtt)
123ae8c6e27Sflorian {
124ae8c6e27Sflorian 	return calc_rto(rtt);
125ae8c6e27Sflorian }
126