xref: /openbsd-src/usr.sbin/unbound/util/rtt.h (revision d1e2768aaf01e4f25312df02afa53db1184689d2)
1933707f3Ssthen /*
2933707f3Ssthen  * util/rtt.h - UDP round trip time estimator for resend timeouts.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains a data type and functions to help estimate good
40933707f3Ssthen  * round trip times for UDP resend timeout values.
41933707f3Ssthen  */
42933707f3Ssthen 
43933707f3Ssthen #ifndef UTIL_RTT_H
44933707f3Ssthen #define UTIL_RTT_H
45933707f3Ssthen 
46933707f3Ssthen /**
47933707f3Ssthen  * RTT information. Keeps packet Round Trip Time.
48933707f3Ssthen  */
49933707f3Ssthen struct rtt_info {
50933707f3Ssthen 	/** smoothed rtt estimator, in milliseconds */
51933707f3Ssthen 	int srtt;
52933707f3Ssthen 	/** smoothed mean deviation, in milliseconds */
53933707f3Ssthen 	int rttvar;
54933707f3Ssthen 	/** current RTO in use, in milliseconds */
55933707f3Ssthen 	int rto;
56933707f3Ssthen };
57933707f3Ssthen 
58933707f3Ssthen /** min retransmit timeout value, in milliseconds */
59b2cdf21fSsthen extern int RTT_MIN_TIMEOUT;
60933707f3Ssthen /** max retransmit timeout value, in milliseconds */
61*d1e2768aSsthen extern int RTT_MAX_TIMEOUT;
62933707f3Ssthen 
63933707f3Ssthen /**
64933707f3Ssthen  * Initialize RTT estimators.
65933707f3Ssthen  * @param rtt: The structure. Caller is responsible for allocation of it.
66933707f3Ssthen  */
67933707f3Ssthen void rtt_init(struct rtt_info* rtt);
68933707f3Ssthen 
69933707f3Ssthen /**
70933707f3Ssthen  * Get timeout to use for sending a UDP packet.
71933707f3Ssthen  * @param rtt: round trip statistics structure.
72933707f3Ssthen  * @return: timeout to use in milliseconds. Relative time value.
73933707f3Ssthen  */
74933707f3Ssthen int rtt_timeout(const struct rtt_info* rtt);
75933707f3Ssthen 
76933707f3Ssthen /**
77933707f3Ssthen  * Get unclamped timeout to use for server selection.
78933707f3Ssthen  * Recent timeouts are reflected in the returned value.
79933707f3Ssthen  * @param rtt: round trip statistics structure.
80933707f3Ssthen  * @return: value to use in milliseconds.
81933707f3Ssthen  */
82933707f3Ssthen int rtt_unclamped(const struct rtt_info* rtt);
83933707f3Ssthen 
84933707f3Ssthen /**
85933707f3Ssthen  * RTT for valid responses. Without timeouts.
86933707f3Ssthen  * @param rtt: round trip statistics structure.
87933707f3Ssthen  * @return: value in msec.
88933707f3Ssthen  */
89933707f3Ssthen int rtt_notimeout(const struct rtt_info* rtt);
90933707f3Ssthen 
91933707f3Ssthen /**
92933707f3Ssthen  * Update the statistics with a new roundtrip estimate observation.
93933707f3Ssthen  * @param rtt: round trip statistics structure.
94933707f3Ssthen  * @param ms: estimate of roundtrip time in milliseconds.
95933707f3Ssthen  */
96933707f3Ssthen void rtt_update(struct rtt_info* rtt, int ms);
97933707f3Ssthen 
98933707f3Ssthen /**
994bfc71b0Ssthen  * Update the statistics with a new timeout expired observation.
100933707f3Ssthen  * @param rtt: round trip statistics structure.
101933707f3Ssthen  * @param orig: original rtt time given for the query that timed out.
102933707f3Ssthen  * 	Used to calculate the maximum responsible backed off time that
103933707f3Ssthen  * 	can reasonably be applied.
104933707f3Ssthen  */
105933707f3Ssthen void rtt_lost(struct rtt_info* rtt, int orig);
106933707f3Ssthen 
107933707f3Ssthen #endif /* UTIL_RTT_H */
108