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