1b7579f77SDag-Erling Smørgrav /* 2b7579f77SDag-Erling Smørgrav * util/rtt.h - UDP round trip time estimator for resend timeouts. 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24*17d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25*17d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26*17d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27*17d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28*17d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29*17d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30*17d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31*17d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32*17d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33*17d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav 36b7579f77SDag-Erling Smørgrav /** 37b7579f77SDag-Erling Smørgrav * \file 38b7579f77SDag-Erling Smørgrav * 39b7579f77SDag-Erling Smørgrav * This file contains a data type and functions to help estimate good 40b7579f77SDag-Erling Smørgrav * round trip times for UDP resend timeout values. 41b7579f77SDag-Erling Smørgrav */ 42b7579f77SDag-Erling Smørgrav 43b7579f77SDag-Erling Smørgrav #ifndef UTIL_RTT_H 44b7579f77SDag-Erling Smørgrav #define UTIL_RTT_H 45b7579f77SDag-Erling Smørgrav 46b7579f77SDag-Erling Smørgrav /** 47b7579f77SDag-Erling Smørgrav * RTT information. Keeps packet Round Trip Time. 48b7579f77SDag-Erling Smørgrav */ 49b7579f77SDag-Erling Smørgrav struct rtt_info { 50b7579f77SDag-Erling Smørgrav /** smoothed rtt estimator, in milliseconds */ 51b7579f77SDag-Erling Smørgrav int srtt; 52b7579f77SDag-Erling Smørgrav /** smoothed mean deviation, in milliseconds */ 53b7579f77SDag-Erling Smørgrav int rttvar; 54b7579f77SDag-Erling Smørgrav /** current RTO in use, in milliseconds */ 55b7579f77SDag-Erling Smørgrav int rto; 56b7579f77SDag-Erling Smørgrav }; 57b7579f77SDag-Erling Smørgrav 58b7579f77SDag-Erling Smørgrav /** min retransmit timeout value, in milliseconds */ 59b7579f77SDag-Erling Smørgrav #define RTT_MIN_TIMEOUT 50 60b7579f77SDag-Erling Smørgrav /** max retransmit timeout value, in milliseconds */ 61b7579f77SDag-Erling Smørgrav #define RTT_MAX_TIMEOUT 120000 62b7579f77SDag-Erling Smørgrav 63b7579f77SDag-Erling Smørgrav /** 64b7579f77SDag-Erling Smørgrav * Initialize RTT estimators. 65b7579f77SDag-Erling Smørgrav * @param rtt: The structure. Caller is responsible for allocation of it. 66b7579f77SDag-Erling Smørgrav */ 67b7579f77SDag-Erling Smørgrav void rtt_init(struct rtt_info* rtt); 68b7579f77SDag-Erling Smørgrav 69b7579f77SDag-Erling Smørgrav /** 70b7579f77SDag-Erling Smørgrav * Get timeout to use for sending a UDP packet. 71b7579f77SDag-Erling Smørgrav * @param rtt: round trip statistics structure. 72b7579f77SDag-Erling Smørgrav * @return: timeout to use in milliseconds. Relative time value. 73b7579f77SDag-Erling Smørgrav */ 74b7579f77SDag-Erling Smørgrav int rtt_timeout(const struct rtt_info* rtt); 75b7579f77SDag-Erling Smørgrav 76b7579f77SDag-Erling Smørgrav /** 77b7579f77SDag-Erling Smørgrav * Get unclamped timeout to use for server selection. 78b7579f77SDag-Erling Smørgrav * Recent timeouts are reflected in the returned value. 79b7579f77SDag-Erling Smørgrav * @param rtt: round trip statistics structure. 80b7579f77SDag-Erling Smørgrav * @return: value to use in milliseconds. 81b7579f77SDag-Erling Smørgrav */ 82b7579f77SDag-Erling Smørgrav int rtt_unclamped(const struct rtt_info* rtt); 83b7579f77SDag-Erling Smørgrav 84b7579f77SDag-Erling Smørgrav /** 85b7579f77SDag-Erling Smørgrav * RTT for valid responses. Without timeouts. 86b7579f77SDag-Erling Smørgrav * @param rtt: round trip statistics structure. 87b7579f77SDag-Erling Smørgrav * @return: value in msec. 88b7579f77SDag-Erling Smørgrav */ 89b7579f77SDag-Erling Smørgrav int rtt_notimeout(const struct rtt_info* rtt); 90b7579f77SDag-Erling Smørgrav 91b7579f77SDag-Erling Smørgrav /** 92b7579f77SDag-Erling Smørgrav * Update the statistics with a new roundtrip estimate observation. 93b7579f77SDag-Erling Smørgrav * @param rtt: round trip statistics structure. 94b7579f77SDag-Erling Smørgrav * @param ms: estimate of roundtrip time in milliseconds. 95b7579f77SDag-Erling Smørgrav */ 96b7579f77SDag-Erling Smørgrav void rtt_update(struct rtt_info* rtt, int ms); 97b7579f77SDag-Erling Smørgrav 98b7579f77SDag-Erling Smørgrav /** 99b7579f77SDag-Erling Smørgrav * Update the statistics with a new timout expired observation. 100b7579f77SDag-Erling Smørgrav * @param rtt: round trip statistics structure. 101b7579f77SDag-Erling Smørgrav * @param orig: original rtt time given for the query that timed out. 102b7579f77SDag-Erling Smørgrav * Used to calculate the maximum responsible backed off time that 103b7579f77SDag-Erling Smørgrav * can reasonably be applied. 104b7579f77SDag-Erling Smørgrav */ 105b7579f77SDag-Erling Smørgrav void rtt_lost(struct rtt_info* rtt, int orig); 106b7579f77SDag-Erling Smørgrav 107b7579f77SDag-Erling Smørgrav #endif /* UTIL_RTT_H */ 108