| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
// Management of the peer's ack rate. |
| 8 |
|
| 9 |
use std::{cmp::max, time::Duration}; |
| 10 |
|
| 11 |
use neqo_common::qtrace; |
| 12 |
|
| 13 |
use crate::{ |
| 14 |
connection::params::ACK_RATIO_SCALE, frame::FrameType, packet::PacketBuilder, |
| 15 |
recovery::RecoveryToken, stats::FrameStats, tracking::DEFAULT_REMOTE_ACK_DELAY, |
| 16 |
}; |
| 17 |
|
| 18 |
#[derive(Debug, Clone)] |
| 19 |
pub struct AckRate { |
| 20 |
/// The maximum number of packets that can be received without sending an ACK. |
| 21 |
packets: usize, |
| 22 |
/// The maximum delay before sending an ACK. |
| 23 |
delay: Duration, |
| 24 |
} |
| 25 |
|
| 26 |
impl AckRate { |
| 27 |
pub fn new(minimum: Duration, ratio: u8, cwnd: usize, mtu: usize, rtt: Duration) -> Self { |
| 28 |
const PACKET_RATIO: usize = ACK_RATIO_SCALE as usize; |
| 29 |
// At worst, ask for an ACK for every other packet. |
| 30 |
const MIN_PACKETS: usize = 2; |
| 31 |
// At worst, require an ACK every 256 packets. |
| 32 |
const MAX_PACKETS: usize = 256; |
| 33 |
const RTT_RATIO: u32 = ACK_RATIO_SCALE as u32; |
| 34 |
const MAX_DELAY: Duration = Duration::from_millis(50); |
| 35 |
|
| 36 |
let packets = cwnd * PACKET_RATIO / mtu / usize::from(ratio); |
| 37 |
let packets = packets.clamp(MIN_PACKETS, MAX_PACKETS) - 1; |
| 38 |
let delay = rtt * RTT_RATIO / u32::from(ratio); |
| 39 |
let delay = delay.clamp(minimum, MAX_DELAY); |
| 40 |
qtrace!("AckRate inputs: {cwnd}/{mtu}/{ratio}, {rtt:?}"); |
| 41 |
Self { packets, delay } |
| 42 |
} |
| 43 |
|
| 44 |
pub fn write_frame(&self, builder: &mut PacketBuilder, seqno: u64) -> bool { |
| 45 |
builder.write_varint_frame(&[ |
| 46 |
u64::from(FrameType::AckFrequency), |
| 47 |
seqno, |
| 48 |
u64::try_from(self.packets + 1).expect("usize fits in u64"), |
| 49 |
u64::try_from(self.delay.as_micros()).unwrap_or(u64::MAX), |
| 50 |
0, |
| 51 |
]) |
| 52 |
} |
| 53 |
|
| 54 |
/// Determine whether to send an update frame. |
| 55 |
pub fn needs_update(&self, target: &Self) -> bool { |
| 56 |
if self.packets != target.packets { |
| 57 |
return true; |
| 58 |
} |
| 59 |
// Allow more flexibility for delays, as those can change |
| 60 |
// by small amounts fairly easily. |
| 61 |
let delta = target.delay / 4; |
| 62 |
target.delay + delta < self.delay || target.delay > self.delay + delta |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[derive(Debug, Clone)] |
| 67 |
pub struct FlexibleAckRate { |
| 68 |
current: AckRate, |
| 69 |
target: AckRate, |
| 70 |
next_frame_seqno: u64, |
| 71 |
frame_outstanding: bool, |
| 72 |
min_ack_delay: Duration, |
| 73 |
ratio: u8, |
| 74 |
} |
| 75 |
|
| 76 |
impl FlexibleAckRate { |
| 77 |
fn new( |
| 78 |
max_ack_delay: Duration, |
| 79 |
min_ack_delay: Duration, |
| 80 |
ratio: u8, |
| 81 |
cwnd: usize, |
| 82 |
mtu: usize, |
| 83 |
rtt: Duration, |
| 84 |
) -> Self { |
| 85 |
qtrace!("FlexibleAckRate: {max_ack_delay:?} {min_ack_delay:?} {ratio}"); |
| 86 |
let ratio = max(ACK_RATIO_SCALE, ratio); // clamp it |
| 87 |
Self { |
| 88 |
current: AckRate { |
| 89 |
packets: 1, |
| 90 |
delay: max_ack_delay, |
| 91 |
}, |
| 92 |
target: AckRate::new(min_ack_delay, ratio, cwnd, mtu, rtt), |
| 93 |
next_frame_seqno: 0, |
| 94 |
frame_outstanding: false, |
| 95 |
min_ack_delay, |
| 96 |
ratio, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
fn write_frames( |
| 101 |
&mut self, |
| 102 |
builder: &mut PacketBuilder, |
| 103 |
tokens: &mut Vec<RecoveryToken>, |
| 104 |
stats: &mut FrameStats, |
| 105 |
) { |
| 106 |
if !self.frame_outstanding |
| 107 |
&& self.current.needs_update(&self.target) |
| 108 |
&& self.target.write_frame(builder, self.next_frame_seqno) |
| 109 |
{ |
| 110 |
qtrace!("FlexibleAckRate: write frame {:?}", self.target); |
| 111 |
self.frame_outstanding = true; |
| 112 |
self.next_frame_seqno += 1; |
| 113 |
tokens.push(RecoveryToken::AckFrequency(self.target.clone())); |
| 114 |
stats.ack_frequency += 1; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fn frame_acked(&mut self, acked: &AckRate) { |
| 119 |
self.frame_outstanding = false; |
| 120 |
self.current = acked.clone(); |
| 121 |
} |
| 122 |
|
| 123 |
fn frame_lost(&mut self, _lost: &AckRate) { |
| 124 |
self.frame_outstanding = false; |
| 125 |
} |
| 126 |
|
| 127 |
fn update(&mut self, cwnd: usize, mtu: usize, rtt: Duration) { |
| 128 |
self.target = AckRate::new(self.min_ack_delay, self.ratio, cwnd, mtu, rtt); |
| 129 |
qtrace!("FlexibleAckRate: {:?} -> {:?}", self.current, self.target); |
| 130 |
} |
| 131 |
|
| 132 |
fn peer_ack_delay(&self) -> Duration { |
| 133 |
max(self.current.delay, self.target.delay) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
#[derive(Debug, Clone)] |
| 138 |
pub enum PeerAckDelay { |
| 139 |
Fixed(Duration), |
| 140 |
Flexible(FlexibleAckRate), |
| 141 |
} |
| 142 |
|
| 143 |
impl PeerAckDelay { |
| 144 |
pub const fn fixed(max_ack_delay: Duration) -> Self { |
| 145 |
Self::Fixed(max_ack_delay) |
| 146 |
} |
| 147 |
|
| 148 |
pub fn flexible( |
| 149 |
max_ack_delay: Duration, |
| 150 |
min_ack_delay: Duration, |
| 151 |
ratio: u8, |
| 152 |
cwnd: usize, |
| 153 |
mtu: usize, |
| 154 |
rtt: Duration, |
| 155 |
) -> Self { |
| 156 |
Self::Flexible(FlexibleAckRate::new( |
| 157 |
max_ack_delay, |
| 158 |
min_ack_delay, |
| 159 |
ratio, |
| 160 |
cwnd, |
| 161 |
mtu, |
| 162 |
rtt, |
| 163 |
)) |
| 164 |
} |
| 165 |
|
| 166 |
pub fn write_frames( |
| 167 |
&mut self, |
| 168 |
builder: &mut PacketBuilder, |
| 169 |
tokens: &mut Vec<RecoveryToken>, |
| 170 |
stats: &mut FrameStats, |
| 171 |
) { |
| 172 |
if let Self::Flexible(rate) = self { |
| 173 |
rate.write_frames(builder, tokens, stats); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
pub fn frame_acked(&mut self, r: &AckRate) { |
| 178 |
if let Self::Flexible(rate) = self { |
| 179 |
rate.frame_acked(r); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
pub fn frame_lost(&mut self, r: &AckRate) { |
| 184 |
if let Self::Flexible(rate) = self { |
| 185 |
rate.frame_lost(r); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
pub fn max(&self) -> Duration { |
| 190 |
match self { |
| 191 |
Self::Flexible(rate) => rate.peer_ack_delay(), |
| 192 |
Self::Fixed(delay) => *delay, |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
pub fn update(&mut self, cwnd: usize, mtu: usize, rtt: Duration) { |
| 197 |
if let Self::Flexible(rate) = self { |
| 198 |
rate.update(cwnd, mtu, rtt); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
impl Default for PeerAckDelay { |
| 204 |
fn default() -> Self { |
| 205 |
Self::fixed(DEFAULT_REMOTE_ACK_DELAY) |
| 206 |
} |
| 207 |
} |