xref: /openbsd-src/usr.sbin/unbound/util/rtt.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * 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 #include "util/log.h"
45 
46 /** calculate RTO from rtt information */
47 static int
48 calc_rto(const struct rtt_info* rtt)
49 {
50 	/* From Stevens, Unix Network Programming, Vol1, 3rd ed., p.598 */
51 	int rto = rtt->srtt + 4*rtt->rttvar;
52 	if(rto < RTT_MIN_TIMEOUT)
53 		rto = RTT_MIN_TIMEOUT;
54 	if(rto > RTT_MAX_TIMEOUT)
55 		rto = RTT_MAX_TIMEOUT;
56 	return rto;
57 }
58 
59 void
60 rtt_init(struct rtt_info* rtt)
61 {
62 	rtt->srtt = 0;
63 	rtt->rttvar = 94;
64 	rtt->rto = calc_rto(rtt);
65 	/* default value from the book is 0 + 4*0.75 = 3 seconds */
66 	/* first RTO is 0 + 4*0.094 = 0.376 seconds */
67 }
68 
69 int
70 rtt_timeout(const struct rtt_info* rtt)
71 {
72 	return rtt->rto;
73 }
74 
75 int
76 rtt_unclamped(const struct rtt_info* rtt)
77 {
78 	if(calc_rto(rtt) != rtt->rto) {
79 		/* timeout fallback has happened */
80 		return rtt->rto;
81 	}
82 	/* return unclamped value */
83 	return rtt->srtt + 4*rtt->rttvar;
84 }
85 
86 void
87 rtt_update(struct rtt_info* rtt, int ms)
88 {
89 	int delta = ms - rtt->srtt;
90 	rtt->srtt += delta / 8; /* g = 1/8 */
91 	if(delta < 0)
92 		delta = -delta; /* |delta| */
93 	rtt->rttvar += (delta - rtt->rttvar) / 4; /* h = 1/4 */
94 	rtt->rto = calc_rto(rtt);
95 }
96 
97 void
98 rtt_lost(struct rtt_info* rtt, int orig)
99 {
100 	/* exponential backoff */
101 
102 	/* if a query succeeded and put down the rto meanwhile, ignore this */
103 	if(rtt->rto < orig)
104 		return;
105 
106 	/* the original rto is doubled, not the current one to make sure
107 	 * that the values in the cache are not increased by lots of
108 	 * queries simultaneously as they time out at the same time */
109 	orig *= 2;
110 	if(rtt->rto <= orig) {
111 		rtt->rto = orig;
112 		if(rtt->rto > RTT_MAX_TIMEOUT)
113 			rtt->rto = RTT_MAX_TIMEOUT;
114 	}
115 }
116 
117 int rtt_notimeout(const struct rtt_info* rtt)
118 {
119 	return calc_rto(rtt);
120 }
121