| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"math" |
| 8 |
"sync" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
const ( |
| 13 |
// RTO.Initial in msec. |
| 14 |
rtoInitial float64 = 1.0 * 1000 |
| 15 |
|
| 16 |
// RTO.Min in msec. |
| 17 |
rtoMin float64 = 1.0 * 1000 |
| 18 |
|
| 19 |
// RTO.Max in msec. |
| 20 |
defaultRTOMax float64 = 60.0 * 1000 |
| 21 |
|
| 22 |
// RTO.Alpha. |
| 23 |
rtoAlpha float64 = 0.125 |
| 24 |
|
| 25 |
// RTO.Beta. |
| 26 |
rtoBeta float64 = 0.25 |
| 27 |
|
| 28 |
// Max.Init.Retransmits. |
| 29 |
maxInitRetrans uint = 8 |
| 30 |
|
| 31 |
// Path.Max.Retrans. |
| 32 |
pathMaxRetrans uint = 5 |
| 33 |
|
| 34 |
noMaxRetrans uint = 0 |
| 35 |
) |
| 36 |
|
| 37 |
// rtoManager manages Rtx timeout values. |
| 38 |
// This is an implementation of RFC 4960 sec 6.3.1. |
| 39 |
type rtoManager struct { |
| 40 |
srtt float64 |
| 41 |
rttvar float64 |
| 42 |
rto float64 |
| 43 |
noUpdate bool |
| 44 |
mutex sync.RWMutex |
| 45 |
rtoMax float64 |
| 46 |
} |
| 47 |
|
| 48 |
// newRTOManager creates a new rtoManager. |
| 49 |
func newRTOManager(rtoMax float64) *rtoManager { |
| 50 |
mgr := rtoManager{ |
| 51 |
rto: rtoInitial, |
| 52 |
rtoMax: rtoMax, |
| 53 |
} |
| 54 |
if mgr.rtoMax == 0 { |
| 55 |
mgr.rtoMax = defaultRTOMax |
| 56 |
} |
| 57 |
|
| 58 |
return &mgr |
| 59 |
} |
| 60 |
|
| 61 |
// setNewRTT takes a newly measured RTT then adjust the RTO in msec. |
| 62 |
func (m *rtoManager) setNewRTT(rtt float64) float64 { |
| 63 |
m.mutex.Lock() |
| 64 |
defer m.mutex.Unlock() |
| 65 |
|
| 66 |
if m.noUpdate { |
| 67 |
return m.srtt |
| 68 |
} |
| 69 |
|
| 70 |
if m.srtt == 0 { |
| 71 |
// First measurement |
| 72 |
m.srtt = rtt |
| 73 |
m.rttvar = rtt / 2 |
| 74 |
} else { |
| 75 |
// Subsequent rtt measurement |
| 76 |
m.rttvar = (1-rtoBeta)*m.rttvar + rtoBeta*(math.Abs(m.srtt-rtt)) |
| 77 |
m.srtt = (1-rtoAlpha)*m.srtt + rtoAlpha*rtt |
| 78 |
} |
| 79 |
m.rto = math.Min(math.Max(m.srtt+4*m.rttvar, rtoMin), m.rtoMax) |
| 80 |
|
| 81 |
return m.srtt |
| 82 |
} |
| 83 |
|
| 84 |
// getRTO simply returns the current RTO in msec. |
| 85 |
func (m *rtoManager) getRTO() float64 { |
| 86 |
m.mutex.RLock() |
| 87 |
defer m.mutex.RUnlock() |
| 88 |
|
| 89 |
return m.rto |
| 90 |
} |
| 91 |
|
| 92 |
// reset resets the RTO variables to the initial values. |
| 93 |
func (m *rtoManager) reset() { |
| 94 |
m.mutex.Lock() |
| 95 |
defer m.mutex.Unlock() |
| 96 |
|
| 97 |
if m.noUpdate { |
| 98 |
return |
| 99 |
} |
| 100 |
|
| 101 |
m.srtt = 0 |
| 102 |
m.rttvar = 0 |
| 103 |
m.rto = rtoInitial |
| 104 |
} |
| 105 |
|
| 106 |
// set RTO value for testing. |
| 107 |
func (m *rtoManager) setRTO(rto float64, noUpdate bool) { |
| 108 |
m.mutex.Lock() |
| 109 |
defer m.mutex.Unlock() |
| 110 |
|
| 111 |
m.rto = rto |
| 112 |
m.noUpdate = noUpdate |
| 113 |
} |
| 114 |
|
| 115 |
// rtxTimerObserver is the inteface to a timer observer. |
| 116 |
// NOTE: Observers MUST NOT call start() or stop() method on rtxTimer |
| 117 |
// from within these callbacks. |
| 118 |
type rtxTimerObserver interface { |
| 119 |
onRetransmissionTimeout(timerID int, n uint) |
| 120 |
onRetransmissionFailure(timerID int) |
| 121 |
} |
| 122 |
|
| 123 |
type rtxTimerState uint8 |
| 124 |
|
| 125 |
const ( |
| 126 |
rtxTimerStopped rtxTimerState = iota |
| 127 |
rtxTimerStarted |
| 128 |
rtxTimerClosed |
| 129 |
) |
| 130 |
|
| 131 |
// rtxTimer provides the retnransmission timer conforms with RFC 4960 Sec 6.3.1. |
| 132 |
type rtxTimer struct { |
| 133 |
timer *time.Timer |
| 134 |
observer rtxTimerObserver |
| 135 |
id int |
| 136 |
maxRetrans uint |
| 137 |
rtoMax float64 |
| 138 |
mutex sync.Mutex |
| 139 |
rto float64 |
| 140 |
nRtos uint |
| 141 |
state rtxTimerState |
| 142 |
pending uint8 |
| 143 |
} |
| 144 |
|
| 145 |
// newRTXTimer creates a new retransmission timer. |
| 146 |
// if maxRetrans is set to 0, it will keep retransmitting until stop() is called. |
| 147 |
// (it will never make onRetransmissionFailure() callback. |
| 148 |
func newRTXTimer(id int, observer rtxTimerObserver, maxRetrans uint, |
| 149 |
rtoMax float64, |
| 150 |
) *rtxTimer { |
| 151 |
timer := rtxTimer{ |
| 152 |
id: id, |
| 153 |
observer: observer, |
| 154 |
maxRetrans: maxRetrans, |
| 155 |
rtoMax: rtoMax, |
| 156 |
} |
| 157 |
if timer.rtoMax == 0 { |
| 158 |
timer.rtoMax = defaultRTOMax |
| 159 |
} |
| 160 |
timer.timer = time.AfterFunc(math.MaxInt64, timer.timeout) |
| 161 |
timer.timer.Stop() |
| 162 |
|
| 163 |
return &timer |
| 164 |
} |
| 165 |
|
| 166 |
func (t *rtxTimer) calculateNextTimeout() time.Duration { |
| 167 |
timeout := calculateNextTimeout(t.rto, t.nRtos, t.rtoMax) |
| 168 |
|
| 169 |
return time.Duration(timeout) * time.Millisecond |
| 170 |
} |
| 171 |
|
| 172 |
func (t *rtxTimer) timeout() { |
| 173 |
t.mutex.Lock() |
| 174 |
if t.pending--; t.pending == 0 && t.state == rtxTimerStarted { |
| 175 |
if t.nRtos++; t.maxRetrans == 0 || t.nRtos <= t.maxRetrans { |
| 176 |
t.timer.Reset(t.calculateNextTimeout()) |
| 177 |
t.pending++ |
| 178 |
defer t.observer.onRetransmissionTimeout(t.id, t.nRtos) |
| 179 |
} else { |
| 180 |
t.state = rtxTimerStopped |
| 181 |
defer t.observer.onRetransmissionFailure(t.id) |
| 182 |
} |
| 183 |
} |
| 184 |
t.mutex.Unlock() |
| 185 |
} |
| 186 |
|
| 187 |
// start starts the timer. |
| 188 |
func (t *rtxTimer) start(rto float64) bool { |
| 189 |
t.mutex.Lock() |
| 190 |
defer t.mutex.Unlock() |
| 191 |
|
| 192 |
// this timer is already closed or aleady running |
| 193 |
if t.state != rtxTimerStopped { |
| 194 |
return false |
| 195 |
} |
| 196 |
|
| 197 |
// Note: rto value is intentionally not capped by RTO.Min to allow |
| 198 |
// fast timeout for the tests. Non-test code should pass in the |
| 199 |
// rto generated by rtoManager getRTO() method which caps the |
| 200 |
// value at RTO.Min or at RTO.Max. |
| 201 |
t.rto = rto |
| 202 |
t.nRtos = 0 |
| 203 |
t.state = rtxTimerStarted |
| 204 |
t.pending++ |
| 205 |
t.timer.Reset(t.calculateNextTimeout()) |
| 206 |
|
| 207 |
return true |
| 208 |
} |
| 209 |
|
| 210 |
// stop stops the timer. |
| 211 |
func (t *rtxTimer) stop() { |
| 212 |
t.mutex.Lock() |
| 213 |
defer t.mutex.Unlock() |
| 214 |
|
| 215 |
if t.state == rtxTimerStarted { |
| 216 |
if t.timer.Stop() { |
| 217 |
t.pending-- |
| 218 |
} |
| 219 |
t.state = rtxTimerStopped |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// closes the timer. this is similar to stop() but subsequent start() call |
| 224 |
// will fail (the timer is no longer usable). |
| 225 |
func (t *rtxTimer) close() { |
| 226 |
t.mutex.Lock() |
| 227 |
defer t.mutex.Unlock() |
| 228 |
|
| 229 |
if t.state == rtxTimerStarted && t.timer.Stop() { |
| 230 |
t.pending-- |
| 231 |
} |
| 232 |
t.state = rtxTimerClosed |
| 233 |
} |
| 234 |
|
| 235 |
// isRunning tests if the timer is running. |
| 236 |
// Debug purpose only. |
| 237 |
func (t *rtxTimer) isRunning() bool { |
| 238 |
t.mutex.Lock() |
| 239 |
defer t.mutex.Unlock() |
| 240 |
|
| 241 |
return t.state == rtxTimerStarted |
| 242 |
} |
| 243 |
|
| 244 |
func calculateNextTimeout(rto float64, nRtos uint, rtoMax float64) float64 { |
| 245 |
// RFC 4096 sec 6.3.3. Handle T3-rtx Expiration |
| 246 |
// E2) For the destination address for which the timer expires, set RTO |
| 247 |
// <- RTO * 2 ("back off the timer"). The maximum value discussed |
| 248 |
// in rule C7 above (RTO.max) may be used to provide an upper bound |
| 249 |
// to this doubling operation. |
| 250 |
if nRtos < 31 { |
| 251 |
m := 1 << nRtos |
| 252 |
|
| 253 |
return math.Min(rto*float64(m), rtoMax) |
| 254 |
} |
| 255 |
|
| 256 |
return rtoMax |
| 257 |
} |