Skip to content

Congestion control

Source files: 4 · checked against shinku-quic v0.1.0
  • shinku-quic/shinku-quic/src/congestion/mod.rs
  • shinku-quic/shinku-quic/src/congestion/brutal.rs
  • shinku-quic/shinku-quic/src/congestion/negotiated.rs
  • shinku-quic/shinku-quic/tests/brutal.rs

quinn fixes a connection’s congestion controller when the connection is created, from the transport configuration’s ControllerFactory. Hysteria agrees on a send rate later, in its authentication request over the connection, and then wants to send at exactly that rate. shinku_quic::congestion bridges the two: Brutal sends at a fixed rate, Fallback names one of quinn’s own controllers, and Negotiated builds a controller that runs the fallback until a switch names a rate.

  • Send at a configured rate, through loss, without probing the path.
  • Run quinn’s Cubic, NewReno or BBR when no rate is agreed.
  • Move one connection between the two at any time, including on paths quinn creates later for migration.

Which rate applies, and when, is the protocol’s decision; in Etemenanki that is protocols/src/hysteria/negotiate.rs.

shinku-quic/src/congestion
pub enum Fallback { #[default] Cubic, NewReno, Bbr }
impl Fallback {
pub fn factory(self) -> Arc<dyn ControllerFactory + Send + Sync>; // quinn's default tuning
}
pub struct BrutalConfig { pub rate: Bandwidth, pub loss_compensation: bool }
impl BrutalConfig { pub const fn new(rate: Bandwidth) -> Self } // compensation on
impl ControllerFactory for BrutalConfig {}
pub struct Brutal { /* … */ }
impl Brutal {
pub fn new(config: BrutalConfig, now: Instant, mtu: u16) -> Self;
pub fn config(&self) -> BrutalConfig;
pub fn ack_rate(&self) -> f64;
}
impl Controller for Brutal {}
pub struct Negotiated { /* fallback, its factory, the switch */ }
impl Negotiated {
pub fn new(fallback: Fallback) -> (Arc<Self>, CongestionSwitch);
}
impl ControllerFactory for Negotiated {}
#[derive(Clone, Default)]
pub struct CongestionSwitch(Arc<SwitchState>);
impl CongestionSwitch {
pub fn use_brutal(&self, config: BrutalConfig);
pub fn use_fallback(&self);
pub fn current(&self) -> Option<BrutalConfig>;
}
pub struct NegotiatedController { /* … */ }
impl NegotiatedController { pub fn brutal(&self) -> Option<BrutalConfig> }
impl Controller for NegotiatedController {}

Brutal is a port of Hysteria’s core/internal/congestion/brutal/brutal.go. It does not probe for bandwidth: it is told the rate and sends at it.

Part Rule
Pacing metrics().pacing_rate is Some(rate / ack_rate × 8), in bits per second as quinn documents the field. The patched pacer releases at that rate; see The quinn patches.
Window max(rate × max(2 × srtt, srtt + 25 ms) / ack_rate, 2 × mtu). Before the first RTT sample, max(14 400, 2 × mtu) bytes.
Loss ack_rate is the share of sampled bytes the peer acknowledged over the last five one-second slots. Below 60 000 sampled bytes it is 1; it never goes below 0.8.
Loss compensation off ack_rate stays 1, so the sender sends exactly rate whatever the path drops (Hysteria’s disableLossCompensation).
ECN A congestion event with zero lost bytes, which is how an ECN mark arrives, changes nothing.

The window is twice the bandwidth-delay product so that packets the path dropped, which quinn counts as in flight until it declares them lost, never stall the sender; the pacer, not the window, sets the rate. Hysteria’s window alone would starve a short path, holding less than what is sent while the peer delays its acknowledgement, so the window is never smaller than the rate times the RTT plus the 25 ms acknowledgement delay. From 25 ms of RTT up, Hysteria’s formula is the larger.

Dividing by ack_rate over-sends to cover loss, by at most a quarter (1 / 0.8). A Brutal sender therefore keeps many packets in flight through loss, and its receiver holds many holes in each stream at once; the second quinn-proto patch is the receiving side of Brutal.

Fallback::factory builds quinn’s CubicConfig, NewRenoConfig or BbrConfig with their defaults. Cubic is the default, as in quinn; Hysteria calls NewReno reno; quinn marks its BBR experimental.

Negotiated::new(fallback) returns a factory and the switch that drives it. Every controller the factory builds starts from the switch’s current state:

sequenceDiagram
    participant P as Protocol
    participant S as CongestionSwitch
    participant C as NegotiatedController
    P->>C: built by Negotiated, running the fallback
    Note over P: Hysteria /auth agrees a rate
    P->>S: use_brutal(BrutalConfig)
    S-->>S: store config, bump generation
    C->>S: next on_sent, on_ack or on_congestion_event reads the generation
    C-->>C: replace inner with Brutal at the rate
  • The switch keeps an AtomicU64 generation beside a mutex-guarded Option<BrutalConfig>. A controller compares the generation at each on_sent, on_ack, on_end_acks and on_congestion_event, and takes the lock only when it moved. A change reaches a live connection at its next send or acknowledgement.
  • On a change, the controller replaces its inner controller: a new Brutal at the new configuration, or a fresh fallback. A new rate builds a new Brutal.
  • A controller built after the switch moved starts where the connection already is. quinn builds a fresh controller from the factory when a connection migrates to a new path, so a migrated connection stays on Brutal.
  • use_fallback moves the connection back.
  • One factory per connection. The switch is shared by every controller its factory builds, so a factory must serve one connection only. Give each connection its own transport configuration: on a server, quinn::Incoming::accept_with with a configuration built for that connection. Etemenanki builds one per client connection (negotiated_client_config) and one per accepted server connection (endpoint::for_connection).
  • Brutal only on the patched stack. On a stock quinn-proto, the pacer would release the 2×BDP window at 1.25 × window / RTT, about 2.5 times the rate, and a lossy path would kill the connection with too many reassembly gaps at the receiver.
  • The rate is the protocol’s. Nothing here chooses or caps a rate; the protocol hands BrutalConfig to the switch.
Test Shows
brutal.rs unit tests The window is twice the BDP; the ack rate’s 0.8 floor; compensation off keeps the rate exact; samples older than five seconds are forgotten
negotiated.rs unit tests A switch reaches the controller at its next event; a controller built after the switch (migration) and clones follow it; a new rate rebuilds Brutal
tests/brutal.rs brutal_sends_at_its_rate_not_at_its_window 20 Mbit/s over a loopback relay with 20 ms of delay each way arrives within 15% of the rate
tests/brutal.rs brutal_keeps_its_rate_through_loss 40 Mbit/s with 3% downstream loss is received at 0.8 to 1.15 times the rate, and the connection stays open
tests/brutal.rs a_negotiated_connection_moves_to_brutal_and_keeps_it_when_it_migrates The switch moves a live connection, and a migration keeps it on Brutal

The integration tests use the loopback relay in tests/support/mod.rs, which adds delay and loss; the test profile builds with opt-level = 2 so the rate measurements are not CPU-bound.