1238106Sdes/*
2238106Sdes * util/rtt.h - UDP round trip time estimator for resend timeouts.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains a data type and functions to help estimate good
40238106Sdes * round trip times for UDP resend timeout values.
41238106Sdes */
42238106Sdes
43238106Sdes#ifndef UTIL_RTT_H
44238106Sdes#define UTIL_RTT_H
45238106Sdes
46238106Sdes/**
47238106Sdes * RTT information. Keeps packet Round Trip Time.
48238106Sdes */
49238106Sdesstruct rtt_info {
50238106Sdes	/** smoothed rtt estimator, in milliseconds */
51238106Sdes	int srtt;
52238106Sdes	/** smoothed mean deviation, in milliseconds */
53238106Sdes	int rttvar;
54238106Sdes	/** current RTO in use, in milliseconds */
55238106Sdes	int rto;
56238106Sdes};
57238106Sdes
58238106Sdes/** min retransmit timeout value, in milliseconds */
59285206Sdesextern int RTT_MIN_TIMEOUT;
60238106Sdes/** max retransmit timeout value, in milliseconds */
61238106Sdes#define RTT_MAX_TIMEOUT 120000
62238106Sdes
63238106Sdes/**
64238106Sdes * Initialize RTT estimators.
65238106Sdes * @param rtt: The structure. Caller is responsible for allocation of it.
66238106Sdes */
67238106Sdesvoid rtt_init(struct rtt_info* rtt);
68238106Sdes
69238106Sdes/**
70238106Sdes * Get timeout to use for sending a UDP packet.
71238106Sdes * @param rtt: round trip statistics structure.
72238106Sdes * @return: timeout to use in milliseconds. Relative time value.
73238106Sdes */
74238106Sdesint rtt_timeout(const struct rtt_info* rtt);
75238106Sdes
76238106Sdes/**
77238106Sdes * Get unclamped timeout to use for server selection.
78238106Sdes * Recent timeouts are reflected in the returned value.
79238106Sdes * @param rtt: round trip statistics structure.
80238106Sdes * @return: value to use in milliseconds.
81238106Sdes */
82238106Sdesint rtt_unclamped(const struct rtt_info* rtt);
83238106Sdes
84238106Sdes/**
85238106Sdes * RTT for valid responses. Without timeouts.
86238106Sdes * @param rtt: round trip statistics structure.
87238106Sdes * @return: value in msec.
88238106Sdes */
89238106Sdesint rtt_notimeout(const struct rtt_info* rtt);
90238106Sdes
91238106Sdes/**
92238106Sdes * Update the statistics with a new roundtrip estimate observation.
93238106Sdes * @param rtt: round trip statistics structure.
94238106Sdes * @param ms: estimate of roundtrip time in milliseconds.
95238106Sdes */
96238106Sdesvoid rtt_update(struct rtt_info* rtt, int ms);
97238106Sdes
98238106Sdes/**
99238106Sdes * Update the statistics with a new timout expired observation.
100238106Sdes * @param rtt: round trip statistics structure.
101238106Sdes * @param orig: original rtt time given for the query that timed out.
102238106Sdes * 	Used to calculate the maximum responsible backed off time that
103238106Sdes * 	can reasonably be applied.
104238106Sdes */
105238106Sdesvoid rtt_lost(struct rtt_info* rtt, int orig);
106238106Sdes
107238106Sdes#endif /* UTIL_RTT_H */
108