The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
rtt.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: bba96d2ca177187efcb086c8e9f4cbe090fcdf28 $
20 *
21 * @brief Calculate smoothed round trip time and round trip time variation.
22 * @file util/rtt.h
23 *
24 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
25 */
26RCSIDH(rtt_h, "$Id: bba96d2ca177187efcb086c8e9f4cbe090fcdf28 $")
27
28#include <freeradius-devel/util/time.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/*
35 * We use *inverse* numbers to avoid numerical calculation issues.
36 *
37 * i.e. The bad way is to take two small numbers divide them by
38 * alpha / beta and then add them. That process can drop the
39 * lower digits. Instead, we take two small numbers, add them,
40 * and then divide the result by alpha / beta.
41 */
42#define FR_RTT_IBETA (4)
43#define FR_RTT_IALPHA (8)
44
45/** Smoothed round trip time, and the variation of the round trip time
46 *
47 */
48typedef struct {
49 fr_time_delta_t rtt; //!< smoothed round trip time
50 fr_time_delta_t rttvar; //!< RTT variation
51} fr_rtt_t;
52
53/** Absolute difference between the smoothed round trip time and a new sample
54 *
55 */
56#define FR_RTT_DIFF(_rtt, _t) \
57 (\
58 fr_time_delta_lt(_rtt, _t) ? \
59 fr_time_delta_sub(_t, _rtt) : \
60 fr_time_delta_sub(_rtt, _t)\
61 )
62
63/** Calculate the new RTT variation from the old values and a new sample
64 *
65 */
66#define FR_RTT_VAR(_rtt, _rttvar, _t) \
67 fr_time_delta_div(\
68 fr_time_delta_add(\
69 fr_time_delta_mul(_rttvar, FR_RTT_IBETA - 1), \
70 FR_RTT_DIFF(_rtt, _t)\
71 ), \
72 fr_time_delta_wrap(FR_RTT_IBETA)\
73 )
74
75/** Calculate the new smoothed round trip time from the old value and a new sample
76 *
77 */
78#define FR_RTT_SMOOTH(_old, _new) fr_time_delta_wrap((fr_time_delta_unwrap(_new) + (fr_time_delta_unwrap(_old) * (FR_RTT_IALPHA - 1))) / FR_RTT_IALPHA)
79
80/** Calculate the retransmission timeout as SRTT + (K * RTTVAR)
81 *
82 * @param[in] _rtt pointer to the #fr_rtt_t.
83 * @param[in] _k integer multiplier for the RTT variation.
84 * @return the retransmission timeout, as an #fr_time_delta_t.
85 */
86#define FR_RTT_RTO(_rtt, _k) \
87 fr_time_delta_add((_rtt)->rtt, fr_time_delta_mul((_rtt)->rttvar, (_k)))
88
89/** Update the smoothed round trip time and the variation with a new sample
90 *
91 * The variation depends on the old RTT, so it is updated first.
92 *
93 * @param[in,out] r to update.
94 * @param[in] t new round trip time sample.
95 */
96static inline void fr_rtt_update(fr_rtt_t *r, fr_time_delta_t t)
97{
98 r->rttvar = FR_RTT_VAR(r->rtt, r->rttvar, t);
99 r->rtt = FR_RTT_SMOOTH(r->rtt, t);
100}
101
102#ifdef __cplusplus
103}
104#endif
#define RCSIDH(h, id)
Definition build.h:610
#define FR_RTT_SMOOTH(_old, _new)
Calculate the new smoothed round trip time from the old value and a new sample.
Definition rtt.h:78
fr_time_delta_t rttvar
RTT variation.
Definition rtt.h:50
fr_time_delta_t rtt
smoothed round trip time
Definition rtt.h:49
static void fr_rtt_update(fr_rtt_t *r, fr_time_delta_t t)
Update the smoothed round trip time and the variation with a new sample.
Definition rtt.h:96
#define FR_RTT_VAR(_rtt, _rttvar, _t)
Calculate the new RTT variation from the old values and a new sample.
Definition rtt.h:66
Smoothed round trip time, and the variation of the round trip time.
Definition rtt.h:48
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80